comparison main.c @ 11:e83b35e864d7

hmac and decrypt keys differ now
author Matt Johnston <matt@ucc.asn.au>
date Wed, 12 Jun 2013 23:00:25 +0800
parents 03da5ff767e9
children aec45c673a60
comparison
equal deleted inserted replaced
10:439b7aaaec9e 11:e83b35e864d7
419 } 419 }
420 420
421 static void 421 static void
422 cmd_hmac(const char *params) 422 cmd_hmac(const char *params)
423 { 423 {
424 uint8_t indata[HMACLEN]; 424 uint8_t indata[2+HMACLEN] = {'H', ':'};
425 uint8_t outdata[HMACLEN]; 425 uint8_t outdata[HMACLEN];
426 uint8_t key_index; 426 uint8_t key_index;
427 if (parse_key(params, &key_index, indata, sizeof(indata)) != 0) 427 if (parse_key(params, &key_index, &indata[2], HMACLEN) != 0)
428 { 428 {
429 printf_P(PSTR("FAIL: Bad input\n")); 429 printf_P(PSTR("FAIL: Bad input\n"));
430 return;
431 }
432
433 if (key_index % 2 != 0)
434 {
435 printf_P(PSTR("Only hmac with even keys\n"));
436 return; 430 return;
437 } 431 }
438 432
439 #ifndef SIM_DEBUG 433 #ifndef SIM_DEBUG
440 long_delay(200); 434 long_delay(200);
441 #endif 435 #endif
442 436
443 hmac_sha1(outdata, avr_keys[key_index], KEYLEN*8, indata, HMACLEN*8); 437 hmac_sha1(outdata, avr_keys[key_index], KEYLEN*8, indata, sizeof(indata)*8);
444
445 printf_P(PSTR("HMAC: ")); 438 printf_P(PSTR("HMAC: "));
446 printhex(outdata, HMACLEN, stdout); 439 printhex(outdata, HMACLEN, stdout);
447 fputc('\n', stdout); 440 fputc('\n', stdout);
448 } 441 }
449 442
450 static void 443 static void
451 cmd_decrypt(const char *params) 444 cmd_decrypt(const char *params)
452 { 445 {
453 uint8_t data[HMACLEN+AESLEN]; 446 uint8_t indata[HMACLEN+AESLEN]; // XXX
454 uint8_t output[HMACLEN]; 447 // a temporary buffer
448 uint8_t output[HMACLEN] = {'D', ':'};
449 _Static_assert(AESLEN+2 <= sizeof(output), "sufficient output buffer");
455 uint8_t key_index; 450 uint8_t key_index;
456 if (parse_key(params, &key_index, data, sizeof(data)) != 0) 451 if (parse_key(params, &key_index, indata, sizeof(indata)) != 0)
457 { 452 {
458 printf_P(PSTR("FAIL: Bad input\n")); 453 printf_P(PSTR("FAIL: Bad input\n"));
459 return;
460 }
461
462 if (key_index % 2 == 0)
463 {
464 printf_P(PSTR("Only decrypt with odd keys\n"));
465 return; 454 return;
466 } 455 }
467 456
468 #ifndef SIM_DEBUG 457 #ifndef SIM_DEBUG
469 long_delay(200); 458 long_delay(200);
470 #endif 459 #endif
471 460
472 // check the signature 461 // check the signature
473 hmac_sha1(output, avr_keys[key_index+1], KEYLEN*8, &data[HMACLEN], AESLEN*8); 462 memcpy(&output[2], &indata[HMACLEN], AESLEN);
474 463 hmac_sha1(output, avr_keys[key_index+1], KEYLEN*8, output, (2+AESLEN)*8);
475 if (memcmp(output, data, HMACLEN) != 0) { 464
465 if (memcmp(output, indata, HMACLEN) != 0) {
476 printf_P(PSTR("FAIL: hmac mismatch\n")); 466 printf_P(PSTR("FAIL: hmac mismatch\n"));
477 } 467 }
478 468
479 uint8_t expkey[AES_EXPKEY_SIZE]; 469 uint8_t tmpbuf[256];
480 ExpandKey(avr_keys[key_index], expkey); 470 aesInit(avr_keys[key_index], tmpbuf);
481 Decrypt(&data[HMACLEN], expkey, output); 471 aesDecrypt(&indata[HMACLEN], NULL);
482 472
483 printf_P(PSTR("DECRYPTED: ")); 473 printf_P(PSTR("DECRYPTED: "));
484 printhex(output, AESLEN, stdout); 474 printhex(output, AESLEN, stdout);
485 fputc('\n', stdout); 475 fputc('\n', stdout);
486 } 476 }