LCOV - code coverage report
Current view: top level - powhsm/src - auth_path.c (source / functions) Hit Total Coverage
Test: powHSM firmware Lines: 22 25 88.0 %
Date: 2025-07-10 13:49:13 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /**
       2             :  * The MIT License (MIT)
       3             :  *
       4             :  * Copyright (c) 2021 RSK Labs Ltd
       5             :  *
       6             :  * Permission is hereby granted, free of charge, to any person obtaining a copy
       7             :  * of this software and associated documentation files (the "Software"), to
       8             :  * deal in the Software without restriction, including without limitation the
       9             :  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
      10             :  * sell copies of the Software, and to permit persons to whom the Software is
      11             :  * furnished to do so, subject to the following conditions:
      12             :  *
      13             :  * The above copyright notice and this permission notice shall be included in
      14             :  * all copies or substantial portions of the Software.
      15             :  *
      16             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      17             :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      18             :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      19             :  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      20             :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      21             :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
      22             :  * IN THE SOFTWARE.
      23             :  */
      24             : 
      25             : #include "hal/platform.h"
      26             : #include "hal/exceptions.h"
      27             : 
      28             : #include "auth.h"
      29             : #include "pathAuth.h"
      30             : 
      31             : #include "mem.h"
      32             : #include "memutil.h"
      33             : #include "util.h"
      34             : 
      35             : /*
      36             :  * Implement the path parsing and validation portion of the signing
      37             :  * authorization protocol.
      38             :  *
      39             :  * @arg[in] rx      number of received bytes from the host
      40             :  * @ret             number of transmited bytes to the host
      41             :  */
      42          78 : unsigned int auth_sign_handle_path(volatile unsigned int rx) {
      43          78 :     if (auth.state != STATE_AUTH_PATH)
      44           0 :         THROW(ERR_AUTH_INVALID_STATE);
      45             : 
      46          78 :     if ((rx != DATA + PATH_LEN + INPUT_INDEX_LEN) &&
      47          17 :         (rx != DATA + PATH_LEN + HASH_LENGTH))
      48           6 :         THROW(ERR_AUTH_INVALID_DATA_SIZE); // Wrong buffer size,
      49             :                                            // has to be either 28
      50             :                                            // (DATA+PATH_LEN+INPUT_INDEX_LEN) or
      51             :                                            // 56 (DATA+PATH_LEN+HASHEN)
      52             : 
      53             :     // Read derivation path
      54         144 :     SAFE_MEMMOVE(auth.path,
      55             :                  sizeof(auth.path),
      56             :                  MEMMOVE_ZERO_OFFSET,
      57             :                  APDU_DATA_PTR,
      58             :                  APDU_TOTAL_DATA_SIZE,
      59             :                  1, // Skip path length (first byte)
      60             :                  sizeof(auth.path),
      61             :                  THROW(ERR_AUTH_INVALID_DATA_SIZE));
      62             : 
      63          72 :     if (pathRequireAuth(APDU_DATA_PTR)) {
      64             :         // If path requires authorization, continue with authorization
      65          58 :         auth.auth_required = true;
      66             : 
      67          58 :         if (rx != DATA + PATH_LEN + INPUT_INDEX_LEN)
      68           0 :             THROW(ERR_AUTH_INVALID_DATA_SIZE_AUTH_SIGN);
      69             : 
      70             :         // Read input index to sign
      71         116 :         SAFE_MEMMOVE(&auth.input_index_to_sign,
      72             :                      sizeof(auth.input_index_to_sign),
      73             :                      MEMMOVE_ZERO_OFFSET,
      74             :                      APDU_DATA_PTR,
      75             :                      APDU_TOTAL_DATA_SIZE,
      76             :                      PATH_LEN,
      77             :                      INPUT_INDEX_LEN,
      78             :                      THROW(ERR_AUTH_INVALID_DATA_SIZE));
      79             : 
      80             :         // Request BTC transaction
      81          58 :         SET_APDU_OP(P1_BTC);
      82          58 :         SET_APDU_TXLEN(AUTH_MAX_EXCHANGE_SIZE);
      83          58 :         auth.expected_bytes = APDU_TXLEN();
      84          58 :         auth_transition_to(STATE_AUTH_BTCTX);
      85          58 :         return TX_FOR_TXLEN();
      86          14 :     } else if (pathDontRequireAuth(APDU_DATA_PTR)) {
      87             :         // If path doesn't require authorization,
      88             :         // go directly to signing
      89           8 :         auth.auth_required = false;
      90             : 
      91           8 :         if (rx != DATA + PATH_LEN + HASH_LENGTH)
      92           0 :             THROW(ERR_AUTH_INVALID_DATA_SIZE_UNAUTH_SIGN);
      93             : 
      94             :         // Read hash to sign
      95          16 :         SAFE_MEMMOVE(auth.sig_hash,
      96             :                      sizeof(auth.sig_hash),
      97             :                      MEMMOVE_ZERO_OFFSET,
      98             :                      APDU_DATA_PTR,
      99             :                      APDU_TOTAL_DATA_SIZE,
     100             :                      PATH_LEN,
     101             :                      sizeof(auth.sig_hash),
     102             :                      THROW(ERR_AUTH_INVALID_DATA_SIZE));
     103             : 
     104           8 :         auth_transition_to(STATE_AUTH_SIGN);
     105           8 :         return 0;
     106             :     }
     107             : 
     108             :     // If no path match, then bail out
     109             :     // signalling invalid path
     110           6 :     THROW(ERR_AUTH_INVALID_PATH);
     111             :     return 0;
     112             : }

Generated by: LCOV version 1.16