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 <string.h>
13 
14 #include "edhoc/buffer_sizes.h"
15 
16 #include "edhoc/suites.h"
17 #include "edhoc/prk.h"
18 #include "edhoc/okm.h"
19 
20 #include "common/crypto_wrapper.h"
21 #include "common/oscore_edhoc_error.h"
22 #include "common/print_util.h"
23 #include "common/memcpy_s.h"
24 
prk_derive(bool static_dh_auth,struct suite suite,uint8_t label,struct byte_array * context,const struct byte_array * prk_in,const struct byte_array * stat_pk,const struct byte_array * stat_sk,uint8_t * prk_out)25 enum err prk_derive(bool static_dh_auth, struct suite suite, uint8_t label,
26 		    struct byte_array *context, const struct byte_array *prk_in,
27 		    const struct byte_array *stat_pk,
28 		    const struct byte_array *stat_sk, uint8_t *prk_out)
29 {
30 	if (static_dh_auth) {
31 		BYTE_ARRAY_NEW(dh_secret, ECDH_SECRET_SIZE, ECDH_SECRET_SIZE);
32 
33 		TRY(shared_secret_derive(suite.edhoc_ecdh, stat_sk, stat_pk,
34 					 dh_secret.ptr));
35 		PRINT_ARRAY("dh_secret", dh_secret.ptr, dh_secret.len);
36 
37 		BYTE_ARRAY_NEW(salt, HASH_SIZE, get_hash_len(suite.edhoc_hash));
38 		TRY(edhoc_kdf(suite.edhoc_hash, prk_in, label, context, &salt));
39 		PRINT_ARRAY("SALT_3e2m or SALT4e3m", salt.ptr, salt.len);
40 
41 		TRY(hkdf_extract(suite.edhoc_hash, &salt, &dh_secret, prk_out));
42 	} else {
43 		/*it is save to do that since prks have the same size*/
44 		memcpy(prk_out, prk_in->ptr, prk_in->len);
45 	}
46 	return ok;
47 }
48