Skip to content

Commit

Permalink
Uses the SDK's version of the BLAKE2b algorithm. Moved the progressBa…
Browse files Browse the repository at this point in the history
…r array to a global variable. Version 7.5.0 release.
  • Loading branch information
NicolasFlamel1 committed Aug 17, 2024
1 parent ea7ebae commit 9201d4b
Show file tree
Hide file tree
Showing 8 changed files with 12,382 additions and 11,964 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ APP_LOAD_PARAMS += $(COMMON_LOAD_PARAMS)

# Application version
APPVERSION_M = 7
APPVERSION_N = 4
APPVERSION_P = 1
APPVERSION_N = 5
APPVERSION_P = 0
APPVERSION = "$(APPVERSION_M).$(APPVERSION_N).$(APPVERSION_P)"

# Emulator flags
Expand Down Expand Up @@ -528,6 +528,8 @@ include $(BOLOS_SDK)/Makefile.glyphs
# Compiler settings
APP_SOURCE_PATH += src
SDK_SOURCE_PATH += lib_stusb lib_stusb_impl
INCLUDES_PATH += $(BOLOS_SDK)/lib_cxng/src
APP_SOURCE_FILES += $(BOLOS_SDK)/lib_cxng/src/cx_ram.c $(BOLOS_SDK)/lib_cxng/src/cx_blake2b.c $(BOLOS_SDK)/lib_cxng/src/cx_hkdf.c

# Check if target is the Nano S
ifeq ($(TARGET_NAME),TARGET_NANOS)
Expand Down
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@ sudo apt install libc6-dev gcc-multilib g++-multilib
```
Download the Ledger Nano S SDK, Ledger Nano X SDK, Ledger Nano S Plus SDK, Ledger Stax SDK, and/or Ledger Flex SDK:
```
git clone https://github.com/LedgerHQ/nanos-secure-sdk.git
git clone https://github.com/LedgerHQ/ledger-secure-sdk.git
mv ledger-secure-sdk nanos-secure-sdk
cd nanos-secure-sdk
git checkout nanos_2.1.0
git checkout API_LEVEL_LNS
git pull
echo nanos > .target
cd ..
git clone https://github.com/LedgerHQ/ledger-secure-sdk.git
mv ledger-secure-sdk nanox-secure-sdk
cd nanox-secure-sdk
git checkout nanox_2.2.3
git checkout nanox_2.2.4
git checkout API_LEVEL_5
git pull
echo nanox > .target
Expand All @@ -30,7 +37,7 @@ cd ..
git clone https://github.com/LedgerHQ/ledger-secure-sdk.git
mv ledger-secure-sdk nanosplus-secure-sdk
cd nanosplus-secure-sdk
git checkout nanos+_1.1.1
git checkout nanos+_1.1.2
git checkout API_LEVEL_5
git pull
echo nanos2 > .target
Expand All @@ -39,17 +46,17 @@ cd ..
git clone https://github.com/LedgerHQ/ledger-secure-sdk.git
mv ledger-secure-sdk stax-secure-sdk
cd stax-secure-sdk
git checkout stax_1.4.0-rc2
git checkout API_LEVEL_15
git checkout stax_1.5.0
git checkout API_LEVEL_21
git pull
echo stax > .target
cd ..
git clone https://github.com/LedgerHQ/ledger-secure-sdk.git
mv ledger-secure-sdk flex-secure-sdk
cd flex-secure-sdk
git checkout flex_0.2.0-rc2
git checkout API_LEVEL_18
git checkout flex_1.1.1
git checkout API_LEVEL_21
git pull
echo flex > .target
cd ..
Expand Down
143 changes: 30 additions & 113 deletions src/blake2b.c
Original file line number Diff line number Diff line change
@@ -1,142 +1,59 @@
// Header files
#include <os.h>
#include <string.h>
#include "blake2b.h"
#include "common.h"


// Definitions

