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