Mercurial > pihelp
comparison main.c @ 46:b1c27f1d6289
bootid hmac challenge
prog hmac
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 30 Jun 2013 23:34:24 +0800 |
parents | a0f2fcc6d9dd |
children | 747695bd4e0d |
comparison
equal
deleted
inserted
replaced
45:a0f2fcc6d9dd | 46:b1c27f1d6289 |
---|---|
764 static void | 764 static void |
765 cmd_bootid(const char *arg) | 765 cmd_bootid(const char *arg) |
766 { | 766 { |
767 uint8_t hmac[HMACLEN]; | 767 uint8_t hmac[HMACLEN]; |
768 uint8_t input[CHALLEN+sizeof(boot_id)]; | 768 uint8_t input[CHALLEN+sizeof(boot_id)]; |
769 | 769 |
770 if (strlen(arg) != CHALLEN*2) | |
771 { | |
772 printf_P(PSTR("Bad challenge\n")); | |
773 } | |
774 for (int i = 0, p = 0; i < CHALLEN; i++, p += 2) | |
775 { | |
776 input[i] = (from_hex(arg[p]) << 4) | from_hex(arg[p+1]); | |
777 } | |
778 memcpy(&input[CHALLEN], boot_id, sizeof(boot_id)); | |
779 | |
780 if (!boot_id_set) | 770 if (!boot_id_set) |
781 { | 771 { |
782 _Static_assert(sizeof(boot_id) == HMACLEN, "boot_id size correct"); | 772 _Static_assert(sizeof(boot_id) == HMACLEN, "boot_id size correct"); |
783 get_random(boot_id); | 773 get_random(boot_id); |
784 boot_id_set = 1; | 774 boot_id_set = 1; |
785 } | 775 } |
776 | |
777 if (strlen(arg) != CHALLEN*2) | |
778 { | |
779 printf_P(PSTR("Bad challenge\n")); | |
780 } | |
781 for (int i = 0, p = 0; i < CHALLEN; i++, p += 2) | |
782 { | |
783 input[i] = (from_hex(arg[p]) << 4) | from_hex(arg[p+1]); | |
784 } | |
785 memcpy(&input[CHALLEN], boot_id, sizeof(boot_id)); | |
786 | |
786 hmac_sha1(hmac, avr_keys[0], KEYLEN*8, input, sizeof(input)*8); | 787 hmac_sha1(hmac, avr_keys[0], KEYLEN*8, input, sizeof(input)*8); |
787 printf_P(PSTR("bootid: ")); | 788 printf_P(PSTR("bootid: ")); |
788 printhex(boot_id, sizeof(boot_id), stdout); | 789 printhex(boot_id, sizeof(boot_id), stdout); |
789 putchar(' '); | 790 putchar(' '); |
790 printhex(hmac, sizeof(hmac), stdout); | 791 printhex(hmac, sizeof(hmac), stdout); |
791 putchar('\n'); | 792 putchar('\n'); |
792 } | 793 } |
793 | 794 |
794 void(*bootloader)() __attribute__ ((noreturn)) = (void*)0x7800; | 795 void(*bootloader)() __attribute__ ((noreturn)) = (void*)0x7800; |
795 | 796 |
796 #ifndef PROG_PASSWORD | |
797 #define PROG_PASSWORD "Y2vvjxO5" | |
798 #endif | |
799 | |
800 static void | 797 static void |
801 cmd_prog(const char* arg) | 798 cmd_prog(const char* arg) |
802 { | 799 { |
803 if (!safe_str_eq(arg, PROG_PASSWORD)) | 800 uint8_t pw_hmac[HMACLEN]; |
801 uint8_t good_hmac[HMACLEN]; | |
802 | |
803 const static char prog_hmac[HMACLEN] PROGMEM = { | |
804 0x73, 0x4d, 0xa6, 0x3f, 0x3b, 0x7e, 0x4d, 0xa4, 0x65, 0xae, 0xea, 0xf9, 0x19, 0xbc, 0x4f, 0x45, 0xa7, 0x8d, 0x5a, 0xce, | |
805 }; | |
806 | |
807 memcpy_P(good_hmac, prog_hmac, HMACLEN); | |
808 hmac_sha1(pw_hmac, arg, strlen(arg)*8, "pihelp", strlen("pihelp")*8); | |
809 if (!safe_mem_eq(pw_hmac, good_hmac, HMACLEN)) | |
804 { | 810 { |
805 printf_P(PSTR("Bad prog password\n")); | 811 printf_P(PSTR("Bad prog password\n")); |
806 return; | 812 return; |
807 } | 813 } |
808 | 814 |