prov: prefix all OSSL_DISPATCH tables names with ossl_
[openssl.git] / providers / implementations / ciphers / cipher_aes_siv_hw.c
1 /*
2  * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /*
11  * This file uses the low level AES functions (which are deprecated for
12  * non-internal use) in order to implement provider AES ciphers.
13  */
14 #include "internal/deprecated.h"
15
16 #include "cipher_aes_siv.h"
17
18 static void aes_siv_cleanup(void *vctx);
19
20 static int aes_siv_initkey(void *vctx, const unsigned char *key, size_t keylen)
21 {
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;
27
28     EVP_CIPHER_free(ctx->cbc);
29     EVP_CIPHER_free(ctx->ctr);
30     ctx->cbc = NULL;
31     ctx->ctr = NULL;
32
33     switch (klen) {
34     case 16:
35         ctx->cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", propq);
36         ctx->ctr = EVP_CIPHER_fetch(libctx, "AES-128-CTR", propq);
37         break;
38     case 24:
39         ctx->cbc = EVP_CIPHER_fetch(libctx, "AES-192-CBC", propq);
40         ctx->ctr = EVP_CIPHER_fetch(libctx, "AES-192-CTR", propq);
41         break;
42     case 32:
43         ctx->cbc = EVP_CIPHER_fetch(libctx, "AES-256-CBC", propq);
44         ctx->ctr = EVP_CIPHER_fetch(libctx, "AES-256-CTR", propq);
45         break;
46     default:
47         break;
48     }
49     if (ctx->cbc == NULL || ctx->ctr == NULL)
50         return 0;
51     /*
52      * klen is the length of the underlying cipher, not the input key,
53      * which should be twice as long
54      */
55     return CRYPTO_siv128_init(sctx, key, klen, ctx->cbc, ctx->ctr, libctx,
56                               propq);
57 }
58
59 static int aes_siv_dupctx(void *in_vctx, void *out_vctx)
60 {
61     PROV_AES_SIV_CTX *in = (PROV_AES_SIV_CTX *)in_vctx;
62     PROV_AES_SIV_CTX *out = (PROV_AES_SIV_CTX *)out_vctx;
63
64     *out = *in;
65     out->siv.cipher_ctx = NULL;
66     out->siv.mac_ctx_init = NULL;
67     out->siv.mac = NULL;
68     if (!CRYPTO_siv128_copy_ctx(&out->siv, &in->siv))
69         return 0;
70     if (out->cbc != NULL)
71         EVP_CIPHER_up_ref(out->cbc);
72     if (out->ctr != NULL)
73         EVP_CIPHER_up_ref(out->ctr);
74     return 1;
75 }
76
77 static int aes_siv_settag(void *vctx, const unsigned char *tag, size_t tagl)
78 {
79     PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
80     SIV128_CONTEXT *sctx = &ctx->siv;
81
82     return CRYPTO_siv128_set_tag(sctx, tag, tagl);
83 }
84
85 static void aes_siv_setspeed(void *vctx, int speed)
86 {
87     PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
88     SIV128_CONTEXT *sctx = &ctx->siv;
89
90     CRYPTO_siv128_speed(sctx, (int)speed);
91 }
92
93 static void aes_siv_cleanup(void *vctx)
94 {
95     PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
96     SIV128_CONTEXT *sctx = &ctx->siv;
97
98     CRYPTO_siv128_cleanup(sctx);
99     EVP_CIPHER_free(ctx->cbc);
100     EVP_CIPHER_free(ctx->ctr);
101 }
102
103 static int aes_siv_cipher(void *vctx, unsigned char *out,
104                           const unsigned char *in, size_t len)
105 {
106     PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
107     SIV128_CONTEXT *sctx = &ctx->siv;
108
109     /* EncryptFinal or DecryptFinal */
110     if (in == NULL)
111         return CRYPTO_siv128_finish(sctx) == 0;
112
113     /* Deal with associated data */
114     if (out == NULL)
115         return (CRYPTO_siv128_aad(sctx, in, len) == 1);
116
117     if (ctx->enc)
118         return CRYPTO_siv128_encrypt(sctx, in, out, len) > 0;
119
120     return CRYPTO_siv128_decrypt(sctx, in, out, len) > 0;
121 }
122
123 static const PROV_CIPHER_HW_AES_SIV aes_siv_hw =
124 {
125     aes_siv_initkey,
126     aes_siv_cipher,
127     aes_siv_setspeed,
128     aes_siv_settag,
129     aes_siv_cleanup,
130     aes_siv_dupctx,
131 };
132
133 const PROV_CIPHER_HW_AES_SIV *PROV_CIPHER_HW_aes_siv(size_t keybits)
134 {
135     return &aes_siv_hw;
136 }