Skip to content

Commit

Permalink
fix: remove magic numbers from readCborInitial function
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilaneDen committed Jan 30, 2025
1 parent 3f708d2 commit 42ecff9
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/common/sign.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
#include "globals.h"

// CBOR encoding constants
#define CBOR_SHORT_COUNT_MAX 23 // Values below this are direct length
#define CBOR_ONE_BYTE_LENGTH 24 // Uses 1 additional byte for length
#define CBOR_TWO_BYTE_LENGTH 25 // Uses 2 additional bytes for length
#define CBOR_FOUR_BYTE_LENGTH 26 // Uses 4 additional bytes for length
#define CBOR_EIGHT_BYTE_LENGTH 27 // Uses 8 additional bytes for length
#define CBOR_INDEFINITE_LENGTH 31 // Indicates indefinite length encoding (unsupported)

// Mask and bit shifts
#define CBOR_MAJOR_TYPE_MASK 0xE0 // 3 high bits
#define CBOR_SHORT_COUNT_MASK 0x1F // 5 lower bits

static tx_state_t *tx_state = &global_tx_state;
static cborContext_t *ctx = &global.withDataBlob.cborContext;

Expand All @@ -21,7 +33,7 @@ void readCborInitial(uint8_t *cdata, uint8_t dataLength) {
// the first byte of an cbor encoding contains the type (3 high bits) and the shortCount (5
// lower bits);
ctx->majorType = header >> 5;
uint8_t shortCount = header & 0x1f;
uint8_t shortCount = header & CBOR_SHORT_COUNT_MASK;

// Calculate length of cbor payload
// sizeLength: number of bytes (beside the header) used to indicate the payload length
Expand All @@ -31,23 +43,22 @@ void readCborInitial(uint8_t *cdata, uint8_t dataLength) {

ctx->displayUsed = 0;

if (shortCount < 24) {
if (shortCount <= CBOR_SHORT_COUNT_MAX) {
// shortCount is the length, no extra bytes are used.
sizeLength = 0;
length = shortCount;
} else if (shortCount == 24) {
} else if (shortCount == CBOR_ONE_BYTE_LENGTH) {
length = cdata[0];
sizeLength = 1;
} else if (shortCount == 25) {
} else if (shortCount == CBOR_TWO_BYTE_LENGTH) {
length = U2BE(cdata, 0);
sizeLength = 2;
} else if (shortCount == 26) {
} else if (shortCount == CBOR_FOUR_BYTE_LENGTH) {
length = U4BE(cdata, 0);
sizeLength = 4;
} else if (shortCount == 27) {
} else if (shortCount == CBOR_EIGHT_BYTE_LENGTH) {
length = U8BE(cdata, 0);
sizeLength = 8;
} else if (shortCount == 31) {
} else if (shortCount == CBOR_INDEFINITE_LENGTH) {
THROW(ERROR_UNSUPPORTED_CBOR);
} else {
THROW(ERROR_INVALID_PARAM);
Expand Down

0 comments on commit 42ecff9

Please sign in to comment.