cmll_locl.h revision 167612
1162911Ssimon/* crypto/camellia/camellia_locl.h -*- mode:C; c-file-style: "eay" -*- */ 2162911Ssimon/* ==================================================================== 3162911Ssimon * Copyright 2006 NTT (Nippon Telegraph and Telephone Corporation) . 4162911Ssimon * ALL RIGHTS RESERVED. 5162911Ssimon * 6162911Ssimon * Intellectual Property information for Camellia: 7162911Ssimon * http://info.isl.ntt.co.jp/crypt/eng/info/chiteki.html 8162911Ssimon * 9162911Ssimon * News Release for Announcement of Camellia open source: 10162911Ssimon * http://www.ntt.co.jp/news/news06e/0604/060413a.html 11162911Ssimon * 12162911Ssimon * The Camellia Code included herein is developed by 13162911Ssimon * NTT (Nippon Telegraph and Telephone Corporation), and is contributed 14162911Ssimon * to the OpenSSL project. 15162911Ssimon * 16162911Ssimon * The Camellia Code is licensed pursuant to the OpenSSL open source 17162911Ssimon * license provided below. 18162911Ssimon */ 19162911Ssimon/* ==================================================================== 20162911Ssimon * Copyright (c) 2006 The OpenSSL Project. All rights reserved. 21162911Ssimon * 22162911Ssimon * Redistribution and use in source and binary forms, with or without 23162911Ssimon * modification, are permitted provided that the following conditions 24162911Ssimon * are met: 25162911Ssimon * 26162911Ssimon * 1. Redistributions of source code must retain the above copyright 27162911Ssimon * notice, this list of conditions and the following disclaimer. 28162911Ssimon * 29162911Ssimon * 2. Redistributions in binary form must reproduce the above copyright 30162911Ssimon * notice, this list of conditions and the following disclaimer in 31162911Ssimon * the documentation and/or other materials provided with the 32162911Ssimon * distribution. 33162911Ssimon * 34162911Ssimon * 3. All advertising materials mentioning features or use of this 35162911Ssimon * software must display the following acknowledgment: 36162911Ssimon * "This product includes software developed by the OpenSSL Project 37162911Ssimon * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 38162911Ssimon * 39162911Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 40162911Ssimon * endorse or promote products derived from this software without 41162911Ssimon * prior written permission. For written permission, please contact 42162911Ssimon * openssl-core@openssl.org. 43162911Ssimon * 44162911Ssimon * 5. Products derived from this software may not be called "OpenSSL" 45162911Ssimon * nor may "OpenSSL" appear in their names without prior written 46162911Ssimon * permission of the OpenSSL Project. 47162911Ssimon * 48162911Ssimon * 6. Redistributions of any form whatsoever must retain the following 49162911Ssimon * acknowledgment: 50162911Ssimon * "This product includes software developed by the OpenSSL Project 51162911Ssimon * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 52162911Ssimon * 53162911Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 54162911Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55162911Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 56162911Ssimon * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 57162911Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 58162911Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 59162911Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 60162911Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61162911Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 62162911Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 63162911Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 64162911Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE. 65162911Ssimon * ==================================================================== 66162911Ssimon */ 67162911Ssimon 68162911Ssimon#ifndef HEADER_CAMELLIA_LOCL_H 69162911Ssimon#define HEADER_CAMELLIA_LOCL_H 70162911Ssimon 71162911Ssimon#include "openssl/e_os2.h" 72162911Ssimon#include <stdio.h> 73162911Ssimon#include <stdlib.h> 74162911Ssimon#include <string.h> 75162911Ssimon 76167612Ssimontypedef unsigned char u8; 77167612Ssimontypedef unsigned int u32; 78162911Ssimon 79162911Ssimon#ifdef __cplusplus 80162911Ssimonextern "C" { 81162911Ssimon#endif 82162911Ssimon 83162911Ssimon#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64)) 84162911Ssimon# define SWAP(x) ( _lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00 ) 85167612Ssimon# define GETU32(p) SWAP(*((u32 *)(p))) 86167612Ssimon# define PUTU32(ct, st) { *((u32 *)(ct)) = SWAP((st)); } 87162911Ssimon# define CAMELLIA_SWAP4(x) (x = ( _lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00) ) 88162911Ssimon 89162911Ssimon#else /* not windows */ 90167612Ssimon# define GETU32(pt) (((u32)(pt)[0] << 24) \ 91167612Ssimon ^ ((u32)(pt)[1] << 16) \ 92167612Ssimon ^ ((u32)(pt)[2] << 8) \ 93167612Ssimon ^ ((u32)(pt)[3])) 94162911Ssimon 95167612Ssimon# define PUTU32(ct, st) { (ct)[0] = (u8)((st) >> 24); \ 96167612Ssimon (ct)[1] = (u8)((st) >> 16); \ 97167612Ssimon (ct)[2] = (u8)((st) >> 8); \ 98167612Ssimon (ct)[3] = (u8)(st); } 99162911Ssimon 100167612Ssimon#if (defined (__GNUC__) && (defined(__x86_64__) || defined(__x86_64))) 101162911Ssimon#define CAMELLIA_SWAP4(x) \ 102162911Ssimon do{\ 103162911Ssimon asm("bswap %1" : "+r" (x));\ 104162911Ssimon }while(0) 105167612Ssimon#else 106162911Ssimon#define CAMELLIA_SWAP4(x) \ 107162911Ssimon do{\ 108167612Ssimon x = ((u32)x << 16) + ((u32)x >> 16);\ 109167612Ssimon x = (((u32)x & 0xff00ff) << 8) + (((u32)x >> 8) & 0xff00ff);\ 110162911Ssimon } while(0) 111162911Ssimon#endif 112167612Ssimon#endif 113162911Ssimon 114162911Ssimon#define COPY4WORD(dst, src) \ 115162911Ssimon do \ 116162911Ssimon { \ 117162911Ssimon (dst)[0]=(src)[0]; \ 118162911Ssimon (dst)[1]=(src)[1]; \ 119162911Ssimon (dst)[2]=(src)[2]; \ 120162911Ssimon (dst)[3]=(src)[3]; \ 121162911Ssimon }while(0) 122162911Ssimon 123162911Ssimon#define SWAP4WORD(word) \ 124162911Ssimon do \ 125162911Ssimon { \ 126162911Ssimon CAMELLIA_SWAP4((word)[0]); \ 127162911Ssimon CAMELLIA_SWAP4((word)[1]); \ 128162911Ssimon CAMELLIA_SWAP4((word)[2]); \ 129162911Ssimon CAMELLIA_SWAP4((word)[3]); \ 130162911Ssimon }while(0) 131162911Ssimon 132162911Ssimon#define XOR4WORD(a, b)/* a = a ^ b */ \ 133162911Ssimon do \ 134162911Ssimon { \ 135162911Ssimon (a)[0]^=(b)[0]; \ 136162911Ssimon (a)[1]^=(b)[1]; \ 137162911Ssimon (a)[2]^=(b)[2]; \ 138162911Ssimon (a)[3]^=(b)[3]; \ 139162911Ssimon }while(0) 140162911Ssimon 141162911Ssimon#define XOR4WORD2(a, b, c)/* a = b ^ c */ \ 142162911Ssimon do \ 143162911Ssimon { \ 144162911Ssimon (a)[0]=(b)[0]^(c)[0]; \ 145162911Ssimon (a)[1]=(b)[1]^(c)[1]; \ 146162911Ssimon (a)[2]=(b)[2]^(c)[2]; \ 147162911Ssimon (a)[3]=(b)[3]^(c)[3]; \ 148162911Ssimon }while(0) 149162911Ssimon 150162911Ssimon 151167612Ssimonvoid camellia_setup128(const u8 *key, u32 *subkey); 152167612Ssimonvoid camellia_setup192(const u8 *key, u32 *subkey); 153167612Ssimonvoid camellia_setup256(const u8 *key, u32 *subkey); 154162911Ssimon 155167612Ssimonvoid camellia_encrypt128(const u32 *subkey, u32 *io); 156167612Ssimonvoid camellia_decrypt128(const u32 *subkey, u32 *io); 157167612Ssimonvoid camellia_encrypt256(const u32 *subkey, u32 *io); 158167612Ssimonvoid camellia_decrypt256(const u32 *subkey, u32 *io); 159162911Ssimon 160162911Ssimon#ifdef __cplusplus 161162911Ssimon} 162162911Ssimon#endif 163162911Ssimon 164162911Ssimon#endif /* #ifndef HEADER_CAMELLIA_LOCL_H */ 165162911Ssimon 166