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