forked from sustrik/dsock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nacl.c
201 lines (180 loc) · 6.99 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
/*
Copyright (c) 2016 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 <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tweetnacl/tweetnacl.h"
#include "iov.h"
#include "dsockimpl.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_msendv(struct msock_vfs *mvfs,
const struct iovec *iov, size_t iovlen, int64_t deadline);
static ssize_t nacl_mrecvv(struct msock_vfs *mvfs,
const struct iovec *iov, size_t iovlen, 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_start(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.msendv = nacl_msendv;
obj->mvfs.mrecvv = nacl_mrecvv;
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_done(int s) {
dsock_assert(0);
}
int nacl_stop(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_msendv(struct msock_vfs *mvfs,
const struct iovec *iov, size_t iovlen, int64_t deadline) {
struct nacl_sock *obj = dsock_cont(mvfs, struct nacl_sock, mvfs);
/* If needed, adjust the buffers. */
size_t len = iov_size(iov, iovlen);
int 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);
iov_copyallfrom(obj->buf1 + crypto_secretbox_ZEROBYTES, iov, iovlen);
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_mrecvv(struct msock_vfs *mvfs,
const struct iovec *iov, size_t iovlen, int64_t deadline) {
struct nacl_sock *obj = dsock_cont(mvfs, struct nacl_sock, mvfs);
/* If needed, adjust the buffers. */
size_t len = iov_size(iov, iovlen);
int 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. */
iov_copyto(iov, iovlen, obj->buf1 + crypto_secretbox_ZEROBYTES, 0,
clen - crypto_secretbox_ZEROBYTES);
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);
}