Add AES_CBC_CTS ciphers to providers
[openssl.git] / providers / implementations / ciphers / cipher_aes_wrp.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  * This file uses the low level AES functions (which are deprecated for
12  * non-internal use) in order to implement provider AES ciphers.
13  */
14 #include "internal/deprecated.h"
15
16 #include "cipher_aes.h"
17 #include "prov/providercommonerr.h"
18 #include "prov/implementations.h"
19
20 /* AES wrap with padding has IV length of 4, without padding 8 */
21 #define AES_WRAP_PAD_IVLEN   4
22 #define AES_WRAP_NOPAD_IVLEN 8
23
24 /* TODO(3.0) Figure out what flags need to be passed */
25 #define WRAP_FLAGS (EVP_CIPH_WRAP_MODE | EVP_CIPH_CUSTOM_IV \
26                     | EVP_CIPH_ALWAYS_CALL_INIT)
27
28 typedef size_t (*aeswrap_fn)(void *key, const unsigned char *iv,
29                              unsigned char *out, const unsigned char *in,
30                              size_t inlen, block128_f block);
31
32 static OSSL_FUNC_cipher_encrypt_init_fn aes_wrap_einit;
33 static OSSL_FUNC_cipher_decrypt_init_fn aes_wrap_dinit;
34 static OSSL_FUNC_cipher_update_fn aes_wrap_cipher;
35 static OSSL_FUNC_cipher_final_fn aes_wrap_final;
36 static OSSL_FUNC_cipher_freectx_fn aes_wrap_freectx;
37
38 typedef struct prov_aes_wrap_ctx_st {
39     PROV_CIPHER_CTX base;
40     union {
41         OSSL_UNION_ALIGN;
42         AES_KEY ks;
43     } ks;
44     aeswrap_fn wrapfn;
45
46 } PROV_AES_WRAP_CTX;
47
48
49 static void *aes_wrap_newctx(size_t kbits, size_t blkbits,
50                              size_t ivbits, unsigned int mode, uint64_t flags)
51 {
52     PROV_AES_WRAP_CTX *wctx = OPENSSL_zalloc(sizeof(*wctx));
53     PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)wctx;
54
55     if (ctx != NULL) {
56         cipher_generic_initkey(ctx, kbits, blkbits, ivbits, mode, flags,
57                                NULL, NULL);
58         ctx->pad = (ctx->ivlen == AES_WRAP_PAD_IVLEN);
59     }
60     return wctx;
61 }
62
63 static void aes_wrap_freectx(void *vctx)
64 {
65     PROV_AES_WRAP_CTX *wctx = (PROV_AES_WRAP_CTX *)vctx;
66
67     cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
68     OPENSSL_clear_free(wctx,  sizeof(*wctx));
69 }
70
71 static int aes_wrap_init(void *vctx, const unsigned char *key,
72                          size_t keylen, const unsigned char *iv,
73                          size_t ivlen, int enc)
74 {
75     PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
76     PROV_AES_WRAP_CTX *wctx = (PROV_AES_WRAP_CTX *)vctx;
77
78     ctx->enc = enc;
79     ctx->block = enc ? (block128_f)AES_encrypt : (block128_f)AES_decrypt;
80     if (ctx->pad)
81         wctx->wrapfn = enc ? CRYPTO_128_wrap_pad : CRYPTO_128_unwrap_pad;
82     else
83         wctx->wrapfn = enc ? CRYPTO_128_wrap : CRYPTO_128_unwrap;
84
85     if (iv != NULL) {
86         if (!cipher_generic_initiv(ctx, iv, ivlen))
87             return 0;
88     }
89     if (key != NULL) {
90         if (keylen != ctx->keylen) {
91            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
92            return 0;
93         }
94         if (ctx->enc)
95             AES_set_encrypt_key(key, keylen * 8, &wctx->ks.ks);
96         else
97             AES_set_decrypt_key(key, keylen * 8, &wctx->ks.ks);
98     }
99     return 1;
100 }
101
102 static int aes_wrap_einit(void *ctx, const unsigned char *key, size_t keylen,
103                           const unsigned char *iv, size_t ivlen)
104 {
105     return aes_wrap_init(ctx, key, keylen, iv, ivlen, 1);
106 }
107
108 static int aes_wrap_dinit(void *ctx, const unsigned char *key, size_t keylen,
109                           const unsigned char *iv, size_t ivlen)
110 {
111     return aes_wrap_init(ctx, key, keylen, iv, ivlen, 0);
112 }
113
114 static int aes_wrap_cipher_internal(void *vctx, unsigned char *out,
115                                     const unsigned char *in, size_t inlen)
116 {
117     PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
118     PROV_AES_WRAP_CTX *wctx = (PROV_AES_WRAP_CTX *)vctx;
119     size_t rv;
120     int pad = ctx->pad;
121
122     /* No final operation so always return zero length */
123     if (in == NULL)
124         return 0;
125
126     /* Input length must always be non-zero */
127     if (inlen == 0)
128         return -1;
129
130     /* If decrypting need at least 16 bytes and multiple of 8 */
131     if (!ctx->enc && (inlen < 16 || inlen & 0x7))
132         return -1;
133
134     /* If not padding input must be multiple of 8 */
135     if (!pad && inlen & 0x7)
136         return -1;
137
138     if (out == NULL) {
139         if (ctx->enc) {
140             /* If padding round up to multiple of 8 */
141             if (pad)
142                 inlen = (inlen + 7) / 8 * 8;
143             /* 8 byte prefix */
144             return inlen + 8;
145         } else {
146             /*
147              * If not padding output will be exactly 8 bytes smaller than
148              * input. If padding it will be at least 8 bytes smaller but we
149              * don't know how much.
150              */
151             return inlen - 8;
152         }
153     }
154
155     rv = wctx->wrapfn(&wctx->ks.ks, ctx->iv_set ? ctx->iv : NULL, out, in,
156                       inlen, ctx->block);
157     return rv ? (int)rv : -1;
158 }
159
160 static int aes_wrap_final(void *vctx, unsigned char *out, size_t *outl,
161                           size_t outsize)
162 {
163     *outl = 0;
164     return 1;
165 }
166
167 static int aes_wrap_cipher(void *vctx,
168                            unsigned char *out, size_t *outl, size_t outsize,
169                            const unsigned char *in, size_t inl)
170 {
171     PROV_AES_WRAP_CTX *ctx = (PROV_AES_WRAP_CTX *)vctx;
172     size_t len;
173
174     if (inl == 0) {
175         *outl = 0;
176         return 1;
177     }
178
179     if (outsize < inl) {
180         ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
181         return -1;
182     }
183
184     len = aes_wrap_cipher_internal(ctx, out, in, inl);
185     if (len == 0)
186         return -1;
187
188     *outl = len;
189     return 1;
190 }
191
192 static int aes_wrap_set_ctx_params(void *vctx, const OSSL_PARAM params[])
193 {
194     PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
195     const OSSL_PARAM *p;
196     size_t keylen = 0;
197
198     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
199     if (p != NULL) {
200         if (!OSSL_PARAM_get_size_t(p, &keylen)) {
201             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
202             return 0;
203         }
204         if (ctx->keylen != keylen) {
205             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
206             return 0;
207         }
208     }
209     return 1;
210 }
211
212 #define IMPLEMENT_cipher(mode, fname, UCMODE, flags, kbits, blkbits, ivbits)   \
213     static OSSL_FUNC_cipher_get_params_fn aes_##kbits##_##fname##_get_params;  \
214     static int aes_##kbits##_##fname##_get_params(OSSL_PARAM params[])         \
215     {                                                                          \
216         return cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,     \
217                                          flags, kbits, blkbits, ivbits);       \
218     }                                                                          \
219     static OSSL_FUNC_cipher_newctx_fn aes_##kbits##fname##_newctx;             \
220     static void *aes_##kbits##fname##_newctx(void *provctx)                    \
221     {                                                                          \
222         return aes_##mode##_newctx(kbits, blkbits, ivbits,                     \
223                                    EVP_CIPH_##UCMODE##_MODE, flags);           \
224     }                                                                          \
225     const OSSL_DISPATCH aes##kbits##fname##_functions[] = {                    \
226         { OSSL_FUNC_CIPHER_NEWCTX,                                             \
227             (void (*)(void))aes_##kbits##fname##_newctx },                     \
228         { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_##mode##_einit }, \
229         { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_##mode##_dinit }, \
230         { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_##mode##_cipher },      \
231         { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_##mode##_final },        \
232         { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_##mode##_freectx },    \
233         { OSSL_FUNC_CIPHER_GET_PARAMS,                                         \
234             (void (*)(void))aes_##kbits##_##fname##_get_params },              \
235         { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                    \
236             (void (*)(void))cipher_generic_gettable_params },                  \
237         { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                     \
238             (void (*)(void))cipher_generic_get_ctx_params },                   \
239         { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                     \
240             (void (*)(void))aes_wrap_set_ctx_params },                         \
241         { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                \
242             (void (*)(void))cipher_generic_gettable_ctx_params },              \
243         { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                \
244             (void (*)(void))cipher_generic_settable_ctx_params },              \
245         { 0, NULL }                                                            \
246     }
247
248 IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 256, 64, AES_WRAP_NOPAD_IVLEN * 8);
249 IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 192, 64, AES_WRAP_NOPAD_IVLEN * 8);
250 IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 128, 64, AES_WRAP_NOPAD_IVLEN * 8);
251 IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 256, 64, AES_WRAP_PAD_IVLEN * 8);
252 IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 192, 64, AES_WRAP_PAD_IVLEN * 8);
253 IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 128, 64, AES_WRAP_PAD_IVLEN * 8);