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 
12 #ifndef ERROR_H
13 #define ERROR_H
14 #include "print_util.h"
15 
16 #define RED "\x1B[31m"
17 #define RESET "\033[0m"
18 
19 /* All possible errors that EDHOC and OSCORE can have */
20 enum err {
21 	/*common errors*/
22 	ok = 0,
23 	buffer_to_small = 1,
24 	hkdf_fialed = 2,
25 	unexpected_result_from_ext_lib = 3,
26 	wrong_parameter = 4,
27 	crypto_operation_not_implemented = 5,
28 
29 	/*EDHOC specifc errors*/
30 	/*todo implement error messages*/
31 	error_message_received = 101,
32 	error_message_sent = 102,
33 
34 	sign_failed = 103,
35 	sha_failed = 104,
36 
37 	unsupported_cipher_suite = 106,
38 	unsupported_ecdh_curve = 107,
39 	unsupported_signature_algorithm = 110,
40 
41 	signature_authentication_failed = 112,
42 	mac_authentication_failed = 113,
43 	certificate_authentication_failed = 115,
44 
45 	credential_not_found = 116,
46 	no_such_ca = 117,
47 
48 	cbor_encoding_error = 119,
49 	suites_i_list_to_long = 121,
50 
51 	/*OSCORE specific errors*/
52 	oscore_unknown_hkdf = 202,
53 	oscore_invalid_algorithm_aead = 204,
54 	oscore_invalid_algorithm_hkdf = 205,
55 	oscore_kid_recipent_id_mismatch = 206,
56 	oscore_valuelen_to_long_error = 208,
57 	oscore_inpkt_invalid_tkl = 209,
58 	oscore_inpkt_invalid_option_delta = 210,
59 	oscore_inpkt_invalid_optionlen = 211,
60 	oscore_inpkt_invalid_piv = 212,
61 	delta_extra_byte_error = 215,
62 	len_extra_byte_error = 216,
63 	not_valid_input_packet = 218,
64 	replayed_packed_received = 219,
65 
66 };
67 
68 /*This macro checks if a function returns an error and if so it propages
69 	the error to the caller function*/
70 #define TRY(x)                                                                       \
71 	do {                                                                         \
72 		enum err retval = (x);                                               \
73 		if (retval != ok) {                                                  \
74 			PRINTF(RED                                                   \
75 			       "Runtime error: %s error code %d at %s:%d\n\n" RESET, \
76 			       #x, retval, __FILE__, __LINE__);                      \
77 			return retval;                                               \
78 		}                                                                    \
79 	} while (0)
80 
81 /* This macro checks if a function belonging to an external library returns an expected result or an error. If an error is returned the macro returns unexpected_result_from_ext_lib. */
82 #define TRY_EXPECT(x, expected_result)                                               \
83 	do {                                                                         \
84 		int retval = (x);                                                    \
85 		if (retval != expected_result) {                                     \
86 			PRINTF(RED                                                   \
87 			       "Runtime error: %s error code %d at %s:%d\n\n" RESET, \
88 			       #x, retval, __FILE__, __LINE__);                      \
89 			return unexpected_result_from_ext_lib;                       \
90 		}                                                                    \
91 	} while (0)
92 
93 #endif
94