/src/openssl/providers/implementations/ciphers/cipher_aes_xts.c
Line | Count | Source |
1 | | |
2 | | /* |
3 | | * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved. |
4 | | * |
5 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
6 | | * this file except in compliance with the License. You can obtain a copy |
7 | | * in the file LICENSE in the source distribution or at |
8 | | * https://www.openssl.org/source/license.html |
9 | | */ |
10 | | |
11 | | |
12 | | /* |
13 | | * AES low level APIs are deprecated for public use, but still ok for internal |
14 | | * use where we're using them to implement the higher level EVP interface, as is |
15 | | * the case here. |
16 | | */ |
17 | | #include "internal/deprecated.h" |
18 | | |
19 | | #include <openssl/proverr.h> |
20 | | #include "cipher_aes_xts.h" |
21 | | #include "prov/implementations.h" |
22 | | #include "prov/providercommon.h" |
23 | | |
24 | | #define AES_XTS_FLAGS PROV_CIPHER_FLAG_CUSTOM_IV |
25 | 62 | #define AES_XTS_IV_BITS 128 |
26 | 62 | #define AES_XTS_BLOCK_BITS 8 |
27 | | |
28 | | /* forward declarations */ |
29 | | static OSSL_FUNC_cipher_encrypt_init_fn aes_xts_einit; |
30 | | static OSSL_FUNC_cipher_decrypt_init_fn aes_xts_dinit; |
31 | | static OSSL_FUNC_cipher_update_fn aes_xts_stream_update; |
32 | | static OSSL_FUNC_cipher_final_fn aes_xts_stream_final; |
33 | | static OSSL_FUNC_cipher_cipher_fn aes_xts_cipher; |
34 | | static OSSL_FUNC_cipher_freectx_fn aes_xts_freectx; |
35 | | static OSSL_FUNC_cipher_dupctx_fn aes_xts_dupctx; |
36 | | static OSSL_FUNC_cipher_set_ctx_params_fn aes_xts_set_ctx_params; |
37 | | static OSSL_FUNC_cipher_settable_ctx_params_fn aes_xts_settable_ctx_params; |
38 | | |
39 | | /* |
40 | | * Verify that the two keys are different. |
41 | | * |
42 | | * This addresses the vulnerability described in Rogaway's |
43 | | * September 2004 paper: |
44 | | * |
45 | | * "Efficient Instantiations of Tweakable Blockciphers and |
46 | | * Refinements to Modes OCB and PMAC". |
47 | | * (http://web.cs.ucdavis.edu/~rogaway/papers/offsets.pdf) |
48 | | * |
49 | | * FIPS 140-2 IG A.9 XTS-AES Key Generation Requirements states |
50 | | * that: |
51 | | * "The check for Key_1 != Key_2 shall be done at any place |
52 | | * BEFORE using the keys in the XTS-AES algorithm to process |
53 | | * data with them." |
54 | | */ |
55 | | static int aes_xts_check_keys_differ(const unsigned char *key, size_t bytes, |
56 | | int enc) |
57 | 60 | { |
58 | 60 | if ((!ossl_aes_xts_allow_insecure_decrypt || enc) |
59 | 30 | && CRYPTO_memcmp(key, key + bytes, bytes) == 0) { |
60 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DUPLICATED_KEYS); |
61 | 0 | return 0; |
62 | 0 | } |
63 | 60 | return 1; |
64 | 60 | } |
65 | | |
66 | | #ifdef AES_XTS_S390X |
67 | | # include "cipher_aes_xts_s390x.inc" |
68 | | #endif |
69 | | |
70 | | /*- |
71 | | * Provider dispatch functions |
72 | | */ |
73 | | static int aes_xts_init(void *vctx, const unsigned char *key, size_t keylen, |
74 | | const unsigned char *iv, size_t ivlen, |
75 | | const OSSL_PARAM params[], int enc) |
76 | 82 | { |
77 | 82 | PROV_AES_XTS_CTX *xctx = (PROV_AES_XTS_CTX *)vctx; |
78 | 82 | PROV_CIPHER_CTX *ctx = &xctx->base; |
79 | | |
80 | 82 | if (!ossl_prov_is_running()) |
81 | 0 | return 0; |
82 | | |
83 | 82 | ctx->enc = enc; |
84 | | |
85 | 82 | if (iv != NULL) { |
86 | 22 | if (!ossl_cipher_generic_initiv(vctx, iv, ivlen)) |
87 | 0 | return 0; |
88 | 22 | } |
89 | 82 | if (key != NULL) { |
90 | 60 | if (keylen != ctx->keylen) { |
91 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
92 | 0 | return 0; |
93 | 0 | } |
94 | 60 | if (!aes_xts_check_keys_differ(key, keylen / 2, enc)) |
95 | 0 | return 0; |
96 | 60 | if (!ctx->hw->init(ctx, key, keylen)) |
97 | 0 | return 0; |
98 | 60 | } |
99 | 82 | return aes_xts_set_ctx_params(ctx, params); |
100 | 82 | } |
101 | | |
102 | | static int aes_xts_einit(void *vctx, const unsigned char *key, size_t keylen, |
103 | | const unsigned char *iv, size_t ivlen, |
104 | | const OSSL_PARAM params[]) |
105 | 30 | { |
106 | | #ifdef AES_XTS_S390X |
107 | | if (s390x_aes_xts_einit(vctx, key, keylen, iv, ivlen, params) == 1) |
108 | | return 1; |
109 | | #endif |
110 | 30 | return aes_xts_init(vctx, key, keylen, iv, ivlen, params, 1); |
111 | 30 | } |
112 | | |
113 | | static int aes_xts_dinit(void *vctx, const unsigned char *key, size_t keylen, |
114 | | const unsigned char *iv, size_t ivlen, |
115 | | const OSSL_PARAM params[]) |
116 | 52 | { |
117 | | #ifdef AES_XTS_S390X |
118 | | if (s390x_aes_xts_dinit(vctx, key, keylen, iv, ivlen, params) == 1) |
119 | | return 1; |
120 | | #endif |
121 | 52 | return aes_xts_init(vctx, key, keylen, iv, ivlen, params, 0); |
122 | 52 | } |
123 | | |
124 | | static void *aes_xts_newctx(void *provctx, unsigned int mode, uint64_t flags, |
125 | | size_t kbits, size_t blkbits, size_t ivbits) |
126 | 60 | { |
127 | 60 | PROV_AES_XTS_CTX *ctx; |
128 | | |
129 | 60 | if (!ossl_prov_is_running()) |
130 | 0 | return NULL; |
131 | | |
132 | 60 | ctx = OPENSSL_zalloc(sizeof(*ctx)); |
133 | 60 | if (ctx != NULL) { |
134 | 60 | ossl_cipher_generic_initkey(&ctx->base, kbits, blkbits, ivbits, mode, |
135 | 60 | flags, ossl_prov_cipher_hw_aes_xts(kbits), |
136 | 60 | NULL); |
137 | 60 | } |
138 | 60 | return ctx; |
139 | 60 | } |
140 | | |
141 | | static void aes_xts_freectx(void *vctx) |
142 | 60 | { |
143 | 60 | PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx; |
144 | | |
145 | 60 | ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx); |
146 | 60 | OPENSSL_clear_free(ctx, sizeof(*ctx)); |
147 | 60 | } |
148 | | |
149 | | static void *aes_xts_dupctx(void *vctx) |
150 | 0 | { |
151 | 0 | PROV_AES_XTS_CTX *in = (PROV_AES_XTS_CTX *)vctx; |
152 | 0 | PROV_AES_XTS_CTX *ret = NULL; |
153 | |
|
154 | 0 | if (!ossl_prov_is_running()) |
155 | 0 | return NULL; |
156 | | |
157 | | #ifdef AES_XTS_S390X |
158 | | if (in->plat.s390x.fc) |
159 | | return s390x_aes_xts_dupctx(vctx); |
160 | | #endif |
161 | | |
162 | 0 | if (in->xts.key1 != NULL) { |
163 | 0 | if (in->xts.key1 != &in->ks1) |
164 | 0 | return NULL; |
165 | 0 | } |
166 | 0 | if (in->xts.key2 != NULL) { |
167 | 0 | if (in->xts.key2 != &in->ks2) |
168 | 0 | return NULL; |
169 | 0 | } |
170 | 0 | ret = OPENSSL_malloc(sizeof(*ret)); |
171 | 0 | if (ret == NULL) |
172 | 0 | return NULL; |
173 | 0 | in->base.hw->copyctx(&ret->base, &in->base); |
174 | 0 | return ret; |
175 | 0 | } |
176 | | |
177 | | static int aes_xts_cipher(void *vctx, unsigned char *out, size_t *outl, |
178 | | size_t outsize, const unsigned char *in, size_t inl) |
179 | 22 | { |
180 | 22 | PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx; |
181 | | |
182 | | #ifdef AES_XTS_S390X |
183 | | if (ctx->plat.s390x.fc) |
184 | | return s390x_aes_xts_cipher(vctx, out, outl, outsize, in, inl); |
185 | | #endif |
186 | | |
187 | 22 | if (!ossl_prov_is_running() |
188 | 22 | || ctx->xts.key1 == NULL |
189 | 22 | || ctx->xts.key2 == NULL |
190 | 22 | || !ctx->base.iv_set |
191 | 22 | || out == NULL |
192 | 22 | || in == NULL |
193 | 22 | || inl < AES_BLOCK_SIZE) |
194 | 0 | return 0; |
195 | | |
196 | | /* |
197 | | * Impose a limit of 2^20 blocks per data unit as specified by |
198 | | * IEEE Std 1619-2018. The earlier and obsolete IEEE Std 1619-2007 |
199 | | * indicated that this was a SHOULD NOT rather than a MUST NOT. |
200 | | * NIST SP 800-38E mandates the same limit. |
201 | | */ |
202 | 22 | if (inl > XTS_MAX_BLOCKS_PER_DATA_UNIT * AES_BLOCK_SIZE) { |
203 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DATA_UNIT_IS_TOO_LARGE); |
204 | 0 | return 0; |
205 | 0 | } |
206 | | |
207 | 22 | if (ctx->stream != NULL) |
208 | 0 | (*ctx->stream)(in, out, inl, ctx->xts.key1, ctx->xts.key2, ctx->base.iv); |
209 | 22 | else if (CRYPTO_xts128_encrypt(&ctx->xts, ctx->base.iv, in, out, inl, |
210 | 22 | ctx->base.enc)) |
211 | 0 | return 0; |
212 | | |
213 | 22 | *outl = inl; |
214 | 22 | return 1; |
215 | 22 | } |
216 | | |
217 | | static int aes_xts_stream_update(void *vctx, unsigned char *out, size_t *outl, |
218 | | size_t outsize, const unsigned char *in, |
219 | | size_t inl) |
220 | 22 | { |
221 | 22 | PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx; |
222 | | |
223 | 22 | if (outsize < inl) { |
224 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
225 | 0 | return 0; |
226 | 0 | } |
227 | | |
228 | 22 | if (!aes_xts_cipher(ctx, out, outl, outsize, in, inl)) { |
229 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED); |
230 | 0 | return 0; |
231 | 0 | } |
232 | | |
233 | 22 | return 1; |
234 | 22 | } |
235 | | |
236 | | static int aes_xts_stream_final(void *vctx, unsigned char *out, size_t *outl, |
237 | | size_t outsize) |
238 | 22 | { |
239 | 22 | if (!ossl_prov_is_running()) |
240 | 0 | return 0; |
241 | 22 | *outl = 0; |
242 | 22 | return 1; |
243 | 22 | } |
244 | | |
245 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
246 | | #ifndef aes_xts_set_ctx_params_list |
247 | | static const OSSL_PARAM aes_xts_set_ctx_params_list[] = { |
248 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), |
249 | | OSSL_PARAM_END |
250 | | }; |
251 | | #endif |
252 | | |
253 | | #ifndef aes_xts_set_ctx_params_st |
254 | | struct aes_xts_set_ctx_params_st { |
255 | | OSSL_PARAM *keylen; |
256 | | }; |
257 | | #endif |
258 | | |
259 | | #ifndef aes_xts_set_ctx_params_decoder |
260 | | static int aes_xts_set_ctx_params_decoder |
261 | | (const OSSL_PARAM *p, struct aes_xts_set_ctx_params_st *r) |
262 | 164 | { |
263 | 164 | const char *s; |
264 | | |
265 | 164 | memset(r, 0, sizeof(*r)); |
266 | 164 | if (p != NULL) |
267 | 164 | for (; (s = p->key) != NULL; p++) |
268 | 82 | if (ossl_likely(strcmp("keylen", s + 0) == 0)) { |
269 | | /* OSSL_CIPHER_PARAM_KEYLEN */ |
270 | 0 | if (ossl_unlikely(r->keylen != NULL)) { |
271 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
272 | 0 | "param %s is repeated", s); |
273 | 0 | return 0; |
274 | 0 | } |
275 | 0 | r->keylen = (OSSL_PARAM *)p; |
276 | 0 | } |
277 | 164 | return 1; |
278 | 164 | } |
279 | | #endif |
280 | | /* End of machine generated */ |
281 | | |
282 | | static const OSSL_PARAM *aes_xts_settable_ctx_params(ossl_unused void *cctx, |
283 | | ossl_unused void *provctx) |
284 | 0 | { |
285 | 0 | return aes_xts_set_ctx_params_list; |
286 | 0 | } |
287 | | |
288 | | static int aes_xts_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
289 | 164 | { |
290 | 164 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; |
291 | 164 | struct aes_xts_set_ctx_params_st p; |
292 | | |
293 | 164 | if (ctx == NULL || !aes_xts_set_ctx_params_decoder(params, &p)) |
294 | 0 | return 0; |
295 | | |
296 | 164 | if (p.keylen != NULL) { |
297 | 0 | size_t keylen; |
298 | |
|
299 | 0 | if (!OSSL_PARAM_get_size_t(p.keylen, &keylen)) { |
300 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
301 | 0 | return 0; |
302 | 0 | } |
303 | | /* The key length can not be modified for xts mode */ |
304 | 0 | if (keylen != ctx->keylen) |
305 | 0 | return 0; |
306 | 0 | } |
307 | | |
308 | 164 | return 1; |
309 | 164 | } |
310 | | |
311 | | #define IMPLEMENT_cipher(lcmode, UCMODE, kbits, flags) \ |
312 | | static OSSL_FUNC_cipher_get_params_fn aes_##kbits##_##lcmode##_get_params; \ |
313 | 2 | static int aes_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \ |
314 | 2 | { \ |
315 | 2 | return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \ |
316 | 2 | flags, 2 * kbits, AES_XTS_BLOCK_BITS, \ |
317 | 2 | AES_XTS_IV_BITS); \ |
318 | 2 | } \ cipher_aes_xts.c:aes_256_xts_get_params Line | Count | Source | 313 | 1 | static int aes_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \ | 314 | 1 | { \ | 315 | 1 | return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \ | 316 | 1 | flags, 2 * kbits, AES_XTS_BLOCK_BITS, \ | 317 | 1 | AES_XTS_IV_BITS); \ | 318 | 1 | } \ |
cipher_aes_xts.c:aes_128_xts_get_params Line | Count | Source | 313 | 1 | static int aes_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \ | 314 | 1 | { \ | 315 | 1 | return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \ | 316 | 1 | flags, 2 * kbits, AES_XTS_BLOCK_BITS, \ | 317 | 1 | AES_XTS_IV_BITS); \ | 318 | 1 | } \ |
|
319 | | static OSSL_FUNC_cipher_newctx_fn aes_##kbits##_xts_newctx; \ |
320 | 60 | static void *aes_##kbits##_xts_newctx(void *provctx) \ |
321 | 60 | { \ |
322 | 60 | return aes_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \ |
323 | 60 | AES_XTS_BLOCK_BITS, AES_XTS_IV_BITS); \ |
324 | 60 | } \ Unexecuted instantiation: cipher_aes_xts.c:aes_256_xts_newctx cipher_aes_xts.c:aes_128_xts_newctx Line | Count | Source | 320 | 60 | static void *aes_##kbits##_xts_newctx(void *provctx) \ | 321 | 60 | { \ | 322 | 60 | return aes_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \ | 323 | 60 | AES_XTS_BLOCK_BITS, AES_XTS_IV_BITS); \ | 324 | 60 | } \ |
|
325 | | const OSSL_DISPATCH ossl_aes##kbits##xts_functions[] = { \ |
326 | | { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))aes_##kbits##_xts_newctx }, \ |
327 | | { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_xts_einit }, \ |
328 | | { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_xts_dinit }, \ |
329 | | { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_xts_stream_update }, \ |
330 | | { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_xts_stream_final }, \ |
331 | | { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))aes_xts_cipher }, \ |
332 | | { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_xts_freectx }, \ |
333 | | { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_xts_dupctx }, \ |
334 | | { OSSL_FUNC_CIPHER_GET_PARAMS, \ |
335 | | (void (*)(void))aes_##kbits##_##lcmode##_get_params }, \ |
336 | | { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \ |
337 | | (void (*)(void))ossl_cipher_generic_gettable_params }, \ |
338 | | { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \ |
339 | | (void (*)(void))ossl_cipher_generic_get_ctx_params }, \ |
340 | | { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \ |
341 | | (void (*)(void))ossl_cipher_generic_gettable_ctx_params }, \ |
342 | | { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \ |
343 | | (void (*)(void))aes_xts_set_ctx_params }, \ |
344 | | { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \ |
345 | | (void (*)(void))aes_xts_settable_ctx_params }, \ |
346 | | OSSL_DISPATCH_END \ |
347 | | } |
348 | | |
349 | | IMPLEMENT_cipher(xts, XTS, 256, AES_XTS_FLAGS); |
350 | | IMPLEMENT_cipher(xts, XTS, 128, AES_XTS_FLAGS); |