Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * RSA/SHA-1 signature verification program |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | 085ab04 | 2015-01-23 11:06:27 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://www.polarssl.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 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 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 24 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 29 | #if defined(POLARSSL_PLATFORM_C) |
| 30 | #include "polarssl/platform.h" |
| 31 | #else |
| 32 | #define polarssl_printf printf |
| 33 | #define polarssl_fprintf fprintf |
| 34 | #define polarssl_malloc malloc |
| 35 | #define polarssl_free free |
| 36 | #endif |
| 37 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 38 | #include <string.h> |
| 39 | #include <stdio.h> |
| 40 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 41 | #include "polarssl/rsa.h" |
| 42 | #include "polarssl/sha1.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 43 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 44 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \ |
| 45 | !defined(POLARSSL_SHA1_C) || !defined(POLARSSL_FS_IO) |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 46 | int main( int argc, char *argv[] ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 47 | { |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 48 | ((void) argc); |
| 49 | ((void) argv); |
| 50 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 51 | polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or " |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 52 | "POLARSSL_SHA1_C and/or POLARSSL_FS_IO not defined.\n"); |
| 53 | return( 0 ); |
| 54 | } |
| 55 | #else |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 56 | int main( int argc, char *argv[] ) |
| 57 | { |
| 58 | FILE *f; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 59 | int ret, c; |
| 60 | size_t i; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 61 | rsa_context rsa; |
| 62 | unsigned char hash[20]; |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 63 | unsigned char buf[POLARSSL_MPI_MAX_SIZE]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 64 | |
| 65 | ret = 1; |
| 66 | if( argc != 2 ) |
| 67 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 68 | polarssl_printf( "usage: rsa_verify <filename>\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 69 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 70 | #if defined(_WIN32) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 71 | polarssl_printf( "\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 72 | #endif |
| 73 | |
| 74 | goto exit; |
| 75 | } |
| 76 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 77 | polarssl_printf( "\n . Reading public key from rsa_pub.txt" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 78 | fflush( stdout ); |
| 79 | |
| 80 | if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL ) |
| 81 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 82 | polarssl_printf( " failed\n ! Could not open rsa_pub.txt\n" \ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 83 | " ! Please run rsa_genkey first\n\n" ); |
| 84 | goto exit; |
| 85 | } |
| 86 | |
Paul Bakker | a802e1a | 2010-08-16 11:56:45 +0000 | [diff] [blame] | 87 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 88 | |
| 89 | if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 || |
| 90 | ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 ) |
| 91 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 92 | polarssl_printf( " failed\n ! mpi_read_file returned %d\n\n", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 93 | goto exit; |
| 94 | } |
| 95 | |
| 96 | rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3; |
| 97 | |
| 98 | fclose( f ); |
| 99 | |
| 100 | /* |
| 101 | * Extract the RSA signature from the text file |
| 102 | */ |
| 103 | ret = 1; |
| 104 | i = strlen( argv[1] ); |
| 105 | memcpy( argv[1] + i, ".sig", 5 ); |
| 106 | |
| 107 | if( ( f = fopen( argv[1], "rb" ) ) == NULL ) |
| 108 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 109 | polarssl_printf( "\n ! Could not open %s\n\n", argv[1] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 110 | goto exit; |
| 111 | } |
| 112 | |
| 113 | argv[1][i] = '\0', i = 0; |
| 114 | |
| 115 | while( fscanf( f, "%02X", &c ) > 0 && |
| 116 | i < (int) sizeof( buf ) ) |
| 117 | buf[i++] = (unsigned char) c; |
| 118 | |
| 119 | fclose( f ); |
| 120 | |
| 121 | if( i != rsa.len ) |
| 122 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 123 | polarssl_printf( "\n ! Invalid RSA signature format\n\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 124 | goto exit; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Compute the SHA-1 hash of the input file and compare |
| 129 | * it with the hash decrypted from the RSA signature. |
| 130 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 131 | polarssl_printf( "\n . Verifying the RSA/SHA-1 signature" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 132 | fflush( stdout ); |
| 133 | |
| 134 | if( ( ret = sha1_file( argv[1], hash ) ) != 0 ) |
| 135 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 136 | polarssl_printf( " failed\n ! Could not open or read %s\n\n", argv[1] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 137 | goto exit; |
| 138 | } |
| 139 | |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 140 | if( ( ret = rsa_pkcs1_verify( &rsa, NULL, NULL, RSA_PUBLIC, |
| 141 | POLARSSL_MD_SHA1, 20, hash, buf ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 142 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 143 | polarssl_printf( " failed\n ! rsa_pkcs1_verify returned -0x%0x\n\n", -ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 144 | goto exit; |
| 145 | } |
| 146 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 147 | polarssl_printf( "\n . OK (the decrypted SHA-1 hash matches)\n\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 148 | |
| 149 | ret = 0; |
| 150 | |
| 151 | exit: |
| 152 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 153 | #if defined(_WIN32) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 154 | polarssl_printf( " + Press Enter to exit this program.\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 155 | fflush( stdout ); getchar(); |
| 156 | #endif |
| 157 | |
| 158 | return( ret ); |
| 159 | } |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 160 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA1_C && |
| 161 | POLARSSL_FS_IO */ |