1 | /* |
2 | * Copyright 2019 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 | /* Dispatch functions for AES SIV mode */ |
11 | |
12 | #include "cipher_aes_siv.h" |
13 | #include "prov/implementations.h" |
14 | #include "prov/providercommonerr.h" |
15 | #include "prov/ciphercommon_aead.h" |
16 | |
17 | #define siv_stream_update siv_cipher |
18 | #define SIV_FLAGS AEAD_FLAGS |
19 | |
20 | static void *aes_siv_newctx(void *provctx, size_t keybits, unsigned int mode, |
21 | uint64_t flags) |
22 | { |
23 | PROV_AES_SIV_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); |
24 | |
25 | if (ctx != NULL) { |
26 | ctx->taglen = SIV_LEN; |
27 | ctx->mode = mode; |
28 | ctx->flags = flags; |
29 | ctx->keylen = keybits / 8; |
30 | ctx->hw = PROV_CIPHER_HW_aes_siv(keybits); |
31 | } |
32 | return ctx; |
33 | } |
34 | |
35 | static void aes_siv_freectx(void *vctx) |
36 | { |
37 | PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx; |
38 | |
39 | if (ctx != NULL) { |
40 | ctx->hw->cleanup(ctx); |
41 | OPENSSL_clear_free(ctx, sizeof(*ctx)); |
42 | } |
43 | } |
44 | |
45 | static int siv_init(void *vctx, const unsigned char *key, size_t keylen, |
46 | const unsigned char *iv, size_t ivlen, int enc) |
47 | { |
48 | PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx; |
49 | |
50 | ctx->enc = enc; |
51 | |
52 | if (key != NULL) { |
53 | if (keylen != ctx->keylen) { |
54 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
55 | return 0; |
56 | } |
57 | return ctx->hw->initkey(ctx, key, ctx->keylen); |
58 | } |
59 | return 1; |
60 | } |
61 | |
62 | static int siv_einit(void *vctx, const unsigned char *key, size_t keylen, |
63 | const unsigned char *iv, size_t ivlen) |
64 | { |
65 | return siv_init(vctx, key, keylen, iv, ivlen, 1); |
66 | } |
67 | |
68 | static int siv_dinit(void *vctx, const unsigned char *key, size_t keylen, |
69 | const unsigned char *iv, size_t ivlen) |
70 | { |
71 | return siv_init(vctx, key, keylen, iv, ivlen, 0); |
72 | } |
73 | |
74 | static int siv_cipher(void *vctx, unsigned char *out, size_t *outl, |
75 | size_t outsize, const unsigned char *in, size_t inl) |
76 | { |
77 | PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx; |
78 | |
79 | if (inl == 0) { |
80 | *outl = 0; |
81 | return 1; |
82 | } |
83 | |
84 | if (outsize < inl) { |
85 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
86 | return 0; |
87 | } |
88 | |
89 | if (ctx->hw->cipher(ctx, out, in, inl) <= 0) |
90 | return 0; |
91 | |
92 | if (outl != NULL) |
93 | *outl = inl; |
94 | return 1; |
95 | } |
96 | |
97 | static int siv_stream_final(void *vctx, unsigned char *out, size_t *outl, |
98 | size_t outsize) |
99 | { |
100 | PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx; |
101 | |
102 | if (!ctx->hw->cipher(vctx, out, NULL, 0)) |
103 | return 0; |
104 | |
105 | if (outl != NULL) |
106 | *outl = 0; |
107 | return 1; |
108 | } |
109 | |
110 | static int aes_siv_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
111 | { |
112 | PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx; |
113 | SIV128_CONTEXT *sctx = &ctx->siv; |
114 | OSSL_PARAM *p; |
115 | |
116 | p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG); |
117 | if (p != NULL && p->data_type == OSSL_PARAM_OCTET_STRING) { |
118 | if (!ctx->enc |
119 | || p->data_size != ctx->taglen |
120 | || !OSSL_PARAM_set_octet_string(p, &sctx->tag.byte, ctx->taglen)) { |
121 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
122 | return 0; |
123 | } |
124 | } |
125 | p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAGLEN); |
126 | if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->taglen)) { |
127 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
128 | return 0; |
129 | } |
130 | p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN); |
131 | if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->keylen)) { |
132 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
133 | return 0; |
134 | } |
135 | return 1; |
136 | } |
137 | |
138 | static const OSSL_PARAM aes_siv_known_gettable_ctx_params[] = { |
139 | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), |
140 | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, NULL), |
141 | OSSL_PARAM_uint(OSSL_CIPHER_PARAM_SPEED, NULL), |
142 | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0), |
143 | OSSL_PARAM_END |
144 | }; |
145 | static const OSSL_PARAM *aes_siv_gettable_ctx_params(void) |
146 | { |
147 | return aes_siv_known_gettable_ctx_params; |
148 | } |
149 | |
150 | static int aes_siv_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
151 | { |
152 | PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx; |
153 | const OSSL_PARAM *p; |
154 | unsigned int speed = 0; |
155 | |
156 | p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG); |
157 | if (p != NULL) { |
158 | if (ctx->enc) |
159 | return 1; |
160 | if (p->data_type != OSSL_PARAM_OCTET_STRING |
161 | || !ctx->hw->settag(ctx, p->data, p->data_size)) { |
162 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
163 | return 0; |
164 | } |
165 | } |
166 | p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_SPEED); |
167 | if (p != NULL) { |
168 | if (!OSSL_PARAM_get_uint(p, &speed)) { |
169 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
170 | return 0; |
171 | } |
172 | ctx->hw->setspeed(ctx, (int)speed); |
173 | } |
174 | p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN); |
175 | if (p != NULL) { |
176 | size_t keylen; |
177 | |
178 | if (!OSSL_PARAM_get_size_t(p, &keylen)) { |
179 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
180 | return 0; |
181 | } |
182 | /* The key length can not be modified */ |
183 | if (keylen != ctx->keylen) |
184 | return 0; |
185 | } |
186 | return 1; |
187 | } |
188 | |
189 | static const OSSL_PARAM aes_siv_known_settable_ctx_params[] = { |
190 | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), |
191 | OSSL_PARAM_uint(OSSL_CIPHER_PARAM_SPEED, NULL), |
192 | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0), |
193 | OSSL_PARAM_END |
194 | }; |
195 | static const OSSL_PARAM *aes_siv_settable_ctx_params(void) |
196 | { |
197 | return aes_siv_known_settable_ctx_params; |
198 | } |
199 | |
200 | #define IMPLEMENT_cipher(alg, lc, UCMODE, flags, kbits, blkbits, ivbits) \ |
201 | static OSSL_OP_cipher_get_params_fn alg##_##kbits##_##lc##_get_params; \ |
202 | static int alg##_##kbits##_##lc##_get_params(OSSL_PARAM params[]) \ |
203 | { \ |
204 | return cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \ |
205 | flags, 2*kbits, blkbits, ivbits); \ |
206 | } \ |
207 | static OSSL_OP_cipher_newctx_fn alg##kbits##lc##_newctx; \ |
208 | static void * alg##kbits##lc##_newctx(void *provctx) \ |
209 | { \ |
210 | return alg##_##lc##_newctx(provctx, 2*kbits, EVP_CIPH_##UCMODE##_MODE, \ |
211 | flags); \ |
212 | } \ |
213 | const OSSL_DISPATCH alg##kbits##lc##_functions[] = { \ |
214 | { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))alg##kbits##lc##_newctx }, \ |
215 | { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))alg##_##lc##_freectx }, \ |
216 | { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void)) lc##_einit }, \ |
217 | { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void)) lc##_dinit }, \ |
218 | { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void)) lc##_stream_update }, \ |
219 | { OSSL_FUNC_CIPHER_FINAL, (void (*)(void)) lc##_stream_final }, \ |
220 | { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void)) lc##_cipher }, \ |
221 | { OSSL_FUNC_CIPHER_GET_PARAMS, \ |
222 | (void (*)(void)) alg##_##kbits##_##lc##_get_params }, \ |
223 | { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \ |
224 | (void (*)(void))cipher_generic_gettable_params }, \ |
225 | { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \ |
226 | (void (*)(void)) alg##_##lc##_get_ctx_params }, \ |
227 | { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \ |
228 | (void (*)(void)) alg##_##lc##_gettable_ctx_params }, \ |
229 | { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \ |
230 | (void (*)(void)) alg##_##lc##_set_ctx_params }, \ |
231 | { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \ |
232 | (void (*)(void)) alg##_##lc##_settable_ctx_params }, \ |
233 | { 0, NULL } \ |
234 | }; |
235 | |
236 | IMPLEMENT_cipher(aes, siv, SIV, SIV_FLAGS, 128, 8, 0) |
237 | IMPLEMENT_cipher(aes, siv, SIV, SIV_FLAGS, 192, 8, 0) |
238 | IMPLEMENT_cipher(aes, siv, SIV, SIV_FLAGS, 256, 8, 0) |
239 | |