Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Public key-based signature creation program |
| 3 | * |
| 4 | * Copyright (C) 2006-2013, 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 | |
Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 26 | #include "polarssl/config.h" |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 27 | |
| 28 | #include <string.h> |
| 29 | #include <stdio.h> |
| 30 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 31 | #include "polarssl/error.h" |
| 32 | #include "polarssl/entropy.h" |
| 33 | #include "polarssl/ctr_drbg.h" |
| 34 | #include "polarssl/md.h" |
| 35 | #include "polarssl/pk.h" |
| 36 | #include "polarssl/sha1.h" |
| 37 | |
| 38 | #if defined _MSC_VER && !defined snprintf |
| 39 | #define snprintf _snprintf |
| 40 | #endif |
| 41 | |
| 42 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \ |
| 43 | !defined(POLARSSL_SHA1_C) || \ |
| 44 | !defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO) || \ |
| 45 | !defined(POLARSSL_CTR_DRBG_C) |
| 46 | int main( int argc, char *argv[] ) |
| 47 | { |
| 48 | ((void) argc); |
| 49 | ((void) argv); |
| 50 | |
| 51 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or " |
| 52 | "POLARSSL_SHA1_C and/or " |
| 53 | "POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO and/or " |
| 54 | "POLARSSL_CTR_DRBG_C not defined.\n"); |
| 55 | return( 0 ); |
| 56 | } |
| 57 | #else |
| 58 | int main( int argc, char *argv[] ) |
| 59 | { |
| 60 | FILE *f; |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame^] | 61 | int ret = 1; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 62 | pk_context pk; |
| 63 | entropy_context entropy; |
| 64 | ctr_drbg_context ctr_drbg; |
| 65 | unsigned char hash[20]; |
| 66 | unsigned char buf[POLARSSL_MPI_MAX_SIZE]; |
| 67 | char filename[512]; |
| 68 | const char *pers = "pk_sign"; |
| 69 | size_t olen = 0; |
| 70 | |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame^] | 71 | entropy_init( &entropy ); |
| 72 | pk_init( &pk ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 73 | |
| 74 | if( argc != 3 ) |
| 75 | { |
| 76 | printf( "usage: pk_sign <key_file> <filename>\n" ); |
| 77 | |
| 78 | #if defined(_WIN32) |
| 79 | printf( "\n" ); |
| 80 | #endif |
| 81 | |
| 82 | goto exit; |
| 83 | } |
| 84 | |
| 85 | printf( "\n . Seeding the random number generator..." ); |
| 86 | fflush( stdout ); |
| 87 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 88 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
| 89 | (const unsigned char *) pers, |
| 90 | strlen( pers ) ) ) != 0 ) |
| 91 | { |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 92 | printf( " failed\n ! ctr_drbg_init returned -0x%04x\n", -ret ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 93 | goto exit; |
| 94 | } |
| 95 | |
| 96 | printf( "\n . Reading private key from '%s'", argv[1] ); |
| 97 | fflush( stdout ); |
| 98 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 99 | if( ( ret = pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 ) |
| 100 | { |
| 101 | ret = 1; |
| 102 | printf( " failed\n ! Could not open '%s'\n", argv[1] ); |
| 103 | goto exit; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Compute the SHA-1 hash of the input file, |
| 108 | * then calculate the signature of the hash. |
| 109 | */ |
| 110 | printf( "\n . Generating the SHA-1 signature" ); |
| 111 | fflush( stdout ); |
| 112 | |
| 113 | if( ( ret = sha1_file( argv[2], hash ) ) != 0 ) |
| 114 | { |
| 115 | printf( " failed\n ! Could not open or read %s\n\n", argv[2] ); |
| 116 | goto exit; |
| 117 | } |
| 118 | |
| 119 | if( ( ret = pk_sign( &pk, POLARSSL_MD_SHA1, hash, 0, buf, &olen, |
| 120 | ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
| 121 | { |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 122 | printf( " failed\n ! pk_sign returned -0x%04x\n", -ret ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 123 | goto exit; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Write the signature into <filename>-sig.txt |
| 128 | */ |
| 129 | snprintf( filename, sizeof(filename), "%s.sig", argv[2] ); |
| 130 | |
| 131 | if( ( f = fopen( filename, "wb+" ) ) == NULL ) |
| 132 | { |
| 133 | ret = 1; |
| 134 | printf( " failed\n ! Could not create %s\n\n", filename ); |
| 135 | goto exit; |
| 136 | } |
| 137 | |
| 138 | if( fwrite( buf, 1, olen, f ) != olen ) |
| 139 | { |
| 140 | printf( "failed\n ! fwrite failed\n\n" ); |
| 141 | goto exit; |
| 142 | } |
| 143 | |
| 144 | fclose( f ); |
| 145 | |
| 146 | printf( "\n . Done (created \"%s\")\n\n", filename ); |
| 147 | |
| 148 | exit: |
| 149 | pk_free( &pk ); |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 150 | entropy_free( &entropy ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 151 | |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 152 | #if defined(POLARSSL_ERROR_C) |
| 153 | polarssl_strerror( ret, (char *) buf, sizeof(buf) ); |
| 154 | printf( " ! Last error was: %s\n", buf ); |
| 155 | #endif |
| 156 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 157 | #if defined(_WIN32) |
| 158 | printf( " + Press Enter to exit this program.\n" ); |
| 159 | fflush( stdout ); getchar(); |
| 160 | #endif |
| 161 | |
| 162 | return( ret ); |
| 163 | } |
| 164 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && |
| 165 | POLARSSL_SHA1_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO && |
| 166 | POLARSSL_CTR_DRBG_C */ |