1/*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19/*
20 * RSA_DSA_signature.h - openssl-based signature classes.
21 */
22
23#ifndef	_RSA_DSA_SIGNATURE_H_
24#define _RSA_DSA_SIGNATURE_H_
25
26#include <openssl/rsa.h>
27#include <openssl/dsa.h>
28#include <RawSigner.h>
29#include <AppleCSPSession.h>
30
31#define RSA_SIG_PADDING_DEFAULT		RSA_PKCS1_PADDING
32
33class RSASigner : public RawSigner {
34public:
35	RSASigner(
36		Allocator	&alloc,
37		AppleCSPSession &session,
38		CSSM_ALGORITHMS	digestAlg) :
39			RawSigner(alloc, digestAlg),
40			mRsaKey(NULL),
41			mWeMallocdRsaKey(false),
42			mSession(session),
43			mPadding(RSA_SIG_PADDING_DEFAULT) { }
44
45	~RSASigner();
46
47	/* reusable init */
48	void signerInit(
49		const Context 	&context,
50		bool			isSigning);
51
52
53	/* sign */
54	void sign(
55		const void 		*data,
56		size_t 			dataLen,
57		void			*sig,
58		size_t			*sigLen);	/* IN/OUT */
59
60	/* verify */
61	void verify(
62		const void 	*data,
63		size_t 			dataLen,
64		const void		*sig,
65		size_t			sigLen);
66
67	/* works for both, but only used for signing */
68	size_t maxSigSize();
69
70private:
71
72	/*
73	 * obtain key from context, validate, convert to RSA key
74	 */
75	void keyFromContext(
76		const Context 	&context);
77
78	RSA					*mRsaKey;
79	bool				mWeMallocdRsaKey;
80	AppleCSPSession		&mSession;
81	int					mPadding;		// RSA_NO_PADDING, RSA_PKCS1_PADDING
82};
83
84class DSASigner : public RawSigner {
85public:
86	DSASigner(
87		Allocator	&alloc,
88		AppleCSPSession &session,
89		CSSM_ALGORITHMS	digestAlg) :
90			RawSigner(alloc, digestAlg),
91			mDsaKey(NULL),
92			mWeMallocdDsaKey(false),
93			mSession(session) { }
94
95	~DSASigner();
96
97	/* reusable init */
98	void signerInit(
99		const Context 	&context,
100		bool			isSigning);
101
102
103	/* sign */
104	void sign(
105		const void 		*data,
106		size_t 			dataLen,
107		void			*sig,
108		size_t			*sigLen);	/* IN/OUT */
109
110	/* verify */
111	void verify(
112		const void 	*data,
113		size_t 			dataLen,
114		const void		*sig,
115		size_t			sigLen);
116
117	/* works for both, but only used for signing */
118	size_t maxSigSize();
119
120private:
121
122	/*
123	 * obtain key from context, validate, convert to DSA key
124	 */
125	void keyFromContext(
126		const Context 	&context);
127
128	DSA					*mDsaKey;
129	bool				mWeMallocdDsaKey;
130	AppleCSPSession		&mSession;
131};
132
133
134#endif	/* _RSA_DSA_SIGNATURE_H_ */
135