blob: 55f977ccd404daefbad3af489202e08584650c95 [file] [log] [blame]
Paul Bakker940f9ce2013-09-18 15:34:57 +02001/*
2 * Public key-based signature verification program
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakker940f9ce2013-09-18 15:34:57 +02005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker940f9ce2013-09-18 15:34:57 +02007 *
Paul Bakker940f9ce2013-09-18 15:34:57 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker940f9ce2013-09-18 15:34:57 +020028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000033#endif
34
Paul Bakker940f9ce2013-09-18 15:34:57 +020035#include <string.h>
36#include <stdio.h>
37
Paul Bakker940f9ce2013-09-18 15:34:57 +020038#include "polarssl/error.h"
39#include "polarssl/md.h"
40#include "polarssl/pk.h"
41#include "polarssl/sha1.h"
42
43#if defined _MSC_VER && !defined snprintf
44#define snprintf _snprintf
45#endif
46
47#if !defined(POLARSSL_BIGNUM_C) || \
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +000048 !defined(POLARSSL_SHA256_C) || !defined(POLARSSL_PK_PARSE_C) || \
Paul Bakker940f9ce2013-09-18 15:34:57 +020049 !defined(POLARSSL_FS_IO)
50int main( int argc, char *argv[] )
51{
52 ((void) argc);
53 ((void) argv);
54
Rich Evansf90016a2015-01-19 14:26:37 +000055 polarssl_printf("POLARSSL_BIGNUM_C and/or "
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +000056 "POLARSSL_SHA256_C and/or POLARSSL_PK_PARSE_C and/or "
Paul Bakker940f9ce2013-09-18 15:34:57 +020057 "POLARSSL_FS_IO not defined.\n");
58 return( 0 );
59}
60#else
61int main( int argc, char *argv[] )
62{
63 FILE *f;
Paul Bakker0c226102014-04-17 16:02:36 +020064 int ret = 1;
Paul Bakker940f9ce2013-09-18 15:34:57 +020065 size_t i;
66 pk_context pk;
67 unsigned char hash[20];
68 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
69 char filename[512];
70
Paul Bakker0c226102014-04-17 16:02:36 +020071 pk_init( &pk );
72
Paul Bakker940f9ce2013-09-18 15:34:57 +020073 if( argc != 3 )
74 {
Rich Evansf90016a2015-01-19 14:26:37 +000075 polarssl_printf( "usage: pk_verify <key_file> <filename>\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +020076
77#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +000078 polarssl_printf( "\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +020079#endif
80
81 goto exit;
82 }
83
Rich Evansf90016a2015-01-19 14:26:37 +000084 polarssl_printf( "\n . Reading public key from '%s'", argv[1] );
Paul Bakker940f9ce2013-09-18 15:34:57 +020085 fflush( stdout );
86
Paul Bakker940f9ce2013-09-18 15:34:57 +020087 if( ( ret = pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
88 {
Rich Evansf90016a2015-01-19 14:26:37 +000089 polarssl_printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +020090 goto exit;
91 }
92
93 /*
94 * Extract the signature from the text file
95 */
96 ret = 1;
97 snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
98
99 if( ( f = fopen( filename, "rb" ) ) == NULL )
100 {
Rich Evansf90016a2015-01-19 14:26:37 +0000101 polarssl_printf( "\n ! Could not open %s\n\n", filename );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200102 goto exit;
103 }
104
105
106 i = fread( buf, 1, sizeof(buf), f );
107
108 fclose( f );
109
110 /*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000111 * Compute the SHA-256 hash of the input file and compare
Paul Bakker940f9ce2013-09-18 15:34:57 +0200112 * it with the hash decrypted from the signature.
113 */
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000114 polarssl_printf( "\n . Verifying the SHA-256 signature" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200115 fflush( stdout );
116
117 if( ( ret = sha1_file( argv[2], hash ) ) != 0 )
118 {
Rich Evansf90016a2015-01-19 14:26:37 +0000119 polarssl_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200120 goto exit;
121 }
122
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000123 if( ( ret = pk_verify( &pk, POLARSSL_MD_SHA256, hash, 0,
Paul Bakker940f9ce2013-09-18 15:34:57 +0200124 buf, i ) ) != 0 )
125 {
Rich Evansf90016a2015-01-19 14:26:37 +0000126 polarssl_printf( " failed\n ! pk_verify returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200127 goto exit;
128 }
129
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000130 polarssl_printf( "\n . OK (the decrypted SHA-256 hash matches)\n\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200131
132 ret = 0;
133
134exit:
135 pk_free( &pk );
136
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200137#if defined(POLARSSL_ERROR_C)
138 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
Rich Evansf90016a2015-01-19 14:26:37 +0000139 polarssl_printf( " ! Last error was: %s\n", buf );
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200140#endif
141
Paul Bakker940f9ce2013-09-18 15:34:57 +0200142#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000143 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200144 fflush( stdout ); getchar();
145#endif
146
147 return( ret );
148}
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000149#endif /* POLARSSL_BIGNUM_C && POLARSSL_SHA256_C &&
Paul Bakker940f9ce2013-09-18 15:34:57 +0200150 POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */