Add basic aria and camellia ciphers modes to default provider
[openssl.git] / providers / common / ciphers / cipher_gcm.c
1 /*
2  * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /* Dispatch functions for gcm mode */
11
12 #include "cipher_locl.h"
13 #include "internal/rand_int.h"
14 #include "internal/provider_ctx.h"
15
16 static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len);
17 static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv,
18                                 size_t len);
19 static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen,
20                           const unsigned char *in, size_t len);
21 static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out,
22                                size_t *padlen, const unsigned char *in,
23                                size_t len);
24
25 void gcm_initctx(void *provctx, PROV_GCM_CTX *ctx, size_t keybits,
26                  const PROV_GCM_HW *hw, size_t ivlen_min)
27 {
28     ctx->pad = 1;
29     ctx->mode = EVP_CIPH_GCM_MODE;
30     ctx->taglen = -1;
31     ctx->tls_aad_len = -1;
32     ctx->ivlen_min = ivlen_min;
33     ctx->ivlen = (EVP_GCM_TLS_FIXED_IV_LEN + EVP_GCM_TLS_EXPLICIT_IV_LEN);
34     ctx->keylen = keybits / 8;
35     ctx->hw = hw;
36     ctx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
37 }
38
39 void gcm_deinitctx(PROV_GCM_CTX *ctx)
40 {
41     OPENSSL_cleanse(ctx->iv, sizeof(ctx->iv));
42 }
43
44 static int gcm_init(void *vctx, const unsigned char *key, size_t keylen,
45                     const unsigned char *iv, size_t ivlen, int enc)
46 {
47     PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
48
49     ctx->enc = enc;
50
51     if (iv != NULL) {
52         if (ivlen < ctx->ivlen_min || ivlen > sizeof(ctx->iv)) {
53             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
54             return 0;
55         }
56         ctx->ivlen = ivlen;
57         memcpy(ctx->iv, iv, ctx->ivlen);
58         ctx->iv_state = IV_STATE_BUFFERED;
59     }
60
61     if (key != NULL) {
62         if (keylen != ctx->keylen) {
63             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
64             return 0;
65         }
66         return ctx->hw->setkey(ctx, key, ctx->keylen);
67     }
68     return 1;
69 }
70
71 int gcm_einit(void *vctx, const unsigned char *key, size_t keylen,
72               const unsigned char *iv, size_t ivlen)
73 {
74     return gcm_init(vctx, key, keylen, iv, ivlen, 1);
75 }
76
77 int gcm_dinit(void *vctx, const unsigned char *key, size_t keylen,
78               const unsigned char *iv, size_t ivlen)
79 {
80     return gcm_init(vctx, key, keylen, iv, ivlen, 0);
81 }
82
83 int gcm_get_ctx_params(void *vctx, OSSL_PARAM params[])
84 {
85     PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
86     OSSL_PARAM *p;
87     size_t sz;
88
89     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
90     if (p != NULL && !OSSL_PARAM_set_int(p, ctx->ivlen)) {
91         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
92         return 0;
93     }
94     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
95     if (p != NULL && !OSSL_PARAM_set_int(p, ctx->keylen)) {
96         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
97         return 0;
98     }
99
100     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IV);
101     if (p != NULL) {
102         if (ctx->iv_gen != 1 && ctx->iv_gen_rand != 1)
103             return 0;
104         if (ctx->ivlen != (int)p->data_size) {
105             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
106             return 0;
107         }
108         if (!OSSL_PARAM_set_octet_string(p, ctx->iv, ctx->ivlen)) {
109             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
110             return 0;
111         }
112     }
113
114     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD);
115     if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) {
116         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
117         return 0;
118     }
119     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG);
120     if (p != NULL) {
121         sz = p->data_size;
122         if (sz == 0 || sz > EVP_GCM_TLS_TAG_LEN || !ctx->enc || ctx->taglen < 0) {
123             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG);
124             return 0;
125         }
126         if (!OSSL_PARAM_set_octet_string(p, ctx->buf, sz)) {
127             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
128             return 0;
129         }
130     }
131     return 1;
132 }
133
134 int gcm_set_ctx_params(void *vctx, const OSSL_PARAM params[])
135 {
136     PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
137     const OSSL_PARAM *p;
138     size_t sz;
139     void *vp;
140
141     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG);
142     if (p != NULL) {
143         vp = ctx->buf;
144         if (!OSSL_PARAM_get_octet_string(p, &vp, EVP_GCM_TLS_TAG_LEN, &sz)) {
145             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
146             return 0;
147         }
148         if (sz == 0 || ctx->enc) {
149             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG);
150             return 0;
151         }
152         ctx->taglen = sz;
153     }
154
155     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_IVLEN);
156     if (p != NULL) {
157         if (!OSSL_PARAM_get_size_t(p, &sz)) {
158             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
159             return 0;
160         }
161         if (sz == 0 || sz > sizeof(ctx->iv)) {
162             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
163             return 0;
164         }
165         ctx->ivlen = sz;
166     }
167
168     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD);
169     if (p != NULL) {
170         if (p->data_type != OSSL_PARAM_OCTET_STRING) {
171             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
172             return 0;
173         }
174         sz = gcm_tls_init(ctx, p->data, p->data_size);
175         if (sz == 0) {
176             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_AAD);
177             return 0;
178         }
179         ctx->tls_aad_pad_sz = sz;
180     }
181
182     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED);
183     if (p != NULL) {
184         if (p->data_type != OSSL_PARAM_OCTET_STRING) {
185             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
186             return 0;
187         }
188         if (gcm_tls_iv_set_fixed(ctx, p->data, p->data_size) == 0) {
189             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
190             return 0;
191         }
192     }
193
194     /*
195      * TODO(3.0) Temporary solution to address fuzz test crash, which will be
196      * reworked once the discussion in PR #9510 is resolved. i.e- We need a
197      * general solution for handling missing parameters inside set_params and
198      * get_params methods.
199      */
200     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
201     if (p != NULL) {
202         int keylen;
203
204         if (!OSSL_PARAM_get_int(p, &keylen)) {
205             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
206             return 0;
207         }
208         /* The key length can not be modified for gcm mode */
209         if (keylen != (int)ctx->keylen)
210             return 0;
211     }
212
213     return 1;
214 }
215
216 int gcm_stream_update(void *vctx, unsigned char *out, size_t *outl,
217                       size_t outsize, const unsigned char *in, size_t inl)
218 {
219     PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
220
221     if (outsize < inl) {
222         ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
223         return -1;
224     }
225
226     if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0) {
227         ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
228         return -1;
229     }
230     return 1;
231 }
232
233 int gcm_stream_final(void *vctx, unsigned char *out, size_t *outl,
234                      size_t outsize)
235 {
236     PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
237     int i;
238
239     i = gcm_cipher_internal(ctx, out, outl, NULL, 0);
240     if (i <= 0)
241         return 0;
242
243     *outl = 0;
244     return 1;
245 }
246
247 int gcm_cipher(void *vctx,
248                unsigned char *out, size_t *outl, size_t outsize,
249                const unsigned char *in, size_t inl)
250 {
251     PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
252
253     if (outsize < inl) {
254         ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
255         return -1;
256     }
257
258     if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0)
259         return -1;
260
261     *outl = inl;
262     return 1;
263 }
264
265 /*
266  * See SP800-38D (GCM) Section 8 "Uniqueness requirement on IVS and keys"
267  *
268  * See also 8.2.2 RBG-based construction.
269  * Random construction consists of a free field (which can be NULL) and a
270  * random field which will use a DRBG that can return at least 96 bits of
271  * entropy strength. (The DRBG must be seeded by the FIPS module).
272  */
273 static int gcm_iv_generate(PROV_GCM_CTX *ctx, int offset)
274 {
275     int sz = ctx->ivlen - offset;
276
277     /* Must be at least 96 bits */
278     if (sz <= 0 || ctx->ivlen < GCM_IV_DEFAULT_SIZE)
279         return 0;
280
281     /* Use DRBG to generate random iv */
282     if (rand_bytes_ex(ctx->libctx, ctx->iv + offset, sz) <= 0)
283         return 0;
284     ctx->iv_state = IV_STATE_BUFFERED;
285     ctx->iv_gen_rand = 1;
286     return 1;
287 }
288
289 static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out,
290                                size_t *padlen, const unsigned char *in,
291                                size_t len)
292 {
293     size_t olen = 0;
294     int rv = 0;
295     const PROV_GCM_HW *hw = ctx->hw;
296
297     if (ctx->tls_aad_len >= 0)
298         return gcm_tls_cipher(ctx, out, padlen, in, len);
299
300     if (!ctx->key_set || ctx->iv_state == IV_STATE_FINISHED)
301         goto err;
302
303     /*
304      * FIPS requires generation of AES-GCM IV's inside the FIPS module.
305      * The IV can still be set externally (the security policy will state that
306      * this is not FIPS compliant). There are some applications
307      * where setting the IV externally is the only option available.
308      */
309     if (ctx->iv_state == IV_STATE_UNINITIALISED) {
310         if (!ctx->enc || !gcm_iv_generate(ctx, 0))
311             goto err;
312     }
313
314     if (ctx->iv_state == IV_STATE_BUFFERED) {
315         if (!hw->setiv(ctx, ctx->iv, ctx->ivlen))
316             goto err;
317         ctx->iv_state = IV_STATE_COPIED;
318     }
319
320     if (in != NULL) {
321         /*  The input is AAD if out is NULL */
322         if (out == NULL) {
323             if (!hw->aadupdate(ctx, in, len))
324                 goto err;
325         } else {
326             /* The input is ciphertext OR plaintext */
327             if (!hw->cipherupdate(ctx, in, len, out))
328                 goto err;
329         }
330     } else {
331         /* Finished when in == NULL */
332         if (!hw->cipherfinal(ctx, ctx->buf))
333             goto err;
334         ctx->iv_state = IV_STATE_FINISHED; /* Don't reuse the IV */
335         goto finish;
336     }
337     olen = len;
338 finish:
339     rv = 1;
340 err:
341     *padlen = olen;
342     return rv;
343 }
344
345 static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len)
346 {
347     unsigned char *buf;
348     size_t len;
349
350     if (aad_len != EVP_AEAD_TLS1_AAD_LEN)
351        return 0;
352
353     /* Save the aad for later use. */
354     buf = dat->buf;
355     memcpy(buf, aad, aad_len);
356     dat->tls_aad_len = aad_len;
357     dat->tls_enc_records = 0;
358
359     len = buf[aad_len - 2] << 8 | buf[aad_len - 1];
360     /* Correct length for explicit iv. */
361     if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN)
362         return 0;
363     len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
364
365     /* If decrypting correct for tag too. */
366     if (!dat->enc) {
367         if (len < EVP_GCM_TLS_TAG_LEN)
368             return 0;
369         len -= EVP_GCM_TLS_TAG_LEN;
370     }
371     buf[aad_len - 2] = (unsigned char)(len >> 8);
372     buf[aad_len - 1] = (unsigned char)(len & 0xff);
373     /* Extra padding: tag appended to record. */
374     return EVP_GCM_TLS_TAG_LEN;
375 }
376
377 static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv,
378                                 size_t len)
379 {
380     /* Special case: -1 length restores whole IV */
381     if (len == (size_t)-1) {
382         memcpy(ctx->iv, iv, ctx->ivlen);
383         ctx->iv_gen = 1;
384         ctx->iv_state = IV_STATE_BUFFERED;
385         return 1;
386     }
387     /* Fixed field must be at least 4 bytes and invocation field at least 8 */
388     if ((len < EVP_GCM_TLS_FIXED_IV_LEN)
389         || (ctx->ivlen - (int)len) < EVP_GCM_TLS_EXPLICIT_IV_LEN)
390             return 0;
391     if (len > 0)
392         memcpy(ctx->iv, iv, len);
393     if (ctx->enc
394         && rand_bytes_ex(ctx->libctx, ctx->iv + len, ctx->ivlen - len) <= 0)
395             return 0;
396     ctx->iv_gen = 1;
397     ctx->iv_state = IV_STATE_BUFFERED;
398     return 1;
399 }
400
401 /* increment counter (64-bit int) by 1 */
402 static void ctr64_inc(unsigned char *counter)
403 {
404     int n = 8;
405     unsigned char c;
406
407     do {
408         --n;
409         c = counter[n];
410         ++c;
411         counter[n] = c;
412         if (c > 0)
413             return;
414     } while (n > 0);
415 }
416
417 /*
418  * Handle TLS GCM packet format. This consists of the last portion of the IV
419  * followed by the payload and finally the tag. On encrypt generate IV,
420  * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
421  * and verify tag.
422  */
423 static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen,
424                           const unsigned char *in, size_t len)
425 {
426     int rv = 0, arg = EVP_GCM_TLS_EXPLICIT_IV_LEN;
427     size_t plen = 0;
428     unsigned char *tag = NULL;
429
430     if (!ctx->key_set)
431         goto err;
432
433     /* Encrypt/decrypt must be performed in place */
434     if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN))
435         goto err;
436
437     /*
438      * Check for too many keys as per FIPS 140-2 IG A.5 "Key/IV Pair Uniqueness
439      * Requirements from SP 800-38D".  The requirements is for one party to the
440      * communication to fail after 2^64 - 1 keys.  We do this on the encrypting
441      * side only.
442      */
443     if (ctx->enc && ++ctx->tls_enc_records == 0) {
444         ERR_raise(ERR_LIB_PROV, EVP_R_TOO_MANY_RECORDS);
445         goto err;
446     }
447
448     if (ctx->iv_gen == 0)
449         goto err;
450     /*
451      * Set IV from start of buffer or generate IV and write to start of
452      * buffer.
453      */
454     if (ctx->enc) {
455         if (!ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen))
456             goto err;
457         if (arg > ctx->ivlen)
458             arg = ctx->ivlen;
459         memcpy(out, ctx->iv + ctx->ivlen - arg, arg);
460         /*
461          * Invocation field will be at least 8 bytes in size and so no need
462          * to check wrap around or increment more than last 8 bytes.
463          */
464         ctr64_inc(ctx->iv + ctx->ivlen - 8);
465     } else {
466         memcpy(ctx->iv + ctx->ivlen - arg, out, arg);
467         if (!ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen))
468             goto err;
469     }
470     ctx->iv_state = IV_STATE_COPIED;
471
472     /* Fix buffer and length to point to payload */
473     in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
474     out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
475     len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
476
477     tag = ctx->enc ? out + len : (unsigned char *)in + len;
478     if (!ctx->hw->oneshot(ctx, ctx->buf, ctx->tls_aad_len, in, len, out, tag,
479                           EVP_GCM_TLS_TAG_LEN)) {
480         if (!ctx->enc)
481             OPENSSL_cleanse(out, len);
482         goto err;
483     }
484     if (ctx->enc)
485         plen =  len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
486     else
487         plen = len;
488
489     rv = 1;
490 err:
491     ctx->iv_state = IV_STATE_FINISHED;
492     ctx->tls_aad_len = -1;
493     *padlen = plen;
494     return rv;
495 }