source: trunk/programs/pkey/rsa_verify_pss.c @ 1122

Revision 1122, 3.8 KB checked in by paul, 6 months ago (diff)
  • Lots of minimal changes to better support WINCE as a build target
Line 
1/*
2 *  RSASSA-PSS/SHA-1 signature verification 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/md.h"
36#include "polarssl/pem.h"
37#include "polarssl/rsa.h"
38#include "polarssl/sha1.h"
39#include "polarssl/x509.h"
40
41#if defined _MSC_VER && !defined snprintf
42#define snprintf _snprintf
43#endif
44
45#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) ||      \
46    !defined(POLARSSL_SHA1_C) || !defined(POLARSSL_X509_PARSE_C) || \
47    !defined(POLARSSL_FS_IO)
48int main( int argc, char *argv[] )
49{
50    ((void) argc);
51    ((void) argv);
52
53    printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
54           "POLARSSL_SHA1_C and/or POLARSSL_X509_PARSE_C and/or "
55           "POLARSSL_FS_IO not defined.\n");
56    return( 0 );
57}
58#else
59int main( int argc, char *argv[] )
60{
61    FILE *f;
62    int ret;
63    size_t i;
64    rsa_context rsa;
65    unsigned char hash[20];
66    unsigned char buf[512];
67    char filename[512];
68
69    ret = 1;
70    if( argc != 3 )
71    {
72        printf( "usage: rsa_verify_pss <key_file> <filename>\n" );
73
74#if defined(_WIN32)
75        printf( "\n" );
76#endif
77
78        goto exit;
79    }
80
81    printf( "\n  . Reading public key from '%s'", argv[1] );
82    fflush( stdout );
83
84    rsa_init( &rsa, RSA_PKCS_V21, POLARSSL_MD_SHA1 );
85
86    if( ( ret = x509parse_public_keyfile( &rsa, argv[1] ) ) != 0 )
87    {
88        printf( " failed\n  ! x509parse_public_key returned %d\n\n", ret );
89        goto exit;
90    }
91
92    /*
93     * Extract the RSA signature from the text file
94     */
95    ret = 1;
96    snprintf( filename, 512, "%s.sig", argv[2] );
97
98    if( ( f = fopen( filename, "rb" ) ) == NULL )
99    {
100        printf( "\n  ! Could not open %s\n\n", filename );
101        goto exit;
102    }
103
104    i = fread( buf, 1, rsa.len, f );
105
106    fclose( f );
107
108    if( i != rsa.len )
109    {
110        printf( "\n  ! Invalid RSA signature format\n\n" );
111        goto exit;
112    }
113
114    /*
115     * Compute the SHA-1 hash of the input file and compare
116     * it with the hash decrypted from the RSA signature.
117     */
118    printf( "\n  . Verifying the RSA/SHA-1 signature" );
119    fflush( stdout );
120
121    if( ( ret = sha1_file( argv[2], hash ) ) != 0 )
122    {
123        printf( " failed\n  ! Could not open or read %s\n\n", argv[2] );
124        goto exit;
125    }
126
127    if( ( ret = rsa_pkcs1_verify( &rsa, RSA_PUBLIC, SIG_RSA_SHA1,
128                                  20, hash, buf ) ) != 0 )
129    {
130        printf( " failed\n  ! rsa_pkcs1_verify returned %d\n\n", ret );
131        goto exit;
132    }
133
134    printf( "\n  . OK (the decrypted SHA-1 hash matches)\n\n" );
135
136    ret = 0;
137
138exit:
139
140#if defined(_WIN32)
141    printf( "  + Press Enter to exit this program.\n" );
142    fflush( stdout ); getchar();
143#endif
144
145    return( ret );
146}
147#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA1_C &&
148          POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */
Note: See TracBrowser for help on using the repository browser.

What are you looking for?