| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  RSA/SHA-1 signature verification program | 
|  | 3 | * | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 4 | *  Copyright (C) 2006-2011, Brainspark B.V. | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * | 
|  | 6 | *  This file is part of PolarSSL (http://www.polarssl.org) | 
| Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 7 | *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 8 | * | 
| Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 9 | *  All rights reserved. | 
| Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 10 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11 | *  This program is free software; you can redistribute it and/or modify | 
|  | 12 | *  it under the terms of the GNU General Public License as published by | 
|  | 13 | *  the Free Software Foundation; either version 2 of the License, or | 
|  | 14 | *  (at your option) any later version. | 
|  | 15 | * | 
|  | 16 | *  This program is distributed in the hope that it will be useful, | 
|  | 17 | *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 18 | *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 19 | *  GNU General Public License for more details. | 
|  | 20 | * | 
|  | 21 | *  You should have received a copy of the GNU General Public License along | 
|  | 22 | *  with this program; if not, write to the Free Software Foundation, Inc., | 
|  | 23 | *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | 
|  | 24 | */ | 
|  | 25 |  | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #if !defined(POLARSSL_CONFIG_FILE) | 
| Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 27 | #include "polarssl/config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 28 | #else | 
|  | 29 | #include POLARSSL_CONFIG_FILE | 
|  | 30 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 31 |  | 
|  | 32 | #include <string.h> | 
|  | 33 | #include <stdio.h> | 
|  | 34 |  | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 35 | #include "polarssl/rsa.h" | 
|  | 36 | #include "polarssl/sha1.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 37 |  | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 38 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) ||  \ | 
|  | 39 | !defined(POLARSSL_SHA1_C) || !defined(POLARSSL_FS_IO) | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 40 | int main( int argc, char *argv[] ) | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 41 | { | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 42 | ((void) argc); | 
|  | 43 | ((void) argv); | 
|  | 44 |  | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 45 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or " | 
|  | 46 | "POLARSSL_SHA1_C and/or POLARSSL_FS_IO not defined.\n"); | 
|  | 47 | return( 0 ); | 
|  | 48 | } | 
|  | 49 | #else | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 50 | int main( int argc, char *argv[] ) | 
|  | 51 | { | 
|  | 52 | FILE *f; | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 53 | int ret, c; | 
|  | 54 | size_t i; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 55 | rsa_context rsa; | 
|  | 56 | unsigned char hash[20]; | 
| Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 57 | unsigned char buf[POLARSSL_MPI_MAX_SIZE]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 58 |  | 
|  | 59 | ret = 1; | 
|  | 60 | if( argc != 2 ) | 
|  | 61 | { | 
|  | 62 | printf( "usage: rsa_verify <filename>\n" ); | 
|  | 63 |  | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 64 | #if defined(_WIN32) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 65 | printf( "\n" ); | 
|  | 66 | #endif | 
|  | 67 |  | 
|  | 68 | goto exit; | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | printf( "\n  . Reading public key from rsa_pub.txt" ); | 
|  | 72 | fflush( stdout ); | 
|  | 73 |  | 
|  | 74 | if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL ) | 
|  | 75 | { | 
|  | 76 | printf( " failed\n  ! Could not open rsa_pub.txt\n" \ | 
|  | 77 | "  ! Please run rsa_genkey first\n\n" ); | 
|  | 78 | goto exit; | 
|  | 79 | } | 
|  | 80 |  | 
| Paul Bakker | a802e1a | 2010-08-16 11:56:45 +0000 | [diff] [blame] | 81 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 82 |  | 
|  | 83 | if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 || | 
|  | 84 | ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 ) | 
|  | 85 | { | 
|  | 86 | printf( " failed\n  ! mpi_read_file returned %d\n\n", ret ); | 
|  | 87 | goto exit; | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3; | 
|  | 91 |  | 
|  | 92 | fclose( f ); | 
|  | 93 |  | 
|  | 94 | /* | 
|  | 95 | * Extract the RSA signature from the text file | 
|  | 96 | */ | 
|  | 97 | ret = 1; | 
|  | 98 | i = strlen( argv[1] ); | 
|  | 99 | memcpy( argv[1] + i, ".sig", 5 ); | 
|  | 100 |  | 
|  | 101 | if( ( f = fopen( argv[1], "rb" ) ) == NULL ) | 
|  | 102 | { | 
|  | 103 | printf( "\n  ! Could not open %s\n\n", argv[1] ); | 
|  | 104 | goto exit; | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | argv[1][i] = '\0', i = 0; | 
|  | 108 |  | 
|  | 109 | while( fscanf( f, "%02X", &c ) > 0 && | 
|  | 110 | i < (int) sizeof( buf ) ) | 
|  | 111 | buf[i++] = (unsigned char) c; | 
|  | 112 |  | 
|  | 113 | fclose( f ); | 
|  | 114 |  | 
|  | 115 | if( i != rsa.len ) | 
|  | 116 | { | 
|  | 117 | printf( "\n  ! Invalid RSA signature format\n\n" ); | 
|  | 118 | goto exit; | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | /* | 
|  | 122 | * Compute the SHA-1 hash of the input file and compare | 
|  | 123 | * it with the hash decrypted from the RSA signature. | 
|  | 124 | */ | 
|  | 125 | printf( "\n  . Verifying the RSA/SHA-1 signature" ); | 
|  | 126 | fflush( stdout ); | 
|  | 127 |  | 
|  | 128 | if( ( ret = sha1_file( argv[1], hash ) ) != 0 ) | 
|  | 129 | { | 
|  | 130 | printf( " failed\n  ! Could not open or read %s\n\n", argv[1] ); | 
|  | 131 | goto exit; | 
|  | 132 | } | 
|  | 133 |  | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 134 | if( ( ret = rsa_pkcs1_verify( &rsa, NULL, NULL, RSA_PUBLIC, | 
|  | 135 | POLARSSL_MD_SHA1, 20, hash, buf ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 136 | { | 
| Paul Bakker | 5ef9db2 | 2012-09-27 13:19:22 +0000 | [diff] [blame] | 137 | printf( " failed\n  ! rsa_pkcs1_verify returned -0x%0x\n\n", -ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 138 | goto exit; | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | printf( "\n  . OK (the decrypted SHA-1 hash matches)\n\n" ); | 
|  | 142 |  | 
|  | 143 | ret = 0; | 
|  | 144 |  | 
|  | 145 | exit: | 
|  | 146 |  | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 147 | #if defined(_WIN32) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 148 | printf( "  + Press Enter to exit this program.\n" ); | 
|  | 149 | fflush( stdout ); getchar(); | 
|  | 150 | #endif | 
|  | 151 |  | 
|  | 152 | return( ret ); | 
|  | 153 | } | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 154 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA1_C && | 
|  | 155 | POLARSSL_FS_IO */ |