-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrypto_str_box.c
29 lines (28 loc) · 989 Bytes
/
crypto_str_box.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* Public Domain */
#include <crypto_box.h>
#include "stralloc.h"
#include "crypto_str.h"
int crypto_str_box(stralloc* c,
const stralloc* m,
const stralloc* n,
const stralloc* pk,
const stralloc* sk)
{
int i;
if (pk->len != crypto_box_PUBLICKEYBYTES) return 110;
if (sk->len != crypto_box_SECRETKEYBYTES) return 110;
if (n->len != crypto_box_NONCEBYTES) return 110;
unsigned int mlen = m->len + crypto_box_ZEROBYTES;
unsigned char mpad[mlen];
for (i = 0;i < crypto_box_ZEROBYTES;++i) mpad[i] = 0;
for (i = crypto_box_ZEROBYTES;i < mlen;++i) mpad[i] = m->s[i - crypto_box_ZEROBYTES];
unsigned char cpad[mlen];
if(crypto_box(cpad,mpad,mlen,
(const unsigned char *) n->s,
(const unsigned char *) pk->s,
(const unsigned char *) sk->s
)!=0) return 110;
if(!stralloc_copyb(c, (const char*)cpad+crypto_box_BOXZEROBYTES,
mlen-crypto_box_BOXZEROBYTES)) return 110;
return 0;
}