|
| 1 | + |
| 2 | +#include "uart_cmds.h" |
| 3 | +#include <stdint.h> |
| 4 | +#include <sys/types.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <unistd.h> |
| 7 | +#include <string.h> |
| 8 | +#include <esp8266.h> |
| 9 | +#include <esp/uart.h> |
| 10 | +#include <stdio.h> |
| 11 | +#include "FreeRTOS.h" |
| 12 | +#include "task.h" |
| 13 | + |
| 14 | +// make test hash |
| 15 | +//> openssl sha -sha256 cmd_pub.c |
| 16 | +// sign test hash |
| 17 | +//> openssl sha -sha256 -sign cert.key -hex cmd_pub.c |
| 18 | + |
| 19 | +void get_line(char *buf, size_t buflen) |
| 20 | +{ |
| 21 | + char ch; |
| 22 | + char cmd[200]; |
| 23 | + int i = 0; |
| 24 | + while(1) { |
| 25 | + if (read(0, (void*)&ch, 1)) { // 0 is stdin |
| 26 | + printf("%c", ch); |
| 27 | + if (ch == '\n' || ch == '\r') { |
| 28 | + cmd[i] = 0; |
| 29 | + i = 0; |
| 30 | + printf("\n"); |
| 31 | + strcpy(buf, cmd); |
| 32 | + return; |
| 33 | + } else { |
| 34 | + if (i < sizeof(cmd)) cmd[i++] = ch; |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +ATCA_STATUS cmd_verify(uint32_t argc, char *argv[]) |
| 41 | +{ |
| 42 | + ATCA_STATUS status; |
| 43 | + if (argc >= 2) { |
| 44 | + uint8_t slot_num = atoi(argv[1]); |
| 45 | + if (slot_num < 0x8 || slot_num > 0xF){ |
| 46 | + LOG("Invalid slot number %d; must be between 8 and 15 for public keys", slot_num); |
| 47 | + return ATCA_BAD_PARAM; |
| 48 | + } |
| 49 | + |
| 50 | + printf("Verifying with public key in slot %d\n", slot_num); |
| 51 | + char hash[100]; uint8_t hashbin[100]; int hashlen = sizeof(hashbin); |
| 52 | + char sig[200]; uint8_t sigbin[200]; int siglen = sizeof(sigbin); |
| 53 | + |
| 54 | + printf("Please paste hash in the terminal (hex format)\n"); |
| 55 | + get_line(hash, sizeof(hash)); |
| 56 | + |
| 57 | + status = atcab_hex2bin(hash, strlen(hash), hashbin, &hashlen); |
| 58 | + if(status != ATCA_SUCCESS){ |
| 59 | + RETURN(status, "Could not parse hash hex"); |
| 60 | + } |
| 61 | + |
| 62 | + printf("Please paste signature in the terminal (hex format)\n"); |
| 63 | + get_line(sig, sizeof(sig)); |
| 64 | + |
| 65 | + status = atcab_hex2bin(sig, strlen(sig), sigbin, &siglen); |
| 66 | + if(status != ATCA_SUCCESS){ |
| 67 | + RETURN(status, "Could not parse signature hex"); |
| 68 | + } |
| 69 | + |
| 70 | + bool isVerified = false; |
| 71 | + printf("Trying to verify with\n"); |
| 72 | + atcab_printbin_label((const uint8_t*)"hash ", hashbin, hashlen); |
| 73 | + printf("Len %d\n", hashlen); |
| 74 | + atcab_printbin_label((const uint8_t*)"sig ", sigbin, siglen); |
| 75 | + printf("Len %d\n", siglen); |
| 76 | + |
| 77 | + uint8_t atca_sig[ATCA_SIG_SIZE] = {0}; |
| 78 | + if(!parse_asn1_signature(sigbin, siglen, atca_sig)) |
| 79 | + { |
| 80 | + RETURN(ATCA_PARSE_ERROR, "Could not parse ASN.1 signature"); |
| 81 | + } |
| 82 | + |
| 83 | + atcab_printbin_label((const uint8_t*)"atca_sig ", atca_sig, ATCA_SIG_SIZE); |
| 84 | + printf("Len %d\n", ATCA_SIG_SIZE); |
| 85 | + |
| 86 | + status = atcab_verify_stored(hashbin, atca_sig, slot_num, &isVerified); |
| 87 | + if(status != ATCA_SUCCESS){ |
| 88 | + RETURN(status, "Could not verify signature"); |
| 89 | + } |
| 90 | + |
| 91 | + printf(isVerified ? "Signature is valid\n" : "Signature is invalid\n"); |
| 92 | + RETURN(status, "Done"); |
| 93 | + } else { |
| 94 | + printf("Error: missing slot number.\n"); |
| 95 | + return ATCA_BAD_PARAM; |
| 96 | + } |
| 97 | +} |
0 commit comments