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 | |
| 33 | #include "polarssl/havege.h" |
| 34 | #include "polarssl/md.h" |
| 35 | #include "polarssl/rsa.h" |
| 36 | #include "polarssl/sha1.h" |
| 37 | #include "polarssl/x509.h" |
| 38 | |
| 39 | int main( int argc, char *argv[] ) |
| 40 | { |
| 41 | FILE *f; |
| 42 | int ret; |
| 43 | rsa_context rsa; |
| 44 | havege_state hs; |
| 45 | unsigned char hash[20]; |
| 46 | unsigned char buf[512]; |
| 47 | char filename[512]; |
| 48 | |
| 49 | ret = 1; |
| 50 | |
| 51 | if( argc != 3 ) |
| 52 | { |
| 53 | printf( "usage: rsa_sign_pss <key_file> <filename>\n" ); |
| 54 | |
| 55 | #ifdef WIN32 |
| 56 | printf( "\n" ); |
| 57 | #endif |
| 58 | |
| 59 | goto exit; |
| 60 | } |
| 61 | |
| 62 | printf( "\n . Reading private key from '%s'", argv[1] ); |
| 63 | fflush( stdout ); |
| 64 | |
| 65 | havege_init( &hs ); |
| 66 | rsa_init( &rsa, RSA_PKCS_V21, POLARSSL_MD_SHA1 ); |
| 67 | |
| 68 | if( ( ret = x509parse_keyfile( &rsa, argv[1], "" ) ) != 0 ) |
| 69 | { |
| 70 | ret = 1; |
| 71 | printf( " failed\n ! Could not open '%s'\n", argv[1] ); |
| 72 | goto exit; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * Compute the SHA-1 hash of the input file, |
| 77 | * then calculate the RSA signature of the hash. |
| 78 | */ |
| 79 | printf( "\n . Generating the RSA/SHA-1 signature" ); |
| 80 | fflush( stdout ); |
| 81 | |
| 82 | if( ( ret = sha1_file( argv[2], hash ) ) != 0 ) |
| 83 | { |
| 84 | printf( " failed\n ! Could not open or read %s\n\n", argv[2] ); |
| 85 | goto exit; |
| 86 | } |
| 87 | |
| 88 | if( ( ret = rsa_pkcs1_sign( &rsa, havege_rand, &hs, RSA_PRIVATE, SIG_RSA_SHA1, |
| 89 | 20, hash, buf ) ) != 0 ) |
| 90 | { |
| 91 | printf( " failed\n ! rsa_pkcs1_sign returned %d\n\n", ret ); |
| 92 | goto exit; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Write the signature into <filename>-sig.txt |
| 97 | */ |
| 98 | snprintf( filename, 512, "%s.sig", argv[2] ); |
| 99 | |
| 100 | if( ( f = fopen( filename, "wb+" ) ) == NULL ) |
| 101 | { |
| 102 | ret = 1; |
| 103 | printf( " failed\n ! Could not create %s\n\n", filename ); |
| 104 | goto exit; |
| 105 | } |
| 106 | |
| 107 | if( fwrite( buf, 1, rsa.len, f ) != (size_t) rsa.len ) |
| 108 | { |
| 109 | printf( "failed\n ! fwrite failed\n\n" ); |
| 110 | goto exit; |
| 111 | } |
| 112 | |
| 113 | fclose( f ); |
| 114 | |
| 115 | printf( "\n . Done (created \"%s\")\n\n", filename ); |
| 116 | |
| 117 | exit: |
| 118 | |
| 119 | #ifdef WIN32 |
| 120 | printf( " + Press Enter to exit this program.\n" ); |
| 121 | fflush( stdout ); getchar(); |
| 122 | #endif |
| 123 | |
| 124 | return( ret ); |
| 125 | } |