blob: 3d5d719701dcdb48ccee5584f3d4ac73a9efdfc0 [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é-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000031#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
37 defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/rsa.h"
39#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Rich Evans18b78c72015-02-11 14:06:19 +000041#include <stdio.h>
42#include <string.h>
43#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
46 !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000047int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000048{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
50 "MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000051 return( 0 );
52}
53#else
Paul Bakker5121ce52009-01-03 21:22:43 +000054int main( int argc, char *argv[] )
55{
56 FILE *f;
Paul Bakker23986e52011-04-24 08:57:21 +000057 int ret, c;
58 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +000060 unsigned char hash[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +000062
63 ret = 1;
64 if( argc != 2 )
65 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 mbedtls_printf( "usage: rsa_verify <filename>\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Paul Bakkercce9d772011-11-18 14:26:47 +000068#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000070#endif
71
72 goto exit;
73 }
74
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 mbedtls_printf( "\n . Reading public key from rsa_pub.txt" );
Paul Bakker5121ce52009-01-03 21:22:43 +000076 fflush( stdout );
77
78 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
79 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 mbedtls_printf( " failed\n ! Could not open rsa_pub.txt\n" \
Paul Bakker5121ce52009-01-03 21:22:43 +000081 " ! Please run rsa_genkey first\n\n" );
82 goto exit;
83 }
84
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 if( ( ret = mbedtls_mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
88 ( ret = mbedtls_mpi_read_file( &rsa.E, 16, f ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000091 goto exit;
92 }
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 rsa.len = ( mbedtls_mpi_msb( &rsa.N ) + 7 ) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96 fclose( f );
97
98 /*
99 * Extract the RSA signature from the text file
100 */
101 ret = 1;
102 i = strlen( argv[1] );
103 memcpy( argv[1] + i, ".sig", 5 );
104
105 if( ( f = fopen( argv[1], "rb" ) ) == NULL )
106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_printf( "\n ! Could not open %s\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 goto exit;
109 }
110
111 argv[1][i] = '\0', i = 0;
112
113 while( fscanf( f, "%02X", &c ) > 0 &&
114 i < (int) sizeof( buf ) )
115 buf[i++] = (unsigned char) c;
116
117 fclose( f );
118
119 if( i != rsa.len )
120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 mbedtls_printf( "\n ! Invalid RSA signature format\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 goto exit;
123 }
124
125 /*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000126 * Compute the SHA-256 hash of the input file and compare
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 * it with the hash decrypted from the RSA signature.
128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_printf( "\n . Verifying the RSA/SHA-256 signature" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 fflush( stdout );
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 if( ( ret = mbedtls_sha1_file( argv[1], hash ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 goto exit;
136 }
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 if( ( ret = mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC,
139 MBEDTLS_MD_SHA256, 20, hash, buf ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_verify returned -0x%0x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 goto exit;
143 }
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_printf( "\n . OK (the decrypted SHA-256 hash matches)\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147 ret = 0;
148
149exit:
150
Paul Bakkercce9d772011-11-18 14:26:47 +0000151#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 fflush( stdout ); getchar();
154#endif
155
156 return( ret );
157}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
159 MBEDTLS_FS_IO */