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