blob: bed3261cc33a9d9b716f41973caa0625991e50ca [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 Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Rich Evans18b78c72015-02-11 14:06:19 +000036#if defined(POLARSSL_BIGNUM_C) &&\
37 defined(POLARSSL_SHA256_C) && defined(POLARSSL_PK_PARSE_C) &&\
38 defined(POLARSSL_FS_IO)
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
Rich Evans18b78c72015-02-11 14:06:19 +000044#include <stdio.h>
45#include <string.h>
46#endif
47
Paul Bakker940f9ce2013-09-18 15:34:57 +020048#if defined _MSC_VER && !defined snprintf
49#define snprintf _snprintf
50#endif
51
52#if !defined(POLARSSL_BIGNUM_C) || \
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +000053 !defined(POLARSSL_SHA256_C) || !defined(POLARSSL_PK_PARSE_C) || \
Paul Bakker940f9ce2013-09-18 15:34:57 +020054 !defined(POLARSSL_FS_IO)
55int main( int argc, char *argv[] )
56{
57 ((void) argc);
58 ((void) argv);
59
Rich Evansf90016a2015-01-19 14:26:37 +000060 polarssl_printf("POLARSSL_BIGNUM_C and/or "
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +000061 "POLARSSL_SHA256_C and/or POLARSSL_PK_PARSE_C and/or "
Paul Bakker940f9ce2013-09-18 15:34:57 +020062 "POLARSSL_FS_IO not defined.\n");
63 return( 0 );
64}
65#else
66int main( int argc, char *argv[] )
67{
68 FILE *f;
Paul Bakker0c226102014-04-17 16:02:36 +020069 int ret = 1;
Paul Bakker940f9ce2013-09-18 15:34:57 +020070 size_t i;
71 pk_context pk;
72 unsigned char hash[20];
73 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
74 char filename[512];
75
Paul Bakker0c226102014-04-17 16:02:36 +020076 pk_init( &pk );
77
Paul Bakker940f9ce2013-09-18 15:34:57 +020078 if( argc != 3 )
79 {
Rich Evansf90016a2015-01-19 14:26:37 +000080 polarssl_printf( "usage: pk_verify <key_file> <filename>\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +020081
82#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +000083 polarssl_printf( "\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +020084#endif
85
86 goto exit;
87 }
88
Rich Evansf90016a2015-01-19 14:26:37 +000089 polarssl_printf( "\n . Reading public key from '%s'", argv[1] );
Paul Bakker940f9ce2013-09-18 15:34:57 +020090 fflush( stdout );
91
Paul Bakker940f9ce2013-09-18 15:34:57 +020092 if( ( ret = pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
93 {
Rich Evansf90016a2015-01-19 14:26:37 +000094 polarssl_printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +020095 goto exit;
96 }
97
98 /*
99 * Extract the signature from the text file
100 */
101 ret = 1;
102 snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
103
104 if( ( f = fopen( filename, "rb" ) ) == NULL )
105 {
Rich Evansf90016a2015-01-19 14:26:37 +0000106 polarssl_printf( "\n ! Could not open %s\n\n", filename );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200107 goto exit;
108 }
109
110
111 i = fread( buf, 1, sizeof(buf), f );
112
113 fclose( f );
114
115 /*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000116 * Compute the SHA-256 hash of the input file and compare
Paul Bakker940f9ce2013-09-18 15:34:57 +0200117 * it with the hash decrypted from the signature.
118 */
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000119 polarssl_printf( "\n . Verifying the SHA-256 signature" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200120 fflush( stdout );
121
122 if( ( ret = sha1_file( argv[2], hash ) ) != 0 )
123 {
Rich Evansf90016a2015-01-19 14:26:37 +0000124 polarssl_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200125 goto exit;
126 }
127
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000128 if( ( ret = pk_verify( &pk, POLARSSL_MD_SHA256, hash, 0,
Paul Bakker940f9ce2013-09-18 15:34:57 +0200129 buf, i ) ) != 0 )
130 {
Rich Evansf90016a2015-01-19 14:26:37 +0000131 polarssl_printf( " failed\n ! pk_verify returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200132 goto exit;
133 }
134
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000135 polarssl_printf( "\n . OK (the decrypted SHA-256 hash matches)\n\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200136
137 ret = 0;
138
139exit:
140 pk_free( &pk );
141
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200142#if defined(POLARSSL_ERROR_C)
143 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
Rich Evansf90016a2015-01-19 14:26:37 +0000144 polarssl_printf( " ! Last error was: %s\n", buf );
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200145#endif
146
Paul Bakker940f9ce2013-09-18 15:34:57 +0200147#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000148 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200149 fflush( stdout ); getchar();
150#endif
151
152 return( ret );
153}
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000154#endif /* POLARSSL_BIGNUM_C && POLARSSL_SHA256_C &&
Paul Bakker940f9ce2013-09-18 15:34:57 +0200155 POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */