Hi, my friend send me his code and library. He use Arduino Uno R3, it work fine. But I only have Wemos D1 R1, and it messed up with datatypes. After a while, I end up with this error:
Arduino: 1.8.8 (Windows 10), Board: "WeMos D1 R1, 80 MHz, Flash, Disabled, 4M (no SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200"
c:/users/euphoria celestial/appdata/local/arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/2.5.0-3-20ed2b9/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld.exe: sketch\sketch_dec05a.ino.cpp.o: in function `ULongLongToString(unsigned long long)':
D:\Desktop on HDD\New folder\Arduino\sketch_dec05a/sketch_dec05a.ino:83: undefined reference to `present_64_128_key_schedule(unsigned char*, char*)'
c:/users/euphoria celestial/appdata/local/arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/2.5.0-3-20ed2b9/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld.exe: sketch\sketch_dec05a.ino.cpp.o: in function `loop':
D:\Desktop on HDD\New folder\Arduino\sketch_dec05a/sketch_dec05a.ino:46: undefined reference to `present_64_128_key_schedule(unsigned char*, char*)'
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board WeMos D1 R1.
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
this is my code:
#include <grain128.h>
#include <DHT.h>
#include <present.h>
const int DHTPIN = 2;
const int DHTTYPE = DHT11;
DHT dht(DHTPIN, DHTTYPE);
int encryptType = 1;
char roundkey[PRESENT_KEY_SIZE_128/8*(PRESENT_ROUNDS+1)];
unsigned char key[] = "1234567890123456";
unsigned char IV[] = "123456789012";
Grain grain(key, IV);
unsigned char * tmp1 = new unsigned char[8];
char str_text[8];
unsigned long time1 = 0;
unsigned long time2 = 0;
void setup() {
Serial.begin(9600);
dht.begin();
//Serial.println("OK");
}
void loop() {
int doam = dht.readHumidity();
int nhietdo = dht.readTemperature();
int ran = random(10,99);
String tmp = String(nhietdo)+"#"+ String(doam)+"#"+ String(ran);
//Serial.println(tmp);
int len = tmp.length();
//Serial.println(key);
tmp.toCharArray(str_text, 9);
if(encryptType == 1)
{
//Serial.println(str_text);
present_64_128_key_schedule(key,roundkey);
unsigned long long ull_tmp = *(unsigned long long *)str_text;
// Serial.println(ULongLongToString(ull_tmp));
//time1 = time2;
//present_encrypt((char *)&ull_tmp, roundkey);
present_encrypt((char *)&ull_tmp, roundkey);
//time2 = millis();
//unsigned long timeTest = time2-time1;
//Serial.println(timeTest);
Serial.println(ULongLongToString(ull_tmp));
}
if(encryptType == 2)
{
//unsigned long time1 = millis();
grain.Encrypt((unsigned char *)str_text, tmp1, 8);
//unsigned long time2 = millis();
//Serial.println(time2-time1);
unsigned long long ull_tmp = *(unsigned long long *)tmp1;
Serial.println(ULongLongToString(ull_tmp));
}
delay(500);
}
String ULongLongToString(unsigned long long number)
{
unsigned long long tmp_num = number;
if(tmp_num ==0) return "0";
String tmp="";
while(tmp_num!=0)
{
int n= tmp_num%10;
tmp = String(n) + tmp;
tmp_num = tmp_num/10;
}
return tmp;
}
present crypto library:
#ifndef PRESENT_H
#define PRESENT_H
#include <Arduino.h>
#define PRESENT_BLOCK_SIZE (64) /* block size in bits */
#define PRESENT_KEY_SIZE_80 (80) /* key size in bits */
#define PRESENT_KEY_SIZE_128 (128) /* key size in bits */
#define PRESENT_ROUNDS (31) /* rounds */
/*
* key schedule
* inputKey: the original keys
* keys: round keys
*/
void present_64_128_key_schedule(unsigned char * inputKey, char * keys );
/*
* encrypt
* plainText: plainText has just one block.
* keys: round keys
*/
void present_encrypt(char * plainText, const char * keys );
void present_enCtr(char * plainText,int length, const char *keys,const char *IV);
/*
* decrypt
* cipherText: cipherText has just one block.
* keys: round keys
*/
void present_decrypt(char * cipherText, const char * keys );
void present_deCtr(char * cipherText,int length, const char *keys,const char *IV);
#endif
grain library:
#ifndef GRAIN128_H_INCLUDED
#define GRAIN128_H_INCLUDED
#include <Arduino.h>
class Grain
{
private:
unsigned char *KEY, *IV, NFSR[128], LFSR[128];
void ECRYPT_ivsetup();
unsigned char grain_keystream();
public:
Grain(unsigned char *key,unsigned char *iv);
void Encrypt(unsigned char * plain, unsigned char *cipher, int length);
void Decrypt(unsigned char * cipher, unsigned char *plain, int length);
};
#endif // GRAIN128_H_INCLUDED