2 * Copyright (c) 2000-2001,2011-2012,2014 Apple Inc. All Rights Reserved.
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
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.
20 * rc2Context.cpp - glue between BlockCrytpor and ssleay RC2 implementation
23 #include <openssl/rc2_legacy.h>
24 #include <misc/rc2_locl.h>
25 #include "rc2Context.h"
27 RC2Context::~RC2Context()
29 memset(&rc2Key
, 0, sizeof(RC2_KEY
));
33 * Standard CSPContext init, called from CSPFullPluginSession::init().
34 * Reusable, e.g., query followed by en/decrypt.
36 void RC2Context::init(
37 const Context
&context
,
41 uint8
*keyData
= NULL
;
44 /* obtain key from context */
45 symmetricKeyBits(context
, session(), CSSM_ALGID_RC2
,
46 encrypting
? CSSM_KEYUSE_ENCRYPT
: CSSM_KEYUSE_DECRYPT
,
48 if((keyLen
< RC2_MIN_KEY_SIZE_BYTES
) || (keyLen
> RC2_MAX_KEY_SIZE_BYTES
)) {
49 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_KEY
);
53 * Optional effective key size in bits - either from Context,
56 effectiveBits
= context
.getInt(CSSM_ATTRIBUTE_EFFECTIVE_BITS
);
57 if(effectiveBits
== 0) {
58 CssmKey
&key
= context
.get
<CssmKey
>(CSSM_ATTRIBUTE_KEY
,
59 CSSMERR_CSP_MISSING_ATTR_KEY
);
60 effectiveBits
= key
.KeyHeader
.LogicalKeySizeInBits
;
63 /* init the low-level state */
64 RC2_set_key(&rc2Key
, (int)keyLen
, keyData
, effectiveBits
);
66 /* Finally, have BlockCryptor do its setup */
67 setup(RC2_BLOCK_SIZE_BYTES
, context
);
71 * Functions called by BlockCryptor
73 void RC2Context::encryptBlock(
74 const void *plainText
, // length implied (one block)
77 size_t &cipherTextLen
, // in/out, throws on overflow
78 bool final
) // ignored
80 if(plainTextLen
!= RC2_BLOCK_SIZE_BYTES
) {
81 CssmError::throwMe(CSSMERR_CSP_INPUT_LENGTH_ERROR
);
83 if(cipherTextLen
< RC2_BLOCK_SIZE_BYTES
) {
84 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
88 * Low-level code operates on array of unsigned 32-bit integers
92 const unsigned char *pt
= (const unsigned char *)plainText
;
95 RC2_encrypt(d
, &rc2Key
);
96 unsigned char *ct
= (unsigned char *)cipherText
;
99 cipherTextLen
= RC2_BLOCK_SIZE_BYTES
;
102 void RC2Context::decryptBlock(
103 const void *cipherText
, // length implied (one block)
104 size_t cipherTextLen
,
106 size_t &plainTextLen
, // in/out, throws on overflow
107 bool final
) // ignored
109 if(plainTextLen
< RC2_BLOCK_SIZE_BYTES
) {
110 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
113 * Low-level code operates on array of unsigned 32-bit integers
117 const unsigned char *ct
= (const unsigned char *)cipherText
;
120 RC2_decrypt(d
, &rc2Key
);
121 unsigned char *pt
= (unsigned char *)plainText
;
124 plainTextLen
= RC2_BLOCK_SIZE_BYTES
;