/src/openssl/providers/implementations/ciphers/ciphercommon_gcm.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2019-2024 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 | | |
11 | | /* Dispatch functions for gcm mode */ |
12 | | |
13 | | #include <openssl/rand.h> |
14 | | #include <openssl/proverr.h> |
15 | | #include "prov/ciphercommon.h" |
16 | | #include "prov/ciphercommon_gcm.h" |
17 | | #include "prov/providercommon.h" |
18 | | #include "prov/provider_ctx.h" |
19 | | |
20 | | static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len); |
21 | | static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv, |
22 | | size_t len); |
23 | | static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen, |
24 | | const unsigned char *in, size_t len); |
25 | | static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out, |
26 | | size_t *padlen, const unsigned char *in, |
27 | | size_t len); |
28 | | |
29 | | /* |
30 | | * Called from EVP_CipherInit when there is currently no context via |
31 | | * the new_ctx() function |
32 | | */ |
33 | | void ossl_gcm_initctx(void *provctx, PROV_GCM_CTX *ctx, size_t keybits, |
34 | | const PROV_GCM_HW *hw) |
35 | 0 | { |
36 | 0 | ctx->pad = 1; |
37 | 0 | ctx->mode = EVP_CIPH_GCM_MODE; |
38 | 0 | ctx->taglen = UNINITIALISED_SIZET; |
39 | 0 | ctx->tls_aad_len = UNINITIALISED_SIZET; |
40 | 0 | ctx->ivlen = (EVP_GCM_TLS_FIXED_IV_LEN + EVP_GCM_TLS_EXPLICIT_IV_LEN); |
41 | 0 | ctx->keylen = keybits / 8; |
42 | 0 | ctx->hw = hw; |
43 | 0 | ctx->libctx = PROV_LIBCTX_OF(provctx); |
44 | 0 | } |
45 | | |
46 | | /* |
47 | | * Called by EVP_CipherInit via the _einit and _dinit functions |
48 | | */ |
49 | | static int gcm_init(void *vctx, const unsigned char *key, size_t keylen, |
50 | | const unsigned char *iv, size_t ivlen, |
51 | | const OSSL_PARAM params[], int enc) |
52 | 0 | { |
53 | 0 | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
54 | |
|
55 | 0 | if (!ossl_prov_is_running()) |
56 | 0 | return 0; |
57 | | |
58 | 0 | ctx->enc = enc; |
59 | |
|
60 | 0 | if (iv != NULL) { |
61 | 0 | if (ivlen == 0 || ivlen > sizeof(ctx->iv)) { |
62 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
63 | 0 | return 0; |
64 | 0 | } |
65 | 0 | ctx->ivlen = ivlen; |
66 | 0 | memcpy(ctx->iv, iv, ivlen); |
67 | 0 | ctx->iv_state = IV_STATE_BUFFERED; |
68 | 0 | } |
69 | | |
70 | 0 | if (key != NULL) { |
71 | 0 | if (keylen != ctx->keylen) { |
72 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
73 | 0 | return 0; |
74 | 0 | } |
75 | 0 | if (!ctx->hw->setkey(ctx, key, ctx->keylen)) |
76 | 0 | return 0; |
77 | 0 | ctx->tls_enc_records = 0; |
78 | 0 | } |
79 | 0 | return ossl_gcm_set_ctx_params(ctx, params); |
80 | 0 | } |
81 | | |
82 | | int ossl_gcm_einit(void *vctx, const unsigned char *key, size_t keylen, |
83 | | const unsigned char *iv, size_t ivlen, |
84 | | const OSSL_PARAM params[]) |
85 | 0 | { |
86 | 0 | return gcm_init(vctx, key, keylen, iv, ivlen, params, 1); |
87 | 0 | } |
88 | | |
89 | | int ossl_gcm_dinit(void *vctx, const unsigned char *key, size_t keylen, |
90 | | const unsigned char *iv, size_t ivlen, |
91 | | const OSSL_PARAM params[]) |
92 | 0 | { |
93 | 0 | return gcm_init(vctx, key, keylen, iv, ivlen, params, 0); |
94 | 0 | } |
95 | | |
96 | | /* increment counter (64-bit int) by 1 */ |
97 | | static void ctr64_inc(unsigned char *counter) |
98 | 0 | { |
99 | 0 | int n = 8; |
100 | 0 | unsigned char c; |
101 | |
|
102 | 0 | do { |
103 | 0 | --n; |
104 | 0 | c = counter[n]; |
105 | 0 | ++c; |
106 | 0 | counter[n] = c; |
107 | 0 | if (c > 0) |
108 | 0 | return; |
109 | 0 | } while (n > 0); |
110 | 0 | } |
111 | | |
112 | | static int getivgen(PROV_GCM_CTX *ctx, unsigned char *out, size_t olen) |
113 | 0 | { |
114 | 0 | if (!ctx->iv_gen |
115 | 0 | || !ctx->key_set |
116 | 0 | || !ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen)) |
117 | 0 | return 0; |
118 | 0 | if (olen == 0 || olen > ctx->ivlen) |
119 | 0 | olen = ctx->ivlen; |
120 | 0 | memcpy(out, ctx->iv + ctx->ivlen - olen, olen); |
121 | | /* |
122 | | * Invocation field will be at least 8 bytes in size and so no need |
123 | | * to check wrap around or increment more than last 8 bytes. |
124 | | */ |
125 | 0 | ctr64_inc(ctx->iv + ctx->ivlen - 8); |
126 | 0 | ctx->iv_state = IV_STATE_COPIED; |
127 | 0 | return 1; |
128 | 0 | } |
129 | | |
130 | | static int setivinv(PROV_GCM_CTX *ctx, unsigned char *in, size_t inl) |
131 | 0 | { |
132 | 0 | if (!ctx->iv_gen |
133 | 0 | || !ctx->key_set |
134 | 0 | || ctx->enc) |
135 | 0 | return 0; |
136 | | |
137 | 0 | memcpy(ctx->iv + ctx->ivlen - inl, in, inl); |
138 | 0 | if (!ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen)) |
139 | 0 | return 0; |
140 | 0 | ctx->iv_state = IV_STATE_COPIED; |
141 | 0 | return 1; |
142 | 0 | } |
143 | | |
144 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
145 | | #ifndef ossl_cipher_gcm_get_ctx_params_list |
146 | | static const OSSL_PARAM ossl_cipher_gcm_get_ctx_params_list[] = { |
147 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), |
148 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), |
149 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, NULL), |
150 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_IV, NULL, 0), |
151 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, NULL, 0), |
152 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0), |
153 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, NULL), |
154 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN, NULL, 0), |
155 | | OSSL_PARAM_uint(OSSL_CIPHER_PARAM_AEAD_IV_GENERATED, NULL), |
156 | | OSSL_PARAM_END |
157 | | }; |
158 | | #endif |
159 | | |
160 | | #ifndef ossl_cipher_gcm_get_ctx_params_st |
161 | | struct ossl_cipher_gcm_get_ctx_params_st { |
162 | | OSSL_PARAM *gen; |
163 | | OSSL_PARAM *iv; |
164 | | OSSL_PARAM *ivgen; |
165 | | OSSL_PARAM *ivlen; |
166 | | OSSL_PARAM *keylen; |
167 | | OSSL_PARAM *pad; |
168 | | OSSL_PARAM *tag; |
169 | | OSSL_PARAM *taglen; |
170 | | OSSL_PARAM *updiv; |
171 | | }; |
172 | | #endif |
173 | | |
174 | | #ifndef ossl_cipher_gcm_get_ctx_params_decoder |
175 | | static int ossl_cipher_gcm_get_ctx_params_decoder |
176 | | (const OSSL_PARAM *p, struct ossl_cipher_gcm_get_ctx_params_st *r) |
177 | 0 | { |
178 | 0 | const char *s; |
179 | |
|
180 | 0 | memset(r, 0, sizeof(*r)); |
181 | 0 | if (p != NULL) |
182 | 0 | for (; (s = p->key) != NULL; p++) |
183 | 0 | switch(s[0]) { |
184 | 0 | default: |
185 | 0 | break; |
186 | 0 | case 'i': |
187 | 0 | switch(s[1]) { |
188 | 0 | default: |
189 | 0 | break; |
190 | 0 | case 'v': |
191 | 0 | switch(s[2]) { |
192 | 0 | default: |
193 | 0 | break; |
194 | 0 | case '-': |
195 | 0 | if (ossl_likely(strcmp("generated", s + 3) == 0)) { |
196 | | /* CIPHER_PARAM_AEAD_IV_GENERATED */ |
197 | 0 | if (ossl_unlikely(r->gen != NULL)) { |
198 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
199 | 0 | "param %s is repeated", s); |
200 | 0 | return 0; |
201 | 0 | } |
202 | 0 | r->gen = (OSSL_PARAM *)p; |
203 | 0 | } |
204 | 0 | break; |
205 | 0 | case 'l': |
206 | 0 | if (ossl_likely(strcmp("en", s + 3) == 0)) { |
207 | | /* CIPHER_PARAM_IVLEN */ |
208 | 0 | if (ossl_unlikely(r->ivlen != NULL)) { |
209 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
210 | 0 | "param %s is repeated", s); |
211 | 0 | return 0; |
212 | 0 | } |
213 | 0 | r->ivlen = (OSSL_PARAM *)p; |
214 | 0 | } |
215 | 0 | break; |
216 | 0 | case '\0': |
217 | 0 | if (ossl_unlikely(r->iv != NULL)) { |
218 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
219 | 0 | "param %s is repeated", s); |
220 | 0 | return 0; |
221 | 0 | } |
222 | 0 | r->iv = (OSSL_PARAM *)p; |
223 | 0 | } |
224 | 0 | } |
225 | 0 | break; |
226 | 0 | case 'k': |
227 | 0 | if (ossl_likely(strcmp("eylen", s + 1) == 0)) { |
228 | | /* CIPHER_PARAM_KEYLEN */ |
229 | 0 | if (ossl_unlikely(r->keylen != NULL)) { |
230 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
231 | 0 | "param %s is repeated", s); |
232 | 0 | return 0; |
233 | 0 | } |
234 | 0 | r->keylen = (OSSL_PARAM *)p; |
235 | 0 | } |
236 | 0 | break; |
237 | 0 | case 't': |
238 | 0 | switch(s[1]) { |
239 | 0 | default: |
240 | 0 | break; |
241 | 0 | case 'a': |
242 | 0 | switch(s[2]) { |
243 | 0 | default: |
244 | 0 | break; |
245 | 0 | case 'g': |
246 | 0 | switch(s[3]) { |
247 | 0 | default: |
248 | 0 | break; |
249 | 0 | case 'l': |
250 | 0 | if (ossl_likely(strcmp("en", s + 4) == 0)) { |
251 | | /* CIPHER_PARAM_AEAD_TAGLEN */ |
252 | 0 | if (ossl_unlikely(r->taglen != NULL)) { |
253 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
254 | 0 | "param %s is repeated", s); |
255 | 0 | return 0; |
256 | 0 | } |
257 | 0 | r->taglen = (OSSL_PARAM *)p; |
258 | 0 | } |
259 | 0 | break; |
260 | 0 | case '\0': |
261 | 0 | if (ossl_unlikely(r->tag != NULL)) { |
262 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
263 | 0 | "param %s is repeated", s); |
264 | 0 | return 0; |
265 | 0 | } |
266 | 0 | r->tag = (OSSL_PARAM *)p; |
267 | 0 | } |
268 | 0 | } |
269 | 0 | break; |
270 | 0 | case 'l': |
271 | 0 | switch(s[2]) { |
272 | 0 | default: |
273 | 0 | break; |
274 | 0 | case 's': |
275 | 0 | switch(s[3]) { |
276 | 0 | default: |
277 | 0 | break; |
278 | 0 | case 'a': |
279 | 0 | if (ossl_likely(strcmp("adpad", s + 4) == 0)) { |
280 | | /* CIPHER_PARAM_AEAD_TLS1_AAD_PAD */ |
281 | 0 | if (ossl_unlikely(r->pad != NULL)) { |
282 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
283 | 0 | "param %s is repeated", s); |
284 | 0 | return 0; |
285 | 0 | } |
286 | 0 | r->pad = (OSSL_PARAM *)p; |
287 | 0 | } |
288 | 0 | break; |
289 | 0 | case 'i': |
290 | 0 | if (ossl_likely(strcmp("vgen", s + 4) == 0)) { |
291 | | /* CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN */ |
292 | 0 | if (ossl_unlikely(r->ivgen != NULL)) { |
293 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
294 | 0 | "param %s is repeated", s); |
295 | 0 | return 0; |
296 | 0 | } |
297 | 0 | r->ivgen = (OSSL_PARAM *)p; |
298 | 0 | } |
299 | 0 | } |
300 | 0 | } |
301 | 0 | } |
302 | 0 | break; |
303 | 0 | case 'u': |
304 | 0 | if (ossl_likely(strcmp("pdated-iv", s + 1) == 0)) { |
305 | | /* CIPHER_PARAM_UPDATED_IV */ |
306 | 0 | if (ossl_unlikely(r->updiv != NULL)) { |
307 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
308 | 0 | "param %s is repeated", s); |
309 | 0 | return 0; |
310 | 0 | } |
311 | 0 | r->updiv = (OSSL_PARAM *)p; |
312 | 0 | } |
313 | 0 | } |
314 | 0 | return 1; |
315 | 0 | } |
316 | | #endif |
317 | | /* End of machine generated */ |
318 | | |
319 | | const OSSL_PARAM *ossl_gcm_gettable_ctx_params( |
320 | | ossl_unused void *cctx, ossl_unused void *provctx |
321 | | ) |
322 | 112 | { |
323 | 112 | return ossl_cipher_gcm_get_ctx_params_list; |
324 | 112 | } |
325 | | |
326 | | int ossl_gcm_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
327 | 0 | { |
328 | 0 | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
329 | 0 | size_t sz; |
330 | 0 | struct ossl_cipher_gcm_get_ctx_params_st p; |
331 | |
|
332 | 0 | if (ctx == NULL || !ossl_cipher_gcm_get_ctx_params_decoder(params, &p)) |
333 | 0 | return 0; |
334 | | |
335 | 0 | if (p.ivlen != NULL && !OSSL_PARAM_set_size_t(p.ivlen, ctx->ivlen)) { |
336 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
337 | 0 | return 0; |
338 | 0 | } |
339 | | |
340 | 0 | if (p.keylen != NULL && !OSSL_PARAM_set_size_t(p.keylen, ctx->keylen)) { |
341 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
342 | 0 | return 0; |
343 | 0 | } |
344 | | |
345 | 0 | if (p.taglen != NULL) { |
346 | 0 | size_t taglen = (ctx->taglen != UNINITIALISED_SIZET) ? ctx->taglen : |
347 | 0 | GCM_TAG_MAX_SIZE; |
348 | |
|
349 | 0 | if (!OSSL_PARAM_set_size_t(p.taglen, taglen)) { |
350 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
351 | 0 | return 0; |
352 | 0 | } |
353 | 0 | } |
354 | | |
355 | 0 | if (p.iv != NULL) { |
356 | 0 | if (ctx->iv_state == IV_STATE_UNINITIALISED) |
357 | 0 | return 0; |
358 | 0 | if (ctx->ivlen > p.iv->data_size) { |
359 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
360 | 0 | return 0; |
361 | 0 | } |
362 | 0 | if (!OSSL_PARAM_set_octet_string_or_ptr(p.iv, ctx->iv, ctx->ivlen)) { |
363 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
364 | 0 | return 0; |
365 | 0 | } |
366 | 0 | } |
367 | | |
368 | 0 | if (p.updiv != NULL) { |
369 | 0 | if (ctx->iv_state == IV_STATE_UNINITIALISED) |
370 | 0 | return 0; |
371 | 0 | if (ctx->ivlen > p.updiv->data_size) { |
372 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
373 | 0 | return 0; |
374 | 0 | } |
375 | 0 | if (!OSSL_PARAM_set_octet_string_or_ptr(p.updiv, ctx->iv, ctx->ivlen)) { |
376 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
377 | 0 | return 0; |
378 | 0 | } |
379 | 0 | } |
380 | | |
381 | 0 | if (p.pad != NULL && !OSSL_PARAM_set_size_t(p.pad, ctx->tls_aad_pad_sz)) { |
382 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
383 | 0 | return 0; |
384 | 0 | } |
385 | | |
386 | 0 | if (p.tag != NULL) { |
387 | 0 | sz = p.tag->data_size; |
388 | 0 | if (sz == 0 |
389 | 0 | || sz > EVP_GCM_TLS_TAG_LEN |
390 | 0 | || !ctx->enc |
391 | 0 | || ctx->taglen == UNINITIALISED_SIZET) { |
392 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG); |
393 | 0 | return 0; |
394 | 0 | } |
395 | 0 | if (!OSSL_PARAM_set_octet_string(p.tag, ctx->buf, sz)) { |
396 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
397 | 0 | return 0; |
398 | 0 | } |
399 | 0 | } |
400 | | |
401 | 0 | if (p.ivgen != NULL) |
402 | 0 | if (p.ivgen->data == NULL |
403 | 0 | || p.ivgen->data_type != OSSL_PARAM_OCTET_STRING |
404 | 0 | || !getivgen(ctx, p.ivgen->data, p.ivgen->data_size)) |
405 | 0 | return 0; |
406 | | |
407 | 0 | if (p.gen != NULL && !OSSL_PARAM_set_uint(p.gen, ctx->iv_gen_rand)) |
408 | 0 | return 0; |
409 | | |
410 | 0 | return 1; |
411 | 0 | } |
412 | | |
413 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
414 | | #ifndef ossl_cipher_gcm_set_ctx_params_list |
415 | | static const OSSL_PARAM ossl_cipher_gcm_set_ctx_params_list[] = { |
416 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN, NULL), |
417 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0), |
418 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD, NULL, 0), |
419 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, NULL, 0), |
420 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV, NULL, 0), |
421 | | OSSL_PARAM_END |
422 | | }; |
423 | | #endif |
424 | | |
425 | | #ifndef ossl_cipher_gcm_set_ctx_params_st |
426 | | struct ossl_cipher_gcm_set_ctx_params_st { |
427 | | OSSL_PARAM *aad; |
428 | | OSSL_PARAM *fixed; |
429 | | OSSL_PARAM *inviv; |
430 | | OSSL_PARAM *ivlen; |
431 | | OSSL_PARAM *tag; |
432 | | }; |
433 | | #endif |
434 | | |
435 | | #ifndef ossl_cipher_gcm_set_ctx_params_decoder |
436 | | static int ossl_cipher_gcm_set_ctx_params_decoder |
437 | | (const OSSL_PARAM *p, struct ossl_cipher_gcm_set_ctx_params_st *r) |
438 | 0 | { |
439 | 0 | const char *s; |
440 | |
|
441 | 0 | memset(r, 0, sizeof(*r)); |
442 | 0 | if (p != NULL) |
443 | 0 | for (; (s = p->key) != NULL; p++) |
444 | 0 | switch(s[0]) { |
445 | 0 | default: |
446 | 0 | break; |
447 | 0 | case 'i': |
448 | 0 | if (ossl_likely(strcmp("vlen", s + 1) == 0)) { |
449 | | /* CIPHER_PARAM_AEAD_IVLEN */ |
450 | 0 | if (ossl_unlikely(r->ivlen != NULL)) { |
451 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
452 | 0 | "param %s is repeated", s); |
453 | 0 | return 0; |
454 | 0 | } |
455 | 0 | r->ivlen = (OSSL_PARAM *)p; |
456 | 0 | } |
457 | 0 | break; |
458 | 0 | case 't': |
459 | 0 | switch(s[1]) { |
460 | 0 | default: |
461 | 0 | break; |
462 | 0 | case 'a': |
463 | 0 | if (ossl_likely(strcmp("g", s + 2) == 0)) { |
464 | | /* CIPHER_PARAM_AEAD_TAG */ |
465 | 0 | if (ossl_unlikely(r->tag != NULL)) { |
466 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
467 | 0 | "param %s is repeated", s); |
468 | 0 | return 0; |
469 | 0 | } |
470 | 0 | r->tag = (OSSL_PARAM *)p; |
471 | 0 | } |
472 | 0 | break; |
473 | 0 | case 'l': |
474 | 0 | switch(s[2]) { |
475 | 0 | default: |
476 | 0 | break; |
477 | 0 | case 's': |
478 | 0 | switch(s[3]) { |
479 | 0 | default: |
480 | 0 | break; |
481 | 0 | case 'a': |
482 | 0 | if (ossl_likely(strcmp("ad", s + 4) == 0)) { |
483 | | /* CIPHER_PARAM_AEAD_TLS1_AAD */ |
484 | 0 | if (ossl_unlikely(r->aad != NULL)) { |
485 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
486 | 0 | "param %s is repeated", s); |
487 | 0 | return 0; |
488 | 0 | } |
489 | 0 | r->aad = (OSSL_PARAM *)p; |
490 | 0 | } |
491 | 0 | break; |
492 | 0 | case 'i': |
493 | 0 | switch(s[4]) { |
494 | 0 | default: |
495 | 0 | break; |
496 | 0 | case 'v': |
497 | 0 | switch(s[5]) { |
498 | 0 | default: |
499 | 0 | break; |
500 | 0 | case 'f': |
501 | 0 | if (ossl_likely(strcmp("ixed", s + 6) == 0)) { |
502 | | /* CIPHER_PARAM_AEAD_TLS1_IV_FIXED */ |
503 | 0 | if (ossl_unlikely(r->fixed != NULL)) { |
504 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
505 | 0 | "param %s is repeated", s); |
506 | 0 | return 0; |
507 | 0 | } |
508 | 0 | r->fixed = (OSSL_PARAM *)p; |
509 | 0 | } |
510 | 0 | break; |
511 | 0 | case 'i': |
512 | 0 | if (ossl_likely(strcmp("nv", s + 6) == 0)) { |
513 | | /* CIPHER_PARAM_AEAD_TLS1_SET_IV_INV */ |
514 | 0 | if (ossl_unlikely(r->inviv != NULL)) { |
515 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
516 | 0 | "param %s is repeated", s); |
517 | 0 | return 0; |
518 | 0 | } |
519 | 0 | r->inviv = (OSSL_PARAM *)p; |
520 | 0 | } |
521 | 0 | } |
522 | 0 | } |
523 | 0 | } |
524 | 0 | } |
525 | 0 | } |
526 | 0 | } |
527 | 0 | return 1; |
528 | 0 | } |
529 | | #endif |
530 | | /* End of machine generated */ |
531 | | |
532 | | const OSSL_PARAM *ossl_gcm_settable_ctx_params( |
533 | | ossl_unused void *cctx, ossl_unused void *provctx |
534 | | ) |
535 | 0 | { |
536 | 0 | return ossl_cipher_gcm_set_ctx_params_list; |
537 | 0 | } |
538 | | |
539 | | int ossl_gcm_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
540 | 0 | { |
541 | 0 | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
542 | 0 | size_t sz; |
543 | 0 | void *vp; |
544 | 0 | struct ossl_cipher_gcm_set_ctx_params_st p; |
545 | |
|
546 | 0 | if (ctx == NULL || !ossl_cipher_gcm_set_ctx_params_decoder(params, &p)) |
547 | 0 | return 0; |
548 | | |
549 | 0 | if (p.tag != NULL) { |
550 | 0 | vp = ctx->buf; |
551 | 0 | if (!OSSL_PARAM_get_octet_string(p.tag, &vp, EVP_GCM_TLS_TAG_LEN, &sz)) { |
552 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
553 | 0 | return 0; |
554 | 0 | } |
555 | 0 | if (sz == 0 || ctx->enc) { |
556 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG); |
557 | 0 | return 0; |
558 | 0 | } |
559 | 0 | ctx->taglen = sz; |
560 | 0 | } |
561 | | |
562 | 0 | if (p.ivlen != NULL) { |
563 | 0 | if (!OSSL_PARAM_get_size_t(p.ivlen, &sz)) { |
564 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
565 | 0 | return 0; |
566 | 0 | } |
567 | 0 | if (sz == 0 || sz > sizeof(ctx->iv)) { |
568 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
569 | 0 | return 0; |
570 | 0 | } |
571 | 0 | if (ctx->ivlen != sz) { |
572 | | /* If the iv was already set or autogenerated, it is invalid. */ |
573 | 0 | if (ctx->iv_state != IV_STATE_UNINITIALISED) |
574 | 0 | ctx->iv_state = IV_STATE_FINISHED; |
575 | 0 | ctx->ivlen = sz; |
576 | 0 | } |
577 | 0 | } |
578 | | |
579 | 0 | if (p.aad != NULL) { |
580 | 0 | if (p.aad->data_type != OSSL_PARAM_OCTET_STRING) { |
581 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
582 | 0 | return 0; |
583 | 0 | } |
584 | 0 | sz = gcm_tls_init(ctx, p.aad->data, p.aad->data_size); |
585 | 0 | if (sz == 0) { |
586 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_AAD); |
587 | 0 | return 0; |
588 | 0 | } |
589 | 0 | ctx->tls_aad_pad_sz = sz; |
590 | 0 | } |
591 | | |
592 | 0 | if (p.fixed != NULL) { |
593 | 0 | if (p.fixed->data_type != OSSL_PARAM_OCTET_STRING) { |
594 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
595 | 0 | return 0; |
596 | 0 | } |
597 | 0 | if (gcm_tls_iv_set_fixed(ctx, p.fixed->data, p.fixed->data_size) == 0) { |
598 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
599 | 0 | return 0; |
600 | 0 | } |
601 | 0 | } |
602 | | |
603 | 0 | if (p.inviv != NULL) |
604 | 0 | if (p.inviv->data == NULL |
605 | 0 | || p.inviv->data_type != OSSL_PARAM_OCTET_STRING |
606 | 0 | || !setivinv(ctx, p.inviv->data, p.inviv->data_size)) |
607 | 0 | return 0; |
608 | | |
609 | 0 | return 1; |
610 | 0 | } |
611 | | |
612 | | int ossl_gcm_stream_update(void *vctx, unsigned char *out, size_t *outl, |
613 | | size_t outsize, const unsigned char *in, size_t inl) |
614 | 0 | { |
615 | 0 | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
616 | |
|
617 | 0 | if (inl == 0) { |
618 | 0 | *outl = 0; |
619 | 0 | return 1; |
620 | 0 | } |
621 | | |
622 | 0 | if (outsize < inl) { |
623 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
624 | 0 | return 0; |
625 | 0 | } |
626 | | |
627 | 0 | if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0) { |
628 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED); |
629 | 0 | return 0; |
630 | 0 | } |
631 | 0 | return 1; |
632 | 0 | } |
633 | | |
634 | | int ossl_gcm_stream_final(void *vctx, unsigned char *out, size_t *outl, |
635 | | size_t outsize) |
636 | 0 | { |
637 | 0 | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
638 | 0 | int i; |
639 | |
|
640 | 0 | if (!ossl_prov_is_running()) |
641 | 0 | return 0; |
642 | | |
643 | 0 | i = gcm_cipher_internal(ctx, out, outl, NULL, 0); |
644 | 0 | if (i <= 0) |
645 | 0 | return 0; |
646 | | |
647 | 0 | *outl = 0; |
648 | 0 | return 1; |
649 | 0 | } |
650 | | |
651 | | int ossl_gcm_cipher(void *vctx, |
652 | | unsigned char *out, size_t *outl, size_t outsize, |
653 | | const unsigned char *in, size_t inl) |
654 | 0 | { |
655 | 0 | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
656 | |
|
657 | 0 | if (!ossl_prov_is_running()) |
658 | 0 | return 0; |
659 | | |
660 | 0 | if (outsize < inl) { |
661 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
662 | 0 | return 0; |
663 | 0 | } |
664 | | |
665 | 0 | if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0) |
666 | 0 | return 0; |
667 | | |
668 | 0 | *outl = inl; |
669 | 0 | return 1; |
670 | 0 | } |
671 | | |
672 | | /* |
673 | | * See SP800-38D (GCM) Section 8 "Uniqueness requirement on IVS and keys" |
674 | | * |
675 | | * See also 8.2.2 RBG-based construction. |
676 | | * Random construction consists of a free field (which can be NULL) and a |
677 | | * random field which will use a DRBG that can return at least 96 bits of |
678 | | * entropy strength. (The DRBG must be seeded by the FIPS module). |
679 | | */ |
680 | | static int gcm_iv_generate(PROV_GCM_CTX *ctx, int offset) |
681 | 0 | { |
682 | 0 | int sz = (int)(ctx->ivlen - offset); |
683 | | |
684 | | /* Must be at least 96 bits */ |
685 | 0 | if (sz <= 0 || ctx->ivlen < GCM_IV_DEFAULT_SIZE) |
686 | 0 | return 0; |
687 | | |
688 | | /* Use DRBG to generate random iv */ |
689 | 0 | if (RAND_bytes_ex(ctx->libctx, ctx->iv + offset, sz, 0) <= 0) |
690 | 0 | return 0; |
691 | 0 | ctx->iv_state = IV_STATE_BUFFERED; |
692 | 0 | ctx->iv_gen_rand = 1; |
693 | 0 | return 1; |
694 | 0 | } |
695 | | |
696 | | static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out, |
697 | | size_t *padlen, const unsigned char *in, |
698 | | size_t len) |
699 | 0 | { |
700 | 0 | size_t olen = 0; |
701 | 0 | int rv = 0; |
702 | 0 | const PROV_GCM_HW *hw = ctx->hw; |
703 | |
|
704 | 0 | if (ctx->tls_aad_len != UNINITIALISED_SIZET) |
705 | 0 | return gcm_tls_cipher(ctx, out, padlen, in, len); |
706 | | |
707 | 0 | if (!ctx->key_set || ctx->iv_state == IV_STATE_FINISHED) |
708 | 0 | goto err; |
709 | | |
710 | | /* |
711 | | * FIPS requires generation of AES-GCM IV's inside the FIPS module. |
712 | | * The IV can still be set externally (the security policy will state that |
713 | | * this is not FIPS compliant). There are some applications |
714 | | * where setting the IV externally is the only option available. |
715 | | */ |
716 | 0 | if (ctx->iv_state == IV_STATE_UNINITIALISED) { |
717 | 0 | if (!ctx->enc || !gcm_iv_generate(ctx, 0)) |
718 | 0 | goto err; |
719 | 0 | } |
720 | | |
721 | 0 | if (ctx->iv_state == IV_STATE_BUFFERED) { |
722 | 0 | if (!hw->setiv(ctx, ctx->iv, ctx->ivlen)) |
723 | 0 | goto err; |
724 | 0 | ctx->iv_state = IV_STATE_COPIED; |
725 | 0 | } |
726 | | |
727 | 0 | if (in != NULL) { |
728 | | /* The input is AAD if out is NULL */ |
729 | 0 | if (out == NULL) { |
730 | 0 | if (!hw->aadupdate(ctx, in, len)) |
731 | 0 | goto err; |
732 | 0 | } else { |
733 | | /* The input is ciphertext OR plaintext */ |
734 | 0 | if (!hw->cipherupdate(ctx, in, len, out)) |
735 | 0 | goto err; |
736 | 0 | } |
737 | 0 | } else { |
738 | | /* The tag must be set before actually decrypting data */ |
739 | 0 | if (!ctx->enc && ctx->taglen == UNINITIALISED_SIZET) |
740 | 0 | goto err; |
741 | 0 | if (!hw->cipherfinal(ctx, ctx->buf)) |
742 | 0 | goto err; |
743 | 0 | ctx->iv_state = IV_STATE_FINISHED; /* Don't reuse the IV */ |
744 | 0 | goto finish; |
745 | 0 | } |
746 | 0 | olen = len; |
747 | 0 | finish: |
748 | 0 | rv = 1; |
749 | 0 | err: |
750 | 0 | *padlen = olen; |
751 | 0 | return rv; |
752 | 0 | } |
753 | | |
754 | | static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len) |
755 | 0 | { |
756 | 0 | unsigned char *buf; |
757 | 0 | size_t len; |
758 | |
|
759 | 0 | if (!ossl_prov_is_running() || aad_len != EVP_AEAD_TLS1_AAD_LEN) |
760 | 0 | return 0; |
761 | | |
762 | | /* Save the aad for later use. */ |
763 | 0 | buf = dat->buf; |
764 | 0 | memcpy(buf, aad, aad_len); |
765 | 0 | dat->tls_aad_len = aad_len; |
766 | |
|
767 | 0 | len = buf[aad_len - 2] << 8 | buf[aad_len - 1]; |
768 | | /* Correct length for explicit iv. */ |
769 | 0 | if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN) |
770 | 0 | return 0; |
771 | 0 | len -= EVP_GCM_TLS_EXPLICIT_IV_LEN; |
772 | | |
773 | | /* If decrypting correct for tag too. */ |
774 | 0 | if (!dat->enc) { |
775 | 0 | if (len < EVP_GCM_TLS_TAG_LEN) |
776 | 0 | return 0; |
777 | 0 | len -= EVP_GCM_TLS_TAG_LEN; |
778 | 0 | } |
779 | 0 | buf[aad_len - 2] = (unsigned char)(len >> 8); |
780 | 0 | buf[aad_len - 1] = (unsigned char)(len & 0xff); |
781 | | /* Extra padding: tag appended to record. */ |
782 | 0 | return EVP_GCM_TLS_TAG_LEN; |
783 | 0 | } |
784 | | |
785 | | static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv, |
786 | | size_t len) |
787 | 0 | { |
788 | | /* Special case: -1 length restores whole IV */ |
789 | 0 | if (len == (size_t)-1) { |
790 | 0 | memcpy(ctx->iv, iv, ctx->ivlen); |
791 | 0 | ctx->iv_gen = 1; |
792 | 0 | ctx->iv_state = IV_STATE_BUFFERED; |
793 | 0 | return 1; |
794 | 0 | } |
795 | | /* Fixed field must be at least 4 bytes and invocation field at least 8 */ |
796 | 0 | if ((len < EVP_GCM_TLS_FIXED_IV_LEN) |
797 | 0 | || (ctx->ivlen - (int)len) < EVP_GCM_TLS_EXPLICIT_IV_LEN) |
798 | 0 | return 0; |
799 | 0 | if (len > 0) |
800 | 0 | memcpy(ctx->iv, iv, len); |
801 | 0 | if (ctx->enc) { |
802 | 0 | if (RAND_bytes_ex(ctx->libctx, ctx->iv + len, ctx->ivlen - len, 0) <= 0) |
803 | 0 | return 0; |
804 | 0 | ctx->iv_gen_rand = 1; |
805 | 0 | } |
806 | 0 | ctx->iv_gen = 1; |
807 | 0 | ctx->iv_state = IV_STATE_BUFFERED; |
808 | 0 | return 1; |
809 | 0 | } |
810 | | |
811 | | /* |
812 | | * Handle TLS GCM packet format. This consists of the last portion of the IV |
813 | | * followed by the payload and finally the tag. On encrypt generate IV, |
814 | | * encrypt payload and write the tag. On verify retrieve IV, decrypt payload |
815 | | * and verify tag. |
816 | | */ |
817 | | static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen, |
818 | | const unsigned char *in, size_t len) |
819 | 0 | { |
820 | 0 | int rv = 0; |
821 | 0 | size_t arg = EVP_GCM_TLS_EXPLICIT_IV_LEN; |
822 | 0 | size_t plen = 0; |
823 | 0 | unsigned char *tag = NULL; |
824 | |
|
825 | 0 | if (!ossl_prov_is_running() || !ctx->key_set) |
826 | 0 | goto err; |
827 | | |
828 | | /* Encrypt/decrypt must be performed in place */ |
829 | 0 | if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN)) |
830 | 0 | goto err; |
831 | | |
832 | | /* |
833 | | * Check for too many keys as per FIPS 140-2 IG A.5 "Key/IV Pair Uniqueness |
834 | | * Requirements from SP 800-38D". The requirements is for one party to the |
835 | | * communication to fail after 2^64 - 1 keys. We do this on the encrypting |
836 | | * side only. |
837 | | */ |
838 | 0 | if (ctx->enc && ++ctx->tls_enc_records == 0) { |
839 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TOO_MANY_RECORDS); |
840 | 0 | goto err; |
841 | 0 | } |
842 | | |
843 | | /* |
844 | | * Set IV from start of buffer or generate IV and write to start of |
845 | | * buffer. |
846 | | */ |
847 | 0 | if (ctx->enc) { |
848 | 0 | if (!getivgen(ctx, out, arg)) |
849 | 0 | goto err; |
850 | 0 | } else { |
851 | 0 | if (!setivinv(ctx, out, arg)) |
852 | 0 | goto err; |
853 | 0 | } |
854 | | |
855 | | /* Fix buffer and length to point to payload */ |
856 | 0 | in += EVP_GCM_TLS_EXPLICIT_IV_LEN; |
857 | 0 | out += EVP_GCM_TLS_EXPLICIT_IV_LEN; |
858 | 0 | len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; |
859 | |
|
860 | 0 | tag = ctx->enc ? out + len : (unsigned char *)in + len; |
861 | 0 | if (!ctx->hw->oneshot(ctx, ctx->buf, ctx->tls_aad_len, in, len, out, tag, |
862 | 0 | EVP_GCM_TLS_TAG_LEN)) { |
863 | 0 | if (!ctx->enc) |
864 | 0 | OPENSSL_cleanse(out, len); |
865 | 0 | goto err; |
866 | 0 | } |
867 | 0 | if (ctx->enc) |
868 | 0 | plen = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; |
869 | 0 | else |
870 | 0 | plen = len; |
871 | |
|
872 | 0 | rv = 1; |
873 | 0 | err: |
874 | 0 | ctx->iv_state = IV_STATE_FINISHED; |
875 | 0 | ctx->tls_aad_len = UNINITIALISED_SIZET; |
876 | 0 | *padlen = plen; |
877 | 0 | return rv; |
878 | 0 | } |