1162911Ssimon/* crypto/camellia/camellia_misc.c -*- mode:C; c-file-style: "eay" -*- */ 2162911Ssimon/* ==================================================================== 3162911Ssimon * Copyright (c) 2006 The OpenSSL Project. All rights reserved. 4162911Ssimon * 5162911Ssimon * Redistribution and use in source and binary forms, with or without 6162911Ssimon * modification, are permitted provided that the following conditions 7162911Ssimon * are met: 8162911Ssimon * 9162911Ssimon * 1. Redistributions of source code must retain the above copyright 10296465Sdelphij * notice, this list of conditions and the following disclaimer. 11162911Ssimon * 12162911Ssimon * 2. Redistributions in binary form must reproduce the above copyright 13162911Ssimon * notice, this list of conditions and the following disclaimer in 14162911Ssimon * the documentation and/or other materials provided with the 15162911Ssimon * distribution. 16162911Ssimon * 17162911Ssimon * 3. All advertising materials mentioning features or use of this 18162911Ssimon * software must display the following acknowledgment: 19162911Ssimon * "This product includes software developed by the OpenSSL Project 20162911Ssimon * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 21162911Ssimon * 22162911Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 23162911Ssimon * endorse or promote products derived from this software without 24162911Ssimon * prior written permission. For written permission, please contact 25162911Ssimon * openssl-core@openssl.org. 26162911Ssimon * 27162911Ssimon * 5. Products derived from this software may not be called "OpenSSL" 28162911Ssimon * nor may "OpenSSL" appear in their names without prior written 29162911Ssimon * permission of the OpenSSL Project. 30162911Ssimon * 31162911Ssimon * 6. Redistributions of any form whatsoever must retain the following 32162911Ssimon * acknowledgment: 33162911Ssimon * "This product includes software developed by the OpenSSL Project 34162911Ssimon * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 35162911Ssimon * 36162911Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 37162911Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 38162911Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 39162911Ssimon * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 40162911Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 41162911Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 42162911Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 43162911Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 44162911Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 45162911Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 46162911Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 47162911Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE. 48162911Ssimon * ==================================================================== 49162911Ssimon * 50162911Ssimon */ 51296465Sdelphij 52162911Ssimon#include <openssl/opensslv.h> 53162911Ssimon#include <openssl/camellia.h> 54162911Ssimon#include "cmll_locl.h" 55194206Ssimon#include <openssl/crypto.h> 56194206Ssimon#ifdef OPENSSL_FIPS 57296465Sdelphij# include <openssl/fips.h> 58194206Ssimon#endif 59162911Ssimon 60296465Sdelphijconst char CAMELLIA_version[] = "CAMELLIA" OPENSSL_VERSION_PTEXT; 61162911Ssimon 62162911Ssimonint Camellia_set_key(const unsigned char *userKey, const int bits, 63296465Sdelphij CAMELLIA_KEY *key) 64194206Ssimon#ifdef OPENSSL_FIPS 65296465Sdelphij{ 66296465Sdelphij if (FIPS_mode()) 67296465Sdelphij FIPS_BAD_ABORT(CAMELLIA) 68296465Sdelphij return private_Camellia_set_key(userKey, bits, key); 69296465Sdelphij} 70296465Sdelphij 71194206Ssimonint private_Camellia_set_key(const unsigned char *userKey, const int bits, 72296465Sdelphij CAMELLIA_KEY *key) 73194206Ssimon#endif 74296465Sdelphij{ 75296465Sdelphij if (!userKey || !key) { 76296465Sdelphij return -1; 77296465Sdelphij } 78162911Ssimon 79296465Sdelphij switch (bits) { 80296465Sdelphij case 128: 81296465Sdelphij camellia_setup128(userKey, (unsigned int *)key->rd_key); 82296465Sdelphij key->enc = camellia_encrypt128; 83296465Sdelphij key->dec = camellia_decrypt128; 84296465Sdelphij break; 85296465Sdelphij case 192: 86296465Sdelphij camellia_setup192(userKey, (unsigned int *)key->rd_key); 87296465Sdelphij key->enc = camellia_encrypt256; 88296465Sdelphij key->dec = camellia_decrypt256; 89296465Sdelphij break; 90296465Sdelphij case 256: 91296465Sdelphij camellia_setup256(userKey, (unsigned int *)key->rd_key); 92296465Sdelphij key->enc = camellia_encrypt256; 93296465Sdelphij key->dec = camellia_decrypt256; 94296465Sdelphij break; 95296465Sdelphij default: 96296465Sdelphij return -2; 97296465Sdelphij } 98296465Sdelphij 99296465Sdelphij key->bitLength = bits; 100296465Sdelphij return 0; 101296465Sdelphij} 102296465Sdelphij 103162911Ssimonvoid Camellia_encrypt(const unsigned char *in, unsigned char *out, 104296465Sdelphij const CAMELLIA_KEY *key) 105296465Sdelphij{ 106296465Sdelphij u32 tmp[CAMELLIA_BLOCK_SIZE / sizeof(u32)]; 107296465Sdelphij const union { 108296465Sdelphij long one; 109296465Sdelphij char little; 110296465Sdelphij } camellia_endian = { 111296465Sdelphij 1 112296465Sdelphij }; 113162911Ssimon 114296465Sdelphij memcpy(tmp, in, CAMELLIA_BLOCK_SIZE); 115296465Sdelphij if (camellia_endian.little) 116296465Sdelphij SWAP4WORD(tmp); 117296465Sdelphij key->enc(key->rd_key, tmp); 118296465Sdelphij if (camellia_endian.little) 119296465Sdelphij SWAP4WORD(tmp); 120296465Sdelphij memcpy(out, tmp, CAMELLIA_BLOCK_SIZE); 121296465Sdelphij} 122162911Ssimon 123162911Ssimonvoid Camellia_decrypt(const unsigned char *in, unsigned char *out, 124296465Sdelphij const CAMELLIA_KEY *key) 125296465Sdelphij{ 126296465Sdelphij u32 tmp[CAMELLIA_BLOCK_SIZE / sizeof(u32)]; 127296465Sdelphij const union { 128296465Sdelphij long one; 129296465Sdelphij char little; 130296465Sdelphij } camellia_endian = { 131296465Sdelphij 1 132296465Sdelphij }; 133162911Ssimon 134296465Sdelphij memcpy(tmp, in, CAMELLIA_BLOCK_SIZE); 135296465Sdelphij if (camellia_endian.little) 136296465Sdelphij SWAP4WORD(tmp); 137296465Sdelphij key->dec(key->rd_key, tmp); 138296465Sdelphij if (camellia_endian.little) 139296465Sdelphij SWAP4WORD(tmp); 140296465Sdelphij memcpy(out, tmp, CAMELLIA_BLOCK_SIZE); 141296465Sdelphij} 142