cryptnox-sdk-arduino 1.0.0
Arduino library for Cryptnox Hardware Wallet
Loading...
Searching...
No Matches
util.h File Reference
#include <stdint.h>
#include <stddef.h>
Include dependency graph for util.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

uint32_t RlpEncodeWholeHeader (uint8_t *header_output, size_t header_cap, uint32_t total_len)
 Encodes the total length into an RLP list header.
uint32_t RlpEncodeItem (uint8_t *output, size_t output_cap, const uint8_t *input, uint32_t input_len)
 Encodes a single item in RLP format.
uint32_t ConvertNumberToUintArray (uint8_t *str, uint64_t val)
 Converts an unsigned integer to a big-endian byte array.
size_t trimLeadingZeros (uint8_t *out, size_t out_cap, const uint8_t *in, size_t in_len)
 Removes leading zeros from a byte array.
int fromHex (char c)
 Convert a hexadecimal character to a byte value.
bool hexToBytes (const char *hex, uint8_t *out, size_t len)
 Convert a hex string to a byte array.

Function Documentation

◆ ConvertNumberToUintArray()

uint32_t ConvertNumberToUintArray ( uint8_t * str,
uint64_t val )

Converts an unsigned integer to a big-endian byte array.

NEW-5: widened from uint32_t to uint64_t so that 64-bit Ethereum fields (nonce, gasLimit, value, maxFeePerGas, maxPriorityFeePerGas) are not silently truncated. Returns up to 8 bytes; str must be >= 8 bytes.

Parameters
[out]strPointer to output buffer (at least 8 bytes).
[in]valUnsigned 64-bit integer to convert.
Returns
Number of bytes used in the output array (1..8).

Converts an unsigned integer to a big-endian byte array.

NEW-5: widened to uint64_t. Previously truncated Tx2 uint64 fields (nonce/fees/gasLimit/value) at 4 bytes, producing wrong-amount tx.

Parameters
[out]strOutput buffer (at least 8 bytes).
[in]valUnsigned 64-bit integer to convert.
Returns
Number of bytes written to the buffer (1..8).

Definition at line 190 of file util.cpp.

Referenced by rlpEncodeTxBody().

◆ fromHex()

int fromHex ( char c)

Convert a hexadecimal character to a byte value.

Returns -1 on any non-hex character so callers can distinguish a true '0' from a parse error. Previously this returned 0 on invalid input, causing silent data corruption (security audit M-07).

Parameters
cHex character.
Returns
0..15 for valid hex digits, -1 otherwise.

M-07: returns -1 on any non-hex character so callers can distinguish a true '0' from a parse error.

Definition at line 15 of file util.cpp.

Referenced by fetchNonce(), and hexToBytes().

◆ hexToBytes()

bool hexToBytes ( const char * hex,
uint8_t * out,
size_t len )

Convert a hex string to a byte array.

Validates that hex contains at least 2*len characters before any read, to prevent out-of-bounds reads on malformed / truncated RPC responses (security audit H-03).

Parameters
hexInput hex string (must be NUL-terminated).
outOutput byte array (caller-allocated, at least len bytes).
lenNumber of bytes to write to out.
Returns
true on success, false if hex is too short or NULL.

H-03: validate input length before any read. Without this guard a truncated or malicious RPC response shorter than 2*len would cause out-of-bounds reads past the NUL terminator.

Returns
true on success, false on NULL input or insufficient length.

Definition at line 32 of file util.cpp.

References fromHex().

Referenced by encodeERC20Transfer(), and rlpEncodeTxBody().

◆ RlpEncodeItem()

uint32_t RlpEncodeItem ( uint8_t * output,
size_t output_cap,
const uint8_t * input,
uint32_t input_len )

Encodes a single item in RLP format.

Bounds-checked: returns 0 (and does not write) if the encoded item would not fit in output_cap bytes.

Parameters
[out]outputPointer to buffer to store the RLP-encoded item.
[in]output_capCapacity of output in bytes.
[in]inputPointer to the data to encode.
[in]input_lenLength of the input data in bytes.
Returns
Number of bytes written into output, or 0 on overflow.

Encodes a single item in RLP format.

Parameters
[out]outputBuffer where the encoded RLP item will be written.
[in]inputInput data to encode.
[in]input_lenLength of input data.
Returns
Number of bytes written to output.

Definition at line 123 of file util.cpp.

Referenced by rlpEncodeSignedTx().

◆ RlpEncodeWholeHeader()

uint32_t RlpEncodeWholeHeader ( uint8_t * header_output,
size_t header_cap,
uint32_t total_len )

Encodes the total length into an RLP list header.

Bounds-checked: returns 0 (and does not write) if the encoded header would not fit in header_cap bytes.

Parameters
[out]header_outputPointer to buffer to store the RLP header.
[in]header_capCapacity of header_output in bytes.
[in]total_lenTotal length of the RLP list content.
Returns
Number of bytes written into header_output, or 0 on overflow.

Encodes the total length into an RLP list header.

This function generates the RLP "whole header" for a list payload of length total_len, according to Ethereum RLP specification.

RLP encoding rules:

  • For a list with total payload < 55 bytes: 0xC0 + total_len (single-byte header)
  • For a list with payload >= 55 bytes: 0xF7 + length_of_length_field, followed by the big-endian encoded total_len

Example: total_len = 10 → header = C0 + 0x0A → C0 0A total_len = 100 → total_len = 0x64 (1 byte) → F8 64 total_len = 1024 → total_len = 0x0400 (2 bytes) → F9 04 00

This header is intended to be placed immediately before the concatenated items in an RLP list. For EIP-1559 typed transactions, it must appear after the type byte (0x02).

Parameters
[out]header_outputPointer where the encoded header will be written.
[in]total_lenLength in bytes of all list items concatenated.
Returns
Number of bytes written to header_output.

Definition at line 76 of file util.cpp.

Referenced by rlpFinalize().

◆ trimLeadingZeros()

size_t trimLeadingZeros ( uint8_t * out,
size_t out_cap,
const uint8_t * in,
size_t in_len )

Removes leading zeros from a byte array.

Bounds-checked: returns 0 if out_cap is smaller than the trimmed length. (Trimmed length is always <= in_len.)

Parameters
[out]outPointer to buffer to store trimmed result.
[in]out_capCapacity of out in bytes.
[in]inPointer to input byte array.
[in]in_lenLength of input array.
Returns
Number of bytes written into out, or 0 on overflow.

Removes leading zeros from a byte array.

Parameters
[out]outOutput buffer.
[in]inInput buffer.
[in]in_lenLength of input buffer.
Returns
Number of bytes written to the output buffer.

Definition at line 220 of file util.cpp.

Referenced by rlpEncodeSignedTx().