aboutsummaryrefslogtreecommitdiffstats
path: root/lib/crypto/c_src/mac.c
blob: 91dd42314ec4645f1b58bfa33083fc8764b38a00 (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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
 * %CopyrightBegin%
 *
 * Copyright Ericsson AB 2010-2019. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * %CopyrightEnd%
 */

#include "common.h"
#include "cipher.h"
#include "digest.h"
#include "mac.h"

#ifdef HAS_EVP_PKEY_CTX
struct mac_type_t {
    union {
	const char*  str;        /* before init, NULL for end-of-table */
	ERL_NIF_TERM atom;       /* after init, 'false' for end-of-table */
    }type;
    union {
        const int type;
    }alg;
};


static struct mac_type_t mac_types[] =
{
    {{"poly1305"},
#ifdef HAVE_POLY1305
     {EVP_PKEY_POLY1305}
#else
     {EVP_PKEY_NONE}
#endif
    },

    {{"hmac"},
#ifdef HAVE_PKEY_HMAC
     {EVP_PKEY_HMAC}
#else
     {EVP_PKEY_NONE}
#endif
    },

    {{"cmac"},
#ifdef HAVE_CMAC
     {EVP_PKEY_CMAC}
#else
     {EVP_PKEY_NONE}
#endif
    },
    /*==== End of list ==== */
    {{NULL},{0}}
};

#endif /* HAS_EVP_PKEY_CTX */


void init_mac_types(ErlNifEnv* env)
{
#ifdef HAS_EVP_PKEY_CTX
    struct mac_type_t* p = mac_types;

    for (p = mac_types; p->type.str; p++) {
	p->type.atom = enif_make_atom(env, p->type.str);
    }
    p->type.atom = atom_false;  /* end marker */
#endif    
}


ERL_NIF_TERM mac_types_as_list(ErlNifEnv* env)
{
#ifdef HAS_EVP_PKEY_CTX
    struct mac_type_t* p;
    ERL_NIF_TERM prev, hd;

    hd = enif_make_list(env, 0);
    prev = atom_undefined;

    for (p = mac_types; (p->type.atom & (p->type.atom != atom_false)); p++) {
        if (prev == p->type.atom)
            continue;

        if (p->alg.type != EVP_PKEY_NONE)
            {
                hd = enif_make_list_cell(env, p->type.atom, hd);
            }
    }

    return hd;
#else
    return enif_make_list1(env, atom_hmac);
#endif
}

     
#ifdef HAS_EVP_PKEY_CTX
struct mac_type_t* get_mac_type(ERL_NIF_TERM type);

struct mac_type_t* get_mac_type(ERL_NIF_TERM type)
{
    struct mac_type_t* p = NULL;
    for (p = mac_types; p->type.atom != atom_false; p++) {
	if (type == p->type.atom) {
	    return p;
	}
    }
    return NULL;
}
#endif




ERL_NIF_TERM mac_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{/* (MacType, SubType, Key, Text) */

    ErlNifBinary key_bin, text;
    int ret_bin_alloc = 0;
    size_t size;
    ERL_NIF_TERM return_term;
    const EVP_MD *md = NULL;
    ErlNifBinary ret_bin;
#ifdef HAS_EVP_PKEY_CTX
    EVP_PKEY *pkey = NULL;
    EVP_MD_CTX *mctx = NULL;
    EVP_PKEY_CTX *pctx = NULL;
#endif

    if (!enif_inspect_iolist_as_binary(env, argv[2], &key_bin))
        {
            return_term = EXCP_BADARG(env, "Bad key");
            goto err;
        }

    if (!enif_inspect_iolist_as_binary(env, argv[3], &text))
        {
            return_term = EXCP_BADARG(env, "Bad text");
            goto err;
        }
    
    if (argv[0] == atom_hmac)
        {
            struct digest_type_t *digp;
            
            if ((digp = get_digest_type(argv[1])) == NULL)
                {
                    return_term = EXCP_BADARG(env, "Bad digest algorithm for HMAC");
                    goto err;
                }
            if (digp->md.p == NULL)
                {
                    return_term = EXCP_NOTSUP(env, "Unsupported digest algorithm");
                    goto err;
                }

            md = digp->md.p;

#ifndef HAS_EVP_PKEY_CTX
            /* Old cryptolib: use low level functions */
            {
                unsigned int size_int;

                /* Find the needed space */
                if (HMAC(md,
                         key_bin.data, (int)key_bin.size,
                         text.data, text.size,
                         NULL, &size_int) == NULL)
                    {
                        return_term = EXCP_ERROR(env, "Get HMAC size failed");
                        goto err;
                    }

                size = (size_t)size_int; /* Otherwise "size" is unused in 0.9.8.... */
                if (!enif_alloc_binary(size, &ret_bin))
                    {
                        return_term = EXCP_ERROR(env, "Alloc binary");
                        goto err;
                    }
                ret_bin_alloc = 1;

                /* And do the real HMAC calc */
                if (HMAC(md,
                         key_bin.data, (int)key_bin.size,
                         text.data, text.size,
                         ret_bin.data, &size_int) == NULL)
                    {
                        return_term = EXCP_ERROR(env, "HMAC sign failed");
                        goto err;
                    }
            }
#else
/* HAS_EVP_PKEY_CTX and HMAC is the type. Produce a PKEY for later use */

# ifdef HAVE_PKEY_new_raw_private_key
            /* Prefered for new applications according to EVP_PKEY_new_mac_key(3) */
            pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, /*engine*/ NULL, key_bin.data,  key_bin.size);
# else
            /* Available in older versions */
            pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, /*engine*/ NULL, key_bin.data,  key_bin.size);
# endif
        }

    else if (argv[0] == atom_cmac)
        {
#ifdef HAVE_CMAC
            const struct cipher_type_t *cipherp;
            if (!(cipherp = get_cipher_type(argv[1], key_bin.size)))
                { /* Something went wrong. Find out what by retrying in another way. */
                    if (!get_cipher_type_no_key(argv[1]))
                        return_term = EXCP_BADARG(env, "Unknown cipher");
                    else
                        /* Cipher exists, so it must be the key size that is wrong */
                        return_term = EXCP_BADARG(env, "Bad key size");
                    goto err;
                }
            
            if (FORBIDDEN_IN_FIPS(cipherp))
                {
                    return_term = EXCP_NOTSUP(env, "Cipher algorithm not supported in FIPS");
                    goto err;
                }

            if (cipherp->cipher.p == NULL)
                {
                    return_term = EXCP_NOTSUP(env, "Unsupported cipher algorithm");
                    goto err;
                }

# ifdef HAVE_EVP_PKEY_new_CMAC_key
            pkey = EVP_PKEY_new_CMAC_key(/*engine*/ NULL, key_bin.data,  key_bin.size, cipherp->cipher.p);
# else
            /* Compatibility with < 1.1.1 that doesn't have EVP_PKEY_new_CMAC_key
               It is a complicated flow so just do some goto to get out of it.
             */
            {
                CMAC_CTX *ctx = NULL;

                if ((ctx = CMAC_CTX_new()) == NULL)
                    goto local_err;

                if (!CMAC_Init(ctx, key_bin.data, key_bin.size, cipherp->cipher.p, NULL))
                    goto local_err;

                if (!CMAC_Update(ctx, text.data, text.size))
                    goto local_err;

                if ((size = (size_t)EVP_CIPHER_block_size(cipherp->cipher.p)) < 0)
                    goto local_err;

                if (!enif_alloc_binary(size, &ret_bin))
                    goto local_err;
                ret_bin_alloc = 1;
                
                if (!CMAC_Final(ctx, ret_bin.data, &ret_bin.size))
                    goto local_err;

                CONSUME_REDS(env, text);

                return_term = enif_make_binary(env, &ret_bin);
                ret_bin_alloc = 0;
                goto done;

            local_err:
                 if (ctx)
                     CMAC_CTX_free(ctx);

                 return_term=EXCP_ERROR(env,"Compat cmac");
                 goto err;
            }
# endif
#else
            return_term = EXCP_NOTSUP(env, "Unsupported mac type");
            goto err;  
#endif /* HAVE_CMAC */
        }

    else if (argv[0] == atom_poly1305)
        {
#ifdef HAVE_POLY1305
            if (key_bin.size != 32)
                {
                    return_term = EXCP_BADARG(env, "Bad key size, != 32 bytes");
                    goto err;
                }
            /* poly1305 implies that EVP_PKEY_new_raw_private_key exists */
            pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_POLY1305, /*engine*/ NULL, key_bin.data,  key_bin.size);
