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