Update copyright year
[openssl.git] / providers / implementations / ciphers / cipher_aes_xts.c
1
2 /*
3  * Copyright 2019-2021 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  */
54 static int aes_xts_check_keys_differ(const unsigned char *key, size_t bytes,
55                                      int enc)
56 {
57     if ((!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 /*-
66  * Provider dispatch functions
67  */
68 static int aes_xts_init(void *vctx, const unsigned char *key, size_t keylen,
69                         const unsigned char *iv, size_t ivlen, int enc)
70 {
71     PROV_AES_XTS_CTX *xctx = (PROV_AES_XTS_CTX *)vctx;
72     PROV_CIPHER_CTX *ctx = &xctx->base;
73
74     if (!ossl_prov_is_running())
75         return 0;
76
77     ctx->enc = enc;
78
79     if (iv != NULL) {
80         if (!ossl_cipher_generic_initiv(vctx, iv, ivlen))
81             return 0;
82     }
83     if (key != NULL) {
84         if (keylen != ctx->keylen) {
85             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
86             return 0;
87         }
88         if (!aes_xts_check_keys_differ(key, keylen / 2, enc))
89             return 0;
90         return ctx->hw->init(ctx, key, keylen);
91     }
92     return 1;
93 }
94
95 static int aes_xts_einit(void *vctx, const unsigned char *key, size_t keylen,
96                          const unsigned char *iv, size_t ivlen)
97 {
98     return aes_xts_init(vctx, key, keylen, iv, ivlen, 1);
99 }
100
101 static int aes_xts_dinit(void *vctx, const unsigned char *key, size_t keylen,
102                          const unsigned char *iv, size_t ivlen)
103 {
104     return aes_xts_init(vctx, key, keylen, iv, ivlen, 0);
105 }
106
107 static void *aes_xts_newctx(void *provctx, unsigned int mode, uint64_t flags,
108                             size_t kbits, size_t blkbits, size_t ivbits)
109 {
110     PROV_AES_XTS_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
111
112     if (ctx != NULL) {
113         ossl_cipher_generic_initkey(&ctx->base, kbits, blkbits, ivbits, mode,
114                                     flags, ossl_prov_cipher_hw_aes_xts(kbits),
115                                     NULL);
116     }
117     return ctx;
118 }
119
120 static void aes_xts_freectx(void *vctx)
121 {
122     PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
123
124     ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
125     OPENSSL_clear_free(ctx,  sizeof(*ctx));
126 }
127
128 static void *aes_xts_dupctx(void *vctx)
129 {
130     PROV_AES_XTS_CTX *in = (PROV_AES_XTS_CTX *)vctx;
131     PROV_AES_XTS_CTX *ret = NULL;
132
133     if (!ossl_prov_is_running())
134         return NULL;
135
136     if (in->xts.key1 != NULL) {
137         if (in->xts.key1 != &in->ks1)
138             return NULL;
139     }
140     if (in->xts.key2 != NULL) {
141         if (in->xts.key2 != &in->ks2)
142             return NULL;
143     }
144     ret = OPENSSL_malloc(sizeof(*ret));
145     if (ret == NULL) {
146         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
147         return NULL;
148     }
149     in->base.hw->copyctx(&ret->base, &in->base);
150     return ret;
151 }
152
153 static int aes_xts_cipher(void *vctx, unsigned char *out, size_t *outl,
154                           size_t outsize, const unsigned char *in, size_t inl)
155 {
156     PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
157
158     if (!ossl_prov_is_running()
159             || ctx->xts.key1 == NULL
160             || ctx->xts.key2 == NULL
161             || !ctx->base.iv_set
162             || out == NULL
163             || in == NULL
164             || inl < AES_BLOCK_SIZE)
165         return 0;
166
167     /*
168      * Impose a limit of 2^20 blocks per data unit as specified by
169      * IEEE Std 1619-2018.  The earlier and obsolete IEEE Std 1619-2007
170      * indicated that this was a SHOULD NOT rather than a MUST NOT.
171      * NIST SP 800-38E mandates the same limit.
172      */
173     if (inl > XTS_MAX_BLOCKS_PER_DATA_UNIT * AES_BLOCK_SIZE) {
174         ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DATA_UNIT_IS_TOO_LARGE);
175         return 0;
176     }
177
178     if (ctx->stream != NULL)
179         (*ctx->stream)(in, out, inl, ctx->xts.key1, ctx->xts.key2, ctx->base.iv);
180     else if (CRYPTO_xts128_encrypt(&ctx->xts, ctx->base.iv, in, out, inl,
181                                    ctx->base.enc))
182         return 0;
183
184     *outl = inl;
185     return 1;
186 }
187
188 static int aes_xts_stream_update(void *vctx, unsigned char *out, size_t *outl,
189                                  size_t outsize, const unsigned char *in,
190                                  size_t inl)
191 {
192     PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
193
194     if (outsize < inl) {
195         ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
196         return 0;
197     }
198
199     if (!aes_xts_cipher(ctx, out, outl, outsize, in, inl)) {
200         ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
201         return 0;
202     }
203
204     return 1;
205 }
206
207 static int aes_xts_stream_final(void *vctx, unsigned char *out, size_t *outl,
208                                 size_t outsize)
209 {
210     if (!ossl_prov_is_running())
211         return 0;
212     *outl = 0;
213     return 1;
214 }
215
216 static const OSSL_PARAM aes_xts_known_settable_ctx_params[] = {
217     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
218     OSSL_PARAM_END
219 };
220
221 static const OSSL_PARAM *aes_xts_settable_ctx_params(ossl_unused void *provctx)
222 {
223     return aes_xts_known_settable_ctx_params;
224 }
225
226 static int aes_xts_set_ctx_params(void *vctx, const OSSL_PARAM params[])
227 {
228     PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
229     const OSSL_PARAM *p;
230
231     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
232     if (p != NULL) {
233         size_t keylen;
234
235         if (!OSSL_PARAM_get_size_t(p, &keylen)) {
236             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
237             return 0;
238         }
239         /* The key length can not be modified for xts mode */
240         if (keylen != ctx->keylen)
241             return 0;
242     }
243
244     return 1;
245 }
246
247 #define IMPLEMENT_cipher(lcmode, UCMODE, kbits, flags)                         \
248 static OSSL_FUNC_cipher_get_params_fn aes_##kbits##_##lcmode##_get_params;     \
249 static int aes_##kbits##_##lcmode##_get_params(OSSL_PARAM params[])            \
250 {                                                                              \
251     return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,    \
252                                      flags, 2 * kbits, AES_XTS_BLOCK_BITS,     \
253                                      AES_XTS_IV_BITS);                         \
254 }                                                                              \
255 static OSSL_FUNC_cipher_newctx_fn aes_##kbits##_xts_newctx;                    \
256 static void *aes_##kbits##_xts_newctx(void *provctx)                           \
257 {                                                                              \
258     return aes_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \
259                           AES_XTS_BLOCK_BITS, AES_XTS_IV_BITS);                \
260 }                                                                              \
261 const OSSL_DISPATCH ossl_aes##kbits##xts_functions[] = {                       \
262     { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))aes_##kbits##_xts_newctx },     \
263     { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_xts_einit },          \
264     { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_xts_dinit },          \
265     { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_xts_stream_update },        \
266     { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_xts_stream_final },          \
267     { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))aes_xts_cipher },               \
268     { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_xts_freectx },             \
269     { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_xts_dupctx },               \
270     { OSSL_FUNC_CIPHER_GET_PARAMS,                                             \
271       (void (*)(void))aes_##kbits##_##lcmode##_get_params },                   \
272     { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                        \
273       (void (*)(void))ossl_cipher_generic_gettable_params },                   \
274     { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                         \
275       (void (*)(void))ossl_cipher_generic_get_ctx_params },                    \
276     { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                    \
277       (void (*)(void))ossl_cipher_generic_gettable_ctx_params },               \
278     { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                         \
279       (void (*)(void))aes_xts_set_ctx_params },                                \
280     { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                    \
281      (void (*)(void))aes_xts_settable_ctx_params },                            \
282     { 0, NULL }                                                                \
283 }
284
285 IMPLEMENT_cipher(xts, XTS, 256, AES_XTS_FLAGS);
286 IMPLEMENT_cipher(xts, XTS, 128, AES_XTS_FLAGS);