-
Notifications
You must be signed in to change notification settings - Fork 2
/
seal.c
160 lines (129 loc) · 3.81 KB
/
seal.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <string.h>
#include <sodium.h>
#include <errno.h>
#include <time.h>
#include <assert.h>
#include "seal.h"
#include "base16.h"
int seal_init()
{
return sodium_init();
}
char *
seal_keygen()
{
char key[crypto_secretbox_KEYBYTES];
char *encoded;
randombytes_buf(key, crypto_secretbox_KEYBYTES);
encoded = malloc(b16elen(crypto_secretbox_KEYBYTES) + 1);
if (!encoded) return NULL;
b16e(encoded, key, crypto_secretbox_KEYBYTES);
encoded[b16elen(crypto_secretbox_KEYBYTES)] = '\0';
return encoded;
}
ssize_t
seal(char **nonce, char **sealed, unsigned long until, const char *text, size_t len, const char *key)
{
char *buf = NULL,
*_sealed = NULL;
char _nonce[crypto_secretbox_NONCEBYTES];
/* check yo-self */
assert(nonce != NULL);
assert(sealed != NULL);
assert(text != NULL);
assert(key != NULL);
assert(until < 0xffffffffff); /* we only want 40 bits */
assert(len < 4096); /* len has no business being astronomical */
/* initialize to sane defaults;
this makes free(3) calls simpler.
*/
*nonce = *sealed = NULL;
/* generate a random nonce (unencoded) */
randombytes_buf(_nonce, crypto_secretbox_NONCEBYTES);
memset(_nonce, 42, crypto_secretbox_NONCEBYTES);
/* encode the nonce under base-32 */
*nonce = malloc(ENCODED_NONCE_LEN);
if (!*nonce) goto fail;
b32e(*nonce, _nonce, sizeof(_nonce));
/* allocate the payload input buffer:
5 bytes (40-bits) for the "freshness" indicator;
$len bytes for the provided message contents; and
enough bytes for the message auth (MAC)
*/
buf = malloc(len+5+crypto_secretbox_MACBYTES);
if (!buf) goto fail;
/* encode the freshness indicator into the first 5 bytes */
buf[0] = ((0xff00000000 & until) >> 32);
buf[1] = ((0x00ff000000 & until) >> 24);
buf[2] = ((0x0000ff0000 & until) >> 16);
buf[3] = ((0x000000ff00 & until) >> 8);
buf[4] = ((0x00000000ff & until) );
/* copy the message text onto the end */
memcpy(buf+5, text, len);
/* allocate the encrypted output buffer (unencoded) */
_sealed = malloc(len+5+crypto_secretbox_MACBYTES);
if (!_sealed) goto fail;
/* allocate the encrypted output buffer (encoded) */
*sealed = malloc(b32elen(len+5+crypto_secretbox_MACBYTES));
if (!*sealed) goto fail;
/* encrypt! */
crypto_secretbox_easy(_sealed, buf, len+5, _nonce, key);
/* encode! */
b32e(*sealed, _sealed, len+5+crypto_secretbox_MACBYTES);
free(buf);
free(_sealed);
return b32elen(len+5+crypto_secretbox_MACBYTES);
fail:
free(buf);
free(_sealed);
free(*nonce);
free(*sealed);
return -1;
}
#define MAX_SEALED_BYTES 512
char *
unseal(const char *nonce, char *sealed, size_t len, const char *key)
{
char *text = NULL;
char buf[MAX_SEALED_BYTES];
char _nonce[crypto_secretbox_NONCEBYTES];
unsigned long notafter;
time_t now;
assert(nonce != NULL);
assert(sealed != NULL);
assert(key != NULL);
/* some length sanity checking */
if (b32dlen(len) < 6 || b32dlen(len) > MAX_SEALED_BYTES) goto fail;
/* decode the nonce */
b32d(_nonce, nonce, ENCODED_NONCE_LEN);
/* decode the input */
b32d(buf, sealed, len);
len = b32dlen(len);
/* decrypt the input */
if (crypto_secretbox_open_easy(buf, buf, len, _nonce, key) != 0) {
goto fail;
}
/* strip off the mac bytes */
len -= crypto_secretbox_MACBYTES;
/* check freshness */
notafter = (((unsigned long)buf[0] << 32) & 0xff00000000)
| (((unsigned long)buf[1] << 24) & 0x00ff000000)
| (((unsigned long)buf[2] << 16) & 0x0000ff0000)
| (((unsigned long)buf[3] << 8) & 0x000000ff00)
| (((unsigned long)buf[4] ) & 0x00000000ff);
now = time(NULL);
if (now < 0) goto fail;
if (now > notafter) {
errno = EINVAL;
goto fail;
}
/* extract the original payload */
text = malloc(len-5+1);
if (!text) goto fail;
memset(text, 0, len-5+1);
memcpy(text, buf+5, len-5);
return text;
fail:
free(text);
return NULL;
}