2 * Copyright (c) 2000-2001,2011,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 // cryptkitcsp - top C++ implementation layer for CryptKit
23 #ifdef CRYPTKIT_CSP_ENABLE
25 #include "cryptkitcsp.h"
26 #include "FEESignatureObject.h" /* raw signer */
27 #include <SignatureContext.h>
29 #include "FEEAsymmetricContext.h"
30 #include <Security/cssmapple.h>
31 #include <security_cryptkit/falloc.h>
32 #include <security_cryptkit/feeFunctions.h>
33 #include <SHA1_MD5_Object.h>
34 #include <SHA2_Object.h>
35 #include <security_cdsa_utilities/digestobject.h>
37 Allocator
*CryptKitFactory::normAllocator
;
38 Allocator
*CryptKitFactory::privAllocator
;
41 * CryptKit-style memory allocator callbacks
43 static void *ckMalloc(unsigned size
)
45 return CryptKitFactory::privAllocator
->malloc(size
);
47 static void ckFree(void *data
)
49 CryptKitFactory::privAllocator
->free(data
);
51 static void *ckRealloc(void *oldPtr
, unsigned newSize
)
53 return CryptKitFactory::privAllocator
->realloc(oldPtr
, newSize
);
57 // Manage the CryptKit algorithm factory
60 CryptKitFactory::CryptKitFactory(Allocator
*normAlloc
, Allocator
*privAlloc
)
62 setNormAllocator(normAlloc
);
63 setPrivAllocator(privAlloc
);
64 /* once-per-address space */
66 fallocRegister(ckMalloc
, ckFree
, ckRealloc
);
69 CryptKitFactory::~CryptKitFactory()
74 bool CryptKitFactory::setup(
75 AppleCSPSession
&session
,
76 CSPFullPluginSession::CSPContext
* &cspCtx
,
77 const Context
&context
)
79 switch(context
.type()) {
80 case CSSM_ALGCLASS_SIGNATURE
:
81 switch(context
.algorithm()) {
82 case CSSM_ALGID_FEE_MD5
:
84 cspCtx
= new SignatureContext(session
,
86 *(new FEERawSigner(feeRandCallback
,
92 case CSSM_ALGID_FEE_SHA1
:
94 cspCtx
= new SignatureContext(session
,
96 *(new FEERawSigner(feeRandCallback
,
102 case CSSM_ALGID_SHA1WithECDSA
:
104 cspCtx
= new SignatureContext(session
,
106 *(new FEEECDSASigner(feeRandCallback
,
112 case CSSM_ALGID_SHA224WithECDSA
:
114 cspCtx
= new SignatureContext(session
,
115 *(new SHA224Object()),
116 *(new FEEECDSASigner(feeRandCallback
,
122 case CSSM_ALGID_SHA256WithECDSA
:
124 cspCtx
= new SignatureContext(session
,
125 *(new SHA256Object()),
126 *(new FEEECDSASigner(feeRandCallback
,
132 case CSSM_ALGID_SHA384WithECDSA
:
134 cspCtx
= new SignatureContext(session
,
135 *(new SHA384Object()),
136 *(new FEEECDSASigner(feeRandCallback
,
142 case CSSM_ALGID_SHA512WithECDSA
:
144 cspCtx
= new SignatureContext(session
,
145 *(new SHA512Object()),
146 *(new FEEECDSASigner(feeRandCallback
,
155 cspCtx
= new SignatureContext(session
,
157 *(new FEERawSigner(feeRandCallback
,
163 case CSSM_ALGID_ECDSA
:
165 cspCtx
= new SignatureContext(session
,
167 *(new FEEECDSASigner(feeRandCallback
,
178 case CSSM_ALGCLASS_KEYGEN
:
179 switch(context
.algorithm()) {
181 case CSSM_ALGID_ECDSA
:
183 cspCtx
= new CryptKit::FEEKeyPairGenContext(session
, context
);
191 case CSSM_ALGCLASS_ASYMMETRIC
:
192 switch(context
.algorithm()) {
193 case CSSM_ALGID_FEEDEXP
:
195 cspCtx
= new CryptKit::FEEDExpContext(session
);
198 case CSSM_ALGID_FEED
:
200 cspCtx
= new CryptKit::FEEDContext(session
);
208 /* more here - symmetric, etc. */
212 /* not implemented here */
216 #endif /* CRYPTKIT_CSP_ENABLE */