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