summaryrefslogtreecommitdiff
path: root/src/lib.c
blob: 001ff7d9304b0053eda126d2e7ea664d0c8f6def (plain) (blame)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <s.h>

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include "random.h"
#include "impl.h"

#include "lib.h"



int
tweetnacl_main(const int argc, const char *const *const argv) {
	int rc = EXIT_FAILURE;

	if (printf("%s %s %s\n", "NAME", "VERSION", "DATE") < 0) {
		perror("printf()");
		goto out;
	}

	for (int i = 0; i < argc; i++) {
		if (printf("argv[%i]: %s\n", i, argv[i]) < 0) {
			perror("printf()");
			goto out;
		}
	}

	rc = EXIT_SUCCESS;
out:
	return rc;
}



bool
crypt_verify(
	const unsigned char buffer1[crypt_verify_BYTES],
	const unsigned char buffer2[crypt_verify_BYTES]
) {
	return crypto_verify_16(buffer1, buffer2) == 0;
}

void
crypt_hash(
	const unsigned long long length,
	const unsigned char *data,
	      unsigned char out[crypt_hash_BYTES]
) {
	const int ret = crypto_hash(out, data, length);
	assert(ret == 0);
}

void
crypt_onetimeauth(
	const unsigned long long length,
	const unsigned char *const data,
	const unsigned char secret_key[crypt_onetimeauth_KEYBYTES],
	unsigned char authenticator_out[crypt_onetimeauth_BYTES]
) {
	const int ret = crypto_onetimeauth(
		authenticator_out,
		data,
		length,
		secret_key
	);
	assert(ret == 0);
}

bool
crypt_onetimeauth_verify(
	const unsigned char authenticator[crypt_onetimeauth_BYTES],
	const unsigned char secret_key[crypt_onetimeauth_KEYBYTES],
	const unsigned long long length,
	const unsigned char *const data
) {
	return crypto_onetimeauth_verify(
		authenticator,
		data,
		length,
		secret_key
	);
}

int
crypt_secretbox(
	const unsigned char secret_key[crypt_secretbox_KEYBYTES],
	const unsigned long long length,
	const unsigned char *const clear_data,
	      unsigned char *const cypher_out
) {
	for (int i = 0; i < crypt_secretbox_ZEROBYTES; i++) {
		if (clear_data[i] != 0) {
			return -2;
		}
	}

	assert(length >= 32);
	unsigned char nonce[crypt_secretbox_NONCEBYTES];
	random_bytes(nonce, crypt_secretbox_NONCEBYTES);
	const int ret = crypto_secretbox(
		cypher_out,
		clear_data,
		length,
		nonce,
		secret_key
	);
	assert(ret == 0);
	return 0;
}

int
crypt_secretbox_open(
	const unsigned char secret_key[crypt_secretbox_KEYBYTES],
	const unsigned long long length,
	const unsigned char *const cypher_data,
	      unsigned char *const clean_out
) {
	for (int i = 0; i < crypt_secretbox_BOXZEROBYTES; i++) {
		if (cypher_data[i] != 0) {
			return -2;
		}
	}

	unsigned char nonce[crypt_secretbox_NONCEBYTES];
	random_bytes(nonce, crypt_secretbox_NONCEBYTES);
	return crypto_secretbox_open(
		clean_out,
		cypher_data,
		length,
		nonce,
		secret_key
	);
}

void
crypt_sign_keypair(
	unsigned char public_key_out[crypt_sign_PUBLICKEYBYTES],
	unsigned char secret_key_out[crypt_sign_SECRETKEYBYTES]
) {
	const int ret = crypto_sign_keypair(public_key_out, secret_key_out);
	assert(ret == 0);
}

void
crypt_sign(
	const unsigned char secret_key[crypt_sign_SECRETKEYBYTES],
	const unsigned long long length,
	const unsigned char *const data,
	      unsigned long long *const outlen,
	      unsigned char *const signed_out
) {
	const int ret = crypto_sign(
		signed_out,
		outlen,
		data,
		length,
		secret_key
	);
	assert(ret == 0);
}

