]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/ciphers/cipher_aes_xts.c
prov: prefix all OSSL_DISPATCH tables names with ossl_
[thirdparty/openssl.git] / providers / implementations / ciphers / cipher_aes_xts.c
CommitLineData
1be63951 1
3a9f26f3 2/*
33388b44 3 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
3a9f26f3
SL
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
c72fa255
MC
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
3a9f26f3 18#include "cipher_aes_xts.h"
af3e7e1b 19#include "prov/implementations.h"
f99d3eed 20#include "prov/providercommon.h"
ddd21319 21#include "prov/providercommonerr.h"
3a9f26f3
SL
22
23/* TODO (3.0) Figure out what flags need to be set */
6ef81d38
RL
24#define AES_XTS_FLAGS (EVP_CIPH_CUSTOM_IV \
25 | EVP_CIPH_ALWAYS_CALL_INIT \
26 | EVP_CIPH_CTRL_INIT \
3a9f26f3
SL
27 | EVP_CIPH_CUSTOM_COPY)
28
29#define AES_XTS_IV_BITS 128
30#define AES_XTS_BLOCK_BITS 8
31
3a9f26f3 32/* forward declarations */
363b1e5d
DMSP
33static OSSL_FUNC_cipher_encrypt_init_fn aes_xts_einit;
34static OSSL_FUNC_cipher_decrypt_init_fn aes_xts_dinit;
35static OSSL_FUNC_cipher_update_fn aes_xts_stream_update;
36static OSSL_FUNC_cipher_final_fn aes_xts_stream_final;
37static OSSL_FUNC_cipher_cipher_fn aes_xts_cipher;
38static OSSL_FUNC_cipher_freectx_fn aes_xts_freectx;
39static OSSL_FUNC_cipher_dupctx_fn aes_xts_dupctx;
40static OSSL_FUNC_cipher_set_ctx_params_fn aes_xts_set_ctx_params;
41static OSSL_FUNC_cipher_settable_ctx_params_fn aes_xts_settable_ctx_params;
3a9f26f3
SL
42
43/*
44 * Verify that the two keys are different.
45 *
46 * This addresses the vulnerability described in Rogaway's
47 * September 2004 paper:
48 *
49 * "Efficient Instantiations of Tweakable Blockciphers and
50 * Refinements to Modes OCB and PMAC".
51 * (http://web.cs.ucdavis.edu/~rogaway/papers/offsets.pdf)
52 *
53 * FIPS 140-2 IG A.9 XTS-AES Key Generation Requirements states
54 * that:
55 * "The check for Key_1 != Key_2 shall be done at any place
56 * BEFORE using the keys in the XTS-AES algorithm to process
57 * data with them."
58 */
59static int aes_xts_check_keys_differ(const unsigned char *key, size_t bytes,
60 int enc)
61{
62 if ((!allow_insecure_decrypt || enc)
63 && CRYPTO_memcmp(key, key + bytes, bytes) == 0) {
64 ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DUPLICATED_KEYS);
65 return 0;
66 }
67 return 1;
68}
69
70/*-
71 * Provider dispatch functions
72 */
73static int aes_xts_init(void *vctx, const unsigned char *key, size_t keylen,
74 const unsigned char *iv, size_t ivlen, int enc)
75{
76 PROV_AES_XTS_CTX *xctx = (PROV_AES_XTS_CTX *)vctx;
77 PROV_CIPHER_CTX *ctx = &xctx->base;
78
f99d3eed
P
79 if (!ossl_prov_is_running())
80 return 0;
81
3a9f26f3
SL
82 ctx->enc = enc;
83
84 if (iv != NULL) {
089cb623 85 if (!cipher_generic_initiv(vctx, iv, ivlen))
3a9f26f3 86 return 0;
3a9f26f3
SL
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 return ctx->hw->init(ctx, key, keylen);
96 }
97 return 1;
98}
99
100static int aes_xts_einit(void *vctx, const unsigned char *key, size_t keylen,
101 const unsigned char *iv, size_t ivlen)
102{
103 return aes_xts_init(vctx, key, keylen, iv, ivlen, 1);
104}
105
106static int aes_xts_dinit(void *vctx, const unsigned char *key, size_t keylen,
107 const unsigned char *iv, size_t ivlen)
108{
109 return aes_xts_init(vctx, key, keylen, iv, ivlen, 0);
110}
111
55c7dc79
SL
112static void *aes_xts_newctx(void *provctx, unsigned int mode, uint64_t flags,
113 size_t kbits, size_t blkbits, size_t ivbits)
3a9f26f3
SL
114{
115 PROV_AES_XTS_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
116
117 if (ctx != NULL) {
55c7dc79 118 cipher_generic_initkey(&ctx->base, kbits, blkbits, ivbits, mode, flags,
3a9f26f3
SL
119 PROV_CIPHER_HW_aes_xts(kbits), NULL);
120 }
121 return ctx;
122}
123
124static void aes_xts_freectx(void *vctx)
125{
126 PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
127
63ee6ec1 128 cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
3a9f26f3
SL
129 OPENSSL_clear_free(ctx, sizeof(*ctx));
130}
131
132static void *aes_xts_dupctx(void *vctx)
133{
134 PROV_AES_XTS_CTX *in = (PROV_AES_XTS_CTX *)vctx;
135 PROV_AES_XTS_CTX *ret = NULL;
136
f99d3eed
P
137 if (!ossl_prov_is_running())
138 return NULL;
139
3a9f26f3
SL
140 if (in->xts.key1 != NULL) {
141 if (in->xts.key1 != &in->ks1)
142 return NULL;
143 }
144 if (in->xts.key2 != NULL) {
145 if (in->xts.key2 != &in->ks2)
146 return NULL;
147 }
148 ret = OPENSSL_malloc(sizeof(*ret));
149 if (ret == NULL) {
150 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
151 return NULL;
152 }
f75abcc0 153 in->base.hw->copyctx(&ret->base, &in->base);
3a9f26f3
SL
154 return ret;
155}
156
157static int aes_xts_cipher(void *vctx, unsigned char *out, size_t *outl,
158 size_t outsize, const unsigned char *in, size_t inl)
159{
160 PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
161
f99d3eed
P
162 if (!ossl_prov_is_running()
163 || ctx->xts.key1 == NULL
3a9f26f3 164 || ctx->xts.key2 == NULL
089cb623 165 || !ctx->base.iv_set
3a9f26f3
SL
166 || out == NULL
167 || in == NULL
168 || inl < AES_BLOCK_SIZE)
169 return 0;
170
171 /*
79c44b4e 172 * Impose a limit of 2^20 blocks per data unit as specified by
3a9f26f3
SL
173 * IEEE Std 1619-2018. The earlier and obsolete IEEE Std 1619-2007
174 * indicated that this was a SHOULD NOT rather than a MUST NOT.
175 * NIST SP 800-38E mandates the same limit.
176 */
177 if (inl > XTS_MAX_BLOCKS_PER_DATA_UNIT * AES_BLOCK_SIZE) {
178 ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DATA_UNIT_IS_TOO_LARGE);
179 return 0;
180 }
181
182 if (ctx->stream != NULL)
183 (*ctx->stream)(in, out, inl, ctx->xts.key1, ctx->xts.key2, ctx->base.iv);
184 else if (CRYPTO_xts128_encrypt(&ctx->xts, ctx->base.iv, in, out, inl,
185 ctx->base.enc))
186 return 0;
3a9f26f3
SL
187
188 *outl = inl;
189 return 1;
190}
191
192static int aes_xts_stream_update(void *vctx, unsigned char *out, size_t *outl,
193 size_t outsize, const unsigned char *in,
194 size_t inl)
195{
196 PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
197
198 if (outsize < inl) {
199 ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
200 return 0;
201 }
202
203 if (!aes_xts_cipher(ctx, out, outl, outsize, in, inl)) {
204 ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
205 return 0;
206 }
207
3a9f26f3
SL
208 return 1;
209}
210
211static int aes_xts_stream_final(void *vctx, unsigned char *out, size_t *outl,
212 size_t outsize)
213{
f99d3eed
P
214 if (!ossl_prov_is_running())
215 return 0;
3a9f26f3
SL
216 *outl = 0;
217 return 1;
218}
219
220static const OSSL_PARAM aes_xts_known_settable_ctx_params[] = {
221 OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
222 OSSL_PARAM_END
223};
224
1017ab21 225static const OSSL_PARAM *aes_xts_settable_ctx_params(ossl_unused void *provctx)
3a9f26f3
SL
226{
227 return aes_xts_known_settable_ctx_params;
228}
229
230static int aes_xts_set_ctx_params(void *vctx, const OSSL_PARAM params[])
231{
232 PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
233 const OSSL_PARAM *p;
234
235 /*
236 * TODO(3.0) We need a general solution for handling missing parameters
237 * inside set_params and get_params methods.
238 */
239 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
240 if (p != NULL) {
241 size_t keylen;
242
243 if (!OSSL_PARAM_get_size_t(p, &keylen)) {
244 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
245 return 0;
246 }
247 /* The key length can not be modified for xts mode */
248 if (keylen != ctx->keylen)
249 return 0;
250 }
251
252 return 1;
253}
254
255#define IMPLEMENT_cipher(lcmode, UCMODE, kbits, flags) \
363b1e5d 256static OSSL_FUNC_cipher_get_params_fn aes_##kbits##_##lcmode##_get_params; \
3a9f26f3
SL
257static int aes_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \
258{ \
259 return cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
260 flags, 2 * kbits, AES_XTS_BLOCK_BITS, \
261 AES_XTS_IV_BITS); \
262} \
363b1e5d 263static OSSL_FUNC_cipher_newctx_fn aes_##kbits##_xts_newctx; \
3a9f26f3
SL
264static void *aes_##kbits##_xts_newctx(void *provctx) \
265{ \
55c7dc79 266 return aes_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \
3a9f26f3
SL
267 AES_XTS_BLOCK_BITS, AES_XTS_IV_BITS); \
268} \
1be63951 269const OSSL_DISPATCH ossl_aes##kbits##xts_functions[] = { \
3a9f26f3
SL
270 { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))aes_##kbits##_xts_newctx }, \
271 { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_xts_einit }, \
272 { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_xts_dinit }, \
273 { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_xts_stream_update }, \
274 { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_xts_stream_final }, \
275 { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))aes_xts_cipher }, \
276 { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_xts_freectx }, \
277 { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_xts_dupctx }, \
278 { OSSL_FUNC_CIPHER_GET_PARAMS, \
279 (void (*)(void))aes_##kbits##_##lcmode##_get_params }, \
280 { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
281 (void (*)(void))cipher_generic_gettable_params }, \
282 { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
283 (void (*)(void))cipher_generic_get_ctx_params }, \
284 { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
285 (void (*)(void))cipher_generic_gettable_ctx_params }, \
286 { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
287 (void (*)(void))aes_xts_set_ctx_params }, \
288 { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
289 (void (*)(void))aes_xts_settable_ctx_params }, \
290 { 0, NULL } \
291}
292
293IMPLEMENT_cipher(xts, XTS, 256, AES_XTS_FLAGS);
294IMPLEMENT_cipher(xts, XTS, 128, AES_XTS_FLAGS);