blob: 88ad575b4376ba0dba3c027c3f618cca60628726 [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
Rich Evans783d9d12015-01-30 11:11:57 +000032#define polarssl_snprintf snprintf
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Paul Bakker940f9ce2013-09-18 15:34:57 +020036#include <string.h>
37#include <stdio.h>
38
Paul Bakker940f9ce2013-09-18 15:34:57 +020039#include "polarssl/error.h"
40#include "polarssl/md.h"
41#include "polarssl/pk.h"
42#include "polarssl/sha1.h"
43
44#if defined _MSC_VER && !defined snprintf
45#define snprintf _snprintf
46#endif
47
48#if !defined(POLARSSL_BIGNUM_C) || \
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +000049 !defined(POLARSSL_SHA256_C) || !defined(POLARSSL_PK_PARSE_C) || \
Paul Bakker940f9ce2013-09-18 15:34:57 +020050 !defined(POLARSSL_FS_IO)
51int main( int argc, char *argv[] )
52{
53 ((void) argc);
54 ((void) argv);
55
Rich Evansf90016a2015-01-19 14:26:37 +000056 polarssl_printf("POLARSSL_BIGNUM_C and/or "
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +000057 "POLARSSL_SHA256_C and/or POLARSSL_PK_PARSE_C and/or "
Paul Bakker940f9ce2013-09-18 15:34:57 +020058 "POLARSSL_FS_IO not defined.\n");
59 return( 0 );
60}
61#else
62int main( int argc, char *argv[] )
63{
64 FILE *f;
Paul Bakker0c226102014-04-17 16:02:36 +020065 int ret = 1;
Paul Bakker940f9ce2013-09-18 15:34:57 +020066 size_t i;
67 pk_context pk;
68 unsigned char hash[20];
69 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
70 char filename[512];
71
Paul Bakker0c226102014-04-17 16:02:36 +020072 pk_init( &pk );
73
Paul Bakker940f9ce2013-09-18 15:34:57 +020074 if( argc != 3 )
75 {
Rich Evansf90016a2015-01-19 14:26:37 +000076 polarssl_printf( "usage: pk_verify <key_file> <filename>\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +020077
78#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +000079 polarssl_printf( "\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +020080#endif
81
82 goto exit;
83 }
84
Rich Evansf90016a2015-01-19 14:26:37 +000085 polarssl_printf( "\n . Reading public key from '%s'", argv[1] );
Paul Bakker940f9ce2013-09-18 15:34:57 +020086 fflush( stdout );
87
Paul Bakker940f9ce2013-09-18 15:34:57 +020088 if( ( ret = pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
89 {
Rich Evansf90016a2015-01-19 14:26:37 +000090 polarssl_printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +020091 goto exit;
92 }
93
94 /*
95 * Extract the signature from the text file
96 */
97 ret = 1;
Rich Evans783d9d12015-01-30 11:11:57 +000098 polarssl_snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
Paul Bakker940f9ce2013-09-18 15:34:57 +020099
100 if( ( f = fopen( filename, "rb" ) ) == NULL )
101 {
Rich Evansf90016a2015-01-19 14:26:37 +0000102 polarssl_printf( "\n ! Could not open %s\n\n", filename );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200103 goto exit;
104 }
105
106
107 i = fread( buf, 1, sizeof(buf), f );
108
109 fclose( f );
110
111 /*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000112 * Compute the SHA-256 hash of the input file and compare
Paul Bakker940f9ce2013-09-18 15:34:57 +0200113 * it with the hash decrypted from the signature.
114 */
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000115 polarssl_printf( "\n . Verifying the SHA-256 signature" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200116 fflush( stdout );
117
118 if( ( ret = sha1_file( argv[2], hash ) ) != 0 )
119 {
Rich Evansf90016a2015-01-19 14:26:37 +0000120 polarssl_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200121 goto exit;
122 }
123
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000124 if( ( ret = pk_verify( &pk, POLARSSL_MD_SHA256, hash, 0,
Paul Bakker940f9ce2013-09-18 15:34:57 +0200125 buf, i ) ) != 0 )
126 {
Rich Evansf90016a2015-01-19 14:26:37 +0000127 polarssl_printf( " failed\n ! pk_verify returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200128 goto exit;
129 }
130
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000131 polarssl_printf( "\n . OK (the decrypted SHA-256 hash matches)\n\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200132
133 ret = 0;
134
135exit:
136 pk_free( &pk );
137
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200138#if defined(POLARSSL_ERROR_C)
139 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
Rich Evansf90016a2015-01-19 14:26:37 +0000140 polarssl_printf( " ! Last error was: %s\n", buf );
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200141#endif
142
Paul Bakker940f9ce2013-09-18 15:34:57 +0200143#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000144 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200145 fflush( stdout ); getchar();
146#endif
147
148 return( ret );
149}
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000150#endif /* POLARSSL_BIGNUM_C && POLARSSL_SHA256_C &&
Paul Bakker940f9ce2013-09-18 15:34:57 +0200151 POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */