xref: /freebsd/crypto/openssl/providers/implementations/include/prov/ciphercommon_ccm.h (revision 95eb4b873b6a8b527c5bd78d7191975dfca38998)
1 /*
2  * Copyright 2019-2021 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 "ciphercommon_aead.h"
11 
12 typedef struct prov_ccm_hw_st PROV_CCM_HW;
13 
14 #if defined(OPENSSL_CPUID_OBJ) && defined(__s390__)
15 /*-
16  * KMAC-AES parameter block - begin
17  * (see z/Architecture Principles of Operation >= SA22-7832-08)
18  */
19 typedef struct S390X_kmac_params_st {
20     union {
21         unsigned long long g[2];
22         unsigned char b[16];
23     } icv;
24     unsigned char k[32];
25 } S390X_KMAC_PARAMS;
26 /* KMAC-AES parameter block - end */
27 #endif
28 
29 /* Base structure that is shared by AES & ARIA for CCM MODE */
30 typedef struct prov_ccm_st {
31     unsigned int enc : 1;
32     unsigned int key_set : 1;  /* Set if key initialised */
33     unsigned int iv_set : 1;   /* Set if an iv is set */
34     unsigned int tag_set : 1;  /* Set if tag is valid */
35     unsigned int len_set : 1;  /* Set if message length set */
36     size_t l, m;               /* L and M parameters from RFC3610 */
37     size_t keylen;
38     size_t tls_aad_len;        /* TLS AAD length */
39     size_t tls_aad_pad_sz;
40     unsigned char iv[GENERIC_BLOCK_SIZE];
41     unsigned char buf[GENERIC_BLOCK_SIZE];
42     CCM128_CONTEXT ccm_ctx;
43     ccm128_f str;
44     const PROV_CCM_HW *hw;     /* hardware specific methods  */
45 } PROV_CCM_CTX;
46 
47 PROV_CIPHER_FUNC(int, CCM_cipher, (PROV_CCM_CTX *ctx, unsigned char *out,      \
48                                    size_t *padlen, const unsigned char *in,    \
49                                    size_t len));
50 PROV_CIPHER_FUNC(int, CCM_setkey, (PROV_CCM_CTX *ctx,                          \
51                                    const unsigned char *key, size_t keylen));
52 PROV_CIPHER_FUNC(int, CCM_setiv, (PROV_CCM_CTX *dat,                           \
53                                   const unsigned char *iv, size_t ivlen,       \
54                                   size_t mlen));
55 PROV_CIPHER_FUNC(int, CCM_setaad, (PROV_CCM_CTX *ctx,                          \
56                                    const unsigned char *aad, size_t aadlen));
57 PROV_CIPHER_FUNC(int, CCM_auth_encrypt, (PROV_CCM_CTX *ctx,                    \
58                                          const unsigned char *in,              \
59                                          unsigned char *out, size_t len,       \
60                                          unsigned char *tag, size_t taglen));
61 PROV_CIPHER_FUNC(int, CCM_auth_decrypt, (PROV_CCM_CTX *ctx,                    \
62                                          const unsigned char *in,              \
63                                          unsigned char *out, size_t len,       \
64                                          unsigned char *tag, size_t taglen));
65 PROV_CIPHER_FUNC(int, CCM_gettag, (PROV_CCM_CTX *ctx,                          \
66                                    unsigned char *tag,  size_t taglen));
67 
68 /*
69  * CCM Mode internal method table used to handle hardware specific differences,
70  * (and different algorithms).
71  */
72 struct prov_ccm_hw_st {
73     OSSL_CCM_setkey_fn setkey;
74     OSSL_CCM_setiv_fn setiv;
75     OSSL_CCM_setaad_fn setaad;
76     OSSL_CCM_auth_encrypt_fn auth_encrypt;
77     OSSL_CCM_auth_decrypt_fn auth_decrypt;
78     OSSL_CCM_gettag_fn gettag;
79 };
80 
81 OSSL_FUNC_cipher_encrypt_init_fn ossl_ccm_einit;
82 OSSL_FUNC_cipher_decrypt_init_fn ossl_ccm_dinit;
83 OSSL_FUNC_cipher_get_ctx_params_fn ossl_ccm_get_ctx_params;
84 OSSL_FUNC_cipher_set_ctx_params_fn ossl_ccm_set_ctx_params;
85 OSSL_FUNC_cipher_update_fn ossl_ccm_stream_update;
86 OSSL_FUNC_cipher_final_fn ossl_ccm_stream_final;
87 OSSL_FUNC_cipher_cipher_fn ossl_ccm_cipher;
88 void ossl_ccm_initctx(PROV_CCM_CTX *ctx, size_t keybits, const PROV_CCM_HW *hw);
89 
90 int ossl_ccm_generic_setiv(PROV_CCM_CTX *ctx, const unsigned char *nonce,
91                            size_t nlen, size_t mlen);
92 int ossl_ccm_generic_setaad(PROV_CCM_CTX *ctx, const unsigned char *aad,
93                             size_t alen);
94 int ossl_ccm_generic_gettag(PROV_CCM_CTX *ctx, unsigned char *tag, size_t tlen);
95 int ossl_ccm_generic_auth_encrypt(PROV_CCM_CTX *ctx, const unsigned char *in,
96                                   unsigned char *out, size_t len,
97                                   unsigned char *tag, size_t taglen);
98 int ossl_ccm_generic_auth_decrypt(PROV_CCM_CTX *ctx, const unsigned char *in,
99                                   unsigned char *out, size_t len,
100                                   unsigned char *expected_tag, size_t taglen);
101