]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_apple_csp/lib/bsafeAsymmetric.cpp
Security-58286.270.3.0.1.tar.gz
[apple/security.git] / OSX / libsecurity_apple_csp / lib / bsafeAsymmetric.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 #ifdef BSAFE_CSP_ENABLE
19
20
21 //
22 // bsafeAsymmetric.cpp - asymmetric encrypt/decrypt
23 //
24 #include "bsafecspi.h"
25
26 #include <stdio.h> // debug
27
28 //
29 // Public key {en,de}cryption (currently RSA only)
30 //
31 // FIXME:
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)
35 {
36 assert(context.algorithm() == CSSM_ALGID_RSA);
37
38 if (reusing(encrypting))
39 return; // all set to go
40
41 switch (context.getInt(CSSM_ATTRIBUTE_MODE)) {
42 case CSSM_ALGMODE_PUBLIC_KEY:
43 setAlgorithm(AI_PKCS_RSAPublic);
44 break;
45 case CSSM_ALGMODE_PRIVATE_KEY:
46 setAlgorithm(AI_PKCS_RSAPrivate);
47 break;
48 case CSSM_ALGMODE_NONE:
49 {
50 /*
51 * None specified (getInt returns zero in that case) -
52 * infer from key type
53 */
54 CssmKey &key = context.get<CssmKey>(
55 CSSM_ATTRIBUTE_KEY, CSSMERR_CSP_MISSING_ATTR_KEY);
56 B_INFO_TYPE bAlgType;
57 switch (key.keyClass()) {
58 case CSSM_KEYCLASS_PUBLIC_KEY:
59 bAlgType = AI_PKCS_RSAPublic;
60 break;
61 case CSSM_KEYCLASS_PRIVATE_KEY:
62 bAlgType = AI_PKCS_RSAPrivate;
63 break;
64 default:
65 CssmError::throwMe(CSSMERR_CSP_INVALID_KEY_CLASS);
66 }
67 setAlgorithm(bAlgType);
68 break;
69 }
70 default:
71 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_MODE);
72 }
73
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
79 }
80
81 // we assume asymmetric crypto algorithms are one-shot output non-repeating
82
83 size_t BSafe::PublicKeyCipherContext::inputSize(size_t outSize)
84 {
85 return 0xFFFFFFFF; // perhaps not the biggest size_t, but big enough...
86 }
87 #endif /* BSAFE_CSP_ENABLE */