LCOV - code coverage report
Current view: top level - src/edhoc - signature_or_mac_msg.c (source / functions) Coverage Total Hit
Test: lcov.info Lines: 94.2 % 69 65
Test Date: 2026-11-06 14:43:19 Functions: 100.0 % 3 3
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 41.9 % 222 93

             Branch data     Line data    Source code
       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 <stdint.h>
      13                 :             : 
      14                 :             : #include "edhoc/buffer_sizes.h"
      15                 :             : #include "edhoc/edhoc_cose.h"
      16                 :             : #include "edhoc/hkdf_info.h"
      17                 :             : #include "edhoc/okm.h"
      18                 :             : #include "edhoc/suites.h"
      19                 :             : #include "edhoc/signature_or_mac_msg.h"
      20                 :             : #include "edhoc/bstr_encode_decode.h"
      21                 :             : #include "edhoc/int_encode_decode.h"
      22                 :             : 
      23                 :             : #include "common/print_util.h"
      24                 :             : #include "common/crypto_wrapper.h"
      25                 :             : #include "common/oscore_edhoc_error.h"
      26                 :             : #include "common/memcpy_s.h"
      27                 :             : 
      28                 :             : #include "cbor/edhoc_encode_enc_structure.h"
      29                 :             : #include "cbor/edhoc_encode_sig_structure.h"
      30                 :             : 
      31                 :             : /**
      32                 :             :  * @brief                       Forms a serialized data structure from a set of 
      33                 :             :  *                              data items and computes a MAC over it.
      34                 :             :  * 
      35                 :             :  * @param[in] prk               The key to be used for the mac.
      36                 :             :  * @param[in] c_r               Connection identifier of the requester
      37                 :             :  * @param[in] th                Transcript hash.
      38                 :             :  * @param[in] id_cred           ID of the credential.
      39                 :             :  * @param[in] cred              The credential.
      40                 :             :  * @param[in] ead               External authorization data.
      41                 :             :  * @param mac_label             An info label.
      42                 :             :  * @param static_dh             True if static DH is used for authentication.
      43                 :             :  * @param suite                 The used crypto suite.
      44                 :             :  * @param[out] mac              The computed mac.
      45                 :             :  * @return                      Ok or error code.
      46                 :             :  */
      47                 :          20 : static enum err mac(const struct byte_array *prk, const struct byte_array *c_r,
      48                 :             :                     const struct byte_array *th,
      49                 :             :                     const struct byte_array *id_cred,
      50                 :             :                     const struct byte_array *cred, const struct byte_array *ead,
      51                 :             :                     enum info_label mac_label, bool static_dh,
      52                 :             :                     struct suite *suite, struct byte_array *mac)
      53                 :             : {
      54                 :             :         /*encode th as bstr*/
      55   [ +  -  -  +  :          20 :         BYTE_ARRAY_NEW(th_enc, AS_BSTR_SIZE(HASH_SIZE), AS_BSTR_SIZE(th->len));
          -  -  -  +  +  
          -  -  +  -  -  
          -  +  +  -  -  
                +  -  - ]
      56         [ -  + ]:          20 :         TRY(encode_bstr(th, &th_enc));
      57                 :             : 
      58                 :             :         /**/
      59   [ -  +  -  -  :          20 :         BYTE_ARRAY_NEW(context_mac, CONTEXT_MAC_SIZE,
          -  -  -  +  -  
          +  -  -  -  -  
          -  +  -  +  -  
                -  -  - ]
      60                 :             :                        AS_BSTR_SIZE(c_r->len) + id_cred->len + cred->len +
      61                 :             :                                ead->len + th_enc.len);
      62                 :          20 :         uint32_t capacity = context_mac.len;
      63                 :          20 :         context_mac.len = 0;
      64                 :             :         /* Only context_2 contains C_R. C_R may be the empty byte string, so
      65                 :             :          * its length can't tell MAC_2 from MAC_3. */
      66         [ +  + ]:          20 :         if (mac_label == MAC_2) {
      67   [ -  +  -  -  :          10 :                 BYTE_ARRAY_NEW(c_r_enc, AS_BSTR_SIZE(C_R_SIZE),
          -  -  -  +  -  
          +  -  -  -  -  
          -  +  -  +  -  
                -  -  - ]
      68                 :             :                                AS_BSTR_SIZE(c_r->len));
      69                 :             :                 /* C_R is encoded as in message_2 (RFC 9528 Section 3.3.2) */
      70         [ +  + ]:          10 :                 if (c_x_is_encoded_int(c_r)) {
      71         [ -  + ]:           6 :                         TRY(byte_array_append(&context_mac, c_r, capacity));
      72                 :             :                 } else {
      73         [ -  + ]:           4 :                         TRY(encode_bstr(c_r, &c_r_enc));
      74         [ -  + ]:           4 :                         TRY(byte_array_append(&context_mac, &c_r_enc,
      75                 :             :                                               capacity));
      76                 :             :                 }
      77                 :             :         }
      78         [ -  + ]:          20 :         TRY(byte_array_append(&context_mac, id_cred, capacity));
      79         [ -  + ]:          20 :         TRY(byte_array_append(&context_mac, &th_enc, capacity));
      80         [ -  + ]:          20 :         TRY(byte_array_append(&context_mac, cred, capacity));
      81                 :             : 
      82         [ -  + ]:          20 :         if (0 < ead->len) {
      83         [ #  # ]:           0 :                 TRY(byte_array_append(&context_mac, ead, capacity));
      84                 :             :         }
      85                 :             : 
      86                 :          20 :         PRINT_ARRAY("MAC context", context_mac.ptr, context_mac.len);
      87                 :             : 
      88         [ +  + ]:          20 :         if (static_dh) {
      89                 :           8 :                 mac->len = suite->edhoc_mac_len_static_dh;
      90                 :             : 
      91                 :             :         } else {
      92                 :          12 :                 mac->len = get_hash_len(suite->edhoc_hash);
      93                 :             :         }
      94                 :             : 
      95         [ -  + ]:          20 :         TRY(edhoc_kdf(suite->edhoc_hash, prk, mac_label, &context_mac, mac));
      96                 :             : 
      97                 :          20 :         PRINT_ARRAY("MAC 2/3", mac->ptr, mac->len);
      98                 :          20 :         return ok;
      99                 :             : }
     100                 :             : 
     101                 :             : /**
     102                 :             :  * @brief                       Creates a byte array to be ready for signing. 
     103                 :             :  * 
     104                 :             :  * @param[in] th                Transcript hash.
     105                 :             :  * @param[in] id_cred           Id of the credential.
     106                 :             :  * @param[in] cred              The credential.
     107                 :             :  * @param[in] ead               External Authorization Data. 
     108                 :             :  * @param[in] mac               Message Authentication Code. 
     109                 :             :  * @param[out] out              The result.
     110                 :             :  * @return                      Ok or error code.
     111                 :             :  */
     112                 :          12 : static enum err signature_struct_gen(const struct byte_array *th,
     113                 :             :                                      const struct byte_array *id_cred,
     114                 :             :                                      const struct byte_array *cred,
     115                 :             :                                      const struct byte_array *ead,
     116                 :             :                                      const struct byte_array *mac,
     117                 :             :                                      struct byte_array *out)
     118                 :             : {
     119         [ -  + ]:          12 :         BYTE_ARRAY_NEW(th_enc, AS_BSTR_SIZE(HASH_SIZE),
     120                 :             :                        AS_BSTR_SIZE(HASH_SIZE));
     121                 :             : 
     122         [ -  + ]:          12 :         TRY(encode_bstr(th, &th_enc));
     123                 :             : 
     124   [ -  +  -  + ]:          12 :         BYTE_ARRAY_NEW(tmp,
     125                 :             :                        (CRED_MAX_SIZE + AS_BSTR_SIZE(HASH_SIZE) + EAD_SIZE),
     126                 :             :                        (th_enc.len + cred->len + ead->len));
     127                 :             : 
     128                 :          12 :         memcpy(tmp.ptr, th_enc.ptr, th_enc.len);
     129                 :          12 :         memcpy(tmp.ptr + th_enc.len, cred->ptr, cred->len);
     130         [ -  + ]:          12 :         if (ead->len != 0) {
     131                 :           0 :                 memcpy(tmp.ptr + th_enc.len + cred->len, ead->ptr, ead->len);
     132                 :             :         }
     133                 :             : 
     134                 :          12 :         uint8_t context_str[] = { "Signature1" };
     135                 :          12 :         struct byte_array str = BYTE_ARRAY_INIT(
     136                 :             :                 context_str, (uint32_t)strlen((char *)context_str));
     137                 :             : 
     138         [ -  + ]:          12 :         TRY(cose_sig_structure_encode(&str, id_cred, &tmp, mac, out));
     139                 :          12 :         PRINT_ARRAY("COSE_Sign1 object to be signed", out->ptr, out->len);
     140                 :          12 :         return ok;
     141                 :             : }
     142                 :             : 
     143                 :             : enum err
     144                 :          20 : signature_or_mac(enum sgn_or_mac_op op, bool static_dh, struct suite *suite,
     145                 :             :                  const struct byte_array *sk, const struct byte_array *pk,
     146                 :             :                  const struct byte_array *prk, const struct byte_array *c_r,
     147                 :             :                  const struct byte_array *th, const struct byte_array *id_cred,
     148                 :             :                  const struct byte_array *cred, const struct byte_array *ead,
     149                 :             :                  enum info_label mac_label, struct byte_array *signature_or_mac)
     150                 :             : {
     151         [ +  + ]:          20 :         if (op == GENERATE) {
     152                 :             :                 /*we always calculate the mac*/
     153         [ -  + ]:          10 :                 TRY(mac(prk, c_r, th, id_cred, cred, ead, mac_label, static_dh,
     154                 :             :                         suite, signature_or_mac));
     155                 :             : 
     156         [ +  + ]:          10 :                 if (static_dh) {
     157                 :             :                         /*signature_or_mac is mac when the caller of this function authenticates with static DH keys*/
     158                 :           4 :                         return ok;
     159                 :             :                 } else {
     160                 :           6 :                         PRINTF("SIG_STRUCT_SIZE: %d\n", SIG_STRUCT_SIZE);
     161   [ +  -  +  +  :           6 :                         uint32_t sig_struct_size = SIG_STRUCT_SIZE_CALC(
          +  -  +  -  -  
          +  -  -  +  -  
          +  -  -  +  -  
          -  +  -  +  -  
          -  +  -  -  +  
          -  +  -  -  +  
          -  -  +  -  -  
                +  -  - ]
     162                 :             :                                 COSE_SIGN1_STR_LEN, id_cred->len,
     163                 :             :                                 (AS_BSTR_SIZE(th->len) + cred->len + ead->len),
     164                 :             :                                 signature_or_mac->len);
     165                 :             : 
     166                 :           6 :                         PRINTF("sig_struct_size: %d\n", sig_struct_size);
     167   [ -  +  -  + ]:           6 :                         BYTE_ARRAY_NEW(sign_struct, SIG_STRUCT_SIZE,
     168                 :             :                                        sig_struct_size);
     169         [ -  + ]:           6 :                         TRY(signature_struct_gen(th, id_cred, cred, ead,
     170                 :             :                                                  signature_or_mac,
     171                 :             :                                                  &sign_struct));
     172                 :             : 
     173                 :           6 :                         signature_or_mac->len =
     174                 :           6 :                                 get_signature_len(suite->edhoc_sign);
     175                 :             : 
     176         [ -  + ]:           6 :                         TRY(sign(suite->edhoc_sign, sk, pk, &sign_struct,
     177                 :             :                                  signature_or_mac->ptr));
     178                 :           6 :                         PRINT_ARRAY("signature_or_mac (is signature)",
     179                 :             :                                     signature_or_mac->ptr,
     180                 :             :                                     signature_or_mac->len);
     181                 :             :                 }
     182                 :             :         } else { /*we verify here*/
     183   [ -  +  -  + ]:          10 :                 BYTE_ARRAY_NEW(_mac, HASH_SIZE,
     184                 :             :                                get_hash_len(suite->edhoc_hash));
     185                 :             : 
     186         [ -  + ]:          10 :                 TRY(mac(prk, c_r, th, id_cred, cred, ead, mac_label, static_dh,
     187                 :             :                         suite, &_mac));
     188                 :             : 
     189         [ +  + ]:          10 :                 if (static_dh) {
     190                 :             :                         /*signature_or_mac is mac when the caller of this function authenticates with static DH keys*/
     191                 :             : 
     192         [ -  + ]:           4 :                         if (0 != memcmp(_mac.ptr, signature_or_mac->ptr,
     193                 :             :                                         signature_or_mac->len)) {
     194                 :           0 :                                 return mac_authentication_failed;
     195                 :             :                         }
     196                 :             : 
     197                 :             :                 } else {
     198                 :           6 :                         PRINTF("SIG_STRUCT_SIZE: %d\n", SIG_STRUCT_SIZE);
     199   [ +  -  +  +  :           6 :                         uint32_t sig_struct_size = SIG_STRUCT_SIZE_CALC(
          +  -  +  -  -  
          +  -  -  +  -  
          +  -  -  +  -  
          -  +  -  +  -  
          -  +  -  -  +  
          -  +  -  -  +  
          -  -  +  -  -  
                +  -  - ]
     200                 :             :                                 COSE_SIGN1_STR_LEN, id_cred->len,
     201                 :             :                                 (AS_BSTR_SIZE(th->len) + cred->len + ead->len),
     202                 :             :                                 _mac.len);
     203                 :             : 
     204                 :           6 :                         PRINTF("sig_struct_size: %d\n", sig_struct_size);
     205   [ -  +  -  + ]:           6 :                         BYTE_ARRAY_NEW(sign_struct, SIG_STRUCT_SIZE,
     206                 :             :                                        sig_struct_size);
     207         [ -  + ]:           6 :                         TRY(signature_struct_gen(th, id_cred, cred, ead, &_mac,
     208                 :             :                                                  &sign_struct));
     209                 :             : 
     210                 :             :                         bool result;
     211                 :           6 :                         PRINT_ARRAY("pk", pk->ptr, pk->len);
     212                 :           6 :                         PRINT_ARRAY("signature_struct", sign_struct.ptr,
     213                 :             :                                     sign_struct.len);
     214                 :           6 :                         PRINT_ARRAY("signature_or_mac", signature_or_mac->ptr,
     215                 :             :                                     signature_or_mac->len);
     216                 :             : 
     217         [ -  + ]:           6 :                         TRY(verify(suite->edhoc_sign, pk,
     218                 :             :                                    (struct const_byte_array *)&sign_struct,
     219                 :             :                                    (struct const_byte_array *)signature_or_mac,
     220                 :             :                                    &result));
     221         [ -  + ]:           6 :                         if (!result) {
     222                 :           0 :                                 return signature_authentication_failed;
     223                 :             :                         }
     224                 :           6 :                         PRINT_MSG(
     225                 :             :                                 "Signature or MAC verification successful!\n");
     226                 :             :                 }
     227                 :             :         }
     228                 :          16 :         return ok;
     229                 :             : }
        

Generated by: LCOV version 2.0-1