2
0

md32_common.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*-
  10. * This is a generic 32 bit "collector" for message digest algorithms.
  11. * Whenever needed it collects input character stream into chunks of
  12. * 32 bit values and invokes a block function that performs actual hash
  13. * calculations.
  14. *
  15. * Porting guide.
  16. *
  17. * Obligatory macros:
  18. *
  19. * DATA_ORDER_IS_BIG_ENDIAN or DATA_ORDER_IS_LITTLE_ENDIAN
  20. * this macro defines byte order of input stream.
  21. * HASH_CBLOCK
  22. * size of a unit chunk HASH_BLOCK operates on.
  23. * HASH_LONG
  24. * has to be at least 32 bit wide.
  25. * HASH_CTX
  26. * context structure that at least contains following
  27. * members:
  28. * typedef struct {
  29. * ...
  30. * HASH_LONG Nl,Nh;
  31. * either {
  32. * HASH_LONG data[HASH_LBLOCK];
  33. * unsigned char data[HASH_CBLOCK];
  34. * };
  35. * unsigned int num;
  36. * ...
  37. * } HASH_CTX;
  38. * data[] vector is expected to be zeroed upon first call to
  39. * HASH_UPDATE.
  40. * HASH_UPDATE
  41. * name of "Update" function, implemented here.
  42. * HASH_TRANSFORM
  43. * name of "Transform" function, implemented here.
  44. * HASH_FINAL
  45. * name of "Final" function, implemented here.
  46. * HASH_BLOCK_DATA_ORDER
  47. * name of "block" function capable of treating *unaligned* input
  48. * message in original (data) byte order, implemented externally.
  49. * HASH_MAKE_STRING
  50. * macro converting context variables to an ASCII hash string.
  51. *
  52. * MD5 example:
  53. *
  54. * #define DATA_ORDER_IS_LITTLE_ENDIAN
  55. *
  56. * #define HASH_LONG MD5_LONG
  57. * #define HASH_CTX MD5_CTX
  58. * #define HASH_CBLOCK MD5_CBLOCK
  59. * #define HASH_UPDATE MD5_Update
  60. * #define HASH_TRANSFORM MD5_Transform
  61. * #define HASH_FINAL MD5_Final
  62. * #define HASH_BLOCK_DATA_ORDER md5_block_data_order
  63. */
  64. #include <openssl/crypto.h>
  65. #if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN)
  66. # error "DATA_ORDER must be defined!"
  67. #endif
  68. #ifndef HASH_CBLOCK
  69. # error "HASH_CBLOCK must be defined!"
  70. #endif
  71. #ifndef HASH_LONG
  72. # error "HASH_LONG must be defined!"
  73. #endif
  74. #ifndef HASH_CTX
  75. # error "HASH_CTX must be defined!"
  76. #endif
  77. #ifndef HASH_UPDATE
  78. # error "HASH_UPDATE must be defined!"
  79. #endif
  80. #ifndef HASH_TRANSFORM
  81. # error "HASH_TRANSFORM must be defined!"
  82. #endif
  83. #ifndef HASH_FINAL
  84. # error "HASH_FINAL must be defined!"
  85. #endif
  86. #ifndef HASH_BLOCK_DATA_ORDER
  87. # error "HASH_BLOCK_DATA_ORDER must be defined!"
  88. #endif
  89. /*
  90. * Engage compiler specific rotate intrinsic function if available.
  91. */
  92. #undef ROTATE
  93. #ifndef PEDANTIC
  94. # if defined(_MSC_VER)
  95. # define ROTATE(a,n) _lrotl(a,n)
  96. # elif defined(__ICC)
  97. # define ROTATE(a,n) _rotl(a,n)
  98. # elif defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
  99. /*
  100. * Some GNU C inline assembler templates. Note that these are
  101. * rotates by *constant* number of bits! But that's exactly
  102. * what we need here...
  103. */
  104. # if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
  105. # define ROTATE(a,n) ({ register unsigned int ret; \
  106. asm ( \
  107. "roll %1,%0" \
  108. : "=r"(ret) \
  109. : "I"(n), "0"((unsigned int)(a)) \
  110. : "cc"); \
  111. ret; \
  112. })
  113. # elif defined(_ARCH_PPC) || defined(_ARCH_PPC64) || \
  114. defined(__powerpc) || defined(__ppc__) || defined(__powerpc64__)
  115. # define ROTATE(a,n) ({ register unsigned int ret; \
  116. asm ( \
  117. "rlwinm %0,%1,%2,0,31" \
  118. : "=r"(ret) \
  119. : "r"(a), "I"(n)); \
  120. ret; \
  121. })
  122. # elif defined(__s390x__)
  123. # define ROTATE(a,n) ({ register unsigned int ret; \
  124. asm ("rll %0,%1,%2" \
  125. : "=r"(ret) \
  126. : "r"(a), "I"(n)); \
  127. ret; \
  128. })
  129. # endif
  130. # endif
  131. #endif /* PEDANTIC */
  132. #ifndef ROTATE
  133. # define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
  134. #endif
  135. #if defined(DATA_ORDER_IS_BIG_ENDIAN)
  136. # ifndef PEDANTIC
  137. # if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
  138. # if ((defined(__i386) || defined(__i386__)) && !defined(I386_ONLY)) || \
  139. (defined(__x86_64) || defined(__x86_64__))
  140. # if !defined(B_ENDIAN)
  141. /*
  142. * This gives ~30-40% performance improvement in SHA-256 compiled
  143. * with gcc [on P4]. Well, first macro to be frank. We can pull
  144. * this trick on x86* platforms only, because these CPUs can fetch
  145. * unaligned data without raising an exception.
  146. */
  147. # define HOST_c2l(c,l) ({ unsigned int r=*((const unsigned int *)(c)); \
  148. asm ("bswapl %0":"=r"(r):"0"(r)); \
  149. (c)+=4; (l)=r; })
  150. # define HOST_l2c(l,c) ({ unsigned int r=(l); \
  151. asm ("bswapl %0":"=r"(r):"0"(r)); \
  152. *((unsigned int *)(c))=r; (c)+=4; r; })
  153. # endif
  154. # elif defined(__aarch64__)
  155. # if defined(__BYTE_ORDER__)
  156. # if defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
  157. # define HOST_c2l(c,l) ({ unsigned int r; \
  158. asm ("rev %w0,%w1" \
  159. :"=r"(r) \
  160. :"r"(*((const unsigned int *)(c))));\
  161. (c)+=4; (l)=r; })
  162. # define HOST_l2c(l,c) ({ unsigned int r; \
  163. asm ("rev %w0,%w1" \
  164. :"=r"(r) \
  165. :"r"((unsigned int)(l)));\
  166. *((unsigned int *)(c))=r; (c)+=4; r; })
  167. # elif defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
  168. # define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, (l))
  169. # define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, (l))
  170. # endif
  171. # endif
  172. # endif
  173. # endif
  174. # if defined(__s390__) || defined(__s390x__)
  175. # define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, (l))
  176. # define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, (l))
  177. # endif
  178. # endif
  179. # ifndef HOST_c2l
  180. # define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \
  181. l|=(((unsigned long)(*((c)++)))<<16), \
  182. l|=(((unsigned long)(*((c)++)))<< 8), \
  183. l|=(((unsigned long)(*((c)++))) ) )
  184. # endif
  185. # ifndef HOST_l2c
  186. # define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
  187. *((c)++)=(unsigned char)(((l)>>16)&0xff), \
  188. *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
  189. *((c)++)=(unsigned char)(((l) )&0xff), \
  190. l)
  191. # endif
  192. #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
  193. # ifndef PEDANTIC
  194. # if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
  195. # if defined(__s390x__)
  196. # define HOST_c2l(c,l) ({ asm ("lrv %0,%1" \
  197. :"=d"(l) :"m"(*(const unsigned int *)(c)));\
  198. (c)+=4; (l); })
  199. # define HOST_l2c(l,c) ({ asm ("strv %1,%0" \
  200. :"=m"(*(unsigned int *)(c)) :"d"(l));\
  201. (c)+=4; (l); })
  202. # endif
  203. # endif
  204. # if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
  205. # ifndef B_ENDIAN
  206. /* See comment in DATA_ORDER_IS_BIG_ENDIAN section. */
  207. # define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, l)
  208. # define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, l)
  209. # endif
  210. # endif
  211. # endif
  212. # ifndef HOST_c2l
  213. # define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++))) ), \
  214. l|=(((unsigned long)(*((c)++)))<< 8), \
  215. l|=(((unsigned long)(*((c)++)))<<16), \
  216. l|=(((unsigned long)(*((c)++)))<<24) )
  217. # endif
  218. # ifndef HOST_l2c
  219. # define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
  220. *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
  221. *((c)++)=(unsigned char)(((l)>>16)&0xff), \
  222. *((c)++)=(unsigned char)(((l)>>24)&0xff), \
  223. l)
  224. # endif
  225. #endif
  226. /*
  227. * Time for some action :-)
  228. */
  229. int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len)
  230. {
  231. const unsigned char *data = data_;
  232. unsigned char *p;
  233. HASH_LONG l;
  234. size_t n;
  235. if (len == 0)
  236. return 1;
  237. l = (c->Nl + (((HASH_LONG) len) << 3)) & 0xffffffffUL;
  238. if (l < c->Nl) /* overflow */
  239. c->Nh++;
  240. c->Nh += (HASH_LONG) (len >> 29); /* might cause compiler warning on
  241. * 16-bit */
  242. c->Nl = l;
  243. n = c->num;
  244. if (n != 0) {
  245. p = (unsigned char *)c->data;
  246. if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
  247. memcpy(p + n, data, HASH_CBLOCK - n);
  248. HASH_BLOCK_DATA_ORDER(c, p, 1);
  249. n = HASH_CBLOCK - n;
  250. data += n;
  251. len -= n;
  252. c->num = 0;
  253. /*
  254. * We use memset rather than OPENSSL_cleanse() here deliberately.
  255. * Using OPENSSL_cleanse() here could be a performance issue. It
  256. * will get properly cleansed on finalisation so this isn't a
  257. * security problem.
  258. */
  259. memset(p, 0, HASH_CBLOCK); /* keep it zeroed */
  260. } else {
  261. memcpy(p + n, data, len);
  262. c->num += (unsigned int)len;
  263. return 1;
  264. }
  265. }
  266. n = len / HASH_CBLOCK;
  267. if (n > 0) {
  268. HASH_BLOCK_DATA_ORDER(c, data, n);
  269. n *= HASH_CBLOCK;
  270. data += n;
  271. len -= n;
  272. }
  273. if (len != 0) {
  274. p = (unsigned char *)c->data;
  275. c->num = (unsigned int)len;
  276. memcpy(p, data, len);
  277. }
  278. return 1;
  279. }
  280. void HASH_TRANSFORM(HASH_CTX *c, const unsigned char *data)
  281. {
  282. HASH_BLOCK_DATA_ORDER(c, data, 1);
  283. }
  284. int HASH_FINAL(unsigned char *md, HASH_CTX *c)
  285. {
  286. unsigned char *p = (unsigned char *)c->data;
  287. size_t n = c->num;
  288. p[n] = 0x80; /* there is always room for one */
  289. n++;
  290. if (n > (HASH_CBLOCK - 8)) {
  291. memset(p + n, 0, HASH_CBLOCK - n);
  292. n = 0;
  293. HASH_BLOCK_DATA_ORDER(c, p, 1);
  294. }
  295. memset(p + n, 0, HASH_CBLOCK - 8 - n);
  296. p += HASH_CBLOCK - 8;
  297. #if defined(DATA_ORDER_IS_BIG_ENDIAN)
  298. (void)HOST_l2c(c->Nh, p);
  299. (void)HOST_l2c(c->Nl, p);
  300. #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
  301. (void)HOST_l2c(c->Nl, p);
  302. (void)HOST_l2c(c->Nh, p);
  303. #endif
  304. p -= HASH_CBLOCK;
  305. HASH_BLOCK_DATA_ORDER(c, p, 1);
  306. c->num = 0;
  307. OPENSSL_cleanse(p, HASH_CBLOCK);
  308. #ifndef HASH_MAKE_STRING
  309. # error "HASH_MAKE_STRING must be defined!"
  310. #else
  311. HASH_MAKE_STRING(c, md);
  312. #endif
  313. return 1;
  314. }
  315. #ifndef MD32_REG_T
  316. # if defined(__alpha) || defined(__sparcv9) || defined(__mips)
  317. # define MD32_REG_T long
  318. /*
  319. * This comment was originally written for MD5, which is why it
  320. * discusses A-D. But it basically applies to all 32-bit digests,
  321. * which is why it was moved to common header file.
  322. *
  323. * In case you wonder why A-D are declared as long and not
  324. * as MD5_LONG. Doing so results in slight performance
  325. * boost on LP64 architectures. The catch is we don't
  326. * really care if 32 MSBs of a 64-bit register get polluted
  327. * with eventual overflows as we *save* only 32 LSBs in
  328. * *either* case. Now declaring 'em long excuses the compiler
  329. * from keeping 32 MSBs zeroed resulting in 13% performance
  330. * improvement under SPARC Solaris7/64 and 5% under AlphaLinux.
  331. * Well, to be honest it should say that this *prevents*
  332. * performance degradation.
  333. */
  334. # else
  335. /*
  336. * Above is not absolute and there are LP64 compilers that
  337. * generate better code if MD32_REG_T is defined int. The above
  338. * pre-processor condition reflects the circumstances under which
  339. * the conclusion was made and is subject to further extension.
  340. */
  341. # define MD32_REG_T int
  342. # endif
  343. #endif