summaryrefslogtreecommitdiff
path: root/c_src/monocypher_nif.c
blob: c8390e255ca3b0feaaa8b77ecf5fa0ddce00b602 (plain)
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// SPDX-FileCopyrightText: 2020 pukkamustard <pukkamustard@posteo.net>
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include <erl_nif.h>
#include "monocypher.h"
#include "monocypher-ed25519.h"

/* Authenticated Encryption */

static ERL_NIF_TERM crypto_lock_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifBinary mac;
    if(!enif_inspect_binary(env, argv[0], &mac)) {
        return enif_make_badarg(env);
    }

    // mac size must be exactly 16 bytes.
    if(mac.size != 16) {
        return enif_make_badarg(env);
    }

    ErlNifBinary key;
    if(!enif_inspect_binary(env, argv[1], &key)) {
        return enif_make_badarg(env);
    }

    // key size must be exactly 32 bytes.
    if(key.size != 32) {
        return enif_make_badarg(env);
    }

    ErlNifBinary nonce;
    if(!enif_inspect_binary(env, argv[2], &nonce)) {
        return enif_make_badarg(env);
    }

    // nonce size must be exactly 24 bytes.
    if(nonce.size != 24) {
        return enif_make_badarg(env);
    }

    ErlNifBinary plain_text;
    if(!enif_inspect_binary(env, argv[3], &plain_text)) {
        return enif_make_badarg(env);
    }

    // create a new binary for the cipher_text
    ERL_NIF_TERM cipher_text_term;
    unsigned char* cipher_text = enif_make_new_binary(env, plain_text.size, &cipher_text_term);

    crypto_lock(mac.data, cipher_text, key.data, nonce.data, plain_text.data, plain_text.size);

    return cipher_text_term;
}

static ERL_NIF_TERM crypto_unlock_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifBinary key;
    if(!enif_inspect_binary(env, argv[0], &key)) {
        return enif_make_badarg(env);
    }

    // key size must be exactly 32 bytes.
    if(key.size != 32) {
        return enif_make_badarg(env);
    }

    ErlNifBinary nonce;
    if(!enif_inspect_binary(env, argv[1], &nonce)) {
        return enif_make_badarg(env);
    }

    // nonce size must be exactly 24 bytes.
    if(nonce.size != 24) {
        return enif_make_badarg(env);
    }

    ErlNifBinary mac;
    if(!enif_inspect_binary(env, argv[2], &mac)) {
        return enif_make_badarg(env);
    }

    // mac size must be exactly 16 bytes.
    if(mac.size != 16) {
        return enif_make_badarg(env);
    }

    ErlNifBinary cipher_text;
    if(!enif_inspect_binary(env, argv[3], &cipher_text)) {
        return enif_make_badarg(env);
    }

    // create a new binary for the plain_text
    ERL_NIF_TERM plain_text_term;
    unsigned char* plain_text = enif_make_new_binary(env, cipher_text.size, &plain_text_term);

    int unlock_return_value = crypto_unlock(plain_text, key.data, nonce.data, mac.data, cipher_text.data, cipher_text.size);

    if (unlock_return_value == 0) {
        return enif_make_tuple2(env, enif_make_atom_len(env, "ok", 2), plain_text_term);
    } else {
        return enif_make_tuple2(env, enif_make_atom_len(env, "error", 5), enif_make_int(env, unlock_return_value));
    }
}

/* Hashing */

static ERL_NIF_TERM crypto_blake2b_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ERL_NIF_TERM hash_term;
    unsigned char* hash = enif_make_new_binary(env, 64, &hash_term);

    ErlNifBinary message;
    if(!enif_inspect_binary(env, argv[0], &message)) {
        return enif_make_badarg(env);
    }

    crypto_blake2b(hash, message.data, message.size);

    return hash_term;
}

static ERL_NIF_TERM crypto_blake2b_general_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    size_t hash_size;
    if (!enif_get_ulong(env, argv[0], &hash_size)) {
        return enif_make_badarg(env);
    }

    // hash_size must be between 1 and 64.
    if(hash_size < 1 || hash_size > 64) {
        return enif_make_badarg(env);
    }

    ERL_NIF_TERM hash_term;
    unsigned char* hash = enif_make_new_binary(env, hash_size, &hash_term);

    ErlNifBinary key;
    if(!enif_inspect_binary(env, argv[1], &key)) {
        return enif_make_badarg(env);
    }

    // key size must be betweeen 0 and 64
    if(key.size < 0 || key.size > 64) {
        return enif_make_badarg(env);
    }

    ErlNifBinary message;
    if(!enif_inspect_binary(env, argv[2], &message)) {
        return enif_make_badarg(env);
    }

    crypto_blake2b_general(hash, hash_size, key.data, key.size, message.data, message.size);

    return hash_term;
}


/* IETF ChaCha20 */

