Skip to content

Instantly share code, notes, and snippets.

@JoeAlamo
Created April 7, 2016 12:09
Show Gist options
  • Save JoeAlamo/496f717d118acceb4d6fa15af232fb48 to your computer and use it in GitHub Desktop.
Save JoeAlamo/496f717d118acceb4d6fa15af232fb48 to your computer and use it in GitHub Desktop.
#include <ChaChaPoly.h>
void setup() {
Serial.begin(9600);
// put your setup code here, to run once:
uint8_t key[32] = {
0x1C, 0x92, 0x40, 0xA5, 0xEB, 0x55, 0xD3, 0x8A,
0xF3, 0x33, 0x88, 0x86, 0x04, 0xF6, 0xB5, 0xF0,
0x47, 0x39, 0x17, 0xC1, 0x40, 0x2B, 0x80, 0x09,
0x9D, 0xCA, 0x5C, 0xBC, 0x20, 0x70, 0x75, 0xC0
};
uint8_t plaintext[265] = {
0x49, 0x6E, 0x74, 0x65, 0x72, 0x6E, 0x65, 0x74,
0x2D, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
0x74, 0x20, 0x64, 0x6F, 0x63, 0x75, 0x6D, 0x65,
0x6E, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6C, 0x69,
0x64, 0x20, 0x66, 0x6F, 0x72, 0x20, 0x61, 0x20,
0x6D, 0x61, 0x78, 0x69, 0x6D, 0x75, 0x6D, 0x20,
0x6F, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6D,
0x6F, 0x6E, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6E,
0x64, 0x20, 0x6D, 0x61, 0x79, 0x20, 0x62, 0x65,
0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
0x2C, 0x20, 0x72, 0x65, 0x70, 0x6C, 0x61, 0x63,
0x65, 0x64, 0x2C, 0x20, 0x6F, 0x72, 0x20, 0x6F,
0x62, 0x73, 0x6F, 0x6C, 0x65, 0x74, 0x65, 0x64,
0x20, 0x62, 0x79, 0x20, 0x6F, 0x74, 0x68, 0x65,
0x72, 0x20, 0x64, 0x6F, 0x63, 0x75, 0x6D, 0x65,
0x6E, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
0x6E, 0x79, 0x20, 0x74, 0x69, 0x6D, 0x65, 0x2E,
0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
0x6E, 0x61, 0x70, 0x70, 0x72, 0x6F, 0x70, 0x72,
0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6F, 0x20,
0x75, 0x73, 0x65, 0x20, 0x49, 0x6E, 0x74, 0x65,
0x72, 0x6E, 0x65, 0x74, 0x2D, 0x44, 0x72, 0x61,
0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
0x65, 0x66, 0x65, 0x72, 0x65, 0x6E, 0x63, 0x65,
0x20, 0x6D, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
0x6C, 0x20, 0x6F, 0x72, 0x20, 0x74, 0x6F, 0x20,
0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
0x6D, 0x20, 0x6F, 0x74, 0x68, 0x65, 0x72, 0x20,
0x74, 0x68, 0x61, 0x6E, 0x20, 0x61, 0x73, 0x20,
0x2F, 0xE2, 0x80, 0x9C, 0x77, 0x6F, 0x72, 0x6B,
0x20, 0x69, 0x6E, 0x20, 0x70, 0x72, 0x6F, 0x67,
0x72, 0x65, 0x73, 0x73, 0x2E, 0x2F, 0xE2, 0x80,
0x9D
};
uint8_t ad[12] = {
0xF3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x4E, 0x91
};
uint8_t nonce[8] = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
};
uint8_t ciphertext[265], tag[16];
ChaChaPoly cipher;
cipher.setKey(key, 32);
cipher.setIV(nonce, 8);
cipher.addAuthData(ad, 12);
cipher.encrypt(ciphertext, plaintext, 265);
cipher.computeTag(tag, 16);
printHex(ciphertext, 265);
printHex(tag, 16);
}
void loop() {
// put your main code here, to run repeatedly:
}
/* Print contents of input in hexadecimal format */
void printHex(uint8_t *input, uint16_t len) {
for (uint16_t i=0; i < len; i++) {
if (i % 8 == 0) {
Serial.println();
}
Serial.print("0x");
if (input[i] < 16) {
Serial.print('0');
}
Serial.print(input[i], HEX);
if (i+1 != len) {
Serial.print(", ");
} else {
Serial.println();
Serial.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment