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 // SSContext - cryptographic contexts for the security server
22 #include "SSContext.h"
24 #include "SSCSPSession.h"
27 using namespace SecurityServer
;
32 SSContext::SSContext(SSCSPSession
&session
)
33 : mSession(session
), mContext(NULL
)
38 SSContext::init(const Context
&context
,
39 bool /* encoding */) // @@@ should be removed from API since it's already in mDirection
44 SecurityServer::ClientSession
&
45 SSContext::clientSession()
47 return mSession
.clientSession();
52 // SSRandomContext -- Context for GenerateRandom operations
54 SSRandomContext::SSRandomContext(SSCSPSession
&session
) : SSContext(session
) {}
57 SSRandomContext::init(const Context
&context
, bool encoding
)
59 SSContext::init(context
, encoding
);
61 // set/freeze output size
62 mOutSize
= context
.getInt(CSSM_ATTRIBUTE_OUTPUT_SIZE
, CSSMERR_CSP_MISSING_ATTR_OUTPUT_SIZE
);
65 // seed the PRNG (if specified)
66 if (const CssmCryptoData
*seed
= context
.get
<CssmCryptoData
>(CSSM_ATTRIBUTE_SEED
)) {
67 const CssmData
&seedValue
= (*seed
)();
68 clientSession().seedRandom(seedValue
);
74 SSRandomContext::outputSize(bool final
, size_t inSize
)
80 SSRandomContext::final(CssmData
&out
)
82 clientSession().generateRandom(out
);
87 // SSSignContext -- Context for signing and GenerateMac operations
89 SSSignContext::SSSignContext(SSCSPSession
&session
) : SSContext(session
) {}
92 SSSignContext::update(const CssmData
&data
)
97 SSSignContext::outputSize(bool final
, size_t inSize
)
103 SSSignContext::final(CssmData
&out
)
109 // SSVerifyContext -- Context for Verify and VerifyMac operations
111 SSVerifyContext::SSVerifyContext(SSCSPSession
&session
) : SSContext(session
) {}
114 SSVerifyContext::update(const CssmData
&data
)
119 SSVerifyContext::final(const CssmData
&in
)
125 // SSCryptContext -- Context for Encrypt and Decrypt operations
127 SSCryptContext::SSCryptContext(SSCSPSession
&session
)
128 : SSContext(session
), mKeyHandle(noKey
), mCurrent(0), mCapacity(0),
134 SSCryptContext::~SSCryptContext()
140 SSCryptContext::freeBuffer()
142 // @@@ We should probably use CssmAllocator::standard(sensitive) instead of malloc/realloc/free here
145 // Zero out buffer (only on decrypt?)
146 if (mCapacity
/* && !encoding() */)
148 memset(mBuffer
, 0, mCapacity
);
158 SSCryptContext::init(const Context
&context
, bool encoding
)
160 SSContext::init(context
, encoding
);
166 const CssmKey
&keyInContext
=
167 context
.get
<const CssmKey
>(CSSM_ATTRIBUTE_KEY
,
168 CSSMERR_CSP_MISSING_ATTR_KEY
);
170 // @@@ Should return SSKey.
171 mKeyHandle
= mSession
.lookupKey(keyInContext
).keyHandle();
175 SSCryptContext::inputSize(size_t outSize
)
181 SSCryptContext::outputSize(bool final
, size_t inSize
)
185 mCapacity
= mCurrent
+ inSize
;
186 mBuffer
= realloc(mBuffer
, mCapacity
);
190 // There should not be any remaining input data left when final is true;
193 // Do the actual operation.
194 const CssmData
in(mBuffer
, mCurrent
);
197 clientSession().encrypt(*mContext
, mKeyHandle
, in
, out
);
199 clientSession().decrypt(*mContext
, mKeyHandle
, in
, out
);
203 mCapacity
= out
.Length
;
209 SSCryptContext::minimumProgress(size_t &in
, size_t &out
)
211 // This should never be called.
216 SSCryptContext::update(void *inp
, size_t &inSize
, void *outp
, size_t &outSize
)
220 assert(mCurrent
+ inSize
<= mCapacity
);
221 memcpy(&reinterpret_cast<uint8
*>(mBuffer
)[mCurrent
], inp
, inSize
);
226 SSCryptContext::final(CssmData
&out
)
228 if(!out
.Length
) return;
229 assert(out
.Data
&& out
.Length
);
230 uint32 todo
= min(out
.Length
, mCapacity
- mCurrent
);
231 memcpy(out
.Data
, &reinterpret_cast<uint8
*>(mBuffer
)[mCurrent
], todo
);
241 // SSKeyPairGenContext -- Context for key pair generation
243 SSKeyPairGenContext::SSKeyPairGenContext(SSCSPSession
&session
)
244 : SSContext(session
) {}
247 SSKeyPairGenContext::generate(const Context
&context
,
256 SSKeyPairGenContext::generate(const Context
&context
,
265 // SSSymmKeyGenContext -- Context for symmetric key generation
267 SSSymmKeyGenContext::SSSymmKeyGenContext(SSCSPSession
&session
,
271 : SSContext(session
),
272 minSizeInBits(minSize
),
273 maxSizeInBits(maxSize
),
274 mustBeByteSized(byteSized
)
279 SSSymmKeyGenContext::generateSymKey(const Context
&context
, CssmKey
&cssmKey
)