static ERL_NIF_TERM crypto_ietf_chacha20_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifBinary plain_text;
    if(!enif_inspect_binary(env, argv[0], &plain_text)) {
        return enif_make_badarg(env);
    }

    ErlNifBinary key;
    if(!enif_inspect_binary(env, argv[1], &key)) {
        return enif_make_badarg(env);
    }

    if(key.size != 32) {
        return enif_make_badarg(env);
    }

    ErlNifBinary nonce;
    if(!enif_inspect_binary(env, argv[2], &nonce)) {
        return enif_make_badarg(env);
    }

    if(nonce.size != 12) {
        return enif_make_badarg(env);
    }

    ERL_NIF_TERM cipher_text_term;
    unsigned char* cipher_text = enif_make_new_binary(env, plain_text.size, &cipher_text_term);

    crypto_ietf_chacha20(cipher_text, plain_text.data, plain_text.size, key.data, nonce.data);

    return cipher_text_term;
}

static ERL_NIF_TERM crypto_ietf_chacha20_ctr_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifBinary plain_text;
    if(!enif_inspect_binary(env, argv[0], &plain_text)) {
        return enif_make_badarg(env);
    }

    ErlNifBinary key;
    if(!enif_inspect_binary(env, argv[1], &key)) {
        return enif_make_badarg(env);
    }

    if(key.size != 32) {
        return enif_make_badarg(env);
    }

    ErlNifBinary nonce;
    if(!enif_inspect_binary(env, argv[2], &nonce)) {
        return enif_make_badarg(env);
    }

    if(nonce.size != 12) {
        return enif_make_badarg(env);
    }

    uint32_t ctr;
    if (!enif_get_uint(env, argv[3], &ctr)) {
        return enif_make_badarg(env);
    }

    ERL_NIF_TERM cipher_text_term;
    unsigned char* cipher_text = enif_make_new_binary(env, plain_text.size, &cipher_text_term);

    crypto_ietf_chacha20_ctr(cipher_text, plain_text.data, plain_text.size, key.data, nonce.data, ctr);

    return cipher_text_term;
}


/* Public Key Signature (Ed25519) */

static ERL_NIF_TERM crypto_ed25519_public_key_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifBinary secret_key;
    if(!enif_inspect_binary(env, argv[0], &secret_key)) {
        return enif_make_badarg(env);
    }

    if(secret_key.size != 32) {
        return enif_make_badarg(env);
    }

    ERL_NIF_TERM public_key_term;
    unsigned char* public_key = enif_make_new_binary(env, 32, &public_key_term);

    crypto_ed25519_public_key(public_key, secret_key.data);

    return public_key_term;
}

static ERL_NIF_TERM crypto_ed25519_sign_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifBinary secret_key;
    if(!enif_inspect_binary(env, argv[0], &secret_key)) {
        return enif_make_badarg(env);
    }

    if(secret_key.size != 32) {
        return enif_make_badarg(env);
    }

    ErlNifBinary public_key;
    if(!enif_inspect_binary(env, argv[1], &public_key)) {
        return enif_make_badarg(env);
    }

    if(public_key.size != 32) {
        return enif_make_badarg(env);
    }

    ErlNifBinary message;
    if(!enif_inspect_binary(env, argv[2], &message)) {
        return enif_make_badarg(env);
    }

    ERL_NIF_TERM signature_term;
    unsigned char* signature = enif_make_new_binary(env, 64, &signature_term);

    crypto_ed25519_sign(signature, secret_key.data, public_key.data, message.data, message.size);

    return signature_term;
}

static ERL_NIF_TERM crypto_ed25519_check_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifBinary signature;
    if(!enif_inspect_binary(env, argv[0], &signature)) {
        return enif_make_badarg(env);
    }

    if(signature.size != 64) {
        return enif_make_badarg(env);
    }

    ErlNifBinary public_key;
    if(!enif_inspect_binary(env, argv[1], &public_key)) {
        return enif_make_badarg(env);
    }

    if(public_key.size != 32) {
        return enif_make_badarg(env);
    }

    ErlNifBinary message;
    if(!enif_inspect_binary(env, argv[2], &message)) {
        return enif_make_badarg(env);
    }

    int check_return_value = crypto_ed25519_check(signature.data, public_key.data, message.data, message.size);

    if (check_return_value == 0) {
        return enif_make_atom_len(env, "ok", 2);
    } else {
        return enif_make_atom_len(env, "forgery", 7);
    }
}


static ErlNifFunc nif_funs[] =
{
    {"crypto_lock", 4, crypto_lock_nif},
    {"crypto_unlock", 4, crypto_unlock_nif},
    {"crypto_blake2b", 1, crypto_blake2b_nif},
    {"crypto_blake2b_general", 3, crypto_blake2b_general_nif},
    {"crypto_ietf_chacha20", 3, crypto_ietf_chacha20_nif},
    {"crypto_ietf_chacha20_ctr", 4, crypto_ietf_chacha20_ctr_nif},
    {"crypto_ed25519_public_key", 1, crypto_ed25519_public_key_nif},
    {"crypto_ed25519_sign", 3, crypto_ed25519_sign_nif},
    {"crypto_ed25519_check", 3, crypto_ed25519_check_nif}
};

ERL_NIF_INIT(monocypher, nif_funs, NULL, NULL, NULL, NULL);