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
10 #include "cipher_aes_siv.h"
12 static int aes_siv_initkey(void *vctx, const unsigned char *key, size_t keylen)
14 PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
15 SIV128_CONTEXT *sctx = &ctx->siv;
16 size_t klen = keylen / 2;
20 ctx->cbc = EVP_CIPHER_fetch(NULL, "AES-128-CBC", "");
21 ctx->ctr = EVP_CIPHER_fetch(NULL, "AES-128-CTR", "");
24 ctx->cbc = EVP_CIPHER_fetch(NULL, "AES-192-CBC", "");
25 ctx->ctr = EVP_CIPHER_fetch(NULL, "AES-192-CTR", "");
28 ctx->cbc = EVP_CIPHER_fetch(NULL, "AES-256-CBC", "");
29 ctx->ctr = EVP_CIPHER_fetch(NULL, "AES-256-CTR", "");
35 * klen is the length of the underlying cipher, not the input key,
36 * which should be twice as long
38 return CRYPTO_siv128_init(sctx, key, klen, ctx->cbc, ctx->ctr);
41 static int aes_siv_settag(void *vctx, const unsigned char *tag, size_t tagl)
43 PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
44 SIV128_CONTEXT *sctx = &ctx->siv;
46 return CRYPTO_siv128_set_tag(sctx, tag, tagl);
49 static void aes_siv_setspeed(void *vctx, int speed)
51 PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
52 SIV128_CONTEXT *sctx = &ctx->siv;
54 CRYPTO_siv128_speed(sctx, (int)speed);
57 static void aes_siv_cleanup(void *vctx)
59 PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
60 SIV128_CONTEXT *sctx = &ctx->siv;
62 CRYPTO_siv128_cleanup(sctx);
63 EVP_CIPHER_free(ctx->cbc);
64 EVP_CIPHER_free(ctx->ctr);
67 static int aes_siv_cipher(void *vctx, unsigned char *out,
68 const unsigned char *in, size_t len)
70 PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
71 SIV128_CONTEXT *sctx = &ctx->siv;
73 /* EncryptFinal or DecryptFinal */
75 return CRYPTO_siv128_finish(sctx) == 0;
77 /* Deal with associated data */
79 return (CRYPTO_siv128_aad(sctx, in, len) == 1);
82 return CRYPTO_siv128_encrypt(sctx, in, out, len) > 0;
84 return CRYPTO_siv128_decrypt(sctx, in, out, len) > 0;
87 static const PROV_CIPHER_HW_AES_SIV aes_siv_hw =
96 const PROV_CIPHER_HW_AES_SIV *PROV_CIPHER_HW_aes_siv(size_t keybits)