2 * RSA/SHA-1 signature verification program
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
6 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
43 #include "tropicssl/rsa.h"
44 #include "tropicssl/sha1.h"
46 int main(int argc
, char *argv
[])
51 unsigned char hash
[20];
52 unsigned char buf
[512];
56 printf("usage: rsa_verify <filename>\n");
65 printf("\n . Reading public key from rsa_pub.txt");
68 if ((f
= fopen("rsa_pub.txt", "rb")) == NULL
) {
69 printf(" failed\n ! Could not open rsa_pub.txt\n"
70 " ! Please run rsa_genkey first\n\n");
74 rsa_init(&rsa
, RSA_PKCS_V15
, 0, NULL
, NULL
);
76 if ((ret
= mpi_read_file(&rsa
.N
, 16, f
)) != 0 ||
77 (ret
= mpi_read_file(&rsa
.E
, 16, f
)) != 0) {
78 printf(" failed\n ! mpi_read_file returned %d\n\n", ret
);
82 rsa
.len
= (mpi_msb(&rsa
.N
) + 7) >> 3;
87 * Extract the RSA signature from the text file
91 memcpy(argv
[1] + i
, ".sig", 5);
93 if ((f
= fopen(argv
[1], "rb")) == NULL
) {
94 printf("\n ! Could not open %s\n\n", argv
[1]);
98 argv
[1][i
] = '\0', i
= 0;
100 while (fscanf(f
, "%02X", &c
) > 0 && i
< (int)sizeof(buf
))
101 buf
[i
++] = (unsigned char)c
;
106 printf("\n ! Invalid RSA signature format\n\n");
111 * Compute the SHA-1 hash of the input file and compare
112 * it with the hash decrypted from the RSA signature.
114 printf("\n . Verifying the RSA/SHA-1 signature");
117 if ((ret
= sha1_file(argv
[1], hash
)) != 0) {
118 printf(" failed\n ! Could not open or read %s\n\n", argv
[1]);
122 if ((ret
= rsa_pkcs1_verify(&rsa
, RSA_PUBLIC
, RSA_SHA1
,
123 20, hash
, buf
)) != 0) {
124 printf(" failed\n ! rsa_pkcs1_verify returned %d\n\n", ret
);
128 printf("\n . OK (the decrypted SHA-1 hash matches)\n\n");
135 printf(" + Press Enter to exit this program.\n");