modes_lcl.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* ====================================================================
  2. * Copyright (c) 2010 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use is governed by OpenSSL license.
  5. * ====================================================================
  6. */
  7. #include <openssl/modes.h>
  8. #if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__)
  9. typedef __int64 i64;
  10. typedef unsigned __int64 u64;
  11. # define U64(C) C##UI64
  12. #elif defined(__arch64__)
  13. typedef long i64;
  14. typedef unsigned long u64;
  15. # define U64(C) C##UL
  16. #else
  17. typedef long long i64;
  18. typedef unsigned long long u64;
  19. # define U64(C) C##ULL
  20. #endif
  21. typedef unsigned int u32;
  22. typedef unsigned char u8;
  23. #define STRICT_ALIGNMENT 1
  24. #if defined(__i386) || defined(__i386__) || \
  25. defined(__x86_64) || defined(__x86_64__) || \
  26. defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64) || \
  27. defined(__s390__) || defined(__s390x__)
  28. # undef STRICT_ALIGNMENT
  29. #endif
  30. #if !defined(PEDANTIC) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
  31. # if defined(__GNUC__) && __GNUC__>=2
  32. # if defined(__x86_64) || defined(__x86_64__)
  33. # define BSWAP8(x) ({ u64 ret=(x); \
  34. asm ("bswapq %0" \
  35. : "+r"(ret)); ret; })
  36. # define BSWAP4(x) ({ u32 ret=(x); \
  37. asm ("bswapl %0" \
  38. : "+r"(ret)); ret; })
  39. # elif (defined(__i386) || defined(__i386__)) && !defined(I386_ONLY)
  40. # define BSWAP8(x) ({ u32 lo=(u64)(x)>>32,hi=(x); \
  41. asm ("bswapl %0; bswapl %1" \
  42. : "+r"(hi),"+r"(lo)); \
  43. (u64)hi<<32|lo; })
  44. # define BSWAP4(x) ({ u32 ret=(x); \
  45. asm ("bswapl %0" \
  46. : "+r"(ret)); ret; })
  47. # elif (defined(__arm__) || defined(__arm)) && !defined(STRICT_ALIGNMENT)
  48. # define BSWAP8(x) ({ u32 lo=(u64)(x)>>32,hi=(x); \
  49. asm ("rev %0,%0; rev %1,%1" \
  50. : "+r"(hi),"+r"(lo)); \
  51. (u64)hi<<32|lo; })
  52. # define BSWAP4(x) ({ u32 ret; \
  53. asm ("rev %0,%1" \
  54. : "=r"(ret) : "r"((u32)(x))); \
  55. ret; })
  56. # endif
  57. # elif defined(_MSC_VER)
  58. # if _MSC_VER>=1300
  59. # pragma intrinsic(_byteswap_uint64,_byteswap_ulong)
  60. # define BSWAP8(x) _byteswap_uint64((u64)(x))
  61. # define BSWAP4(x) _byteswap_ulong((u32)(x))
  62. # elif defined(_M_IX86)
  63. __inline u32 _bswap4(u32 val)
  64. {
  65. _asm mov eax, val _asm bswap eax}
  66. # define BSWAP4(x) _bswap4(x)
  67. # endif
  68. # endif
  69. #endif
  70. #if defined(BSWAP4) && !defined(STRICT_ALIGNMENT)
  71. # define GETU32(p) BSWAP4(*(const u32 *)(p))
  72. # define PUTU32(p,v) *(u32 *)(p) = BSWAP4(v)
  73. #else
  74. # define GETU32(p) ((u32)(p)[0]<<24|(u32)(p)[1]<<16|(u32)(p)[2]<<8|(u32)(p)[3])
  75. # define PUTU32(p,v) ((p)[0]=(u8)((v)>>24),(p)[1]=(u8)((v)>>16),(p)[2]=(u8)((v)>>8),(p)[3]=(u8)(v))
  76. #endif
  77. /*- GCM definitions */ typedef struct {
  78. u64 hi, lo;
  79. } u128;
  80. #ifdef TABLE_BITS
  81. # undef TABLE_BITS
  82. #endif
  83. /*
  84. * Even though permitted values for TABLE_BITS are 8, 4 and 1, it should
  85. * never be set to 8 [or 1]. For further information see gcm128.c.
  86. */
  87. #define TABLE_BITS 4
  88. struct gcm128_context {
  89. /* Following 6 names follow names in GCM specification */
  90. union {
  91. u64 u[2];
  92. u32 d[4];
  93. u8 c[16];
  94. size_t t[16 / sizeof(size_t)];
  95. } Yi, EKi, EK0, len, Xi, H;
  96. /*
  97. * Relative position of Xi, H and pre-computed Htable is used in some
  98. * assembler modules, i.e. don't change the order!
  99. */
  100. #if TABLE_BITS==8
  101. u128 Htable[256];
  102. #else
  103. u128 Htable[16];
  104. void (*gmult) (u64 Xi[2], const u128 Htable[16]);
  105. void (*ghash) (u64 Xi[2], const u128 Htable[16], const u8 *inp,
  106. size_t len);
  107. #endif
  108. unsigned int mres, ares;
  109. block128_f block;
  110. void *key;
  111. };
  112. struct xts128_context {
  113. void *key1, *key2;
  114. block128_f block1, block2;
  115. };
  116. struct ccm128_context {
  117. union {
  118. u64 u[2];
  119. u8 c[16];
  120. } nonce, cmac;
  121. u64 blocks;
  122. block128_f block;
  123. void *key;
  124. };