Skip to content

Commit

Permalink
initial change
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-kielbasa-aa committed Nov 29, 2023
1 parent 9c18a35 commit 89c9dcf
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
4 changes: 4 additions & 0 deletions inc/edhoc/plaintext.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

#include "common/oscore_edhoc_error.h"

/* draft-ietf-lake-edhoc-22: 3.3.2. Representation of Byte String Identifiers */
#define ONE_BYTE_CBOR_ENCODED_INT_MIN_VAL ( -24 )
#define ONE_BYTE_CBOR_ENCODED_INT_MAX_VAL ( 23 )

/**
* @brief Decodes id_cred to kid.
*
Expand Down
35 changes: 30 additions & 5 deletions src/edhoc/plaintext_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>

#include "edhoc/retrieve_cred.h"
#include "edhoc/plaintext.h"
Expand Down Expand Up @@ -43,13 +44,32 @@ static enum err id_cred_x_encode(enum id_cred_x_label label, int algo,

switch (label) {
case kid:
{
//todo update that to v15
map._id_cred_x_map_kid_present = true;
map._id_cred_x_map_kid._id_cred_x_map_kid_choice =
_id_cred_x_map_kid_int;
map._id_cred_x_map_kid._id_cred_x_map_kid_int =
*((const int32_t *)id);

int32_t kid_as_int = 0;
const size_t kid_as_int_len = (id_len < sizeof(kid_as_int)) ?
id_len : sizeof(kid_as_int);
memcpy(&kid_as_int, id, kid_as_int_len);

if (kid_as_int >= ONE_BYTE_CBOR_ENCODED_INT_MIN_VAL &&
kid_as_int <= ONE_BYTE_CBOR_ENCODED_INT_MIN_VAL) {
map._id_cred_x_map_kid._id_cred_x_map_kid_choice =
_id_cred_x_map_kid_int;
map._id_cred_x_map_kid._id_cred_x_map_kid_int =
*((const int32_t *)id);
} else {
map._id_cred_x_map_kid._id_cred_x_map_kid_choice =
_id_cred_x_map_kid_bstr;
map._id_cred_x_map_kid._id_cred_x_map_kid_bstr.value =
(const uint8_t *)id;
map._id_cred_x_map_kid._id_cred_x_map_kid_bstr.len =
id_len;
}

break;
}
case x5chain:
map._id_cred_x_map_x5chain_present = true;
map._id_cred_x_map_x5chain._id_cred_x_map_x5chain.value = id;
Expand Down Expand Up @@ -120,7 +140,12 @@ enum err plaintext_split(struct byte_array *ptxt, struct byte_array *id_cred_x,
kid, 0, p._plaintext_ID_CRED_x_bstr.value,
(uint32_t)p._plaintext_ID_CRED_x_bstr.len,
id_cred_x));

} else if (_plaintext_ID_CRED_x_bstr ==
p._plaintext_ID_CRED_x_choice) {
TRY(id_cred_x_encode(
kid, 0, p._plaintext_ID_CRED_x_bstr.value,
(uint32_t)p._plaintext_ID_CRED_x_bstr.len,
id_cred_x));
} else {
int _kid = p._plaintext_ID_CRED_x_int;
TRY(id_cred_x_encode(kid, 0, &_kid, 1, id_cred_x));
Expand Down
32 changes: 26 additions & 6 deletions src/edhoc/plaintext_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

#include <stdint.h>
#include <stddef.h>

#include "edhoc/retrieve_cred.h"
#include "edhoc/signature_or_mac_msg.h"
Expand All @@ -22,6 +23,7 @@

#include "cbor/edhoc_decode_id_cred_x.h"
#include "cbor/edhoc_encode_int_type.h"
#include "cbor/edhoc_encode_bstr_type.h"

enum err id_cred2kid(const struct byte_array *id_cred, struct byte_array *kid)
{
Expand All @@ -33,12 +35,30 @@ enum err id_cred2kid(const struct byte_array *id_cred, struct byte_array *kid)
0);

if (map._id_cred_x_map_kid_present) {
TRY_EXPECT(
cbor_encode_int_type_i(
kid->ptr, kid->len,
&map._id_cred_x_map_kid._id_cred_x_map_kid_int,
&payload_len_out),
ZCBOR_SUCCESS);
int32_t kid_as_int = 0;
const size_t kid_as_int_len =
(id_cred->len < sizeof(kid_as_int)) ?
id_cred->len : sizeof(kid_as_int);
memcpy(&kid_as_int, id_cred->ptr, kid_as_int_len);

if (_id_cred_x_map_kid_int == map._id_cred_x_map_kid._id_cred_x_map_kid_choice &&
kid_as_int >= ONE_BYTE_CBOR_ENCODED_INT_MIN_VAL &&
kid_as_int <= ONE_BYTE_CBOR_ENCODED_INT_MIN_VAL) {
TRY_EXPECT(
cbor_encode_int_type_i(
kid->ptr, kid->len,
&map._id_cred_x_map_kid._id_cred_x_map_kid_int,
&payload_len_out),
ZCBOR_SUCCESS);
} else {
TRY_EXPECT(
cbor_encode_bstr_type_b_str(
kid->ptr, kid->len,
&map._id_cred_x_map_kid._id_cred_x_map_kid_bstr,
&payload_len_out),
ZCBOR_SUCCESS);
}

kid->len = (uint32_t)payload_len_out;
} else {
kid->len = 0;
Expand Down

0 comments on commit 89c9dcf

Please sign in to comment.