Skip to content

Commit

Permalink
chore: slight refactor of crypto lib for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
xtruan committed Aug 10, 2023
1 parent b81adf6 commit 75f3c96
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lib/crypto/bip32.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
#include <string.h>

#include "address.h"
#if USE_NEM
#include "aes/aes.h"
#endif
#include "base58.h"
#include "bignum.h"
#include "bip32.h"
Expand Down
1 change: 1 addition & 0 deletions lib/crypto/memzero.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void memzero(void* const pnt, const size_t len) {
SecureZeroMemory(pnt, len);
#elif defined(HAVE_MEMSET_S)
memset_s(pnt, (rsize_t)len, 0, (rsize_t)len);
// REMOVED - Flipper Zero does not have this function
// #elif defined(HAVE_EXPLICIT_BZERO)
// explicit_bzero(pnt, len);
#elif defined(HAVE_EXPLICIT_MEMSET)
Expand Down
7 changes: 6 additions & 1 deletion lib/crypto/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,14 @@
#define USE_KECCAK 1
#endif

// add way how to mark confidential data
// add a way to mark confidential data
#ifndef CONFIDENTIAL
#define CONFIDENTIAL
#endif

// use Flipper Zero hardware random number generator
#ifndef USE_FLIPPER_HAL_RANDOM
#define USE_FLIPPER_HAL_RANDOM 1
#endif

#endif
10 changes: 5 additions & 5 deletions lib/crypto/rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
* OTHER DEALINGS IN THE SOFTWARE.
*/

#define FLIPPER_HAL_RANDOM

#include "rand.h"

#ifdef FLIPPER_HAL_RANDOM
#if USE_FLIPPER_HAL_RANDOM

// NOTE:
// random32() and random_buffer() have been replaced in this implementation
Expand Down Expand Up @@ -67,14 +65,16 @@ void random_buffer(uint8_t* buf, size_t len) {
// The following code is platform independent
//

static uint32_t seed = 0;

uint32_t random32(void) {
// Linear congruential generator from Numerical Recipes
// https://en.wikipedia.org/wiki/Linear_congruential_generator
seed = 1664525 * seed + 1013904223;
return seed;
}

void __attribute__((weak)) random_buffer(uint8_t *buf, size_t len) {
void random_buffer(uint8_t *buf, size_t len) {
uint32_t r = 0;
for (size_t i = 0; i < len; i++) {
if (i % 4 == 0) {
Expand All @@ -84,7 +84,7 @@ void __attribute__((weak)) random_buffer(uint8_t *buf, size_t len) {
}
}

#endif /* FLIPPER_HAL_RANDOM */
#endif /* USE_FLIPPER_HAL_RANDOM */

uint32_t random_uniform(uint32_t n) {
uint32_t x = 0, max = 0xFFFFFFFF - (0xFFFFFFFF % n);
Expand Down
1 change: 1 addition & 0 deletions lib/crypto/rand.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <stdint.h>
#include <stdlib.h>
#include "options.h"

void random_reseed(const uint32_t value);
uint32_t random32(void);
Expand Down

0 comments on commit 75f3c96

Please sign in to comment.