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 * rc5Context.cpp - glue between BlockCrytpor and ssleay RC5 implementation
23 #include <openssl/rc5.h>
24 #include <misc/rc5_locl.h>
25 #include "rc5Context.h"
27 RC5Context::~RC5Context()
29 memset(&rc5Key
, 0, sizeof(RC5_32_KEY
));
33 * Standard CSPContext init, called from CSPFullPluginSession::init().
34 * Reusable, e.g., query followed by en/decrypt.
36 void RC5Context::init(
37 const Context
&context
,
41 uint8
*keyData
= NULL
;
42 uint32 rounds
= RC5_16_ROUNDS
;
44 /* obtain key from context */
45 symmetricKeyBits(context
, session(), CSSM_ALGID_RC5
,
46 encrypting
? CSSM_KEYUSE_ENCRYPT
: CSSM_KEYUSE_DECRYPT
,
48 if((keyLen
< RC5_MIN_KEY_SIZE_BYTES
) || (keyLen
> RC5_MAX_KEY_SIZE_BYTES
)) {
49 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_KEY
);
55 rounds
= context
.getInt(CSSM_ATTRIBUTE_ROUNDS
);
58 rounds
= RC5_16_ROUNDS
;
61 /* init the low-level state */
62 RC5_32_set_key(&rc5Key
, (int)keyLen
, keyData
, rounds
);
64 /* Finally, have BlockCryptor do its setup */
65 setup(RC5_BLOCK_SIZE_BYTES
, context
);
69 * Functions called by BlockCryptor
71 void RC5Context::encryptBlock(
72 const void *plainText
, // length implied (one block)
75 size_t &cipherTextLen
, // in/out, throws on overflow
76 bool final
) // ignored
78 if(plainTextLen
!= RC5_BLOCK_SIZE_BYTES
) {
79 CssmError::throwMe(CSSMERR_CSP_INPUT_LENGTH_ERROR
);
81 if(cipherTextLen
< RC5_BLOCK_SIZE_BYTES
) {
82 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
86 * Low-level code operates on array of unsigned 32-bit integers
90 const unsigned char *pt
= (const unsigned char *)plainText
;
93 RC5_32_encrypt(d
, &rc5Key
);
94 unsigned char *ct
= (unsigned char *)cipherText
;
97 cipherTextLen
= RC5_BLOCK_SIZE_BYTES
;
100 void RC5Context::decryptBlock(
101 const void *cipherText
, // length implied (one block)
102 size_t cipherTextLen
,
104 size_t &plainTextLen
, // in/out, throws on overflow
105 bool final
) // ignored
107 if(plainTextLen
< RC5_BLOCK_SIZE_BYTES
) {
108 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
111 * Low-level code operates on array of unsigned 32-bit integers
115 const unsigned char *ct
= (const unsigned char *)cipherText
;
118 RC5_32_decrypt(d
, &rc5Key
);
119 unsigned char *pt
= (unsigned char *)plainText
;
122 plainTextLen
= RC5_BLOCK_SIZE_BYTES
;