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