-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Factored out seed operations - Added unit tests for seed module - Removed unused parameters from unlock and pin modules
- Loading branch information
1 parent
43b457d
commit fe0dc79
Showing
12 changed files
with
495 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/** | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2021 RSK Labs Ltd | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to | ||
* deal in the Software without restriction, including without limitation the | ||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
* sell copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
* IN THE SOFTWARE. | ||
*/ | ||
|
||
#include <string.h> | ||
|
||
#include "apdu.h" | ||
#include "defs.h" | ||
#include "err.h" | ||
#include "os.h" | ||
#include "seed.h" | ||
|
||
/* | ||
* Init the given seed context to point to a target buffer | ||
* | ||
* @arg[out] seed_ctx seed context | ||
* @arg[in] words_buffer pointer to the words buffer | ||
* @arg[in] words_buffer_size words_buffer size in bytes | ||
* @arg[in] seed_buffer pointer to the seed buffer | ||
* @arg[in] seed_buffer_size seed_buffer size in bytes | ||
*/ | ||
void init_seed_ctx(seed_t* seed_ctx, | ||
char* words_buffer, | ||
size_t words_buffer_size, | ||
char* seed_buffer, | ||
size_t seed_buffer_size, | ||
mnemonic_from_data_callback_t callback) { | ||
seed_ctx->words_buffer = words_buffer; | ||
seed_ctx->words_buffer_size = words_buffer_size; | ||
seed_ctx->seed_buffer = seed_buffer; | ||
seed_ctx->seed_buffer_size = seed_buffer_size; | ||
seed_ctx->mnemonic_from_data_callback = callback; | ||
} | ||
|
||
/* | ||
* Reset the given seed context | ||
* | ||
* @arg[out] seed_ctx seed context | ||
*/ | ||
void reset_seed_ctx(seed_t* seed_ctx) { | ||
explicit_bzero(seed_ctx, sizeof(seed_t)); | ||
} | ||
|
||
/* | ||
* Generates the mnemonic from an initial seed. The seed should be in | ||
* words_buffer before calling this function. The mnemonic will be saved | ||
* on words_buffer and words_buffer_size will be set accordingly | ||
* | ||
* @arg[out] seed_ctx seed context | ||
*/ | ||
void generate_mnemonic(seed_t* seed_ctx) { | ||
volatile int i; | ||
// Generate 32 bytes of random with onboard rng | ||
cx_rng((unsigned char*)seed_ctx->seed_buffer, HASHSIZE); | ||
// XOR with host-generated 32 bytes random | ||
for (i = 0; i < HASHSIZE; i++) { | ||
seed_ctx->seed_buffer[i] ^= seed_ctx->words_buffer[i]; | ||
} | ||
// The seed is now in seed_buffer, generate the mnemonic | ||
os_memset(seed_ctx->words_buffer, 0, seed_ctx->seed_buffer_size); | ||
seed_ctx->words_buffer_size = seed_ctx->mnemonic_from_data_callback( | ||
(unsigned char*)seed_ctx->seed_buffer, | ||
SEEDSIZE, | ||
(unsigned char*)seed_ctx->words_buffer, | ||
seed_ctx->words_buffer_size); | ||
// Clear the seed | ||
explicit_bzero(seed_ctx->seed_buffer, seed_ctx->seed_buffer_size); | ||
} | ||
|
||
/* | ||
* Wipes the contents of words_buffer | ||
* | ||
* @arg[out] seed_ctx seed context | ||
*/ | ||
void clear_mnemonic(seed_t* seed_ctx) { | ||
explicit_bzero(seed_ctx->words_buffer, seed_ctx->words_buffer_size); | ||
} | ||
|
||
/* | ||
* Implement the RSK SEED command. | ||
* | ||
* Receives one byte at a time and fills words_buffer in seed_ctx. | ||
* | ||
* @arg[in] rx number of received bytes from the Host | ||
* @arg[out] seed_ctx seed context | ||
* @ret number of transmited bytes to the host | ||
*/ | ||
unsigned int update_words_buffer(volatile unsigned int rx, seed_t* seed_ctx) { | ||
// Should receive 1 byte per call | ||
if (APDU_DATA_SIZE(rx) != 1) { | ||
THROW(PROT_INVALID); | ||
} | ||
|
||
unsigned char index = APDU_OP(); | ||
if ((index >= 0) && ((size_t)index <= seed_ctx->words_buffer_size)) { | ||
seed_ctx->words_buffer[index] = APDU_AT(3); | ||
} | ||
|
||
// No bytes transmited to host | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2021 RSK Labs Ltd | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to | ||
* deal in the Software without restriction, including without limitation the | ||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
* sell copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
* IN THE SOFTWARE. | ||
*/ | ||
|
||
#ifndef __SEED | ||
#define __SEED | ||
|
||
typedef unsigned int (*mnemonic_from_data_callback_t)(unsigned char* in, | ||
unsigned int in_length, | ||
unsigned char* out, | ||
unsigned int out_length); | ||
|
||
// Seed context | ||
typedef struct { | ||
// Buffer used to hold the initial seed and the generated mnemominc | ||
char* words_buffer; | ||
// words_buffer size in bytes | ||
size_t words_buffer_size; | ||
// Temporary buffer used in seed generation | ||
char* seed_buffer; | ||
// seed_buffer size in bytes | ||
size_t seed_buffer_size; | ||
// callback function for mnemonic generation | ||
mnemonic_from_data_callback_t mnemonic_from_data_callback; | ||
} seed_t; | ||
|
||
/* | ||
* Init the given seed context to point to a target buffer | ||
* | ||
* @arg[out] seed_ctx seed context | ||
* @arg[in] words_buffer pointer to the words buffer | ||
* @arg[in] words_buffer_size words_buffer size in bytes | ||
* @arg[in] seed_buffer pointer to the seed buffer | ||
* @arg[in] seed_buffer_size seed_buffer size in bytes | ||
*/ | ||
void init_seed_ctx(seed_t* seed_ctx, | ||
char* words_buffer, | ||
size_t words_buffer_size, | ||
char* string_buffer, | ||
size_t string_buffer_size, | ||
mnemonic_from_data_callback_t callback); | ||
|
||
/* | ||
* Reset the given seed context | ||
* | ||
* @arg[in] seed_ctx seed context | ||
*/ | ||
void reset_seed_ctx(seed_t* seed_ctx); | ||
|
||
/* | ||
* Generates the mnemonic from an initial seed. The seed should be in | ||
* words_buffer before calling this function. The mnemonic will be saved | ||
* on words_buffer and words_buffer_size will be set accordingly | ||
* | ||
* @arg[out] seed_ctx seed context | ||
*/ | ||
void generate_mnemonic(seed_t* seed_ctx); | ||
void clear_mnemonic(seed_t* seed_ctx); | ||
|
||
/* | ||
* Implement the RSK SEED command. | ||
* | ||
* Receives one byte at a time and fills words_buffer in seed_ctx. | ||
* | ||
* @arg[in] rx number of received bytes from the Host | ||
* @arg[out] seed_ctx seed context | ||
* @ret number of transmited bytes to the host | ||
*/ | ||
unsigned int update_words_buffer(volatile unsigned int rx, seed_t* seed_ctx); | ||
|
||
#endif |
Oops, something went wrong.