2 * Copyright 2019-2020 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 * This file uses the low level AES functions (which are deprecated for
12 * non-internal use) in order to implement provider AES ciphers.
14 #include "internal/deprecated.h"
16 #include "cipher_aes_siv.h"
18 static void aes_siv_cleanup(void *vctx);
20 static int aes_siv_initkey(void *vctx, const unsigned char *key, size_t keylen)
22 PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
23 SIV128_CONTEXT *sctx = &ctx->siv;
24 size_t klen = keylen / 2;
25 OPENSSL_CTX *libctx = ctx->libctx;
26 const char *propq = NULL;
28 EVP_CIPHER_free(ctx->cbc);
29 EVP_CIPHER_free(ctx->ctr);
35 ctx->cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", propq);
36 ctx->ctr = EVP_CIPHER_fetch(libctx, "AES-128-CTR", propq);
39 ctx->cbc = EVP_CIPHER_fetch(libctx, "AES-192-CBC", propq);
40 ctx->ctr = EVP_CIPHER_fetch(libctx, "AES-192-CTR", propq);
43 ctx->cbc = EVP_CIPHER_fetch(libctx, "AES-256-CBC", propq);
44 ctx->ctr = EVP_CIPHER_fetch(libctx, "AES-256-CTR", propq);
49 if (ctx->cbc == NULL || ctx->ctr == NULL)
52 * klen is the length of the underlying cipher, not the input key,
53 * which should be twice as long
55 return CRYPTO_siv128_init(sctx, key, klen, ctx->cbc, ctx->ctr, libctx,
59 static int aes_siv_dupctx(void *in_vctx, void *out_vctx)
61 PROV_AES_SIV_CTX *in = (PROV_AES_SIV_CTX *)in_vctx;
62 PROV_AES_SIV_CTX *out = (PROV_AES_SIV_CTX *)out_vctx;
65 out->siv.cipher_ctx = NULL;
66 out->siv.mac_ctx_init = NULL;
68 if (!CRYPTO_siv128_copy_ctx(&out->siv, &in->siv))
71 EVP_CIPHER_up_ref(out->cbc);
73 EVP_CIPHER_up_ref(out->ctr);
77 static int aes_siv_settag(void *vctx, const unsigned char *tag, size_t tagl)
79 PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
80 SIV128_CONTEXT *sctx = &ctx->siv;
82 return CRYPTO_siv128_set_tag(sctx, tag, tagl);
85 static void aes_siv_setspeed(void *vctx, int speed)
87 PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
88 SIV128_CONTEXT *sctx = &ctx->siv;
90 CRYPTO_siv128_speed(sctx, (int)speed);
93 static void aes_siv_cleanup(void *vctx)
95 PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
96 SIV128_CONTEXT *sctx = &ctx->siv;
98 CRYPTO_siv128_cleanup(sctx);
99 EVP_CIPHER_free(ctx->cbc);
100 EVP_CIPHER_free(ctx->ctr);
103 static int aes_siv_cipher(void *vctx, unsigned char *out,
104 const unsigned char *in, size_t len)
106 PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
107 SIV128_CONTEXT *sctx = &ctx->siv;
109 /* EncryptFinal or DecryptFinal */
111 return CRYPTO_siv128_finish(sctx) == 0;
113 /* Deal with associated data */
115 return (CRYPTO_siv128_aad(sctx, in, len) == 1);
118 return CRYPTO_siv128_encrypt(sctx, in, out, len) > 0;
120 return CRYPTO_siv128_decrypt(sctx, in, out, len) > 0;
123 static const PROV_CIPHER_HW_AES_SIV aes_siv_hw =
133 const PROV_CIPHER_HW_AES_SIV *PROV_CIPHER_HW_aes_siv(size_t keybits)