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 #include "edhoc/suites.h"
13 
14 #include "common/oscore_edhoc_error.h"
15 
get_suite(enum suite_label label,struct suite * suite)16 enum err get_suite(enum suite_label label, struct suite *suite)
17 {
18 	switch (label) {
19 	case SUITE_0:
20 		suite->suite_label = SUITE_0;
21 		suite->edhoc_aead = AES_CCM_16_64_128;
22 		suite->edhoc_hash = SHA_256;
23 		suite->edhoc_mac_len_static_dh = MAC8;
24 		suite->edhoc_ecdh = X25519;
25 		suite->edhoc_sign = EdDSA;
26 		suite->app_aead = AES_CCM_16_64_128;
27 		suite->app_hash = SHA_256;
28 		break;
29 	case SUITE_1:
30 		suite->suite_label = SUITE_1;
31 		suite->edhoc_aead = AES_CCM_16_128_128;
32 		suite->edhoc_hash = SHA_256;
33 		suite->edhoc_mac_len_static_dh = MAC16;
34 		suite->edhoc_ecdh = X25519;
35 		suite->edhoc_sign = EdDSA;
36 		suite->app_aead = AES_CCM_16_64_128;
37 		suite->app_hash = SHA_256;
38 		break;
39 	case SUITE_2:
40 		suite->suite_label = SUITE_2;
41 		suite->edhoc_aead = AES_CCM_16_64_128;
42 		suite->edhoc_hash = SHA_256;
43 		suite->edhoc_mac_len_static_dh = MAC8;
44 		suite->edhoc_ecdh = P256;
45 		suite->edhoc_sign = ES256;
46 		suite->app_aead = AES_CCM_16_64_128;
47 		suite->app_hash = SHA_256;
48 		break;
49 	case SUITE_3:
50 		suite->suite_label = SUITE_3;
51 		suite->edhoc_aead = AES_CCM_16_128_128;
52 		suite->edhoc_hash = SHA_256;
53 		suite->edhoc_mac_len_static_dh = MAC16;
54 		suite->edhoc_ecdh = P256;
55 		suite->edhoc_sign = ES256;
56 		suite->app_aead = AES_CCM_16_64_128;
57 		suite->app_hash = SHA_256;
58 		break;
59 	default:
60 		return unsupported_cipher_suite;
61 		break;
62 	}
63 	return ok;
64 }
65 
get_hash_len(enum hash_alg alg)66 uint32_t get_hash_len(enum hash_alg alg)
67 {
68 	switch (alg) {
69 	case SHA_256:
70 		return 32;
71 		break;
72 	}
73 	return 0;
74 }
75 
get_aead_mac_len(enum aead_alg alg)76 uint32_t get_aead_mac_len(enum aead_alg alg)
77 {
78 	switch (alg) {
79 	case AES_CCM_16_128_128:
80 		return 16;
81 		break;
82 	case AES_CCM_16_64_128:
83 		return 8;
84 		break;
85 	}
86 	return 0;
87 }
88 
get_aead_key_len(enum aead_alg alg)89 uint32_t get_aead_key_len(enum aead_alg alg)
90 {
91 	switch (alg) {
92 	case AES_CCM_16_128_128:
93 	case AES_CCM_16_64_128:
94 		return 16;
95 		break;
96 	}
97 	return 0;
98 }
99 
get_aead_iv_len(enum aead_alg alg)100 uint32_t get_aead_iv_len(enum aead_alg alg)
101 {
102 	switch (alg) {
103 	case AES_CCM_16_128_128:
104 	case AES_CCM_16_64_128:
105 		return 13;
106 		break;
107 	}
108 	return 0;
109 }
110 
get_signature_len(enum sign_alg alg)111 uint32_t get_signature_len(enum sign_alg alg)
112 {
113 	switch (alg) {
114 	case ES256:
115 	case EdDSA:
116 		return 64;
117 		break;
118 	}
119 	return 0;
120 }
121 
get_ecdh_pk_len(enum ecdh_alg alg)122 uint32_t get_ecdh_pk_len(enum ecdh_alg alg)
123 {
124 	switch (alg) {
125 	case P256:
126 		/*The publick key is in compressed form*/
127 		return 33;
128 		break;
129 	case X25519:
130 		return 32;
131 		break;
132 	}
133 	return 0;
134 }
135