blob: 0fb733b502b43912bd98125656130721e2f19aea [file] [log] [blame]
Paul Bakker2291f6c2011-03-25 14:07:53 +00001/*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +00002 * RSASSA-PSS/SHA-256 signature verification program
Paul Bakker2291f6c2011-03-25 14:07:53 +00003 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakker2291f6c2011-03-25 14:07:53 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker2291f6c2011-03-25 14:07:53 +00007 *
Paul Bakker2291f6c2011-03-25 14:07:53 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker2291f6c2011-03-25 14:07:53 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000031#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_snprintf snprintf
34#define mbedtls_printf printf
35#define mbedtls_snprintf snprintf
Rich Evansf90016a2015-01-19 14:26:37 +000036#endif
37
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
39 defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_PK_PARSE_C) && \
40 defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/md.h"
42#include "mbedtls/pem.h"
43#include "mbedtls/pk.h"
44#include "mbedtls/sha1.h"
45#include "mbedtls/x509.h"
Paul Bakker2291f6c2011-03-25 14:07:53 +000046
Rich Evans18b78c72015-02-11 14:06:19 +000047#include <stdio.h>
48#include <string.h>
49#endif
50
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000051#if defined _MSC_VER && !defined snprintf
52#define snprintf _snprintf
53#endif
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
56 !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_PK_PARSE_C) || \
57 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000058int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000059{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
61 "MBEDTLS_SHA256_C and/or MBEDTLS_PK_PARSE_C and/or "
62 "MBEDTLS_FS_IO not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000063 return( 0 );
64}
65#else
Paul Bakker2291f6c2011-03-25 14:07:53 +000066int main( int argc, char *argv[] )
67{
68 FILE *f;
Paul Bakker0c226102014-04-17 16:02:36 +020069 int ret = 1;
Paul Bakker23986e52011-04-24 08:57:21 +000070 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 mbedtls_pk_context pk;
Paul Bakker2291f6c2011-03-25 14:07:53 +000072 unsigned char hash[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakker2291f6c2011-03-25 14:07:53 +000074 char filename[512];
75
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 mbedtls_pk_init( &pk );
Paul Bakker0c226102014-04-17 16:02:36 +020077
Paul Bakker2291f6c2011-03-25 14:07:53 +000078 if( argc != 3 )
79 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 mbedtls_printf( "usage: rsa_verify_pss <key_file> <filename>\n" );
Paul Bakker2291f6c2011-03-25 14:07:53 +000081
Paul Bakkercce9d772011-11-18 14:26:47 +000082#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_printf( "\n" );
Paul Bakker2291f6c2011-03-25 14:07:53 +000084#endif
85
86 goto exit;
87 }
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_printf( "\n . Reading public key from '%s'", argv[1] );
Paul Bakker2291f6c2011-03-25 14:07:53 +000090 fflush( stdout );
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
Paul Bakker2291f6c2011-03-25 14:07:53 +000093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 mbedtls_printf( " failed\n ! Could not read key from '%s'\n", argv[1] );
95 mbedtls_printf( " ! mbedtls_pk_parse_public_keyfile returned %d\n\n", ret );
Paul Bakker30520d12013-09-17 11:39:31 +020096 goto exit;
97 }
98
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 if( !mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) )
Paul Bakker30520d12013-09-17 11:39:31 +0200100 {
101 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_printf( " failed\n ! Key is not an RSA key\n" );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000103 goto exit;
104 }
105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 mbedtls_rsa_set_padding( mbedtls_pk_rsa( pk ), MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100107
Paul Bakker2291f6c2011-03-25 14:07:53 +0000108 /*
109 * Extract the RSA signature from the text file
110 */
111 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_snprintf( filename, 512, "%s.sig", argv[2] );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000113
114 if( ( f = fopen( filename, "rb" ) ) == NULL )
115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_printf( "\n ! Could not open %s\n\n", filename );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000117 goto exit;
118 }
119
Paul Bakker30520d12013-09-17 11:39:31 +0200120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 i = fread( buf, 1, MBEDTLS_MPI_MAX_SIZE, f );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000122
123 fclose( f );
124
Paul Bakker2291f6c2011-03-25 14:07:53 +0000125 /*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000126 * Compute the SHA-256 hash of the input file and compare
Paul Bakker2291f6c2011-03-25 14:07:53 +0000127 * it with the hash decrypted from the RSA signature.
128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_printf( "\n . Verifying the RSA/SHA-256 signature" );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000130 fflush( stdout );
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 if( ( ret = mbedtls_sha1_file( argv[2], hash ) ) != 0 )
Paul Bakker2291f6c2011-03-25 14:07:53 +0000133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000135 goto exit;
136 }
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 if( ( ret = mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, hash, 0,
Paul Bakker30520d12013-09-17 11:39:31 +0200139 buf, i ) ) != 0 )
Paul Bakker2291f6c2011-03-25 14:07:53 +0000140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_printf( " failed\n ! mbedtls_pk_verify returned %d\n\n", ret );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000142 goto exit;
143 }
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_printf( "\n . OK (the decrypted SHA-256 hash matches)\n\n" );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000146
147 ret = 0;
148
149exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_pk_free( &pk );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000151
Paul Bakkercce9d772011-11-18 14:26:47 +0000152#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000154 fflush( stdout ); getchar();
155#endif
156
157 return( ret );
158}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
160 MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */