seed_locl.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. # include "openssl/e_os2.h"
  29. # include <openssl/seed.h>
  30. # ifdef SEED_LONG /* need 32-bit type */
  31. typedef unsigned long seed_word;
  32. # else
  33. typedef unsigned int seed_word;
  34. # endif
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. # define G_FUNC(v) \
  39. SS[0][(unsigned char) (v) & 0xff] ^ SS[1][(unsigned char) ((v)>>8) & 0xff] ^ \
  40. SS[2][(unsigned char)((v)>>16) & 0xff] ^ SS[3][(unsigned char)((v)>>24) & 0xff]
  41. # define char2word(c, i) \
  42. (i) = ((((seed_word)(c)[0]) << 24) | (((seed_word)(c)[1]) << 16) | (((seed_word)(c)[2]) << 8) | ((seed_word)(c)[3]))
  43. # define word2char(l, c) \
  44. *((c)+0) = (unsigned char)((l)>>24) & 0xff; \
  45. *((c)+1) = (unsigned char)((l)>>16) & 0xff; \
  46. *((c)+2) = (unsigned char)((l)>> 8) & 0xff; \
  47. *((c)+3) = (unsigned char)((l)) & 0xff
  48. # define KEYSCHEDULE_UPDATE0(T0, T1, X1, X2, X3, X4, KC) \
  49. (T0) = (X3); \
  50. (X3) = (((X3)<<8) ^ ((X4)>>24)) & 0xffffffff; \
  51. (X4) = (((X4)<<8) ^ ((T0)>>24)) & 0xffffffff; \
  52. (T0) = ((X1) + (X3) - (KC)) & 0xffffffff; \
  53. (T1) = ((X2) + (KC) - (X4)) & 0xffffffff
  54. # define KEYSCHEDULE_UPDATE1(T0, T1, X1, X2, X3, X4, KC) \
  55. (T0) = (X1); \
  56. (X1) = (((X1)>>8) ^ ((X2)<<24)) & 0xffffffff; \
  57. (X2) = (((X2)>>8) ^ ((T0)<<24)) & 0xffffffff; \
  58. (T0) = ((X1) + (X3) - (KC)) & 0xffffffff; \
  59. (T1) = ((X2) + (KC) - (X4)) & 0xffffffff
  60. # define KEYUPDATE_TEMP(T0, T1, K) \
  61. (K)[0] = G_FUNC((T0)); \
  62. (K)[1] = G_FUNC((T1))
  63. # define XOR_SEEDBLOCK(DST, SRC) \
  64. ((DST))[0] ^= ((SRC))[0]; \
  65. ((DST))[1] ^= ((SRC))[1]; \
  66. ((DST))[2] ^= ((SRC))[2]; \
  67. ((DST))[3] ^= ((SRC))[3]
  68. # define MOV_SEEDBLOCK(DST, SRC) \
  69. ((DST))[0] = ((SRC))[0]; \
  70. ((DST))[1] = ((SRC))[1]; \
  71. ((DST))[2] = ((SRC))[2]; \
  72. ((DST))[3] = ((SRC))[3]
  73. # define CHAR2WORD(C, I) \
  74. char2word((C), (I)[0]); \
  75. char2word((C+4), (I)[1]); \
  76. char2word((C+8), (I)[2]); \
  77. char2word((C+12), (I)[3])
  78. # define WORD2CHAR(I, C) \
  79. word2char((I)[0], (C)); \
  80. word2char((I)[1], (C+4)); \
  81. word2char((I)[2], (C+8)); \
  82. word2char((I)[3], (C+12))
  83. # define E_SEED(T0, T1, X1, X2, X3, X4, rbase) \
  84. (T0) = (X3) ^ (ks->data)[(rbase)]; \
  85. (T1) = (X4) ^ (ks->data)[(rbase)+1]; \
  86. (T1) ^= (T0); \
  87. (T1) = G_FUNC((T1)); \
  88. (T0) = ((T0) + (T1)) & 0xffffffff; \
  89. (T0) = G_FUNC((T0)); \
  90. (T1) = ((T1) + (T0)) & 0xffffffff; \
  91. (T1) = G_FUNC((T1)); \
  92. (T0) = ((T0) + (T1)) & 0xffffffff; \
  93. (X1) ^= (T0); \
  94. (X2) ^= (T1)
  95. #ifdef __cplusplus
  96. }
  97. #endif
  98. #endif /* HEADER_SEED_LOCL_H */