blob: ee92c8287e4957c3114d9ca8b0c6ea9f10fee728 [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é-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, 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
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020037#if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_ENTROPY_C) || \
38 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
39 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
40 !defined(MBEDTLS_CTR_DRBG_C)
41int main( void )
42{
43 mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_ENTROPY_C and/or "
44 "MBEDTLS_RSA_C and/or MBEDTLS_SHA256_C and/or "
45 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
46 "MBEDTLS_CTR_DRBG_C not defined.\n");
47 return( 0 );
48}
49#else
50
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/md.h"
52#include "mbedtls/pem.h"
53#include "mbedtls/pk.h"
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020054#include "mbedtls/md.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/x509.h"
Paul Bakker2291f6c2011-03-25 14:07:53 +000056
Rich Evans18b78c72015-02-11 14:06:19 +000057#include <stdio.h>
58#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000059
Paul Bakker2291f6c2011-03-25 14:07:53 +000060int main( int argc, char *argv[] )
61{
62 FILE *f;
Paul Bakker0c226102014-04-17 16:02:36 +020063 int ret = 1;
Paul Bakker23986e52011-04-24 08:57:21 +000064 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 mbedtls_pk_context pk;
Paul Bakker2291f6c2011-03-25 14:07:53 +000066 unsigned char hash[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakker2291f6c2011-03-25 14:07:53 +000068 char filename[512];
69
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 mbedtls_pk_init( &pk );
Paul Bakker0c226102014-04-17 16:02:36 +020071
Paul Bakker2291f6c2011-03-25 14:07:53 +000072 if( argc != 3 )
73 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 mbedtls_printf( "usage: rsa_verify_pss <key_file> <filename>\n" );
Paul Bakker2291f6c2011-03-25 14:07:53 +000075
Paul Bakkercce9d772011-11-18 14:26:47 +000076#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_printf( "\n" );
Paul Bakker2291f6c2011-03-25 14:07:53 +000078#endif
79
80 goto exit;
81 }
82
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_printf( "\n . Reading public key from '%s'", argv[1] );
Paul Bakker2291f6c2011-03-25 14:07:53 +000084 fflush( stdout );
85
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
Paul Bakker2291f6c2011-03-25 14:07:53 +000087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_printf( " failed\n ! Could not read key from '%s'\n", argv[1] );
89 mbedtls_printf( " ! mbedtls_pk_parse_public_keyfile returned %d\n\n", ret );
Paul Bakker30520d12013-09-17 11:39:31 +020090 goto exit;
91 }
92
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 if( !mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) )
Paul Bakker30520d12013-09-17 11:39:31 +020094 {
95 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_printf( " failed\n ! Key is not an RSA key\n" );
Paul Bakker2291f6c2011-03-25 14:07:53 +000097 goto exit;
98 }
99
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_rsa_set_padding( mbedtls_pk_rsa( pk ), MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100101
Paul Bakker2291f6c2011-03-25 14:07:53 +0000102 /*
Manuel Pégourié-Gonnard1d8f2da2015-08-27 21:42:27 +0200103 * Extract the RSA signature from the file
Paul Bakker2291f6c2011-03-25 14:07:53 +0000104 */
105 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 mbedtls_snprintf( filename, 512, "%s.sig", argv[2] );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000107
108 if( ( f = fopen( filename, "rb" ) ) == NULL )
109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_printf( "\n ! Could not open %s\n\n", filename );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000111 goto exit;
112 }
113
Paul Bakker30520d12013-09-17 11:39:31 +0200114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 i = fread( buf, 1, MBEDTLS_MPI_MAX_SIZE, f );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000116
117 fclose( f );
118
Paul Bakker2291f6c2011-03-25 14:07:53 +0000119 /*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000120 * Compute the SHA-256 hash of the input file and compare
Paul Bakker2291f6c2011-03-25 14:07:53 +0000121 * it with the hash decrypted from the RSA signature.
122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 mbedtls_printf( "\n . Verifying the RSA/SHA-256 signature" );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000124 fflush( stdout );
125
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +0200126 if( ( ret = mbedtls_md_file(
127 mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
128 argv[2], hash ) ) != 0 )
Paul Bakker2291f6c2011-03-25 14:07:53 +0000129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000131 goto exit;
132 }
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 if( ( ret = mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, hash, 0,
Paul Bakker30520d12013-09-17 11:39:31 +0200135 buf, i ) ) != 0 )
Paul Bakker2291f6c2011-03-25 14:07:53 +0000136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_printf( " failed\n ! mbedtls_pk_verify returned %d\n\n", ret );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000138 goto exit;
139 }
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_printf( "\n . OK (the decrypted SHA-256 hash matches)\n\n" );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000142
143 ret = 0;
144
145exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_pk_free( &pk );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000147
Paul Bakkercce9d772011-11-18 14:26:47 +0000148#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000150 fflush( stdout ); getchar();
151#endif
152
153 return( ret );
154}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
156 MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */