blob: 4b43380744f16ede331698f7cb64bb437ab31a47 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +00002 * RSA/SHA-256 signature verification program
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * 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 Bakker5121ce52009-01-03 21:22:43 +000028
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 Bakker5121ce52009-01-03 21:22:43 +000035#include <string.h>
36#include <stdio.h>
37
Paul Bakker40e46942009-01-03 21:51:57 +000038#include "polarssl/rsa.h"
39#include "polarssl/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker5690efc2011-05-26 13:16:06 +000041#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +000042 !defined(POLARSSL_SHA256_C) || !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000043int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000044{
Paul Bakkercce9d772011-11-18 14:26:47 +000045 ((void) argc);
46 ((void) argv);
47
Rich Evansf90016a2015-01-19 14:26:37 +000048 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +000049 "POLARSSL_SHA256_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000050 return( 0 );
51}
52#else
Paul Bakker5121ce52009-01-03 21:22:43 +000053int main( int argc, char *argv[] )
54{
55 FILE *f;
Paul Bakker23986e52011-04-24 08:57:21 +000056 int ret, c;
57 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +000058 rsa_context rsa;
59 unsigned char hash[20];
Paul Bakker1d569582012-10-03 20:35:44 +000060 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +000061
62 ret = 1;
63 if( argc != 2 )
64 {
Rich Evansf90016a2015-01-19 14:26:37 +000065 polarssl_printf( "usage: rsa_verify <filename>\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Paul Bakkercce9d772011-11-18 14:26:47 +000067#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +000068 polarssl_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000069#endif
70
71 goto exit;
72 }
73
Rich Evansf90016a2015-01-19 14:26:37 +000074 polarssl_printf( "\n . Reading public key from rsa_pub.txt" );
Paul Bakker5121ce52009-01-03 21:22:43 +000075 fflush( stdout );
76
77 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
78 {
Rich Evansf90016a2015-01-19 14:26:37 +000079 polarssl_printf( " failed\n ! Could not open rsa_pub.txt\n" \
Paul Bakker5121ce52009-01-03 21:22:43 +000080 " ! Please run rsa_genkey first\n\n" );
81 goto exit;
82 }
83
Paul Bakkera802e1a2010-08-16 11:56:45 +000084 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000085
86 if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
87 ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
88 {
Rich Evansf90016a2015-01-19 14:26:37 +000089 polarssl_printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000090 goto exit;
91 }
92
93 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
94
95 fclose( f );
96
97 /*
98 * Extract the RSA signature from the text file
99 */
100 ret = 1;
101 i = strlen( argv[1] );
102 memcpy( argv[1] + i, ".sig", 5 );
103
104 if( ( f = fopen( argv[1], "rb" ) ) == NULL )
105 {
Rich Evansf90016a2015-01-19 14:26:37 +0000106 polarssl_printf( "\n ! Could not open %s\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 goto exit;
108 }
109
110 argv[1][i] = '\0', i = 0;
111
112 while( fscanf( f, "%02X", &c ) > 0 &&
113 i < (int) sizeof( buf ) )
114 buf[i++] = (unsigned char) c;
115
116 fclose( f );
117
118 if( i != rsa.len )
119 {
Rich Evansf90016a2015-01-19 14:26:37 +0000120 polarssl_printf( "\n ! Invalid RSA signature format\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 goto exit;
122 }
123
124 /*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000125 * Compute the SHA-256 hash of the input file and compare
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 * it with the hash decrypted from the RSA signature.
127 */
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000128 polarssl_printf( "\n . Verifying the RSA/SHA-256 signature" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 fflush( stdout );
130
131 if( ( ret = sha1_file( argv[1], hash ) ) != 0 )
132 {
Rich Evansf90016a2015-01-19 14:26:37 +0000133 polarssl_printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 goto exit;
135 }
136
Paul Bakker548957d2013-08-30 10:30:02 +0200137 if( ( ret = rsa_pkcs1_verify( &rsa, NULL, NULL, RSA_PUBLIC,
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000138 POLARSSL_MD_SHA256, 20, hash, buf ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 {
Rich Evansf90016a2015-01-19 14:26:37 +0000140 polarssl_printf( " failed\n ! rsa_pkcs1_verify returned -0x%0x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 goto exit;
142 }
143
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000144 polarssl_printf( "\n . OK (the decrypted SHA-256 hash matches)\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
146 ret = 0;
147
148exit:
149
Paul Bakkercce9d772011-11-18 14:26:47 +0000150#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000151 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 fflush( stdout ); getchar();
153#endif
154
155 return( ret );
156}
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000157#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA256_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000158 POLARSSL_FS_IO */