Path: blob/main/crypto/openssl/providers/implementations/ciphers/cipher_aes_siv_hw.c
48383 views
/*1* Copyright 2019-2025 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89/*10* This file uses the low level AES functions (which are deprecated for11* non-internal use) in order to implement provider AES ciphers.12*/13#include "internal/deprecated.h"1415#include "cipher_aes_siv.h"1617static void aes_siv_cleanup(void *vctx);1819static int aes_siv_initkey(void *vctx, const unsigned char *key, size_t keylen)20{21PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;22SIV128_CONTEXT *sctx = &ctx->siv;23size_t klen = keylen / 2;24OSSL_LIB_CTX *libctx = ctx->libctx;25const char *propq = NULL;2627EVP_CIPHER_free(ctx->cbc);28EVP_CIPHER_free(ctx->ctr);29ctx->cbc = NULL;30ctx->ctr = NULL;3132switch (klen) {33case 16:34ctx->cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", propq);35ctx->ctr = EVP_CIPHER_fetch(libctx, "AES-128-CTR", propq);36break;37case 24:38ctx->cbc = EVP_CIPHER_fetch(libctx, "AES-192-CBC", propq);39ctx->ctr = EVP_CIPHER_fetch(libctx, "AES-192-CTR", propq);40break;41case 32:42ctx->cbc = EVP_CIPHER_fetch(libctx, "AES-256-CBC", propq);43ctx->ctr = EVP_CIPHER_fetch(libctx, "AES-256-CTR", propq);44break;45default:46break;47}48if (ctx->cbc == NULL || ctx->ctr == NULL)49return 0;50/*51* klen is the length of the underlying cipher, not the input key,52* which should be twice as long53*/54return ossl_siv128_init(sctx, key, klen, ctx->cbc, ctx->ctr, libctx,55propq);56}5758static int aes_siv_dupctx(void *in_vctx, void *out_vctx)59{60PROV_AES_SIV_CTX *in = (PROV_AES_SIV_CTX *)in_vctx;61PROV_AES_SIV_CTX *out = (PROV_AES_SIV_CTX *)out_vctx;6263if (in->cbc != NULL && !EVP_CIPHER_up_ref(in->cbc))64return 0;65if (in->ctr != NULL && !EVP_CIPHER_up_ref(in->ctr)) {66EVP_CIPHER_free(in->cbc);67return 0;68}6970*out = *in;71out->siv.cipher_ctx = NULL;72out->siv.mac_ctx_init = NULL;73out->siv.mac = NULL;74if (!ossl_siv128_copy_ctx(&out->siv, &in->siv))75return 0;7677return 1;78}7980static int aes_siv_settag(void *vctx, const unsigned char *tag, size_t tagl)81{82PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;83SIV128_CONTEXT *sctx = &ctx->siv;8485return ossl_siv128_set_tag(sctx, tag, tagl);86}8788static void aes_siv_setspeed(void *vctx, int speed)89{90PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;91SIV128_CONTEXT *sctx = &ctx->siv;9293ossl_siv128_speed(sctx, (int)speed);94}9596static void aes_siv_cleanup(void *vctx)97{98PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;99SIV128_CONTEXT *sctx = &ctx->siv;100101ossl_siv128_cleanup(sctx);102EVP_CIPHER_free(ctx->cbc);103EVP_CIPHER_free(ctx->ctr);104}105106static int aes_siv_cipher(void *vctx, unsigned char *out,107const unsigned char *in, size_t len)108{109PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;110SIV128_CONTEXT *sctx = &ctx->siv;111112/* EncryptFinal or DecryptFinal */113if (in == NULL)114return ossl_siv128_finish(sctx) == 0;115116/* Deal with associated data */117if (out == NULL)118return (ossl_siv128_aad(sctx, in, len) == 1);119120if (ctx->enc)121return ossl_siv128_encrypt(sctx, in, out, len) > 0;122123return ossl_siv128_decrypt(sctx, in, out, len) > 0;124}125126static const PROV_CIPHER_HW_AES_SIV aes_siv_hw = {127aes_siv_initkey,128aes_siv_cipher,129aes_siv_setspeed,130aes_siv_settag,131aes_siv_cleanup,132aes_siv_dupctx,133};134135const PROV_CIPHER_HW_AES_SIV *ossl_prov_cipher_hw_aes_siv(size_t keybits)136{137return &aes_siv_hw;138}139140141