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