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