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.
18 #ifdef BSAFE_CSP_ENABLE
22 // bsafeAsymmetric.cpp - asymmetric encrypt/decrypt
24 #include "bsafecspi.h"
26 #include <stdio.h> // debug
29 // Public key {en,de}cryption (currently RSA only)
32 // We really should match the key algorithm to the en/decrypt
33 // algorithm. Also: verify key usage bits.
34 void BSafe::PublicKeyCipherContext::init(const Context
&context
, bool encrypting
)
36 assert(context
.algorithm() == CSSM_ALGID_RSA
);
38 if (reusing(encrypting
))
39 return; // all set to go
41 switch (context
.getInt(CSSM_ATTRIBUTE_MODE
)) {
42 case CSSM_ALGMODE_PUBLIC_KEY
:
43 setAlgorithm(AI_PKCS_RSAPublic
);
45 case CSSM_ALGMODE_PRIVATE_KEY
:
46 setAlgorithm(AI_PKCS_RSAPrivate
);
48 case CSSM_ALGMODE_NONE
:
51 * None specified (getInt returns zero in that case) -
54 CssmKey
&key
= context
.get
<CssmKey
>(
55 CSSM_ATTRIBUTE_KEY
, CSSMERR_CSP_MISSING_ATTR_KEY
);
57 switch (key
.keyClass()) {
58 case CSSM_KEYCLASS_PUBLIC_KEY
:
59 bAlgType
= AI_PKCS_RSAPublic
;
61 case CSSM_KEYCLASS_PRIVATE_KEY
:
62 bAlgType
= AI_PKCS_RSAPrivate
;
65 CssmError::throwMe(CSSMERR_CSP_INVALID_KEY_CLASS
);
67 setAlgorithm(bAlgType
);
71 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_MODE
);
74 // put it all together
75 setKeyFromContext(context
); // set BSafe key
76 setRandom(); // some PK cryption algs need random input
77 cipherInit(); // common cipher init
78 //@@@ calculate output buffer size
81 // we assume asymmetric crypto algorithms are one-shot output non-repeating
83 size_t BSafe::PublicKeyCipherContext::inputSize(size_t outSize
)
85 return 0xFFFFFFFF; // perhaps not the biggest size_t, but big enough...
87 #endif /* BSAFE_CSP_ENABLE */