| Paul Bakker | 2291f6c | 2011-03-25 14:07:53 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  RSASSA-PSS/SHA-1 signature creation program | 
|  | 3 | * | 
|  | 4 | *  Copyright (C) 2006-2010, Brainspark B.V. | 
|  | 5 | * | 
|  | 6 | *  This file is part of PolarSSL (http://www.polarssl.org) | 
|  | 7 | *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> | 
|  | 8 | * | 
|  | 9 | *  All rights reserved. | 
|  | 10 | * | 
|  | 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 |  | 
|  | 26 | #ifndef _CRT_SECURE_NO_DEPRECATE | 
|  | 27 | #define _CRT_SECURE_NO_DEPRECATE 1 | 
|  | 28 | #endif | 
|  | 29 |  | 
|  | 30 | #include <string.h> | 
|  | 31 | #include <stdio.h> | 
|  | 32 |  | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 33 | #include "polarssl/config.h" | 
|  | 34 |  | 
| Paul Bakker | 2291f6c | 2011-03-25 14:07:53 +0000 | [diff] [blame] | 35 | #include "polarssl/havege.h" | 
|  | 36 | #include "polarssl/md.h" | 
|  | 37 | #include "polarssl/rsa.h" | 
|  | 38 | #include "polarssl/sha1.h" | 
|  | 39 | #include "polarssl/x509.h" | 
|  | 40 |  | 
| Paul Bakker | af5c85f | 2011-04-18 03:47:52 +0000 | [diff] [blame] | 41 | #if defined _MSC_VER && !defined snprintf | 
|  | 42 | #define snprintf _snprintf | 
|  | 43 | #endif | 
|  | 44 |  | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 45 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_HAVEGE_C) ||   \ | 
|  | 46 | !defined(POLARSSL_RSA_C) || !defined(POLARSSL_SHA1_C) ||        \ | 
|  | 47 | !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO) | 
|  | 48 | int main( void ) | 
|  | 49 | { | 
|  | 50 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_HAVEGE_C and/or " | 
|  | 51 | "POLARSSL_RSA_C and/or POLARSSL_SHA1_C and/or " | 
|  | 52 | "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n"); | 
|  | 53 | return( 0 ); | 
|  | 54 | } | 
|  | 55 | #else | 
| Paul Bakker | 2291f6c | 2011-03-25 14:07:53 +0000 | [diff] [blame] | 56 | int main( int argc, char *argv[] ) | 
|  | 57 | { | 
|  | 58 | FILE *f; | 
|  | 59 | int ret; | 
|  | 60 | rsa_context rsa; | 
|  | 61 | havege_state hs; | 
|  | 62 | unsigned char hash[20]; | 
|  | 63 | unsigned char buf[512]; | 
|  | 64 | char filename[512]; | 
|  | 65 |  | 
|  | 66 | ret = 1; | 
|  | 67 |  | 
|  | 68 | if( argc != 3 ) | 
|  | 69 | { | 
|  | 70 | printf( "usage: rsa_sign_pss <key_file> <filename>\n" ); | 
|  | 71 |  | 
|  | 72 | #ifdef WIN32 | 
|  | 73 | printf( "\n" ); | 
|  | 74 | #endif | 
|  | 75 |  | 
|  | 76 | goto exit; | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | printf( "\n  . Reading private key from '%s'", argv[1] ); | 
|  | 80 | fflush( stdout ); | 
|  | 81 |  | 
|  | 82 | havege_init( &hs ); | 
|  | 83 | rsa_init( &rsa, RSA_PKCS_V21, POLARSSL_MD_SHA1 ); | 
|  | 84 |  | 
|  | 85 | if( ( ret = x509parse_keyfile( &rsa, argv[1], "" ) ) != 0 ) | 
|  | 86 | { | 
|  | 87 | ret = 1; | 
|  | 88 | printf( " failed\n  ! Could not open '%s'\n", argv[1] ); | 
|  | 89 | goto exit; | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | /* | 
|  | 93 | * Compute the SHA-1 hash of the input file, | 
|  | 94 | * then calculate the RSA signature of the hash. | 
|  | 95 | */ | 
|  | 96 | printf( "\n  . Generating the RSA/SHA-1 signature" ); | 
|  | 97 | fflush( stdout ); | 
|  | 98 |  | 
|  | 99 | if( ( ret = sha1_file( argv[2], hash ) ) != 0 ) | 
|  | 100 | { | 
|  | 101 | printf( " failed\n  ! Could not open or read %s\n\n", argv[2] ); | 
|  | 102 | goto exit; | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | if( ( ret = rsa_pkcs1_sign( &rsa, havege_rand, &hs, RSA_PRIVATE, SIG_RSA_SHA1, | 
|  | 106 | 20, hash, buf ) ) != 0 ) | 
|  | 107 | { | 
|  | 108 | printf( " failed\n  ! rsa_pkcs1_sign returned %d\n\n", ret ); | 
|  | 109 | goto exit; | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | /* | 
|  | 113 | * Write the signature into <filename>-sig.txt | 
|  | 114 | */ | 
|  | 115 | snprintf( filename, 512, "%s.sig", argv[2] ); | 
|  | 116 |  | 
|  | 117 | if( ( f = fopen( filename, "wb+" ) ) == NULL ) | 
|  | 118 | { | 
|  | 119 | ret = 1; | 
|  | 120 | printf( " failed\n  ! Could not create %s\n\n", filename ); | 
|  | 121 | goto exit; | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | if( fwrite( buf, 1, rsa.len, f ) != (size_t) rsa.len ) | 
|  | 125 | { | 
|  | 126 | printf( "failed\n  ! fwrite failed\n\n" ); | 
|  | 127 | goto exit; | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | fclose( f ); | 
|  | 131 |  | 
|  | 132 | printf( "\n  . Done (created \"%s\")\n\n", filename ); | 
|  | 133 |  | 
|  | 134 | exit: | 
|  | 135 |  | 
|  | 136 | #ifdef WIN32 | 
|  | 137 | printf( "  + Press Enter to exit this program.\n" ); | 
|  | 138 | fflush( stdout ); getchar(); | 
|  | 139 | #endif | 
|  | 140 |  | 
|  | 141 | return( ret ); | 
|  | 142 | } | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 143 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_HAVEGE_C && POLARSSL_RSA_C && | 
|  | 144 | POLARSSL_SHA1_C && POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */ |