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