Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Public Key abstraction layer: wrapper functions |
| 3 | * |
| 4 | * Copyright (C) 2006-2013, 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 | #include "polarssl/config.h" |
| 27 | |
| 28 | #include "polarssl/pk_wrap.h" |
| 29 | |
| 30 | #if defined(POLARSSL_RSA_C) |
| 31 | #include "polarssl/rsa.h" |
| 32 | #endif |
| 33 | |
| 34 | #if defined(POLARSSL_ECP_C) |
| 35 | #include "polarssl/ecp.h" |
| 36 | #endif |
| 37 | |
| 38 | #if defined(POLARSSL_ECDSA_C) |
| 39 | #include "polarssl/ecdsa.h" |
| 40 | #endif |
| 41 | |
| 42 | #if defined(POLARSSL_RSA_C) |
| 43 | static int rsa_verify_wrap( void *ctx, |
| 44 | const unsigned char *hash, const md_info_t *md_info, |
| 45 | const unsigned char *sig, size_t sig_len ) |
| 46 | { |
| 47 | ((void) sig_len); |
| 48 | |
| 49 | return( rsa_pkcs1_verify( (rsa_context *) ctx, |
| 50 | RSA_PUBLIC, md_info->type, 0, hash, sig ) ); |
| 51 | } |
| 52 | |
| 53 | const pk_info_t rsa_info = { |
| 54 | POLARSSL_PK_RSA, |
| 55 | rsa_verify_wrap, |
| 56 | }; |
| 57 | #endif /* POLARSSL_RSA_C */ |
| 58 | |
| 59 | #if defined(POLARSSL_ECDSA_C) |
| 60 | int ecdsa_verify_wrap( void *ctx, |
| 61 | const unsigned char *hash, const md_info_t *md_info, |
| 62 | const unsigned char *sig, size_t sig_len ) |
| 63 | { |
| 64 | return( ecdsa_read_signature( (ecdsa_context *) ctx, |
| 65 | hash, md_info->size, sig, sig_len ) ); |
| 66 | } |
| 67 | |
| 68 | const pk_info_t ecdsa_info = { |
| 69 | POLARSSL_PK_ECDSA, |
| 70 | ecdsa_verify_wrap, |
| 71 | }; |
| 72 | #endif /* POLARSSL_ECDSA_C */ |
| 73 | |
| 74 | #if defined(POLARSSL_ECP_C) |
| 75 | static int eckey_verify_wrap( void *ctx, |
| 76 | const unsigned char *hash, const md_info_t *md_info, |
| 77 | const unsigned char *sig, size_t sig_len ) |
| 78 | { |
| 79 | #if !defined(POLARSSL_ECDSA_C) |
| 80 | ((void) ctx); |
| 81 | ((void) hash); |
| 82 | ((void) md_info); |
| 83 | ((void) sig); |
| 84 | ((void) sig_len); |
| 85 | |
| 86 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
| 87 | #else |
| 88 | int ret; |
| 89 | ecdsa_context ecdsa; |
| 90 | |
| 91 | ecdsa_init( &ecdsa ); |
| 92 | |
| 93 | ret = ecdsa_from_keypair( &ecdsa, ctx ) || |
| 94 | ecdsa_verify_wrap( &ecdsa, hash, md_info, sig, sig_len ); |
| 95 | |
| 96 | ecdsa_free( &ecdsa ); |
| 97 | |
| 98 | return( ret ); |
| 99 | #endif /* POLARSSL_ECDSA_C */ |
| 100 | } |
| 101 | |
| 102 | const pk_info_t eckey_info = { |
| 103 | POLARSSL_PK_ECKEY, |
| 104 | eckey_verify_wrap, |
| 105 | }; |
| 106 | #endif /* POLARSSL_ECP_C */ |