Implement AES CTR ciphers in the default provider
[openssl.git] / providers / common / ciphers / aes.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 <string.h>
11 #include <openssl/crypto.h>
12 #include <openssl/core_numbers.h>
13 #include <openssl/core_names.h>
14 #include <openssl/evp.h>
15 #include <openssl/params.h>
16 #include "internal/cryptlib.h"
17 #include "internal/provider_algs.h"
18 #include "ciphers_locl.h"
19
20 static void PROV_AES_KEY_generic_init(PROV_AES_KEY *ctx,
21                                       const unsigned char *iv,
22                                       int enc)
23 {
24     if (iv != NULL)
25         memcpy(ctx->iv, iv, AES_BLOCK_SIZE);
26     ctx->enc = enc;
27 }
28
29 static int aes_einit(void *vctx, const unsigned char *key,
30                            const unsigned char *iv)
31 {
32     PROV_AES_KEY *ctx = (PROV_AES_KEY *)vctx;
33
34     PROV_AES_KEY_generic_init(ctx, iv, 1);
35     if (key != NULL)
36         return ctx->ciph->init(ctx, key, ctx->keylen);
37
38     return 1;
39 }
40
41 static int aes_dinit(void *vctx, const unsigned char *key,
42                      const unsigned char *iv)
43 {
44     PROV_AES_KEY *ctx = (PROV_AES_KEY *)vctx;
45
46     PROV_AES_KEY_generic_init(ctx, iv, 0);
47     if (key != NULL)
48         return ctx->ciph->init(ctx, key, ctx->keylen);
49
50     return 1;
51 }
52
53 static int aes_block_update(void *vctx, unsigned char *out, size_t *outl,
54                             const unsigned char *in, size_t inl)
55 {
56     PROV_AES_KEY *ctx = (PROV_AES_KEY *)vctx;
57     size_t nextblocks = fillblock(ctx->buf, &ctx->bufsz, AES_BLOCK_SIZE, &in,
58                                   &inl);
59     size_t outlint = 0;
60
61     /*
62      * If we're decrypting and we end an update on a block boundary we hold
63      * the last block back in case this is the last update call and the last
64      * block is padded.
65      */
66     if (ctx->bufsz == AES_BLOCK_SIZE
67             && (ctx->enc || inl > 0 || !ctx->pad)) {
68         if (!ctx->ciph->cipher(ctx, out, ctx->buf, AES_BLOCK_SIZE))
69             return 0;
70         ctx->bufsz = 0;
71         outlint = AES_BLOCK_SIZE;
72         out += AES_BLOCK_SIZE;
73     }
74     if (nextblocks > 0) {
75         if (!ctx->enc && ctx->pad && nextblocks == inl) {
76             if (!ossl_assert(inl >= AES_BLOCK_SIZE))
77                 return 0;
78             nextblocks -= AES_BLOCK_SIZE;
79         }
80         if (!ctx->ciph->cipher(ctx, out, in, nextblocks))
81             return 0;
82         in += nextblocks;
83         inl -= nextblocks;
84         outlint += nextblocks;
85     }
86     if (!trailingdata(ctx->buf, &ctx->bufsz, AES_BLOCK_SIZE, &in, &inl))
87         return 0;
88
89     *outl = outlint;
90     return inl == 0;
91 }
92
93 static int aes_block_final(void *vctx, unsigned char *out, size_t *outl)
94 {
95     PROV_AES_KEY *ctx = (PROV_AES_KEY *)vctx;
96
97     if (ctx->enc) {
98         if (ctx->pad) {
99             padblock(ctx->buf, &ctx->bufsz, AES_BLOCK_SIZE);
100         } else if (ctx->bufsz == 0) {
101             *outl = 0;
102             return 1;
103         } else if (ctx->bufsz != AES_BLOCK_SIZE) {
104             /* TODO(3.0): What is the correct error code here? */
105             return 0;
106         }
107
108         if (!ctx->ciph->cipher(ctx, out, ctx->buf, AES_BLOCK_SIZE))
109             return 0;
110         ctx->bufsz = 0;
111         *outl = AES_BLOCK_SIZE;
112         return 1;
113     }
114
115     /* Decrypting */
116     /* TODO(3.0): What's the correct error here */
117     if (ctx->bufsz != AES_BLOCK_SIZE) {
118         if (ctx->bufsz == 0 && !ctx->pad) {
119             *outl = 0;
120             return 1;
121         }
122         return 0;
123     }
124
125     if (!ctx->ciph->cipher(ctx, ctx->buf, ctx->buf, AES_BLOCK_SIZE))
126         return 0;
127
128     /* TODO(3.0): What is the correct error here */
129     if (ctx->pad && !unpadblock(ctx->buf, &ctx->bufsz, AES_BLOCK_SIZE))
130         return 0;
131
132     memcpy(out, ctx->buf, ctx->bufsz);
133     *outl = ctx->bufsz;
134     ctx->bufsz = 0;
135     return 1;
136 }
137
138 static int aes_stream_update(void *vctx, unsigned char *out, size_t *outl,
139                              const unsigned char *in, size_t inl)
140 {
141     PROV_AES_KEY *ctx = (PROV_AES_KEY *)vctx;
142
143     if (!ctx->ciph->cipher(ctx, out, in, inl))
144         return 0;
145
146     *outl = inl;
147     return 1;
148 }
149 static int aes_stream_final(void *vctx, unsigned char *out, size_t *outl)
150 {
151     *outl = 0;
152     return 1;
153 }
154
155 static int aes_cipher(void *vctx, unsigned char *out, const unsigned char *in,
156                       size_t inl)
157 {
158     PROV_AES_KEY *ctx = (PROV_AES_KEY *)vctx;
159
160     if (!ctx->ciph->cipher(ctx, out, in, inl))
161         return 0;
162
163     return 1;
164 }
165
166 #define IMPLEMENT_new_params(lcmode, UCMODE) \
167     static int aes_##lcmode##_get_params(const OSSL_PARAM params[]) \
168     { \
169         const OSSL_PARAM *p; \
170     \
171         p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_MODE); \
172         if (p != NULL && !OSSL_PARAM_set_int(p, EVP_CIPH_##UCMODE##_MODE)) \
173             return 0; \
174     \
175         return 1; \
176     }
177
178 #define IMPLEMENT_new_ctx(lcmode, UCMODE, len) \
179     static void *aes_##len##_##lcmode##_newctx(void) \
180     { \
181         PROV_AES_KEY *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
182     \
183         ctx->pad = 1; \
184         ctx->keylen = (len / 8); \
185         ctx->ciph = PROV_AES_CIPHER_##lcmode(); \
186         ctx->mode = EVP_CIPH_##UCMODE##_MODE; \
187         return ctx; \
188     }
189
190 /* ECB */
191 IMPLEMENT_new_params(ecb, ECB)
192 IMPLEMENT_new_ctx(ecb, ECB, 256)
193 IMPLEMENT_new_ctx(ecb, ECB, 192)
194 IMPLEMENT_new_ctx(ecb, ECB, 128)
195
196 /* CBC */
197 IMPLEMENT_new_params(cbc, CBC)
198 IMPLEMENT_new_ctx(cbc, CBC, 256)
199 IMPLEMENT_new_ctx(cbc, CBC, 192)
200 IMPLEMENT_new_ctx(cbc, CBC, 128)
201
202 /* OFB */
203 IMPLEMENT_new_params(ofb, OFB)
204 IMPLEMENT_new_ctx(ofb, OFB, 256)
205 IMPLEMENT_new_ctx(ofb, OFB, 192)
206 IMPLEMENT_new_ctx(ofb, OFB, 128)
207
208 /* CFB */
209 IMPLEMENT_new_params(cfb, CFB)
210 IMPLEMENT_new_params(cfb1, CFB)
211 IMPLEMENT_new_params(cfb8, CFB)
212 IMPLEMENT_new_ctx(cfb, CFB, 256)
213 IMPLEMENT_new_ctx(cfb, CFB, 192)
214 IMPLEMENT_new_ctx(cfb, CFB, 128)
215 IMPLEMENT_new_ctx(cfb1, CFB, 256)
216 IMPLEMENT_new_ctx(cfb1, CFB, 192)
217 IMPLEMENT_new_ctx(cfb1, CFB, 128)
218 IMPLEMENT_new_ctx(cfb8, CFB, 256)
219 IMPLEMENT_new_ctx(cfb8, CFB, 192)
220 IMPLEMENT_new_ctx(cfb8, CFB, 128)
221
222 /* CTR */
223 IMPLEMENT_new_params(ctr, CTR)
224 IMPLEMENT_new_ctx(ctr, CTR, 256)
225 IMPLEMENT_new_ctx(ctr, CTR, 192)
226 IMPLEMENT_new_ctx(ctr, CTR, 128)
227
228 static void aes_freectx(void *vctx)
229 {
230     PROV_AES_KEY *ctx = (PROV_AES_KEY *)vctx;
231
232     OPENSSL_clear_free(ctx,  sizeof(*ctx));
233 }
234
235 static void *aes_dupctx(void *ctx)
236 {
237     PROV_AES_KEY *in = (PROV_AES_KEY *)ctx;
238     PROV_AES_KEY *ret = OPENSSL_malloc(sizeof(*ret));
239
240     *ret = *in;
241
242     return ret;
243 }
244
245 static size_t key_length_256(void)
246 {
247     return 256 / 8;
248 }
249
250 static size_t key_length_192(void)
251 {
252     return 192 / 8;
253 }
254
255 static size_t key_length_128(void)
256 {
257     return 128 / 8;
258 }
259
260 static size_t iv_length_16(void)
261 {
262     return 16;
263 }
264
265 static size_t iv_length_0(void)
266 {
267     return 0;
268 }
269
270 static size_t block_size_16(void)
271 {
272     return 16;
273 }
274
275 static size_t block_size_1(void)
276 {
277     return 1;
278 }
279
280 static int aes_ctx_get_params(void *vctx, const OSSL_PARAM params[])
281 {
282     PROV_AES_KEY *ctx = (PROV_AES_KEY *)vctx;
283     const OSSL_PARAM *p;
284
285     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_PADDING);
286     if (p != NULL && !OSSL_PARAM_set_uint(p, ctx->pad))
287         return 0;
288
289     return 1;
290 }
291
292 static int aes_ctx_set_params(void *vctx, const OSSL_PARAM params[])
293 {
294     PROV_AES_KEY *ctx = (PROV_AES_KEY *)vctx;
295     const OSSL_PARAM *p;
296
297     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_PADDING);
298     if (p != NULL) {
299         int pad;
300
301         if (!OSSL_PARAM_get_int(p, &pad))
302             return 0;
303         ctx->pad = pad ? 1 : 0;
304     }
305     return 1;
306 }
307
308 #define IMPLEMENT_block_funcs(mode, keylen, ivlen) \
309     const OSSL_DISPATCH aes##keylen##mode##_functions[] = { \
310         { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))aes_##keylen##_##mode##_newctx }, \
311         { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_einit }, \
312         { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_dinit }, \
313         { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_block_update }, \
314         { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_block_final }, \
315         { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))aes_cipher }, \
316         { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_freectx }, \
317         { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_dupctx }, \
318         { OSSL_FUNC_CIPHER_KEY_LENGTH, (void (*)(void))key_length_##keylen }, \
319         { OSSL_FUNC_CIPHER_IV_LENGTH, (void (*)(void))iv_length_##ivlen }, \
320         { OSSL_FUNC_CIPHER_BLOCK_SIZE, (void (*)(void))block_size_16 }, \
321         { OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))aes_##mode##_get_params }, \
322         { OSSL_FUNC_CIPHER_CTX_GET_PARAMS, (void (*)(void))aes_ctx_get_params }, \
323         { OSSL_FUNC_CIPHER_CTX_SET_PARAMS, (void (*)(void))aes_ctx_set_params }, \
324         { 0, NULL } \
325     };
326
327 #define IMPLEMENT_stream_funcs(mode, keylen, ivlen) \
328     const OSSL_DISPATCH aes##keylen##mode##_functions[] = { \
329         { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))aes_##keylen##_##mode##_newctx }, \
330         { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_einit }, \
331         { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_dinit }, \
332         { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_stream_update }, \
333         { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_stream_final }, \
334         { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))aes_cipher }, \
335         { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_freectx }, \
336         { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_dupctx }, \
337         { OSSL_FUNC_CIPHER_KEY_LENGTH, (void (*)(void))key_length_##keylen }, \
338         { OSSL_FUNC_CIPHER_IV_LENGTH, (void (*)(void))iv_length_##ivlen }, \
339         { OSSL_FUNC_CIPHER_BLOCK_SIZE, (void (*)(void))block_size_1 }, \
340         { OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))aes_##mode##_get_params }, \
341         { OSSL_FUNC_CIPHER_CTX_GET_PARAMS, (void (*)(void))aes_ctx_get_params }, \
342         { OSSL_FUNC_CIPHER_CTX_SET_PARAMS, (void (*)(void))aes_ctx_set_params }, \
343         { 0, NULL } \
344     };
345
346 /* ECB */
347 IMPLEMENT_block_funcs(ecb, 256, 0)
348 IMPLEMENT_block_funcs(ecb, 192, 0)
349 IMPLEMENT_block_funcs(ecb, 128, 0)
350
351 /* CBC */
352 IMPLEMENT_block_funcs(cbc, 256, 16)
353 IMPLEMENT_block_funcs(cbc, 192, 16)
354 IMPLEMENT_block_funcs(cbc, 128, 16)
355
356 /* OFB */
357 IMPLEMENT_stream_funcs(ofb, 256, 16)
358 IMPLEMENT_stream_funcs(ofb, 192, 16)
359 IMPLEMENT_stream_funcs(ofb, 128, 16)
360
361 /* CFB */
362 IMPLEMENT_stream_funcs(cfb, 256, 16)
363 IMPLEMENT_stream_funcs(cfb, 192, 16)
364 IMPLEMENT_stream_funcs(cfb, 128, 16)
365 IMPLEMENT_stream_funcs(cfb1, 256, 16)
366 IMPLEMENT_stream_funcs(cfb1, 192, 16)
367 IMPLEMENT_stream_funcs(cfb1, 128, 16)
368 IMPLEMENT_stream_funcs(cfb8, 256, 16)
369 IMPLEMENT_stream_funcs(cfb8, 192, 16)
370 IMPLEMENT_stream_funcs(cfb8, 128, 16)
371
372 /* CTR */
373 IMPLEMENT_stream_funcs(ctr, 256, 16)
374 IMPLEMENT_stream_funcs(ctr, 192, 16)
375 IMPLEMENT_stream_funcs(ctr, 128, 16)