LCOV - code coverage report
Current view: top level - test/edhoc_integration_tests - edhoc_tests_kid.c (source / functions) Coverage Total Hit
Test: lcov.info Lines: 100.0 % 94 94
Test Date: 2026-11-06 14:43:19 Functions: 100.0 % 7 7
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 100.0 % 2 2

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Copyright (c) 2026 SmartWireless GmbH & Co. KG.
       3                 :             :  *
       4                 :             :  * SPDX-License-Identifier: Apache-2.0 or MIT
       5                 :             :  */
       6                 :             : 
       7                 :             : /*
       8                 :             :  * Representation of a 'kid' in ID_CRED_x, RFC 9528 Section 3.3.2 and
       9                 :             :  * Section 3.5.3.2.
      10                 :             :  */
      11                 :             : 
      12                 :             : #include <zephyr/kernel.h>
      13                 :             : #include <zephyr/ztest.h>
      14                 :             : 
      15                 :             : #include <edhoc.h>
      16                 :             : #include "edhoc/plaintext.h"
      17                 :             : #include "edhoc/retrieve_cred.h"
      18                 :             : 
      19                 :             : #include "common/byte_array.h"
      20                 :             : #include "common/oscore_edhoc_error.h"
      21                 :             : 
      22                 :             : #define ARRAY(...) ((const uint8_t[]){ __VA_ARGS__ })
      23                 :             : 
      24                 :             : /* SGN_or_MAC_3 as a bstr, appended to the ID_CRED_I under test */
      25                 :             : #define MAC_3 0x48, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
      26                 :             : 
      27                 :           8 : static void check_id_cred2kid(const uint8_t *id_cred, uint32_t id_cred_len,
      28                 :             :                               const uint8_t *exp, uint32_t exp_len)
      29                 :             : {
      30                 :           8 :         uint8_t kid_buf[8];
      31                 :           8 :         struct byte_array kid = { .ptr = kid_buf, .len = sizeof(kid_buf) };
      32                 :           8 :         struct byte_array in = { .ptr = (uint8_t *)id_cred,
      33                 :             :                                  .len = id_cred_len };
      34                 :             : 
      35                 :           8 :         zassert_equal(id_cred2kid(&in, &kid), ok, "id_cred2kid failed");
      36                 :           8 :         zassert_equal(kid.len, exp_len, "wrong kid length");
      37                 :           8 :         zassert_mem_equal__(kid.ptr, exp, exp_len, "wrong kid");
      38                 :           8 : }
      39                 :             : 
      40                 :           1 : void t_kid_id_cred2kid(void)
      41                 :             : {
      42                 :             :         /* kid that is a 1-byte CBOR int encoding: sent as int */
      43                 :           1 :         check_id_cred2kid(ARRAY(0xa1, 0x04, 0x41, 0x32), 4, ARRAY(0x32), 1);
      44                 :           1 :         check_id_cred2kid(ARRAY(0xa1, 0x04, 0x41, 0x05), 4, ARRAY(0x05), 1);
      45                 :           1 :         check_id_cred2kid(ARRAY(0xa1, 0x04, 0x41, 0x37), 4, ARRAY(0x37), 1);
      46                 :             : 
      47                 :             :         /* any other kid: sent as bstr */
      48                 :           1 :         check_id_cred2kid(ARRAY(0xa1, 0x04, 0x41, 0x18), 4, ARRAY(0x41, 0x18),
      49                 :             :                           2);
      50                 :           1 :         check_id_cred2kid(ARRAY(0xa1, 0x04, 0x41, 0x38), 4, ARRAY(0x41, 0x38),
      51                 :             :                           2);
      52                 :           1 :         check_id_cred2kid(ARRAY(0xa1, 0x04, 0x42, 0x01, 0x02), 5,
      53                 :           1 :                           ARRAY(0x42, 0x01, 0x02), 3);
      54                 :             : 
      55                 :             :         /* kid as int, as in drafts before RFC 9528 */
      56                 :           1 :         check_id_cred2kid(ARRAY(0xa1, 0x04, 0x32), 3, ARRAY(0x32), 1);
      57                 :             : 
      58                 :             :         /* kid together with another parameter: no compact encoding */
      59                 :           1 :         check_id_cred2kid(ARRAY(0xa2, 0x04, 0x41, 0x05, 0x18, 0x22, 0x82, 0x2e,
      60                 :             :                                 0x42, 0x01, 0x02),
      61                 :             :                           11, NULL, 0);
      62                 :           1 : }
      63                 :             : 
      64                 :           5 : static void check_plaintext3(const uint8_t *ptxt, uint32_t ptxt_len,
      65                 :             :                              const uint8_t *exp, uint32_t exp_len)
      66                 :             : {
      67                 :           5 :         uint8_t id_cred_buf[16];
      68                 :           5 :         uint8_t mac_buf[16];
      69                 :           5 :         uint8_t ead_buf[1];
      70                 :           5 :         struct byte_array in = { .ptr = (uint8_t *)ptxt, .len = ptxt_len };
      71                 :           5 :         struct byte_array id_cred = { .ptr = id_cred_buf,
      72                 :             :                                       .len = sizeof(id_cred_buf) };
      73                 :           5 :         struct byte_array mac = { .ptr = mac_buf, .len = sizeof(mac_buf) };
      74                 :           5 :         struct byte_array ead = { .ptr = ead_buf, .len = sizeof(ead_buf) };
      75                 :             : 
      76                 :           5 :         zassert_equal(plaintext_split(&in, NULL, &id_cred, &mac, &ead), ok,
      77                 :             :                       "plaintext_split failed");
      78                 :           5 :         zassert_equal(id_cred.len, exp_len, "wrong ID_CRED_I length");
      79                 :           5 :         zassert_mem_equal__(id_cred.ptr, exp, exp_len, "wrong ID_CRED_I");
      80                 :           5 :         zassert_equal(mac.len, 8, "wrong MAC_3 length");
      81                 :           5 : }
      82                 :             : 
      83                 :           1 : void t_kid_plaintext_split(void)
      84                 :             : {
      85                 :             :         /* int -12 stands for the kid h'2b' */
      86                 :           1 :         check_plaintext3(ARRAY(0x2b, MAC_3), 10, ARRAY(0xa1, 0x04, 0x41, 0x2b),
      87                 :             :                          4);
      88                 :             :         /* int 5 stands for the kid h'05' */
      89                 :           1 :         check_plaintext3(ARRAY(0x05, MAC_3), 10, ARRAY(0xa1, 0x04, 0x41, 0x05),
      90                 :             :                          4);
      91                 :             :         /* kid sent as bstr */
      92                 :           1 :         check_plaintext3(ARRAY(0x41, 0x18, MAC_3), 11,
      93                 :           1 :                          ARRAY(0xa1, 0x04, 0x41, 0x18), 4);
      94                 :           1 :         check_plaintext3(ARRAY(0x42, 0x01, 0x02, MAC_3), 12,
      95                 :           1 :                          ARRAY(0xa1, 0x04, 0x42, 0x01, 0x02), 5);
      96                 :             :         /* int outside -24..23, only sent by peers before RFC 9528 */
      97                 :           1 :         check_plaintext3(ARRAY(0x18, 0x20, MAC_3), 11,
      98                 :           1 :                          ARRAY(0xa1, 0x04, 0x18, 0x20), 4);
      99                 :           1 : }
     100                 :             : 
     101                 :           4 : static enum err lookup(const uint8_t *configured, uint32_t configured_len,
     102                 :             :                        struct byte_array *id_cred)
     103                 :             : {
     104                 :           4 :         static const uint8_t cred_x[] = { 0xa0 };
     105                 :           4 :         static const uint8_t g_x[32] = { 0x01 };
     106                 :           4 :         uint8_t cred_buf[8];
     107                 :           4 :         uint8_t pk_buf[65];
     108                 :           4 :         uint8_t g_buf[65];
     109                 :           4 :         struct byte_array cred = { .ptr = cred_buf, .len = sizeof(cred_buf) };
     110                 :           4 :         struct byte_array pk = { .ptr = pk_buf, .len = sizeof(pk_buf) };
     111                 :           4 :         struct byte_array g = { .ptr = g_buf, .len = sizeof(g_buf) };
     112                 :             : 
     113                 :           4 :         struct other_party_cred other = {
     114                 :             :                 .id_cred = { .ptr = (uint8_t *)configured,
     115                 :             :                              .len = configured_len },
     116                 :             :                 .cred = { .ptr = (uint8_t *)cred_x, .len = sizeof(cred_x) },
     117                 :             :                 .g = { .ptr = (uint8_t *)g_x, .len = sizeof(g_x) },
     118                 :             :         };
     119                 :           4 :         struct cred_array creds = { .len = 1, .ptr = &other };
     120                 :             : 
     121                 :           4 :         return retrieve_cred(true, &creds, id_cred, &cred, &pk, &g);
     122                 :             : }
     123                 :             : 
     124                 :           1 : void t_kid_retrieve_cred(void)
     125                 :             : {
     126                 :           1 :         uint8_t buf[4];
     127                 :           1 :         struct byte_array id_cred = { .ptr = buf, .len = sizeof(buf) };
     128                 :           1 :         const uint8_t received[] = { 0xa1, 0x04, 0x41, 0x32 };
     129                 :             : 
     130                 :             :         /* credential configured as in RFC 9528 */
     131                 :           1 :         memcpy(buf, received, sizeof(received));
     132                 :           1 :         id_cred.len = sizeof(received);
     133                 :           1 :         zassert_equal(lookup(received, sizeof(received), &id_cred), ok,
     134                 :             :                       "RFC 9528 kid not found");
     135                 :           1 :         zassert_equal(id_cred.len, 4, "ID_CRED changed");
     136                 :             : 
     137                 :             :         /* credential configured with the int kid of drafts before RFC 9528 */
     138                 :           1 :         id_cred.len = sizeof(received);
     139                 :           1 :         zassert_equal(lookup(ARRAY(0xa1, 0x04, 0x32), 3, &id_cred), ok,
     140                 :             :                       "int kid not found");
     141                 :           1 :         zassert_equal(id_cred.len, 3, "ID_CRED not replaced");
     142                 :           1 :         zassert_mem_equal__(id_cred.ptr, ARRAY(0xa1, 0x04, 0x32), 3,
     143                 :             :                             "ID_CRED not replaced");
     144                 :             : 
     145                 :             :         /* different kid */
     146                 :           1 :         memcpy(buf, received, sizeof(received));
     147                 :           1 :         id_cred.len = sizeof(received);
     148                 :           1 :         zassert_equal(lookup(ARRAY(0xa1, 0x04, 0x41, 0x33), 4, &id_cred),
     149                 :             :                       credential_not_found, "wrong credential found");
     150                 :             : 
     151                 :             :         /* kid h'18' isn't an int encoding, so no int fallback */
     152                 :           1 :         buf[3] = 0x18;
     153                 :           1 :         id_cred.len = sizeof(received);
     154                 :           1 :         zassert_equal(lookup(ARRAY(0xa1, 0x04, 0x18, 0x18), 4, &id_cred),
     155                 :             :                       credential_not_found, "wrong credential found");
     156                 :           1 : }
     157                 :             : 
     158                 :           1 : void t_retrieve_cred_uncompressed_g(void)
     159                 :             : {
     160                 :           1 :         static const uint8_t id_cred_x[] = { 0xa1, 0x04, 0x41, 0x2b };
     161                 :           1 :         static const uint8_t cred_x[] = { 0xa0 };
     162                 :           1 :         uint8_t g_x[65] = { 0x04 };
     163                 :           1 :         uint8_t cred_buf[8];
     164                 :           1 :         uint8_t pk_buf[65];
     165                 :           1 :         uint8_t g_buf[65];
     166                 :           1 :         uint8_t id_cred_buf[sizeof(id_cred_x)];
     167                 :           1 :         struct byte_array cred = { .ptr = cred_buf, .len = sizeof(cred_buf) };
     168                 :           1 :         struct byte_array pk = { .ptr = pk_buf, .len = sizeof(pk_buf) };
     169                 :           1 :         struct byte_array g = { .ptr = g_buf, .len = sizeof(g_buf) };
     170                 :           1 :         struct byte_array id_cred = { .ptr = id_cred_buf,
     171                 :             :                                       .len = sizeof(id_cred_buf) };
     172                 :             : 
     173         [ +  + ]:          65 :         for (uint32_t i = 1; i < sizeof(g_x); i++) {
     174                 :          64 :                 g_x[i] = (uint8_t)i;
     175                 :             :         }
     176                 :           1 :         memcpy(id_cred_buf, id_cred_x, sizeof(id_cred_x));
     177                 :             : 
     178                 :           1 :         struct other_party_cred other = {
     179                 :             :                 .id_cred = { .ptr = (uint8_t *)id_cred_x,
     180                 :             :                              .len = sizeof(id_cred_x) },
     181                 :             :                 .cred = { .ptr = (uint8_t *)cred_x, .len = sizeof(cred_x) },
     182                 :             :                 .g = { .ptr = g_x, .len = sizeof(g_x) },
     183                 :             :         };
     184                 :           1 :         struct cred_array creds = { .len = 1, .ptr = &other };
     185                 :             : 
     186                 :           1 :         zassert_equal(retrieve_cred(true, &creds, &id_cred, &cred, &pk, &g), ok,
     187                 :             :                       "credential not found");
     188                 :           1 :         zassert_equal(g.len, 32, "wrong G_X length");
     189                 :           1 :         zassert_mem_equal__(g.ptr, &g_x[1], 32, "wrong G_X");
     190                 :           1 : }
        

Generated by: LCOV version 2.0-1