]> git.saurik.com Git - apple/security.git/blob - AppleCSP/MiscCSPAlgs/rc2Context.cpp
3116968906afe0b4de732ff9bfa41770f7989a8d
[apple/security.git] / AppleCSP / MiscCSPAlgs / rc2Context.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 * rc2Context.cpp - glue between BlockCrytpor and ssleay RC2 implementation
21 * Written by Doug Mitchell 04/03/2001
22 */
23
24 #include <openssl/rc2.h>
25 #include <misc/rc2_locl.h>
26 #include "rc2Context.h"
27
28 RC2Context::~RC2Context()
29 {
30 memset(&rc2Key, 0, sizeof(RC2_KEY));
31 }
32
33 /*
34 * Standard CSPContext init, called from CSPFullPluginSession::init().
35 * Reusable, e.g., query followed by en/decrypt.
36 */
37 void RC2Context::init(
38 const Context &context,
39 bool encrypting)
40 {
41 UInt32 keyLen;
42 UInt8 *keyData = NULL;
43 UInt32 effectiveBits;
44
45 /* obtain key from context */
46 symmetricKeyBits(context, CSSM_ALGID_RC2,
47 encrypting ? CSSM_KEYUSE_ENCRYPT : CSSM_KEYUSE_DECRYPT,
48 keyData, keyLen);
49 if((keyLen < RC2_MIN_KEY_SIZE_BYTES) || (keyLen > RC2_MAX_KEY_SIZE_BYTES)) {
50 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_KEY);
51 }
52
53 /*
54 * Optional effective key size in bits - either from Context,
55 * or the key
56 */
57 effectiveBits = context.getInt(CSSM_ATTRIBUTE_EFFECTIVE_BITS);
58 if(effectiveBits == 0) {
59 CssmKey &key = context.get<CssmKey>(CSSM_ATTRIBUTE_KEY,
60 CSSMERR_CSP_MISSING_ATTR_KEY);
61 effectiveBits = key.KeyHeader.LogicalKeySizeInBits;
62 }
63
64 /* init the low-level state */
65 RC2_set_key(&rc2Key, keyLen, keyData, effectiveBits);
66
67 /* Finally, have BlockCryptor do its setup */
68 setup(RC2_BLOCK_SIZE_BYTES, context);
69 }
70
71 /*
72 * Functions called by BlockCryptor
73 */
74 void RC2Context::encryptBlock(
75 const void *plainText, // length implied (one block)
76 size_t plainTextLen,
77 void *cipherText,
78 size_t &cipherTextLen, // in/out, throws on overflow
79 bool final) // ignored
80 {
81 if(plainTextLen != RC2_BLOCK_SIZE_BYTES) {
82 CssmError::throwMe(CSSMERR_CSP_INPUT_LENGTH_ERROR);
83 }
84 if(cipherTextLen < RC2_BLOCK_SIZE_BYTES) {
85 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR);
86 }
87
88 /*
89 * Low-level code operates on array of unsigned 32-bit integers
90 */
91 RC2_INT d[2];
92 RC2_INT l;
93 c2l((unsigned char *)plainText, l); d[0]=l;
94 c2l((unsigned char *)plainText, l); d[1]=l;
95 RC2_encrypt(d, &rc2Key);
96 l=d[0]; l2c(l, (unsigned char *)cipherText);
97 l=d[1]; l2c(l, (unsigned char *)cipherText);
98 cipherTextLen = RC2_BLOCK_SIZE_BYTES;
99 }
100
101 void RC2Context::decryptBlock(
102 const void *cipherText, // length implied (one block)
103 void *plainText,
104 size_t &plainTextLen, // in/out, throws on overflow
105 bool final) // ignored
106 {
107 if(plainTextLen < RC2_BLOCK_SIZE_BYTES) {
108 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR);
109 }
110 /*
111 * Low-level code operates on array of unsigned 32-bit integers
112 */
113 RC2_INT d[2];
114 RC2_INT l;
115 c2l((unsigned char *)cipherText, l); d[0]=l;
116 c2l((unsigned char *)cipherText, l); d[1]=l;
117 RC2_decrypt(d, &rc2Key);
118 l=d[0]; l2c(l, (unsigned char *)plainText);
119 l=d[1]; l2c(l, (unsigned char *)plainText);
120 plainTextLen = RC2_BLOCK_SIZE_BYTES;
121 }
122