├── .gitignore ├── LICENSE ├── README.md ├── examples ├── Arduino_Code │ └── Arduino_Code.ino └── ESP8266_Code │ └── ESP8266_Code.ino ├── keywords.txt ├── library.properties └── src ├── Ducos1a.cpp ├── Ducos1a.h ├── DuinoCoin.cpp ├── DuinoCoin.h ├── DuinoCoinWifi.cpp ├── DuinoCoinWifi.h ├── avr ├── Ducos1a_avr.h ├── backend.cpp ├── config.h ├── sha1.cpp ├── sha1.h └── sha1 │ ├── basic.h │ ├── constants.c │ ├── constants.h │ ├── default.h │ ├── hash.c │ ├── hash.h │ ├── sha1.c │ ├── sha1.h │ ├── types.c │ └── types.h ├── esp ├── Ducos1a_esp.h └── Hash.h ├── esp32 └── Ducos1a_esp32.h └── hash ├── Ducos1a_hash.h └── Hash ├── LICENSE ├── README.md ├── library.properties └── src ├── Hash.cpp ├── Hash.h └── sha1 ├── sha1.c └── sha1.h /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | # vscode 35 | *.vscode 36 | 37 | # old 38 | old/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Luiz Henrique Cassettari 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # arduino-DuinoCoin 2 | 3 | Code for Arduino boards v2.4 4 | 5 | © Duino-Coin Community 2019-2021 6 | 7 | Distributed under MIT License 8 | 9 | * https://github.com/revoxhere/duino-coin 10 | 11 | ## Library Dependency 12 | 13 | The library uses the [`ArduinoUniqueID`][0] library. 14 | 15 | # Installation 16 | 17 | * Install the library by [Using the Library Manager](https://www.arduino.cc/en/Guide/Libraries#toc3) 18 | * **OR** by [Importing the .zip library](https://www.arduino.cc/en/Guide/Libraries#toc4) using either the [master](https://github.com/ricaun/arduino-DuinoCoin/archive/1.0.0.zip) or one of the [releases](https://github.com/ricaun/arduino-DuinoCoin/releases) ZIP files. 19 | 20 | ## Examples 21 | 22 | The library comes with [examples](examples). After installing the library you need to restart the Arduino IDE before they can be found under **File > Examples > arduino-DuinoCoin**. 23 | 24 | --- 25 | 26 | # Reference 27 | 28 | ## Include Library 29 | 30 | ```c 31 | #include 32 | ``` 33 | 34 | ```c 35 | DuinoCoin DuinoCoin(Serial); 36 | ``` 37 | 38 | ## Method:begin 39 | 40 | ```c 41 | void setup() { 42 | DuinoCoin.begin(); 43 | } 44 | ``` 45 | 46 | ## Method:loop 47 | 48 | ```c 49 | void loop() { 50 | if (DuinoCoin.loop()) 51 | { 52 | // Work Done 53 | } 54 | } 55 | ``` 56 | 57 | --- 58 | 59 | Do you like this library? Please [star this project on GitHub](https://github.com/ricaun/arduino-DuinoCoin/stargazers)! 60 | 61 | [0]: https://github.com/ricaun/ArduinoUniqueID -------------------------------------------------------------------------------- /examples/Arduino_Code/Arduino_Code.ino: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////// 2 | // _____ _ _____ _ 3 | // | __ \ (_) / ____| (_) 4 | // | | | |_ _ _ _ __ ___ ______| | ___ _ _ __ 5 | // | | | | | | | | '_ \ / _ \______| | / _ \| | '_ \ 6 | // | |__| | |_| | | | | | (_) | | |___| (_) | | | | | 7 | // |_____/ \__,_|_|_| |_|\___/ \_____\___/|_|_| |_| 8 | // Code for Arduino boards v2.4 9 | // © Duino-Coin Community 2019-2021 10 | // Distributed under MIT License 11 | ////////////////////////////////////////////////////////// 12 | // https://github.com/revoxhere/duino-coin - GitHub 13 | // https://duinocoin.com - Official Website 14 | // https://discord.gg/k48Ht5y - Discord 15 | ////////////////////////////////////////////////////////// 16 | // If you don't know what to do, visit official website 17 | // and navigate to Getting Started page. Happy mining! 18 | ////////////////////////////////////////////////////////// 19 | 20 | #include 21 | 22 | DuinoCoin DuinoCoin(Serial); 23 | 24 | void setup() { 25 | pinMode(LED_BUILTIN, OUTPUT); // Prepare built-in led pin as output 26 | DuinoCoin.begin(); 27 | } 28 | 29 | void loop() { 30 | if (DuinoCoin.loop()) 31 | { 32 | digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); 33 | delay(25); // Wait a bit 34 | digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /examples/ESP8266_Code/ESP8266_Code.ino: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////// 2 | // _____ _ _____ _ 3 | // | __ \ (_) / ____| (_) 4 | // | | | |_ _ _ _ __ ___ ______| | ___ _ _ __ 5 | // | | | | | | | | '_ \ / _ \______| | / _ \| | '_ \ 6 | // | |__| | |_| | | | | | (_) | | |___| (_) | | | | | 7 | // |_____/ \__,_|_|_| |_|\___/ \_____\___/|_|_| |_| 8 | // Code for ESP8266 boards - V2.4.5 9 | // © Duino-Coin Community 2019-2021 10 | // Distributed under MIT License 11 | ////////////////////////////////////////////////////////// 12 | // https://github.com/revoxhere/duino-coin - GitHub 13 | // https://duinocoin.com - Official Website 14 | ////////////////////////////////////////////////////////// 15 | // If you don't know what to do, visit official website 16 | // and navigate to Getting Started page. Happy mining! 17 | ////////////////////////////////////////////////////////// 18 | 19 | #include // Include WiFi library 20 | #include // OTA libraries 21 | #include 22 | #include 23 | #include 24 | 25 | namespace 26 | { 27 | const char *ssid = "WiFi SSID"; // Change this to your WiFi SSID 28 | const char *password = "WiFi Pass"; // Change this to your WiFi password 29 | const char *ducouser = "DUCO Username"; // Change this to your Duino-Coin username 30 | const char *rigIdentifier = "None"; // Change this if you want a custom miner name 31 | 32 | const char *host = "51.15.127.80"; // Static server IP 33 | const int port = 2811; 34 | unsigned int Shares = 0; // Share variable 35 | 36 | String clientBuffer = ""; 37 | 38 | WiFiClient client; 39 | 40 | #define LED_BUILTIN 2 41 | 42 | #define BLINK_SHARE_FOUND 1 43 | #define BLINK_SETUP_COMPLETE 2 44 | #define BLINK_CLIENT_CONNECT 3 45 | #define BLINK_RESET_DEVICE 5 46 | 47 | #define END_TOKEN '\n' 48 | #define SEP_TOKEN ',' 49 | 50 | void SetupWifi() 51 | { 52 | pinMode(LED_BUILTIN, OUTPUT); 53 | Serial.println("Connecting to: " + String(ssid)); 54 | WiFi.mode(WIFI_STA); // Setup ESP in client mode 55 | WiFi.begin(ssid, password); // Connect to wifi 56 | 57 | while (WiFi.status() != WL_CONNECTED) 58 | { 59 | delay(500); 60 | Serial.print("."); 61 | } 62 | 63 | Serial.println("\nConnected to WiFi!"); 64 | Serial.println("Local IP address: " + WiFi.localIP().toString()); 65 | } 66 | 67 | void SetupOTA() 68 | { 69 | ArduinoOTA.onStart([]() { // Prepare OTA stuff 70 | Serial.println("Start"); 71 | }); 72 | ArduinoOTA.onEnd([]() { 73 | Serial.println("\nEnd"); 74 | }); 75 | ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { 76 | Serial.printf("Progress: %u%%\r", (progress / (total / 100))); 77 | }); 78 | ArduinoOTA.onError([](ota_error_t error) { 79 | Serial.printf("Error[%u]: ", error); 80 | if (error == OTA_AUTH_ERROR) 81 | Serial.println("Auth Failed"); 82 | else if (error == OTA_BEGIN_ERROR) 83 | Serial.println("Begin Failed"); 84 | else if (error == OTA_CONNECT_ERROR) 85 | Serial.println("Connect Failed"); 86 | else if (error == OTA_RECEIVE_ERROR) 87 | Serial.println("Receive Failed"); 88 | else if (error == OTA_END_ERROR) 89 | Serial.println("End Failed"); 90 | }); 91 | ArduinoOTA.begin(); 92 | } 93 | 94 | void blink(uint8_t count) 95 | { 96 | for (int x = 0; x < count; ++x) 97 | { 98 | digitalWrite(LED_BUILTIN, LOW); 99 | delay(50); 100 | digitalWrite(LED_BUILTIN, HIGH); 101 | delay(50); 102 | } 103 | yield(); // register updates can take several cpu cycles; 104 | } 105 | 106 | void RestartESP(String msg) 107 | { 108 | Serial.println(msg); 109 | Serial.println("Resetting ESP..."); 110 | blink(BLINK_RESET_DEVICE); 111 | ESP.reset(); 112 | } 113 | 114 | void handleSystemEvents(void) 115 | { 116 | ArduinoOTA.handle(); 117 | yield(); 118 | } 119 | 120 | // https://stackoverflow.com/questions/9072320/split-string-into-string-array 121 | String getValue(String data, char separator, int index) 122 | { 123 | int found = 0; 124 | int strIndex[] = {0, -1}; 125 | int maxIndex = data.length() - 1; 126 | 127 | for (int i = 0; i <= maxIndex && found <= index; i++) 128 | { 129 | if (data.charAt(i) == separator || i == maxIndex) 130 | { 131 | found++; 132 | strIndex[0] = strIndex[1] + 1; 133 | strIndex[1] = (i == maxIndex) ? i + 1 : i; 134 | } 135 | } 136 | 137 | return found > index ? data.substring(strIndex[0], strIndex[1]) : ""; 138 | } 139 | 140 | void waitForClientData(void) 141 | { 142 | clientBuffer = ""; 143 | 144 | while (client.connected()) 145 | { 146 | if (client.available()) 147 | { 148 | clientBuffer = client.readStringUntil(END_TOKEN); 149 | if (clientBuffer.length() == 1 && clientBuffer[0] == END_TOKEN) 150 | clientBuffer = "???\n"; // NOTE: Should never happen... 151 | 152 | break; 153 | } 154 | handleSystemEvents(); 155 | } 156 | } 157 | 158 | void ConnectToServer() 159 | { 160 | if (client.connected()) 161 | return; 162 | 163 | Serial.println("\nConnecting to Duino-Coin server..."); 164 | if (!client.connect(host, port)) 165 | RestartESP("Connection failed."); 166 | 167 | waitForClientData(); 168 | blink(BLINK_CLIENT_CONNECT); // Blink 3 times - indicate sucessfull connection with the server 169 | } 170 | 171 | bool max_micros_elapsed(unsigned long current, unsigned long max_elapsed) 172 | { 173 | static unsigned long _start = 0; 174 | 175 | if ((current - _start) > max_elapsed) 176 | { 177 | _start = current; 178 | return true; 179 | } 180 | 181 | return false; 182 | } 183 | } // namespace 184 | 185 | void setup() 186 | { 187 | Serial.begin(115200); // Start serial connection 188 | Serial.println("\nDuino-Coin ESP8266 Miner v2.4.5"); 189 | 190 | SetupWifi(); 191 | SetupOTA(); 192 | 193 | blink(BLINK_SETUP_COMPLETE); // Blink 2 times - indicate sucessfull connection with wifi network 194 | } 195 | 196 | void loop() 197 | { 198 | ArduinoOTA.handle(); // Enable OTA handler 199 | ConnectToServer(); 200 | 201 | Serial.println("Asking for a new job for user: " + String(ducouser)); 202 | 203 | client.print("JOB," + String(ducouser) + ",ESP8266"); // Ask for new job 204 | String hash = getValue(clientBuffer, SEP_TOKEN, 0); 205 | String job = getValue(clientBuffer, SEP_TOKEN, 1); 206 | unsigned int diff = getValue(clientBuffer, SEP_TOKEN, 2).toInt(); 207 | 208 | Serial.println("Job received: " + hash + " " + job + " " + String(diff)); 209 | 210 | float StartTime = micros(); // Start time measurement 211 | unsigned int iJob = Ducos1a.work(hash, job, diff); 212 | 213 | if (iJob > 0) 214 | { 215 | unsigned long ElapsedTime = micros() - StartTime; // Calculate elapsed time 216 | float ElapsedTimeSeconds = ElapsedTime * .000001f; // Convert to seconds 217 | float HashRate = iJob / ElapsedTimeSeconds; 218 | client.print(String(iJob) + "," + String(HashRate) + ",ESP8266 Miner v2.4.5" + "," + String(rigIdentifier)); // Send result to server 219 | waitForClientData(); 220 | Shares++; 221 | Serial.println(clientBuffer + " share #" + String(Shares) + " (" + String(iJob) + ")" + " Hashrate: " + String(HashRate) + " Free RAM: " + String(ESP.getFreeHeap())); 222 | blink(BLINK_SHARE_FOUND); 223 | } 224 | } -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map 3 | ####################################### 4 | 5 | ####################################### 6 | # Class (KEYWORD1) 7 | ####################################### 8 | 9 | DuinoCoin KEYWORD1 10 | Ducos1a KEYWORD1 11 | 12 | ####################################### 13 | # Methods and Functions (KEYWORD2) 14 | ####################################### 15 | 16 | begin KEYWORD2 17 | loop KEYWORD2 18 | work KEYWORD2 19 | 20 | ####################################### 21 | # Constants (LITERAL1) 22 | ####################################### 23 | 24 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=DuinoCoin 2 | version=1.1.0 3 | author=ricaun 4 | maintainer=ricaun 5 | sentence=DuinoCoin 6 | paragraph=DuinoCoin 7 | category=Communication 8 | url=https://github.com/ricaun/arduino-DuinoCoin 9 | architectures=* 10 | includes=DuinoCoin.h 11 | depends=ArduinoUniqueID 12 | -------------------------------------------------------------------------------- /src/Ducos1a.cpp: -------------------------------------------------------------------------------- 1 | #include "Ducos1a.h" 2 | 3 | Ducos1a_t Ducos1a; -------------------------------------------------------------------------------- /src/Ducos1a.h: -------------------------------------------------------------------------------- 1 | #ifndef Ducos1a_h 2 | #define Ducos1a_h 3 | 4 | #include 5 | 6 | #if defined(ARDUINO_ARCH_AVR) 7 | //#include "sha1/Ducos1a_sha1.h" 8 | #include "avr/Ducos1a_avr.h" 9 | #elif defined(ARDUINO_ARCH_ESP8266) 10 | #include "esp/Ducos1a_esp.h" 11 | #elif defined(ARDUINO_ARCH_ESP32) 12 | #include "esp32/Ducos1a_esp32.h" 13 | #else 14 | #include "hash/Ducos1a_hash.h" 15 | #endif 16 | 17 | extern Ducos1a_t Ducos1a; 18 | 19 | #endif -------------------------------------------------------------------------------- /src/DuinoCoin.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "DuinoCoin.h" 3 | 4 | DuinoCoin::DuinoCoin(HardwareSerial &stream) : serial(stream) 5 | { 6 | 7 | } 8 | 9 | DuinoCoin::~DuinoCoin(void) 10 | { 11 | 12 | } 13 | 14 | void DuinoCoin::begin(void) 15 | { 16 | serial.begin(115200); // Open serial port 17 | while (!serial); 18 | serial.setTimeout(5000); 19 | } 20 | 21 | bool DuinoCoin::loop(void) 22 | { 23 | if (serial.available() > 0) { 24 | // Read last block hash 25 | String lastblockhash = serial.readStringUntil(','); 26 | // Read expected hash 27 | String newblockhash = serial.readStringUntil(','); 28 | // Read difficulty 29 | unsigned int difficulty = serial.readStringUntil(',').toInt(); 30 | // Start time measurement 31 | unsigned long startTime = micros(); 32 | // Call DUCO-S1A hasher 33 | unsigned int ducos1result = ducos1a(lastblockhash, newblockhash, difficulty); 34 | // End time measurement 35 | unsigned long endTime = micros(); 36 | // Calculate elapsed time 37 | unsigned long elapsedTime = endTime - startTime; 38 | // Send result back to the program with share time 39 | serial.print(String(ducos1result) + "," + String(elapsedTime) + "," + String(getDUCOID()) + "\n"); 40 | return true; 41 | } 42 | return false; 43 | } 44 | 45 | // DUCO-S1A hasher 46 | uint32_t DuinoCoin::ducos1a(String lastblockhash, String newblockhash, int difficulty) { 47 | return Ducos1a.work(lastblockhash, newblockhash, difficulty); 48 | } 49 | 50 | // Grab Arduino chip DUCOID 51 | String DuinoCoin::getDUCOID() { 52 | String ID = "DUCOID"; 53 | char buff[4]; 54 | for (size_t i = 0; i < 8; i++) 55 | { 56 | sprintf(buff, "%02X", (uint8_t) UniqueID8[i]); 57 | ID += buff; 58 | } 59 | return ID; 60 | } -------------------------------------------------------------------------------- /src/DuinoCoin.h: -------------------------------------------------------------------------------- 1 | #ifndef _DuinoCoin_H_ 2 | #define _DuinoCoin_H_ 3 | 4 | #include 5 | #include 6 | #include "Ducos1a.h" 7 | 8 | class DuinoCoin 9 | { 10 | public: 11 | DuinoCoin(HardwareSerial &stream); 12 | ~DuinoCoin(); 13 | 14 | void begin(); 15 | bool loop(); 16 | 17 | uint32_t ducos1a(String lastblockhash, String newblockhash, int difficulty); 18 | String getDUCOID(); 19 | 20 | private: 21 | HardwareSerial &serial; 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /src/DuinoCoinWifi.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "DuinoCoinWifi.h" 3 | /* 4 | DuinoCoinWifi::DuinoCoinWifi(HardwareSerial &stream) : serial(stream) 5 | { 6 | // Grab Arduino chip ID 7 | ID = "DUCOID"; 8 | for (size_t i = 0; i < 8; i++) 9 | ID += UniqueID[i]; 10 | } 11 | 12 | DuinoCoinWifi::~DuinoCoinWifi(void) 13 | { 14 | 15 | } 16 | 17 | void DuinoCoinWifi::begin(void) 18 | { 19 | serial.begin(115200); // Open serial port 20 | while (!serial); 21 | serial.setTimeout(5000); 22 | } 23 | 24 | void DuinoCoinWifi::test(Stream &stream) 25 | { 26 | stream.print("251 "); 27 | stream.println(ducos1a("2eebb02e9b4995c15d46727dc0427478e738a505", "73a34a3ca74449c0565961476772714e940c9cf0", 6)); 28 | stream.print("482 "); 29 | stream.println(ducos1a("68958c4aab39fbfb4453fb4ee0398a580af7abaf", "b430b3b1d76a6b1ae0464b327e786f43801a02cb", 6)); 30 | stream.print("185 "); 31 | stream.println(ducos1a("f5da2de2ca442868678363e28298bf16f1e3a856", "5a9320c0015d1dd4eee6f90ad1a7d429577abf88", 6)); 32 | } 33 | 34 | bool DuinoCoinWifi::loop(void) 35 | { 36 | if (serial.available() > 0) { 37 | // Read last block hash 38 | String lastblockhash = serial.readStringUntil(','); 39 | // Read expected hash 40 | String newblockhash = serial.readStringUntil(','); 41 | // Read difficulty 42 | unsigned int difficulty = serial.readStringUntil(',').toInt(); 43 | // Start time measurement 44 | unsigned long startTime = micros(); 45 | // Call DUCO-S1A hasher 46 | unsigned int ducos1result = ducos1a(lastblockhash, newblockhash, difficulty); 47 | // End time measurement 48 | unsigned long endTime = micros(); 49 | // Calculate elapsed time 50 | unsigned long elapsedTime = endTime - startTime; 51 | // Send result back to the program with share time 52 | serial.print(String(ducos1result) + "," + String(elapsedTime) + "," + String(ID) + "\n"); 53 | return true; 54 | } 55 | return false; 56 | } 57 | 58 | 59 | #if defined(ARDUINO_ARCH_AVR) 60 | // DUCO-S1A hasher 61 | uint32_t DuinoCoinWifi::ducos1a(String lastblockhash, String newblockhash, int difficulty) { 62 | char buffer[44]; 63 | // DUCO-S1 algorithm implementation for AVR boards (DUCO-S1A) 64 | // Difficulty loop 65 | int ducos1res = 0; 66 | for (int ducos1res = 0; ducos1res < difficulty * 100 + 1; ducos1res++) { 67 | Sha1.init(); 68 | Sha1.print(lastblockhash + ducos1res); 69 | // Get SHA1 result 70 | uint8_t * hash_bytes = Sha1.result(); 71 | // Cast result to array 72 | for (int i = 0; i < 10; i++) { 73 | for (int i = 0; i < 20; i++) { 74 | // MSB to LSB (Depending on the address in hash_bytes) 75 | buffer[2 * i] = "0123456789abcdef"[hash_bytes[i] >> 4]; 76 | // Choose that from the given array of characters 77 | // and retreve the value from address next spot over 78 | buffer[2 * i + 1] = "0123456789abcdef"[hash_bytes[i] & 0xf]; 79 | } 80 | } 81 | if (String(buffer) == String(newblockhash)) { 82 | // If expected hash is equal to the found hash, return the result 83 | return ducos1res; 84 | } 85 | } 86 | return 0; 87 | } 88 | #elif defined(ARDUINO_ARCH_ESP8266) 89 | // DUCO-S1A hasher 90 | uint32_t DuinoCoinWifi::ducos1a(String lastblockhash, String newblockhash, int difficulty) { 91 | newblockhash.toUpperCase(); 92 | // DUCO-S1 algorithm implementation for AVR boards (DUCO-S1A) 93 | // Difficulty loop 94 | int ducos1res = 0; 95 | for (int ducos1res = 0; ducos1res < difficulty * 100 + 1; ducos1res++) { 96 | String result = SHA1::hash(lastblockhash + ducos1res); 97 | if (String(result) == String(newblockhash)) { 98 | // If expected hash is equal to the found hash, return the result 99 | return ducos1res; 100 | } 101 | } 102 | return 0; 103 | } 104 | #else 105 | // DUCO-S1A hasher 106 | uint32_t DuinoCoinWifi::ducos1a(String lastblockhash, String newblockhash, int difficulty) { 107 | return 0; 108 | } 109 | #endif 110 | 111 | */ -------------------------------------------------------------------------------- /src/DuinoCoinWifi.h: -------------------------------------------------------------------------------- 1 | #ifndef _DuinoCoinWifi_H_ 2 | #define _DuinoCoinWifi_H_ 3 | /* 4 | #include 5 | #include 6 | 7 | #if defined(ARDUINO_ARCH_AVR) 8 | #include "avr/sha1.h" 9 | #elif defined(ARDUINO_ARCH_ESP8266) 10 | #include "esp8266/ducos1a.h" 11 | #else 12 | #include "avr/sha1.h" 13 | #endif 14 | 15 | class DuinoCoinWifi 16 | { 17 | public: 18 | DuinoCoinWifi(HardwareSerial &stream); 19 | ~DuinoCoinWifi(); 20 | 21 | void begin(); 22 | bool loop(); 23 | 24 | void test(Stream &stream); 25 | 26 | uint32_t ducos1a(String lastblockhash, String newblockhash, int difficulty); 27 | 28 | private: 29 | HardwareSerial &serial; 30 | String ID; 31 | }; 32 | */ 33 | #endif 34 | -------------------------------------------------------------------------------- /src/avr/Ducos1a_avr.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef Ducos1a_avr_h 4 | #define Ducos1a_avr_h 5 | 6 | #include "sha1.h" 7 | 8 | class Ducos1a_avr 9 | { 10 | public: 11 | // DUCO-S1A hasher 12 | uint32_t work(String lastblockhash, String newblockhash, int difficulty) 13 | { 14 | // DUCO-S1 algorithm implementation for AVR boards (DUCO-S1A) 15 | newblockhash.toUpperCase(); 16 | const char *c = newblockhash.c_str(); 17 | uint8_t final_len = newblockhash.length() >> 1; 18 | uint8_t job[final_len+1]; 19 | for (uint8_t i = 0, j = 0; j < final_len; i += 2, j++) 20 | job[j] = ((((c[i] & 0x1F) + 9) % 25) << 4) + ((c[i + 1] & 0x1F) + 9) % 25; 21 | 22 | if (difficulty > 655) 23 | return 0; 24 | 25 | for (int ducos1res = 0; ducos1res < difficulty * 100 + 1; ducos1res++) 26 | { 27 | Sha1.init(); 28 | Sha1.print(lastblockhash + String(ducos1res)); 29 | // Get SHA1 result 30 | uint8_t *hash_bytes = Sha1.result(); 31 | if (memcmp(hash_bytes, job, SHA1_HASH_LEN*sizeof(char)) == 0) 32 | { 33 | // If expected hash is equal to the found hash, return the result 34 | return ducos1res; 35 | } 36 | } 37 | return 0; 38 | } 39 | }; 40 | 41 | #define Ducos1a_t Ducos1a_avr 42 | 43 | #endif 44 | 45 | -------------------------------------------------------------------------------- /src/avr/backend.cpp: -------------------------------------------------------------------------------- 1 | // This file is part of cryptosuite2. // 2 | // // 3 | // cryptosuite2 is free software: you can redistribute it and/or modify // 4 | // it under the terms of the GNU General Public License as published by // 5 | // the Free Software Foundation, either version 3 of the License, or // 6 | // (at your option) any later version. // 7 | // // 8 | // cryptosuite2 is distributed in the hope that it will be useful, // 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 11 | // GNU General Public License for more details. // 12 | // // 13 | // You should have received a copy of the GNU General Public License // 14 | // along with cryptosuite2. If not, see . // 15 | // // 16 | 17 | #include "config.h" 18 | 19 | #ifndef SHA1_DISABLED 20 | //#include "sha1/constants.c" 21 | #include "sha1/hash.c" 22 | #include "sha1/types.c" 23 | #include "sha1/sha1.c" 24 | #endif 25 | -------------------------------------------------------------------------------- /src/avr/config.h: -------------------------------------------------------------------------------- 1 | // This file is part of cryptosuite2. // 2 | // // 3 | // cryptosuite2 is free software: you can redistribute it and/or modify // 4 | // it under the terms of the GNU General Public License as published by // 5 | // the Free Software Foundation, either version 3 of the License, or // 6 | // (at your option) any later version. // 7 | // // 8 | // cryptosuite2 is distributed in the hope that it will be useful, // 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 11 | // GNU General Public License for more details. // 12 | // // 13 | // You should have received a copy of the GNU General Public License // 14 | // along with cryptosuite2. If not, see . // 15 | // // 16 | 17 | 18 | 19 | // include the module config first, 20 | // overwrite it in the arduino interface config. 21 | #include "sha1/default.h" 22 | 23 | #ifndef SHA_CONFIG_H_ 24 | #define SHA_CONFIG_H_ 25 | 26 | // No changes yet. 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /src/avr/sha1.cpp: -------------------------------------------------------------------------------- 1 | // This file is part of cryptosuite2. // 2 | // // 3 | // cryptosuite2 is free software: you can redistribute it and/or modify // 4 | // it under the terms of the GNU General Public License as published by // 5 | // the Free Software Foundation, either version 3 of the License, or // 6 | // (at your option) any later version. // 7 | // // 8 | // cryptosuite2 is distributed in the hope that it will be useful, // 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 11 | // GNU General Public License for more details. // 12 | // // 13 | // You should have received a copy of the GNU General Public License // 14 | // along with cryptosuite2. If not, see . // 15 | // // 16 | 17 | #include "sha1.h" 18 | 19 | #ifndef SHA1_DISABLED 20 | #ifndef SHA1_DISABLE_WRAPPER 21 | void Sha1Wrapper::init(void) 22 | { 23 | sha1_hasher_init(&_hasher); 24 | } 25 | 26 | #ifdef SHA1_ENABLE_HMAC 27 | void Sha1Wrapper::initHmac(const uint8_t * secret, uint16_t secretLength) 28 | { 29 | sha1_hasher_init_hmac(&_hasher, secret, secretLength); 30 | } 31 | 32 | uint8_t * Sha1Wrapper::resultHmac(void) 33 | { 34 | return sha1_hasher_gethmac(&_hasher); 35 | } 36 | #endif 37 | 38 | 39 | size_t Sha1Wrapper::write(uint8_t byte) 40 | { 41 | if(sha1_hasher_putc(&_hasher, byte) == byte) 42 | { 43 | return 1; 44 | } 45 | return 0; 46 | } 47 | 48 | uint8_t * Sha1Wrapper::result(void) 49 | { 50 | return sha1_hasher_gethash(&_hasher); 51 | } 52 | 53 | Sha1Wrapper Sha1; 54 | 55 | #endif 56 | #endif 57 | -------------------------------------------------------------------------------- /src/avr/sha1.h: -------------------------------------------------------------------------------- 1 | // This file is part of cryptosuite2. // 2 | // // 3 | // cryptosuite2 is free software: you can redistribute it and/or modify // 4 | // it under the terms of the GNU General Public License as published by // 5 | // the Free Software Foundation, either version 3 of the License, or // 6 | // (at your option) any later version. // 7 | // // 8 | // cryptosuite2 is distributed in the hope that it will be useful, // 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 11 | // GNU General Public License for more details. // 12 | // // 13 | // You should have received a copy of the GNU General Public License // 14 | // along with cryptosuite2. If not, see . // 15 | // // 16 | 17 | #include "config.h" 18 | 19 | #ifndef Sha1_h 20 | #define Sha1_h 21 | 22 | #include 23 | #include "Print.h" 24 | #include "sha1/sha1.h" 25 | 26 | #ifndef SHA1_DISABLE_WRAPPER 27 | class Sha1Wrapper : public Print 28 | { 29 | public: 30 | void init(void); 31 | uint8_t * result(void); 32 | #ifdef SHA1_ENABLE_HMAC 33 | void initHmac(const uint8_t * secret, uint16_t secretLength); 34 | uint8_t * resultHmac(void); 35 | #endif 36 | virtual size_t write(uint8_t); 37 | using Print::write; 38 | private: 39 | struct sha1_hasher_s _hasher; 40 | 41 | }; 42 | 43 | extern Sha1Wrapper Sha1; 44 | #endif 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /src/avr/sha1/basic.h: -------------------------------------------------------------------------------- 1 | // This file is part of cryptosuite2. // 2 | // // 3 | // cryptosuite2 is free software: you can redistribute it and/or modify // 4 | // it under the terms of the GNU General Public License as published by // 5 | // the Free Software Foundation, either version 3 of the License, or // 6 | // (at your option) any later version. // 7 | // // 8 | // cryptosuite2 is distributed in the hope that it will be useful, // 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 11 | // GNU General Public License for more details. // 12 | // // 13 | // You should have received a copy of the GNU General Public License // 14 | // along with cryptosuite2. If not, see . // 15 | // // 16 | 17 | #ifndef SHA1_BASIC_H_ 18 | #define SHA1_BASIC_H_ 19 | 20 | #include "default.h" 21 | #include 22 | 23 | #define sha1_rotl(bits,word) (((word) << (bits)) | ((word) >> (32 - (bits)))) 24 | 25 | #endif 26 | 27 | -------------------------------------------------------------------------------- /src/avr/sha1/constants.c: -------------------------------------------------------------------------------- 1 | // This file is part of cryptosuite2. // 2 | // // 3 | // cryptosuite2 is free software: you can redistribute it and/or modify // 4 | // it under the terms of the GNU General Public License as published by // 5 | // the Free Software Foundation, either version 3 of the License, or // 6 | // (at your option) any later version. // 7 | // // 8 | // cryptosuite2 is distributed in the hope that it will be useful, // 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 11 | // GNU General Public License for more details. // 12 | // // 13 | // You should have received a copy of the GNU General Public License // 14 | // along with cryptosuite2. If not, see . // 15 | // // 16 | 17 | #include "constants.h" 18 | 19 | const uint32_t sha1_init_state[SHA1_HASH_LEN / 4] PROGMEM = 20 | { 21 | 0x67452301, 0xefcdab89, 0x98badcfe, 22 | 0x10325476, 0xc3d2e1f0 23 | }; 24 | 25 | const uint32_t sha1_constants[4] PROGMEM = 26 | { 27 | 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 28 | }; 29 | 30 | 31 | -------------------------------------------------------------------------------- /src/avr/sha1/constants.h: -------------------------------------------------------------------------------- 1 | // This file is part of cryptosuite2. // 2 | // // 3 | // cryptosuite2 is free software: you can redistribute it and/or modify // 4 | // it under the terms of the GNU General Public License as published by // 5 | // the Free Software Foundation, either version 3 of the License, or // 6 | // (at your option) any later version. // 7 | // // 8 | // cryptosuite2 is distributed in the hope that it will be useful, // 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 11 | // GNU General Public License for more details. // 12 | // // 13 | // You should have received a copy of the GNU General Public License // 14 | // along with cryptosuite2. If not, see . // 15 | // // 16 | 17 | #ifndef SHA1_CONSTANTS_H_ 18 | #define SHA1_CONSTANTS_H_ 19 | 20 | #include "default.h" 21 | #include 22 | 23 | #define SHA1_BLOCK_LEN 64 24 | #define SHA1_HASH_LEN 20 25 | 26 | #include "Arduino.h" 27 | 28 | extern const uint32_t sha1_init_state[SHA1_HASH_LEN / 4] PROGMEM; 29 | 30 | extern const uint32_t sha1_constants[4] PROGMEM; 31 | 32 | 33 | // From RFC3174 (http://www.faqs.org/rfcs/rfc3174.html) 34 | // (Section 5): 35 | // 36 | // A sequence of constant words K(0), K(1), ... , K(79) is used in the 37 | // SHA-1. In hex these are given by 38 | // 39 | // K(t) = 5A827999 ( 0 <= t <= 19) 40 | // 41 | // K(t) = 6ED9EBA1 (20 <= t <= 39) 42 | // 43 | // K(t) = 8F1BBCDC (40 <= t <= 59) 44 | // 45 | // K(t) = CA62C1D6 (60 <= t <= 79). 46 | // 47 | // This can be achieved using an integer division by 20 and only 4 constants. 48 | 49 | #define sha1_k(i) pgm_read_dword(sha1_constants + (i / 20)) 50 | 51 | 52 | #ifdef SHA1_ENABLE_HMAC 53 | #define SHA1_HMAC_IPAD 0x36 54 | #define SHA1_HMAC_OPAD 0x5c 55 | #endif 56 | 57 | #endif 58 | 59 | -------------------------------------------------------------------------------- /src/avr/sha1/default.h: -------------------------------------------------------------------------------- 1 | // This file is part of cryptosuite2. // 2 | // // 3 | // cryptosuite2 is free software: you can redistribute it and/or modify // 4 | // it under the terms of the GNU General Public License as published by // 5 | // the Free Software Foundation, either version 3 of the License, or // 6 | // (at your option) any later version. // 7 | // // 8 | // cryptosuite2 is distributed in the hope that it will be useful, // 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 11 | // GNU General Public License for more details. // 12 | // // 13 | // You should have received a copy of the GNU General Public License // 14 | // along with cryptosuite2. If not, see . // 15 | // // 16 | 17 | // This file is the module config header 18 | // when the arduino interface is NOT used. 19 | // 20 | // If you want to use the library with Arduino, 21 | // edit sha/config.h. If you use a proper build system, 22 | // use this config file for sha1 and sha/sha256/default.h 23 | // for sha256. 24 | #ifndef SHA1_DEFAULT_H_ 25 | #define SHA1_DEFAULT_H_ 26 | 27 | #define SHA1_ENABLE_HMAC 28 | 29 | 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /src/avr/sha1/hash.c: -------------------------------------------------------------------------------- 1 | // This file is part of cryptosuite2. // 2 | // // 3 | // cryptosuite2 is free software: you can redistribute it and/or modify // 4 | // it under the terms of the GNU General Public License as published by // 5 | // the Free Software Foundation, either version 3 of the License, or // 6 | // (at your option) any later version. // 7 | // // 8 | // cryptosuite2 is distributed in the hope that it will be useful, // 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 11 | // GNU General Public License for more details. // 12 | // // 13 | // You should have received a copy of the GNU General Public License // 14 | // along with cryptosuite2. If not, see . // 15 | // // 16 | 17 | #include "hash.h" 18 | #include 19 | #include 20 | 21 | 22 | void sha1_hash_block(sha1_hasher_t hasher) 23 | { 24 | uint8_t i; 25 | uint32_t a, b, c, d, e, temp; 26 | 27 | // XXX: Omit initializing the message schedule. 28 | // See how I did this below. 29 | // Allocating the message schedule would eat 2k RAM 30 | // which is a no-go on an AVR. 31 | uint8_t i4; 32 | // On x86 we have to change the byte order, because... 33 | // I actually do not know. 34 | for(i = i4 = 0; i < 16; i++, i4 += 4) 35 | { 36 | hasher->buffer.words[i] = (((uint32_t)hasher->buffer.bytes[i4]) << 24) | 37 | (((uint32_t)hasher->buffer.bytes[i4 + 1]) << 16) | 38 | (((uint32_t)hasher->buffer.bytes[i4 + 2]) << 8) | 39 | (((uint32_t)hasher->buffer.bytes[i4 + 3])); 40 | } 41 | 42 | a = hasher->state.words[0]; 43 | b = hasher->state.words[1]; 44 | c = hasher->state.words[2]; 45 | d = hasher->state.words[3]; 46 | e = hasher->state.words[4]; 47 | 48 | for(i = 0; i < 80; i++) 49 | { 50 | // XXX: 51 | // This part of the computation omits the message schedule 52 | // W as described in https://tools.ietf.org/html/rfc4634 53 | // The first 16 words of the message schedule is just the block 54 | // anyways and the computation of the message schedule uses only 55 | // the last 16 words, so we can do that. 56 | if( i >= 16 ) 57 | { 58 | hasher->buffer.words[i & 15] = sha1_rotl(1 59 | , hasher->buffer.words[(i - 3)& 15] 60 | ^ hasher->buffer.words[(i - 8)& 15] 61 | ^ hasher->buffer.words[(i - 14)& 15] 62 | ^ hasher->buffer.words[(i - 16)& 15]); 63 | } 64 | 65 | temp = sha1_rotl(5, a) + e + hasher->buffer.words[i & 15] + sha1_k(i); 66 | if(i < 20) 67 | { 68 | temp += (b & c) | ((~b) & d); 69 | } 70 | else if(i < 40) 71 | { 72 | temp += b ^ c ^ d; 73 | } 74 | else if(i < 60) 75 | { 76 | temp += (b & c) | (b & d) | (c & d); 77 | } 78 | else 79 | { 80 | temp += b ^ c ^ d; 81 | } 82 | 83 | 84 | 85 | 86 | 87 | e = d; 88 | d = c; 89 | c = sha1_rotl(30, b); 90 | b = a; 91 | a = temp; 92 | } 93 | hasher->state.words[0] += a; 94 | hasher->state.words[1] += b; 95 | hasher->state.words[2] += c; 96 | hasher->state.words[3] += d; 97 | hasher->state.words[4] += e; 98 | 99 | } 100 | 101 | 102 | 103 | void sha1_hasher_add_byte(sha1_hasher_t hasher, uint8_t byte) 104 | { 105 | hasher->buffer.bytes[hasher->block_offset] = byte; 106 | hasher->block_offset++; 107 | if(hasher->block_offset == SHA1_BLOCK_LEN) 108 | { 109 | sha1_hash_block(hasher); 110 | hasher->block_offset = 0; 111 | } 112 | } 113 | 114 | /** 115 | * NOTE: once the block has been pad'ed the hasher will 116 | * produce nonsense data. Therefore putc will return EOF 117 | * once the hasher has been pad'ed (this happens, when 118 | * sha1_hasher_gethash or sha1_hasher_gethmac are invoced). 119 | * */ 120 | uint8_t sha1_hasher_putc(sha1_hasher_t hasher, uint8_t byte) 121 | { 122 | if(hasher->_lock) 123 | { 124 | return EOF; 125 | } 126 | hasher->total_bytes++; 127 | sha1_hasher_add_byte(hasher, byte); 128 | return byte; 129 | 130 | } 131 | 132 | 133 | 134 | void sha1_hasher_pad(sha1_hasher_t hasher) 135 | { 136 | hasher->_lock = 1; 137 | sha1_hasher_add_byte(hasher, 0x80); 138 | while(hasher->block_offset != 56) 139 | { 140 | sha1_hasher_add_byte(hasher, 0); 141 | } 142 | 143 | // FIXME: 144 | // Use a loop for this. 145 | sha1_hasher_add_byte(hasher, hasher->total_bytes * 8 >> 56); 146 | sha1_hasher_add_byte(hasher, hasher->total_bytes * 8 >> 48); 147 | sha1_hasher_add_byte(hasher, hasher->total_bytes * 8 >> 40); 148 | sha1_hasher_add_byte(hasher, hasher->total_bytes * 8 >> 32); 149 | sha1_hasher_add_byte(hasher, hasher->total_bytes * 8 >> 24); 150 | sha1_hasher_add_byte(hasher, hasher->total_bytes * 8 >> 16); 151 | sha1_hasher_add_byte(hasher, hasher->total_bytes * 8 >> 8); 152 | sha1_hasher_add_byte(hasher, hasher->total_bytes * 8); 153 | 154 | } 155 | 156 | uint8_t * sha1_hasher_gethash(sha1_hasher_t hasher) 157 | { 158 | sha1_hasher_pad(hasher); 159 | uint8_t i; 160 | 161 | // switch byte order. 162 | for(i = 0; i < (SHA1_HASH_LEN / 4); i++) 163 | { 164 | uint32_t a, b; 165 | a = hasher->state.words[i]; 166 | b = a << 24; 167 | b |= ( a << 8) & 0x00ff0000; 168 | b |= ( a >> 8) & 0x0000ff00; 169 | b |= a >> 24; 170 | hasher->state.words[i] = b; 171 | } 172 | return hasher->state.bytes; 173 | } 174 | 175 | 176 | #ifdef SHA1_ENABLE_HMAC 177 | void sha1_hasher_init_hmac(sha1_hasher_t hasher, const uint8_t * key, size_t key_len) 178 | { 179 | uint8_t i; 180 | memset(hasher->hmac_key_buffer, 0, SHA1_BLOCK_LEN); 181 | 182 | if(key_len > SHA1_BLOCK_LEN) 183 | { 184 | sha1_hasher_init(hasher); 185 | while(key_len--) 186 | { 187 | sha1_hasher_putc(hasher, *key++); 188 | } 189 | memcpy(hasher->hmac_key_buffer, 190 | sha1_hasher_gethash(hasher), 191 | SHA1_HASH_LEN); 192 | } 193 | else 194 | { 195 | memcpy(hasher->hmac_key_buffer, key, key_len); 196 | } 197 | sha1_hasher_init(hasher); 198 | for(i = 0; i < SHA1_BLOCK_LEN; i++) 199 | { 200 | sha1_hasher_putc(hasher, hasher->hmac_key_buffer[i] ^ SHA1_HMAC_IPAD); 201 | } 202 | 203 | } 204 | uint8_t * sha1_hasher_gethmac(sha1_hasher_t hasher) 205 | { 206 | uint8_t i; 207 | memcpy(hasher->hmac_inner_hash, sha1_hasher_gethash(hasher), 208 | SHA1_HASH_LEN); 209 | sha1_hasher_init(hasher); 210 | 211 | for(i = 0; i < SHA1_BLOCK_LEN; i++) 212 | { 213 | sha1_hasher_putc(hasher, hasher->hmac_key_buffer[i] ^ SHA1_HMAC_OPAD); 214 | } 215 | for(i = 0; i < SHA1_HASH_LEN; i++) 216 | { 217 | sha1_hasher_putc(hasher, hasher->hmac_inner_hash[i]); 218 | } 219 | return sha1_hasher_gethash(hasher); 220 | } 221 | #endif 222 | 223 | -------------------------------------------------------------------------------- /src/avr/sha1/hash.h: -------------------------------------------------------------------------------- 1 | // This file is part of cryptosuite2. // 2 | // // 3 | // cryptosuite2 is free software: you can redistribute it and/or modify // 4 | // it under the terms of the GNU General Public License as published by // 5 | // the Free Software Foundation, either version 3 of the License, or // 6 | // (at your option) any later version. // 7 | // // 8 | // cryptosuite2 is distributed in the hope that it will be useful, // 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 11 | // GNU General Public License for more details. // 12 | // // 13 | // You should have received a copy of the GNU General Public License // 14 | // along with cryptosuite2. If not, see . // 15 | // // 16 | 17 | #ifndef SHA1_HASH_H_ 18 | #define SHA1_HASH_H_ 19 | 20 | 21 | #include "default.h" 22 | #include "constants.h" 23 | #include "types.h" 24 | #include "basic.h" 25 | 26 | void sha1_hash_block(sha1_hasher_t hasher); 27 | 28 | void sha1_hasher_add_byte(sha1_hasher_t hasher, uint8_t byte); 29 | 30 | /** 31 | * NOTE: once the block has been pad'ed the hasher will 32 | * produce nonsense data. Therefore putc will return EOF 33 | * once the hasher has been pad'ed (this happens, when 34 | * sha1_hasher_gethash or sha1_hasher_gethmac are invoced). 35 | * */ 36 | uint8_t sha1_hasher_putc(sha1_hasher_t hasher, uint8_t byte); 37 | 38 | void sha1_hasher_pad(sha1_hasher_t hasher); 39 | 40 | /** 41 | * NOTE: this will NOT return a copy of the data but 42 | * a REFERENCE! One MUST NOT free the result. 43 | * 44 | * Also this modifies the state of the hasher. The 45 | * hasher has an internal lock ensuring that writing 46 | * to the hasher fails after this operation. 47 | * */ 48 | uint8_t * sha1_hasher_gethash(sha1_hasher_t hasher); 49 | 50 | #ifdef SHA1_ENABLE_HMAC 51 | void sha1_hasher_init_hmac(sha1_hasher_t hasher, const uint8_t * key, size_t key_len); 52 | uint8_t * sha1_hasher_gethmac(sha1_hasher_t hasher); 53 | #endif 54 | 55 | #endif 56 | 57 | -------------------------------------------------------------------------------- /src/avr/sha1/sha1.c: -------------------------------------------------------------------------------- 1 | // This file is part of cryptosuite2. // 2 | // // 3 | // cryptosuite2 is free software: you can redistribute it and/or modify // 4 | // it under the terms of the GNU General Public License as published by // 5 | // the Free Software Foundation, either version 3 of the License, or // 6 | // (at your option) any later version. // 7 | // // 8 | // cryptosuite2 is distributed in the hope that it will be useful, // 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 11 | // GNU General Public License for more details. // 12 | // // 13 | // You should have received a copy of the GNU General Public License // 14 | // along with cryptosuite2. If not, see . // 15 | // // 16 | 17 | #include "sha1.h" 18 | 19 | ssize_t sha1_hasher_write(sha1_hasher_t hasher, const void * buf, size_t count) 20 | { 21 | size_t written = 0; 22 | uint8_t chk_result; 23 | char c; 24 | while(written < count) 25 | { 26 | c = ((char *) buf)[written]; 27 | chk_result = sha1_hasher_putc(hasher, c); 28 | if(chk_result != c) 29 | { 30 | return -1; 31 | } 32 | written++; 33 | } 34 | return written; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /src/avr/sha1/sha1.h: -------------------------------------------------------------------------------- 1 | // This file is part of cryptosuite2. // 2 | // // 3 | // cryptosuite2 is free software: you can redistribute it and/or modify // 4 | // it under the terms of the GNU General Public License as published by // 5 | // the Free Software Foundation, either version 3 of the License, or // 6 | // (at your option) any later version. // 7 | // // 8 | // cryptosuite2 is distributed in the hope that it will be useful, // 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 11 | // GNU General Public License for more details. // 12 | // // 13 | // You should have received a copy of the GNU General Public License // 14 | // along with cryptosuite2. If not, see . // 15 | // // 16 | 17 | #ifndef SHA1_SHA1_H_ 18 | #define SHA1_SHA1_H_ 19 | 20 | #include "default.h" 21 | #include "types.h" 22 | #include "hash.h" 23 | #include 24 | //#include 25 | 26 | #ifndef ssize_t 27 | #define ssize_t long int 28 | #endif 29 | 30 | ssize_t sha1_hasher_write(sha1_hasher_t hasher, const void * buf, size_t count); 31 | 32 | #endif 33 | 34 | -------------------------------------------------------------------------------- /src/avr/sha1/types.c: -------------------------------------------------------------------------------- 1 | // This file is part of cryptosuite2. // 2 | // // 3 | // cryptosuite2 is free software: you can redistribute it and/or modify // 4 | // it under the terms of the GNU General Public License as published by // 5 | // the Free Software Foundation, either version 3 of the License, or // 6 | // (at your option) any later version. // 7 | // // 8 | // cryptosuite2 is distributed in the hope that it will be useful, // 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 11 | // GNU General Public License for more details. // 12 | // // 13 | // You should have received a copy of the GNU General Public License // 14 | // along with cryptosuite2. If not, see . // 15 | // // 16 | 17 | #include "types.h" 18 | #include 19 | #include 20 | #include "constants.h" 21 | 22 | sha1_hasher_t sha1_hasher_new(void) 23 | { 24 | sha1_hasher_t hasher = (sha1_hasher_t) malloc(sizeof(struct sha1_hasher_s)); 25 | if(!hasher) 26 | { 27 | return NULL; 28 | } 29 | sha1_hasher_init(hasher); 30 | return hasher; 31 | } 32 | 33 | void sha1_hasher_init(sha1_hasher_t hasher) 34 | { 35 | uint8_t i; 36 | for(i = 0; i < SHA1_HASH_LEN / 4; i++) 37 | { 38 | hasher->state.words[i] = pgm_read_dword(sha1_init_state + i); 39 | } 40 | hasher->block_offset = 0; 41 | hasher->total_bytes = 0; 42 | hasher->_lock = 0; 43 | 44 | } 45 | 46 | 47 | void sha1_hasher_del(sha1_hasher_t hasher) 48 | { 49 | free(hasher); 50 | } 51 | 52 | 53 | -------------------------------------------------------------------------------- /src/avr/sha1/types.h: -------------------------------------------------------------------------------- 1 | // This file is part of cryptosuite2. // 2 | // // 3 | // cryptosuite2 is free software: you can redistribute it and/or modify // 4 | // it under the terms of the GNU General Public License as published by // 5 | // the Free Software Foundation, either version 3 of the License, or // 6 | // (at your option) any later version. // 7 | // // 8 | // cryptosuite2 is distributed in the hope that it will be useful, // 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 11 | // GNU General Public License for more details. // 12 | // // 13 | // You should have received a copy of the GNU General Public License // 14 | // along with cryptosuite2. If not, see . // 15 | // // 16 | 17 | #ifndef SHA1_TYPES_H_ 18 | #define SHA1_TYPES_H_ 19 | 20 | #include "default.h" 21 | #include 22 | #include 23 | #include "constants.h" 24 | 25 | 26 | typedef union 27 | { 28 | uint8_t bytes[SHA1_HASH_LEN]; 29 | uint32_t words[SHA1_HASH_LEN / 4]; 30 | 31 | } sha1_state_t; 32 | 33 | typedef union 34 | { 35 | uint8_t bytes[SHA1_BLOCK_LEN]; 36 | uint32_t words[SHA1_BLOCK_LEN / 4]; 37 | 38 | } sha1_block_t; 39 | 40 | typedef struct __attribute__((__packed__)) sha1_hasher_s 41 | { 42 | sha1_state_t state; 43 | sha1_block_t buffer; 44 | 45 | uint8_t block_offset; 46 | uint64_t total_bytes; 47 | uint8_t _lock; 48 | #ifdef SHA1_ENABLE_HMAC 49 | uint8_t hmac_key_buffer[SHA1_BLOCK_LEN]; 50 | uint8_t hmac_inner_hash[SHA1_HASH_LEN]; 51 | #endif 52 | } * sha1_hasher_t; 53 | 54 | sha1_hasher_t sha1_hasher_new(void); 55 | void sha1_hasher_del(sha1_hasher_t hasher); 56 | void sha1_hasher_init(sha1_hasher_t hasher); 57 | 58 | 59 | #endif 60 | 61 | -------------------------------------------------------------------------------- /src/esp/Ducos1a_esp.h: -------------------------------------------------------------------------------- 1 | #ifndef Ducos1a_esp_h 2 | #define Ducos1a_esp_h 3 | 4 | #include 5 | #include // experimental SHA1 crypto library 6 | using namespace experimental::crypto; 7 | 8 | class Ducos1a_esp 9 | { 10 | public: 11 | // DUCO-S1A hasher 12 | uint32_t work(String lastblockhash, String newblockhash, int difficulty) 13 | { 14 | // DUCO-S1 algorithm implementation for ESP8266 boards (DUCO-S1A) 15 | newblockhash.toUpperCase(); 16 | // Difficulty loop 17 | for (int ducos1res = 0; ducos1res < difficulty * 100 + 1; ducos1res++) 18 | { 19 | String result = SHA1::hash(lastblockhash + String(ducos1res)); 20 | if (String(result) == String(newblockhash)) 21 | { 22 | // If expected hash is equal to the found hash, return the result 23 | return ducos1res; 24 | } 25 | ESP.wdtFeed(); 26 | } 27 | return 0; 28 | } 29 | }; 30 | 31 | #define Ducos1a_t Ducos1a_esp 32 | 33 | #endif -------------------------------------------------------------------------------- /src/esp/Hash.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////// 2 | //file Hash.h 3 | //date 24.04.2020 4 | //author Markus Sattler 5 | //edits revox 6 | ////////////////////////////////////////////////////////// 7 | //Copyright (c) 2015 Markus Sattler. All rights reserved. 8 | //This file is part of the esp8266 core for Arduino environment. 9 | //This library is free software; you can redistribute it and/or 10 | //modify it under the terms of the GNU Lesser General Public 11 | //License as published by the Free Software Foundation; either 12 | //version 2.1 of the License, or (at your option) any later version. 13 | ////////////////////////////////////////////////////////// 14 | 15 | #ifndef HASH_H_ 16 | #define HASH_H_ 17 | void sha1(const uint8_t* data, uint32_t size, uint8_t hash[20]); 18 | void sha1(const char* data, uint32_t size, uint8_t hash[20]); 19 | void sha1(const String& data, uint8_t hash[20]); 20 | 21 | String sha1(const uint8_t* data, uint32_t size); 22 | String sha1(const char* data, uint32_t size); 23 | String sha1(const String& data); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /src/esp32/Ducos1a_esp32.h: -------------------------------------------------------------------------------- 1 | #ifndef Ducos1a_esp32_h 2 | #define Ducos1a_esp32_h 3 | 4 | #include 5 | #include "mbedtls/md.h" // Include software hashing library 6 | #include "hwcrypto/sha.h" // Include hardware accelerated hashing library 7 | 8 | class Ducos1a_esp32 9 | { 10 | public: 11 | // DUCO-S1A hasher 12 | uint32_t work(String lastblockhash, String newblockhash, int difficulty) 13 | { 14 | // DUCO-S1 algorithm implementation for ESP32 boards (DUCO-S1A) 15 | newblockhash.toUpperCase(); 16 | const char *c = newblockhash.c_str(); 17 | size_t len = strlen(c); 18 | size_t final_len = len / 2; 19 | uint8_t job[final_len+1]; 20 | for (size_t i = 0, j = 0; j < final_len; i += 2, j++) 21 | job[j] = (c[i] % 32 + 9) % 25 * 16 + (c[i + 1] % 32 + 9) % 25; 22 | 23 | for (int ducos1res = 0; ducos1res < difficulty * 100 + 1; ducos1res++) 24 | { 25 | String hash11 = String(lastblockhash) + String(ducos1res); 26 | const unsigned char *payload1 = (const unsigned char *)hash11.c_str(); 27 | unsigned int payloadLenght1 = hash11.length(); 28 | 29 | byte shaResult1[20]; 30 | esp_sha(SHA1, payload1, payloadLenght1, shaResult1); 31 | 32 | if (memcmp(shaResult1, job, sizeof(shaResult1)) == 0) 33 | { 34 | // If expected hash is equal to the found hash, return the result 35 | return ducos1res; 36 | } 37 | yield(); // uncomment if ESP watchdog triggers 38 | } 39 | return 0; 40 | } 41 | 42 | uint32_t work2(String lastblockhash, String newblockhash, int difficulty) 43 | { 44 | // DUCO-S1 algorithm implementation for ESP32 boards (DUCO-S1A) 45 | newblockhash.toUpperCase(); 46 | const char *c = newblockhash.c_str(); 47 | size_t len = strlen(c); 48 | size_t final_len = len / 2; 49 | uint8_t job[final_len+1]; 50 | for (size_t i = 0, j = 0; j < final_len; i += 2, j++) 51 | job[j] = (c[i] % 32 + 9) % 25 * 16 + (c[i + 1] % 32 + 9) % 25; 52 | 53 | for (int ducos1res = 0; ducos1res < difficulty * 100 + 1; ducos1res++) 54 | { 55 | String hash1 = String(lastblockhash) + String(ducos1res); 56 | const unsigned char *payload = (const unsigned char *)hash1.c_str(); 57 | unsigned int payloadLength = hash1.length(); 58 | 59 | byte shaResult[20]; 60 | mbedtls_md_context_t ctx; 61 | mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1; 62 | mbedtls_md_init(&ctx); 63 | mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(md_type), 0); 64 | mbedtls_md_starts(&ctx); 65 | mbedtls_md_update(&ctx, (const unsigned char *)payload, payloadLength); 66 | mbedtls_md_finish(&ctx, shaResult); 67 | mbedtls_md_free(&ctx); 68 | 69 | if (memcmp(shaResult, job, sizeof(shaResult)) == 0) 70 | { 71 | // If expected hash is equal to the found hash, return the result 72 | return ducos1res; 73 | } 74 | yield(); // uncomment if ESP watchdog triggers 75 | } 76 | return 0; 77 | } 78 | }; 79 | 80 | #define Ducos1a_t Ducos1a_esp32 81 | 82 | #endif -------------------------------------------------------------------------------- /src/hash/Ducos1a_hash.h: -------------------------------------------------------------------------------- 1 | #ifndef Ducos1a_hash_h 2 | #define Ducos1a_hash_h 3 | 4 | #include "Hash/src/Hash.h" 5 | 6 | class Ducos1a_hash 7 | { 8 | public: 9 | // DUCO-S1A hasher 10 | uint32_t work(String lastblockhash, String newblockhash, int difficulty) 11 | { 12 | // DUCO-S1 algorithm implementation (DUCO-S1A) 13 | // Difficulty loop 14 | for (int ducos1res = 0; ducos1res < difficulty * 100 + 1; ducos1res++) 15 | { 16 | String result = sha1(lastblockhash + String(ducos1res)); 17 | if (String(result) == String(newblockhash)) 18 | { 19 | // If expected hash is equal to the found hash, return the result 20 | return ducos1res; 21 | } 22 | } 23 | return 0; 24 | } 25 | }; 26 | 27 | #define Ducos1a_t Ducos1a_hash 28 | 29 | #endif -------------------------------------------------------------------------------- /src/hash/Hash/LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | (This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.) 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | 460 | How to Apply These Terms to Your New Libraries 461 | 462 | If you develop a new library, and you want it to be of the greatest 463 | possible use to the public, we recommend making it free software that 464 | everyone can redistribute and change. You can do so by permitting 465 | redistribution under these terms (or, alternatively, under the terms of the 466 | ordinary General Public License). 467 | 468 | To apply these terms, attach the following notices to the library. It is 469 | safest to attach them to the start of each source file to most effectively 470 | convey the exclusion of warranty; and each file should have at least the 471 | "copyright" line and a pointer to where the full notice is found. 472 | 473 | {description} 474 | Copyright (C) {year} {fullname} 475 | 476 | This library is free software; you can redistribute it and/or 477 | modify it under the terms of the GNU Lesser General Public 478 | License as published by the Free Software Foundation; either 479 | version 2.1 of the License, or (at your option) any later version. 480 | 481 | This library is distributed in the hope that it will be useful, 482 | but WITHOUT ANY WARRANTY; without even the implied warranty of 483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 484 | Lesser General Public License for more details. 485 | 486 | You should have received a copy of the GNU Lesser General Public 487 | License along with this library; if not, write to the Free Software 488 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 489 | USA 490 | 491 | Also add information on how to contact you by electronic and paper mail. 492 | 493 | You should also get your employer (if you work as a programmer) or your 494 | school, if any, to sign a "copyright disclaimer" for the library, if 495 | necessary. Here is a sample; alter the names: 496 | 497 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 498 | library `Frob' (a library for tweaking knobs) written by James Random 499 | Hacker. 500 | 501 | {signature of Ty Coon}, 1 April 1990 502 | Ty Coon, President of Vice 503 | 504 | That's all there is to it! 505 | -------------------------------------------------------------------------------- /src/hash/Hash/README.md: -------------------------------------------------------------------------------- 1 | # Arduino-SHA-1-Hash 2 | 3 | This library is imported from [Arduino-SHA-1-Hash](https://github.com/mr-glt/Arduino-SHA-1-Hash). 4 | 5 | ## License 6 | 7 | This library is [licensed](LICENSE) under the `GNU LESSER GENERAL PUBLIC LICENSE`. -------------------------------------------------------------------------------- /src/hash/Hash/library.properties: -------------------------------------------------------------------------------- 1 | name=SHA-1 Hash 2 | version=1.1 3 | author=Markus Sattler 4 | maintainer=Charlie Wade 5 | sentence=Generate SHA-1 Hash from data 6 | paragraph= 7 | category=Data Processing 8 | url=https://github.com/mr-glt/Arduino-SHA-1-Hash 9 | architectures=* 10 | -------------------------------------------------------------------------------- /src/hash/Hash/src/Hash.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file Hash.cpp 3 | * @date 09.09.2017 4 | * 5 | * Ported from esp8266 core to stand alone library. 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public 18 | * License along with this library; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 | * 21 | */ 22 | 23 | #include 24 | 25 | #include "Hash.h" 26 | 27 | extern "C" { 28 | #include "sha1/sha1.h" 29 | } 30 | 31 | /** 32 | * create a sha1 hash from data 33 | * @param data uint8_t * 34 | * @param size uint32_t 35 | * @param hash uint8_t[20] 36 | */ 37 | void sha1(uint8_t * data, uint32_t size, uint8_t hash[20]) { 38 | 39 | SHA1_CTX ctx; 40 | 41 | #ifdef DEBUG_SHA1 42 | os_printf("DATA:"); 43 | for(uint16_t i = 0; i < size; i++) { 44 | os_printf("%02X", data[i]); 45 | } 46 | os_printf("\n"); 47 | os_printf("DATA:"); 48 | for(uint16_t i = 0; i < size; i++) { 49 | os_printf("%c", data[i]); 50 | } 51 | os_printf("\n"); 52 | #endif 53 | 54 | SHA1Init(&ctx); 55 | SHA1Update(&ctx, data, size); 56 | SHA1Final(hash, &ctx); 57 | 58 | #ifdef DEBUG_SHA1 59 | os_printf("SHA1:"); 60 | for(uint16_t i = 0; i < 20; i++) { 61 | os_printf("%02X", hash[i]); 62 | } 63 | os_printf("\n\n"); 64 | #endif 65 | } 66 | 67 | void sha1(char * data, uint32_t size, uint8_t hash[20]) { 68 | sha1((uint8_t *) data, size, hash); 69 | } 70 | 71 | void sha1(const uint8_t * data, uint32_t size, uint8_t hash[20]) { 72 | sha1((uint8_t *) data, size, hash); 73 | } 74 | 75 | void sha1(const char * data, uint32_t size, uint8_t hash[20]) { 76 | sha1((uint8_t *) data, size, hash); 77 | } 78 | 79 | void sha1(String data, uint8_t hash[20]) { 80 | sha1(data.c_str(), data.length(), hash); 81 | } 82 | 83 | String sha1(uint8_t* data, uint32_t size) { 84 | uint8_t hash[20]; 85 | String hashStr = ""; 86 | 87 | sha1(&data[0], size, &hash[0]); 88 | 89 | for(uint16_t i = 0; i < 20; i++) { 90 | String hex = String(hash[i], HEX); 91 | if(hex.length() < 2) { 92 | hex = "0" + hex; 93 | } 94 | hashStr += hex; 95 | } 96 | 97 | return hashStr; 98 | } 99 | 100 | String sha1(char* data, uint32_t size) { 101 | return sha1((uint8_t*) data, size); 102 | } 103 | 104 | String sha1(const uint8_t* data, uint32_t size) { 105 | return sha1((uint8_t*) data, size); 106 | } 107 | 108 | String sha1(const char* data, uint32_t size) { 109 | return sha1((uint8_t*) data, size); 110 | } 111 | 112 | String sha1(String data) { 113 | return sha1(data.c_str(), data.length()); 114 | } 115 | 116 | -------------------------------------------------------------------------------- /src/hash/Hash/src/Hash.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file Hash.h 3 | * @date 09.09.2017 4 | * 5 | * 6 | * Ported from esp8266 core to stand alone library. 7 | * 8 | * This library is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU Lesser General Public 10 | * License as published by the Free Software Foundation; either 11 | * version 2.1 of the License, or (at your option) any later version. 12 | * 13 | * This library is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 | * Lesser General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public 19 | * License along with this library; if not, write to the Free Software 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 21 | * 22 | */ 23 | 24 | #ifndef HASH_H_ 25 | #define HASH_H_ 26 | 27 | //#define DEBUG_SHA1 28 | 29 | void sha1(uint8_t * data, uint32_t size, uint8_t hash[20]); 30 | void sha1(char * data, uint32_t size, uint8_t hash[20]); 31 | void sha1(const uint8_t * data, uint32_t size, uint8_t hash[20]); 32 | void sha1(const char * data, uint32_t size, uint8_t hash[20]); 33 | void sha1(String data, uint8_t hash[20]); 34 | 35 | String sha1(uint8_t* data, uint32_t size); 36 | String sha1(char* data, uint32_t size); 37 | String sha1(const uint8_t* data, uint32_t size); 38 | String sha1(const char* data, uint32_t size); 39 | String sha1(String data); 40 | 41 | #endif /* HASH_H_ */ 42 | -------------------------------------------------------------------------------- /src/hash/Hash/src/sha1/sha1.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file sha1.c 3 | * @date 09.09.2017 4 | * @author Steve Reid 5 | * 6 | * from: http://www.virtualbox.org/svn/vbox/trunk/src/recompiler/tests/sha1.c 7 | */ 8 | 9 | /* from valgrind tests */ 10 | 11 | /* ================ sha1.c ================ */ 12 | /* 13 | SHA-1 in C 14 | By Steve Reid 15 | Maintained by Charlie Wade 16 | 100% Public Domain 17 | 18 | Test Vectors (from FIPS PUB 180-1) 19 | "abc" 20 | A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D 21 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" 22 | 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 23 | A million repetitions of "a" 24 | 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F 25 | */ 26 | 27 | /* #define LITTLE_ENDIAN * This should be #define'd already, if true. */ 28 | /* #define SHA1HANDSOFF * Copies data before messing with it. */ 29 | 30 | #define SHA1HANDSOFF 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | #include "sha1.h" 37 | 38 | //#include 39 | 40 | #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) 41 | 42 | /* blk0() and blk() perform the initial expand. */ 43 | /* I got the idea of expanding during the round function from SSLeay */ 44 | #if BYTE_ORDER == LITTLE_ENDIAN 45 | #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ 46 | |(rol(block->l[i],8)&0x00FF00FF)) 47 | #elif BYTE_ORDER == BIG_ENDIAN 48 | #define blk0(i) block->l[i] 49 | #else 50 | #error "Endianness not defined!" 51 | #endif 52 | #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ 53 | ^block->l[(i+2)&15]^block->l[i&15],1)) 54 | 55 | /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ 56 | #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); 57 | #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); 58 | #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); 59 | #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); 60 | #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); 61 | 62 | 63 | /* Hash a single 512-bit block. This is the core of the algorithm. */ 64 | 65 | void ICACHE_FLASH_ATTR SHA1Transform(uint32_t state[5], uint8_t buffer[64]) 66 | { 67 | uint32_t a, b, c, d, e; 68 | typedef union { 69 | unsigned char c[64]; 70 | uint32_t l[16]; 71 | } CHAR64LONG16; 72 | #ifdef SHA1HANDSOFF 73 | CHAR64LONG16 block[1]; /* use array to appear as a pointer */ 74 | memcpy(block, buffer, 64); 75 | #else 76 | /* The following had better never be used because it causes the 77 | * pointer-to-const buffer to be cast into a pointer to non-const. 78 | * And the result is written through. I threw a "const" in, hoping 79 | * this will cause a diagnostic. 80 | */ 81 | CHAR64LONG16* block = (const CHAR64LONG16*)buffer; 82 | #endif 83 | /* Copy context->state[] to working vars */ 84 | a = state[0]; 85 | b = state[1]; 86 | c = state[2]; 87 | d = state[3]; 88 | e = state[4]; 89 | /* 4 rounds of 20 operations each. Loop unrolled. */ 90 | R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); 91 | R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); 92 | R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); 93 | R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); 94 | R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); 95 | R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); 96 | R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); 97 | R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); 98 | R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); 99 | R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); 100 | R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); 101 | R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); 102 | R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); 103 | R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); 104 | R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); 105 | R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); 106 | R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); 107 | R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); 108 | R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); 109 | R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); 110 | /* Add the working vars back into context.state[] */ 111 | state[0] += a; 112 | state[1] += b; 113 | state[2] += c; 114 | state[3] += d; 115 | state[4] += e; 116 | /* Wipe variables */ 117 | a = b = c = d = e = 0; 118 | #ifdef SHA1HANDSOFF 119 | memset(block, '\0', sizeof(block)); 120 | #endif 121 | } 122 | 123 | 124 | /* SHA1Init - Initialize new context */ 125 | 126 | void ICACHE_FLASH_ATTR SHA1Init(SHA1_CTX* context) 127 | { 128 | /* SHA1 initialization constants */ 129 | context->state[0] = 0x67452301; 130 | context->state[1] = 0xEFCDAB89; 131 | context->state[2] = 0x98BADCFE; 132 | context->state[3] = 0x10325476; 133 | context->state[4] = 0xC3D2E1F0; 134 | context->count[0] = context->count[1] = 0; 135 | } 136 | 137 | 138 | /* Run your data through this. */ 139 | 140 | void ICACHE_FLASH_ATTR SHA1Update(SHA1_CTX* context, uint8_t* data, uint32_t len) 141 | { 142 | uint32_t i; 143 | uint32_t j; 144 | 145 | j = context->count[0]; 146 | if ((context->count[0] += len << 3) < j) 147 | context->count[1]++; 148 | context->count[1] += (len>>29); 149 | j = (j >> 3) & 63; 150 | if ((j + len) > 63) { 151 | memcpy(&context->buffer[j], data, (i = 64-j)); 152 | SHA1Transform(context->state, context->buffer); 153 | for ( ; i + 63 < len; i += 64) { 154 | SHA1Transform(context->state, &data[i]); 155 | } 156 | j = 0; 157 | } 158 | else i = 0; 159 | memcpy(&context->buffer[j], &data[i], len - i); 160 | } 161 | 162 | 163 | /* Add padding and return the message digest. */ 164 | 165 | void ICACHE_FLASH_ATTR SHA1Final(unsigned char digest[20], SHA1_CTX* context) 166 | { 167 | unsigned i; 168 | unsigned char finalcount[8]; 169 | unsigned char c; 170 | 171 | #if 0 /* untested "improvement" by DHR */ 172 | /* Convert context->count to a sequence of bytes 173 | * in finalcount. Second element first, but 174 | * big-endian order within element. 175 | * But we do it all backwards. 176 | */ 177 | unsigned char *fcp = &finalcount[8]; 178 | 179 | for (i = 0; i < 2; i++) 180 | { 181 | uint32_t t = context->count[i]; 182 | int j; 183 | 184 | for (j = 0; j < 4; t >>= 8, j++) 185 | *--fcp = (unsigned char) t; 186 | } 187 | #else 188 | for (i = 0; i < 8; i++) { 189 | finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] 190 | >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ 191 | } 192 | #endif 193 | c = 0200; 194 | SHA1Update(context, &c, 1); 195 | while ((context->count[0] & 504) != 448) { 196 | c = 0000; 197 | SHA1Update(context, &c, 1); 198 | } 199 | SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ 200 | for (i = 0; i < 20; i++) { 201 | digest[i] = (unsigned char) 202 | ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); 203 | } 204 | /* Wipe variables */ 205 | memset(context, '\0', sizeof(*context)); 206 | memset(&finalcount, '\0', sizeof(finalcount)); 207 | } 208 | /* ================ end of sha1.c ================ */ 209 | -------------------------------------------------------------------------------- /src/hash/Hash/src/sha1/sha1.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file sha1.h 3 | * @date 20.05.2015 4 | * @author Steve Reid 5 | * 6 | * from: http://www.virtualbox.org/svn/vbox/trunk/src/recompiler/tests/sha1.c 7 | */ 8 | 9 | /* ================ sha1.h ================ */ 10 | /* 11 | SHA-1 in C 12 | By Steve Reid 13 | Maintained by Charlie Wade 14 | 100% Public Domain 15 | */ 16 | 17 | #ifndef SHA1_H_ 18 | #define SHA1_H_ 19 | #define ICACHE_FLASH_ATTR __attribute__((section(".irom0.text"))) 20 | typedef struct { 21 | uint32_t state[5]; 22 | uint32_t count[2]; 23 | unsigned char buffer[64]; 24 | } SHA1_CTX; 25 | 26 | void SHA1Transform(uint32_t state[5], uint8_t buffer[64]); 27 | void SHA1Init(SHA1_CTX* context); 28 | void SHA1Update(SHA1_CTX* context, uint8_t* data, uint32_t len); 29 | void SHA1Final(unsigned char digest[20], SHA1_CTX* context); 30 | 31 | #endif /* SHA1_H_ */ 32 | 33 | /* ================ end of sha1.h ================ */ 34 | --------------------------------------------------------------------------------