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