1 /*
2    Copyright (c) 2021 Fraunhofer AISEC. See the COPYRIGHT
3    file at the top-level directory of this distribution.
4 
5    Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6    http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7    <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8    option. This file may not be copied, modified, or distributed
9    except according to those terms.
10 */
11 #ifndef SIGNATURE_OR_MAC_MSG_H
12 #define SIGNATURE_OR_MAC_MSG_H
13 
14 #include <stdbool.h>
15 
16 #include "suites.h"
17 
18 #include "common/oscore_edhoc_error.h"
19 
20 enum sgn_or_mac_op { VERIFY, GENERATE };
21 
22 /**
23  * @brief   Encodes an array of data to cbor byte string
24  * @param   in pointer to data to be encoded
25  * @param   in_len length of in
26  * @param   out pointer to the output buffer
27  * @param   out_len length of out
28  * @retval  edhoc error code
29  */
30 enum err encode_byte_string(const uint8_t *in, uint32_t in_len, uint8_t *out,
31 			    uint32_t *out_len);
32 
33 enum err decode_byte_string(const uint8_t *in, const uint32_t in_len,
34 			    uint8_t *out, uint32_t *out_len);
35 
36 /**
37  * @brief   If the calling party (initiator / responder) authenticates with
38  *          static DH keys it calculates the MAC. Otherwise it calculates a
39  *          message to be signed.
40  * @param   static_dh_auth true if the caller of this fuction authenticates
41  *          with static DH keys
42  * @param   suite the cipher suite used
43  * @param   label_k label to be used in the key derivation
44  * @param   label_iv label to be used in the iv derivation
45  * @param   prk pseudo random key to be used in key iv derivation
46  * @param   prk_len length of prk
47  * @param   th transcript hash
48  * @param   th_len length of th
49  * @param   id_cred ID_CRED of the calling party
50  * @param   id_cred_len length of id_cred
51  * @param   cred CRED of the calling party
52  * @param   cred_len length of cred
53  * @param   ad aditionall data
54  * @param   ad_len length of ad
55  * @param   m message to be signed
56  * @param   m_len length of m
57  * @param   mac MAC_2/MAC_3 when the calling party uses static DH authentication
58  * @param   m_len length of mac
59  */
60 enum err signature_or_mac_msg_create(
61 	bool static_dh_auth, struct suite suite, const char *label_k,
62 	const char *label_iv, const uint8_t *prk, const uint8_t prk_len,
63 	const uint8_t *th, const uint8_t th_len, const uint8_t *id_cred,
64 	const uint8_t id_cred_len, const uint8_t *cred, const uint16_t cred_len,
65 	const uint8_t *ad, const uint8_t ad_len, uint8_t *m, uint16_t *m_len,
66 	uint8_t *mac, uint8_t *mac_len);
67 
68 enum err mac(const uint8_t *prk, uint32_t prk_len, const uint8_t *th,
69 	     uint32_t th_len, const uint8_t *id_cred, uint32_t id_cred_len,
70 	     const uint8_t *cred, uint32_t cred_len, const uint8_t *ead,
71 	     uint32_t ead_len, const char *mac_label, bool static_dh,
72 	     struct suite *suite, uint8_t *mac, uint32_t *mac_len);
73 
74 enum err
75 signature_or_mac(enum sgn_or_mac_op op, bool static_dh, struct suite *suite,
76 		 const uint8_t *sk, uint32_t sk_len, const uint8_t *pk,
77 		 uint32_t pk_len, const uint8_t *prk, uint32_t prk_len,
78 		 const uint8_t *th, uint32_t th_len, const uint8_t *id_cred,
79 		 uint32_t id_cred_len, const uint8_t *cred, uint32_t cred_len,
80 		 const uint8_t *ead, uint32_t ead_len, const char *mac_label,
81 		 uint8_t *signature_or_mac, uint32_t *signature_or_mac_len);
82 
83 #endif
84