md32_common.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /* md32_common.h file used by sha256 implementation */
  2. /* ====================================================================
  3. * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this
  18. * software must display the following acknowledgment:
  19. * "This product includes software developed by the OpenSSL Project
  20. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  21. *
  22. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For written permission, please contact
  25. * licensing@OpenSSL.org.
  26. *
  27. * 5. Products derived from this software may not be called "OpenSSL"
  28. * nor may "OpenSSL" appear in their names without prior written
  29. * permission of the OpenSSL Project.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. * OF THE POSSIBILITY OF SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. */
  51. /*-
  52. * This is a generic 32 bit "collector" for message digest algorithms.
  53. * Whenever needed it collects input character stream into chunks of
  54. * 32 bit values and invokes a block function that performs actual hash
  55. * calculations.
  56. *
  57. * Porting guide.
  58. *
  59. * Obligatory macros:
  60. *
  61. * DATA_ORDER_IS_BIG_ENDIAN or DATA_ORDER_IS_LITTLE_ENDIAN
  62. * this macro defines byte order of input stream.
  63. * HASH_CBLOCK
  64. * size of a unit chunk HASH_BLOCK operates on.
  65. * HASH_LONG
  66. * has to be at lest 32 bit wide, if it's wider, then
  67. * HASH_LONG_LOG2 *has to* be defined along
  68. * HASH_CTX
  69. * context structure that at least contains following
  70. * members:
  71. * typedef struct {
  72. * ...
  73. * HASH_LONG Nl,Nh;
  74. * either {
  75. * HASH_LONG data[HASH_LBLOCK];
  76. * unsigned char data[HASH_CBLOCK];
  77. * };
  78. * unsigned int num;
  79. * ...
  80. * } HASH_CTX;
  81. * data[] vector is expected to be zeroed upon first call to
  82. * HASH_UPDATE.
  83. * HASH_UPDATE
  84. * name of "Update" function, implemented here.
  85. * HASH_TRANSFORM
  86. * name of "Transform" function, implemented here.
  87. * HASH_FINAL
  88. * name of "Final" function, implemented here.
  89. * HASH_BLOCK_DATA_ORDER
  90. * name of "block" function capable of treating *unaligned* input
  91. * message in original (data) byte order, implemented externally.
  92. * HASH_MAKE_STRING
  93. * macro convering context variables to an ASCII hash string.
  94. *
  95. * MD5 example:
  96. *
  97. * #define DATA_ORDER_IS_LITTLE_ENDIAN
  98. *
  99. * #define HASH_LONG MD5_LONG
  100. * #define HASH_LONG_LOG2 MD5_LONG_LOG2
  101. * #define HASH_CTX MD5_CTX
  102. * #define HASH_CBLOCK MD5_CBLOCK
  103. * #define HASH_UPDATE MD5_Update
  104. * #define HASH_TRANSFORM MD5_Transform
  105. * #define HASH_FINAL MD5_Final
  106. * #define HASH_BLOCK_DATA_ORDER md5_block_data_order
  107. *
  108. * <appro@fy.chalmers.se>
  109. */
  110. #if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN)
  111. # error "DATA_ORDER must be defined!"
  112. #endif
  113. #ifndef HASH_CBLOCK
  114. # error "HASH_CBLOCK must be defined!"
  115. #endif
  116. #ifndef HASH_LONG
  117. # error "HASH_LONG must be defined!"
  118. #endif
  119. #ifndef HASH_CTX
  120. # error "HASH_CTX must be defined!"
  121. #endif
  122. #ifndef HASH_UPDATE
  123. # error "HASH_UPDATE must be defined!"
  124. #endif
  125. #ifndef HASH_TRANSFORM
  126. # error "HASH_TRANSFORM must be defined!"
  127. #endif
  128. #ifndef HASH_FINAL
  129. # error "HASH_FINAL must be defined!"
  130. #endif
  131. #ifndef HASH_BLOCK_DATA_ORDER
  132. # error "HASH_BLOCK_DATA_ORDER must be defined!"
  133. #endif
  134. /*
  135. * Engage compiler specific rotate intrinsic function if available.
  136. */
  137. #undef ROTATE
  138. #ifndef PEDANTIC
  139. # if defined(_MSC_VER)
  140. # define ROTATE(a,n) _lrotl(a,n)
  141. # elif defined(__ICC)
  142. # define ROTATE(a,n) _rotl(a,n)
  143. # elif defined(__MWERKS__)
  144. # if defined(__POWERPC__)
  145. # define ROTATE(a,n) __rlwinm(a,n,0,31)
  146. # elif defined(__MC68K__)
  147. /* Motorola specific tweak. <appro@fy.chalmers.se> */
  148. # define ROTATE(a,n) ( n<24 ? __rol(a,n) : __ror(a,32-n) )
  149. # else
  150. # define ROTATE(a,n) __rol(a,n)
  151. # endif
  152. # elif defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
  153. /*
  154. * Some GNU C inline assembler templates. Note that these are
  155. * rotates by *constant* number of bits! But that's exactly
  156. * what we need here...
  157. * <appro@fy.chalmers.se>
  158. */
  159. # if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
  160. # define ROTATE(a,n) ({ register unsigned int ret; \
  161. asm ( \
  162. "roll %1,%0" \
  163. : "=r"(ret) \
  164. : "I"(n), "0"((unsigned int)(a)) \
  165. : "cc"); \
  166. ret; \
  167. })
  168. # elif defined(_ARCH_PPC) || defined(_ARCH_PPC64) || \
  169. defined(__powerpc) || defined(__ppc__) || defined(__powerpc64__)
  170. # define ROTATE(a,n) ({ register unsigned int ret; \
  171. asm ( \
  172. "rlwinm %0,%1,%2,0,31" \
  173. : "=r"(ret) \
  174. : "r"(a), "I"(n)); \
  175. ret; \
  176. })
  177. # elif defined(__s390x__)
  178. # define ROTATE(a,n) ({ register unsigned int ret; \
  179. asm ("rll %0,%1,%2" \
  180. : "=r"(ret) \
  181. : "r"(a), "I"(n)); \
  182. ret; \
  183. })
  184. # endif
  185. # endif
  186. #endif /* PEDANTIC */
  187. #ifndef ROTATE
  188. # define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
  189. #endif
  190. #if defined(DATA_ORDER_IS_BIG_ENDIAN)
  191. # ifndef PEDANTIC
  192. # if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
  193. # if ((defined(__i386) || defined(__i386__)) && !defined(I386_ONLY)) || \
  194. (defined(__x86_64) || defined(__x86_64__))
  195. # if !defined(B_ENDIAN)
  196. /*
  197. * This gives ~30-40% performance improvement in SHA-256 compiled
  198. * with gcc [on P4]. Well, first macro to be frank. We can pull
  199. * this trick on x86* platforms only, because these CPUs can fetch
  200. * unaligned data without raising an exception.
  201. */
  202. # define HOST_c2l(c,l) ({ unsigned int r=*((const unsigned int *)(c)); \
  203. asm ("bswapl %0":"=r"(r):"0"(r)); \
  204. (c)+=4; (l)=r; })
  205. # define HOST_l2c(l,c) ({ unsigned int r=(l); \
  206. asm ("bswapl %0":"=r"(r):"0"(r)); \
  207. *((unsigned int *)(c))=r; (c)+=4; r; })
  208. # endif
  209. # elif defined(__aarch64__)
  210. # if defined(__BYTE_ORDER__)
  211. # if defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
  212. # define HOST_c2l(c,l) ({ unsigned int r; \
  213. asm ("rev %w0,%w1" \
  214. :"=r"(r) \
  215. :"r"(*((const unsigned int *)(c))));\
  216. (c)+=4; (l)=r; })
  217. # define HOST_l2c(l,c) ({ unsigned int r; \
  218. asm ("rev %w0,%w1" \
  219. :"=r"(r) \
  220. :"r"((unsigned int)(l)));\
  221. *((unsigned int *)(c))=r; (c)+=4; r; })
  222. # elif defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
  223. # define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, (l))
  224. # define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, (l))
  225. # endif
  226. # endif
  227. # endif
  228. # endif
  229. # if defined(__s390__) || defined(__s390x__)
  230. # define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, (l))
  231. # define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, (l))
  232. # endif
  233. # endif
  234. # ifndef HOST_c2l
  235. # define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \
  236. l|=(((unsigned long)(*((c)++)))<<16), \
  237. l|=(((unsigned long)(*((c)++)))<< 8), \
  238. l|=(((unsigned long)(*((c)++))) ) )
  239. # endif
  240. # ifndef HOST_l2c
  241. # define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
  242. *((c)++)=(unsigned char)(((l)>>16)&0xff), \
  243. *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
  244. *((c)++)=(unsigned char)(((l) )&0xff), \
  245. l)
  246. # endif
  247. #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
  248. # ifndef PEDANTIC
  249. # if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
  250. # if defined(__s390x__)
  251. # define HOST_c2l(c,l) ({ asm ("lrv %0,%1" \
  252. :"=d"(l) :"m"(*(const unsigned int *)(c)));\
  253. (c)+=4; (l); })
  254. # define HOST_l2c(l,c) ({ asm ("strv %1,%0" \
  255. :"=m"(*(unsigned int *)(c)) :"d"(l));\
  256. (c)+=4; (l); })
  257. # endif
  258. # endif
  259. # if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
  260. # ifndef B_ENDIAN
  261. /* See comment in DATA_ORDER_IS_BIG_ENDIAN section. */
  262. # define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, l)
  263. # define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, l)
  264. # endif
  265. # endif
  266. # endif
  267. # ifndef HOST_c2l
  268. # define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++))) ), \
  269. l|=(((unsigned long)(*((c)++)))<< 8), \
  270. l|=(((unsigned long)(*((c)++)))<<16), \
  271. l|=(((unsigned long)(*((c)++)))<<24) )
  272. # endif
  273. # ifndef HOST_l2c
  274. # define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
  275. *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
  276. *((c)++)=(unsigned char)(((l)>>16)&0xff), \
  277. *((c)++)=(unsigned char)(((l)>>24)&0xff), \
  278. l)
  279. # endif
  280. #endif
  281. /*
  282. * Time for some action:-)
  283. */
  284. int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len)
  285. {
  286. const unsigned char *data = data_;
  287. unsigned char *p;
  288. HASH_LONG l;
  289. size_t n;
  290. if (len == 0)
  291. return 1;
  292. l = (c->Nl + (((HASH_LONG) len) << 3)) & 0xffffffffUL;
  293. /*
  294. * 95-05-24 eay Fixed a bug with the overflow handling, thanks to Wei Dai
  295. * <weidai@eskimo.com> for pointing it out.
  296. */
  297. if (l < c->Nl) /* overflow */
  298. c->Nh++;
  299. c->Nh += (HASH_LONG) (len >> 29); /* might cause compiler warning on
  300. * 16-bit */
  301. c->Nl = l;
  302. n = c->num;
  303. if (n != 0) {
  304. p = (unsigned char *)c->data;
  305. if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
  306. memcpy(p + n, data, HASH_CBLOCK - n);
  307. HASH_BLOCK_DATA_ORDER(c, p, 1);
  308. n = HASH_CBLOCK - n;
  309. data += n;
  310. len -= n;
  311. c->num = 0;
  312. memset(p, 0, HASH_CBLOCK); /* keep it zeroed */
  313. } else {
  314. memcpy(p + n, data, len);
  315. c->num += (unsigned int)len;
  316. return 1;
  317. }
  318. }
  319. n = len / HASH_CBLOCK;
  320. if (n > 0) {
  321. HASH_BLOCK_DATA_ORDER(c, data, n);
  322. n *= HASH_CBLOCK;
  323. data += n;
  324. len -= n;
  325. }
  326. if (len != 0) {
  327. p = (unsigned char *)c->data;
  328. c->num = (unsigned int)len;
  329. memcpy(p, data, len);
  330. }
  331. return 1;
  332. }
  333. void HASH_TRANSFORM(HASH_CTX *c, const unsigned char *data)
  334. {
  335. HASH_BLOCK_DATA_ORDER(c, data, 1);
  336. }
  337. int HASH_FINAL(unsigned char *md, HASH_CTX *c)
  338. {
  339. unsigned char *p = (unsigned char *)c->data;
  340. size_t n = c->num;
  341. p[n] = 0x80; /* there is always room for one */
  342. n++;
  343. if (n > (HASH_CBLOCK - 8)) {
  344. memset(p + n, 0, HASH_CBLOCK - n);
  345. n = 0;
  346. HASH_BLOCK_DATA_ORDER(c, p, 1);
  347. }
  348. memset(p + n, 0, HASH_CBLOCK - 8 - n);
  349. p += HASH_CBLOCK - 8;
  350. #if defined(DATA_ORDER_IS_BIG_ENDIAN)
  351. (void)HOST_l2c(c->Nh, p);
  352. (void)HOST_l2c(c->Nl, p);
  353. #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
  354. (void)HOST_l2c(c->Nl, p);
  355. (void)HOST_l2c(c->Nh, p);
  356. #endif
  357. p -= HASH_CBLOCK;
  358. HASH_BLOCK_DATA_ORDER(c, p, 1);
  359. c->num = 0;
  360. memset(p, 0, HASH_CBLOCK);
  361. #ifndef HASH_MAKE_STRING
  362. # error "HASH_MAKE_STRING must be defined!"
  363. #else
  364. HASH_MAKE_STRING(c, md);
  365. #endif
  366. return 1;
  367. }
  368. #ifndef MD32_REG_T
  369. # if defined(__alpha) || defined(__sparcv9) || defined(__mips)
  370. # define MD32_REG_T long
  371. /*
  372. * This comment was originaly written for MD5, which is why it
  373. * discusses A-D. But it basically applies to all 32-bit digests,
  374. * which is why it was moved to common header file.
  375. *
  376. * In case you wonder why A-D are declared as long and not
  377. * as MD5_LONG. Doing so results in slight performance
  378. * boost on LP64 architectures. The catch is we don't
  379. * really care if 32 MSBs of a 64-bit register get polluted
  380. * with eventual overflows as we *save* only 32 LSBs in
  381. * *either* case. Now declaring 'em long excuses the compiler
  382. * from keeping 32 MSBs zeroed resulting in 13% performance
  383. * improvement under SPARC Solaris7/64 and 5% under AlphaLinux.
  384. * Well, to be honest it should say that this *prevents*
  385. * performance degradation.
  386. * <appro@fy.chalmers.se>
  387. */
  388. # else
  389. /*
  390. * Above is not absolute and there are LP64 compilers that
  391. * generate better code if MD32_REG_T is defined int. The above
  392. * pre-processor condition reflects the circumstances under which
  393. * the conclusion was made and is subject to further extension.
  394. * <appro@fy.chalmers.se>
  395. */
  396. # define MD32_REG_T int
  397. # endif
  398. #endif