summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2018-11-28 18:13:53 +0100
committerChristian Pointner <equinox@spreadspace.org>2018-11-28 18:13:53 +0100
commitc29aa83128d1762dcbd00e90aeaecaed367f4a6e (patch)
tree6873d0684bbc1b0ec0024b6be396bbe1a9753994
parentadded supported for arduino crypot lib (diff)
added usb-crypto example
-rw-r--r--usb-crypto/Makefile51
-rw-r--r--usb-crypto/usb-crypto.cpp123
2 files changed, 174 insertions, 0 deletions
diff --git a/usb-crypto/Makefile b/usb-crypto/Makefile
new file mode 100644
index 0000000..d6e6444
--- /dev/null
+++ b/usb-crypto/Makefile
@@ -0,0 +1,51 @@
+##
+## 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/>.
+##
+
+NAME := usb-crypto
+BOARD_TYPE := teensy2
+CXX_OBJ := $(NAME).o
+LIBS := util led lufa-descriptor-usbserial usbio
+EXTERNAL_LIBS := lufa crypto
+SPREADAVR_PATH := ..
+RESET_FUNC := $(SPREADAVR_PATH)/tools/reset_lufa_cdc
+RESET_PARAM := 'r'
+
+LUFA_PATH := $(SPREADAVR_PATH)/contrib/lufa-LUFA-151115
+LUFA_OPTS = -D USB_DEVICE_ONLY
+LUFA_OPTS += -D DEVICE_STATE_AS_GPIOR=0
+LUFA_OPTS += -D ORDERED_EP_CONFIG
+LUFA_OPTS += -D FIXED_CONTROL_ENDPOINT_SIZE=8
+LUFA_OPTS += -D FIXED_NUM_CONFIGURATIONS=1
+LUFA_OPTS += -D USE_FLASH_DESCRIPTORS
+LUFA_OPTS += -D USE_STATIC_OPTIONS="(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)"
+LUFA_OPTS += -D INTERRUPT_CONTROL_ENDPOINT
+
+LUFA_OPTS += -D USB_MANUFACTURER="L\"equinox\""
+LUFA_OPTS += -D USB_PRODUCT="L\"spreadspace usb-crypto example\""
+
+LUFA_COMPONENTS := USB USBCLASS
+
+
+CRYPTO_PATH := $(SPREADAVR_PATH)/contrib/crypto
+
+
+include $(SPREADAVR_PATH)/include.mk
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();
+ }
+}