Mercurial > pihelp
comparison main.c @ 5:87c8d0a11906
make it work
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 05 Jun 2013 21:29:02 +0800 |
parents | fd28c7358ce8 |
children | 76f3ed943180 |
comparison
equal
deleted
inserted
replaced
4:fd28c7358ce8 | 5:87c8d0a11906 |
---|---|
12 #include <avr/wdt.h> | 12 #include <avr/wdt.h> |
13 #include <util/atomic.h> | 13 #include <util/atomic.h> |
14 #include <util/crc16.h> | 14 #include <util/crc16.h> |
15 | 15 |
16 #include "hmac-sha1.h" | 16 #include "hmac-sha1.h" |
17 #include "aes.h" | |
17 | 18 |
18 //#include "simple_ds18b20.h" | 19 //#include "simple_ds18b20.h" |
19 //#include "onewire.h" | 20 //#include "onewire.h" |
20 | 21 |
22 LOCKBITS = (LB_MODE_3 & BLB0_MODE_4 & BLB1_MODE_4); | |
23 | |
21 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y)) | 24 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y)) |
22 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y)) | 25 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y)) |
23 | 26 |
24 // TICK should be 8 or less (8 untested). all timers need | 27 // TICK should be 8 or less (8 untested). all timers need |
25 // to be a multiple. | 28 // to be a multiple. |
26 | 29 |
27 #define TICK 1 | 30 #define TICK 1 |
28 #define SLEEP_COMPARE (2000000/64) // == 31250 exactly | 31 #define SLEEP_COMPARE (2000000/64) // == 31250 exactly |
29 #define NKEYS 6 | 32 #define NKEYS 10 |
30 #define KEYLEN 20 | 33 #define HMACLEN 20 |
34 #define AESLEN 16 | |
35 #define KEYLEN HMACLEN | |
31 | 36 |
32 #define BAUD 19200 | 37 #define BAUD 19200 |
33 #define UBRR ((F_CPU)/8/(BAUD)-1) | 38 #define UBRR ((F_CPU)/8/(BAUD)-1) |
34 | 39 |
35 // XXX | |
36 #define PORT_PI_BOOT PORTD | 40 #define PORT_PI_BOOT PORTD |
37 #define DDR_PI_BOOT DDRD | 41 #define DDR_PI_BOOT DDRD |
38 #define PIN_PI_BOOT PD5 | 42 #define PIN_PI_BOOT PD5 |
39 | 43 |
40 // XXX | |
41 #define PORT_PI_RESET PORTD | 44 #define PORT_PI_RESET PORTD |
42 #define DDR_PI_RESET DDRD | 45 #define DDR_PI_RESET DDRD |
43 #define PIN_PI_RESET PD6 | 46 #define PIN_PI_RESET PD6 |
44 | |
45 | 47 |
46 #define PORT_LED PORTD | 48 #define PORT_LED PORTD |
47 #define DDR_LED DDRD | 49 #define DDR_LED DDRD |
48 #define PIN_LED PD7 | 50 #define PIN_LED PD7 |
49 | 51 |
136 CLKPR = _BV(CLKPCE); | 138 CLKPR = _BV(CLKPCE); |
137 CLKPR = _BV(CLKPS1); | 139 CLKPR = _BV(CLKPS1); |
138 | 140 |
139 // enable pullups | 141 // enable pullups |
140 // XXX matt pihelp | 142 // XXX matt pihelp |
141 PORTB = 0xff; // XXX change when using SPI | 143 //PORTB = 0xff; // XXX change when using SPI |
142 PORTD = 0xff; | 144 //PORTD = 0xff; |
143 PORTC = 0xff; | 145 //PORTC = 0xff; |
144 | 146 |
145 // 3.3v power for bluetooth and SD | 147 // 3.3v power for bluetooth and SD |
146 DDR_LED |= _BV(PIN_LED); | 148 DDR_LED |= _BV(PIN_LED); |
147 | 149 |
150 #if 0 | |
148 // set pullup | 151 // set pullup |
149 PORTD |= _BV(PD2); | 152 PORTD |= _BV(PD2); |
150 // INT0 setup | 153 // INT0 setup |
151 EICRA = (1<<ISC01); // falling edge - data sheet says it won't work? | 154 EICRA = (1<<ISC01); // falling edge - data sheet says it won't work? |
152 EIMSK = _BV(INT0); | 155 EIMSK = _BV(INT0); |
156 #endif | |
153 | 157 |
154 // comparator disable | 158 // comparator disable |
155 ACSR = _BV(ACD); | 159 ACSR = _BV(ACD); |
156 | 160 |
157 // disable adc pin input buffers | 161 // disable adc pin input buffers |
363 printhex_byte(id[i], stream); | 367 printhex_byte(id[i], stream); |
364 } | 368 } |
365 } | 369 } |
366 | 370 |
367 static int8_t | 371 static int8_t |
368 parse_key(const char *params, uint8_t *key_index, uint8_t bytes[KEYLEN]) | 372 parse_key(const char *params, uint8_t *key_index, uint8_t *bytes, |
373 uint8_t bytes_len) | |
369 { | 374 { |
370 // "N HEXKEY" | 375 // "N HEXKEY" |
371 if (strlen(params) != KEYLEN*2+2) { | 376 if (strlen(params) != bytes_len*2 + 2) { |
372 printf_P(PSTR("Wrong length key\n")); | 377 printf_P(PSTR("Wrong length key\n")); |
373 return -1; | 378 return -1; |
374 } | 379 } |
375 | 380 |
376 if (params[1] != ' ') | 381 if (params[1] != ' ') |
384 { | 389 { |
385 printf_P(PSTR("Bad key index %d, max %d\n"), *key_index, NKEYS); | 390 printf_P(PSTR("Bad key index %d, max %d\n"), *key_index, NKEYS); |
386 return -1; | 391 return -1; |
387 } | 392 } |
388 | 393 |
389 for (int i = 0, p = 0; i < KEYLEN; i++, p += 2) | 394 for (int i = 0, p = 0; i < bytes_len; i++, p += 2) |
390 { | 395 { |
391 bytes[i] = (from_hex(params[p+2]) << 4) | from_hex(params[p+3]); | 396 bytes[i] = (from_hex(params[p+2]) << 4) | from_hex(params[p+3]); |
392 } | 397 } |
393 return 0; | 398 return 0; |
394 } | 399 } |
396 static void | 401 static void |
397 cmd_set_avr_key(const char *params) | 402 cmd_set_avr_key(const char *params) |
398 { | 403 { |
399 uint8_t new_key[KEYLEN]; | 404 uint8_t new_key[KEYLEN]; |
400 uint8_t key_index; | 405 uint8_t key_index; |
401 if (parse_key(params, &key_index, new_key) != 0) | 406 if (parse_key(params, &key_index, new_key, sizeof(new_key)) != 0) |
402 { | 407 { |
403 return; | 408 return; |
404 } | 409 } |
405 memcpy(avr_keys[key_index], new_key, sizeof(new_key)); | 410 memcpy(avr_keys[key_index], new_key, sizeof(new_key)); |
406 eeprom_write(avr_keys, avr_keys); | 411 eeprom_write(avr_keys, avr_keys); |
407 } | 412 } |
408 | 413 |
409 static void | 414 static void |
410 cmd_hmac(const char *params) | 415 cmd_hmac(const char *params) |
411 { | 416 { |
412 uint8_t data[KEYLEN]; | 417 uint8_t data[HMACLEN]; |
413 uint8_t key_index; | 418 uint8_t key_index; |
414 if (parse_key(params, &key_index, data) != 0) | 419 if (parse_key(params, &key_index, data, sizeof(data)) != 0) |
415 { | 420 { |
416 printf_P(PSTR("FAIL: Bad input\n")); | 421 printf_P(PSTR("FAIL: Bad input\n")); |
417 } | 422 return; |
423 } | |
424 | |
425 if (key_index % 2 == 0) | |
426 { | |
427 printf_P(PSTR("Only hmac with even keys\n")); | |
428 return; | |
429 } | |
430 | |
431 long_delay(200); | |
418 | 432 |
419 hmac_sha1_ctx_t ctx; | 433 hmac_sha1_ctx_t ctx; |
420 hmac_sha1_init(&ctx, avr_keys[key_index], KEYLEN); | 434 hmac_sha1_init(&ctx, avr_keys[key_index], KEYLEN); |
421 hmac_sha1_lastBlock(&ctx, data, KEYLEN); | 435 hmac_sha1_lastBlock(&ctx, data, HMACLEN); |
422 hmac_sha1_final(data, &ctx); | 436 hmac_sha1_final(data, &ctx); |
423 | 437 |
424 printf_P(PSTR("HMAC: ")); | 438 printf_P(PSTR("HMAC: ")); |
425 printhex(data, KEYLEN, stdout); | 439 printhex(data, HMACLEN, stdout); |
440 fputc('\n', stdout); | |
441 } | |
442 | |
443 static void | |
444 cmd_decrypt(const char *params) | |
445 { | |
446 uint8_t data[HMACLEN+AESLEN]; | |
447 uint8_t output[HMACLEN]; | |
448 uint8_t key_index; | |
449 if (parse_key(params, &key_index, data, sizeof(data)) != 0) | |
450 { | |
451 printf_P(PSTR("FAIL: Bad input\n")); | |
452 return; | |
453 } | |
454 | |
455 if (key_index % 2) | |
456 { | |
457 printf_P(PSTR("Only decrypt with odd keys\n")); | |
458 return; | |
459 } | |
460 | |
461 long_delay(200); | |
462 | |
463 // check the signature | |
464 hmac_sha1_ctx_t ctx; | |
465 hmac_sha1_init(&ctx, avr_keys[key_index], KEYLEN); | |
466 hmac_sha1_lastBlock(&ctx, &data[HMACLEN], AESLEN); | |
467 hmac_sha1_final(output, &ctx); | |
468 | |
469 if (memcmp(output, data, HMACLEN) != 0) { | |
470 printf_P(PSTR("FAIL: hmac mismatch\n")); | |
471 } | |
472 | |
473 uint8_t expkey[AES_EXPKEY_SIZE]; | |
474 ExpandKey(avr_keys[key_index], expkey); | |
475 Decrypt(&data[HMACLEN], expkey, output); | |
476 | |
477 printf_P(PSTR("DECRYPTED: ")); | |
478 printhex(output, AESLEN, stdout); | |
426 fputc('\n', stdout); | 479 fputc('\n', stdout); |
427 } | 480 } |
428 | 481 |
429 static void | 482 static void |
430 cmd_oneshot_reboot(const char *params) | 483 cmd_oneshot_reboot(const char *params) |
480 } | 533 } |
481 else if (strncmp_P(readbuf, PSTR("hmac "), 5) == 0) | 534 else if (strncmp_P(readbuf, PSTR("hmac "), 5) == 0) |
482 { | 535 { |
483 cmd_hmac(&readbuf[5]); | 536 cmd_hmac(&readbuf[5]); |
484 } | 537 } |
538 else if (strncmp_P(readbuf, PSTR("decrypt "), 8) == 0) | |
539 { | |
540 cmd_hmac(&readbuf[8]); | |
541 } | |
485 else if (strcmp_P(readbuf, PSTR("vcc")) == 0) | 542 else if (strcmp_P(readbuf, PSTR("vcc")) == 0) |
486 { | 543 { |
487 cmd_vcc(); | 544 cmd_vcc(); |
488 } | 545 } |
489 else if (strcmp_P(readbuf, PSTR("reset")) == 0) | 546 else if (strcmp_P(readbuf, PSTR("reset")) == 0) |
504 { | 561 { |
505 blink(); | 562 blink(); |
506 _delay_ms(100); | 563 _delay_ms(100); |
507 blink(); | 564 blink(); |
508 } | 565 } |
509 | |
510 | 566 |
511 ISR(USART_RX_vect) | 567 ISR(USART_RX_vect) |
512 { | 568 { |
513 char c = UDR0; | 569 char c = UDR0; |
514 #ifdef HAVE_UART_ECHO | 570 #ifdef HAVE_UART_ECHO |
728 blink(); | 784 blink(); |
729 | 785 |
730 stdout = &mystdout; | 786 stdout = &mystdout; |
731 uart_on(); | 787 uart_on(); |
732 | 788 |
733 printf(PSTR("Started.\n")); | 789 printf(PSTR("Pi Watchdog\nMatt Johnston [email protected]")); |
734 | 790 |
735 set_pi_boot_normal(0); | 791 set_pi_boot_normal(0); |
736 | 792 |
737 load_params(); | 793 load_params(); |
738 | 794 |