1 /*
2  *	pubkey.h
3  *	Release $Name: MATRIXSSL-3-3-0-OPEN $
4  */
5 /*
6  *	Copyright (c) AuthenTec, Inc. 2011-2012
7  *	Copyright (c) PeerSec Networks, 2002-2011
8  *	All Rights Reserved
9  *
10  *	The latest version of this code is available at http://www.matrixssl.org
11  *
12  *	This software is open source; you can redistribute it and/or modify
13  *	it under the terms of the GNU General Public License as published by
14  *	the Free Software Foundation; either version 2 of the License, or
15  *	(at your option) any later version.
16  *
17  *	This General Public License does NOT permit incorporating this software
18  *	into proprietary programs.  If you are unable to comply with the GPL, a
19  *	commercial license for this software may be purchased from AuthenTec at
20  *	http://www.authentec.com/Products/EmbeddedSecurity/SecurityToolkits.aspx
21  *
22  *	This program is distributed in WITHOUT ANY WARRANTY; without even the
23  *	implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24  *	See the GNU General Public License for more details.
25  *
26  *	You should have received a copy of the GNU General Public License
27  *	along with this program; if not, write to the Free Software
28  *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29  *	http://www.gnu.org/copyleft/gpl.html
30  */
31 /******************************************************************************/
32 
33 #ifndef _h_PS_PUBKEY
34 #define _h_PS_PUBKEY
35 
36 #define PUBKEY_TYPE		0x01
37 #define PRIVKEY_TYPE	0x02
38 
39 /* Public Key types for psPubKey_t */
40 #define PS_RSA	1
41 #define	PS_ECC	2
42 #define PS_DH	3
43 
44 /* Sig types */
45 #define	RSA_TYPE_SIG			5
46 #define	DSA_TYPE_SIG			6
47 
48 /*
49 	Pub key speed or size optimization handling
50 */
51 #if defined(PS_PUBKEY_OPTIMIZE_FOR_FASTER_SPEED) &&	defined(PS_PUBKEY_OPTIMIZE_FOR_SMALLER_RAM)
52 #error "May only enable either PS_PUBKEY_OPTIMIZE_FOR_FASTER_SPEED or PS_PUBKEY_OPTIMIZE_FOR_SMALLER_RAM"
53 #endif
54 
55 #if !defined(PS_PUBKEY_OPTIMIZE_FOR_FASTER_SPEED) && !defined(PS_PUBKEY_OPTIMIZE_FOR_SMALLER_RAM)
56 #define PS_PUBKEY_OPTIMIZE_FOR_SMALLER_RAM
57 #endif
58 
59 #ifdef PS_PUBKEY_OPTIMIZE_FOR_SMALLER_RAM
60 #define PS_EXPTMOD_WINSIZE		3
61 #endif
62 
63 #ifdef PS_PUBKEY_OPTIMIZE_FOR_FASTER_SPEED
64 #define PS_EXPTMOD_WINSIZE		5
65 #endif
66 
67 /******************************************************************************/
68 #ifdef USE_RSA
69 /******************************************************************************/
70 /*
71     Primary RSA Key struct.  Define here for crypto
72 */
73 typedef struct {
74     pstm_int    e, d, N, qP, dP, dQ, p, q;
75     uint32      size;   /* Size of the key in bytes */
76     int32       optimized; /* 1 for optimized */
77 } psRsaKey_t;
78 #endif /* USE_RSA */
79 /******************************************************************************/
80 
81 /******************************************************************************/
82 /******************************************************************************/
83 
84 /******************************************************************************/
85 /******************************************************************************/
86 
87 /******************************************************************************/
88 /*
89 	Univeral public key type
90 
91 	The pubKey name comes from the generic public-key crypto terminology and
92 	does not mean these key are restricted to the public side only. These
93 	may be private keys.
94 */
95 /******************************************************************************/
96 typedef union {
97 #ifdef USE_RSA
98     psRsaKey_t	rsa;
99 #else
100 	short		notEmpty; /* Prevents from being empty */
101 #endif /* USE_RSA */
102 } pubKeyUnion_t;
103 
104 typedef struct {
105 	pubKeyUnion_t	*key;
106 	uint32			keysize; /* in bytes */
107 	int32			type; /* PS_RSA, PS_ECC, PS_DH */
108 } psPubKey_t;
109 
110 /******************************************************************************/
111 /*
112 	Internal helpers
113 */
114 extern int32 pkcs1Pad(unsigned char *in, uint32 inlen, unsigned char *out,
115 				uint32 outlen, int32 cryptType);
116 extern int32 pkcs1Unpad(unsigned char *in, uint32 inlen, unsigned char *out,
117 				uint32 outlen, int32 decryptType);
118 
119 #ifdef USE_RSA
120 extern void psRsaFreeKey(psRsaKey_t *key);
121 #endif /* USE_RSA */
122 /******************************************************************************/
123 #endif /* _h_PS_PUBKEY */
124 
125