]> git.saurik.com Git - apple/security.git/blob - AppleCSP/MiscCSPAlgs/desContext.cpp
Security-54.tar.gz
[apple/security.git] / AppleCSP / MiscCSPAlgs / desContext.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 * desContext.cpp - glue between BlockCrytpor and DES implementation
21 * Written by Doug Mitchell 3/28/2001
22 */
23
24 #include "desContext.h"
25 #include <Security/debugging.h>
26 #include <Security/globalizer.h>
27 #include <Security/threading.h>
28
29 ModuleNexus<Mutex> desInitMutex;
30
31 #define DESDebug(args...) debug("desContext", ## args)
32
33 /*
34 * DES encrypt/decrypt.
35 */
36 DESContext::~DESContext()
37 {
38 desdone(&DesInst);
39 memset(&DesInst, 0, sizeof(struct _desInst));
40 }
41
42 /*
43 * Standard CSPContext init, called from CSPFullPluginSession::init().
44 * Reusable, e.g., query followed by en/decrypt.
45 */
46 void DESContext::init(
47 const Context &context,
48 bool encrypting)
49 {
50 UInt32 keyLen;
51 UInt8 *keyData = NULL;
52
53 /* obtain key from context */
54 symmetricKeyBits(context, CSSM_ALGID_DES,
55 encrypting ? CSSM_KEYUSE_ENCRYPT : CSSM_KEYUSE_DECRYPT,
56 keyData, keyLen);
57 if(keyLen != (DES_KEY_SIZE_BITS_EXTERNAL / 8)) {
58 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_KEY);
59 }
60
61 /* init the low-level state */
62 {
63 StLock<Mutex> _(desInitMutex());
64 if(int irtn = desinit(&DesInst, DES_MODE_STD)) {
65 DESDebug("desinit returned %d\n", irtn);
66 CssmError::throwMe(CSSMERR_CSP_MEMORY_ERROR);
67 }
68 }
69 dessetkey(&DesInst, (char *)keyData);
70
71 /* Finally, have BlockCryptor do its setup */
72 setup(DES_BLOCK_SIZE_BYTES, context);
73 }
74
75 /*
76 * Functions called by BlockCryptor
77 * DES does encrypt/decrypt in place
78 */
79 void DESContext::encryptBlock(
80 const void *plainText, // length implied (one block)
81 size_t plainTextLen,
82 void *cipherText,
83 size_t &cipherTextLen, // in/out, throws on overflow
84 bool final) // ignored
85 {
86 if(plainTextLen != DES_BLOCK_SIZE_BYTES) {
87 CssmError::throwMe(CSSMERR_CSP_INPUT_LENGTH_ERROR);
88 }
89 if(cipherTextLen < DES_BLOCK_SIZE_BYTES) {
90 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR);
91 }
92 if(plainText != cipherText) {
93 /* little optimization for callers who want to encrypt in place */
94 memmove(cipherText, plainText, DES_BLOCK_SIZE_BYTES);
95 }
96 endes(&DesInst, (char *)cipherText);
97 cipherTextLen = DES_BLOCK_SIZE_BYTES;
98 }
99
100 void DESContext::decryptBlock(
101 const void *cipherText, // length implied (one block)
102 void *plainText,
103 size_t &plainTextLen, // in/out, throws on overflow
104 bool final) // ignored
105 {
106 if(plainTextLen < DES_BLOCK_SIZE_BYTES) {
107 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR);
108 }
109 if(plainText != cipherText) {
110 /* little optimization for callers who want to decrypt in place */
111 memmove(plainText, cipherText, DES_BLOCK_SIZE_BYTES);
112 }
113 dedes(&DesInst, (char *)plainText);
114 plainTextLen = DES_BLOCK_SIZE_BYTES;
115 }
116
117 /***
118 *** Triple-DES - EDE, 24-bit key only
119 ***/
120
121 DES3Context::~DES3Context()
122 {
123 for(int i =0; i<3; i++) {
124 desdone(&DesInst[i]);
125 memset(&DesInst[i], 0, sizeof(struct _desInst));
126 }
127 }
128
129 /*
130 * Standard CSPContext init, called from CSPFullPluginSession::init().
131 * Reusable, e.g., query followed by en/decrypt.
132 */
133 void DES3Context::init(
134 const Context &context,
135 bool encrypting)
136 {
137 UInt32 keyLen;
138 UInt8 *keyData = NULL;
139
140 /* obtain key from context */
141 symmetricKeyBits(context, CSSM_ALGID_3DES_3KEY_EDE,
142 encrypting ? CSSM_KEYUSE_ENCRYPT : CSSM_KEYUSE_DECRYPT,
143 keyData, keyLen);
144 if(keyLen != DES3_KEY_SIZE_BYTES) {
145 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_KEY);
146 }
147
148 /* init the low-level state */
149 int irtn;
150 unsigned i;
151 {
152 StLock<Mutex> _(desInitMutex());
153 for(i=0; i<3; i++) {
154 if((irtn = desinit(&DesInst[i], DES_MODE_STD))) {
155 DESDebug("desinit returned %d\n", irtn);
156 CssmError::throwMe(CSSMERR_CSP_MEMORY_ERROR);
157 }
158 dessetkey(&DesInst[i], (char *)keyData + (8 * i));
159 }
160 }
161
162 /* Finally, have BlockCryptor do its setup */
163 setup(DES3_BLOCK_SIZE_BYTES, context);
164 }
165
166 /*
167 * Functions called by BlockCryptor
168 * DES does encrypt/decrypt in place
169 */
170 void DES3Context::encryptBlock(
171 const void *plainText, // length implied (one block)
172 size_t plainTextLen,
173 void *cipherText,
174 size_t &cipherTextLen, // in/out, throws on overflow
175 bool final) // ignored
176 {
177 if(plainTextLen != DES3_BLOCK_SIZE_BYTES) {
178 CssmError::throwMe(CSSMERR_CSP_INPUT_LENGTH_ERROR);
179 }
180 if(cipherTextLen < DES3_BLOCK_SIZE_BYTES) {
181 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR);
182 }
183 if(plainText != cipherText) {
184 /* little optimization for callers who want to encrypt in place */
185 memmove(cipherText, plainText, DES3_BLOCK_SIZE_BYTES);
186 }
187
188 /* encrypt --> decrypt --> encrypt */
189 endes(&DesInst[0], (char *)cipherText);
190 dedes(&DesInst[1], (char *)cipherText);
191 endes(&DesInst[2], (char *)cipherText);
192 cipherTextLen = DES3_BLOCK_SIZE_BYTES;
193 }
194
195 void DES3Context::decryptBlock(
196 const void *cipherText, // length implied (one block)
197 void *plainText,
198 size_t &plainTextLen, // in/out, throws on overflow
199 bool final) // ignored
200 {
201 if(plainTextLen < DES3_BLOCK_SIZE_BYTES) {
202 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR);
203 }
204 if(plainText != cipherText) {
205 /* little optimization for callers who want to decrypt in place */
206 memmove(plainText, cipherText, DES3_BLOCK_SIZE_BYTES);
207 }
208
209 /* decrypt --> encrypt -->decrypt */
210 dedes(&DesInst[2], (char *)plainText);
211 endes(&DesInst[1], (char *)plainText);
212 dedes(&DesInst[0], (char *)plainText);
213 plainTextLen = DES3_BLOCK_SIZE_BYTES;
214 }