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/suites.h"
15 #include "edhoc/hkdf_info.h"
16 
17 #include "common/byte_array.h"
18 #include "common/oscore_edhoc_error.h"
19 
20 #include "cbor/edhoc_encode_info.h"
21 
create_hkdf_info(uint8_t label,struct byte_array * context,uint32_t okm_len,struct byte_array * out)22 enum err create_hkdf_info(uint8_t label, struct byte_array *context,
23 			  uint32_t okm_len, struct byte_array *out)
24 {
25 	struct info info;
26 
27 	info.info_label = label;
28 
29 	/* NULL context with zero size is acceptable from EDHOC point of view,
30 	 * but CBOR encoder does not accept NULL as input argument.
31 	 * Internally it calls memmove that generates runtime error when input
32 	 * is NULL even if length is set to 0.
33 	 * Workaround is to provide dummy buffer to avoid passing NULL. It does not
34 	 * impact the EDHOC process, since context length is set to 0 and no value
35 	 * is copied to the EDHOC message. */
36 	const char dummy_buffer;
37 
38 	if (NULL == context->ptr) {
39 		if (0 != context->len) {
40 			return wrong_parameter;
41 		} else {
42 			info.info_context.value =
43 				(const uint8_t *)&dummy_buffer;
44 		}
45 	} else {
46 		info.info_context.value = context->ptr;
47 	}
48 
49 	info.info_context.len = context->len;
50 
51 	info.info_length = okm_len;
52 
53 	size_t payload_len_out = 0;
54 	TRY_EXPECT(cbor_encode_info(out->ptr, out->len, &info,
55 				    &payload_len_out),
56 		   0);
57 
58 	out->len = (uint32_t)payload_len_out;
59 
60 	return ok;
61 }
62