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 "internal/provider_algs.h"
18 #include "ciphers_locl.h"
20 static void PROV_AES_KEY_generic_init(PROV_AES_KEY *ctx,
21 const unsigned char *iv,
25 memcpy(ctx->iv, iv, AES_BLOCK_SIZE);
29 static int aes_einit(void *vctx, const unsigned char *key,
30 const unsigned char *iv)
32 PROV_AES_KEY *ctx = (PROV_AES_KEY *)vctx;
34 PROV_AES_KEY_generic_init(ctx, iv, 1);
36 return ctx->ciph->init(ctx, key, ctx->keylen);
41 static int aes_dinit(void *vctx, const unsigned char *key,
42 const unsigned char *iv)
44 PROV_AES_KEY *ctx = (PROV_AES_KEY *)vctx;
46 PROV_AES_KEY_generic_init(ctx, iv, 0);
48 return ctx->ciph->init(ctx, key, ctx->keylen);
53 static int aes_update(void *vctx, unsigned char *out, size_t *outl,
54 const unsigned char *in, size_t inl)
56 PROV_AES_KEY *ctx = (PROV_AES_KEY *)vctx;
57 size_t nextblocks = fillblock(ctx->buf, &ctx->bufsz, AES_BLOCK_SIZE, &in,
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
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))
71 outlint = AES_BLOCK_SIZE;
72 out += AES_BLOCK_SIZE;
75 if (!ctx->enc && ctx->pad && nextblocks == inl) {
76 if (!ossl_assert(inl >= AES_BLOCK_SIZE))
78 nextblocks -= AES_BLOCK_SIZE;
80 if (!ctx->ciph->cipher(ctx, out, in, nextblocks))
84 outlint += nextblocks;
86 if (!trailingdata(ctx->buf, &ctx->bufsz, AES_BLOCK_SIZE, &in, &inl))
93 static int aes_final(void *vctx, unsigned char *out, size_t *outl)
95 PROV_AES_KEY *ctx = (PROV_AES_KEY *)vctx;
99 padblock(ctx->buf, &ctx->bufsz, AES_BLOCK_SIZE);
100 } else if (ctx->bufsz == 0) {
103 } else if (ctx->bufsz != AES_BLOCK_SIZE) {
104 /* TODO(3.0): What is the correct error code here? */
108 if (!ctx->ciph->cipher(ctx, out, ctx->buf, AES_BLOCK_SIZE))
111 *outl = AES_BLOCK_SIZE;
116 /* TODO(3.0): What's the correct error here */
117 if (ctx->bufsz != AES_BLOCK_SIZE) {
118 if (ctx->bufsz == 0 && !ctx->pad) {
125 if (!ctx->ciph->cipher(ctx, ctx->buf, ctx->buf, AES_BLOCK_SIZE))
128 /* TODO(3.0): What is the correct error here */
129 if (ctx->pad && !unpadblock(ctx->buf, &ctx->bufsz, AES_BLOCK_SIZE))
132 memcpy(out, ctx->buf, ctx->bufsz);
138 static void *aes_256_ecb_newctx(void)
140 PROV_AES_KEY *ctx = OPENSSL_zalloc(sizeof(*ctx));
143 ctx->keylen = 256 / 8;
144 ctx->ciph = PROV_AES_CIPHER_ecb();
145 ctx->mode = EVP_CIPH_ECB_MODE;
149 static void aes_freectx(void *vctx)
151 PROV_AES_KEY *ctx = (PROV_AES_KEY *)vctx;
153 OPENSSL_clear_free(ctx, sizeof(*ctx));
156 static void *aes_dupctx(void *ctx)
158 PROV_AES_KEY *in = (PROV_AES_KEY *)ctx;
159 PROV_AES_KEY *ret = OPENSSL_malloc(sizeof(*ret));
166 static size_t key_length_256(void)
171 static int aes_get_params(void *vctx, const OSSL_PARAM params[])
173 PROV_AES_KEY *ctx = (PROV_AES_KEY *)vctx;
176 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_PADDING);
177 if (p != NULL && !OSSL_PARAM_set_uint(p, ctx->pad))
183 static int aes_set_params(void *vctx, const OSSL_PARAM params[])
185 PROV_AES_KEY *ctx = (PROV_AES_KEY *)vctx;
188 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_PADDING);
192 if (!OSSL_PARAM_get_int(p, &pad))
194 ctx->pad = pad ? 1 : 0;
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 },