Просмотр исходного кода

chore: slight refactor of crypto lib for consistency

Struan Clark 2 лет назад
Родитель
Сommit
75f3c96014
5 измененных файлов с 15 добавлено и 6 удалено
  1. 2 0
      lib/crypto/bip32.c
  2. 1 0
      lib/crypto/memzero.c
  3. 6 1
      lib/crypto/options.h
  4. 5 5
      lib/crypto/rand.c
  5. 1 0
      lib/crypto/rand.h

+ 2 - 0
lib/crypto/bip32.c

@@ -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"

+ 1 - 0
lib/crypto/memzero.c

@@ -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)

+ 6 - 1
lib/crypto/options.h

@@ -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

+ 5 - 5
lib/crypto/rand.c

@@ -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
@@ -67,6 +65,8 @@ 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
@@ -74,7 +74,7 @@ uint32_t random32(void) {
   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) {
@@ -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);

+ 1 - 0
lib/crypto/rand.h

@@ -26,6 +26,7 @@
 
 #include <stdint.h>
 #include <stdlib.h>
+#include "options.h"
 
 void random_reseed(const uint32_t value);
 uint32_t random32(void);