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