1 /*
2  * Copyright (c) 2023 Eriptic Technologies
3  *
4  * SPDX-License-Identifier: Apache-2.0 or MIT
5  */
6 
7 #ifndef BUFFER_SIZES_H
8 #define BUFFER_SIZES_H
9 
10 #ifndef EAD_SIZE
11 #define EAD_SIZE 0
12 #endif
13 
14 #ifndef C_I_SIZE
15 #define C_I_SIZE 10
16 #endif
17 
18 #ifndef C_R_SIZE
19 #define C_R_SIZE 10
20 #endif
21 
22 #ifndef ID_CRED_I_SIZE
23 #define ID_CRED_I_SIZE 400
24 #endif
25 
26 #ifndef ID_CRED_R_SIZE
27 #define ID_CRED_R_SIZE 400
28 #endif
29 
30 #ifndef CRED_I_SIZE
31 #define CRED_I_SIZE 400
32 #endif
33 
34 #ifndef CRED_R_SIZE
35 #define CRED_R_SIZE 400
36 #endif
37 
38 #ifndef SUITES_I_SIZE
39 #define SUITES_I_SIZE 6
40 #endif
41 
42 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
43 
44 #define BSTR_ENCODING_OVERHEAD(x)                                              \
45 	(((x) <= 5) ? 1 : ((x) <= UINT8_MAX) ? 2 : ((x) <= UINT16_MAX) ? 3 : 5)
46 
47 #define AS_BSTR_SIZE(x) (BSTR_ENCODING_OVERHEAD(x) + x)
48 
49 #define P_256_PRIV_KEY_SIZE 32
50 #define P_256_PUB_KEY_COMPRESSED_SIZE 33
51 #define P_256_PUB_KEY_UNCOMPRESSED_SIZE 65
52 #define P_256_PUB_KEY_X_CORD_SIZE 32
53 #define PK_SIZE P_256_PUB_KEY_UNCOMPRESSED_SIZE
54 #define G_Y_SIZE P_256_PUB_KEY_X_CORD_SIZE
55 #define G_X_SIZE P_256_PUB_KEY_X_CORD_SIZE
56 #define G_R_SIZE P_256_PUB_KEY_UNCOMPRESSED_SIZE
57 #define G_I_SIZE P_256_PUB_KEY_UNCOMPRESSED_SIZE
58 #define SIGNATURE_SIZE 64
59 #define ECDH_SECRET_SIZE 32
60 #define PRK_SIZE 32
61 #define HASH_SIZE 32
62 #define AEAD_IV_SIZE 13
63 #define MAC_SIZE 16
64 #define MAC23_SIZE 32
65 #define AAD_SIZE 45
66 #define KID_SIZE 8
67 #define SIG_OR_MAC_SIZE 64
68 #define COSE_SIGN1_STR_LEN 10 /* The length of the string "Signature1" */
69 #define COSE_ENC0_STR_LEN 8 /* The length of the string "Encrypt0"   */
70 #define CBOR_ENCODED_UINT 2
71 #define CBOR_ARRAY_4_ELEMENTS_OVERHEAD 1
72 
73 #define PLAINTEXT2_SIZE                                                        \
74 	(AS_BSTR_SIZE(C_R_SIZE) + ID_CRED_I_SIZE +                             \
75 	 AS_BSTR_SIZE(SIG_OR_MAC_SIZE) + EAD_SIZE)
76 #define CIPHERTEXT2_SIZE PLAINTEXT2_SIZE
77 #define G_Y_CIPHERTEXT_2 (G_Y_SIZE + CIPHERTEXT2_SIZE)
78 
79 #define PLAINTEXT3_SIZE                                                        \
80 	(ID_CRED_I_SIZE + AS_BSTR_SIZE(SIG_OR_MAC_SIZE) + EAD_SIZE)
81 
82 #define CIPHERTEXT3_SIZE PLAINTEXT3_SIZE + MAC_SIZE
83 
84 #define PLAINTEXT4_SIZE EAD_SIZE + COSE_ENC0_STR_LEN
85 #define CIPHERTEXT4_SIZE PLAINTEXT4_SIZE
86 
87 #define MSG_1_SIZE                                                             \
88 	(1 + SUITES_I_SIZE + G_X_SIZE + AS_BSTR_SIZE(C_I_SIZE) + EAD_SIZE)
89 #define MSG_2_SIZE (G_Y_SIZE + CIPHERTEXT2_SIZE + AS_BSTR_SIZE(C_R_SIZE))
90 
91 #define MSG_3_SIZE AS_BSTR_SIZE(CIPHERTEXT3_SIZE)
92 #define MSG_4_SIZE AS_BSTR_SIZE(CIPHERTEXT4_SIZE)
93 
94 #define MSG12_MAX MAX(MSG_1_SIZE, MSG_2_SIZE)
95 #define MSG34_MAX MAX(MSG_3_SIZE, MSG_4_SIZE)
96 #define MSG_MAX_SIZE MAX(MSG12_MAX, MSG34_MAX)
97 #define PLAINTEXT23_MAX_SIZE MAX(PLAINTEXT2_SIZE, PLAINTEXT3_SIZE)
98 #define CRED_MAX_SIZE MAX(CRED_R_SIZE, CRED_I_SIZE)
99 #define ID_CRED_MAX_SIZE MAX(ID_CRED_R_SIZE, ID_CRED_I_SIZE)
100 
101 #define SIG_STRUCT_SIZE_CALC(context, protected, external_aad, payload)        \
102 	AS_BSTR_SIZE(context) + AS_BSTR_SIZE(protected) +                      \
103 		AS_BSTR_SIZE(external_aad) + AS_BSTR_SIZE(payload) +           \
104 		CBOR_ARRAY_4_ELEMENTS_OVERHEAD
105 
106 #define SIG_STRUCT_SIZE                                                        \
107 	SIG_STRUCT_SIZE_CALC(                                                  \
108 		COSE_SIGN1_STR_LEN, ID_CRED_MAX_SIZE,                          \
109 		(AS_BSTR_SIZE(HASH_SIZE) + CRED_MAX_SIZE + EAD_SIZE),          \
110 		MAC23_SIZE)
111 
112 
113 #define CONTEXT_MAC_SIZE                                                       \
114 	AS_BSTR_SIZE(AS_BSTR_SIZE(C_R_SIZE) + AS_BSTR_SIZE(HASH_SIZE) +        \
115 		     ID_CRED_MAX_SIZE + CRED_MAX_SIZE + EAD_SIZE)
116 
117 #define INFO_MAX_SIZE CONTEXT_MAC_SIZE + 2 * CBOR_ENCODED_UINT
118 
119 #define TH34_INPUT_SIZE                                                        \
120 	(AS_BSTR_SIZE(HASH_SIZE) + PLAINTEXT23_MAX_SIZE + CRED_MAX_SIZE)
121 
122 #define TH2_INPUT_SIZE (AS_BSTR_SIZE(G_Y_SIZE) + AS_BSTR_SIZE(HASH_SIZE))
123 
124 #endif
125