blob: 63cc17c71041835b73563355815eb7396c865248 [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é-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, 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
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +020034#define mbedtls_snprintf snprintf
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020038 !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
39 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000040int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000041{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020043 "MBEDLTS_MD_C and/or "
44 "MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000045 return( 0 );
46}
47#else
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020048
49#include "mbedtls/rsa.h"
50#include "mbedtls/md.h"
51
52#include <stdio.h>
53#include <string.h>
54
Paul Bakker5121ce52009-01-03 21:22:43 +000055int main( int argc, char *argv[] )
56{
57 FILE *f;
Paul Bakker23986e52011-04-24 08:57:21 +000058 int ret, c;
59 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +000061 unsigned char hash[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +020063 char filename[512];
Paul Bakker5121ce52009-01-03 21:22:43 +000064
65 ret = 1;
66 if( argc != 2 )
67 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 mbedtls_printf( "usage: rsa_verify <filename>\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Paul Bakkercce9d772011-11-18 14:26:47 +000070#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000072#endif
73
74 goto exit;
75 }
76
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_printf( "\n . Reading public key from rsa_pub.txt" );
Paul Bakker5121ce52009-01-03 21:22:43 +000078 fflush( stdout );
79
80 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
81 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 mbedtls_printf( " failed\n ! Could not open rsa_pub.txt\n" \
Paul Bakker5121ce52009-01-03 21:22:43 +000083 " ! Please run rsa_genkey first\n\n" );
84 goto exit;
85 }
86
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 if( ( ret = mbedtls_mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
90 ( ret = mbedtls_mpi_read_file( &rsa.E, 16, f ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000093 goto exit;
94 }
95
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +020096 rsa.len = ( mbedtls_mpi_bitlen( &rsa.N ) + 7 ) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +000097
98 fclose( f );
99
100 /*
101 * Extract the RSA signature from the text file
102 */
103 ret = 1;
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +0200104 mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +0200106 if( ( f = fopen( filename, "rb" ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 {
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +0200108 mbedtls_printf( "\n ! Could not open %s\n\n", filename );
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 goto exit;
110 }
111
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +0200112 i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 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é-Gonnard06d5d612015-05-28 16:23:18 +0200132 if( ( ret = mbedtls_md_file(
133 mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
134 argv[1], hash ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 goto exit;
138 }
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 if( ( ret = mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC,
141 MBEDTLS_MD_SHA256, 20, hash, buf ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_verify returned -0x%0x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 goto exit;
145 }
146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_printf( "\n . OK (the decrypted SHA-256 hash matches)\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
149 ret = 0;
150
151exit:
152
Paul Bakkercce9d772011-11-18 14:26:47 +0000153#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 fflush( stdout ); getchar();
156#endif
157
158 return( ret );
159}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
161 MBEDTLS_FS_IO */