/src/openssl41/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_TAG_NOT_SET); |
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.43M | { |
277 | 1.43M | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
278 | 1.43M | size_t sz; |
279 | 1.43M | void *vp; |
280 | 1.43M | struct ossl_cipher_gcm_set_ctx_params_st p; |
281 | | |
282 | 1.43M | if (ctx == NULL || !ossl_cipher_gcm_set_ctx_params_decoder(params, &p)) |
283 | 0 | return 0; |
284 | | |
285 | 1.43M | if (p.tag != NULL) { |
286 | 280k | vp = ctx->buf; |
287 | 280k | if (!OSSL_PARAM_get_octet_string(p.tag, &vp, EVP_GCM_TLS_TAG_LEN, &sz)) { |
288 | 4 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
289 | 4 | return 0; |
290 | 4 | } |
291 | 280k | if (ctx->enc) { |
292 | 20 | ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_NEEDED); |
293 | 20 | return 0; |
294 | 20 | } |
295 | 280k | if (sz == 0) { |
296 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG); |
297 | 0 | return 0; |
298 | 0 | } |
299 | 280k | ctx->taglen = sz; |
300 | 280k | } |
301 | | |
302 | 1.43M | if (p.ivlen != NULL) { |
303 | 2.06k | if (!OSSL_PARAM_get_size_t(p.ivlen, &sz)) { |
304 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
305 | 0 | return 0; |
306 | 0 | } |
307 | 2.06k | if (sz == 0 || sz > sizeof(ctx->iv)) { |
308 | 10 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
309 | 10 | return 0; |
310 | 10 | } |
311 | 2.05k | if (ctx->ivlen != sz) { |
312 | | /* If the iv was already set or autogenerated, it is invalid. */ |
313 | 9 | if (ctx->iv_state != IV_STATE_UNINITIALISED) |
314 | 0 | ctx->iv_state = IV_STATE_FINISHED; |
315 | 9 | ctx->ivlen = sz; |
316 | 9 | } |
317 | 2.05k | } |
318 | | |
319 | 1.43M | if (p.aad != NULL) { |
320 | 40.6k | if (p.aad->data_type != OSSL_PARAM_OCTET_STRING) { |
321 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
322 | 0 | return 0; |
323 | 0 | } |
324 | 40.6k | sz = gcm_tls_init(ctx, p.aad->data, p.aad->data_size); |
325 | 40.6k | if (sz == 0) { |
326 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_AAD); |
327 | 0 | return 0; |
328 | 0 | } |
329 | 40.6k | ctx->tls_aad_pad_sz = sz; |
330 | 40.6k | } |
331 | | |
332 | 1.43M | if (p.fixed != NULL) { |
333 | 1.73k | if (p.fixed->data_type != OSSL_PARAM_OCTET_STRING) { |
334 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
335 | 0 | return 0; |
336 | 0 | } |
337 | 1.73k | if (gcm_tls_iv_set_fixed(ctx, p.fixed->data, p.fixed->data_size) == 0) { |
338 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
339 | 0 | return 0; |
340 | 0 | } |
341 | 1.73k | } |
342 | | |
343 | 1.43M | if (p.inviv != NULL) |
344 | 0 | if (p.inviv->data == NULL |
345 | 0 | || p.inviv->data_type != OSSL_PARAM_OCTET_STRING |
346 | 0 | || !setivinv(ctx, p.inviv->data, p.inviv->data_size)) |
347 | 0 | return 0; |
348 | | |
349 | 1.43M | return 1; |
350 | 1.43M | } |
351 | | |
352 | | int ossl_gcm_stream_update(void *vctx, unsigned char *out, size_t *outl, |
353 | | size_t outsize, const unsigned char *in, size_t inl) |
354 | 4.90M | { |
355 | 4.90M | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
356 | | |
357 | 4.90M | if (inl == 0) { |
358 | 0 | *outl = 0; |
359 | 0 | return 1; |
360 | 0 | } |
361 | | |
362 | 4.90M | if (outsize < inl) { |
363 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
364 | 0 | return 0; |
365 | 0 | } |
366 | | |
367 | 4.90M | if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0) { |
368 | 88.8k | ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED); |
369 | 88.8k | return 0; |
370 | 88.8k | } |
371 | 4.81M | return 1; |
372 | 4.90M | } |
373 | | |
374 | | int ossl_gcm_stream_final(void *vctx, unsigned char *out, size_t *outl, |
375 | | size_t outsize) |
376 | 2.72M | { |
377 | 2.72M | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
378 | 2.72M | int i; |
379 | | |
380 | 2.72M | if (!ossl_prov_is_running()) |
381 | 0 | return 0; |
382 | | |
383 | 2.72M | i = gcm_cipher_internal(ctx, out, outl, NULL, 0); |
384 | 2.72M | if (i <= 0) |
385 | 941k | return 0; |
386 | | |
387 | 1.78M | *outl = 0; |
388 | 1.78M | return 1; |
389 | 2.72M | } |
390 | | |
391 | | int ossl_gcm_cipher(void *vctx, |
392 | | unsigned char *out, size_t *outl, size_t outsize, |
393 | | const unsigned char *in, size_t inl) |
394 | 0 | { |
395 | 0 | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
396 | |
|
397 | 0 | if (!ossl_prov_is_running()) |
398 | 0 | return 0; |
399 | | |
400 | 0 | if (outsize < inl) { |
401 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
402 | 0 | return 0; |
403 | 0 | } |
404 | | |
405 | 0 | if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0) |
406 | 0 | return 0; |
407 | | |
408 | 0 | *outl = inl; |
409 | 0 | return 1; |
410 | 0 | } |
411 | | |
412 | | /* |
413 | | * See SP800-38D (GCM) Section 8 "Uniqueness requirement on IVS and keys" |
414 | | * |
415 | | * See also 8.2.2 RBG-based construction. |
416 | | * Random construction consists of a free field (which can be NULL) and a |
417 | | * random field which will use a DRBG that can return at least 96 bits of |
418 | | * entropy strength. (The DRBG must be seeded by the FIPS module). |
419 | | */ |
420 | | static int gcm_iv_generate(PROV_GCM_CTX *ctx, int offset) |
421 | 512 | { |
422 | 512 | int sz = (int)(ctx->ivlen - offset); |
423 | | |
424 | | /* Must be at least 96 bits */ |
425 | 512 | if (sz <= 0 || ctx->ivlen < GCM_IV_DEFAULT_SIZE) |
426 | 0 | return 0; |
427 | | |
428 | | /* Use DRBG to generate random iv */ |
429 | 512 | if (RAND_bytes_ex(ctx->libctx, ctx->iv + offset, sz, 0) <= 0) |
430 | 0 | return 0; |
431 | 512 | ctx->iv_state = IV_STATE_BUFFERED; |
432 | 512 | ctx->iv_gen_rand = 1; |
433 | 512 | return 1; |
434 | 512 | } |
435 | | |
436 | | /* |
437 | | * If we try to grab the iv before the update - it wont be generated yet, |
438 | | * so we generate it here for this case. |
439 | | */ |
440 | | static int on_preupdate_generate_iv(PROV_GCM_CTX *ctx) |
441 | 0 | { |
442 | 0 | if (ctx->iv_state == IV_STATE_UNINITIALISED) { |
443 | 0 | if (ctx->tls_aad_len != UNINITIALISED_SIZET) |
444 | 0 | return 0; |
445 | 0 | if (!ctx->enc || !gcm_iv_generate(ctx, 0)) |
446 | 0 | return 0; |
447 | 0 | } |
448 | 0 | return 1; |
449 | 0 | } |
450 | | |
451 | | static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out, |
452 | | size_t *padlen, const unsigned char *in, |
453 | | size_t len) |
454 | 1.37M | { |
455 | 1.37M | size_t olen = 0; |
456 | 1.37M | int rv = 0; |
457 | 1.37M | const PROV_GCM_HW *hw = ctx->hw; |
458 | | |
459 | 1.37M | if (ctx->tls_aad_len != UNINITIALISED_SIZET) |
460 | 16.5k | return gcm_tls_cipher(ctx, out, padlen, in, len); |
461 | | |
462 | 1.35M | if (!ctx->key_set || ctx->iv_state == IV_STATE_FINISHED) |
463 | 0 | goto err; |
464 | | |
465 | | /* |
466 | | * FIPS requires generation of AES-GCM IV's inside the FIPS module. |
467 | | * The IV can still be set externally (the security policy will state that |
468 | | * this is not FIPS compliant). There are some applications |
469 | | * where setting the IV externally is the only option available. |
470 | | */ |
471 | 1.35M | if (ctx->iv_state == IV_STATE_UNINITIALISED) { |
472 | 0 | if (!ctx->enc || !gcm_iv_generate(ctx, 0)) |
473 | 0 | goto err; |
474 | 0 | } |
475 | | |
476 | 1.35M | if (ctx->iv_state == IV_STATE_BUFFERED) { |
477 | 420k | if (!hw->setiv(ctx, ctx->iv, ctx->ivlen)) |
478 | 0 | goto err; |
479 | 420k | ctx->iv_state = IV_STATE_COPIED; |
480 | 420k | } |
481 | | |
482 | 1.35M | if (in != NULL) { |
483 | | /* The input is AAD if out is NULL */ |
484 | 936k | if (out == NULL) { |
485 | 612k | if (!hw->aadupdate(ctx, in, len)) |
486 | 0 | goto err; |
487 | 612k | } else { |
488 | | /* The input is ciphertext OR plaintext */ |
489 | 324k | if (!hw->cipherupdate(ctx, in, len, out)) |
490 | 0 | goto err; |
491 | 324k | } |
492 | 936k | } else { |
493 | | /* The tag must be set before actually decrypting data */ |
494 | 420k | if (!ctx->enc && ctx->taglen == UNINITIALISED_SIZET) { |
495 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET); |
496 | 0 | goto err; |
497 | 0 | } |
498 | 420k | if (hw->cipherfinal(ctx, ctx->buf) == 0) { |
499 | 72.8k | if (ctx->enc == 0) |
500 | 72.8k | ERR_raise(ERR_LIB_PROV, PROV_R_BAD_DECRYPT); |
501 | 72.8k | goto err; |
502 | 72.8k | } |
503 | 347k | ctx->iv_state = IV_STATE_FINISHED; /* Don't reuse the IV */ |
504 | 347k | goto finish; |
505 | 420k | } |
506 | 936k | olen = len; |
507 | 1.28M | finish: |
508 | 1.28M | rv = 1; |
509 | 1.35M | err: |
510 | 1.35M | *padlen = olen; |
511 | 1.35M | return rv; |
512 | 1.28M | } |
513 | | |
514 | | static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len) |
515 | 114k | { |
516 | 114k | unsigned char *buf; |
517 | 114k | size_t len; |
518 | | |
519 | 114k | if (!ossl_prov_is_running() || aad_len != EVP_AEAD_TLS1_AAD_LEN) |
520 | 0 | return 0; |
521 | | |
522 | | /* Save the aad for later use. */ |
523 | 114k | buf = dat->buf; |
524 | 114k | memcpy(buf, aad, aad_len); |
525 | 114k | dat->tls_aad_len = aad_len; |
526 | | |
527 | 114k | len = buf[aad_len - 2] << 8 | buf[aad_len - 1]; |
528 | | /* Correct length for explicit iv. */ |
529 | 114k | if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN) |
530 | 0 | return 0; |
531 | 114k | len -= EVP_GCM_TLS_EXPLICIT_IV_LEN; |
532 | | |
533 | | /* If decrypting correct for tag too. */ |
534 | 114k | if (!dat->enc) { |
535 | 112k | if (len < EVP_GCM_TLS_TAG_LEN) |
536 | 0 | return 0; |
537 | 112k | len -= EVP_GCM_TLS_TAG_LEN; |
538 | 112k | } |
539 | 114k | buf[aad_len - 2] = (unsigned char)(len >> 8); |
540 | 114k | buf[aad_len - 1] = (unsigned char)(len & 0xff); |
541 | | /* Extra padding: tag appended to record. */ |
542 | 114k | return EVP_GCM_TLS_TAG_LEN; |
543 | 114k | } |
544 | | |
545 | | static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv, |
546 | | size_t len) |
547 | 1.73k | { |
548 | | /* Special case: -1 length restores whole IV */ |
549 | 1.73k | if (len == (size_t)-1) { |
550 | 0 | memcpy(ctx->iv, iv, ctx->ivlen); |
551 | 0 | ctx->iv_gen_rand = 0; |
552 | 0 | ctx->iv_gen = 1; |
553 | 0 | ctx->iv_state = IV_STATE_BUFFERED; |
554 | 0 | return 1; |
555 | 0 | } |
556 | | /* Fixed field must be at least 4 bytes and invocation field at least 8 */ |
557 | 1.73k | if (len < EVP_GCM_TLS_FIXED_IV_LEN |
558 | 1.73k | || len > ctx->ivlen |
559 | 1.73k | || (ctx->ivlen - len) < EVP_GCM_TLS_EXPLICIT_IV_LEN) |
560 | 0 | return 0; |
561 | 1.73k | if (len > 0) |
562 | 1.73k | memcpy(ctx->iv, iv, len); |
563 | 1.73k | if (ctx->enc) { |
564 | 619 | if (RAND_bytes_ex(ctx->libctx, ctx->iv + len, ctx->ivlen - len, 0) <= 0) |
565 | 0 | return 0; |
566 | 619 | ctx->iv_gen_rand = 1; |
567 | 619 | } |
568 | 1.73k | ctx->iv_gen = 1; |
569 | 1.73k | ctx->iv_state = IV_STATE_BUFFERED; |
570 | 1.73k | return 1; |
571 | 1.73k | } |
572 | | |
573 | | /* |
574 | | * Handle TLS GCM packet format. This consists of the last portion of the IV |
575 | | * followed by the payload and finally the tag. On encrypt generate IV, |
576 | | * encrypt payload and write the tag. On verify retrieve IV, decrypt payload |
577 | | * and verify tag. |
578 | | */ |
579 | | static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen, |
580 | | const unsigned char *in, size_t len) |
581 | 90.5k | { |
582 | 90.5k | int rv = 0; |
583 | 90.5k | size_t arg = EVP_GCM_TLS_EXPLICIT_IV_LEN; |
584 | 90.5k | size_t plen = 0; |
585 | 90.5k | unsigned char *tag = NULL; |
586 | | |
587 | 90.5k | if (!ossl_prov_is_running() || !ctx->key_set) |
588 | 0 | goto err; |
589 | | |
590 | | /* Encrypt/decrypt must be performed in place */ |
591 | 90.5k | if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN)) |
592 | 0 | goto err; |
593 | | |
594 | | /* |
595 | | * Check for too many keys as per FIPS 140-2 IG A.5 "Key/IV Pair Uniqueness |
596 | | * Requirements from SP 800-38D". The requirements is for one party to the |
597 | | * communication to fail after 2^64 - 1 keys. We do this on the encrypting |
598 | | * side only. |
599 | | */ |
600 | 90.5k | if (ctx->enc && ++ctx->tls_enc_records == 0) { |
601 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TOO_MANY_RECORDS); |
602 | 0 | goto err; |
603 | 0 | } |
604 | | |
605 | | /* |
606 | | * Set IV from start of buffer or generate IV and write to start of |
607 | | * buffer. |
608 | | */ |
609 | 90.5k | if (ctx->enc) { |
610 | 1.65k | if (!getivgen(ctx, out, arg)) |
611 | 0 | goto err; |
612 | 88.9k | } else { |
613 | 88.9k | if (!setivinv(ctx, out, arg)) |
614 | 0 | goto err; |
615 | 88.9k | } |
616 | | |
617 | | /* Fix buffer and length to point to payload */ |
618 | 90.5k | in += EVP_GCM_TLS_EXPLICIT_IV_LEN; |
619 | 90.5k | out += EVP_GCM_TLS_EXPLICIT_IV_LEN; |
620 | 90.5k | len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; |
621 | | |
622 | 90.5k | tag = ctx->enc ? out + len : (unsigned char *)in + len; |
623 | 90.5k | if (!ctx->hw->oneshot(ctx, ctx->buf, ctx->tls_aad_len, in, len, out, tag, |
624 | 90.5k | EVP_GCM_TLS_TAG_LEN)) { |
625 | 88.8k | if (!ctx->enc) |
626 | 88.8k | OPENSSL_cleanse(out, len); |
627 | 88.8k | goto err; |
628 | 88.8k | } |
629 | 1.79k | if (ctx->enc) |
630 | 1.65k | plen = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; |
631 | 143 | else |
632 | 143 | plen = len; |
633 | | |
634 | 1.79k | rv = 1; |
635 | 90.5k | err: |
636 | 90.5k | ctx->iv_state = IV_STATE_FINISHED; |
637 | 90.5k | ctx->tls_aad_len = UNINITIALISED_SIZET; |
638 | 90.5k | *padlen = plen; |
639 | 90.5k | return rv; |
640 | 1.79k | } |