#else
            return_term = EXCP_NOTSUP(env, "Unsupported mac type");
            goto err;  
#endif /* HAVE_POLY1305 */
#endif /* HAS_EVP_PKEY_CTX */

        }
    else
        {
            return_term = EXCP_BADARG(env, "Bad mac type");
            goto err;
        }

#ifdef HAS_EVP_PKEY_CTX
    if (!pkey)
        {
            return_term = EXCP_ERROR(env, "EVP_PKEY_key creation");
            goto err;
        }

    
    if ((mctx = EVP_MD_CTX_new()) == NULL)
        {
            return_term = EXCP_ERROR(env, "EVP_MD_CTX_new");
            goto err;
        }

    if (EVP_DigestSignInit(mctx, &pctx, md, /*engine*/ NULL, pkey) != 1)
        {
            return_term = EXCP_ERROR(env, "EVP_DigestSign");
            goto err;
        }

# ifdef HAVE_DigestSign_as_single_op
    if (EVP_DigestSign(mctx, NULL, &size, text.data, text.size) != 1)
# else
    if (EVP_DigestSignUpdate(mctx, text.data, text.size) != 1)
        {
            return_term = EXCP_ERROR(env, "EVP_DigestSignUpdate");
            goto err;
        }

    if (EVP_DigestSignFinal(mctx, NULL, &size) != 1)
# endif
        {
            return_term = EXCP_ERROR(env, "Can't get sign size");
            goto err;
        }
   
    if (!enif_alloc_binary(size, &ret_bin))
        {
            return_term = EXCP_ERROR(env, "Alloc binary");
            goto err;
        }
    ret_bin_alloc = 1;

# ifdef HAVE_DigestSign_as_single_op
    if (EVP_DigestSign(mctx, ret_bin.data, &size, text.data, text.size) != 1)
# else    
    if (EVP_DigestSignFinal(mctx, ret_bin.data, &size) != 1)
# endif
        {
            return_term = EXCP_ERROR(env, "Signing");
            goto err;
        }
    
#endif /* ifdef HAS_EVP_PKEY_CTX */

    CONSUME_REDS(env, text);

    return_term = enif_make_binary(env, &ret_bin);
    ret_bin_alloc = 0;
    goto done;

err:
    if (ret_bin_alloc)
        enif_release_binary(&ret_bin);

done:
#ifdef HAS_EVP_PKEY_CTX
    if (pkey)
        EVP_PKEY_free(pkey);
#endif
    return return_term;
}