comparison main.c @ 7:76f3ed943180

a few fixes
author Matt Johnston <matt@ucc.asn.au>
date Wed, 05 Jun 2013 23:08:08 +0800
parents 87c8d0a11906
children 03da5ff767e9
comparison
equal deleted inserted replaced
6:ed8d308b4993 7:76f3ed943180
49 #define DDR_LED DDRD 49 #define DDR_LED DDRD
50 #define PIN_LED PD7 50 #define PIN_LED PD7
51 51
52 // #define HAVE_UART_ECHO 52 // #define HAVE_UART_ECHO
53 53
54 // stores a value of clock_epoch combined with the remainder of TCNT2, 54 // stores a value of clock_epoch combined with the remainder of TCNT1,
55 // for 1/32 second accuracy 55 // for 1/32 second accuracy
56 struct epoch_ticks 56 struct epoch_ticks
57 { 57 {
58 uint32_t ticks; 58 uint32_t ticks;
59 // remainder 59 // remainder
169 get_epoch_ticks(struct epoch_ticks *t) 169 get_epoch_ticks(struct epoch_ticks *t)
170 { 170 {
171 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) 171 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
172 { 172 {
173 t->ticks = clock_epoch; 173 t->ticks = clock_epoch;
174 t->rem = TCNT2; 174 t->rem = TCNT1;
175 } 175 }
176 } 176 }
177 177
178 static void 178 static void
179 setup_tick_counter() 179 setup_tick_counter()
184 // COM21 COM20 Set OC2 on Compare Match (p116) 184 // COM21 COM20 Set OC2 on Compare Match (p116)
185 // WGM21 Clear counter on compare 185 // WGM21 Clear counter on compare
186 //TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21); 186 //TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21);
187 // toggle on match 187 // toggle on match
188 TCCR1A = _BV(COM1A0); 188 TCCR1A = _BV(COM1A0);
189 // systemclock/1024 189 // systemclock/64
190 TCCR1B = _BV(CS12) | _BV(CS10); 190 TCCR1B = _BV(CS11) | _BV(CS10);
191 TCNT1 = 0; 191 TCNT1 = 0;
192 OCR1A = SLEEP_COMPARE; 192 OCR1A = SLEEP_COMPARE;
193 // interrupt 193 // interrupt
194 TIMSK1 = _BV(OCIE1A); 194 TIMSK1 = _BV(OCIE1A);
195 } 195 }
412 } 412 }
413 413
414 static void 414 static void
415 cmd_hmac(const char *params) 415 cmd_hmac(const char *params)
416 { 416 {
417 uint8_t data[HMACLEN]; 417 uint8_t indata[HMACLEN];
418 uint8_t outdata[HMACLEN];
418 uint8_t key_index; 419 uint8_t key_index;
419 if (parse_key(params, &key_index, data, sizeof(data)) != 0) 420 if (parse_key(params, &key_index, indata, sizeof(indata)) != 0)
420 { 421 {
421 printf_P(PSTR("FAIL: Bad input\n")); 422 printf_P(PSTR("FAIL: Bad input\n"));
422 return; 423 return;
423 } 424 }
424 425
425 if (key_index % 2 == 0) 426 if (key_index % 2 != 0)
426 { 427 {
427 printf_P(PSTR("Only hmac with even keys\n")); 428 printf_P(PSTR("Only hmac with even keys\n"));
428 return; 429 return;
429 } 430 }
430 431
431 long_delay(200); 432 long_delay(200);
432 433
433 hmac_sha1_ctx_t ctx; 434 hmac_sha1(outdata, avr_keys[key_index], KEYLEN*8, indata, HMACLEN*8);
434 hmac_sha1_init(&ctx, avr_keys[key_index], KEYLEN);
435 hmac_sha1_lastBlock(&ctx, data, HMACLEN);
436 hmac_sha1_final(data, &ctx);
437 435
438 printf_P(PSTR("HMAC: ")); 436 printf_P(PSTR("HMAC: "));
439 printhex(data, HMACLEN, stdout); 437 printhex(outdata, HMACLEN, stdout);
440 fputc('\n', stdout); 438 fputc('\n', stdout);
441 } 439 }
442 440
443 static void 441 static void
444 cmd_decrypt(const char *params) 442 cmd_decrypt(const char *params)
450 { 448 {
451 printf_P(PSTR("FAIL: Bad input\n")); 449 printf_P(PSTR("FAIL: Bad input\n"));
452 return; 450 return;
453 } 451 }
454 452
455 if (key_index % 2) 453 if (key_index % 2 == 0)
456 { 454 {
457 printf_P(PSTR("Only decrypt with odd keys\n")); 455 printf_P(PSTR("Only decrypt with odd keys\n"));
458 return; 456 return;
459 } 457 }
460 458
461 long_delay(200); 459 long_delay(200);
462 460
461
463 // check the signature 462 // check the signature
464 hmac_sha1_ctx_t ctx; 463 hmac_sha1(output, avr_keys[key_index], KEYLEN*8, &data[HMACLEN], AESLEN*8);
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 464
469 if (memcmp(output, data, HMACLEN) != 0) { 465 if (memcmp(output, data, HMACLEN) != 0) {
470 printf_P(PSTR("FAIL: hmac mismatch\n")); 466 printf_P(PSTR("FAIL: hmac mismatch\n"));
471 } 467 }
472 468