Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * RSA/SHA-1 signature verification program |
| 3 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 4 | * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine |
| 5 | * |
Paul Bakker | 27db1f5 | 2009-01-25 15:27:00 +0000 | [diff] [blame] | 6 | * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org> |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7 | * |
| 8 | * 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 | |
| 23 | #ifndef _CRT_SECURE_NO_DEPRECATE |
| 24 | #define _CRT_SECURE_NO_DEPRECATE 1 |
| 25 | #endif |
| 26 | |
| 27 | #include <string.h> |
| 28 | #include <stdio.h> |
| 29 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 30 | #include "polarssl/rsa.h" |
| 31 | #include "polarssl/sha1.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 32 | |
| 33 | int main( int argc, char *argv[] ) |
| 34 | { |
| 35 | FILE *f; |
| 36 | int ret, i, c; |
| 37 | rsa_context rsa; |
| 38 | unsigned char hash[20]; |
| 39 | unsigned char buf[512]; |
| 40 | |
| 41 | ret = 1; |
| 42 | if( argc != 2 ) |
| 43 | { |
| 44 | printf( "usage: rsa_verify <filename>\n" ); |
| 45 | |
| 46 | #ifdef WIN32 |
| 47 | printf( "\n" ); |
| 48 | #endif |
| 49 | |
| 50 | goto exit; |
| 51 | } |
| 52 | |
| 53 | printf( "\n . Reading public key from rsa_pub.txt" ); |
| 54 | fflush( stdout ); |
| 55 | |
| 56 | if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL ) |
| 57 | { |
| 58 | printf( " failed\n ! Could not open rsa_pub.txt\n" \ |
| 59 | " ! Please run rsa_genkey first\n\n" ); |
| 60 | goto exit; |
| 61 | } |
| 62 | |
| 63 | rsa_init( &rsa, RSA_PKCS_V15, 0, NULL, NULL ); |
| 64 | |
| 65 | if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 || |
| 66 | ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 ) |
| 67 | { |
| 68 | printf( " failed\n ! mpi_read_file returned %d\n\n", ret ); |
| 69 | goto exit; |
| 70 | } |
| 71 | |
| 72 | rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3; |
| 73 | |
| 74 | fclose( f ); |
| 75 | |
| 76 | /* |
| 77 | * Extract the RSA signature from the text file |
| 78 | */ |
| 79 | ret = 1; |
| 80 | i = strlen( argv[1] ); |
| 81 | memcpy( argv[1] + i, ".sig", 5 ); |
| 82 | |
| 83 | if( ( f = fopen( argv[1], "rb" ) ) == NULL ) |
| 84 | { |
| 85 | printf( "\n ! Could not open %s\n\n", argv[1] ); |
| 86 | goto exit; |
| 87 | } |
| 88 | |
| 89 | argv[1][i] = '\0', i = 0; |
| 90 | |
| 91 | while( fscanf( f, "%02X", &c ) > 0 && |
| 92 | i < (int) sizeof( buf ) ) |
| 93 | buf[i++] = (unsigned char) c; |
| 94 | |
| 95 | fclose( f ); |
| 96 | |
| 97 | if( i != rsa.len ) |
| 98 | { |
| 99 | printf( "\n ! Invalid RSA signature format\n\n" ); |
| 100 | goto exit; |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Compute the SHA-1 hash of the input file and compare |
| 105 | * it with the hash decrypted from the RSA signature. |
| 106 | */ |
| 107 | printf( "\n . Verifying the RSA/SHA-1 signature" ); |
| 108 | fflush( stdout ); |
| 109 | |
| 110 | if( ( ret = sha1_file( argv[1], hash ) ) != 0 ) |
| 111 | { |
| 112 | printf( " failed\n ! Could not open or read %s\n\n", argv[1] ); |
| 113 | goto exit; |
| 114 | } |
| 115 | |
| 116 | if( ( ret = rsa_pkcs1_verify( &rsa, RSA_PUBLIC, RSA_SHA1, |
| 117 | 20, hash, buf ) ) != 0 ) |
| 118 | { |
| 119 | printf( " failed\n ! rsa_pkcs1_verify returned %d\n\n", ret ); |
| 120 | goto exit; |
| 121 | } |
| 122 | |
| 123 | printf( "\n . OK (the decrypted SHA-1 hash matches)\n\n" ); |
| 124 | |
| 125 | ret = 0; |
| 126 | |
| 127 | exit: |
| 128 | |
| 129 | #ifdef WIN32 |
| 130 | printf( " + Press Enter to exit this program.\n" ); |
| 131 | fflush( stdout ); getchar(); |
| 132 | #endif |
| 133 | |
| 134 | return( ret ); |
| 135 | } |