2
0

modes_lcl.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. ( (defined(__arm__) || defined(__arm)) && \
  29. (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || \
  30. defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__)) )
  31. # undef STRICT_ALIGNMENT
  32. #endif
  33. #if !defined(PEDANTIC) && !defined(OPENSSL_NO_ASM) && !defined(OPNESSL_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__))
  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(__arm__) || defined(__arm)) && !defined(STRICT_ALIGNMENT)
  51. # define BSWAP8(x) ({ u32 lo=(u64)(x)>>32,hi=(x); \
  52. asm ("rev %0,%0; rev %1,%1" \
  53. : "+r"(hi),"+r"(lo)); \
  54. (u64)hi<<32|lo; })
  55. # define BSWAP4(x) ({ u32 ret; \
  56. asm ("rev %0,%1" \
  57. : "=r"(ret) : "r"((u32)(x))); \
  58. ret; })
  59. # endif
  60. #elif defined(_MSC_VER)
  61. # if _MSC_VER>=1300
  62. # pragma intrinsic(_byteswap_uint64,_byteswap_ulong)
  63. # define BSWAP8(x) _byteswap_uint64((u64)(x))
  64. # define BSWAP4(x) _byteswap_ulong((u32)(x))
  65. # elif defined(_M_IX86)
  66. __inline u32 _bswap4(u32 val) {
  67. _asm mov eax,val
  68. _asm bswap eax
  69. }
  70. # define BSWAP4(x) _bswap4(x)
  71. # endif
  72. #endif
  73. #endif
  74. #if defined(BSWAP4) && !defined(STRICT_ALIGNMENT)
  75. #define GETU32(p) BSWAP4(*(const u32 *)(p))
  76. #define PUTU32(p,v) *(u32 *)(p) = BSWAP4(v)
  77. #else
  78. #define GETU32(p) ((u32)(p)[0]<<24|(u32)(p)[1]<<16|(u32)(p)[2]<<8|(u32)(p)[3])
  79. #define PUTU32(p,v) ((p)[0]=(u8)((v)>>24),(p)[1]=(u8)((v)>>16),(p)[2]=(u8)((v)>>8),(p)[3]=(u8)(v))
  80. #endif
  81. /* GCM definitions */
  82. typedef struct { u64 hi,lo; } u128;
  83. #ifdef TABLE_BITS
  84. #undef TABLE_BITS
  85. #endif
  86. /*
  87. * Even though permitted values for TABLE_BITS are 8, 4 and 1, it should
  88. * never be set to 8 [or 1]. For further information see gcm128.c.
  89. */
  90. #define TABLE_BITS 4
  91. struct gcm128_context {
  92. /* Following 6 names follow names in GCM specification */
  93. union { u64 u[2]; u32 d[4]; u8 c[16]; } Yi,EKi,EK0,len,
  94. Xi,H;
  95. /* Relative position of Xi, H and pre-computed Htable is used
  96. * in some assembler modules, i.e. don't change the order! */
  97. #if TABLE_BITS==8
  98. u128 Htable[256];
  99. #else
  100. u128 Htable[16];
  101. void (*gmult)(u64 Xi[2],const u128 Htable[16]);
  102. void (*ghash)(u64 Xi[2],const u128 Htable[16],const u8 *inp,size_t len);
  103. #endif
  104. unsigned int mres, ares;
  105. block128_f block;
  106. void *key;
  107. };
  108. struct xts128_context {
  109. void *key1, *key2;
  110. block128_f block1,block2;
  111. };
  112. struct ccm128_context {
  113. union { u64 u[2]; u8 c[16]; } nonce, cmac;
  114. u64 blocks;
  115. block128_f block;
  116. void *key;
  117. };