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