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#include "ciphercommon_aead.h"
11
12typedef 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 */
19typedef 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 */
30typedef 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
47PROV_CIPHER_FUNC(int, CCM_cipher, (PROV_CCM_CTX *ctx, unsigned char *out, \
48 size_t *padlen, const unsigned char *in, \
49 size_t len));
50PROV_CIPHER_FUNC(int, CCM_setkey, (PROV_CCM_CTX *ctx, \
51 const unsigned char *key, size_t keylen));
52PROV_CIPHER_FUNC(int, CCM_setiv, (PROV_CCM_CTX *dat, \
53 const unsigned char *iv, size_t ivlen, \
54 size_t mlen));
55PROV_CIPHER_FUNC(int, CCM_setaad, (PROV_CCM_CTX *ctx, \
56 const unsigned char *aad, size_t aadlen));
57PROV_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));
61PROV_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));
65PROV_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 */
72struct 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
81OSSL_OP_cipher_encrypt_init_fn ccm_einit;
82OSSL_OP_cipher_decrypt_init_fn ccm_dinit;
83OSSL_OP_cipher_get_ctx_params_fn ccm_get_ctx_params;
84OSSL_OP_cipher_set_ctx_params_fn ccm_set_ctx_params;
85OSSL_OP_cipher_update_fn ccm_stream_update;
86OSSL_OP_cipher_final_fn ccm_stream_final;
87OSSL_OP_cipher_cipher_fn ccm_cipher;
88void ccm_initctx(PROV_CCM_CTX *ctx, size_t keybits, const PROV_CCM_HW *hw);
89
90int ccm_generic_setiv(PROV_CCM_CTX *ctx, const unsigned char *nonce,
91 size_t nlen, size_t mlen);
92int ccm_generic_setaad(PROV_CCM_CTX *ctx, const unsigned char *aad, size_t alen);
93int ccm_generic_gettag(PROV_CCM_CTX *ctx, unsigned char *tag, size_t tlen);
94int ccm_generic_auth_encrypt(PROV_CCM_CTX *ctx, const unsigned char *in,
95 unsigned char *out, size_t len,
96 unsigned char *tag, size_t taglen);
97int ccm_generic_auth_decrypt(PROV_CCM_CTX *ctx, const unsigned char *in,
98 unsigned char *out, size_t len,
99 unsigned char *expected_tag, size_t taglen);
100