bignum: make mpi_init() and mpi_free() accept a single argument
[tropicssl.git] / programs / pkey / rsa_sign.c
blobb496f7d96b632eb0e04c6e814d28724b1f184445
1 /*
2 * RSA/SHA-1 signature creation program
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
6 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the names of PolarSSL or XySSL nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #ifndef _CRT_SECURE_NO_DEPRECATE
37 #define _CRT_SECURE_NO_DEPRECATE 1
38 #endif
40 #include <string.h>
41 #include <stdio.h>
43 #include "tropicssl/rsa.h"
44 #include "tropicssl/sha1.h"
46 int main(int argc, char *argv[])
48 FILE *f;
49 int ret, i;
50 rsa_context rsa;
51 unsigned char hash[20];
52 unsigned char buf[512];
54 ret = 1;
56 if (argc != 2) {
57 printf("usage: rsa_sign <filename>\n");
59 #ifdef WIN32
60 printf("\n");
61 #endif
63 goto exit;
66 printf("\n . Reading private key from rsa_priv.txt");
67 fflush(stdout);
69 if ((f = fopen("rsa_priv.txt", "rb")) == NULL) {
70 ret = 1;
71 printf(" failed\n ! Could not open rsa_priv.txt\n"
72 " ! Please run rsa_genkey first\n\n");
73 goto exit;
76 rsa_init(&rsa, RSA_PKCS_V15, 0, NULL, NULL);
78 if ((ret = mpi_read_file(&rsa.N, 16, f)) != 0 ||
79 (ret = mpi_read_file(&rsa.E, 16, f)) != 0 ||
80 (ret = mpi_read_file(&rsa.D, 16, f)) != 0 ||
81 (ret = mpi_read_file(&rsa.P, 16, f)) != 0 ||
82 (ret = mpi_read_file(&rsa.Q, 16, f)) != 0 ||
83 (ret = mpi_read_file(&rsa.DP, 16, f)) != 0 ||
84 (ret = mpi_read_file(&rsa.DQ, 16, f)) != 0 ||
85 (ret = mpi_read_file(&rsa.QP, 16, f)) != 0) {
86 printf(" failed\n ! mpi_read_file returned %d\n\n", ret);
87 goto exit;
90 rsa.len = (mpi_msb(&rsa.N) + 7) >> 3;
92 fclose(f);
95 * Compute the SHA-1 hash of the input file,
96 * then calculate the RSA signature of the hash.
98 printf("\n . Generating the RSA/SHA-1 signature");
99 fflush(stdout);
101 if ((ret = sha1_file(argv[1], hash)) != 0) {
102 printf(" failed\n ! Could not open or read %s\n\n", argv[1]);
103 goto exit;
106 if ((ret = rsa_pkcs1_sign(&rsa, RSA_PRIVATE, RSA_SHA1,
107 20, hash, buf)) != 0) {
108 printf(" failed\n ! rsa_pkcs1_sign returned %d\n\n", ret);
109 goto exit;
113 * Write the signature into <filename>-sig.txt
115 memcpy(argv[1] + strlen(argv[1]), ".sig", 5);
117 if ((f = fopen(argv[1], "wb+")) == NULL) {
118 ret = 1;
119 printf(" failed\n ! Could not create %s\n\n", argv[1]);
120 goto exit;
123 for (i = 0; i < rsa.len; i++)
124 fprintf(f, "%02X%s", buf[i], (i + 1) % 16 == 0 ? "\r\n" : " ");
126 fclose(f);
128 printf("\n . Done (created \"%s\")\n\n", argv[1]);
130 exit:
132 #ifdef WIN32
133 printf(" + Press Enter to exit this program.\n");
134 fflush(stdout);
135 getchar();
136 #endif
138 return (ret);