]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_apple_csp/lib/cryptkitcsp.cpp
Security-59306.120.7.tar.gz
[apple/security.git] / OSX / libsecurity_apple_csp / lib / cryptkitcsp.cpp
1 /*
2 * Copyright (c) 2000-2001,2011,2014 Apple 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 // cryptkitcsp - top C++ implementation layer for CryptKit
21 //
22
23 #ifdef CRYPTKIT_CSP_ENABLE
24
25 #include "cryptkitcsp.h"
26 #include "FEESignatureObject.h" /* raw signer */
27 #include <SignatureContext.h>
28 #include "FEEKeys.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>
36
37 Allocator *CryptKitFactory::normAllocator;
38 Allocator *CryptKitFactory::privAllocator;
39
40 /*
41 * CryptKit-style memory allocator callbacks
42 */
43 static void *ckMalloc(unsigned size)
44 {
45 return CryptKitFactory::privAllocator->malloc(size);
46 }
47 static void ckFree(void *data)
48 {
49 CryptKitFactory::privAllocator->free(data);
50 }
51 static void *ckRealloc(void *oldPtr, unsigned newSize)
52 {
53 return CryptKitFactory::privAllocator->realloc(oldPtr, newSize);
54 }
55
56 //
57 // Manage the CryptKit algorithm factory
58 //
59
60 CryptKitFactory::CryptKitFactory(Allocator *normAlloc, Allocator *privAlloc)
61 {
62 setNormAllocator(normAlloc);
63 setPrivAllocator(privAlloc);
64 /* once-per-address space */
65 initCryptKit();
66 fallocRegister(ckMalloc, ckFree, ckRealloc);
67 }
68
69 CryptKitFactory::~CryptKitFactory()
70 {
71 terminateCryptKit();
72 }
73
74 bool CryptKitFactory::setup(
75 AppleCSPSession &session,
76 CSPFullPluginSession::CSPContext * &cspCtx,
77 const Context &context)
78 {
79 switch(context.type()) {
80 case CSSM_ALGCLASS_SIGNATURE:
81 switch(context.algorithm()) {
82 case CSSM_ALGID_FEE_MD5:
83 if(cspCtx == NULL) {
84 cspCtx = new SignatureContext(session,
85 *(new MD5Object()),
86 *(new FEERawSigner(feeRandCallback,
87 &session,
88 session,
89 *privAllocator)));
90 }
91 return true;
92 case CSSM_ALGID_FEE_SHA1:
93 if(cspCtx == NULL) {
94 cspCtx = new SignatureContext(session,
95 *(new SHA1Object()),
96 *(new FEERawSigner(feeRandCallback,
97 &session,
98 session,
99 *privAllocator)));
100 }
101 return true;
102 case CSSM_ALGID_SHA1WithECDSA:
103 if(cspCtx == NULL) {
104 cspCtx = new SignatureContext(session,
105 *(new SHA1Object()),
106 *(new FEEECDSASigner(feeRandCallback,
107 &session,
108 session,
109 *privAllocator)));
110 }
111 return true;
112 case CSSM_ALGID_SHA224WithECDSA:
113 if(cspCtx == NULL) {
114 cspCtx = new SignatureContext(session,
115 *(new SHA224Object()),
116 *(new FEEECDSASigner(feeRandCallback,
117 &session,
118 session,
119 *privAllocator)));
120 }
121 return true;
122 case CSSM_ALGID_SHA256WithECDSA:
123 if(cspCtx == NULL) {
124 cspCtx = new SignatureContext(session,
125 *(new SHA256Object()),
126 *(new FEEECDSASigner(feeRandCallback,
127 &session,
128 session,
129 *privAllocator)));
130 }
131 return true;
132 case CSSM_ALGID_SHA384WithECDSA:
133 if(cspCtx == NULL) {
134 cspCtx = new SignatureContext(session,
135 *(new SHA384Object()),
136 *(new FEEECDSASigner(feeRandCallback,
137 &session,
138 session,
139 *privAllocator)));
140 }
141 return true;
142 case CSSM_ALGID_SHA512WithECDSA:
143 if(cspCtx == NULL) {
144 cspCtx = new SignatureContext(session,
145 *(new SHA512Object()),
146 *(new FEEECDSASigner(feeRandCallback,
147 &session,
148 session,
149 *privAllocator)));
150 }
151 return true;
152
153 case CSSM_ALGID_FEE:
154 if(cspCtx == NULL) {
155 cspCtx = new SignatureContext(session,
156 *(new NullDigest()),
157 *(new FEERawSigner(feeRandCallback,
158 &session,
159 session,
160 *privAllocator)));
161 }
162 return true;
163 case CSSM_ALGID_ECDSA:
164 if(cspCtx == NULL) {
165 cspCtx = new SignatureContext(session,
166 *(new NullDigest()),
167 *(new FEEECDSASigner(feeRandCallback,
168 &session,
169 session,
170 *privAllocator)));
171 }
172 return true;
173 default:
174 break;
175 }
176 break;
177
178 case CSSM_ALGCLASS_KEYGEN:
179 switch(context.algorithm()) {
180 case CSSM_ALGID_FEE:
181 case CSSM_ALGID_ECDSA:
182 if(cspCtx == NULL) {
183 cspCtx = new CryptKit::FEEKeyPairGenContext(session, context);
184 }
185 return true;
186 default:
187 break;
188 }
189 break;
190
191 case CSSM_ALGCLASS_ASYMMETRIC:
192 switch(context.algorithm()) {
193 case CSSM_ALGID_FEEDEXP:
194 if(cspCtx == NULL) {
195 cspCtx = new CryptKit::FEEDExpContext(session);
196 }
197 return true;
198 case CSSM_ALGID_FEED:
199 if(cspCtx == NULL) {
200 cspCtx = new CryptKit::FEEDContext(session);
201 }
202 return true;
203 default:
204 break;
205 }
206 break;
207
208 /* more here - symmetric, etc. */
209 default:
210 break;
211 }
212 /* not implemented here */
213 return false;
214 }
215
216 #endif /* CRYPTKIT_CSP_ENABLE */
217
218