rweather / arduinolibs

Arduino Cryptography Library
444 stars 212 forks source link

Need help with ChaCha and Strings #69

Closed daricht closed 2 years ago

daricht commented 2 years ago

Im trying to use your library and as a beginner I am having problems with the c++ way of handling datatypes and with pointers.

I tried to follow along with your basic AES Example and use whatever I can from the testChaCha example.

can I just use the Chacha.Encrypt() like you are using the AES.Encrypt() in the sense that it can encrypt a whole array of bytes in one go? because it seems that the way you are using it is bytewise.

Here is my code so far: `

include

include "ChaCha.h"

include "Crypto.h"

include "utility/EndianUtil.h"

include "utility/ProgMemUtil.h"

include "utility/RotateUtil.h"

define MAX_CIPHERTEXT_SIZE 32

ChaCha chacha;

const int msgLen = 32;

int size =5; byte iv[8] = {101,102,103,104,105,106,107,108}; byte counter[8] = {109, 110, 111, 112, 113, 114, 115, 116}; byte plaintext[5] = {'H', 'e', 'l', 'l', 'o'}; byte cypher[5]; byte key[24] = {0x32, 0x62, 0x37, 0x65, 0x31, 0x35, 0x31, 0x36, 0x32, 0x38, 0x61, 0x65, 0x64, 0x32, 0x61, 0x36, 0x61, 0x62, 0x66, 0x37, 0x31, 0x35, 0x38, 0x39}; byte decrypted[5];

void setup() { Serial.begin(115200); }

void loop(){ chacha.clear(); chacha.setKey(key, sizeof(key)); chacha.setIV(iv,8); chacha.setCounter(counter,8); Serial.println("####"); for (int i = 0; i < sizeof(plaintext); i++) {Serial.printf("%c", plaintext[i]);}Serial.println(); chacha.encrypt(cypher, plaintext, sizeof(plaintext)); for (int i = 0; i < sizeof(cypher); i++) {Serial.printf("%c", cypher[i]);}Serial.println(); chacha.decrypt(decrypted, cypher, sizeof(cypher)); for (int i = 0; i < sizeof(decrypted); i++) {Serial.printf("%c", decrypted[i]);}Serial.println(); delay(1000); } `

daricht commented 2 years ago

I think I figured it out! I didn't think you'd have to reset the key iv and counter each time...

If im not mistaken this code is working now:

include

include "ChaCha.h"

include "Crypto.h"

include "utility/EndianUtil.h"

include "utility/ProgMemUtil.h"

include "utility/RotateUtil.h"

define MAX_CIPHERTEXT_SIZE 32

ChaCha chacha;

const int msgLen = 32;

int size =5; byte iv[8] = {101,102,103,104,105,106,107,108}; byte counter[8] = {109, 110, 111, 112, 113, 114, 115, 116}; byte plaintext[5] = {'#','j','0','1','G'}; byte cypher[5]; byte key[24] = {0x32, 0x62, 0x37, 0x65, 0x31, 0x35, 0x31, 0x36, 0x32, 0x38, 0x61, 0x65, 0x64, 0x32, 0x61, 0x36, 0x61, 0x62, 0x66, 0x37, 0x31, 0x35, 0x38, 0x39}; byte decrypted[5];

void setup() { Serial.begin(115200);

}

void loop(){

chacha.clear(); chacha.setKey(key, 24); chacha.setIV(iv,8); chacha.setCounter(counter,8); Serial.println("####"); for (int i = 0; i < sizeof(plaintext); i++) {Serial.printf("%c", plaintext[i]);}Serial.println(); chacha.encrypt(cypher, plaintext, sizeof(plaintext)); for (int i = 0; i < sizeof(cypher); i++) {Serial.printf("%c", cypher[i]);}Serial.println();

chacha.setKey(key, 24); chacha.setIV(iv,8); chacha.setCounter(counter,8);

chacha.decrypt(decrypted, cypher, sizeof(cypher)); for (int i = 0; i < sizeof(decrypted); i++) {Serial.printf("%c", decrypted[i]);}Serial.println(); delay(1000); }