-
Notifications
You must be signed in to change notification settings - Fork 23
/
nacl.c
217 lines (196 loc) · 7.49 KB
/
nacl.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
Copyright (c) 2017 Martin Sustrik
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#include <errno.h>
#include <libdillimpl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tweetnacl/tweetnacl.h"
#include "dsock.h"
#include "iol.h"
#include "utils.h"
dsock_unique_id(nacl_type);
static void *nacl_hquery(struct hvfs *hvfs, const void *type);
static void nacl_hclose(struct hvfs *hvfs);
static int nacl_msendl(struct msock_vfs *mvfs,
struct iolist *first, struct iolist *last, int64_t deadline);
static ssize_t nacl_mrecvl(struct msock_vfs *mvfs,
struct iolist *first, struct iolist *last, int64_t deadline);
#define NACL_EXTRABYTES \
(crypto_secretbox_ZEROBYTES + crypto_secretbox_NONCEBYTES)
struct nacl_sock {
struct hvfs hvfs;
struct msock_vfs mvfs;
int s;
size_t buflen;
uint8_t *buf1;
uint8_t *buf2;
uint8_t key[crypto_secretbox_KEYBYTES];
uint8_t send_nonce[crypto_secretbox_NONCEBYTES];
uint8_t recv_nonce[crypto_secretbox_NONCEBYTES];
};
static void *nacl_hquery(struct hvfs *hvfs, const void *type) {
struct nacl_sock *obj = (struct nacl_sock*)hvfs;
if(type == msock_type) return &obj->mvfs;
if(type == nacl_type) return obj;
errno = ENOTSUP;
return NULL;
}
int nacl_attach(int s, const void *key, size_t keylen, int64_t deadline) {
int err;
if(dsock_slow(!key || keylen != crypto_secretbox_KEYBYTES)) {
err = EINVAL; goto error2;}
/* Check whether underlying socket is message-based. */
if(dsock_slow(!hquery(s, msock_type))) {err = errno; goto error1;}
/* Create the object. */
struct nacl_sock *obj = malloc(sizeof(struct nacl_sock));
if(dsock_slow(!obj)) {errno = ENOMEM; goto error1;}
obj->hvfs.query = nacl_hquery;
obj->hvfs.close = nacl_hclose;
obj->mvfs.msendl = nacl_msendl;
obj->mvfs.mrecvl = nacl_mrecvl;
obj->s = s;
obj->buflen = 0;
obj->buf1 = NULL;
obj->buf2 = NULL;
memcpy(obj->key, key, crypto_secretbox_KEYBYTES);
/* Generate random nonce for sending. */
int rc = dsock_random(obj->send_nonce, crypto_secretbox_NONCEBYTES,
deadline);
if(dsock_slow(rc != 0)) {err = errno; goto error2;}
/* Create the handle. */
int h = hmake(&obj->hvfs);
if(dsock_slow(h < 0)) {err = errno; goto error2;}
/* Make a private copy of the underlying socket. */
obj->s = hdup(s);
if(dsock_slow(obj->s < 0)) {err = errno; goto error3;}
rc = hclose(s);
dsock_assert(rc == 0);
return h;
error3:
rc = hclose(h);
dsock_assert(rc == 0);
error2:
free(obj);
error1:
errno = err;
return -1;
}
int nacl_detach(int s) {
struct nacl_sock *obj = hquery(s, nacl_type);
if(dsock_slow(!obj)) return -1;
free(obj->buf1);
free(obj->buf2);
int u = obj->s;
free(obj);
return u;
}
static int nacl_resizebufs(struct nacl_sock *obj, size_t len) {
if(dsock_slow(!obj->buf1 || obj->buflen < len)) {
obj->buflen = len;
obj->buf1 = realloc(obj->buf1, len);
if(dsock_slow(!obj->buf1)) {errno = ENOMEM; return -1;}
obj->buf2 = realloc(obj->buf2, len);
if(dsock_slow(!obj->buf2)) {errno = ENOMEM; return -1;}
}
return 0;
}
static int nacl_msendl(struct msock_vfs *mvfs,
struct iolist *first, struct iolist *last, int64_t deadline) {
struct nacl_sock *obj = dsock_cont(mvfs, struct nacl_sock, mvfs);
size_t len;
int rc = iol_check(first, last, NULL, &len);
if(dsock_slow(rc < 0)) return -1;
/* If needed, adjust the buffers. */
rc = nacl_resizebufs(obj, NACL_EXTRABYTES + len);
if(dsock_slow(rc < 0)) return -1;
/* Increase nonce. */
int i;
for(i = 0; i != sizeof(obj->send_nonce); ++i) {
obj->send_nonce[i]++;
if(obj->send_nonce[i]) break;
}
/* Encrypt and authenticate the message. */
size_t mlen = len + crypto_secretbox_ZEROBYTES;
memset(obj->buf1, 0, crypto_secretbox_ZEROBYTES);
uint8_t *pos = obj->buf1 + crypto_secretbox_ZEROBYTES;
struct iolist *it;
for(it = first; it; it = it->iol_next) {
memcpy(pos, it->iol_base, it->iol_len);
pos += it->iol_len;
}
crypto_secretbox(obj->buf2, obj->buf1, mlen, obj->send_nonce, obj->key);
/* Prepare the message: nonce + ciphertext */
memcpy(obj->buf1, obj->send_nonce, crypto_secretbox_NONCEBYTES);
memcpy(obj->buf1 + crypto_secretbox_NONCEBYTES,
obj->buf2 + crypto_secretbox_BOXZEROBYTES,
mlen - crypto_secretbox_BOXZEROBYTES);
/* Send the the encrypted message. */
return msend(obj->s, obj->buf1, crypto_secretbox_NONCEBYTES + mlen -
crypto_secretbox_BOXZEROBYTES , deadline);
}
static ssize_t nacl_mrecvl(struct msock_vfs *mvfs,
struct iolist *first, struct iolist *last, int64_t deadline) {
struct nacl_sock *obj = dsock_cont(mvfs, struct nacl_sock, mvfs);
size_t len;
int rc = iol_check(first, last, NULL, &len);
if(dsock_slow(rc < 0)) return -1;
/* If needed, adjust the buffers. */
rc = nacl_resizebufs(obj, NACL_EXTRABYTES + len);
/* Read the encrypted message. */
ssize_t sz = mrecv(obj->s, obj->buf1, NACL_EXTRABYTES + len, deadline);
if(dsock_slow(sz < 0)) return -1;
if(sz > NACL_EXTRABYTES + len) {errno = EMSGSIZE; return -1;}
/* Store the nonce. */
memcpy(obj->recv_nonce, obj->buf1, crypto_secretbox_NONCEBYTES);
/* Decrypt and authenticate the message. */
size_t clen = crypto_secretbox_BOXZEROBYTES +
(sz - crypto_secretbox_NONCEBYTES);
memset(obj->buf2, 0, crypto_secretbox_BOXZEROBYTES);
memcpy(obj->buf2 + crypto_secretbox_BOXZEROBYTES,
obj->buf1 + crypto_secretbox_NONCEBYTES,
clen - crypto_secretbox_BOXZEROBYTES);
rc = crypto_secretbox_open(obj->buf1, obj->buf2, clen,
obj->recv_nonce, obj->key);
if(dsock_slow(rc < 0)) {errno = EACCES; return -1;}
/* Copy the message into user's buffer. */
sz = clen - crypto_secretbox_ZEROBYTES;
uint8_t *pos = obj->buf1 + crypto_secretbox_ZEROBYTES;
struct iolist *it = first;
while(1) {
size_t tocopy = sz < it->iol_len ? sz : it->iol_len;
if(it->iol_base) memcpy(it->iol_base, pos, tocopy);
sz -= tocopy;
if(sz == 0) break;
pos += it->iol_len;
it = it->iol_next;
}
return clen - crypto_secretbox_ZEROBYTES;
}
static void nacl_hclose(struct hvfs *hvfs) {
struct nacl_sock *obj = (struct nacl_sock*)hvfs;
if(dsock_fast(obj->s >= 0)) {
int rc = hclose(obj->s);
dsock_assert(rc == 0);
}
free(obj->buf1);
free(obj->buf2);
free(obj);
}