// Parameter reserved size
#define PARAMETER_RESERVED_SIZE 14

// Bits size
#define BITS_SIZE (32 * BITS_IN_A_BYTE)

// Parameter fanout value
#define PARAMETER_FANOUT_VALUE 1

// Parameter depth value
#define PARAMETER_DEPTH_VALUE 1


// Structures

// Parameter
struct Parameter {

// Digest length
uint8_t digestLength;

// Key length
uint8_t keyLength;

// Fanout
uint8_t fanout;

// Depth
uint8_t depth;

// Leaf length
uint32_t leafLength;

// Node offset
uint32_t nodeOffset;

// XOF length
uint32_t xofLength;

// Node depth
uint8_t nodeDepth;

// Inner length
uint8_t innerLength;

// Reserved
uint8_t reserved[PARAMETER_RESERVED_SIZE];

// Salt
uint8_t salt[BLAKE2B_SALTBYTES];

// Personal
uint8_t personal[BLAKE2B_PERSONALBYTES];
};


// Constants

// Initialization vector
static const uint64_t INITIALIZATION_VECTOR[] = {
0x6A09E667F3BCC908,
0xBB67AE8584CAA73B,
0x3C6EF372FE94F82B,
0xA54FF53A5F1D36F1,
0x510E527FADE682D1,
0x9B05688C2B3E6C1F,
0x1F83D9ABFB41BD6B,
0x5BE0CD19137E2179,
};


// Supporting function implementation

