]> git.saurik.com Git - apple/security.git/blob - libsecurity_cdsa_client/lib/cryptoclient.cpp
Security-55163.44.tar.gz
[apple/security.git] / libsecurity_cdsa_client / lib / cryptoclient.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, 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 // cryptoclient - client interface to CSSM CSP encryption/decryption operations
21 //
22 #include <security_cdsa_client/cryptoclient.h>
23
24 using namespace CssmClient;
25
26
27 Crypt::Crypt(const CSP &csp, CSSM_ALGORITHMS alg)
28 : Context(csp, alg), mMode(CSSM_ALGMODE_NONE), mInitVector(NULL),
29 mPadding(CSSM_PADDING_NONE)
30 {
31 }
32
33 void Crypt::key(const Key &key)
34 {
35 mKey = key;
36 set(CSSM_ATTRIBUTE_KEY, static_cast<const CssmKey &>(key));
37 }
38
39
40 void
41 Crypt::activate()
42 {
43 if (!mActive)
44 {
45 // Key is required unless we have a NULL algorithm (cleartext wrap/unwrap),
46 // in which case we'll make a symmetric context (it shouldn't matter then).
47 if (!mKey && mAlgorithm != CSSM_ALGID_NONE)
48 CssmError::throwMe(CSSMERR_CSP_MISSING_ATTR_KEY);
49 if (!mKey || mKey->keyClass() == CSSM_KEYCLASS_SESSION_KEY)
50 { // symmetric key
51 check(CSSM_CSP_CreateSymmetricContext(attachment()->handle(), mAlgorithm,
52 mMode, neededCred(), mKey, mInitVector, mPadding, NULL,
53 &mHandle));
54 }
55 else
56 {
57 check(CSSM_CSP_CreateAsymmetricContext(attachment()->handle(), mAlgorithm,
58 neededCred(), mKey, mPadding, &mHandle));
59 //@@@ stick mode and initVector explicitly into the context?
60 }
61 mActive = true;
62 }
63 }
64
65
66 //
67 // Manage encryption contexts
68 //
69 CSSM_SIZE
70 Encrypt::encrypt(const CssmData *in, uint32 inCount,
71 CssmData *out, uint32 outCount, CssmData &remData)
72 {
73 unstaged();
74 CSSM_SIZE total;
75 check(CSSM_EncryptData(handle(), in, inCount, out, outCount, &total, &remData));
76 return total;
77 }
78
79 void
80 Encrypt::init()
81 {
82 check(CSSM_EncryptDataInit(handle()));
83 mStaged = true;
84 }
85
86 CSSM_SIZE
87 Encrypt::encrypt(const CssmData *in, uint32 inCount,
88 CssmData *out, uint32 outCount)
89 {
90 staged();
91 CSSM_SIZE total;
92 check(CSSM_EncryptDataUpdate(handle(), in, inCount, out, outCount, &total));
93 return total;
94 }
95
96 void
97 Encrypt::final(CssmData &remData)
98 {
99 staged();
100 check(CSSM_EncryptDataFinal(handle(), &remData));
101 mStaged = false;
102 }
103
104
105 //
106 // Manage Decryption contexts
107 //
108
109 CSSM_SIZE
110 Decrypt::decrypt(const CssmData *in, uint32 inCount,
111 CssmData *out, uint32 outCount, CssmData &remData)
112 {
113 unstaged();
114 CSSM_SIZE total;
115 check(CSSM_DecryptData(handle(), in, inCount, out, outCount, &total, &remData));
116 return total;
117 }
118
119 void
120 Decrypt::init()
121 {
122 check(CSSM_DecryptDataInit(handle()));
123 mStaged = true;
124 }
125
126 CSSM_SIZE
127 Decrypt::decrypt(const CssmData *in, uint32 inCount,
128 CssmData *out, uint32 outCount)
129 {
130 staged();
131 CSSM_SIZE total;
132 check(CSSM_DecryptDataUpdate(handle(), in, inCount, out, outCount, &total));
133 return total;
134 }
135
136 void
137 Decrypt::final(CssmData &remData)
138 {
139 staged();
140 check(CSSM_DecryptDataFinal(handle(), &remData));
141 mStaged = false;
142 }