From 578d366f90bc93cb6ed2050cd0310b731b6f2a68 Mon Sep 17 00:00:00 2001 From: TransientResponse Date: Sat, 13 Dec 2014 18:54:11 -0500 Subject: [PATCH 1/5] Adapted to work on TI Launchpad TM4C1294 Still has compilation issues which require breaking compatibility with other hardware. --- Sha/sha256.cpp | 4 +-- Sha/sha256.h | 73 ++++++++++++++++++++++++-------------------------- 2 files changed, 37 insertions(+), 40 deletions(-) diff --git a/Sha/sha256.cpp b/Sha/sha256.cpp index bf61075..2a4ccdc 100644 --- a/Sha/sha256.cpp +++ b/Sha/sha256.cpp @@ -1,6 +1,6 @@ #include "sha256.h" -uint32_t sha256K[] PROGMEM = { +static const uint32_t sha256K[] = { 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174, 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da, @@ -13,7 +13,7 @@ uint32_t sha256K[] PROGMEM = { #define BUFFER_SIZE 64 -uint8_t sha256InitState[] PROGMEM = { +static const uint8_t sha256InitState[] = { 0x67,0xe6,0x09,0x6a, // H0 0x85,0xae,0x67,0xbb, // H1 0x72,0xf3,0x6e,0x3c, // H2 diff --git a/Sha/sha256.h b/Sha/sha256.h index 58df136..4300541 100644 --- a/Sha/sha256.h +++ b/Sha/sha256.h @@ -2,32 +2,29 @@ #define Sha256_h #include -#if (defined(__linux) || defined(linux)) || defined(__ARDUINO_X86__) - #define memcpy_P memcpy - #undef PROGMEM - #define PROGMEM __attribute__(( section(".progmem.data") )) - #define pgm_read_dword(p) (*(p)) - #if defined(__ARDUINO_X86__) - #include "Print.h" - #endif -#else - #include - #include - #include "Print.h" -#endif + +#define memcpy_P memcpy +#undef PROGMEM +//#define PROGMEM __attribute__(( section(".progmem.data") )) +#define PROGMEM +#define pgm_read_dword(p) (*(p)) +#include "Print.h" + #include #define HASH_LENGTH 32 #define BLOCK_LENGTH 64 -union _buffer { - uint8_t b[BLOCK_LENGTH]; - uint32_t w[BLOCK_LENGTH/4]; +union _buffer +{ + uint8_t b[BLOCK_LENGTH]; + uint32_t w[BLOCK_LENGTH / 4]; }; -union _state { - uint8_t b[HASH_LENGTH]; - uint32_t w[HASH_LENGTH/4]; +union _state +{ + uint8_t b[HASH_LENGTH]; + uint32_t w[HASH_LENGTH / 4]; }; #if (defined(__linux) || defined(linux)) && !defined(__ARDUINO_X86__) @@ -36,31 +33,31 @@ class Sha256Class class Sha256Class : public Print #endif { - public: - void init(void); - void initHmac(const uint8_t* secret, int secretLength); - uint8_t* result(void); - uint8_t* resultHmac(void); - #if (defined(__linux) || defined(linux)) && !defined(__ARDUINO_X86__) +public: + void init(void); + void initHmac(const uint8_t* secret, int secretLength); + uint8_t* result(void); + uint8_t* resultHmac(void); +#if (defined(__linux) || defined(linux)) && !defined(__ARDUINO_X86__) virtual size_t write(uint8_t); size_t write_L(const char *str); size_t write_L(const uint8_t *buffer, size_t size); size_t print(const char* str); - #else +#else virtual size_t write(uint8_t); using Print::write; - #endif - private: - void pad(); - void addUncounted(uint8_t data); - void hashBlock(); - uint32_t ror32(uint32_t number, uint8_t bits); - _buffer buffer; - uint8_t bufferOffset; - _state state; - uint32_t byteCount; - uint8_t keyBuffer[BLOCK_LENGTH]; - uint8_t innerHash[HASH_LENGTH]; +#endif +private: + void pad(); + void addUncounted(uint8_t data); + void hashBlock(); + uint32_t ror32(uint32_t number, uint8_t bits); + _buffer buffer; + uint8_t bufferOffset; + _state state; + uint32_t byteCount; + uint8_t keyBuffer[BLOCK_LENGTH]; + uint8_t innerHash[HASH_LENGTH]; }; extern Sha256Class Sha256; From 5a059b5568faefd9463b466441723f44cf169f0f Mon Sep 17 00:00:00 2001 From: TransientResponse Date: Sun, 14 Dec 2014 09:41:04 -0500 Subject: [PATCH 2/5] Now (should) work with Arduino and RPi also Just need to figure out which memory section to put the big arrays. --- Sha/sha256.h | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/Sha/sha256.h b/Sha/sha256.h index 4300541..053c09a 100644 --- a/Sha/sha256.h +++ b/Sha/sha256.h @@ -2,12 +2,25 @@ #define Sha256_h #include +#include "part.h" -#define memcpy_P memcpy -#undef PROGMEM -//#define PROGMEM __attribute__(( section(".progmem.data") )) -#define PROGMEM -#define pgm_read_dword(p) (*(p)) +#if (defined(__linux) || defined(linux)) || defined(__ARDUINO_X86__) || defined(__TM4C1294NCPDT__) + #define memcpy_P memcpy + #undef PROGMEM + #ifndef __TM4C1294NCPDT__ + #define PROGMEM __attribute__((section(".progmem.data"))) + #else + #define PROGMEM + #endif + #define pgm_read_dword(p) (*(p)) + #if defined(__ARDUINO_X86__) || defined(__TM4C1294NCPDT__) + #include "Print.h" + #endif +#else + #include + #include + #include "Print.h" +#endif #include "Print.h" From b936372208b54831f536ff26bf6678e6ca08dbb2 Mon Sep 17 00:00:00 2001 From: TransientResponse Date: Sun, 14 Dec 2014 12:08:08 -0500 Subject: [PATCH 3/5] Fixed compatibility with Arduino, RPi, Galileo Hopefully, at least --- Sha/sha256.cpp | 4 ++-- Sha/sha256.h | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Sha/sha256.cpp b/Sha/sha256.cpp index 2a4ccdc..23eb7dd 100644 --- a/Sha/sha256.cpp +++ b/Sha/sha256.cpp @@ -1,6 +1,6 @@ #include "sha256.h" -static const uint32_t sha256K[] = { +static const uint32_t sha256K[] PROGMEM = { 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174, 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da, @@ -13,7 +13,7 @@ static const uint32_t sha256K[] = { #define BUFFER_SIZE 64 -static const uint8_t sha256InitState[] = { +static const uint8_t sha256InitState[] PROGMEM = { 0x67,0xe6,0x09,0x6a, // H0 0x85,0xae,0x67,0xbb, // H1 0x72,0xf3,0x6e,0x3c, // H2 diff --git a/Sha/sha256.h b/Sha/sha256.h index 053c09a..58d9506 100644 --- a/Sha/sha256.h +++ b/Sha/sha256.h @@ -2,7 +2,10 @@ #define Sha256_h #include -#include "part.h" + +#if !defined(__linux) || !defined(linux) + #include "Arduino.h" //To bring in part.h for Energia +#endif #if (defined(__linux) || defined(linux)) || defined(__ARDUINO_X86__) || defined(__TM4C1294NCPDT__) #define memcpy_P memcpy @@ -10,7 +13,7 @@ #ifndef __TM4C1294NCPDT__ #define PROGMEM __attribute__((section(".progmem.data"))) #else - #define PROGMEM + #define PROGMEM __attribute__((section("flash"))) #endif #define pgm_read_dword(p) (*(p)) #if defined(__ARDUINO_X86__) || defined(__TM4C1294NCPDT__) @@ -21,8 +24,6 @@ #include #include "Print.h" #endif -#include "Print.h" - #include From f91892aacc43867cec0848b019e8d8722bfe6732 Mon Sep 17 00:00:00 2001 From: TransientResponse Date: Mon, 15 Dec 2014 16:11:40 -0500 Subject: [PATCH 4/5] Fixed memory location for PROGMEM "flash" section not supported correctly, using ".text" instead. --- Sha/sha256.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sha/sha256.h b/Sha/sha256.h index 58d9506..8a8817b 100644 --- a/Sha/sha256.h +++ b/Sha/sha256.h @@ -13,7 +13,7 @@ #ifndef __TM4C1294NCPDT__ #define PROGMEM __attribute__((section(".progmem.data"))) #else - #define PROGMEM __attribute__((section("flash"))) + #define PROGMEM __attribute__((section(".text"))) #endif #define pgm_read_dword(p) (*(p)) #if defined(__ARDUINO_X86__) || defined(__TM4C1294NCPDT__) @@ -41,7 +41,7 @@ union _state uint32_t w[HASH_LENGTH / 4]; }; -#if (defined(__linux) || defined(linux)) && !defined(__ARDUINO_X86__) +#if (defined(__linux) || defined(linux)) && !defined(__ARDUINO_X86__) && !defined(__TM4C1294NCPDT__) class Sha256Class #else class Sha256Class : public Print @@ -52,7 +52,7 @@ class Sha256Class : public Print void initHmac(const uint8_t* secret, int secretLength); uint8_t* result(void); uint8_t* resultHmac(void); -#if (defined(__linux) || defined(linux)) && !defined(__ARDUINO_X86__) +#if (defined(__linux) || defined(linux)) && !defined(__ARDUINO_X86__) && !defined(__TM4C1294NCPDT__) virtual size_t write(uint8_t); size_t write_L(const char *str); size_t write_L(const uint8_t *buffer, size_t size); From c458aa370728f024dcd4b79dd8d513e6a1b0aa17 Mon Sep 17 00:00:00 2001 From: TransientResponse Date: Mon, 15 Dec 2014 19:20:15 -0500 Subject: [PATCH 5/5] Ported changes to SHA1 Also, result functions no longer return raw pointers to internal state. --- Sha/sha1.cpp | 15 +++++++++------ Sha/sha1.h | 21 +++++++++++++++------ Sha/sha256.cpp | 13 ++++++++----- Sha/sha256.h | 4 ++-- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/Sha/sha1.cpp b/Sha/sha1.cpp index 9961fbe..1d944d5 100644 --- a/Sha/sha1.cpp +++ b/Sha/sha1.cpp @@ -5,7 +5,7 @@ #define SHA1_K40 0x8f1bbcdc #define SHA1_K60 0xca62c1d6 -uint8_t sha1InitState[] PROGMEM = { +static const uint8_t sha1InitState[] PROGMEM = { 0x01,0x23,0x45,0x67, // H0 0x89,0xab,0xcd,0xef, // H1 0xfe,0xdc,0xba,0x98, // H2 @@ -94,7 +94,8 @@ void Sha1Class::pad() { } -uint8_t* Sha1Class::result(void) { +void Sha1Class::result(uint8_t* out) +{ // Pad to complete the last block pad(); @@ -109,8 +110,8 @@ uint8_t* Sha1Class::result(void) { state.w[i]=b; } - // Return pointer to hash (20 characters) - return state.b; + // Copy hash result to output buffer + memcpy(out, state.b, HASH_LENGTH); } #define HMAC_IPAD 0x36 @@ -135,7 +136,8 @@ void Sha1Class::initHmac(const uint8_t* key, int keyLength) { } } -uint8_t* Sha1Class::resultHmac(void) { +void Sha1Class::resultHmac(uint8_t* out) +{ uint8_t i; // Complete inner hash memcpy(innerHash,result(),HASH_LENGTH); @@ -143,7 +145,8 @@ uint8_t* Sha1Class::resultHmac(void) { init(); for (i=0; i -#if (defined(__linux) || defined(linux)) || defined(__ARDUINO_X86__) + +#if !defined(__linux) || !defined(linux) + #include "Arduino.h" //To bring in part.h for Energia +#endif + +#if (defined(__linux) || defined(linux)) || defined(__ARDUINO_X86__) || defined(__TM4C1294NCPDT__) #define memcpy_P memcpy #undef PROGMEM - #define PROGMEM __attribute__(( section(".progmem.data") )) + #ifndef __TM4C1294NCPDT__ + #define PROGMEM __attribute__((section(".progmem.data"))) + #else + #define PROGMEM __attribute__((section(".text"))) + #endif #define pgm_read_dword(p) (*(p)) - #if defined(__ARDUINO_X86__) + #if defined(__ARDUINO_X86__) || defined(__TM4C1294NCPDT__) #include "Print.h" - #endif + #endif #else #include #include @@ -38,8 +47,8 @@ class Sha1Class : public Print public: void init(void); void initHmac(const uint8_t* secret, int secretLength); - uint8_t* result(void); - uint8_t* resultHmac(void); + void result(uint8_t*); + void resultHmac(uint8_t*); #if (defined(__linux) || defined(linux)) && !defined(__ARDUINO_X86__) virtual size_t write(uint8_t); size_t write_L(const char *str); diff --git a/Sha/sha256.cpp b/Sha/sha256.cpp index 23eb7dd..46b7f1f 100644 --- a/Sha/sha256.cpp +++ b/Sha/sha256.cpp @@ -109,7 +109,7 @@ void Sha256Class::pad() { } -uint8_t* Sha256Class::result(void) { +void Sha256Class::result(uint8_t* out) { // Pad to complete the last block pad(); @@ -124,8 +124,8 @@ uint8_t* Sha256Class::result(void) { state.w[i]=b; } - // Return pointer to hash (20 characters) - return state.b; + // Copy hash result to output buffer + memcpy(out, state.b, HASH_LENGTH); } #define HMAC_IPAD 0x36 @@ -153,7 +153,8 @@ void Sha256Class::initHmac(const uint8_t* key, int keyLength) { } } -uint8_t* Sha256Class::resultHmac(void) { +void Sha256Class::resultHmac(uint8_t* out) +{ uint8_t i; // Complete inner hash memcpy(innerHash,result(),HASH_LENGTH); @@ -161,8 +162,10 @@ uint8_t* Sha256Class::resultHmac(void) { init(); for (i=0; i