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