diff options
Diffstat (limited to 'usb-crypto/usb-crypto.cpp')
-rw-r--r-- | usb-crypto/usb-crypto.cpp | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/usb-crypto/usb-crypto.cpp b/usb-crypto/usb-crypto.cpp new file mode 100644 index 0000000..777b939 --- /dev/null +++ b/usb-crypto/usb-crypto.cpp @@ -0,0 +1,123 @@ +/* + * spreadspace avr utils + * + * + * Copyright (C) 2013-2016 Christian Pointner <equinox@spreadspace.org> + * + * This file is part of spreadspace avr utils. + * + * spreadspace avr utils is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * spreadspace avr utils is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with spreadspace avr utils. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <avr/io.h> +#include <avr/wdt.h> +#include <avr/interrupt.h> +#include <avr/power.h> +#include <stdio.h> +#include <string.h> + +#include "util.h" +#include "led.h" +#include "usbio.h" + +#include <Crypto.h> +#include <ChaChaPoly.h> + +ChaChaPoly cipher; + +void print_hex_dump(const uint8_t* data, size_t len) { + for(size_t i=0; i<len; ++i) { + printf(" 0x%02X", data[i]); + if((i+1)%8 == 0) printf("\r\n"); + } + printf("\r\n"); +} + +void encrypt() { + + uint8_t hdr[] = "hello world!"; + size_t hdr_len = sizeof(hdr)-1; + uint8_t body[] = "this is a secret message."; + size_t body_len = sizeof(body)-1; + + uint8_t key[] = {0x70, 0x43, 0xb6, 0x9b, 0xde, 0x20, 0x44, 0x66, + 0x61, 0xba, 0x57, 0x9e, 0x83, 0xfd, 0xa0, 0x83, + 0x0e, 0x3e, 0x61, 0xc9, 0x5b, 0x5a, 0xc8, 0xde, + 0xeb, 0x79, 0x97, 0x3b, 0xa0, 0xdf, 0x02, 0xd8}; + + uint8_t iv[] = {0x6f, 0xac, 0x1c, 0x6a, 0x94, 0xa5, 0x78, 0x87, + 0x61, 0xcf, 0x9e, 0xcd}; + + cipher.clear(); + if(!cipher.setKey(key, sizeof(key))) { + printf("failed to set key\r\n"); + return; + } + if(!cipher.setIV(iv, sizeof(iv))) { + printf("failed to set iv\r\n"); + return; + } + + uint8_t buf[256]; + uint8_t tag[16]; + memset(buf, 0, sizeof(buf)); + memset(tag, 0, sizeof(tag)); + + cipher.addAuthData(hdr, hdr_len); + cipher.encrypt(buf, body, body_len); + cipher.computeTag(tag, sizeof(tag)); + + printf("encrypted data (%d bytes):\r\n", body_len); + print_hex_dump(buf, body_len); + printf("\r\n"); + printf("auth tag (%d bytes):\r\n", sizeof(tag)); + print_hex_dump(tag, sizeof(tag)); +} + +void handle_cmd(uint8_t cmd) +{ + switch(cmd) { + case 'e': encrypt(); return; + case '0': led_off(); break; + case '1': led_on(); break; + case 't': led_toggle(); break; + case 'r': reset2bootloader(); break; + default: printf("error\r\n"); return; + } + printf("ok\r\n"); +} + +int main(void) +{ + MCUSR &= ~(1 << WDRF); + wdt_disable(); + + cpu_init(); + led_init(); + usbio_init(); + sei(); + + for(;;) { + int16_t BytesReceived = usbio_bytes_received(); + while(BytesReceived > 0) { + int ReceivedByte = fgetc(stdin); + if(ReceivedByte != EOF) { + handle_cmd(ReceivedByte); + } + BytesReceived--; + } + + usbio_task(); + } +} |