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