1 /*
   2  * Copyright (c) 2007 KISA(Korea Information Security Agency). All rights reserved.  
   3  *
   4  * Redistribution and use in source and binary forms, with or without
   5  * modification, are permitted provided that the following conditions
   6  * are met:
   7  * 1. Redistributions of source code must retain the above copyright
   8  *    notice, this list of conditions and the following disclaimer.
   9  * 2. Neither the name of author nor the names of its contributors may
  10  *    be used to endorse or promote products derived from this software
  11  *    without specific prior written permission.
  12  *
  13  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
  17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23  * SUCH DAMAGE.
  24  *
  25  */
  26 #ifndef HEADER_SEED_LOCL_H
  27 #define HEADER_SEED_LOCL_H
  28 
  29 #include "openssl/e_os2.h"
  30 #include <openssl/seed.h>
  31 
  32 
  33 #ifdef SEED_LONG /* need 32-bit type */
  34 typedef unsigned long seed_word;
  35 #else
  36 typedef unsigned int seed_word;
  37 #endif
  38 
  39 
  40 #ifdef  __cplusplus
  41 extern "C" {
  42 #endif
  43 
  44 #define G_FUNC(v)       \
  45         SS[0][(unsigned char)      (v) & 0xff] ^ SS[1][(unsigned char) ((v)>>8) & 0xff] ^ \
  46         SS[2][(unsigned char)((v)>>16) & 0xff] ^ SS[3][(unsigned char)((v)>>24) & 0xff]
  47 
  48 #define char2word(c, i)  \
  49         (i) = ((((seed_word)(c)[0]) << 24) | (((seed_word)(c)[1]) << 16) | (((seed_word)(c)[2]) << 8) | ((seed_word)(c)[3]))
  50 
  51 #define word2char(l, c)  \
  52         *((c)+0) = (unsigned char)((l)>>24) & 0xff; \
  53         *((c)+1) = (unsigned char)((l)>>16) & 0xff; \
  54         *((c)+2) = (unsigned char)((l)>> 8) & 0xff; \
  55         *((c)+3) = (unsigned char)((l))     & 0xff
  56 
  57 #define KEYSCHEDULE_UPDATE0(T0, T1, X1, X2, X3, X4, KC)  \
  58         (T0) = (X3);                                     \
  59         (X3) = (((X3)<<8) ^ ((X4)>>24)) & 0xffffffff;    \
  60         (X4) = (((X4)<<8) ^ ((T0)>>24)) & 0xffffffff;    \
  61         (T0) = ((X1) + (X3) - (KC))     & 0xffffffff;    \
  62         (T1) = ((X2) + (KC) - (X4))     & 0xffffffff
  63 
  64 #define KEYSCHEDULE_UPDATE1(T0, T1, X1, X2, X3, X4, KC)  \
  65         (T0) = (X1);                                     \
  66         (X1) = (((X1)>>8) ^ ((X2)<<24)) & 0xffffffff;    \
  67         (X2) = (((X2)>>8) ^ ((T0)<<24)) & 0xffffffff;    \
  68         (T0) = ((X1) + (X3) - (KC))     & 0xffffffff;     \
  69         (T1) = ((X2) + (KC) - (X4))     & 0xffffffff
  70 
  71 #define KEYUPDATE_TEMP(T0, T1, K)   \
  72         (K)[0] = G_FUNC((T0));      \
  73         (K)[1] = G_FUNC((T1))
  74 
  75 #define XOR_SEEDBLOCK(DST, SRC)      \
  76         ((DST))[0] ^= ((SRC))[0];    \
  77         ((DST))[1] ^= ((SRC))[1];    \
  78         ((DST))[2] ^= ((SRC))[2];    \
  79         ((DST))[3] ^= ((SRC))[3]
  80 
  81 #define MOV_SEEDBLOCK(DST, SRC)      \
  82         ((DST))[0] = ((SRC))[0];     \
  83         ((DST))[1] = ((SRC))[1];     \
  84         ((DST))[2] = ((SRC))[2];     \
  85         ((DST))[3] = ((SRC))[3]
  86 
  87 # define CHAR2WORD(C, I)              \
  88         char2word((C),    (I)[0]);    \
  89         char2word((C+4),  (I)[1]);    \
  90         char2word((C+8),  (I)[2]);    \
  91         char2word((C+12), (I)[3])
  92 
  93 # define WORD2CHAR(I, C)              \
  94         word2char((I)[0], (C));       \
  95         word2char((I)[1], (C+4));     \
  96         word2char((I)[2], (C+8));     \
  97         word2char((I)[3], (C+12))
  98 
  99 # define E_SEED(T0, T1, X1, X2, X3, X4, rbase)   \
 100         (T0) = (X3) ^ (ks->data)[(rbase)];       \
 101         (T1) = (X4) ^ (ks->data)[(rbase)+1];     \
 102         (T1) ^= (T0);                            \
 103         (T1) = G_FUNC((T1));                     \
 104         (T0) = ((T0) + (T1)) & 0xffffffff;       \
 105         (T0) = G_FUNC((T0));                     \
 106         (T1) = ((T1) + (T0)) & 0xffffffff;       \
 107         (T1) = G_FUNC((T1));                     \
 108         (T0) = ((T0) + (T1)) & 0xffffffff;       \
 109         (X1) ^= (T0);                            \
 110         (X2) ^= (T1)
 111 
 112 #ifdef  __cplusplus
 113 }
 114 #endif
 115 
 116 #endif /* HEADER_SEED_LOCL_H */