Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
Manuel Pégourié-Gonnard | 6f60cd8 | 2015-02-10 10:47:03 +0000 | [diff] [blame] | 2 | * RSA/SHA-256 signature creation program |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 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 | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://tls.mbed.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 |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 32 | #include <stdio.h> |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 33 | #define polarssl_fprintf fprintf |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 34 | #define polarssl_printf printf |
Manuel Pégourié-Gonnard | c728f94 | 2015-08-31 12:28:30 +0200 | [diff] [blame] | 35 | #define polarssl_snprintf snprintf |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 36 | #endif |
| 37 | |
Manuel Pégourié-Gonnard | 013bffe | 2015-02-13 14:09:44 +0000 | [diff] [blame] | 38 | #if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_RSA_C) && \ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 39 | defined(POLARSSL_SHA256_C) && defined(POLARSSL_FS_IO) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 40 | #include "polarssl/rsa.h" |
| 41 | #include "polarssl/sha1.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 42 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 43 | #include <stdio.h> |
| 44 | #include <string.h> |
| 45 | #endif |
| 46 | |
Manuel Pégourié-Gonnard | c728f94 | 2015-08-31 12:28:30 +0200 | [diff] [blame] | 47 | #if defined _MSC_VER && !defined snprintf |
| 48 | #define snprintf _snprintf |
| 49 | #endif |
| 50 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 51 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \ |
Manuel Pégourié-Gonnard | 6f60cd8 | 2015-02-10 10:47:03 +0000 | [diff] [blame] | 52 | !defined(POLARSSL_SHA256_C) || !defined(POLARSSL_FS_IO) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 53 | int main( void ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 54 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 55 | polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or " |
Manuel Pégourié-Gonnard | 6f60cd8 | 2015-02-10 10:47:03 +0000 | [diff] [blame] | 56 | "POLARSSL_SHA256_C and/or POLARSSL_FS_IO not defined.\n"); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 57 | return( 0 ); |
| 58 | } |
| 59 | #else |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 60 | int main( int argc, char *argv[] ) |
| 61 | { |
| 62 | FILE *f; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 63 | int ret; |
| 64 | size_t i; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 65 | rsa_context rsa; |
Manuel Pégourié-Gonnard | ee7db9c | 2015-08-31 11:32:03 +0200 | [diff] [blame] | 66 | unsigned char hash[32]; |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 67 | unsigned char buf[POLARSSL_MPI_MAX_SIZE]; |
Manuel Pégourié-Gonnard | 6432c7e | 2015-08-31 11:30:07 +0200 | [diff] [blame] | 68 | char filename[512]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 69 | |
Janos Follath | 347552d | 2016-03-17 15:21:39 +0000 | [diff] [blame] | 70 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 71 | ret = 1; |
| 72 | |
| 73 | if( argc != 2 ) |
| 74 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 75 | polarssl_printf( "usage: rsa_sign <filename>\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 76 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 77 | #if defined(_WIN32) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 78 | polarssl_printf( "\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 79 | #endif |
| 80 | |
| 81 | goto exit; |
| 82 | } |
| 83 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 84 | polarssl_printf( "\n . Reading private key from rsa_priv.txt" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 85 | fflush( stdout ); |
| 86 | |
| 87 | if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL ) |
| 88 | { |
| 89 | ret = 1; |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 90 | polarssl_printf( " failed\n ! Could not open rsa_priv.txt\n" \ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 91 | " ! Please run rsa_genkey first\n\n" ); |
| 92 | goto exit; |
| 93 | } |
| 94 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 95 | if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 || |
| 96 | ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 || |
| 97 | ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 || |
| 98 | ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 || |
| 99 | ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 || |
| 100 | ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 || |
| 101 | ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 || |
| 102 | ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 ) |
| 103 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 104 | polarssl_printf( " failed\n ! mpi_read_file returned %d\n\n", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 105 | goto exit; |
| 106 | } |
| 107 | |
| 108 | rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3; |
| 109 | |
| 110 | fclose( f ); |
| 111 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 112 | polarssl_printf( "\n . Checking the private key" ); |
Paul Bakker | 5ef9db2 | 2012-09-27 13:19:22 +0000 | [diff] [blame] | 113 | fflush( stdout ); |
| 114 | if( ( ret = rsa_check_privkey( &rsa ) ) != 0 ) |
| 115 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 116 | polarssl_printf( " failed\n ! rsa_check_privkey failed with -0x%0x\n", -ret ); |
Paul Bakker | 5ef9db2 | 2012-09-27 13:19:22 +0000 | [diff] [blame] | 117 | goto exit; |
| 118 | } |
| 119 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 120 | /* |
Manuel Pégourié-Gonnard | 6f60cd8 | 2015-02-10 10:47:03 +0000 | [diff] [blame] | 121 | * Compute the SHA-256 hash of the input file, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 122 | * then calculate the RSA signature of the hash. |
| 123 | */ |
Manuel Pégourié-Gonnard | 6f60cd8 | 2015-02-10 10:47:03 +0000 | [diff] [blame] | 124 | polarssl_printf( "\n . Generating the RSA/SHA-256 signature" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 125 | fflush( stdout ); |
| 126 | |
| 127 | if( ( ret = sha1_file( argv[1], hash ) ) != 0 ) |
| 128 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 129 | 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] | 130 | goto exit; |
| 131 | } |
| 132 | |
Manuel Pégourié-Gonnard | 6f60cd8 | 2015-02-10 10:47:03 +0000 | [diff] [blame] | 133 | if( ( ret = rsa_pkcs1_sign( &rsa, NULL, NULL, RSA_PRIVATE, POLARSSL_MD_SHA256, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 134 | 20, hash, buf ) ) != 0 ) |
| 135 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 136 | polarssl_printf( " failed\n ! rsa_pkcs1_sign returned -0x%0x\n\n", -ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 137 | goto exit; |
| 138 | } |
| 139 | |
| 140 | /* |
Manuel Pégourié-Gonnard | 6432c7e | 2015-08-31 11:30:07 +0200 | [diff] [blame] | 141 | * Write the signature into <filename>.sig |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 142 | */ |
Manuel Pégourié-Gonnard | 8fbb5a3 | 2015-08-31 12:38:12 +0200 | [diff] [blame] | 143 | polarssl_snprintf( filename, sizeof( filename ), "%s.sig", argv[1] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 144 | |
Manuel Pégourié-Gonnard | 6432c7e | 2015-08-31 11:30:07 +0200 | [diff] [blame] | 145 | if( ( f = fopen( filename, "wb+" ) ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 146 | { |
| 147 | ret = 1; |
Manuel Pégourié-Gonnard | 6432c7e | 2015-08-31 11:30:07 +0200 | [diff] [blame] | 148 | polarssl_printf( " failed\n ! Could not create %s\n\n", filename ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 149 | goto exit; |
| 150 | } |
| 151 | |
| 152 | for( i = 0; i < rsa.len; i++ ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 153 | polarssl_fprintf( f, "%02X%s", buf[i], |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 154 | ( i + 1 ) % 16 == 0 ? "\r\n" : " " ); |
| 155 | |
| 156 | fclose( f ); |
| 157 | |
Manuel Pégourié-Gonnard | 6432c7e | 2015-08-31 11:30:07 +0200 | [diff] [blame] | 158 | polarssl_printf( "\n . Done (created \"%s\")\n\n", filename ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 159 | |
| 160 | exit: |
| 161 | |
Janos Follath | 347552d | 2016-03-17 15:21:39 +0000 | [diff] [blame] | 162 | rsa_free( &rsa ); |
| 163 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 164 | #if defined(_WIN32) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 165 | polarssl_printf( " + Press Enter to exit this program.\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 166 | fflush( stdout ); getchar(); |
| 167 | #endif |
| 168 | |
| 169 | return( ret ); |
| 170 | } |
Manuel Pégourié-Gonnard | 6f60cd8 | 2015-02-10 10:47:03 +0000 | [diff] [blame] | 171 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA256_C && |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 172 | POLARSSL_FS_IO */ |