2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
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
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 "ciphers_locl.h"
19 static void PROV_AES_KEY_generic_init(PROV_AES_KEY
*ctx
,
20 const unsigned char *iv
,
24 memcpy(ctx
->iv
, iv
, AES_BLOCK_SIZE
);
28 static int aes_einit(void *vctx
, const unsigned char *key
,
29 const unsigned char *iv
)
31 PROV_AES_KEY
*ctx
= (PROV_AES_KEY
*)vctx
;
33 PROV_AES_KEY_generic_init(ctx
, iv
, 1);
35 return ctx
->ciph
->init(ctx
, key
, ctx
->keylen
);
40 static int aes_dinit(void *vctx
, const unsigned char *key
,
41 const unsigned char *iv
)
43 PROV_AES_KEY
*ctx
= (PROV_AES_KEY
*)vctx
;
45 PROV_AES_KEY_generic_init(ctx
, iv
, 0);
47 return ctx
->ciph
->init(ctx
, key
, ctx
->keylen
);
52 static int aes_update(void *vctx
, unsigned char *out
, size_t *outl
,
53 const unsigned char *in
, size_t inl
)
55 PROV_AES_KEY
*ctx
= (PROV_AES_KEY
*)vctx
;
56 size_t nextblocks
= fillblock(ctx
->buf
, &ctx
->bufsz
, AES_BLOCK_SIZE
, &in
,
61 * If we're decrypting and we end an update on a block boundary we hold
62 * the last block back in case this is the last update call and the last
65 if (ctx
->bufsz
== AES_BLOCK_SIZE
66 && (ctx
->enc
|| inl
> 0 || !ctx
->pad
)) {
67 if (!ctx
->ciph
->cipher(ctx
, out
, ctx
->buf
, AES_BLOCK_SIZE
))
70 outlint
= AES_BLOCK_SIZE
;
71 out
+= AES_BLOCK_SIZE
;
74 if (!ctx
->enc
&& ctx
->pad
&& nextblocks
== inl
) {
75 if (!ossl_assert(inl
>= AES_BLOCK_SIZE
))
77 nextblocks
-= AES_BLOCK_SIZE
;
79 if (!ctx
->ciph
->cipher(ctx
, out
, in
, nextblocks
))
83 outlint
+= nextblocks
;
85 if (!trailingdata(ctx
->buf
, &ctx
->bufsz
, AES_BLOCK_SIZE
, &in
, &inl
))
92 static int aes_final(void *vctx
, unsigned char *out
, size_t *outl
)
94 PROV_AES_KEY
*ctx
= (PROV_AES_KEY
*)vctx
;
98 padblock(ctx
->buf
, &ctx
->bufsz
, AES_BLOCK_SIZE
);
99 } else if (ctx
->bufsz
== 0) {
102 } else if (ctx
->bufsz
!= AES_BLOCK_SIZE
) {
103 /* TODO(3.0): What is the correct error code here? */
107 if (!ctx
->ciph
->cipher(ctx
, out
, ctx
->buf
, AES_BLOCK_SIZE
))
110 *outl
= AES_BLOCK_SIZE
;
115 /* TODO(3.0): What's the correct error here */
116 if (ctx
->bufsz
!= AES_BLOCK_SIZE
) {
117 if (ctx
->bufsz
== 0 && !ctx
->pad
) {
124 if (!ctx
->ciph
->cipher(ctx
, ctx
->buf
, ctx
->buf
, AES_BLOCK_SIZE
))
127 /* TODO(3.0): What is the correct error here */
128 if (ctx
->pad
&& !unpadblock(ctx
->buf
, &ctx
->bufsz
, AES_BLOCK_SIZE
))
131 memcpy(out
, ctx
->buf
, ctx
->bufsz
);
137 static void *aes_256_ecb_newctx(void)
139 PROV_AES_KEY
*ctx
= OPENSSL_zalloc(sizeof(*ctx
));
142 ctx
->keylen
= 256 / 8;
143 ctx
->ciph
= PROV_AES_CIPHER_ecb();
144 ctx
->mode
= EVP_CIPH_ECB_MODE
;
148 static void aes_freectx(void *vctx
)
150 PROV_AES_KEY
*ctx
= (PROV_AES_KEY
*)vctx
;
152 OPENSSL_clear_free(ctx
, sizeof(*ctx
));
155 static void *aes_dupctx(void *ctx
)
157 PROV_AES_KEY
*in
= (PROV_AES_KEY
*)ctx
;
158 PROV_AES_KEY
*ret
= OPENSSL_malloc(sizeof(*ret
));
165 static size_t key_length_256(void)
170 static int aes_get_params(void *vctx
, const OSSL_PARAM params
[])
172 PROV_AES_KEY
*ctx
= (PROV_AES_KEY
*)vctx
;
175 p
= OSSL_PARAM_locate(params
, OSSL_CIPHER_PARAM_PADDING
);
176 if (p
!= NULL
&& !OSSL_PARAM_set_uint(p
, ctx
->pad
))
182 static int aes_set_params(void *vctx
, const OSSL_PARAM params
[])
184 PROV_AES_KEY
*ctx
= (PROV_AES_KEY
*)vctx
;
187 p
= OSSL_PARAM_locate(params
, OSSL_CIPHER_PARAM_PADDING
);
191 if (!OSSL_PARAM_get_int(p
, &pad
))
193 ctx
->pad
= pad
? 1 : 0;
198 extern const OSSL_DISPATCH aes256ecb_functions
[];
199 const OSSL_DISPATCH aes256ecb_functions
[] = {
200 { OSSL_FUNC_CIPHER_NEWCTX
, (void (*)(void))aes_256_ecb_newctx
},
201 { OSSL_FUNC_CIPHER_ENCRYPT_INIT
, (void (*)(void))aes_einit
},
202 { OSSL_FUNC_CIPHER_DECRYPT_INIT
, (void (*)(void))aes_dinit
},
203 { OSSL_FUNC_CIPHER_UPDATE
, (void (*)(void))aes_update
},
204 { OSSL_FUNC_CIPHER_FINAL
, (void (*)(void))aes_final
},
205 { OSSL_FUNC_CIPHER_FREECTX
, (void (*)(void))aes_freectx
},
206 { OSSL_FUNC_CIPHER_DUPCTX
, (void (*)(void))aes_dupctx
},
207 { OSSL_FUNC_CIPHER_KEY_LENGTH
, (void (*)(void))key_length_256
},
208 { OSSL_FUNC_CIPHER_GET_PARAMS
, (void (*)(void))aes_get_params
},
209 { OSSL_FUNC_CIPHER_SET_PARAMS
, (void (*)(void))aes_set_params
},