1162911Ssimon/* crypto/camellia/camellia_locl.h -*- mode:C; c-file-style: "eay" -*- */
2162911Ssimon/* ====================================================================
3296465Sdelphij * 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
27296465Sdelphij *    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
69296465Sdelphij# define HEADER_CAMELLIA_LOCL_H
70162911Ssimon
71296465Sdelphij# include "openssl/e_os2.h"
72296465Sdelphij# include <stdio.h>
73296465Sdelphij# include <stdlib.h>
74296465Sdelphij# include <string.h>
75162911Ssimon
76167612Ssimontypedef unsigned char u8;
77167612Ssimontypedef unsigned int u32;
78162911Ssimon
79162911Ssimon#ifdef __cplusplus
80162911Ssimonextern "C" {
81162911Ssimon#endif
82162911Ssimon
83296465Sdelphij# if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64))
84296465Sdelphij#  define SWAP(x) ( _lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00 )
85296465Sdelphij#  define GETU32(p) SWAP(*((u32 *)(p)))
86296465Sdelphij#  define PUTU32(ct, st) { *((u32 *)(ct)) = SWAP((st)); }
87296465Sdelphij#  define CAMELLIA_SWAP4(x) (x = ( _lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00) )
88162911Ssimon
89296465Sdelphij# else                          /* not windows */
90296465Sdelphij#  define GETU32(pt) (((u32)(pt)[0] << 24) \
91296465Sdelphij        ^ ((u32)(pt)[1] << 16) \
92296465Sdelphij        ^ ((u32)(pt)[2] <<  8) \
93296465Sdelphij        ^ ((u32)(pt)[3]))
94162911Ssimon
95296465Sdelphij#  define PUTU32(ct, st) { (ct)[0] = (u8)((st) >> 24); \
96296465Sdelphij        (ct)[1] = (u8)((st) >> 16); \
97296465Sdelphij        (ct)[2] = (u8)((st) >>  8); \
98296465Sdelphij        (ct)[3] = (u8)(st); }
99162911Ssimon
100296465Sdelphij#  if (defined (__GNUC__) && (defined(__x86_64__) || defined(__x86_64)))
101296465Sdelphij#   define CAMELLIA_SWAP4(x) \
102162911Ssimon  do{\
103162911Ssimon    asm("bswap %1" : "+r" (x));\
104162911Ssimon  }while(0)
105296465Sdelphij#  else
106296465Sdelphij#   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)
111296465Sdelphij#  endif
112296465Sdelphij# endif
113162911Ssimon
114296465Sdelphij# define COPY4WORD(dst, src)      \
115296465Sdelphij             do                  \
116296465Sdelphij                     {           \
117296465Sdelphij                     (dst)[0]=(src)[0];         \
118296465Sdelphij                     (dst)[1]=(src)[1];         \
119296465Sdelphij                     (dst)[2]=(src)[2];         \
120296465Sdelphij                     (dst)[3]=(src)[3];         \
121296465Sdelphij                     }while(0)
122162911Ssimon
123296465Sdelphij# define SWAP4WORD(word)                         \
124296465Sdelphij   do                                           \
125296465Sdelphij           {                                    \
126296465Sdelphij           CAMELLIA_SWAP4((word)[0]);                   \
127296465Sdelphij           CAMELLIA_SWAP4((word)[1]);                   \
128296465Sdelphij           CAMELLIA_SWAP4((word)[2]);                   \
129296465Sdelphij           CAMELLIA_SWAP4((word)[3]);                   \
130296465Sdelphij           }while(0)
131162911Ssimon
132296465Sdelphij# define XOR4WORD(a, b)/* a = a ^ b */           \
133296465Sdelphij   do                                           \
134296465Sdelphij        {                                       \
135296465Sdelphij        (a)[0]^=(b)[0];                         \
136296465Sdelphij        (a)[1]^=(b)[1];                         \
137296465Sdelphij        (a)[2]^=(b)[2];                         \
138296465Sdelphij        (a)[3]^=(b)[3];                         \
139296465Sdelphij        }while(0)
140162911Ssimon
141296465Sdelphij# define XOR4WORD2(a, b, c)/* a = b ^ c */       \
142296465Sdelphij   do                                           \
143296465Sdelphij        {                                       \
144296465Sdelphij        (a)[0]=(b)[0]^(c)[0];                   \
145296465Sdelphij        (a)[1]=(b)[1]^(c)[1];                           \
146296465Sdelphij        (a)[2]=(b)[2]^(c)[2];                           \
147296465Sdelphij        (a)[3]=(b)[3]^(c)[3];                           \
148296465Sdelphij        }while(0)
149162911Ssimon
150167612Ssimonvoid camellia_setup128(const u8 *key, u32 *subkey);
151167612Ssimonvoid camellia_setup192(const u8 *key, u32 *subkey);
152167612Ssimonvoid camellia_setup256(const u8 *key, u32 *subkey);
153162911Ssimon
154167612Ssimonvoid camellia_encrypt128(const u32 *subkey, u32 *io);
155167612Ssimonvoid camellia_decrypt128(const u32 *subkey, u32 *io);
156167612Ssimonvoid camellia_encrypt256(const u32 *subkey, u32 *io);
157167612Ssimonvoid camellia_decrypt256(const u32 *subkey, u32 *io);
158162911Ssimon
159162911Ssimon#ifdef __cplusplus
160162911Ssimon}
161162911Ssimon#endif
162162911Ssimon
163296465Sdelphij#endif                          /* #ifndef HEADER_CAMELLIA_LOCL_H */
164