Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * RSA/SHA-1 signature creation program |
| 3 | * |
| 4 | * Copyright (C) 2006-2007 Christophe Devine |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | #ifndef _CRT_SECURE_NO_DEPRECATE |
| 22 | #define _CRT_SECURE_NO_DEPRECATE 1 |
| 23 | #endif |
| 24 | |
| 25 | #include <string.h> |
| 26 | #include <stdio.h> |
| 27 | |
| 28 | #include "xyssl/rsa.h" |
| 29 | #include "xyssl/sha1.h" |
| 30 | |
| 31 | int main( int argc, char *argv[] ) |
| 32 | { |
| 33 | FILE *f; |
| 34 | int ret, i; |
| 35 | rsa_context rsa; |
| 36 | unsigned char hash[20]; |
| 37 | unsigned char buf[512]; |
| 38 | |
| 39 | ret = 1; |
| 40 | |
| 41 | if( argc != 2 ) |
| 42 | { |
| 43 | printf( "usage: rsa_sign <filename>\n" ); |
| 44 | |
| 45 | #ifdef WIN32 |
| 46 | printf( "\n" ); |
| 47 | #endif |
| 48 | |
| 49 | goto exit; |
| 50 | } |
| 51 | |
| 52 | printf( "\n . Reading private key from rsa_priv.txt" ); |
| 53 | fflush( stdout ); |
| 54 | |
| 55 | if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL ) |
| 56 | { |
| 57 | ret = 1; |
| 58 | printf( " failed\n ! Could not open rsa_priv.txt\n" \ |
| 59 | " ! Please run rsa_genkey first\n\n" ); |
| 60 | goto exit; |
| 61 | } |
| 62 | |
| 63 | rsa_init( &rsa, RSA_PKCS_V15, 0, NULL, NULL ); |
| 64 | |
| 65 | if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 || |
| 66 | ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 || |
| 67 | ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 || |
| 68 | ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 || |
| 69 | ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 || |
| 70 | ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 || |
| 71 | ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 || |
| 72 | ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 ) |
| 73 | { |
| 74 | printf( " failed\n ! mpi_read_file returned %d\n\n", ret ); |
| 75 | goto exit; |
| 76 | } |
| 77 | |
| 78 | rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3; |
| 79 | |
| 80 | fclose( f ); |
| 81 | |
| 82 | /* |
| 83 | * Compute the SHA-1 hash of the input file, |
| 84 | * then calculate the RSA signature of the hash. |
| 85 | */ |
| 86 | printf( "\n . Generating the RSA/SHA-1 signature" ); |
| 87 | fflush( stdout ); |
| 88 | |
| 89 | if( ( ret = sha1_file( argv[1], hash ) ) != 0 ) |
| 90 | { |
| 91 | printf( " failed\n ! Could not open or read %s\n\n", argv[1] ); |
| 92 | goto exit; |
| 93 | } |
| 94 | |
| 95 | if( ( ret = rsa_pkcs1_sign( &rsa, RSA_PRIVATE, RSA_SHA1, |
| 96 | 20, hash, buf ) ) != 0 ) |
| 97 | { |
| 98 | printf( " failed\n ! rsa_pkcs1_sign returned %d\n\n", ret ); |
| 99 | goto exit; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Write the signature into <filename>-sig.txt |
| 104 | */ |
| 105 | memcpy( argv[1] + strlen( argv[1] ), ".sig", 5 ); |
| 106 | |
| 107 | if( ( f = fopen( argv[1], "wb+" ) ) == NULL ) |
| 108 | { |
| 109 | ret = 1; |
| 110 | printf( " failed\n ! Could not create %s\n\n", argv[1] ); |
| 111 | goto exit; |
| 112 | } |
| 113 | |
| 114 | for( i = 0; i < rsa.len; i++ ) |
| 115 | fprintf( f, "%02X%s", buf[i], |
| 116 | ( i + 1 ) % 16 == 0 ? "\r\n" : " " ); |
| 117 | |
| 118 | fclose( f ); |
| 119 | |
| 120 | printf( "\n . Done (created \"%s\")\n\n", argv[1] ); |
| 121 | |
| 122 | exit: |
| 123 | |
| 124 | #ifdef WIN32 |
| 125 | printf( " + Press Enter to exit this program.\n" ); |
| 126 | fflush( stdout ); getchar(); |
| 127 | #endif |
| 128 | |
| 129 | return( ret ); |
| 130 | } |