// Get BLAKE2b
void getBlake2b(volatile uint8_t *output, const size_t outputLength, const uint8_t *input, const size_t inputLength, const uint8_t *key, const size_t keyLength) {

// Initialize hash and throw error if it fails
volatile cx_blake2b_t hash;
CX_THROW(cx_blake2b_init_no_throw((cx_blake2b_t *)&hash, BITS_SIZE));

// Initialize parameter
struct Parameter parameter = {

// Digest length
.digestLength = hash.ctx.outlen,

// Key length
.keyLength = key ? keyLength : 0,

// Fan out
.fanout = PARAMETER_FANOUT_VALUE,

// Depth
.depth = PARAMETER_DEPTH_VALUE,
};

// Set hash to the initialization vector XORed with the parameter
os_xor((uint64_t *)hash.ctx.h, (void *)INITIALIZATION_VECTOR, &parameter, sizeof(hash.ctx.h));

// Initialize key block
volatile uint8_t keyBlock[BLAKE2B_BLOCKBYTES] = {0};
// Initialize state
volatile blake2b_state state;

// Begin try
BEGIN_TRY {

// Try
TRY {

// Check if a key is provided
// Check if key is provided
if(key) {

// Set key at the start of the key block
memcpy((uint8_t *)keyBlock, key, keyLength);
// Check if initializing the state with the key failed
if(blake2b_init_key((blake2b_state *)&state, outputLength, key, keyLength)) {

// Throw internal error error
THROW(INTERNAL_ERROR_ERROR);
}
}

// Otherwise
else {

// Update the hash with the block and throw error if it fails
CX_THROW(cx_hash_no_throw((cx_hash_t *)&hash.header, 0, (uint8_t *)keyBlock, sizeof(keyBlock), NULL, 0));
// Check if initializing the state failed
if(blake2b_init((blake2b_state *)&state, outputLength, NULL, 0, NULL, 0)) {

// Throw internal error error
THROW(INTERNAL_ERROR_ERROR);
}
}

// Get hash and throw error if it fails
CX_THROW(cx_hash_no_throw((cx_hash_t *)&hash.header, CX_LAST, input, inputLength, (uint8_t *)output, outputLength));
// Update state with the input
blake2b_update((blake2b_state *)&state, input, inputLength);

// Check if getting the hash from the state failed
if(blake2b_final((blake2b_state *)&state, (uint8_t *)output, outputLength) != (ssize_t)outputLength) {

// Throw internal error error
THROW(INTERNAL_ERROR_ERROR);
}
}

// Finally
FINALLY {

// Clear the key block
explicit_bzero((uint8_t *)keyBlock, sizeof(keyBlock));

// Clear the hash
explicit_bzero((cx_blake2b_t *)&hash, sizeof(hash));
// Clear the state
explicit_bzero((blake2b_state *)&state, sizeof(state));
}
}

Expand Down
1 change: 1 addition & 0 deletions src/blake2b.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


// Header files
#include <stddef.h>
#include <stdint.h>


Expand Down
3 changes: 2 additions & 1 deletion src/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

// Header files
#include <libcxng.h>
#include <../src/cx_hkdf.h>
#include <cx_blake2.h>
#include <cx_hkdf.h>

// Check if performing unit tests or fuzzing
#if defined UNIT_TESTS || defined FUZZING
Expand Down
54 changes: 22 additions & 32 deletions src/menus.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,6 @@
#define PROGRESS_BAR_HEIGHT 12
#endif

// Check if QR code image isn't defined
#ifndef C_QRCode_32px

// Define QR code image
#define C_QRCode_32px C_QRcode32px
#endif

// Check if close image isn't defined
#ifndef C_Close_32px

// Define close image
#define C_Close_32px C_cross32px
#endif


// Global variables

Expand Down Expand Up @@ -575,6 +561,17 @@ enum ResultTokens {
#endif


// Global variables

// Check if has BAGL
#ifdef HAVE_BAGL

// Progress bar
static bagl_element_t progressBar[ARRAYLEN(PROGRESS_BAR) + 1];

#endif


// Function prototypes

// Check if has BAGL
Expand Down Expand Up @@ -1159,39 +1156,32 @@ void showMenu(enum Menu menu) {
// Show progress bar
void showProgressBar(const uint8_t percent) {

// Clear the progress bar
explicit_bzero(progressBar, sizeof(progressBar));

// Include progress bar outline and text in the progress bar
memcpy(progressBar, PROGRESS_BAR, sizeof(PROGRESS_BAR));

// Get percent width
const short percentWidth = (BAGL_WIDTH - ((PROGRESS_BAR_PADDING + 1) * 2)) * percent / MAXIMUM_PROGRESS_BAR_PERCENT;

// Check if percent width exists
if(percentWidth) {

// Create progress bar with percent
bagl_element_t progressBar[ARRAYLEN(PROGRESS_BAR) + 1];
memcpy(progressBar, PROGRESS_BAR, sizeof(PROGRESS_BAR));
// Include progress bar percent in the progress bar
bagl_element_t progressBarPercent = {{BAGL_RECTANGLE, 0x00, PROGRESS_BAR_PADDING + 1, (BAGL_HEIGHT / 2) + 5 + ((percentWidth == 1) ? 1 : 0), percentWidth, PROGRESS_BAR_HEIGHT - ((percentWidth == 1) ? 2 : 0), 0, 1, BAGL_FILL, 0xFFFFFF, 0x000000, 0, 0}, NULL};
memcpy(&progressBar[ARRAYLEN(PROGRESS_BAR)], &progressBarPercent, sizeof(progressBarPercent));

// Display progress bar
UX_DISPLAY(progressBar, NULL);
}

// Otherwise
else {

// Display progress bar
bagl_element_t progressBar[ARRAYLEN(PROGRESS_BAR)];
memcpy(progressBar, PROGRESS_BAR, sizeof(PROGRESS_BAR));

// Display progress bar
UX_DISPLAY(progressBar, NULL);
}
// Display the progress bar
UX_DISPLAY(progressBar, NULL);

// Wait for display to update
UX_WAIT_DISPLAYED();
}

// Otherwise check if has NBGL
#elif defined HAVE_NBGL
// Otherwise
#else

// Show progress bar
void showProgressBar(__attribute__((unused)) const uint8_t percent) {
Expand Down
Loading

0 comments on commit 9201d4b

Please sign in to comment.