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) |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 43 | static int rsa_can_do( pk_type_t type ) |
| 44 | { |
| 45 | return( type == POLARSSL_PK_RSA ); |
| 46 | } |
| 47 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 48 | static int rsa_verify_wrap( void *ctx, |
| 49 | const unsigned char *hash, const md_info_t *md_info, |
| 50 | const unsigned char *sig, size_t sig_len ) |
| 51 | { |
| 52 | ((void) sig_len); |
| 53 | |
| 54 | return( rsa_pkcs1_verify( (rsa_context *) ctx, |
| 55 | RSA_PUBLIC, md_info->type, 0, hash, sig ) ); |
| 56 | } |
| 57 | |
| 58 | const pk_info_t rsa_info = { |
| 59 | POLARSSL_PK_RSA, |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 60 | rsa_can_do, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 61 | rsa_verify_wrap, |
| 62 | }; |
| 63 | #endif /* POLARSSL_RSA_C */ |
| 64 | |
| 65 | #if defined(POLARSSL_ECDSA_C) |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 66 | int ecdsa_can_do( pk_type_t type ) |
| 67 | { |
| 68 | return( type == POLARSSL_PK_ECDSA ); |
| 69 | } |
| 70 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 71 | int ecdsa_verify_wrap( void *ctx, |
| 72 | const unsigned char *hash, const md_info_t *md_info, |
| 73 | const unsigned char *sig, size_t sig_len ) |
| 74 | { |
| 75 | return( ecdsa_read_signature( (ecdsa_context *) ctx, |
| 76 | hash, md_info->size, sig, sig_len ) ); |
| 77 | } |
| 78 | |
| 79 | const pk_info_t ecdsa_info = { |
| 80 | POLARSSL_PK_ECDSA, |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 81 | ecdsa_can_do, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 82 | ecdsa_verify_wrap, |
| 83 | }; |
| 84 | #endif /* POLARSSL_ECDSA_C */ |
| 85 | |
| 86 | #if defined(POLARSSL_ECP_C) |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame^] | 87 | /* |
| 88 | * Generic EC key |
| 89 | */ |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 90 | static int eckey_can_do( pk_type_t type ) |
| 91 | { |
| 92 | return( type == POLARSSL_PK_ECKEY || |
| 93 | type == POLARSSL_PK_ECKEY_DH || |
| 94 | type == POLARSSL_PK_ECDSA ); |
| 95 | } |
| 96 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 97 | static int eckey_verify_wrap( void *ctx, |
| 98 | const unsigned char *hash, const md_info_t *md_info, |
| 99 | const unsigned char *sig, size_t sig_len ) |
| 100 | { |
| 101 | #if !defined(POLARSSL_ECDSA_C) |
| 102 | ((void) ctx); |
| 103 | ((void) hash); |
| 104 | ((void) md_info); |
| 105 | ((void) sig); |
| 106 | ((void) sig_len); |
| 107 | |
| 108 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
| 109 | #else |
| 110 | int ret; |
| 111 | ecdsa_context ecdsa; |
| 112 | |
| 113 | ecdsa_init( &ecdsa ); |
| 114 | |
| 115 | ret = ecdsa_from_keypair( &ecdsa, ctx ) || |
| 116 | ecdsa_verify_wrap( &ecdsa, hash, md_info, sig, sig_len ); |
| 117 | |
| 118 | ecdsa_free( &ecdsa ); |
| 119 | |
| 120 | return( ret ); |
| 121 | #endif /* POLARSSL_ECDSA_C */ |
| 122 | } |
| 123 | |
| 124 | const pk_info_t eckey_info = { |
| 125 | POLARSSL_PK_ECKEY, |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 126 | eckey_can_do, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 127 | eckey_verify_wrap, |
| 128 | }; |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame^] | 129 | |
| 130 | /* |
| 131 | * EC key resticted to ECDH |
| 132 | */ |
| 133 | static int eckeydh_can_do( pk_type_t type ) |
| 134 | { |
| 135 | return( type == POLARSSL_PK_ECKEY || |
| 136 | type == POLARSSL_PK_ECKEY_DH ); |
| 137 | } |
| 138 | |
| 139 | static int eckeydh_verify_wrap( void *ctx, |
| 140 | const unsigned char *hash, const md_info_t *md_info, |
| 141 | const unsigned char *sig, size_t sig_len ) |
| 142 | { |
| 143 | ((void) ctx); |
| 144 | ((void) hash); |
| 145 | ((void) md_info); |
| 146 | ((void) sig); |
| 147 | ((void) sig_len); |
| 148 | |
| 149 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
| 150 | } |
| 151 | |
| 152 | const pk_info_t eckeydh_info = { |
| 153 | POLARSSL_PK_ECKEY_DH, |
| 154 | eckeydh_can_do, |
| 155 | eckeydh_verify_wrap, |
| 156 | }; |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 157 | #endif /* POLARSSL_ECP_C */ |