2 * Copyright (c) 2000-2001,2011-2012,2014 Apple Inc. All Rights Reserved.
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
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.
20 * FEESignatureObject.cpp - implementations of FEE-style raw sign/verify classes
24 #ifdef CRYPTKIT_CSP_ENABLE
26 #include "FEESignatureObject.h"
27 #include <security_cryptkit/feePublicKey.h>
28 #include <security_cryptkit/feeDigitalSignature.h>
29 #include <security_cryptkit/falloc.h>
32 #include <security_utilities/debugging.h>
34 #define feeSigObjDebug(args...) secdebug("feeSig", ##args)
36 CryptKit::FEESigner::~FEESigner()
38 if(mWeMallocdFeeKey
) {
39 assert(mFeeKey
!= NULL
);
40 feePubKeyFree(mFeeKey
);
45 * obtain key from context, validate, convert to native FEE key
47 void CryptKit::FEESigner::keyFromContext(
48 const Context
&context
)
50 if(initFlag() && (mFeeKey
!= NULL
)) {
51 /* reusing context, OK */
55 CSSM_KEYCLASS keyClass
;
58 /* signing with private key */
59 keyClass
= CSSM_KEYCLASS_PRIVATE_KEY
;
60 keyUse
= CSSM_KEYUSE_SIGN
;
63 /* verifying with public key */
64 keyClass
= CSSM_KEYCLASS_PUBLIC_KEY
;
65 keyUse
= CSSM_KEYUSE_VERIFY
;
68 mFeeKey
= contextToFeeKey(context
,
78 void CryptKit::FEESigner::signerInit(
79 const Context
&context
,
82 setIsSigning(isSigning
);
83 keyFromContext(context
);
88 * Note that, unlike the implementation in security_cryptkit/feePublicKey.c, we ignore
89 * the Pm which used to be used as salt for the digest. That made staged verification
90 * impossible and I do not believe it increased security.
92 void CryptKit::FEERawSigner::sign(
96 size_t *sigLen
) /* IN/OUT */
100 unsigned char *feeSig
;
101 unsigned feeSigLen
=0;
103 if(mFeeKey
== NULL
) {
104 throwCryptKit(FR_BadPubKey
, "FEERawSigner::sign (no key)");
106 fsig
= feeSigNewWithKey(mFeeKey
, mRandFcn
, mRandRef
);
108 throwCryptKit(FR_BadPubKey
, "FEERawSigner::sign");
110 frtn
= feeSigSign(fsig
,
111 (unsigned char *)data
,
114 if(frtn
== FR_Success
) {
115 frtn
= feeSigData(fsig
, &feeSig
, &feeSigLen
);
119 throwCryptKit(frtn
, "FEERawSigner::sign");
122 /* copy out to caller and ffree */
123 if(*sigLen
< feeSigLen
) {
124 feeSigObjDebug("FEERawSigner sign overflow\n");
126 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
128 memmove(sig
, feeSig
, feeSigLen
);
133 void CryptKit::FEERawSigner::verify(
142 if(mFeeKey
== NULL
) {
143 throwCryptKit(FR_BadPubKey
, "FEERawSigner::verify (no key)");
145 frtn
= feeSigParse((unsigned char *)sig
, sigLen
, &fsig
);
147 throwCryptKit(frtn
, "feeSigParse");
149 frtn
= feeSigVerify(fsig
,
150 (unsigned char *)data
,
151 (unsigned int)dataLen
,
155 throwCryptKit(frtn
, NULL
);
159 size_t CryptKit::FEERawSigner::maxSigSize()
164 frtn
= feeSigSize(mFeeKey
, &rtn
);
166 throwCryptKit(frtn
, "feeSigSize");
171 /* ECDSA - this is really easy. */
173 void CryptKit::FEEECDSASigner::sign(
177 size_t *sigLen
) /* IN/OUT */
179 unsigned char *feeSig
;
183 if(mFeeKey
== NULL
) {
184 throwCryptKit(FR_BadPubKey
, "FEERawSigner::sign (no key)");
186 frtn
= feeECDSASign(mFeeKey
,
187 (unsigned char *)data
, // data to be signed
188 (unsigned int)dataLen
, // in bytes
194 throwCryptKit(frtn
, "feeECDSASign");
196 /* copy out to caller and ffree */
197 if(*sigLen
< feeSigLen
) {
198 feeSigObjDebug("feeECDSASign overflow\n");
200 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
202 memmove(sig
, feeSig
, feeSigLen
);
208 void CryptKit::FEEECDSASigner::verify(
216 if(mFeeKey
== NULL
) {
217 throwCryptKit(FR_BadPubKey
, "FEERawSigner::verify (no key)");
219 frtn
= feeECDSAVerify((unsigned char *)sig
,
221 (unsigned char *)data
,
222 (unsigned int)dataLen
,
225 throwCryptKit(frtn
, NULL
);
229 size_t CryptKit::FEEECDSASigner::maxSigSize()
234 frtn
= feeECDSASigSize(mFeeKey
, &rtn
);
236 throwCryptKit(frtn
, "feeECDSASigSize");
241 #endif /* CRYPTKIT_CSP_ENABLE */