/src/openssl/providers/implementations/ciphers/cipher_ascon_aead128.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2026 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 | | #include "cipher_ascon_aead128.h" |
11 | | #include <string.h> |
12 | | #include <openssl/core_names.h> |
13 | | #include <openssl/err.h> |
14 | | #include <openssl/proverr.h> |
15 | | #include "prov/implementations.h" |
16 | | #include "prov/providercommon.h" |
17 | | #include "prov/ciphercommon.h" |
18 | | #include "prov/ciphercommon_aead.h" |
19 | | #include "internal/common.h" |
20 | | #include "providers/implementations/ciphers/cipher_ascon_aead128.inc" |
21 | | |
22 | | static OSSL_FUNC_cipher_newctx_fn ascon_aead128_newctx; |
23 | | static OSSL_FUNC_cipher_freectx_fn ascon_aead128_freectx; |
24 | | static OSSL_FUNC_cipher_dupctx_fn ascon_aead128_dupctx; |
25 | | static OSSL_FUNC_cipher_encrypt_init_fn ascon_aead128_einit; |
26 | | static OSSL_FUNC_cipher_decrypt_init_fn ascon_aead128_dinit; |
27 | | static OSSL_FUNC_cipher_update_fn ascon_aead128_update; |
28 | | static OSSL_FUNC_cipher_final_fn ascon_aead128_final; |
29 | | static OSSL_FUNC_cipher_cipher_fn ascon_aead128_cipher; |
30 | | static OSSL_FUNC_cipher_get_params_fn ascon_aead128_get_params; |
31 | | static OSSL_FUNC_cipher_get_ctx_params_fn ascon_aead128_get_ctx_params; |
32 | | static OSSL_FUNC_cipher_set_ctx_params_fn ascon_aead128_set_ctx_params; |
33 | | static OSSL_FUNC_cipher_gettable_ctx_params_fn ascon_aead128_gettable_ctx_params; |
34 | | static OSSL_FUNC_cipher_settable_ctx_params_fn ascon_aead128_settable_ctx_params; |
35 | | #define ascon_aead128_gettable_params ossl_cipher_generic_gettable_params |
36 | | |
37 | 1 | #define ASCON_AEAD128_FLAGS (PROV_CIPHER_FLAG_AEAD | PROV_CIPHER_FLAG_CUSTOM_IV) |
38 | | |
39 | | static void ascon_aead128_cleanctx(void *vctx) |
40 | 0 | { |
41 | 0 | struct ascon_aead128_ctx_st *ctx = vctx; |
42 | |
|
43 | 0 | ctx->is_tag_set = 0; |
44 | 0 | ctx->is_ongoing = 0; |
45 | 0 | ctx->assoc_data_not_allowed = 0; |
46 | 0 | ctx->iv_set = 0; |
47 | 0 | if (ctx->internal_ctx != NULL) |
48 | 0 | ossl_ascon_aead_cleanup(ctx->internal_ctx); |
49 | 0 | OPENSSL_cleanse(ctx->tag, sizeof(ctx->tag)); |
50 | 0 | OPENSSL_cleanse(ctx->iv, sizeof(ctx->iv)); |
51 | | /* Note: key is not cleared here to allow reinitialization with NULL key */ |
52 | 0 | } |
53 | | |
54 | | static void *ascon_aead128_newctx(void *provctx) |
55 | 0 | { |
56 | 0 | struct ascon_aead128_ctx_st *ctx; |
57 | 0 | ASCON_AEAD_CTX *intctx; |
58 | |
|
59 | 0 | if (!ossl_prov_is_running()) |
60 | 0 | return NULL; |
61 | | |
62 | 0 | ctx = OPENSSL_zalloc(sizeof(*ctx)); |
63 | 0 | if (ctx == NULL) |
64 | 0 | return NULL; |
65 | | |
66 | 0 | ctx->provctx = provctx; |
67 | 0 | ctx->is_tag_set = 0; |
68 | 0 | ctx->is_ongoing = 0; |
69 | 0 | ctx->assoc_data_not_allowed = 0; |
70 | 0 | ctx->iv_set = 0; |
71 | 0 | ctx->key_set = 0; |
72 | |
|
73 | 0 | intctx = OPENSSL_zalloc(sizeof(*intctx)); |
74 | 0 | if (intctx == NULL) { |
75 | 0 | OPENSSL_free(ctx); |
76 | 0 | return NULL; |
77 | 0 | } |
78 | 0 | ctx->internal_ctx = intctx; |
79 | |
|
80 | 0 | return ctx; |
81 | 0 | } |
82 | | |
83 | | static void *ascon_aead128_dupctx(void *vctx) |
84 | 0 | { |
85 | 0 | struct ascon_aead128_ctx_st *src = vctx; |
86 | 0 | struct ascon_aead128_ctx_st *dst = NULL; |
87 | 0 | ASCON_AEAD_CTX *saved_internal_ctx; |
88 | |
|
89 | 0 | if (src == NULL || !ossl_prov_is_running()) |
90 | 0 | return NULL; |
91 | | |
92 | | /* Create new context using the same provider context */ |
93 | 0 | if ((dst = ascon_aead128_newctx(src->provctx)) == NULL) |
94 | 0 | return NULL; |
95 | | |
96 | | /* Save the newly allocated internal_ctx pointer before overwriting */ |
97 | 0 | saved_internal_ctx = dst->internal_ctx; |
98 | | |
99 | | /* Copy all context fields */ |
100 | 0 | *dst = *src; |
101 | | |
102 | | /* Restore the newly allocated internal_ctx pointer */ |
103 | 0 | dst->internal_ctx = saved_internal_ctx; |
104 | | |
105 | | /* Deep copy the internal LibAscon context */ |
106 | 0 | if (src->internal_ctx != NULL && dst->internal_ctx != NULL) |
107 | 0 | memcpy(dst->internal_ctx, src->internal_ctx, sizeof(*dst->internal_ctx)); |
108 | |
|
109 | 0 | return dst; |
110 | 0 | } |
111 | | |
112 | | static void ascon_aead128_freectx(void *vctx) |
113 | 0 | { |
114 | 0 | struct ascon_aead128_ctx_st *ctx = vctx; |
115 | |
|
116 | 0 | if (ctx == NULL) |
117 | 0 | return; |
118 | | |
119 | 0 | ctx->provctx = NULL; |
120 | 0 | ascon_aead128_cleanctx(ctx); |
121 | 0 | OPENSSL_cleanse(ctx->key, sizeof(ctx->key)); |
122 | 0 | OPENSSL_free(ctx->internal_ctx); |
123 | 0 | OPENSSL_free(ctx); |
124 | 0 | } |
125 | | |
126 | | /* Internal initialization function (shared by encrypt and decrypt init) */ |
127 | | |
128 | | static int ascon_aead128_internal_init(void *vctx, direction_t direction, |
129 | | const unsigned char *key, size_t keylen, |
130 | | const unsigned char *iv, size_t ivlen, |
131 | | const OSSL_PARAM params[]) |
132 | 0 | { |
133 | 0 | struct ascon_aead128_ctx_st *ctx = vctx; |
134 | 0 | uint8_t saved_tag[ASCON_AEAD_TAG_LEN]; |
135 | 0 | int tag_was_set; |
136 | 0 | unsigned char ivcopy[ASCON_AEAD_NONCE_LEN]; |
137 | |
|
138 | 0 | if (ctx == NULL) |
139 | 0 | return 0; |
140 | | |
141 | 0 | if (key == NULL && iv == NULL) { |
142 | 0 | ctx->direction = direction; |
143 | 0 | if (params != NULL && !ascon_aead128_set_ctx_params(ctx, params)) |
144 | 0 | return 0; |
145 | 0 | return 1; |
146 | 0 | } |
147 | | |
148 | 0 | if (key != NULL) { |
149 | 0 | if (keylen != ASCON_AEAD128_KEY_LEN) { |
150 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
151 | 0 | return 0; |
152 | 0 | } |
153 | 0 | memcpy(ctx->key, key, ASCON_AEAD128_KEY_LEN); |
154 | 0 | ctx->key_set = 1; |
155 | 0 | } |
156 | | |
157 | 0 | if (iv != NULL) { |
158 | 0 | if (ivlen != ASCON_AEAD_NONCE_LEN) { |
159 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
160 | 0 | return 0; |
161 | 0 | } |
162 | 0 | memcpy(ctx->iv, iv, ASCON_AEAD_NONCE_LEN); |
163 | 0 | ctx->iv_set = 1; |
164 | 0 | } |
165 | | |
166 | 0 | ctx->direction = direction; |
167 | |
|
168 | 0 | tag_was_set = ctx->is_tag_set; |
169 | 0 | if (tag_was_set && direction == DECRYPTION) |
170 | 0 | memcpy(saved_tag, ctx->tag, ASCON_AEAD_TAG_LEN); |
171 | |
|
172 | 0 | if (ctx->key_set && ctx->iv_set) { |
173 | 0 | memcpy(ivcopy, ctx->iv, sizeof(ivcopy)); |
174 | 0 | ascon_aead128_cleanctx(ctx); |
175 | 0 | ctx->key_set = 1; |
176 | 0 | memcpy(ctx->iv, ivcopy, sizeof(ivcopy)); |
177 | 0 | ctx->iv_set = 1; |
178 | 0 | if (tag_was_set && direction == DECRYPTION) { |
179 | 0 | memcpy(ctx->tag, saved_tag, ASCON_AEAD_TAG_LEN); |
180 | 0 | ctx->is_tag_set = 1; |
181 | 0 | } |
182 | 0 | ossl_ascon_aead128_init(ctx->internal_ctx, ctx->key, ctx->iv); |
183 | 0 | ctx->is_ongoing = 1; |
184 | 0 | } else { |
185 | 0 | ctx->is_ongoing = 0; |
186 | 0 | ctx->assoc_data_not_allowed = 0; |
187 | 0 | ctx->is_tag_set = 0; |
188 | 0 | if (ctx->internal_ctx != NULL) |
189 | 0 | ossl_ascon_aead_cleanup(ctx->internal_ctx); |
190 | 0 | OPENSSL_cleanse(ctx->tag, sizeof(ctx->tag)); |
191 | 0 | if (!ctx->key_set) |
192 | 0 | OPENSSL_cleanse(ctx->key, sizeof(ctx->key)); |
193 | 0 | if (!ctx->iv_set) |
194 | 0 | OPENSSL_cleanse(ctx->iv, sizeof(ctx->iv)); |
195 | 0 | } |
196 | |
|
197 | 0 | if (params != NULL && !ascon_aead128_set_ctx_params(ctx, params)) |
198 | 0 | return 0; |
199 | | |
200 | 0 | return 1; |
201 | 0 | } |
202 | | |
203 | | static int ascon_aead128_einit(void *vctx, const unsigned char *key, |
204 | | size_t keylen, const unsigned char *iv, |
205 | | size_t ivlen, const OSSL_PARAM params[]) |
206 | 0 | { |
207 | 0 | return ascon_aead128_internal_init(vctx, ENCRYPTION, key, keylen, iv, ivlen, params); |
208 | 0 | } |
209 | | |
210 | | static int ascon_aead128_dinit(void *vctx, const unsigned char *key, |
211 | | size_t keylen, const unsigned char *iv, |
212 | | size_t ivlen, const OSSL_PARAM params[]) |
213 | 0 | { |
214 | 0 | return ascon_aead128_internal_init(vctx, DECRYPTION, key, keylen, iv, ivlen, params); |
215 | 0 | } |
216 | | |
217 | | static int ascon_aead128_update(void *vctx, unsigned char *out, size_t *outl, |
218 | | size_t outsize, const unsigned char *in, size_t inl) |
219 | 0 | { |
220 | 0 | struct ascon_aead128_ctx_st *ctx = vctx; |
221 | |
|
222 | 0 | if (ctx == NULL) { |
223 | | /* Context must be set before update */ |
224 | 0 | return 0; |
225 | 0 | } |
226 | | |
227 | 0 | if (!ctx->is_ongoing) { |
228 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
229 | 0 | return 0; |
230 | 0 | } |
231 | | |
232 | | /* Handle AAD operation (out == NULL) - process associated data */ |
233 | 0 | if (out == NULL) { |
234 | | /* Can only add AAD before encryption/decryption updates start */ |
235 | 0 | if (ctx->assoc_data_not_allowed) { |
236 | | /* AAD already processed or encryption started - cannot add more */ |
237 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_UPDATE_CALL_OUT_OF_ORDER); |
238 | 0 | return 0; |
239 | 0 | } |
240 | | |
241 | | /* Process AAD if provided */ |
242 | 0 | if (inl > 0 && in != NULL) { |
243 | 0 | if (ctx->internal_ctx == NULL) { |
244 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
245 | 0 | return 0; |
246 | 0 | } |
247 | 0 | ossl_ascon_aead128_assoc_data_update(ctx->internal_ctx, in, inl); |
248 | 0 | } |
249 | 0 | if (outl != NULL) |
250 | 0 | *outl = 0; |
251 | 0 | return 1; |
252 | 0 | } |
253 | | |
254 | 0 | if (outsize < inl) { |
255 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
256 | 0 | return 0; |
257 | 0 | } |
258 | 0 | if (inl > 0 && in == NULL) { |
259 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_INPUT_LENGTH); |
260 | 0 | return 0; |
261 | 0 | } |
262 | | |
263 | 0 | if (ctx->direction == ENCRYPTION) { |
264 | 0 | const uint8_t *plaintext = in; |
265 | 0 | size_t plaintext_len = inl; |
266 | 0 | uint8_t *ciphertext = out; |
267 | 0 | size_t ciphertext_len; |
268 | | |
269 | | /* Mark that we've started encryption - AAD cannot be added after this */ |
270 | | /* LibAscon encrypt_update will finalize AAD automatically if needed */ |
271 | 0 | ctx->assoc_data_not_allowed = 1; |
272 | |
|
273 | 0 | if (ctx->internal_ctx == NULL) { |
274 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
275 | 0 | return 0; |
276 | 0 | } |
277 | | |
278 | 0 | ciphertext_len = ossl_ascon_aead128_encrypt_update(ctx->internal_ctx, |
279 | 0 | ciphertext, |
280 | 0 | plaintext, |
281 | 0 | plaintext_len); |
282 | 0 | if (outl != NULL) |
283 | 0 | *outl = ciphertext_len; |
284 | 0 | return 1; |
285 | 0 | } else if (ctx->direction == DECRYPTION) { |
286 | 0 | uint8_t *plaintext = out; |
287 | 0 | size_t plaintext_len; |
288 | 0 | const uint8_t *ciphertext = in; |
289 | 0 | size_t ciphertext_len = inl; |
290 | | |
291 | | /* Mark that we've started decryption - AAD cannot be added after this */ |
292 | | /* LibAscon decrypt_update will finalize AAD automatically if needed */ |
293 | 0 | ctx->assoc_data_not_allowed = 1; |
294 | |
|
295 | 0 | if (ctx->internal_ctx == NULL) { |
296 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
297 | 0 | return 0; |
298 | 0 | } |
299 | | |
300 | 0 | plaintext_len = ossl_ascon_aead128_decrypt_update(ctx->internal_ctx, |
301 | 0 | plaintext, |
302 | 0 | ciphertext, |
303 | 0 | ciphertext_len); |
304 | 0 | if (outl != NULL) |
305 | 0 | *outl = plaintext_len; |
306 | 0 | return 1; |
307 | 0 | } |
308 | 0 | return 0; |
309 | 0 | } |
310 | | |
311 | | /* PROVIDER'S FINAL FUNCTION */ |
312 | | |
313 | | static int ascon_aead128_final(void *vctx, unsigned char *out, size_t *outl, size_t outsize) |
314 | 0 | { |
315 | 0 | struct ascon_aead128_ctx_st *ctx = vctx; |
316 | |
|
317 | 0 | if (!ossl_prov_is_running()) |
318 | 0 | return 0; |
319 | | |
320 | 0 | if (ctx == NULL) { |
321 | | /* Context must be set before final */ |
322 | 0 | return 0; |
323 | 0 | } |
324 | | |
325 | 0 | if (!ctx->is_ongoing) { |
326 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
327 | 0 | return 0; |
328 | 0 | } |
329 | | |
330 | 0 | if (ctx->direction == ENCRYPTION) { |
331 | 0 | uint8_t *ciphertext = out; |
332 | 0 | uint8_t *tag = ctx->tag; |
333 | 0 | size_t tag_len = ASCON_AEAD_TAG_LEN; |
334 | 0 | size_t ret; |
335 | |
|
336 | 0 | ret = ossl_ascon_aead128_encrypt_final((ASCON_AEAD_CTX *)ctx->internal_ctx, |
337 | 0 | ciphertext, tag, tag_len); |
338 | 0 | *outl = ret; |
339 | 0 | ctx->is_tag_set = 1; |
340 | 0 | ctx->is_ongoing = 0; /* finished: no further update/final on this IV */ |
341 | |
|
342 | 0 | return 1; |
343 | 0 | } else if (ctx->direction == DECRYPTION) { |
344 | 0 | uint8_t *plaintext = out; |
345 | 0 | int is_tag_valid = 0; |
346 | 0 | size_t ret; |
347 | |
|
348 | 0 | if (ctx->is_tag_set) { |
349 | 0 | const uint8_t *expected_tag = ctx->tag; |
350 | 0 | size_t expected_tag_len = ASCON_AEAD_TAG_LEN; |
351 | |
|
352 | 0 | ret = ossl_ascon_aead128_decrypt_final((ASCON_AEAD_CTX *)ctx->internal_ctx, |
353 | 0 | plaintext, &is_tag_valid, |
354 | 0 | expected_tag, |
355 | 0 | expected_tag_len); |
356 | 0 | ctx->is_ongoing = 0; /* finished, whatever the verdict */ |
357 | |
|
358 | 0 | if (is_tag_valid) { |
359 | 0 | *outl = ret; |
360 | 0 | return 1; |
361 | 0 | } |
362 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_BAD_DECRYPT); |
363 | 0 | return 0; |
364 | 0 | } else { |
365 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET); |
366 | 0 | return 0; |
367 | 0 | } |
368 | 0 | } |
369 | | |
370 | 0 | *outl = 0; |
371 | 0 | return 1; |
372 | 0 | } |
373 | | |
374 | | static int ascon_aead128_get_params(OSSL_PARAM params[]) |
375 | 1 | { |
376 | 1 | return ossl_cipher_generic_get_params(params, 0, ASCON_AEAD128_FLAGS, |
377 | 1 | ASCON_AEAD128_KEY_LEN * 8, |
378 | 1 | 1 * 8, |
379 | 1 | ASCON_AEAD_NONCE_LEN * 8); |
380 | 1 | } |
381 | | |
382 | | static const OSSL_PARAM *ascon_aead128_gettable_ctx_params(ossl_unused void *cctx, |
383 | | ossl_unused void *provctx) |
384 | 1 | { |
385 | 1 | return ascon_aead128_get_ctx_params_list; |
386 | 1 | } |
387 | | |
388 | | static int ascon_aead128_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
389 | 0 | { |
390 | 0 | struct ascon_aead128_ctx_st *ctx = vctx; |
391 | 0 | struct ascon_aead128_get_ctx_params_st p; |
392 | |
|
393 | 0 | if (ctx == NULL || !ascon_aead128_get_ctx_params_decoder(params, &p)) |
394 | 0 | return 0; |
395 | | |
396 | 0 | if (p.keylen != NULL |
397 | 0 | && !OSSL_PARAM_set_size_t(p.keylen, ASCON_AEAD128_KEY_LEN)) { |
398 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
399 | 0 | return 0; |
400 | 0 | } |
401 | | |
402 | 0 | if (p.ivlen != NULL |
403 | 0 | && !OSSL_PARAM_set_size_t(p.ivlen, ASCON_AEAD_NONCE_LEN)) { |
404 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
405 | 0 | return 0; |
406 | 0 | } |
407 | | |
408 | 0 | if (p.taglen != NULL |
409 | 0 | && !OSSL_PARAM_set_size_t(p.taglen, ASCON_AEAD_TAG_LEN)) { |
410 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
411 | 0 | return 0; |
412 | 0 | } |
413 | | |
414 | 0 | if (p.tag != NULL) { |
415 | | /* |
416 | | * reject the tag read in two cases: |
417 | | * - decrypting, a tag is an input to be verified, never an output |
418 | | * - encrypting, but final has not yet generated (set) the tag |
419 | | */ |
420 | 0 | if (ctx->direction != ENCRYPTION || !ctx->is_tag_set) { |
421 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET); |
422 | 0 | return 0; |
423 | 0 | } |
424 | 0 | if (!OSSL_PARAM_set_octet_string(p.tag, ctx->tag, ASCON_AEAD_TAG_LEN)) { |
425 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
426 | 0 | return 0; |
427 | 0 | } |
428 | 0 | } |
429 | | |
430 | 0 | if (p.upd_iv != NULL) { |
431 | 0 | if (!ctx->iv_set) { |
432 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
433 | 0 | return 0; |
434 | 0 | } |
435 | 0 | if (!OSSL_PARAM_set_octet_string(p.upd_iv, ctx->iv, ASCON_AEAD_NONCE_LEN)) { |
436 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
437 | 0 | return 0; |
438 | 0 | } |
439 | 0 | } |
440 | | |
441 | 0 | return 1; |
442 | 0 | } |
443 | | |
444 | | static const OSSL_PARAM *ascon_aead128_settable_ctx_params(ossl_unused void *cctx, |
445 | | ossl_unused void *provctx) |
446 | 0 | { |
447 | 0 | return ascon_aead128_set_ctx_params_list; |
448 | 0 | } |
449 | | |
450 | | static int ascon_aead128_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
451 | 0 | { |
452 | 0 | struct ascon_aead128_ctx_st *ctx = vctx; |
453 | 0 | struct ascon_aead128_set_ctx_params_st p; |
454 | |
|
455 | 0 | if (ctx == NULL || !ascon_aead128_set_ctx_params_decoder(params, &p)) |
456 | 0 | return 0; |
457 | | |
458 | 0 | if (p.taglen != NULL) { |
459 | 0 | size_t tag_len = 0; |
460 | |
|
461 | 0 | if (!OSSL_PARAM_get_size_t(p.taglen, &tag_len)) { |
462 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
463 | 0 | return 0; |
464 | 0 | } |
465 | 0 | if (tag_len != ASCON_AEAD_TAG_LEN) { |
466 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH); |
467 | 0 | return 0; |
468 | 0 | } |
469 | 0 | } |
470 | | |
471 | 0 | if (p.tag != NULL) { |
472 | | /* when data is NULL, this is a request to set tag length */ |
473 | 0 | if (p.tag->data == NULL) { |
474 | | /* |
475 | | * this implementation only supports fixed, full-length tags of |
476 | | * length ASCON_AEAD_TAG_LEN, so do nothing and return success |
477 | | */ |
478 | 0 | return 1; |
479 | 0 | } |
480 | | |
481 | | /* only take tags as input during decryption */ |
482 | 0 | if (ctx->direction == ENCRYPTION) { |
483 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_NEEDED); |
484 | 0 | return 0; |
485 | 0 | } |
486 | | |
487 | 0 | if (p.tag->data_type != OSSL_PARAM_OCTET_STRING) { |
488 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
489 | 0 | return 0; |
490 | 0 | } |
491 | | |
492 | | /* We only accept strictly 16-byte tags here */ |
493 | 0 | if (p.tag->data_size != ASCON_AEAD_TAG_LEN) { |
494 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH); |
495 | 0 | return 0; |
496 | 0 | } |
497 | 0 | memcpy(ctx->tag, p.tag->data, ASCON_AEAD_TAG_LEN); |
498 | 0 | ctx->is_tag_set = 1; |
499 | 0 | } |
500 | | |
501 | 0 | return 1; |
502 | 0 | } |
503 | | |
504 | | /* |
505 | | * One-shot cipher function for OSSL_FUNC_CIPHER_CIPHER |
506 | | * This function handles one-shot encryption/decryption operations. |
507 | | * Based on AES-SIV pattern: handles final (in == NULL), AAD (out == NULL), |
508 | | * and regular encryption/decryption operations. |
509 | | * For AEAD ciphers, CIPHER and UPDATE should behave the same way. |
510 | | */ |
511 | | static int ascon_aead128_cipher(void *vctx, unsigned char *out, size_t *outl, |
512 | | size_t outsize, const unsigned char *in, |
513 | | size_t inl) |
514 | 0 | { |
515 | 0 | struct ascon_aead128_ctx_st *ctx = vctx; |
516 | 0 | size_t update_outl = 0; |
517 | |
|
518 | 0 | if (!ossl_prov_is_running()) |
519 | 0 | return 0; |
520 | | |
521 | 0 | if (ctx == NULL) |
522 | 0 | return 0; |
523 | | |
524 | | /* Handle final operation (in == NULL) */ |
525 | 0 | if (in == NULL) { |
526 | 0 | size_t final_outl = 0; |
527 | |
|
528 | 0 | if (ascon_aead128_final(ctx, out, &final_outl, outsize) == 1) { |
529 | 0 | if (outl != NULL) |
530 | 0 | *outl = final_outl; |
531 | 0 | return 1; |
532 | 0 | } |
533 | 0 | return 0; |
534 | 0 | } |
535 | | |
536 | | /* Handle AAD operation (out == NULL) - process associated data */ |
537 | 0 | if (out == NULL) { |
538 | 0 | if (!ctx->is_ongoing) { |
539 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
540 | 0 | return 0; |
541 | 0 | } |
542 | 0 | if (ctx->assoc_data_not_allowed) { |
543 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_UPDATE_CALL_OUT_OF_ORDER); |
544 | 0 | return 0; |
545 | 0 | } |
546 | 0 | if (inl > 0) |
547 | 0 | ossl_ascon_aead128_assoc_data_update(ctx->internal_ctx, in, inl); |
548 | 0 | if (outl != NULL) |
549 | 0 | *outl = 0; |
550 | 0 | return 1; |
551 | 0 | } |
552 | | |
553 | | /* Check output buffer size */ |
554 | 0 | if (outsize < inl) { |
555 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
556 | 0 | return 0; |
557 | 0 | } |
558 | | |
559 | | /* Handle regular encryption/decryption (streaming update) */ |
560 | 0 | if (!ctx->is_ongoing) { |
561 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
562 | 0 | return 0; |
563 | 0 | } |
564 | | |
565 | | /* Use the same logic as update */ |
566 | 0 | if (ascon_aead128_update(ctx, out, &update_outl, outsize, in, inl) |
567 | 0 | == 1) { |
568 | 0 | if (outl != NULL) |
569 | 0 | *outl = update_outl; |
570 | 0 | return 1; |
571 | 0 | } |
572 | | |
573 | 0 | return 0; |
574 | 0 | } |
575 | | |
576 | | /********************************************************************* |
577 | | * |
578 | | * Setup |
579 | | * |
580 | | *****/ |
581 | | |
582 | | /* The dispatch table for ASCON-AEAD128 */ |
583 | | const OSSL_DISPATCH ossl_ascon_aead128_functions[] = { |
584 | | { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))ascon_aead128_newctx }, |
585 | | { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))ascon_aead128_freectx }, |
586 | | { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))ascon_aead128_dupctx }, |
587 | | { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ascon_aead128_einit }, |
588 | | { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ascon_aead128_dinit }, |
589 | | { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ascon_aead128_update }, |
590 | | { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ascon_aead128_final }, |
591 | | { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ascon_aead128_cipher }, |
592 | | { OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))ascon_aead128_get_params }, |
593 | | { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, (void (*)(void))ascon_aead128_gettable_params }, |
594 | | { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))ascon_aead128_get_ctx_params }, |
595 | | { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, (void (*)(void))ascon_aead128_gettable_ctx_params }, |
596 | | { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, (void (*)(void))ascon_aead128_set_ctx_params }, |
597 | | { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, (void (*)(void))ascon_aead128_settable_ctx_params }, |
598 | | OSSL_DISPATCH_END |
599 | | }; |