Reorganize private crypto header files
[openssl.git] / providers / common / ciphers / cipher_aes_xts.c
1 /*
2  * Copyright 2019 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 #include "cipher_aes_xts.h"
11 #include "internal/provider_algs.h"
12 #include "internal/providercommonerr.h"
13
14 /* TODO (3.0) Figure out what flags need to be set */
15 #define AES_XTS_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV         \
16                        | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT        \
17                        | EVP_CIPH_CUSTOM_COPY)
18
19 #define AES_XTS_IV_BITS 128
20 #define AES_XTS_BLOCK_BITS 8
21
22 #ifdef FIPS_MODE
23 static const int allow_insecure_decrypt = 0;
24 #else
25 static const int allow_insecure_decrypt = 1;
26 #endif /* FIPS_MODE */
27
28 /* forward declarations */
29 static OSSL_OP_cipher_encrypt_init_fn aes_xts_einit;
30 static OSSL_OP_cipher_decrypt_init_fn aes_xts_dinit;
31 static OSSL_OP_cipher_update_fn aes_xts_stream_update;
32 static OSSL_OP_cipher_final_fn aes_xts_stream_final;
33 static OSSL_OP_cipher_cipher_fn aes_xts_cipher;
34 static OSSL_OP_cipher_freectx_fn aes_xts_freectx;
35 static OSSL_OP_cipher_dupctx_fn aes_xts_dupctx;
36 static OSSL_OP_cipher_set_ctx_params_fn aes_xts_set_ctx_params;
37 static OSSL_OP_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 {
58     if ((!allow_insecure_decrypt || enc)
59             && CRYPTO_memcmp(key, key + bytes, bytes) == 0) {
60         ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DUPLICATED_KEYS);
61         return 0;
62     }
63     return 1;
64 }
65
66 /*-
67  * Provider dispatch functions
68  */
69 static int aes_xts_init(void *vctx, const unsigned char *key, size_t keylen,
70                         const unsigned char *iv, size_t ivlen, int enc)
71 {
72     PROV_AES_XTS_CTX *xctx = (PROV_AES_XTS_CTX *)vctx;
73     PROV_CIPHER_CTX *ctx = &xctx->base;
74
75     ctx->enc = enc;
76
77     if (iv != NULL) {
78         if (ivlen != ctx->ivlen) {
79             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
80             return 0;
81         }
82         memcpy(ctx->iv, iv, ivlen);
83         xctx->iv_set = 1;
84     }
85     if (key != NULL) {
86         if (keylen != ctx->keylen) {
87             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
88             return 0;
89         }
90         if (!aes_xts_check_keys_differ(key, keylen / 2, enc))
91             return 0;
92         return ctx->hw->init(ctx, key, keylen);
93     }
94     return 1;
95 }
96
97 static int aes_xts_einit(void *vctx, const unsigned char *key, size_t keylen,
98                          const unsigned char *iv, size_t ivlen)
99 {
100     return aes_xts_init(vctx, key, keylen, iv, ivlen, 1);
101 }
102
103 static int aes_xts_dinit(void *vctx, const unsigned char *key, size_t keylen,
104                          const unsigned char *iv, size_t ivlen)
105 {
106     return aes_xts_init(vctx, key, keylen, iv, ivlen, 0);
107 }
108
109 static void *aes_xts_newctx(void *provctx, unsigned int mode, uint64_t flags,
110                             size_t kbits, size_t blkbits, size_t ivbits)
111 {
112     PROV_AES_XTS_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
113
114     if (ctx != NULL) {
115         cipher_generic_initkey(&ctx->base, kbits, blkbits, ivbits, mode, flags,
116                                PROV_CIPHER_HW_aes_xts(kbits), NULL);
117     }
118     return ctx;
119 }
120
121 static void aes_xts_freectx(void *vctx)
122 {
123     PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
124
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 (in->xts.key1 != NULL) {
134         if (in->xts.key1 != &in->ks1)
135             return NULL;
136     }
137     if (in->xts.key2 != NULL) {
138         if (in->xts.key2 != &in->ks2)
139             return NULL;
140     }
141     ret = OPENSSL_malloc(sizeof(*ret));
142     if (ret == NULL) {
143         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
144         return NULL;
145     }
146     *ret = *in;
147     return ret;
148 }
149
150 static int aes_xts_cipher(void *vctx, unsigned char *out, size_t *outl,
151                           size_t outsize, const unsigned char *in, size_t inl)
152 {
153     PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
154
155     if (ctx->xts.key1 == NULL
156             || ctx->xts.key2 == NULL
157             || !ctx->iv_set
158             || out == NULL
159             || in == NULL
160             || inl < AES_BLOCK_SIZE)
161         return 0;
162
163     /*
164      * Impose a limit of 2^20 blocks per data unit as specifed by
165      * IEEE Std 1619-2018.  The earlier and obsolete IEEE Std 1619-2007
166      * indicated that this was a SHOULD NOT rather than a MUST NOT.
167      * NIST SP 800-38E mandates the same limit.
168      */
169     if (inl > XTS_MAX_BLOCKS_PER_DATA_UNIT * AES_BLOCK_SIZE) {
170         ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DATA_UNIT_IS_TOO_LARGE);
171         return 0;
172     }
173
174     if (ctx->stream != NULL)
175         (*ctx->stream)(in, out, inl, ctx->xts.key1, ctx->xts.key2, ctx->base.iv);
176     else if (CRYPTO_xts128_encrypt(&ctx->xts, ctx->base.iv, in, out, inl,
177                                    ctx->base.enc))
178         return 0;
179
180     *outl = inl;
181     return 1;
182 }
183
184 static int aes_xts_stream_update(void *vctx, unsigned char *out, size_t *outl,
185                                  size_t outsize, const unsigned char *in,
186                                  size_t inl)
187 {
188     PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
189
190     if (outsize < inl) {
191         ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
192         return 0;
193     }
194
195     if (!aes_xts_cipher(ctx, out, outl, outsize, in, inl)) {
196         ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
197         return 0;
198     }
199
200     return 1;
201 }
202
203 static int aes_xts_stream_final(void *vctx, unsigned char *out, size_t *outl,
204                                 size_t outsize)
205 {
206     *outl = 0;
207     return 1;
208 }
209
210 static const OSSL_PARAM aes_xts_known_settable_ctx_params[] = {
211     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
212     OSSL_PARAM_END
213 };
214
215 static const OSSL_PARAM *aes_xts_settable_ctx_params(void)
216 {
217     return aes_xts_known_settable_ctx_params;
218 }
219
220 static int aes_xts_set_ctx_params(void *vctx, const OSSL_PARAM params[])
221 {
222     PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
223     const OSSL_PARAM *p;
224
225     /*
226      * TODO(3.0) We need a general solution for handling missing parameters
227      * inside set_params and get_params methods.
228      */
229     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
230     if (p != NULL) {
231         size_t keylen;
232
233         if (!OSSL_PARAM_get_size_t(p, &keylen)) {
234             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
235             return 0;
236         }
237         /* The key length can not be modified for xts mode */
238         if (keylen != ctx->keylen)
239             return 0;
240     }
241
242     return 1;
243 }
244
245 #define IMPLEMENT_cipher(lcmode, UCMODE, kbits, flags)                         \
246 static OSSL_OP_cipher_get_params_fn aes_##kbits##_##lcmode##_get_params;       \
247 static int aes_##kbits##_##lcmode##_get_params(OSSL_PARAM params[])            \
248 {                                                                              \
249     return cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,         \
250                                      flags, 2 * kbits, AES_XTS_BLOCK_BITS,     \
251                                      AES_XTS_IV_BITS);                         \
252 }                                                                              \
253 static OSSL_OP_cipher_newctx_fn aes_##kbits##_xts_newctx;                      \
254 static void *aes_##kbits##_xts_newctx(void *provctx)                           \
255 {                                                                              \
256     return aes_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \
257                           AES_XTS_BLOCK_BITS, AES_XTS_IV_BITS);                \
258 }                                                                              \
259 const OSSL_DISPATCH aes##kbits##xts_functions[] = {                            \
260     { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))aes_##kbits##_xts_newctx },     \
261     { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_xts_einit },          \
262     { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_xts_dinit },          \
263     { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_xts_stream_update },        \
264     { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_xts_stream_final },          \
265     { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))aes_xts_cipher },               \
266     { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_xts_freectx },             \
267     { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_xts_dupctx },               \
268     { OSSL_FUNC_CIPHER_GET_PARAMS,                                             \
269       (void (*)(void))aes_##kbits##_##lcmode##_get_params },                   \
270     { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                        \
271       (void (*)(void))cipher_generic_gettable_params },                        \
272     { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                         \
273       (void (*)(void))cipher_generic_get_ctx_params },                         \
274     { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                    \
275       (void (*)(void))cipher_generic_gettable_ctx_params },                    \
276     { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                         \
277       (void (*)(void))aes_xts_set_ctx_params },                                \
278     { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                    \
279      (void (*)(void))aes_xts_settable_ctx_params },                            \
280     { 0, NULL }                                                                \
281 }
282
283 IMPLEMENT_cipher(xts, XTS, 256, AES_XTS_FLAGS);
284 IMPLEMENT_cipher(xts, XTS, 128, AES_XTS_FLAGS);