/src/openssl/providers/implementations/ciphers/cipher_aes_ocb.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 2019-2023 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  |  |  * AES low level APIs are deprecated for public use, but still ok for internal  | 
12  |  |  * use where we're using them to implement the higher level EVP interface, as is  | 
13  |  |  * the case here.  | 
14  |  |  */  | 
15  |  | #include "internal/deprecated.h"  | 
16  |  |  | 
17  |  | #include <openssl/proverr.h>  | 
18  |  | #include "cipher_aes_ocb.h"  | 
19  |  | #include "prov/providercommon.h"  | 
20  |  | #include "prov/ciphercommon_aead.h"  | 
21  |  | #include "prov/implementations.h"  | 
22  |  |  | 
23  |  | #define AES_OCB_FLAGS AEAD_FLAGS  | 
24  |  |  | 
25  | 0  | #define OCB_DEFAULT_TAG_LEN 16  | 
26  |  | #define OCB_DEFAULT_IV_LEN  12  | 
27  | 0  | #define OCB_MIN_IV_LEN      1  | 
28  | 0  | #define OCB_MAX_IV_LEN      15  | 
29  |  |  | 
30  |  | PROV_CIPHER_FUNC(int, ocb_cipher, (PROV_AES_OCB_CTX *ctx,  | 
31  |  |                                    const unsigned char *in, unsigned char *out,  | 
32  |  |                                    size_t nextblock));  | 
33  |  | /* forward declarations */  | 
34  |  | static OSSL_FUNC_cipher_encrypt_init_fn aes_ocb_einit;  | 
35  |  | static OSSL_FUNC_cipher_decrypt_init_fn aes_ocb_dinit;  | 
36  |  | static OSSL_FUNC_cipher_update_fn aes_ocb_block_update;  | 
37  |  | static OSSL_FUNC_cipher_final_fn aes_ocb_block_final;  | 
38  |  | static OSSL_FUNC_cipher_cipher_fn aes_ocb_cipher;  | 
39  |  | static OSSL_FUNC_cipher_freectx_fn aes_ocb_freectx;  | 
40  |  | static OSSL_FUNC_cipher_dupctx_fn aes_ocb_dupctx;  | 
41  |  | static OSSL_FUNC_cipher_get_ctx_params_fn aes_ocb_get_ctx_params;  | 
42  |  | static OSSL_FUNC_cipher_set_ctx_params_fn aes_ocb_set_ctx_params;  | 
43  |  | static OSSL_FUNC_cipher_gettable_ctx_params_fn cipher_ocb_gettable_ctx_params;  | 
44  |  | static OSSL_FUNC_cipher_settable_ctx_params_fn cipher_ocb_settable_ctx_params;  | 
45  |  |  | 
46  |  | /*  | 
47  |  |  * The following methods could be moved into PROV_AES_OCB_HW if  | 
48  |  |  * multiple hardware implementations are ever needed.  | 
49  |  |  */  | 
50  |  | static ossl_inline int aes_generic_ocb_setiv(PROV_AES_OCB_CTX *ctx,  | 
51  |  |                                              const unsigned char *iv,  | 
52  |  |                                              size_t ivlen, size_t taglen)  | 
53  | 0  | { | 
54  | 0  |     return (CRYPTO_ocb128_setiv(&ctx->ocb, iv, ivlen, taglen) == 1);  | 
55  | 0  | }  | 
56  |  |  | 
57  |  | static ossl_inline int aes_generic_ocb_setaad(PROV_AES_OCB_CTX *ctx,  | 
58  |  |                                               const unsigned char *aad,  | 
59  |  |                                               size_t alen)  | 
60  | 0  | { | 
61  | 0  |     return CRYPTO_ocb128_aad(&ctx->ocb, aad, alen) == 1;  | 
62  | 0  | }  | 
63  |  |  | 
64  |  | static ossl_inline int aes_generic_ocb_gettag(PROV_AES_OCB_CTX *ctx,  | 
65  |  |                                               unsigned char *tag, size_t tlen)  | 
66  | 0  | { | 
67  | 0  |     return CRYPTO_ocb128_tag(&ctx->ocb, tag, tlen) > 0;  | 
68  | 0  | }  | 
69  |  |  | 
70  |  | static ossl_inline int aes_generic_ocb_final(PROV_AES_OCB_CTX *ctx)  | 
71  | 0  | { | 
72  | 0  |     return (CRYPTO_ocb128_finish(&ctx->ocb, ctx->tag, ctx->taglen) == 0);  | 
73  | 0  | }  | 
74  |  |  | 
75  |  | static ossl_inline void aes_generic_ocb_cleanup(PROV_AES_OCB_CTX *ctx)  | 
76  | 0  | { | 
77  | 0  |     CRYPTO_ocb128_cleanup(&ctx->ocb);  | 
78  | 0  | }  | 
79  |  |  | 
80  |  | static ossl_inline int aes_generic_ocb_cipher(PROV_AES_OCB_CTX *ctx,  | 
81  |  |                                               const unsigned char *in,  | 
82  |  |                                               unsigned char *out, size_t len)  | 
83  | 0  | { | 
84  | 0  |     if (ctx->base.enc) { | 
85  | 0  |         if (!CRYPTO_ocb128_encrypt(&ctx->ocb, in, out, len))  | 
86  | 0  |             return 0;  | 
87  | 0  |     } else { | 
88  | 0  |         if (!CRYPTO_ocb128_decrypt(&ctx->ocb, in, out, len))  | 
89  | 0  |             return 0;  | 
90  | 0  |     }  | 
91  | 0  |     return 1;  | 
92  | 0  | }  | 
93  |  |  | 
94  |  | static ossl_inline int aes_generic_ocb_copy_ctx(PROV_AES_OCB_CTX *dst,  | 
95  |  |                                                 PROV_AES_OCB_CTX *src)  | 
96  | 0  | { | 
97  | 0  |     return CRYPTO_ocb128_copy_ctx(&dst->ocb, &src->ocb,  | 
98  | 0  |                                   &dst->ksenc.ks, &dst->ksdec.ks);  | 
99  | 0  | }  | 
100  |  |  | 
101  |  | /*-  | 
102  |  |  * Provider dispatch functions  | 
103  |  |  */  | 
104  |  | static int aes_ocb_init(void *vctx, const unsigned char *key, size_t keylen,  | 
105  |  |                         const unsigned char *iv, size_t ivlen,  | 
106  |  |                         const OSSL_PARAM params[], int enc)  | 
107  | 0  | { | 
108  | 0  |     PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;  | 
109  |  | 
  | 
110  | 0  |     if (!ossl_prov_is_running())  | 
111  | 0  |         return 0;  | 
112  |  |  | 
113  | 0  |     ctx->aad_buf_len = 0;  | 
114  | 0  |     ctx->data_buf_len = 0;  | 
115  | 0  |     ctx->base.enc = enc;  | 
116  |  | 
  | 
117  | 0  |     if (iv != NULL) { | 
118  | 0  |         if (ivlen != ctx->base.ivlen) { | 
119  |  |             /* IV len must be 1 to 15 */  | 
120  | 0  |             if (ivlen < OCB_MIN_IV_LEN || ivlen > OCB_MAX_IV_LEN) { | 
121  | 0  |                 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);  | 
122  | 0  |                 return 0;  | 
123  | 0  |             }  | 
124  | 0  |             ctx->base.ivlen = ivlen;  | 
125  | 0  |         }  | 
126  | 0  |         if (!ossl_cipher_generic_initiv(&ctx->base, iv, ivlen))  | 
127  | 0  |             return 0;  | 
128  | 0  |         ctx->iv_state = IV_STATE_BUFFERED;  | 
129  | 0  |     }  | 
130  | 0  |     if (key != NULL) { | 
131  | 0  |         if (keylen != ctx->base.keylen) { | 
132  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);  | 
133  | 0  |             return 0;  | 
134  | 0  |         }  | 
135  | 0  |         if (!ctx->base.hw->init(&ctx->base, key, keylen))  | 
136  | 0  |             return 0;  | 
137  | 0  |     }  | 
138  | 0  |     return aes_ocb_set_ctx_params(ctx, params);  | 
139  | 0  | }  | 
140  |  |  | 
141  |  | static int aes_ocb_einit(void *vctx, const unsigned char *key, size_t keylen,  | 
142  |  |                          const unsigned char *iv, size_t ivlen,  | 
143  |  |                          const OSSL_PARAM params[])  | 
144  | 0  | { | 
145  | 0  |     return aes_ocb_init(vctx, key, keylen, iv, ivlen, params, 1);  | 
146  | 0  | }  | 
147  |  |  | 
148  |  | static int aes_ocb_dinit(void *vctx, const unsigned char *key, size_t keylen,  | 
149  |  |                          const unsigned char *iv, size_t ivlen,  | 
150  |  |                          const OSSL_PARAM params[])  | 
151  | 0  | { | 
152  | 0  |     return aes_ocb_init(vctx, key, keylen, iv, ivlen, params, 0);  | 
153  | 0  | }  | 
154  |  |  | 
155  |  | /*  | 
156  |  |  * Because of the way OCB works, both the AAD and data are buffered in the  | 
157  |  |  * same way. Only the last block can be a partial block.  | 
158  |  |  */  | 
159  |  | static int aes_ocb_block_update_internal(PROV_AES_OCB_CTX *ctx,  | 
160  |  |                                          unsigned char *buf, size_t *bufsz,  | 
161  |  |                                          unsigned char *out, size_t *outl,  | 
162  |  |                                          size_t outsize, const unsigned char *in,  | 
163  |  |                                          size_t inl, OSSL_ocb_cipher_fn ciph)  | 
164  | 0  | { | 
165  | 0  |     size_t nextblocks;  | 
166  | 0  |     size_t outlint = 0;  | 
167  |  | 
  | 
168  | 0  |     if (*bufsz != 0)  | 
169  | 0  |         nextblocks = ossl_cipher_fillblock(buf, bufsz, AES_BLOCK_SIZE, &in, &inl);  | 
170  | 0  |     else  | 
171  | 0  |         nextblocks = inl & ~(AES_BLOCK_SIZE-1);  | 
172  |  | 
  | 
173  | 0  |     if (*bufsz == AES_BLOCK_SIZE) { | 
174  | 0  |         if (outsize < AES_BLOCK_SIZE) { | 
175  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);  | 
176  | 0  |             return 0;  | 
177  | 0  |         }  | 
178  | 0  |         if (!ciph(ctx, buf, out, AES_BLOCK_SIZE)) { | 
179  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);  | 
180  | 0  |             return 0;  | 
181  | 0  |         }  | 
182  | 0  |         *bufsz = 0;  | 
183  | 0  |         outlint = AES_BLOCK_SIZE;  | 
184  | 0  |         if (out != NULL)  | 
185  | 0  |             out += AES_BLOCK_SIZE;  | 
186  | 0  |     }  | 
187  | 0  |     if (nextblocks > 0) { | 
188  | 0  |         outlint += nextblocks;  | 
189  | 0  |         if (outsize < outlint) { | 
190  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);  | 
191  | 0  |             return 0;  | 
192  | 0  |         }  | 
193  | 0  |         if (!ciph(ctx, in, out, nextblocks)) { | 
194  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);  | 
195  | 0  |             return 0;  | 
196  | 0  |         }  | 
197  | 0  |         in += nextblocks;  | 
198  | 0  |         inl -= nextblocks;  | 
199  | 0  |     }  | 
200  | 0  |     if (inl != 0  | 
201  | 0  |         && !ossl_cipher_trailingdata(buf, bufsz, AES_BLOCK_SIZE, &in, &inl)) { | 
202  |  |         /* PROVerr already called */  | 
203  | 0  |         return 0;  | 
204  | 0  |     }  | 
205  |  |  | 
206  | 0  |     *outl = outlint;  | 
207  | 0  |     return inl == 0;  | 
208  | 0  | }  | 
209  |  |  | 
210  |  | /* A wrapper function that has the same signature as cipher */  | 
211  |  | static int cipher_updateaad(PROV_AES_OCB_CTX *ctx, const unsigned char *in,  | 
212  |  |                             unsigned char *out, size_t len)  | 
213  | 0  | { | 
214  | 0  |     return aes_generic_ocb_setaad(ctx, in, len);  | 
215  | 0  | }  | 
216  |  |  | 
217  |  | static int update_iv(PROV_AES_OCB_CTX *ctx)  | 
218  | 0  | { | 
219  | 0  |     if (ctx->iv_state == IV_STATE_FINISHED  | 
220  | 0  |         || ctx->iv_state == IV_STATE_UNINITIALISED)  | 
221  | 0  |         return 0;  | 
222  | 0  |     if (ctx->iv_state == IV_STATE_BUFFERED) { | 
223  | 0  |         if (!aes_generic_ocb_setiv(ctx, ctx->base.iv, ctx->base.ivlen,  | 
224  | 0  |                                    ctx->taglen))  | 
225  | 0  |             return 0;  | 
226  | 0  |         ctx->iv_state = IV_STATE_COPIED;  | 
227  | 0  |     }  | 
228  | 0  |     return 1;  | 
229  | 0  | }  | 
230  |  |  | 
231  |  | static int aes_ocb_block_update(void *vctx, unsigned char *out, size_t *outl,  | 
232  |  |                                 size_t outsize, const unsigned char *in,  | 
233  |  |                                 size_t inl)  | 
234  | 0  | { | 
235  | 0  |     PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;  | 
236  | 0  |     unsigned char *buf;  | 
237  | 0  |     size_t *buflen;  | 
238  | 0  |     OSSL_ocb_cipher_fn fn;  | 
239  |  | 
  | 
240  | 0  |     if (!ctx->key_set || !update_iv(ctx))  | 
241  | 0  |         return 0;  | 
242  |  |  | 
243  | 0  |     if (inl == 0) { | 
244  | 0  |         *outl = 0;  | 
245  | 0  |         return 1;  | 
246  | 0  |     }  | 
247  |  |  | 
248  |  |     /* Are we dealing with AAD or normal data here? */  | 
249  | 0  |     if (out == NULL) { | 
250  | 0  |         buf = ctx->aad_buf;  | 
251  | 0  |         buflen = &ctx->aad_buf_len;  | 
252  | 0  |         fn = cipher_updateaad;  | 
253  | 0  |     } else { | 
254  | 0  |         buf = ctx->data_buf;  | 
255  | 0  |         buflen = &ctx->data_buf_len;  | 
256  | 0  |         fn = aes_generic_ocb_cipher;  | 
257  | 0  |     }  | 
258  | 0  |     return aes_ocb_block_update_internal(ctx, buf, buflen, out, outl, outsize,  | 
259  | 0  |                                          in, inl, fn);  | 
260  | 0  | }  | 
261  |  |  | 
262  |  | static int aes_ocb_block_final(void *vctx, unsigned char *out, size_t *outl,  | 
263  |  |                                size_t outsize)  | 
264  | 0  | { | 
265  | 0  |     PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;  | 
266  |  | 
  | 
267  | 0  |     if (!ossl_prov_is_running())  | 
268  | 0  |         return 0;  | 
269  |  |  | 
270  |  |     /* If no block_update has run then the iv still needs to be set */  | 
271  | 0  |     if (!ctx->key_set || !update_iv(ctx))  | 
272  | 0  |         return 0;  | 
273  |  |  | 
274  |  |     /*  | 
275  |  |      * Empty the buffer of any partial block that we might have been provided,  | 
276  |  |      * both for data and AAD  | 
277  |  |      */  | 
278  | 0  |     *outl = 0;  | 
279  | 0  |     if (ctx->data_buf_len > 0) { | 
280  | 0  |         if (!aes_generic_ocb_cipher(ctx, ctx->data_buf, out, ctx->data_buf_len))  | 
281  | 0  |             return 0;  | 
282  | 0  |         *outl = ctx->data_buf_len;  | 
283  | 0  |         ctx->data_buf_len = 0;  | 
284  | 0  |     }  | 
285  | 0  |     if (ctx->aad_buf_len > 0) { | 
286  | 0  |         if (!aes_generic_ocb_setaad(ctx, ctx->aad_buf, ctx->aad_buf_len))  | 
287  | 0  |             return 0;  | 
288  | 0  |         ctx->aad_buf_len = 0;  | 
289  | 0  |     }  | 
290  | 0  |     if (ctx->base.enc) { | 
291  |  |         /* If encrypting then just get the tag */  | 
292  | 0  |         if (!aes_generic_ocb_gettag(ctx, ctx->tag, ctx->taglen))  | 
293  | 0  |             return 0;  | 
294  | 0  |     } else { | 
295  |  |         /* If decrypting then verify */  | 
296  | 0  |         if (ctx->taglen == 0)  | 
297  | 0  |             return 0;  | 
298  | 0  |         if (!aes_generic_ocb_final(ctx))  | 
299  | 0  |             return 0;  | 
300  | 0  |     }  | 
301  |  |     /* Don't reuse the IV */  | 
302  | 0  |     ctx->iv_state = IV_STATE_FINISHED;  | 
303  | 0  |     return 1;  | 
304  | 0  | }  | 
305  |  |  | 
306  |  | static void *aes_ocb_newctx(void *provctx, size_t kbits, size_t blkbits,  | 
307  |  |                             size_t ivbits, unsigned int mode, uint64_t flags)  | 
308  | 0  | { | 
309  | 0  |     PROV_AES_OCB_CTX *ctx;  | 
310  |  | 
  | 
311  | 0  |     if (!ossl_prov_is_running())  | 
312  | 0  |         return NULL;  | 
313  |  |  | 
314  | 0  |     ctx = OPENSSL_zalloc(sizeof(*ctx));  | 
315  | 0  |     if (ctx != NULL) { | 
316  | 0  |         ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits, mode, flags,  | 
317  | 0  |                                     ossl_prov_cipher_hw_aes_ocb(kbits), NULL);  | 
318  | 0  |         ctx->taglen = OCB_DEFAULT_TAG_LEN;  | 
319  | 0  |     }  | 
320  | 0  |     return ctx;  | 
321  | 0  | }  | 
322  |  |  | 
323  |  | static void aes_ocb_freectx(void *vctx)  | 
324  | 0  | { | 
325  | 0  |     PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;  | 
326  |  | 
  | 
327  | 0  |     if (ctx != NULL) { | 
328  | 0  |         aes_generic_ocb_cleanup(ctx);  | 
329  | 0  |         ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);  | 
330  | 0  |         OPENSSL_clear_free(ctx,  sizeof(*ctx));  | 
331  | 0  |     }  | 
332  | 0  | }  | 
333  |  |  | 
334  |  | static void *aes_ocb_dupctx(void *vctx)  | 
335  | 0  | { | 
336  | 0  |     PROV_AES_OCB_CTX *in = (PROV_AES_OCB_CTX *)vctx;  | 
337  | 0  |     PROV_AES_OCB_CTX *ret;  | 
338  |  | 
  | 
339  | 0  |     if (!ossl_prov_is_running())  | 
340  | 0  |         return NULL;  | 
341  |  |  | 
342  | 0  |     ret = OPENSSL_malloc(sizeof(*ret));  | 
343  | 0  |     if (ret == NULL)  | 
344  | 0  |         return NULL;  | 
345  | 0  |     *ret = *in;  | 
346  | 0  |     if (!aes_generic_ocb_copy_ctx(ret, in)) { | 
347  | 0  |         OPENSSL_free(ret);  | 
348  | 0  |         ret = NULL;  | 
349  | 0  |     }  | 
350  | 0  |     return ret;  | 
351  | 0  | }  | 
352  |  |  | 
353  |  | static int aes_ocb_set_ctx_params(void *vctx, const OSSL_PARAM params[])  | 
354  | 0  | { | 
355  | 0  |     PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;  | 
356  | 0  |     const OSSL_PARAM *p;  | 
357  | 0  |     size_t sz;  | 
358  |  | 
  | 
359  | 0  |     if (ossl_param_is_empty(params))  | 
360  | 0  |         return 1;  | 
361  |  |  | 
362  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG);  | 
363  | 0  |     if (p != NULL) { | 
364  | 0  |         if (p->data_type != OSSL_PARAM_OCTET_STRING) { | 
365  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);  | 
366  | 0  |             return 0;  | 
367  | 0  |         }  | 
368  | 0  |         if (p->data == NULL) { | 
369  |  |             /* Tag len must be 0 to 16 */  | 
370  | 0  |             if (p->data_size > OCB_MAX_TAG_LEN) { | 
371  | 0  |                 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH);  | 
372  | 0  |                 return 0;  | 
373  | 0  |             }  | 
374  | 0  |             ctx->taglen = p->data_size;  | 
375  | 0  |         } else { | 
376  | 0  |             if (ctx->base.enc) { | 
377  | 0  |                 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);  | 
378  | 0  |                 return 0;  | 
379  | 0  |             }  | 
380  | 0  |             if (p->data_size != ctx->taglen) { | 
381  | 0  |                 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH);  | 
382  | 0  |                 return 0;  | 
383  | 0  |             }  | 
384  | 0  |             memcpy(ctx->tag, p->data, p->data_size);  | 
385  | 0  |         }  | 
386  | 0  |      }  | 
387  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_IVLEN);  | 
388  | 0  |     if (p != NULL) { | 
389  | 0  |         if (!OSSL_PARAM_get_size_t(p, &sz)) { | 
390  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);  | 
391  | 0  |             return 0;  | 
392  | 0  |         }  | 
393  |  |         /* IV len must be 1 to 15 */  | 
394  | 0  |         if (sz < OCB_MIN_IV_LEN || sz > OCB_MAX_IV_LEN)  | 
395  | 0  |             return 0;  | 
396  | 0  |         if (ctx->base.ivlen != sz) { | 
397  | 0  |             ctx->base.ivlen = sz;  | 
398  | 0  |             ctx->iv_state = IV_STATE_UNINITIALISED;  | 
399  | 0  |         }  | 
400  | 0  |     }  | 
401  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);  | 
402  | 0  |     if (p != NULL) { | 
403  | 0  |         size_t keylen;  | 
404  |  | 
  | 
405  | 0  |         if (!OSSL_PARAM_get_size_t(p, &keylen)) { | 
406  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);  | 
407  | 0  |             return 0;  | 
408  | 0  |         }  | 
409  | 0  |         if (ctx->base.keylen != keylen) { | 
410  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);  | 
411  | 0  |             return 0;  | 
412  | 0  |         }  | 
413  | 0  |     }  | 
414  | 0  |     return 1;  | 
415  | 0  | }  | 
416  |  |  | 
417  |  | static int aes_ocb_get_ctx_params(void *vctx, OSSL_PARAM params[])  | 
418  | 0  | { | 
419  | 0  |     PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;  | 
420  | 0  |     OSSL_PARAM *p;  | 
421  |  | 
  | 
422  | 0  |     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);  | 
423  | 0  |     if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->base.ivlen)) { | 
424  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);  | 
425  | 0  |         return 0;  | 
426  | 0  |     }  | 
427  | 0  |     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);  | 
428  | 0  |     if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->base.keylen)) { | 
429  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);  | 
430  | 0  |         return 0;  | 
431  | 0  |     }  | 
432  | 0  |     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAGLEN);  | 
433  | 0  |     if (p != NULL) { | 
434  | 0  |         if (!OSSL_PARAM_set_size_t(p, ctx->taglen)) { | 
435  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);  | 
436  | 0  |             return 0;  | 
437  | 0  |         }  | 
438  | 0  |     }  | 
439  |  |  | 
440  | 0  |     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IV);  | 
441  | 0  |     if (p != NULL) { | 
442  | 0  |         if (ctx->base.ivlen > p->data_size) { | 
443  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);  | 
444  | 0  |             return 0;  | 
445  | 0  |         }  | 
446  | 0  |         if (!OSSL_PARAM_set_octet_string_or_ptr(p, ctx->base.oiv, ctx->base.ivlen)) { | 
447  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);  | 
448  | 0  |             return 0;  | 
449  | 0  |         }  | 
450  | 0  |     }  | 
451  | 0  |     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_UPDATED_IV);  | 
452  | 0  |     if (p != NULL) { | 
453  | 0  |         if (ctx->base.ivlen > p->data_size) { | 
454  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);  | 
455  | 0  |             return 0;  | 
456  | 0  |         }  | 
457  | 0  |         if (!OSSL_PARAM_set_octet_string_or_ptr(p, ctx->base.iv, ctx->base.ivlen)) { | 
458  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);  | 
459  | 0  |             return 0;  | 
460  | 0  |         }  | 
461  | 0  |     }  | 
462  | 0  |     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG);  | 
463  | 0  |     if (p != NULL) { | 
464  | 0  |         if (p->data_type != OSSL_PARAM_OCTET_STRING) { | 
465  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);  | 
466  | 0  |             return 0;  | 
467  | 0  |         }  | 
468  | 0  |         if (!ctx->base.enc || p->data_size != ctx->taglen) { | 
469  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH);  | 
470  | 0  |             return 0;  | 
471  | 0  |         }  | 
472  | 0  |         memcpy(p->data, ctx->tag, ctx->taglen);  | 
473  | 0  |     }  | 
474  | 0  |     return 1;  | 
475  | 0  | }  | 
476  |  |  | 
477  |  | static const OSSL_PARAM cipher_ocb_known_gettable_ctx_params[] = { | 
478  |  |     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),  | 
479  |  |     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),  | 
480  |  |     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, NULL),  | 
481  |  |     OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_IV, NULL, 0),  | 
482  |  |     OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, NULL, 0),  | 
483  |  |     OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0),  | 
484  |  |     OSSL_PARAM_END  | 
485  |  | };  | 
486  |  | static const OSSL_PARAM *cipher_ocb_gettable_ctx_params(ossl_unused void *cctx,  | 
487  |  |                                                         ossl_unused void *p_ctx)  | 
488  | 3  | { | 
489  | 3  |     return cipher_ocb_known_gettable_ctx_params;  | 
490  | 3  | }  | 
491  |  |  | 
492  |  | static const OSSL_PARAM cipher_ocb_known_settable_ctx_params[] = { | 
493  |  |     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),  | 
494  |  |     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN, NULL),  | 
495  |  |     OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0),  | 
496  |  |     OSSL_PARAM_END  | 
497  |  | };  | 
498  |  | static const OSSL_PARAM *cipher_ocb_settable_ctx_params(ossl_unused void *cctx,  | 
499  |  |                                                         ossl_unused void *p_ctx)  | 
500  | 0  | { | 
501  | 0  |     return cipher_ocb_known_settable_ctx_params;  | 
502  | 0  | }  | 
503  |  |  | 
504  |  | static int aes_ocb_cipher(void *vctx, unsigned char *out, size_t *outl,  | 
505  |  |                           size_t outsize, const unsigned char *in, size_t inl)  | 
506  | 0  | { | 
507  | 0  |     PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;  | 
508  |  | 
  | 
509  | 0  |     if (!ossl_prov_is_running())  | 
510  | 0  |         return 0;  | 
511  |  |  | 
512  | 0  |     if (outsize < inl) { | 
513  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);  | 
514  | 0  |         return 0;  | 
515  | 0  |     }  | 
516  |  |  | 
517  | 0  |     if (!aes_generic_ocb_cipher(ctx, in, out, inl)) { | 
518  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);  | 
519  | 0  |         return 0;  | 
520  | 0  |     }  | 
521  |  |  | 
522  | 0  |     *outl = inl;  | 
523  | 0  |     return 1;  | 
524  | 0  | }  | 
525  |  |  | 
526  |  | #define IMPLEMENT_cipher(mode, UCMODE, flags, kbits, blkbits, ivbits)          \  | 
527  |  | static OSSL_FUNC_cipher_get_params_fn aes_##kbits##_##mode##_get_params;       \  | 
528  | 3  | static int aes_##kbits##_##mode##_get_params(OSSL_PARAM params[])              \  | 
529  | 3  | {                                                                              \ | 
530  | 3  |     return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,    \  | 
531  | 3  |                                           flags, kbits, blkbits, ivbits);      \  | 
532  | 3  | }                                                                              \ cipher_aes_ocb.c:aes_256_ocb_get_params Line  | Count  | Source  |  528  | 1  | static int aes_##kbits##_##mode##_get_params(OSSL_PARAM params[])              \  |  529  | 1  | {                                                                              \ |  530  | 1  |     return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,    \  |  531  | 1  |                                           flags, kbits, blkbits, ivbits);      \  |  532  | 1  | }                                                                              \  |  
 cipher_aes_ocb.c:aes_192_ocb_get_params Line  | Count  | Source  |  528  | 1  | static int aes_##kbits##_##mode##_get_params(OSSL_PARAM params[])              \  |  529  | 1  | {                                                                              \ |  530  | 1  |     return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,    \  |  531  | 1  |                                           flags, kbits, blkbits, ivbits);      \  |  532  | 1  | }                                                                              \  |  
 cipher_aes_ocb.c:aes_128_ocb_get_params Line  | Count  | Source  |  528  | 1  | static int aes_##kbits##_##mode##_get_params(OSSL_PARAM params[])              \  |  529  | 1  | {                                                                              \ |  530  | 1  |     return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,    \  |  531  | 1  |                                           flags, kbits, blkbits, ivbits);      \  |  532  | 1  | }                                                                              \  |  
  | 
