summaryrefslogtreecommitdiff
path: root/usb-crypto/usb-crypto.cpp
blob: 0f7a9bec907d81abb18d454aef21c0ac91c596b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 *  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 run_crypto()
{
  printf("running encrypt/decrypt test using ChaCha20Poly1305:\r\n");

  printf("\r\nencrypting...\r\n");
  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};

  uint8_t buf[256];
  uint8_t tag[16];
  memset(buf, 0, sizeof(buf));
  memset(tag, 0, sizeof(tag));

  led_on();
  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;
  }
  led_off();

  led_on();
  cipher.addAuthData(hdr, hdr_len);
  cipher.encrypt(buf, body, body_len);
  cipher.computeTag(tag, sizeof(tag));
  led_off();

  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));

  printf("\r\ndecrypting...\r\n");
  memcpy(body, buf, body_len);
  memset(buf, 0, sizeof(buf));

  led_on();
  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;
  }
  led_off();

  led_on();
  cipher.addAuthData(hdr, hdr_len);
  cipher.decrypt(buf, body, body_len);
  bool ct = cipher.checkTag(tag, sizeof(tag));
  led_off();

  if(!ct) {
    printf("auth tag mismatch!\r\n");
    return;
  } else {
    printf("auth tag correct!\r\n");
  }
  printf("decrypted body: '%s'\r\n", buf);
}

void handle_cmd(uint8_t cmd)
{
  switch(cmd) {
  case 'c': run_crypto(); break;
  case 'r': reset2bootloader(); break;
  default: printf("unknown command"); break;
  }
  printf("\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();
  }
}