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