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 int aes_siv_initkey(void *vctx, const unsigned char *key, size_t keylen)
20 PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
21 SIV128_CONTEXT *sctx = &ctx->siv;
22 size_t klen = keylen / 2;
26 ctx->cbc = EVP_CIPHER_fetch(NULL, "AES-128-CBC", "");
27 ctx->ctr = EVP_CIPHER_fetch(NULL, "AES-128-CTR", "");
30 ctx->cbc = EVP_CIPHER_fetch(NULL, "AES-192-CBC", "");
31 ctx->ctr = EVP_CIPHER_fetch(NULL, "AES-192-CTR", "");
34 ctx->cbc = EVP_CIPHER_fetch(NULL, "AES-256-CBC", "");
35 ctx->ctr = EVP_CIPHER_fetch(NULL, "AES-256-CTR", "");
41 * klen is the length of the underlying cipher, not the input key,
42 * which should be twice as long
44 return CRYPTO_siv128_init(sctx, key, klen, ctx->cbc, ctx->ctr);
47 static int aes_siv_settag(void *vctx, const unsigned char *tag, size_t tagl)
49 PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
50 SIV128_CONTEXT *sctx = &ctx->siv;
52 return CRYPTO_siv128_set_tag(sctx, tag, tagl);
55 static void aes_siv_setspeed(void *vctx, int speed)
57 PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
58 SIV128_CONTEXT *sctx = &ctx->siv;
60 CRYPTO_siv128_speed(sctx, (int)speed);
63 static void aes_siv_cleanup(void *vctx)
65 PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
66 SIV128_CONTEXT *sctx = &ctx->siv;
68 CRYPTO_siv128_cleanup(sctx);
69 EVP_CIPHER_free(ctx->cbc);
70 EVP_CIPHER_free(ctx->ctr);
73 static int aes_siv_cipher(void *vctx, unsigned char *out,
74 const unsigned char *in, size_t len)
76 PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
77 SIV128_CONTEXT *sctx = &ctx->siv;
79 /* EncryptFinal or DecryptFinal */
81 return CRYPTO_siv128_finish(sctx) == 0;
83 /* Deal with associated data */
85 return (CRYPTO_siv128_aad(sctx, in, len) == 1);
88 return CRYPTO_siv128_encrypt(sctx, in, out, len) > 0;
90 return CRYPTO_siv128_decrypt(sctx, in, out, len) > 0;
93 static const PROV_CIPHER_HW_AES_SIV aes_siv_hw =
102 const PROV_CIPHER_HW_AES_SIV *PROV_CIPHER_HW_aes_siv(size_t keybits)