2  * Copyright (c) 2000-2001 Apple Computer, 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 
  22  * Created 2/20/2001 by dmitch. 
  25 #ifdef  CRYPTKIT_CSP_ENABLE 
  27 #include "FEESignatureObject.h" 
  28 #include <security_cryptkit/feePublicKey.h> 
  29 #include <security_cryptkit/feeDigitalSignature.h> 
  30 #include <security_cryptkit/falloc.h> 
  33 #include <security_utilities/debugging.h> 
  35 #define feeSigObjDebug(args...)         secdebug("feeSig", ##args) 
  37 CryptKit::FEESigner::~FEESigner() 
  39         if(mWeMallocdFeeKey
) { 
  40                 assert(mFeeKey 
!= NULL
); 
  41                 feePubKeyFree(mFeeKey
); 
  46  * obtain key from context, validate, convert to native FEE key 
  48 void CryptKit::FEESigner::keyFromContext( 
  49         const Context   
&context
) 
  51         if(initFlag() && (mFeeKey 
!= NULL
)) { 
  52                 /* reusing context, OK */ 
  56         CSSM_KEYCLASS   keyClass
; 
  59                 /* signing with private key */ 
  60                 keyClass 
= CSSM_KEYCLASS_PRIVATE_KEY
; 
  61                 keyUse   
= CSSM_KEYUSE_SIGN
; 
  64                 /* verifying with public key */ 
  65                 keyClass 
= CSSM_KEYCLASS_PUBLIC_KEY
; 
  66                 keyUse   
= CSSM_KEYUSE_VERIFY
; 
  69                 mFeeKey 
= contextToFeeKey(context
, 
  79 void CryptKit::FEESigner::signerInit( 
  80         const Context   
&context
, 
  83         setIsSigning(isSigning
); 
  84         keyFromContext(context
); 
  89  * Note that, unlike the implementation in security_cryptkit/feePublicKey.c, we ignore 
  90  * the Pm which used to be used as salt for the digest. That made staged verification 
  91  * impossible and I do not believe it increased security.  
  93 void CryptKit::FEERawSigner::sign( 
  97         size_t                  *sigLen
)        /* IN/OUT */ 
 101         unsigned char   *feeSig
; 
 104         if(mFeeKey 
== NULL
) { 
 105                 throwCryptKit(FR_BadPubKey
, "FEERawSigner::sign (no key)"); 
 107         fsig 
= feeSigNewWithKey(mFeeKey
, mRandFcn
, mRandRef
); 
 109                 throwCryptKit(FR_BadPubKey
, "FEERawSigner::sign"); 
 111         frtn 
= feeSigSign(fsig
, 
 112                 (unsigned char *)data
, 
 115         if(frtn 
== FR_Success
) { 
 116                 frtn 
= feeSigData(fsig
, &feeSig
, &feeSigLen
); 
 120                 throwCryptKit(frtn
, "FEERawSigner::sign"); 
 123         /* copy out to caller and ffree */ 
 124         if(*sigLen 
< feeSigLen
) { 
 125                 feeSigObjDebug("FEERawSigner sign overflow\n"); 
 127                 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
); 
 129         memmove(sig
, feeSig
, feeSigLen
); 
 134 void CryptKit::FEERawSigner::verify( 
 143         if(mFeeKey 
== NULL
) { 
 144                 throwCryptKit(FR_BadPubKey
, "FEERawSigner::verify (no key)"); 
 146         frtn 
= feeSigParse((unsigned char *)sig
, sigLen
, &fsig
); 
 148                 throwCryptKit(frtn
, "feeSigParse"); 
 150         frtn 
= feeSigVerify(fsig
, 
 151                 (unsigned char *)data
, 
 156                 throwCryptKit(frtn
, NULL
); 
 160 size_t CryptKit::FEERawSigner::maxSigSize() 
 165         frtn 
= feeSigSize(mFeeKey
, &rtn
); 
 167                 throwCryptKit(frtn
, "feeSigSize"); 
 172 /* ECDSA - this is really easy. */ 
 174 void CryptKit::FEEECDSASigner::sign( 
 178         size_t                  *sigLen
)        /* IN/OUT */ 
 180         unsigned char   *feeSig
; 
 184         if(mFeeKey 
== NULL
) { 
 185                 throwCryptKit(FR_BadPubKey
, "FEERawSigner::sign (no key)"); 
 187         frtn 
= feeECDSASign(mFeeKey
, 
 188                 (unsigned char *)data
,   // data to be signed 
 195                 throwCryptKit(frtn
, "feeECDSASign"); 
 197         /* copy out to caller and ffree */ 
 198         if(*sigLen 
< feeSigLen
) { 
 199                 feeSigObjDebug("feeECDSASign overflow\n"); 
 201                 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
); 
 203         memmove(sig
, feeSig
, feeSigLen
); 
 209 void CryptKit::FEEECDSASigner::verify( 
 217         if(mFeeKey 
== NULL
) { 
 218                 throwCryptKit(FR_BadPubKey
, "FEERawSigner::verify (no key)"); 
 220         frtn 
= feeECDSAVerify((unsigned char *)sig
, 
 222                 (unsigned char *)data
, 
 226                 throwCryptKit(frtn
, NULL
); 
 230 size_t CryptKit::FEEECDSASigner::maxSigSize() 
 235         frtn 
= feeECDSASigSize(mFeeKey
, &rtn
); 
 237                 throwCryptKit(frtn
, "feeECDSASigSize"); 
 242 #endif  /* CRYPTKIT_CSP_ENABLE */