1
2 #include <limits.h>
3 #include <stdint.h>
4 #include <stdlib.h>
5
6 #include "core.h"
7 #include "crypto_box.h"
8 #include "crypto_secretbox.h"
9 #include "private/common.h"
10 #include "utils.h"
11
12 int
crypto_box_detached_afternm(unsigned char * c,unsigned char * mac,const unsigned char * m,unsigned long long mlen,const unsigned char * n,const unsigned char * k)13 crypto_box_detached_afternm(unsigned char *c, unsigned char *mac,
14 const unsigned char *m, unsigned long long mlen,
15 const unsigned char *n, const unsigned char *k)
16 {
17 return crypto_secretbox_detached(c, mac, m, mlen, n, k);
18 }
19
20 int
crypto_box_detached(unsigned char * c,unsigned char * mac,const unsigned char * m,unsigned long long mlen,const unsigned char * n,const unsigned char * pk,const unsigned char * sk)21 crypto_box_detached(unsigned char *c, unsigned char *mac,
22 const unsigned char *m, unsigned long long mlen,
23 const unsigned char *n, const unsigned char *pk,
24 const unsigned char *sk)
25 {
26 unsigned char k[crypto_box_BEFORENMBYTES];
27 int ret;
28
29 COMPILER_ASSERT(crypto_box_BEFORENMBYTES >= crypto_secretbox_KEYBYTES);
30 if (crypto_box_beforenm(k, pk, sk) != 0) {
31 return -1;
32 }
33 ret = crypto_box_detached_afternm(c, mac, m, mlen, n, k);
34 sodium_memzero(k, sizeof k);
35
36 return ret;
37 }
38
39 int
crypto_box_easy_afternm(unsigned char * c,const unsigned char * m,unsigned long long mlen,const unsigned char * n,const unsigned char * k)40 crypto_box_easy_afternm(unsigned char *c, const unsigned char *m,
41 unsigned long long mlen, const unsigned char *n,
42 const unsigned char *k)
43 {
44 if (mlen > crypto_box_MESSAGEBYTES_MAX) {
45 sodium_misuse();
46 }
47 return crypto_box_detached_afternm(c + crypto_box_MACBYTES, c, m, mlen, n,
48 k);
49 }
50
51 int
crypto_box_easy(unsigned char * c,const unsigned char * m,unsigned long long mlen,const unsigned char * n,const unsigned char * pk,const unsigned char * sk)52 crypto_box_easy(unsigned char *c, const unsigned char *m,
53 unsigned long long mlen, const unsigned char *n,
54 const unsigned char *pk, const unsigned char *sk)
55 {
56 if (mlen > crypto_box_MESSAGEBYTES_MAX) {
57 sodium_misuse();
58 }
59 return crypto_box_detached(c + crypto_box_MACBYTES, c, m, mlen, n,
60 pk, sk);
61 }
62
63 int
crypto_box_open_detached_afternm(unsigned char * m,const unsigned char * c,const unsigned char * mac,unsigned long long clen,const unsigned char * n,const unsigned char * k)64 crypto_box_open_detached_afternm(unsigned char *m, const unsigned char *c,
65 const unsigned char *mac,
66 unsigned long long clen,
67 const unsigned char *n,
68 const unsigned char *k)
69 {
70 return crypto_secretbox_open_detached(m, c, mac, clen, n, k);
71 }
72
73 int
crypto_box_open_detached(unsigned char * m,const unsigned char * c,const unsigned char * mac,unsigned long long clen,const unsigned char * n,const unsigned char * pk,const unsigned char * sk)74 crypto_box_open_detached(unsigned char *m, const unsigned char *c,
75 const unsigned char *mac,
76 unsigned long long clen, const unsigned char *n,
77 const unsigned char *pk, const unsigned char *sk)
78 {
79 unsigned char k[crypto_box_BEFORENMBYTES];
80 int ret;
81
82 if (crypto_box_beforenm(k, pk, sk) != 0) {
83 return -1;
84 }
85 ret = crypto_box_open_detached_afternm(m, c, mac, clen, n, k);
86 sodium_memzero(k, sizeof k);
87
88 return ret;
89 }
90
91 int
crypto_box_open_easy_afternm(unsigned char * m,const unsigned char * c,unsigned long long clen,const unsigned char * n,const unsigned char * k)92 crypto_box_open_easy_afternm(unsigned char *m, const unsigned char *c,
93 unsigned long long clen, const unsigned char *n,
94 const unsigned char *k)
95 {
96 if (clen < crypto_box_MACBYTES) {
97 return -1;
98 }
99 return crypto_box_open_detached_afternm(m, c + crypto_box_MACBYTES, c,
100 clen - crypto_box_MACBYTES,
101 n, k);
102 }
103
104 int
crypto_box_open_easy(unsigned char * m,const unsigned char * c,unsigned long long clen,const unsigned char * n,const unsigned char * pk,const unsigned char * sk)105 crypto_box_open_easy(unsigned char *m, const unsigned char *c,
106 unsigned long long clen, const unsigned char *n,
107 const unsigned char *pk, const unsigned char *sk)
108 {
109 if (clen < crypto_box_MACBYTES) {
110 return -1;
111 }
112 return crypto_box_open_detached(m, c + crypto_box_MACBYTES, c,
113 clen - crypto_box_MACBYTES,
114 n, pk, sk);
115 }
116