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