/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 | 0 | if (in == NULL && inl != 0) { |
233 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER); |
234 | 0 | return 0; |
235 | 0 | } |
236 | | |
237 | | /* Handle AAD operation (out == NULL) - process associated data */ |
238 | 0 | if (out == NULL) { |
239 | | /* Can only add AAD before encryption/decryption updates start */ |
240 | 0 | if (ctx->assoc_data_not_allowed) { |
241 | | /* AAD already processed or encryption started - cannot add more */ |
242 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_UPDATE_CALL_OUT_OF_ORDER); |
243 | 0 | return 0; |
244 | 0 | } |
245 | | |
246 | | /* Process AAD if provided */ |
247 | 0 | if (inl > 0 && in != NULL) { |
248 | 0 | if (ctx->internal_ctx == NULL) { |
249 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
250 | 0 | return 0; |
251 | 0 | } |
252 | 0 | ossl_ascon_aead128_assoc_data_update(ctx->internal_ctx, in, inl); |
253 | 0 | } |
254 | 0 | if (outl != NULL) |
255 | 0 | *outl = 0; |
256 | 0 | return 1; |
257 | 0 | } |
258 | | |
259 | 0 | if (outsize < inl) { |
260 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
261 | 0 | return 0; |
262 | 0 | } |
263 | 0 | if (inl > 0 && in == NULL) { |
264 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_INPUT_LENGTH); |
265 | 0 | return 0; |
266 | 0 | } |
267 | | |
268 | 0 | if (ctx->direction == ENCRYPTION) { |
269 | 0 | const uint8_t *plaintext = in; |
270 | 0 | size_t plaintext_len = inl; |
271 | 0 | uint8_t *ciphertext = out; |
272 | 0 | size_t ciphertext_len; |
273 | | |
274 | | /* Mark that we've started encryption - AAD cannot be added after this */ |
275 | | /* LibAscon encrypt_update will finalize AAD automatically if needed */ |
276 | 0 | ctx->assoc_data_not_allowed = 1; |
277 | |
|
278 | 0 | if (ctx->internal_ctx == NULL) { |
279 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
280 | 0 | return 0; |
281 | 0 | } |
282 | | |
283 | 0 | ciphertext_len = ossl_ascon_aead128_encrypt_update(ctx->internal_ctx, |
284 | 0 | ciphertext, |
285 | 0 | plaintext, |
286 | 0 | plaintext_len); |
287 | 0 | if (outl != NULL) |
288 | 0 | *outl = ciphertext_len; |
289 | 0 | return 1; |
290 | 0 | } else if (ctx->direction == DECRYPTION) { |
291 | 0 | uint8_t *plaintext = out; |
292 | 0 | size_t plaintext_len; |
293 | 0 | const uint8_t *ciphertext = in; |
294 | 0 | size_t ciphertext_len = inl; |
295 | | |
296 | | /* Mark that we've started decryption - AAD cannot be added after this */ |
297 | | /* LibAscon decrypt_update will finalize AAD automatically if needed */ |
298 | 0 | ctx->assoc_data_not_allowed = 1; |
299 | |
|
300 | 0 | if (ctx->internal_ctx == NULL) { |
301 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
302 | 0 | return 0; |
303 | 0 | } |
304 | | |
305 | 0 | plaintext_len = ossl_ascon_aead128_decrypt_update(ctx->internal_ctx, |
306 | 0 | plaintext, |
307 | 0 | ciphertext, |
308 | 0 | ciphertext_len); |
309 | 0 | if (outl != NULL) |
310 | 0 | *outl = plaintext_len; |
311 | 0 | return 1; |
312 | 0 | } |
313 | 0 | return 0; |
314 | 0 | } |
315 | | |
316 | | /* PROVIDER'S FINAL FUNCTION */ |
317 | | |
318 | | static int ascon_aead128_final(void *vctx, unsigned char *out, size_t *outl, size_t outsize) |
319 | 0 | { |
320 | 0 | struct ascon_aead128_ctx_st *ctx = vctx; |
321 | |
|
322 | 0 | if (!ossl_prov_is_running()) |
323 | 0 | return 0; |
324 | | |
325 | 0 | if (ctx == NULL) { |
326 | | /* Context must be set before final */ |
327 | 0 | return 0; |
328 | 0 | } |
329 | | |
330 | 0 | if (!ctx->is_ongoing) { |
331 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
332 | 0 | return 0; |
333 | 0 | } |
334 | | |
335 | 0 | if (ctx->direction == ENCRYPTION) { |
336 | 0 | uint8_t *ciphertext = out; |
337 | 0 | uint8_t *tag = ctx->tag; |
338 | 0 | size_t tag_len = ASCON_AEAD_TAG_LEN; |
339 | 0 | size_t ret; |
340 | |
|
341 | 0 | ret = ossl_ascon_aead128_encrypt_final((ASCON_AEAD_CTX *)ctx->internal_ctx, |
342 | 0 | ciphertext, tag, tag_len); |
343 | 0 | *outl = ret; |
344 | 0 | ctx->is_tag_set = 1; |
345 | 0 | ctx->is_ongoing = 0; /* finished: no further update/final on this IV */ |
346 | |
|
347 | 0 | return 1; |
348 | 0 | } else if (ctx->direction == DECRYPTION) { |
349 | 0 | uint8_t *plaintext = out; |
350 | 0 | int is_tag_valid = 0; |
351 | 0 | size_t ret; |
352 | |
|
353 | 0 | if (ctx->is_tag_set) { |
354 | 0 | const uint8_t *expected_tag = ctx->tag; |
355 | 0 | size_t expected_tag_len = ASCON_AEAD_TAG_LEN; |
356 | |
|
357 | 0 | ret = ossl_ascon_aead128_decrypt_final((ASCON_AEAD_CTX *)ctx->internal_ctx, |
358 | 0 | plaintext, &is_tag_valid, |
359 | 0 | expected_tag, |
360 | 0 | expected_tag_len); |
361 | 0 | ctx->is_ongoing = 0; /* finished, whatever the verdict */ |
362 | |
|
363 | 0 | if (is_tag_valid) { |
364 | 0 | *outl = ret; |
365 | 0 | return 1; |
366 | 0 | } |
367 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_BAD_DECRYPT); |
368 | 0 | return 0; |
369 | 0 | } else { |
370 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET); |
371 | 0 | return 0; |
372 | 0 | } |
373 | 0 | } |
374 | | |
375 | 0 | *outl = 0; |
376 | 0 | return 1; |
377 | 0 | } |
378 | | |
379 | | static int ascon_aead128_get_params(OSSL_PARAM params[]) |
380 | 1 | { |
381 | 1 | return ossl_cipher_generic_get_params(params, 0, ASCON_AEAD128_FLAGS, |
382 | 1 | ASCON_AEAD128_KEY_LEN * 8, |
383 | 1 | 1 * 8, |
384 | 1 | ASCON_AEAD_NONCE_LEN * 8); |
385 | 1 | } |
386 | | |
387 | | static const OSSL_PARAM *ascon_aead128_gettable_ctx_params(ossl_unused void *cctx, |
388 | | ossl_unused void *provctx) |
389 | 1 | { |
390 | 1 | return ascon_aead128_get_ctx_params_list; |
391 | 1 | } |
392 | | |
393 | | static int ascon_aead128_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
394 | 0 | { |
395 | 0 | struct ascon_aead128_ctx_st *ctx = vctx; |
396 | 0 | struct ascon_aead128_get_ctx_params_st p; |
397 | |
|
398 | 0 | if (ctx == NULL || !ascon_aead128_get_ctx_params_decoder(params, &p)) |
399 | 0 | return 0; |
400 | | |
401 | 0 | if (p.keylen != NULL |
402 | 0 | && !OSSL_PARAM_set_size_t(p.keylen, ASCON_AEAD128_KEY_LEN)) { |
403 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
404 | 0 | return 0; |
405 | 0 | } |
406 | | |
407 | 0 | if (p.ivlen != NULL |
408 | 0 | && !OSSL_PARAM_set_size_t(p.ivlen, ASCON_AEAD_NONCE_LEN)) { |
409 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
410 | 0 | return 0; |
411 | 0 | } |
412 | | |
413 | 0 | if (p.taglen != NULL |
414 | 0 | && !OSSL_PARAM_set_size_t(p.taglen, ASCON_AEAD_TAG_LEN)) { |
415 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
416 | 0 | return 0; |
417 | 0 | } |
418 | | |
419 | 0 | if (p.tag != NULL) { |
420 | | /* |
421 | | * reject the tag read in two cases: |
422 | | * - decrypting, a tag is an input to be verified, never an output |
423 | | * - encrypting, but final has not yet generated (set) the tag |
424 | | */ |
425 | 0 | if (ctx->direction != ENCRYPTION || !ctx->is_tag_set) { |
426 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET); |
427 | 0 | return 0; |
428 | 0 | } |
429 | 0 | if (!OSSL_PARAM_set_octet_string(p.tag, ctx->tag, ASCON_AEAD_TAG_LEN)) { |
430 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
431 | 0 | return 0; |
432 | 0 | } |
433 | 0 | } |
434 | | |
435 | 0 | if (p.upd_iv != NULL) { |
436 | 0 | if (!ctx->iv_set) { |
437 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
438 | 0 | return 0; |
439 | 0 | } |
440 | 0 | if (!OSSL_PARAM_set_octet_string(p.upd_iv, ctx->iv, ASCON_AEAD_NONCE_LEN)) { |
441 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
442 | 0 | return 0; |
443 | 0 | } |
444 | 0 | } |
445 | | |
446 | 0 | return 1; |
447 | 0 | } |
448 | | |
449 | | static const OSSL_PARAM *ascon_aead128_settable_ctx_params(ossl_unused void *cctx, |
450 | | ossl_unused void *provctx) |
451 | 0 | { |
452 | 0 | return ascon_aead128_set_ctx_params_list; |
453 | 0 | } |
454 | | |
455 | | static int ascon_aead128_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
456 | 0 | { |
457 | 0 | struct ascon_aead128_ctx_st *ctx = vctx; |
458 | 0 | struct ascon_aead128_set_ctx_params_st p; |
459 | |
|
460 | 0 | if (ctx == NULL || !ascon_aead128_set_ctx_params_decoder(params, &p)) |
461 | 0 | return 0; |
462 | | |
463 | 0 | if (p.taglen != NULL) { |
464 | 0 | size_t tag_len = 0; |
465 | |
|
466 | 0 | if (!OSSL_PARAM_get_size_t(p.taglen, &tag_len)) { |
467 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
468 | 0 | return 0; |
469 | 0 | } |
470 | 0 | if (tag_len != ASCON_AEAD_TAG_LEN) { |
471 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH); |
472 | 0 | return 0; |
473 | 0 | } |
474 | 0 | } |
475 | | |
476 | 0 | if (p.tag != NULL) { |
477 | | /* when data is NULL, this is a request to set tag length */ |
478 | 0 | if (p.tag->data == NULL) { |
479 | | /* |
480 | | * this implementation only supports fixed, full-length tags of |
481 | | * length ASCON_AEAD_TAG_LEN, so do nothing and return success |
482 | | */ |
483 | 0 | return 1; |
484 | 0 | } |
485 | | |
486 | | /* only take tags as input during decryption */ |
487 | 0 | if (ctx->direction == ENCRYPTION) { |
488 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_NEEDED); |
489 | 0 | return 0; |
490 | 0 | } |
491 | | |
492 | 0 | if (p.tag->data_type != OSSL_PARAM_OCTET_STRING) { |
493 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
494 | 0 | return 0; |
495 | 0 | } |
496 | | |
497 | | /* We only accept strictly 16-byte tags here */ |
498 | 0 | if (p.tag->data_size != ASCON_AEAD_TAG_LEN) { |
499 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH); |
500 | 0 | return 0; |
501 | 0 | } |
502 | 0 | memcpy(ctx->tag, p.tag->data, ASCON_AEAD_TAG_LEN); |
503 | 0 | ctx->is_tag_set = 1; |
504 | 0 | } |
505 | | |
506 | 0 | return 1; |
507 | 0 | } |
508 | | |
509 | | /* |
510 | | * One-shot cipher function for OSSL_FUNC_CIPHER_CIPHER |
511 | | * This function handles one-shot encryption/decryption operations. |
512 | | * Based on AES-SIV pattern: handles final (in == NULL), AAD (out == NULL), |
513 | | * and regular encryption/decryption operations. |
514 | | * For AEAD ciphers, CIPHER and UPDATE should behave the same way. |
515 | | */ |
516 | | static int ascon_aead128_cipher(void *vctx, unsigned char *out, size_t *outl, |
517 | | size_t outsize, const unsigned char *in, |
518 | | size_t inl) |
519 | 0 | { |
520 | 0 | struct ascon_aead128_ctx_st *ctx = vctx; |
521 | 0 | size_t update_outl = 0; |
522 | |
|
523 | 0 | if (!ossl_prov_is_running()) |
524 | 0 | return 0; |
525 | | |
526 | 0 | if (ctx == NULL) |
527 | 0 | return 0; |
528 | | |
529 | | /* Handle final operation (in == NULL) */ |
530 | 0 | if (in == NULL) { |
531 | 0 | size_t final_outl = 0; |
532 | |
|
533 | 0 | if (ascon_aead128_final(ctx, out, &final_outl, outsize) == 1) { |
534 | 0 | if (outl != NULL) |
535 | 0 | *outl = final_outl; |
536 | 0 | return 1; |
537 | 0 | } |
538 | 0 | return 0; |
539 | 0 | } |
540 | | |
541 | | /* Handle AAD operation (out == NULL) - process associated data */ |
542 | 0 | if (out == NULL) { |
543 | 0 | if (!ctx->is_ongoing) { |
544 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
545 | 0 | return 0; |
546 | 0 | } |
547 | 0 | if (ctx->assoc_data_not_allowed) { |
548 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_UPDATE_CALL_OUT_OF_ORDER); |
549 | 0 | return 0; |
550 | 0 | } |
551 | 0 | if (inl > 0) |
552 | 0 | ossl_ascon_aead128_assoc_data_update(ctx->internal_ctx, in, inl); |
553 | 0 | if (outl != NULL) |
554 | 0 | *outl = 0; |
555 | 0 | return 1; |
556 | 0 | } |
557 | | |
558 | | /* Check output buffer size */ |
559 | 0 | if (outsize < inl) { |
560 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
561 | 0 | return 0; |
562 | 0 | } |
563 | | |
564 | | /* Handle regular encryption/decryption (streaming update) */ |
565 | 0 | if (!ctx->is_ongoing) { |
566 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
567 | 0 | return 0; |
568 | 0 | } |
569 | | |
570 | | /* Use the same logic as update */ |
571 | 0 | if (ascon_aead128_update(ctx, out, &update_outl, outsize, in, inl) |
572 | 0 | == 1) { |
573 | 0 | if (outl != NULL) |
574 | 0 | *outl = update_outl; |
575 | 0 | return 1; |
576 | 0 | } |
577 | | |
578 | 0 | return 0; |
579 | 0 | } |
580 | | |
581 | | /********************************************************************* |
582 | | * |
583 | | * Setup |
584 | | * |
585 | | *****/ |
586 | | |
587 | | /* The dispatch table for ASCON-AEAD128 */ |
588 | | const OSSL_DISPATCH ossl_ascon_aead128_functions[] = { |
589 | | { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))ascon_aead128_newctx }, |
590 | | { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))ascon_aead128_freectx }, |
591 | | { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))ascon_aead128_dupctx }, |
592 | | { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ascon_aead128_einit }, |
593 | | { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ascon_aead128_dinit }, |
594 | | { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ascon_aead128_update }, |
595 | | { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ascon_aead128_final }, |
596 | | { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ascon_aead128_cipher }, |
597 | | { OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))ascon_aead128_get_params }, |
598 | | { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, (void (*)(void))ascon_aead128_gettable_params }, |
599 | | { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))ascon_aead128_get_ctx_params }, |
600 | | { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, (void (*)(void))ascon_aead128_gettable_ctx_params }, |
601 | | { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, (void (*)(void))ascon_aead128_set_ctx_params }, |
602 | | { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, (void (*)(void))ascon_aead128_settable_ctx_params }, |
603 | | OSSL_DISPATCH_END |
604 | | }; |