int
crypt_sign_open(
	const unsigned char public_key[crypt_sign_PUBLICKEYBYTES],
	const unsigned long long signed_message_length,
	const unsigned char *const signed_message,
	      unsigned long long *const inoutlen,
	      unsigned char *const out
) {
	assert(signed_message_length >= crypt_sign_BYTES);
	assert(*inoutlen == signed_message_length);
	return crypto_sign_open(
		out,
		inoutlen,
		signed_message,
		signed_message_length,
		public_key
	);
}

void
crypt_box_keypair(
	unsigned char public_key_out[crypt_box_PUBLICKEYBYTES],
	unsigned char secret_key_out[crypt_box_SECRETKEYBYTES]
) {
	const int ret = crypto_box_keypair(public_key_out, secret_key_out);
	assert(ret == 0);
}

int
crypt_box(
	const unsigned char receiver_public_key[crypt_box_PUBLICKEYBYTES],
	const unsigned char sender_secret_key[crypt_box_PUBLICKEYBYTES],
	const unsigned long long length,
	const unsigned char *const clear_data,
	      unsigned char *const cypher_out
) {
	for (int i = 0U; i < crypt_box_ZEROBYTES; i++) {
		if (clear_data[i] != 0) {
			return -2;
		}
	}

	unsigned char nonce[crypt_box_NONCEBYTES];
	random_bytes(nonce, crypt_box_NONCEBYTES);
	const int ret = crypto_box(
		cypher_out,
		clear_data,
		length,
		nonce,
		receiver_public_key,
		sender_secret_key
	);
	assert(ret == 0);
	return 0;
}

int
crypt_box_open(
	const unsigned char sender_public_key[crypt_box_PUBLICKEYBYTES],
	const unsigned char receiver_secret_key[crypt_box_SECRETKEYBYTES],
	const unsigned long long length,
	const unsigned char *const cypher_data,
	      unsigned char *const clear_out
) {
	for (int i = 0U; i < crypt_box_BOXZEROBYTES; i++) {
		if (cypher_data[i] != 0) {
			return -2;
		}
	}

	unsigned char nonce[crypt_box_NONCEBYTES];
	random_bytes(nonce, crypt_box_NONCEBYTES);
	return crypto_box_open(
		clear_out,
		cypher_data,
		length,
		nonce,
		sender_public_key,
		receiver_secret_key
	);
}

void
crypt_box_beforenm(
	const unsigned char public_key[crypt_box_PUBLICKEYBYTES],
	const unsigned char secret_key[crypt_box_PUBLICKEYBYTES],
	      unsigned char out[crypt_box_BEFORENMBYTES]
) {
	const int ret = crypto_box_beforenm(out, public_key, secret_key);
	assert(ret == 0);
}

void
crypt_box_afternm(
	const unsigned char beforenm_intermediate[crypt_box_BEFORENMBYTES],
	const unsigned long long length,
	const unsigned char *const clear_data,
	      unsigned char *const cypher_out
) {
	unsigned char nonce[crypt_box_NONCEBYTES];
	random_bytes(nonce, crypt_box_NONCEBYTES);
	const int ret = crypto_box_afternm(
		cypher_out,
		clear_data,
		length,
		nonce,
		beforenm_intermediate
	);
	assert(ret == 0);
}

int
crypt_box_open_afternm(
	const unsigned char beforenm_intermediate[crypt_box_BEFORENMBYTES],
	const unsigned long long length,
	const unsigned char *const cypher_data,
	      unsigned char *const clear_out
) {
	unsigned char nonce[crypt_box_NONCEBYTES];
	random_bytes(nonce, crypt_box_NONCEBYTES);
	return crypto_box_open_afternm(
		clear_out,
		cypher_data,
		length,
		nonce,
		beforenm_intermediate
	);
}