blob: f7b0833ebd9a1bb617b49bf0f4ff60a678b4c7b0 [file] [log] [blame]
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02001/*
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é-Gonnardf18c3e02013-08-12 18:41:18 +020043static int rsa_can_do( pk_type_t type )
44{
45 return( type == POLARSSL_PK_RSA );
46}
47
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020048static 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
58const pk_info_t rsa_info = {
59 POLARSSL_PK_RSA,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020060 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020061 rsa_verify_wrap,
62};
63#endif /* POLARSSL_RSA_C */
64
65#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020066int ecdsa_can_do( pk_type_t type )
67{
68 return( type == POLARSSL_PK_ECDSA );
69}
70
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020071int 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
79const pk_info_t ecdsa_info = {
80 POLARSSL_PK_ECDSA,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020081 ecdsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020082 ecdsa_verify_wrap,
83};
84#endif /* POLARSSL_ECDSA_C */
85
86#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020087static int eckey_can_do( pk_type_t type )
88{
89 return( type == POLARSSL_PK_ECKEY ||
90 type == POLARSSL_PK_ECKEY_DH ||
91 type == POLARSSL_PK_ECDSA );
92}
93
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020094static int eckey_verify_wrap( void *ctx,
95 const unsigned char *hash, const md_info_t *md_info,
96 const unsigned char *sig, size_t sig_len )
97{
98#if !defined(POLARSSL_ECDSA_C)
99 ((void) ctx);
100 ((void) hash);
101 ((void) md_info);
102 ((void) sig);
103 ((void) sig_len);
104
105 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
106#else
107 int ret;
108 ecdsa_context ecdsa;
109
110 ecdsa_init( &ecdsa );
111
112 ret = ecdsa_from_keypair( &ecdsa, ctx ) ||
113 ecdsa_verify_wrap( &ecdsa, hash, md_info, sig, sig_len );
114
115 ecdsa_free( &ecdsa );
116
117 return( ret );
118#endif /* POLARSSL_ECDSA_C */
119}
120
121const pk_info_t eckey_info = {
122 POLARSSL_PK_ECKEY,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200123 eckey_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200124 eckey_verify_wrap,
125};
126#endif /* POLARSSL_ECP_C */