1 /*
2    Copyright (c) 2023 Assa Abloy. 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 
12 #ifndef OSCORE_COAP_DEFINES_H
13 #define OSCORE_COAP_DEFINES_H
14 
15 #define MAX_TOKEN_LEN 8
16 #define MAX_PIV_LEN 5
17 #define MAX_KID_CONTEXT_LEN                                                    \
18 	8 /*This implementation supports Context IDs up to 8 byte*/
19 #define MAX_KID_LEN 8
20 #define MAX_AAD_LEN 30
21 #define MAX_INFO_LEN 50
22 #define MAX_PIV_FIELD_VALUE                                                    \
23 	0xFFFFFFFFFF /* maximum possible value of SSN/PIV field is 2^40-1, according to RFC 8613 p. 7.2.1.*/
24 
25 /**
26  * @brief Maximum URI Path (resource name) size in bytes.
27  */
28 #ifndef OSCORE_MAX_URI_PATH_LEN
29 #define OSCORE_MAX_URI_PATH_LEN 30
30 #endif
31 
32 /* Mask and offset for first byte in CoAP/OSCORE header*/
33 #define HEADER_LEN 4
34 #define HEADER_VERSION_MASK 0xC0
35 #define HEADER_VERSION_OFFSET 6
36 #define HEADER_TYPE_MASK 0x30
37 #define HEADER_TYPE_OFFSET 4
38 #define HEADER_TKL_MASK 0x0F
39 #define HEADER_TKL_OFFSET 0
40 
41 /* Mask and offset for first byte in compressed OSCORE option*/
42 #define COMP_OSCORE_OPT_KIDC_H_MASK 0x10
43 #define COMP_OSCORE_OPT_KIDC_H_OFFSET 4
44 #define COMP_OSCORE_OPT_KID_K_MASK 0x08
45 #define COMP_OSCORE_OPT_KID_K_OFFSET 3
46 #define COMP_OSCORE_OPT_PIV_N_MASK 0x07
47 #define COMP_OSCORE_OPT_PIV_N_OFFSET 0
48 
49 #define ECHO_OPT_VALUE_LEN 12 /*see RFC9175 Appendix A.2*/
50 #define OSCORE_OPT_VALUE_LEN                                                   \
51 	(2 + MAX_PIV_LEN + MAX_KID_CONTEXT_LEN + MAX_KID_LEN)
52 
53 #define TYPE_CON 0x00
54 #define TYPE_NON 0x01
55 #define TYPE_ACK 0x02
56 #define TYPE_RST 0x03
57 
58 #define CODE_CLASS_MASK 0xe0
59 #define CODE_DETAIL_MASK 0x1f
60 #define CODE_EMPTY 0x00
61 #define CODE_REQ_GET 0x01
62 #define CODE_REQ_POST 0x02
63 #define CODE_REQ_FETCH 0x05
64 #define CODE_RESP_CHANGED 0x44
65 #define CODE_RESP_CONTENT 0x45
66 #define CODE_RESP_UNAUTHORIZED 0x81
67 #define REQUEST_CLASS 0x00
68 
69 #define OPTION_PAYLOAD_MARKER 0xFF
70 
71 #define MAX_OPTION_COUNT 20
72 #define MAX_E_OPTION_COUNT 10
73 
74 /**
75  * @brief Possible coap message types.
76  */
77 enum o_coap_msg {
78 	COAP_MSG_REQUEST = 0, /* Regular request */
79 	COAP_MSG_REGISTRATION, /* Request with OBSERVE option set to 0 */
80 	COAP_MSG_CANCELLATION, /* Request with OBSERVE option set to 1 */
81 	COAP_MSG_RESPONSE, /* Regular response */
82 	COAP_MSG_NOTIFICATION, /* Response with OBSERVE option */
83 };
84 
85 #endif
86