2
0

md32_common.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. #define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
  90. #if defined(DATA_ORDER_IS_BIG_ENDIAN)
  91. # define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \
  92. l|=(((unsigned long)(*((c)++)))<<16), \
  93. l|=(((unsigned long)(*((c)++)))<< 8), \
  94. l|=(((unsigned long)(*((c)++))) ) )
  95. # define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
  96. *((c)++)=(unsigned char)(((l)>>16)&0xff), \
  97. *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
  98. *((c)++)=(unsigned char)(((l) )&0xff), \
  99. l)
  100. #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
  101. # define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++))) ), \
  102. l|=(((unsigned long)(*((c)++)))<< 8), \
  103. l|=(((unsigned long)(*((c)++)))<<16), \
  104. l|=(((unsigned long)(*((c)++)))<<24) )
  105. # define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
  106. *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
  107. *((c)++)=(unsigned char)(((l)>>16)&0xff), \
  108. *((c)++)=(unsigned char)(((l)>>24)&0xff), \
  109. l)
  110. #endif
  111. /*
  112. * Time for some action :-)
  113. */
  114. int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len)
  115. {
  116. const unsigned char *data = data_;
  117. unsigned char *p;
  118. HASH_LONG l;
  119. size_t n;
  120. if (len == 0)
  121. return 1;
  122. l = (c->Nl + (((HASH_LONG) len) << 3)) & 0xffffffffUL;
  123. if (l < c->Nl) /* overflow */
  124. c->Nh++;
  125. c->Nh += (HASH_LONG) (len >> 29); /* might cause compiler warning on
  126. * 16-bit */
  127. c->Nl = l;
  128. n = c->num;
  129. if (n != 0) {
  130. p = (unsigned char *)c->data;
  131. if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
  132. memcpy(p + n, data, HASH_CBLOCK - n);
  133. HASH_BLOCK_DATA_ORDER(c, p, 1);
  134. n = HASH_CBLOCK - n;
  135. data += n;
  136. len -= n;
  137. c->num = 0;
  138. /*
  139. * We use memset rather than OPENSSL_cleanse() here deliberately.
  140. * Using OPENSSL_cleanse() here could be a performance issue. It
  141. * will get properly cleansed on finalisation so this isn't a
  142. * security problem.
  143. */
  144. memset(p, 0, HASH_CBLOCK); /* keep it zeroed */
  145. } else {
  146. memcpy(p + n, data, len);
  147. c->num += (unsigned int)len;
  148. return 1;
  149. }
  150. }
  151. n = len / HASH_CBLOCK;
  152. if (n > 0) {
  153. HASH_BLOCK_DATA_ORDER(c, data, n);
  154. n *= HASH_CBLOCK;
  155. data += n;
  156. len -= n;
  157. }
  158. if (len != 0) {
  159. p = (unsigned char *)c->data;
  160. c->num = (unsigned int)len;
  161. memcpy(p, data, len);
  162. }
  163. return 1;
  164. }
  165. void HASH_TRANSFORM(HASH_CTX *c, const unsigned char *data)
  166. {
  167. HASH_BLOCK_DATA_ORDER(c, data, 1);
  168. }
  169. int HASH_FINAL(unsigned char *md, HASH_CTX *c)
  170. {
  171. unsigned char *p = (unsigned char *)c->data;
  172. size_t n = c->num;
  173. p[n] = 0x80; /* there is always room for one */
  174. n++;
  175. if (n > (HASH_CBLOCK - 8)) {
  176. memset(p + n, 0, HASH_CBLOCK - n);
  177. n = 0;
  178. HASH_BLOCK_DATA_ORDER(c, p, 1);
  179. }
  180. memset(p + n, 0, HASH_CBLOCK - 8 - n);
  181. p += HASH_CBLOCK - 8;
  182. #if defined(DATA_ORDER_IS_BIG_ENDIAN)
  183. (void)HOST_l2c(c->Nh, p);
  184. (void)HOST_l2c(c->Nl, p);
  185. #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
  186. (void)HOST_l2c(c->Nl, p);
  187. (void)HOST_l2c(c->Nh, p);
  188. #endif
  189. p -= HASH_CBLOCK;
  190. HASH_BLOCK_DATA_ORDER(c, p, 1);
  191. c->num = 0;
  192. OPENSSL_cleanse(p, HASH_CBLOCK);
  193. #ifndef HASH_MAKE_STRING
  194. # error "HASH_MAKE_STRING must be defined!"
  195. #else
  196. HASH_MAKE_STRING(c, md);
  197. #endif
  198. return 1;
  199. }
  200. #ifndef MD32_REG_T
  201. # if defined(__alpha) || defined(__sparcv9) || defined(__mips)
  202. # define MD32_REG_T long
  203. /*
  204. * This comment was originally written for MD5, which is why it
  205. * discusses A-D. But it basically applies to all 32-bit digests,
  206. * which is why it was moved to common header file.
  207. *
  208. * In case you wonder why A-D are declared as long and not
  209. * as MD5_LONG. Doing so results in slight performance
  210. * boost on LP64 architectures. The catch is we don't
  211. * really care if 32 MSBs of a 64-bit register get polluted
  212. * with eventual overflows as we *save* only 32 LSBs in
  213. * *either* case. Now declaring 'em long excuses the compiler
  214. * from keeping 32 MSBs zeroed resulting in 13% performance
  215. * improvement under SPARC Solaris7/64 and 5% under AlphaLinux.
  216. * Well, to be honest it should say that this *prevents*
  217. * performance degradation.
  218. */
  219. # else
  220. /*
  221. * Above is not absolute and there are LP64 compilers that
  222. * generate better code if MD32_REG_T is defined int. The above
  223. * pre-processor condition reflects the circumstances under which
  224. * the conclusion was made and is subject to further extension.
  225. */
  226. # define MD32_REG_T int
  227. # endif
  228. #endif