You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

371 lines
10 KiB

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