]> git.saurik.com Git - apple/security.git/blob - AppleCSP/MiscCSPAlgs/rc4Context.cpp
Security-54.tar.gz
[apple/security.git] / AppleCSP / MiscCSPAlgs / rc4Context.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 * rc4Context.cpp - glue between BlockCrytpor and ssleay RC4 implementation
21 * Written by Doug Mitchell 04/03/2001
22 */
23
24 #include <openssl/rc4.h>
25 #include "rc4Context.h"
26
27 RC4Context::~RC4Context()
28 {
29 memset(&rc4Key, 0, sizeof(RC4_KEY));
30 }
31
32 /*
33 * Standard CSPContext init, called from CSPFullPluginSession::init().
34 * Reusable, e.g., query followed by en/decrypt.
35 */
36 void RC4Context::init(
37 const Context &context,
38 bool encrypting)
39 {
40 UInt32 keyLen;
41 UInt8 *keyData = NULL;
42
43 /* obtain key from context */
44 symmetricKeyBits(context, CSSM_ALGID_RC4,
45 encrypting ? CSSM_KEYUSE_ENCRYPT : CSSM_KEYUSE_DECRYPT,
46 keyData, keyLen);
47 if((keyLen < RC4_MIN_KEY_SIZE_BYTES) || (keyLen > RC4_MAX_KEY_SIZE_BYTES)) {
48 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_KEY);
49 }
50
51 /* All other context attributes ignored */
52 /* init the low-level state */
53 RC4_set_key(&rc4Key, keyLen, keyData);
54 }
55
56 /*
57 * All of these functions are called by CSPFullPluginSession.
58 */
59 void RC4Context::update(
60 void *inp,
61 size_t &inSize, // in/out
62 void *outp,
63 size_t &outSize) // in/out
64 {
65 RC4(&rc4Key, inSize, (unsigned char *)inp, (unsigned char *)outp);
66 outSize = inSize;
67 }
68
69 /* remainding functions are trivial for any stream cipher */
70 void RC4Context::final(
71 CssmData &out)
72 {
73 out.length(0);
74 }
75
76 size_t RC4Context::inputSize(
77 size_t outSize) // input for given output size
78 {
79 return outSize;
80 }
81
82 size_t RC4Context::outputSize(
83 bool final = false,
84 size_t inSize = 0) // output for given input size
85 {
86 return inSize;
87 }
88
89 void RC4Context::minimumProgress(
90 size_t &in,
91 size_t &out) // minimum progress chunks
92 {
93 in = 1;
94 out = 1;
95 }