/src/openssl40/providers/implementations/ciphers/ciphercommon_gcm.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2019-2026 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 <openssl/rand.h> |
13 | | #include <openssl/proverr.h> |
14 | | #include "prov/ciphercommon.h" |
15 | | #include "prov/ciphercommon_gcm.h" |
16 | | #include "prov/providercommon.h" |
17 | | #include "prov/provider_ctx.h" |
18 | | #include "fips/fipsindicator.h" |
19 | | |
20 | | #include "providers/implementations/ciphers/ciphercommon_gcm.inc" |
21 | | |
22 | | static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len); |
23 | | static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv, |
24 | | size_t len); |
25 | | static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen, |
26 | | const unsigned char *in, size_t len); |
27 | | static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out, |
28 | | size_t *padlen, const unsigned char *in, |
29 | | size_t len); |
30 | | static int on_preupdate_generate_iv(PROV_GCM_CTX *ctx); |
31 | | |
32 | | #ifdef FIPS_MODULE |
33 | | static int gcm_fips_taglen_approved(size_t taglen) |
34 | | { |
35 | | return taglen == UNINITIALISED_SIZET || taglen == 4 || taglen == 8 |
36 | | || (taglen >= 12 && taglen <= GCM_TAG_MAX_SIZE); |
37 | | } |
38 | | #endif |
39 | | |
40 | | /* |
41 | | * Called from EVP_CipherInit when there is currently no context via |
42 | | * the new_ctx() function |
43 | | */ |
44 | | void ossl_gcm_initctx(void *provctx, PROV_GCM_CTX *ctx, size_t keybits, |
45 | | const PROV_GCM_HW *hw) |
46 | 1.01M | { |
47 | 1.01M | ctx->pad = 1; |
48 | 1.01M | ctx->mode = EVP_CIPH_GCM_MODE; |
49 | 1.01M | ctx->taglen = UNINITIALISED_SIZET; |
50 | 1.01M | ctx->tls_aad_len = UNINITIALISED_SIZET; |
51 | 1.01M | ctx->ivlen = (EVP_GCM_TLS_FIXED_IV_LEN + EVP_GCM_TLS_EXPLICIT_IV_LEN); |
52 | 1.01M | ctx->keylen = keybits / 8; |
53 | 1.01M | ctx->hw = hw; |
54 | 1.01M | ctx->libctx = PROV_LIBCTX_OF(provctx); |
55 | 1.01M | } |
56 | | |
57 | | /* |
58 | | * Called by EVP_CipherInit via the _einit and _dinit functions |
59 | | */ |
60 | | static int gcm_init(void *vctx, const unsigned char *key, size_t keylen, |
61 | | const unsigned char *iv, size_t ivlen, |
62 | | const OSSL_PARAM params[], int enc) |
63 | 3.06M | { |
64 | 3.06M | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
65 | | |
66 | 3.06M | if (!ossl_prov_is_running()) |
67 | 0 | return 0; |
68 | | |
69 | 3.06M | ctx->enc = enc; |
70 | | |
71 | 3.06M | if (iv != NULL) { |
72 | 2.95M | if (ivlen == 0 || ivlen > sizeof(ctx->iv)) { |
73 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
74 | 0 | return 0; |
75 | 0 | } |
76 | 2.95M | ctx->iv_gen_rand = 0; |
77 | 2.95M | ctx->ivlen = ivlen; |
78 | 2.95M | memcpy(ctx->iv, iv, ivlen); |
79 | 2.95M | ctx->iv_state = IV_STATE_BUFFERED; |
80 | 2.95M | } |
81 | | |
82 | 3.06M | if (key != NULL) { |
83 | 1.01M | if (keylen != ctx->keylen) { |
84 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
85 | 0 | return 0; |
86 | 0 | } |
87 | 1.01M | if (!ctx->hw->setkey(ctx, key, ctx->keylen)) |
88 | 0 | return 0; |
89 | 1.01M | ctx->tls_enc_records = 0; |
90 | 1.01M | } |
91 | 3.06M | return ossl_gcm_set_ctx_params(ctx, params); |
92 | 3.06M | } |
93 | | |
94 | | int ossl_gcm_einit(void *vctx, const unsigned char *key, size_t keylen, |
95 | | const unsigned char *iv, size_t ivlen, |
96 | | const OSSL_PARAM params[]) |
97 | 1.88M | { |
98 | 1.88M | return gcm_init(vctx, key, keylen, iv, ivlen, params, 1); |
99 | 1.88M | } |
100 | | |
101 | | int ossl_gcm_dinit(void *vctx, const unsigned char *key, size_t keylen, |
102 | | const unsigned char *iv, size_t ivlen, |
103 | | const OSSL_PARAM params[]) |
104 | 1.18M | { |
105 | 1.18M | return gcm_init(vctx, key, keylen, iv, ivlen, params, 0); |
106 | 1.18M | } |
107 | | |
108 | | /* increment counter (64-bit int) by 1 */ |
109 | | static void ctr64_inc(unsigned char *counter) |
110 | 2.07k | { |
111 | 2.07k | int n = 8; |
112 | 2.07k | unsigned char c; |
113 | | |
114 | 2.07k | do { |
115 | 2.07k | --n; |
116 | 2.07k | c = counter[n]; |
117 | 2.07k | ++c; |
118 | 2.07k | counter[n] = c; |
119 | 2.07k | if (c > 0) |
120 | 2.07k | return; |
121 | 2.07k | } while (n > 0); |
122 | 2.07k | } |
123 | | |
124 | | static int getivgen(PROV_GCM_CTX *ctx, unsigned char *out, size_t olen) |
125 | 2.07k | { |
126 | 2.07k | if (!ctx->iv_gen |
127 | 2.07k | || !ctx->key_set |
128 | 2.07k | || !ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen)) |
129 | 0 | return 0; |
130 | 2.07k | if (olen == 0 || olen > ctx->ivlen) |
131 | 0 | olen = ctx->ivlen; |
132 | 2.07k | memcpy(out, ctx->iv + ctx->ivlen - olen, olen); |
133 | | /* |
134 | | * Invocation field will be at least 8 bytes in size and so no need |
135 | | * to check wrap around or increment more than last 8 bytes. |
136 | | */ |
137 | 2.07k | ctr64_inc(ctx->iv + ctx->ivlen - 8); |
138 | 2.07k | ctx->iv_state = IV_STATE_COPIED; |
139 | 2.07k | return 1; |
140 | 2.07k | } |
141 | | |
142 | | static int setivinv(PROV_GCM_CTX *ctx, unsigned char *in, size_t inl) |
143 | 112k | { |
144 | 112k | if (!ctx->iv_gen |
145 | 112k | || !ctx->key_set |
146 | 112k | || ctx->enc) |
147 | 0 | return 0; |
148 | | |
149 | 112k | memcpy(ctx->iv + ctx->ivlen - inl, in, inl); |
150 | 112k | if (!ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen)) |
151 | 0 | return 0; |
152 | 112k | ctx->iv_state = IV_STATE_COPIED; |
153 | 112k | return 1; |
154 | 112k | } |
155 | | |
156 | | const OSSL_PARAM *ossl_gcm_gettable_ctx_params( |
157 | | ossl_unused void *cctx, ossl_unused void *provctx) |
158 | 413 | { |
159 | 413 | return ossl_cipher_gcm_get_ctx_params_list; |
160 | 413 | } |
161 | | |
162 | | int ossl_gcm_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
163 | 2.02M | { |
164 | 2.02M | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
165 | 2.02M | size_t sz; |
166 | 2.02M | struct ossl_cipher_gcm_get_ctx_params_st p; |
167 | | |
168 | 2.02M | if (ctx == NULL || !ossl_cipher_gcm_get_ctx_params_decoder(params, &p)) |
169 | 0 | return 0; |
170 | | |
171 | 2.02M | if (p.ivlen != NULL && !OSSL_PARAM_set_size_t(p.ivlen, ctx->ivlen)) { |
172 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
173 | 0 | return 0; |
174 | 0 | } |
175 | | |
176 | 2.02M | if (p.keylen != NULL && !OSSL_PARAM_set_size_t(p.keylen, ctx->keylen)) { |
177 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
178 | 0 | return 0; |
179 | 0 | } |
180 | | |
181 | 2.02M | if (p.taglen != NULL) { |
182 | 0 | size_t taglen = (ctx->taglen != UNINITIALISED_SIZET) ? ctx->taglen : GCM_TAG_MAX_SIZE; |
183 | |
|
184 | 0 | if (!OSSL_PARAM_set_size_t(p.taglen, taglen)) { |
185 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
186 | 0 | return 0; |
187 | 0 | } |
188 | 0 | } |
189 | | |
190 | | /* |
191 | | * Note p.updiv and p.iv are aliases that get the same information, |
192 | | * so any code changes should be duplicated below. |
193 | | */ |
194 | 2.02M | if (p.iv != NULL) { |
195 | 0 | if (!on_preupdate_generate_iv(ctx)) |
196 | 0 | return 0; |
197 | 0 | if (p.iv->data != NULL && ctx->ivlen > p.iv->data_size) { |
198 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
199 | 0 | return 0; |
200 | 0 | } |
201 | 0 | if (!OSSL_PARAM_set_octet_string_or_ptr(p.iv, ctx->iv, ctx->ivlen)) { |
202 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
203 | 0 | return 0; |
204 | 0 | } |
205 | 0 | } |
206 | 2.02M | if (p.updiv != NULL) { |
207 | 0 | if (!on_preupdate_generate_iv(ctx)) |
208 | 0 | return 0; |
209 | 0 | if (p.updiv->data != NULL && ctx->ivlen > p.updiv->data_size) { |
210 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
211 | 0 | return 0; |
212 | 0 | } |
213 | 0 | if (!OSSL_PARAM_set_octet_string_or_ptr(p.updiv, ctx->iv, ctx->ivlen)) { |
214 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
215 | 0 | return 0; |
216 | 0 | } |
217 | 0 | } |
218 | | |
219 | 2.02M | if (p.pad != NULL && !OSSL_PARAM_set_size_t(p.pad, ctx->tls_aad_pad_sz)) { |
220 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
221 | 0 | return 0; |
222 | 0 | } |
223 | | |
224 | 2.02M | if (p.tag != NULL) { |
225 | 909k | if (!ctx->enc || ctx->taglen == UNINITIALISED_SIZET) { |
226 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG); |
227 | 0 | return 0; |
228 | 0 | } |
229 | 909k | if (p.tag->data == NULL) { |
230 | | /* size query: report the tag length, as for the iv above */ |
231 | 0 | sz = ctx->taglen; |
232 | 909k | } else { |
233 | 909k | sz = p.tag->data_size; |
234 | 909k | if (sz > EVP_GCM_TLS_TAG_LEN || sz == 0) { |
235 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG); |
236 | 0 | return 0; |
237 | 0 | } |
238 | 909k | } |
239 | | |
240 | 909k | if (!OSSL_PARAM_set_octet_string(p.tag, ctx->buf, sz)) { |
241 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
242 | 0 | return 0; |
243 | 0 | } |
244 | 909k | } |
245 | | |
246 | 2.02M | if (p.ivgen != NULL) |
247 | 0 | if (p.ivgen->data == NULL |
248 | 0 | || p.ivgen->data_type != OSSL_PARAM_OCTET_STRING |
249 | 0 | || !getivgen(ctx, p.ivgen->data, p.ivgen->data_size)) |
250 | 0 | return 0; |
251 | | |
252 | 2.02M | if (p.gen != NULL && !OSSL_PARAM_set_uint(p.gen, ctx->iv_gen_rand)) |
253 | 0 | return 0; |
254 | | |
255 | | /* |
256 | | * Externally supplied IVs are permitted but not approved for encryption. |
257 | | * Approved tag lengths are 4, 8, and 12 through 16 bytes. |
258 | | * For encryption, requesting only a prefix of the generated tag does not |
259 | | * change the tag length used by the operation. |
260 | | */ |
261 | 2.02M | if (!OSSL_FIPS_IND_GET_PARAM_CONDITIONAL(p.ind, |
262 | 2.02M | (!ctx->enc || ctx->iv_gen_rand) |
263 | 2.02M | && gcm_fips_taglen_approved(ctx->taglen))) |
264 | 0 | return 0; |
265 | | |
266 | 2.02M | return 1; |
267 | 2.02M | } |
268 | | |
269 | | const OSSL_PARAM *ossl_gcm_settable_ctx_params( |
270 | | ossl_unused void *cctx, ossl_unused void *provctx) |
271 | 26 | { |
272 | 26 | return ossl_cipher_gcm_set_ctx_params_list; |
273 | 26 | } |
274 | | |
275 | | int ossl_gcm_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
276 | 1.19M | { |
277 | 1.19M | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
278 | 1.19M | size_t sz; |
279 | 1.19M | void *vp; |
280 | 1.19M | struct ossl_cipher_gcm_set_ctx_params_st p; |
281 | | |
282 | 1.19M | if (ctx == NULL || !ossl_cipher_gcm_set_ctx_params_decoder(params, &p)) |
283 | 0 | return 0; |
284 | | |
285 | 1.19M | if (p.tag != NULL) { |
286 | 324k | vp = ctx->buf; |
287 | 324k | if (!OSSL_PARAM_get_octet_string(p.tag, &vp, EVP_GCM_TLS_TAG_LEN, &sz)) { |
288 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
289 | 0 | return 0; |
290 | 0 | } |
291 | 324k | if (sz == 0 || ctx->enc) { |
292 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG); |
293 | 0 | return 0; |
294 | 0 | } |
295 | 324k | ctx->taglen = sz; |
296 | 324k | } |
297 | | |
298 | 1.19M | if (p.ivlen != NULL) { |
299 | 1.57k | if (!OSSL_PARAM_get_size_t(p.ivlen, &sz)) { |
300 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
301 | 0 | return 0; |
302 | 0 | } |
303 | 1.57k | if (sz == 0 || sz > sizeof(ctx->iv)) { |
304 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
305 | 0 | return 0; |
306 | 0 | } |
307 | 1.57k | if (ctx->ivlen != sz) { |
308 | | /* If the iv was already set or autogenerated, it is invalid. */ |
309 | 0 | if (ctx->iv_state != IV_STATE_UNINITIALISED) |
310 | 0 | ctx->iv_state = IV_STATE_FINISHED; |
311 | 0 | ctx->ivlen = sz; |
312 | 0 | } |
313 | 1.57k | } |
314 | | |
315 | 1.19M | if (p.aad != NULL) { |
316 | 12.9k | if (p.aad->data_type != OSSL_PARAM_OCTET_STRING) { |
317 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
318 | 0 | return 0; |
319 | 0 | } |
320 | 12.9k | sz = gcm_tls_init(ctx, p.aad->data, p.aad->data_size); |
321 | 12.9k | if (sz == 0) { |
322 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_AAD); |
323 | 0 | return 0; |
324 | 0 | } |
325 | 12.9k | ctx->tls_aad_pad_sz = sz; |
326 | 12.9k | } |
327 | | |
328 | 1.19M | if (p.fixed != NULL) { |
329 | 584 | if (p.fixed->data_type != OSSL_PARAM_OCTET_STRING) { |
330 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
331 | 0 | return 0; |
332 | 0 | } |
333 | 584 | if (gcm_tls_iv_set_fixed(ctx, p.fixed->data, p.fixed->data_size) == 0) { |
334 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
335 | 0 | return 0; |
336 | 0 | } |
337 | 584 | } |
338 | | |
339 | 1.19M | if (p.inviv != NULL) |
340 | 0 | if (p.inviv->data == NULL |
341 | 0 | || p.inviv->data_type != OSSL_PARAM_OCTET_STRING |
342 | 0 | || !setivinv(ctx, p.inviv->data, p.inviv->data_size)) |
343 | 0 | return 0; |
344 | | |
345 | 1.19M | return 1; |
346 | 1.19M | } |
347 | | |
348 | | int ossl_gcm_stream_update(void *vctx, unsigned char *out, size_t *outl, |
349 | | size_t outsize, const unsigned char *in, size_t inl) |
350 | 4.90M | { |
351 | 4.90M | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
352 | | |
353 | 4.90M | if (inl == 0) { |
354 | 0 | *outl = 0; |
355 | 0 | return 1; |
356 | 0 | } |
357 | | |
358 | 4.90M | if (outsize < inl) { |
359 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
360 | 0 | return 0; |
361 | 0 | } |
362 | | |
363 | 4.90M | if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0) { |
364 | 88.8k | ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED); |
365 | 88.8k | return 0; |
366 | 88.8k | } |
367 | 4.81M | return 1; |
368 | 4.90M | } |
369 | | |
370 | | int ossl_gcm_stream_final(void *vctx, unsigned char *out, size_t *outl, |
371 | | size_t outsize) |
372 | 2.72M | { |
373 | 2.72M | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
374 | 2.72M | int i; |
375 | | |
376 | 2.72M | if (!ossl_prov_is_running()) |
377 | 0 | return 0; |
378 | | |
379 | 2.72M | i = gcm_cipher_internal(ctx, out, outl, NULL, 0); |
380 | 2.72M | if (i <= 0) |
381 | 941k | return 0; |
382 | | |
383 | 1.78M | *outl = 0; |
384 | 1.78M | return 1; |
385 | 2.72M | } |
386 | | |
387 | | int ossl_gcm_cipher(void *vctx, |
388 | | unsigned char *out, size_t *outl, size_t outsize, |
389 | | const unsigned char *in, size_t inl) |
390 | 0 | { |
391 | 0 | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
392 | |
|
393 | 0 | if (!ossl_prov_is_running()) |
394 | 0 | return 0; |
395 | | |
396 | 0 | if (outsize < inl) { |
397 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
398 | 0 | return 0; |
399 | 0 | } |
400 | | |
401 | 0 | if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0) |
402 | 0 | return 0; |
403 | | |
404 | 0 | *outl = inl; |
405 | 0 | return 1; |
406 | 0 | } |
407 | | |
408 | | /* |
409 | | * See SP800-38D (GCM) Section 8 "Uniqueness requirement on IVS and keys" |
410 | | * |
411 | | * See also 8.2.2 RBG-based construction. |
412 | | * Random construction consists of a free field (which can be NULL) and a |
413 | | * random field which will use a DRBG that can return at least 96 bits of |
414 | | * entropy strength. (The DRBG must be seeded by the FIPS module). |
415 | | */ |
416 | | static int gcm_iv_generate(PROV_GCM_CTX *ctx, int offset) |
417 | 512 | { |
418 | 512 | int sz = (int)(ctx->ivlen - offset); |
419 | | |
420 | | /* Must be at least 96 bits */ |
421 | 512 | if (sz <= 0 || ctx->ivlen < GCM_IV_DEFAULT_SIZE) |
422 | 0 | return 0; |
423 | | |
424 | | /* Use DRBG to generate random iv */ |
425 | 512 | if (RAND_bytes_ex(ctx->libctx, ctx->iv + offset, sz, 0) <= 0) |
426 | 0 | return 0; |
427 | 512 | ctx->iv_state = IV_STATE_BUFFERED; |
428 | 512 | ctx->iv_gen_rand = 1; |
429 | 512 | return 1; |
430 | 512 | } |
431 | | |
432 | | /* |
433 | | * If we try to grab the iv before the update - it wont be generated yet, |
434 | | * so we generate it here for this case. |
435 | | */ |
436 | | static int on_preupdate_generate_iv(PROV_GCM_CTX *ctx) |
437 | 0 | { |
438 | 0 | if (ctx->iv_state == IV_STATE_UNINITIALISED) { |
439 | 0 | if (ctx->tls_aad_len != UNINITIALISED_SIZET) |
440 | 0 | return 0; |
441 | 0 | if (!ctx->enc || !gcm_iv_generate(ctx, 0)) |
442 | 0 | return 0; |
443 | 0 | } |
444 | 0 | return 1; |
445 | 0 | } |
446 | | |
447 | | static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out, |
448 | | size_t *padlen, const unsigned char *in, |
449 | | size_t len) |
450 | 5.67M | { |
451 | 5.67M | size_t olen = 0; |
452 | 5.67M | int rv = 0; |
453 | 5.67M | const PROV_GCM_HW *hw = ctx->hw; |
454 | | |
455 | 5.67M | if (ctx->tls_aad_len != UNINITIALISED_SIZET) |
456 | 74.0k | return gcm_tls_cipher(ctx, out, padlen, in, len); |
457 | | |
458 | 5.59M | if (!ctx->key_set || ctx->iv_state == IV_STATE_FINISHED) |
459 | 0 | goto err; |
460 | | |
461 | | /* |
462 | | * FIPS requires generation of AES-GCM IV's inside the FIPS module. |
463 | | * The IV can still be set externally (the security policy will state that |
464 | | * this is not FIPS compliant). There are some applications |
465 | | * where setting the IV externally is the only option available. |
466 | | */ |
467 | 5.59M | if (ctx->iv_state == IV_STATE_UNINITIALISED) { |
468 | 288 | if (!ctx->enc || !gcm_iv_generate(ctx, 0)) |
469 | 0 | goto err; |
470 | 288 | } |
471 | | |
472 | 5.59M | if (ctx->iv_state == IV_STATE_BUFFERED) { |
473 | 1.72M | if (!hw->setiv(ctx, ctx->iv, ctx->ivlen)) |
474 | 0 | goto err; |
475 | 1.72M | ctx->iv_state = IV_STATE_COPIED; |
476 | 1.72M | } |
477 | | |
478 | 5.59M | if (in != NULL) { |
479 | | /* The input is AAD if out is NULL */ |
480 | 3.87M | if (out == NULL) { |
481 | 2.08M | if (!hw->aadupdate(ctx, in, len)) |
482 | 0 | goto err; |
483 | 2.08M | } else { |
484 | | /* The input is ciphertext OR plaintext */ |
485 | 1.79M | if (!hw->cipherupdate(ctx, in, len, out)) |
486 | 0 | goto err; |
487 | 1.79M | } |
488 | 3.87M | } else { |
489 | | /* The tag must be set before actually decrypting data */ |
490 | 1.72M | if (!ctx->enc && ctx->taglen == UNINITIALISED_SIZET) { |
491 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET); |
492 | 0 | goto err; |
493 | 0 | } |
494 | 1.72M | if (!hw->cipherfinal(ctx, ctx->buf)) |
495 | 661k | goto err; |
496 | 1.06M | ctx->iv_state = IV_STATE_FINISHED; /* Don't reuse the IV */ |
497 | 1.06M | goto finish; |
498 | 1.72M | } |
499 | 3.87M | olen = len; |
500 | 4.93M | finish: |
501 | 4.93M | rv = 1; |
502 | 5.59M | err: |
503 | 5.59M | *padlen = olen; |
504 | 5.59M | return rv; |
505 | 4.93M | } |
506 | | |
507 | | static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len) |
508 | 114k | { |
509 | 114k | unsigned char *buf; |
510 | 114k | size_t len; |
511 | | |
512 | 114k | if (!ossl_prov_is_running() || aad_len != EVP_AEAD_TLS1_AAD_LEN) |
513 | 0 | return 0; |
514 | | |
515 | | /* Save the aad for later use. */ |
516 | 114k | buf = dat->buf; |
517 | 114k | memcpy(buf, aad, aad_len); |
518 | 114k | dat->tls_aad_len = aad_len; |
519 | | |
520 | 114k | len = buf[aad_len - 2] << 8 | buf[aad_len - 1]; |
521 | | /* Correct length for explicit iv. */ |
522 | 114k | if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN) |
523 | 0 | return 0; |
524 | 114k | len -= EVP_GCM_TLS_EXPLICIT_IV_LEN; |
525 | | |
526 | | /* If decrypting correct for tag too. */ |
527 | 114k | if (!dat->enc) { |
528 | 112k | if (len < EVP_GCM_TLS_TAG_LEN) |
529 | 0 | return 0; |
530 | 112k | len -= EVP_GCM_TLS_TAG_LEN; |
531 | 112k | } |
532 | 114k | buf[aad_len - 2] = (unsigned char)(len >> 8); |
533 | 114k | buf[aad_len - 1] = (unsigned char)(len & 0xff); |
534 | | /* Extra padding: tag appended to record. */ |
535 | 114k | return EVP_GCM_TLS_TAG_LEN; |
536 | 114k | } |
537 | | |
538 | | static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv, |
539 | | size_t len) |
540 | 2.60k | { |
541 | | /* Special case: -1 length restores whole IV */ |
542 | 2.60k | if (len == (size_t)-1) { |
543 | 0 | memcpy(ctx->iv, iv, ctx->ivlen); |
544 | 0 | ctx->iv_gen_rand = 0; |
545 | 0 | ctx->iv_gen = 1; |
546 | 0 | ctx->iv_state = IV_STATE_BUFFERED; |
547 | 0 | return 1; |
548 | 0 | } |
549 | | /* Fixed field must be at least 4 bytes and invocation field at least 8 */ |
550 | 2.60k | if ((len < EVP_GCM_TLS_FIXED_IV_LEN) |
551 | 2.60k | || (ctx->ivlen - (int)len) < EVP_GCM_TLS_EXPLICIT_IV_LEN) |
552 | 0 | return 0; |
553 | 2.60k | if (len > 0) |
554 | 2.60k | memcpy(ctx->iv, iv, len); |
555 | 2.60k | if (ctx->enc) { |
556 | 1.06k | if (RAND_bytes_ex(ctx->libctx, ctx->iv + len, ctx->ivlen - len, 0) <= 0) |
557 | 0 | return 0; |
558 | 1.06k | ctx->iv_gen_rand = 1; |
559 | 1.06k | } |
560 | 2.60k | ctx->iv_gen = 1; |
561 | 2.60k | ctx->iv_state = IV_STATE_BUFFERED; |
562 | 2.60k | return 1; |
563 | 2.60k | } |
564 | | |
565 | | /* |
566 | | * Handle TLS GCM packet format. This consists of the last portion of the IV |
567 | | * followed by the payload and finally the tag. On encrypt generate IV, |
568 | | * encrypt payload and write the tag. On verify retrieve IV, decrypt payload |
569 | | * and verify tag. |
570 | | */ |
571 | | static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen, |
572 | | const unsigned char *in, size_t len) |
573 | 90.5k | { |
574 | 90.5k | int rv = 0; |
575 | 90.5k | size_t arg = EVP_GCM_TLS_EXPLICIT_IV_LEN; |
576 | 90.5k | size_t plen = 0; |
577 | 90.5k | unsigned char *tag = NULL; |
578 | | |
579 | 90.5k | if (!ossl_prov_is_running() || !ctx->key_set) |
580 | 0 | goto err; |
581 | | |
582 | | /* Encrypt/decrypt must be performed in place */ |
583 | 90.5k | if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN)) |
584 | 0 | goto err; |
585 | | |
586 | | /* |
587 | | * Check for too many keys as per FIPS 140-2 IG A.5 "Key/IV Pair Uniqueness |
588 | | * Requirements from SP 800-38D". The requirements is for one party to the |
589 | | * communication to fail after 2^64 - 1 keys. We do this on the encrypting |
590 | | * side only. |
591 | | */ |
592 | 90.5k | if (ctx->enc && ++ctx->tls_enc_records == 0) { |
593 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TOO_MANY_RECORDS); |
594 | 0 | goto err; |
595 | 0 | } |
596 | | |
597 | | /* |
598 | | * Set IV from start of buffer or generate IV and write to start of |
599 | | * buffer. |
600 | | */ |
601 | 90.5k | if (ctx->enc) { |
602 | 1.65k | if (!getivgen(ctx, out, arg)) |
603 | 0 | goto err; |
604 | 88.9k | } else { |
605 | 88.9k | if (!setivinv(ctx, out, arg)) |
606 | 0 | goto err; |
607 | 88.9k | } |
608 | | |
609 | | /* Fix buffer and length to point to payload */ |
610 | 90.5k | in += EVP_GCM_TLS_EXPLICIT_IV_LEN; |
611 | 90.5k | out += EVP_GCM_TLS_EXPLICIT_IV_LEN; |
612 | 90.5k | len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; |
613 | | |
614 | 90.5k | tag = ctx->enc ? out + len : (unsigned char *)in + len; |
615 | 90.5k | if (!ctx->hw->oneshot(ctx, ctx->buf, ctx->tls_aad_len, in, len, out, tag, |
616 | 90.5k | EVP_GCM_TLS_TAG_LEN)) { |
617 | 88.8k | if (!ctx->enc) |
618 | 88.8k | OPENSSL_cleanse(out, len); |
619 | 88.8k | goto err; |
620 | 88.8k | } |
621 | 1.79k | if (ctx->enc) |
622 | 1.65k | plen = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; |
623 | 143 | else |
624 | 143 | plen = len; |
625 | | |
626 | 1.79k | rv = 1; |
627 | 90.5k | err: |
628 | 90.5k | ctx->iv_state = IV_STATE_FINISHED; |
629 | 90.5k | ctx->tls_aad_len = UNINITIALISED_SIZET; |
630 | 90.5k | *padlen = plen; |
631 | 90.5k | return rv; |
632 | 1.79k | } |