533  |  | static OSSL_FUNC_cipher_newctx_fn aes_##kbits##_##mode##_newctx;               \  | 
534  | 0  | static void *aes_##kbits##_##mode##_newctx(void *provctx)                      \  | 
535  | 0  | {                                                                              \ | 
536  | 0  |     return aes_##mode##_newctx(provctx, kbits, blkbits, ivbits,                \  | 
537  | 0  |                                EVP_CIPH_##UCMODE##_MODE, flags);               \  | 
538  | 0  | }                                                                              \ Unexecuted instantiation: cipher_aes_ocb.c:aes_256_ocb_newctx Unexecuted instantiation: cipher_aes_ocb.c:aes_192_ocb_newctx Unexecuted instantiation: cipher_aes_ocb.c:aes_128_ocb_newctx  | 
539  |  | const OSSL_DISPATCH ossl_##aes##kbits##mode##_functions[] = {                  \ | 
540  |  |     { OSSL_FUNC_CIPHER_NEWCTX,                                                 \ | 
541  |  |         (void (*)(void))aes_##kbits##_##mode##_newctx },                       \  | 
542  |  |     { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_##mode##_einit },     \ | 
543  |  |     { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_##mode##_dinit },     \ | 
544  |  |     { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_##mode##_block_update },    \ | 
545  |  |     { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_##mode##_block_final },      \ | 
546  |  |     { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))aes_ocb_cipher },               \ | 
547  |  |     { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_##mode##_freectx },        \ | 
548  |  |     { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_##mode##_dupctx },          \ | 
549  |  |     { OSSL_FUNC_CIPHER_GET_PARAMS,                                             \ | 
550  |  |         (void (*)(void))aes_##kbits##_##mode##_get_params },                   \  | 
551  |  |     { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                         \ | 
552  |  |         (void (*)(void))aes_##mode##_get_ctx_params },                         \  | 
553  |  |     { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                         \ | 
554  |  |         (void (*)(void))aes_##mode##_set_ctx_params },                         \  | 
555  |  |     { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                        \ | 
556  |  |         (void (*)(void))ossl_cipher_generic_gettable_params },                 \  | 
557  |  |     { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                    \ | 
558  |  |         (void (*)(void))cipher_ocb_gettable_ctx_params },                      \  | 
559  |  |     { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                    \ | 
560  |  |         (void (*)(void))cipher_ocb_settable_ctx_params },                      \  | 
561  |  |     OSSL_DISPATCH_END                                                          \  | 
562  |  | }  | 
563  |  |  | 
564  |  | IMPLEMENT_cipher(ocb, OCB, AES_OCB_FLAGS, 256, 128, OCB_DEFAULT_IV_LEN * 8);  | 
565  |  | IMPLEMENT_cipher(ocb, OCB, AES_OCB_FLAGS, 192, 128, OCB_DEFAULT_IV_LEN * 8);  | 
566  |  | IMPLEMENT_cipher(ocb, OCB, AES_OCB_FLAGS, 128, 128, OCB_DEFAULT_IV_LEN * 8);  |