cmll_locl.h revision 167613
1290001Sglebius/* crypto/camellia/camellia_locl.h -*- mode:C; c-file-style: "eay" -*- */
2132451Sroberto/* ====================================================================
354359Sroberto * Copyright 2006 NTT (Nippon Telegraph and Telephone Corporation) .
4290001Sglebius * ALL RIGHTS RESERVED.
5290001Sglebius *
654359Sroberto * Intellectual Property information for Camellia:
754359Sroberto *     http://info.isl.ntt.co.jp/crypt/eng/info/chiteki.html
854359Sroberto *
954359Sroberto * News Release for Announcement of Camellia open source:
1054359Sroberto *     http://www.ntt.co.jp/news/news06e/0604/060413a.html
1154359Sroberto *
1254359Sroberto * The Camellia Code included herein is developed by
1354359Sroberto * NTT (Nippon Telegraph and Telephone Corporation), and is contributed
1454359Sroberto * to the OpenSSL project.
15106163Sroberto *
16106163Sroberto * The Camellia Code is licensed pursuant to the OpenSSL open source
17182007Sroberto * license provided below.
18182007Sroberto */
19182007Sroberto/* ====================================================================
20182007Sroberto * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
21182007Sroberto *
22290001Sglebius * Redistribution and use in source and binary forms, with or without
23290001Sglebius * modification, are permitted provided that the following conditions
2454359Sroberto * are met:
25290001Sglebius *
26290001Sglebius * 1. Redistributions of source code must retain the above copyright
27290001Sglebius *    notice, this list of conditions and the following disclaimer.
28290001Sglebius *
29290001Sglebius * 2. Redistributions in binary form must reproduce the above copyright
30290001Sglebius *    notice, this list of conditions and the following disclaimer in
31290001Sglebius *    the documentation and/or other materials provided with the
32290001Sglebius *    distribution.
33290001Sglebius *
34290001Sglebius * 3. All advertising materials mentioning features or use of this
35290001Sglebius *    software must display the following acknowledgment:
36290001Sglebius *    "This product includes software developed by the OpenSSL Project
37290001Sglebius *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
38290001Sglebius *
39290001Sglebius * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
40290001Sglebius *    endorse or promote products derived from this software without
41290001Sglebius *    prior written permission. For written permission, please contact
42290001Sglebius *    openssl-core@openssl.org.
43290001Sglebius *
44290001Sglebius * 5. Products derived from this software may not be called "OpenSSL"
45290001Sglebius *    nor may "OpenSSL" appear in their names without prior written
46290001Sglebius *    permission of the OpenSSL Project.
47290001Sglebius *
48290001Sglebius * 6. Redistributions of any form whatsoever must retain the following
49290001Sglebius *    acknowledgment:
50290001Sglebius *    "This product includes software developed by the OpenSSL Project
51290001Sglebius *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
52290001Sglebius *
53290001Sglebius * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
54290001Sglebius * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55290001Sglebius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56290001Sglebius * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
57290001Sglebius * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
58290001Sglebius * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
59290001Sglebius * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
60290001Sglebius * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61290001Sglebius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
62290001Sglebius * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63290001Sglebius * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
64290001Sglebius * OF THE POSSIBILITY OF SUCH DAMAGE.
65290001Sglebius * ====================================================================
66290001Sglebius */
67290001Sglebius
68290001Sglebius#ifndef HEADER_CAMELLIA_LOCL_H
69290001Sglebius#define HEADER_CAMELLIA_LOCL_H
70290001Sglebius
71290001Sglebius#include "openssl/e_os2.h"
72290001Sglebius#include <stdio.h>
73290001Sglebius#include <stdlib.h>
74290001Sglebius#include <string.h>
75290001Sglebius
76290001Sglebiustypedef unsigned char u8;
77290001Sglebiustypedef unsigned int u32;
78290001Sglebius
79290001Sglebius#ifdef __cplusplus
8054359Srobertoextern "C" {
81200576Sroberto#endif
8254359Sroberto
83200576Sroberto#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64))
84132451Sroberto# define SWAP(x) ( _lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00 )
85132451Sroberto# define GETU32(p) SWAP(*((u32 *)(p)))
86132451Sroberto# define PUTU32(ct, st) { *((u32 *)(ct)) = SWAP((st)); }
87132451Sroberto# define CAMELLIA_SWAP4(x) (x = ( _lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00) )
8882498Sroberto
89132451Sroberto#else /* not windows */
9054359Sroberto# define GETU32(pt) (((u32)(pt)[0] << 24) \
9154359Sroberto	^ ((u32)(pt)[1] << 16) \
9254359Sroberto	^ ((u32)(pt)[2] <<  8) \
9354359Sroberto	^ ((u32)(pt)[3]))
9454359Sroberto
9554359Sroberto# define PUTU32(ct, st) { (ct)[0] = (u8)((st) >> 24); \
9654359Sroberto	(ct)[1] = (u8)((st) >> 16); \
9754359Sroberto	(ct)[2] = (u8)((st) >>  8); \
98290001Sglebius	(ct)[3] = (u8)(st); }
99290001Sglebius
100182007Sroberto#if (defined (__GNUC__) && (defined(__x86_64__) || defined(__x86_64)))
101182007Sroberto#define CAMELLIA_SWAP4(x) \
102290001Sglebius  do{\
103290001Sglebius    asm("bswap %1" : "+r" (x));\
104290001Sglebius  }while(0)
105290001Sglebius#else
106290001Sglebius#define CAMELLIA_SWAP4(x) \
107290001Sglebius   do{\
108290001Sglebius     x = ((u32)x << 16) + ((u32)x >> 16);\
109290001Sglebius     x = (((u32)x & 0xff00ff) << 8) + (((u32)x >> 8) & 0xff00ff);\
110290001Sglebius   } while(0)
111290001Sglebius#endif
112290001Sglebius#endif
113290001Sglebius
114290001Sglebius#define COPY4WORD(dst, src)	 \
115290001Sglebius	     do			 \
116290001Sglebius		     {		 \
117290001Sglebius		     (dst)[0]=(src)[0];		\
118290001Sglebius		     (dst)[1]=(src)[1];		\
119290001Sglebius		     (dst)[2]=(src)[2];		\
120290001Sglebius		     (dst)[3]=(src)[3];		\
121290001Sglebius		     }while(0)
122290001Sglebius
123290001Sglebius#define SWAP4WORD(word)				\
124290001Sglebius   do						\
125290001Sglebius	   {					\
126290001Sglebius	   CAMELLIA_SWAP4((word)[0]);			\
127290001Sglebius	   CAMELLIA_SWAP4((word)[1]);			\
128290001Sglebius	   CAMELLIA_SWAP4((word)[2]);			\
129290001Sglebius	   CAMELLIA_SWAP4((word)[3]);			\
130290001Sglebius	   }while(0)
131290001Sglebius
132290001Sglebius#define XOR4WORD(a, b)/* a = a ^ b */		\
133290001Sglebius   do						\
134290001Sglebius	{					\
135290001Sglebius	(a)[0]^=(b)[0];				\
136290001Sglebius	(a)[1]^=(b)[1];				\
137290001Sglebius	(a)[2]^=(b)[2];				\
138290001Sglebius	(a)[3]^=(b)[3];				\
139290001Sglebius	}while(0)
140182007Sroberto
141182007Sroberto#define XOR4WORD2(a, b, c)/* a = b ^ c */	\
142290001Sglebius   do						\
143290001Sglebius	{					\
144182007Sroberto	(a)[0]=(b)[0]^(c)[0];			\
145182007Sroberto	(a)[1]=(b)[1]^(c)[1];				\
146290001Sglebius	(a)[2]=(b)[2]^(c)[2];				\
147200576Sroberto	(a)[3]=(b)[3]^(c)[3];				\
148182007Sroberto	}while(0)
149182007Sroberto
150290001Sglebius
151290001Sglebiusvoid camellia_setup128(const u8 *key, u32 *subkey);
152290001Sglebiusvoid camellia_setup192(const u8 *key, u32 *subkey);
153290001Sglebiusvoid camellia_setup256(const u8 *key, u32 *subkey);
154182007Sroberto
155182007Srobertovoid camellia_encrypt128(const u32 *subkey, u32 *io);
156290001Sglebiusvoid camellia_decrypt128(const u32 *subkey, u32 *io);
157290001Sglebiusvoid camellia_encrypt256(const u32 *subkey, u32 *io);
158290001Sglebiusvoid camellia_decrypt256(const u32 *subkey, u32 *io);
159290001Sglebius
160290001Sglebius#ifdef __cplusplus
161290001Sglebius}
162290001Sglebius#endif
163290001Sglebius
164290001Sglebius#endif /* #ifndef HEADER_CAMELLIA_LOCL_H */
165290001Sglebius
166290001Sglebius