modes_lcl.h 4.7 KB

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