| 1 | /* |
|---|
| 2 | * RSA/SHA-1 signature creation program |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2006-2011, 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/config.h" |
|---|
| 34 | |
|---|
| 35 | #include "polarssl/rsa.h" |
|---|
| 36 | #include "polarssl/sha1.h" |
|---|
| 37 | |
|---|
| 38 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \ |
|---|
| 39 | !defined(POLARSSL_SHA1_C) || !defined(POLARSSL_FS_IO) |
|---|
| 40 | int main( int argc, char *argv[] ) |
|---|
| 41 | { |
|---|
| 42 | ((void) argc); |
|---|
| 43 | ((void) argv); |
|---|
| 44 | |
|---|
| 45 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or " |
|---|
| 46 | "POLARSSL_SHA1_C and/or POLARSSL_FS_IO not defined.\n"); |
|---|
| 47 | return( 0 ); |
|---|
| 48 | } |
|---|
| 49 | #else |
|---|
| 50 | int main( int argc, char *argv[] ) |
|---|
| 51 | { |
|---|
| 52 | FILE *f; |
|---|
| 53 | int ret; |
|---|
| 54 | size_t i; |
|---|
| 55 | rsa_context rsa; |
|---|
| 56 | unsigned char hash[20]; |
|---|
| 57 | unsigned char buf[512]; |
|---|
| 58 | |
|---|
| 59 | ret = 1; |
|---|
| 60 | |
|---|
| 61 | if( argc != 2 ) |
|---|
| 62 | { |
|---|
| 63 | printf( "usage: rsa_sign <filename>\n" ); |
|---|
| 64 | |
|---|
| 65 | #if defined(_WIN32) |
|---|
| 66 | printf( "\n" ); |
|---|
| 67 | #endif |
|---|
| 68 | |
|---|
| 69 | goto exit; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | printf( "\n . Reading private key from rsa_priv.txt" ); |
|---|
| 73 | fflush( stdout ); |
|---|
| 74 | |
|---|
| 75 | if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL ) |
|---|
| 76 | { |
|---|
| 77 | ret = 1; |
|---|
| 78 | printf( " failed\n ! Could not open rsa_priv.txt\n" \ |
|---|
| 79 | " ! Please run rsa_genkey first\n\n" ); |
|---|
| 80 | goto exit; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); |
|---|
| 84 | |
|---|
| 85 | if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 || |
|---|
| 86 | ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 || |
|---|
| 87 | ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 || |
|---|
| 88 | ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 || |
|---|
| 89 | ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 || |
|---|
| 90 | ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 || |
|---|
| 91 | ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 || |
|---|
| 92 | ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 ) |
|---|
| 93 | { |
|---|
| 94 | printf( " failed\n ! mpi_read_file returned %d\n\n", ret ); |
|---|
| 95 | goto exit; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3; |
|---|
| 99 | |
|---|
| 100 | fclose( f ); |
|---|
| 101 | |
|---|
| 102 | /* |
|---|
| 103 | * Compute the SHA-1 hash of the input file, |
|---|
| 104 | * then calculate the RSA signature of the hash. |
|---|
| 105 | */ |
|---|
| 106 | printf( "\n . Generating the RSA/SHA-1 signature" ); |
|---|
| 107 | fflush( stdout ); |
|---|
| 108 | |
|---|
| 109 | if( ( ret = sha1_file( argv[1], hash ) ) != 0 ) |
|---|
| 110 | { |
|---|
| 111 | printf( " failed\n ! Could not open or read %s\n\n", argv[1] ); |
|---|
| 112 | goto exit; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | if( ( ret = rsa_pkcs1_sign( &rsa, NULL, NULL, RSA_PRIVATE, SIG_RSA_SHA1, |
|---|
| 116 | 20, hash, buf ) ) != 0 ) |
|---|
| 117 | { |
|---|
| 118 | printf( " failed\n ! rsa_pkcs1_sign returned %d\n\n", ret ); |
|---|
| 119 | goto exit; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | /* |
|---|
| 123 | * Write the signature into <filename>-sig.txt |
|---|
| 124 | */ |
|---|
| 125 | memcpy( argv[1] + strlen( argv[1] ), ".sig", 5 ); |
|---|
| 126 | |
|---|
| 127 | if( ( f = fopen( argv[1], "wb+" ) ) == NULL ) |
|---|
| 128 | { |
|---|
| 129 | ret = 1; |
|---|
| 130 | printf( " failed\n ! Could not create %s\n\n", argv[1] ); |
|---|
| 131 | goto exit; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | for( i = 0; i < rsa.len; i++ ) |
|---|
| 135 | fprintf( f, "%02X%s", buf[i], |
|---|
| 136 | ( i + 1 ) % 16 == 0 ? "\r\n" : " " ); |
|---|
| 137 | |
|---|
| 138 | fclose( f ); |
|---|
| 139 | |
|---|
| 140 | printf( "\n . Done (created \"%s\")\n\n", argv[1] ); |
|---|
| 141 | |
|---|
| 142 | exit: |
|---|
| 143 | |
|---|
| 144 | #if defined(_WIN32) |
|---|
| 145 | printf( " + Press Enter to exit this program.\n" ); |
|---|
| 146 | fflush( stdout ); getchar(); |
|---|
| 147 | #endif |
|---|
| 148 | |
|---|
| 149 | return( ret ); |
|---|
| 150 | } |
|---|
| 151 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA1_C && |
|---|
| 152 | POLARSSL_FS_IO */ |
|---|