Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

md32_common.h 10 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. #if defined(__cplusplus)
  52. extern "C" {
  53. #endif
  54. #define asm __asm__
  55. /* This is a generic 32 bit "collector" for message digest algorithms.
  56. * Whenever needed it collects input character stream into chunks of
  57. * 32 bit values and invokes a block function that performs actual hash
  58. * calculations.
  59. *
  60. * Porting guide.
  61. *
  62. * Obligatory macros:
  63. *
  64. * DATA_ORDER_IS_BIG_ENDIAN or DATA_ORDER_IS_LITTLE_ENDIAN
  65. * this macro defines byte order of input stream.
  66. * HASH_CBLOCK
  67. * size of a unit chunk HASH_BLOCK operates on.
  68. * HASH_LONG
  69. * has to be at least 32 bit wide.
  70. * HASH_CTX
  71. * context structure that at least contains following
  72. * members:
  73. * typedef struct {
  74. * ...
  75. * HASH_LONG Nl,Nh;
  76. * either {
  77. * HASH_LONG data[HASH_LBLOCK];
  78. * unsigned char data[HASH_CBLOCK];
  79. * };
  80. * unsigned int num;
  81. * ...
  82. * } HASH_CTX;
  83. * data[] vector is expected to be zeroed upon first call to
  84. * HASH_UPDATE.
  85. * HASH_UPDATE
  86. * name of "Update" function, implemented here.
  87. * HASH_TRANSFORM
  88. * name of "Transform" function, implemented here.
  89. * HASH_FINAL
  90. * name of "Final" function, implemented here.
  91. * HASH_BLOCK_DATA_ORDER
  92. * name of "block" function capable of treating *unaligned* input
  93. * message in original (data) byte order, implemented externally.
  94. * HASH_MAKE_STRING
  95. * macro convering context variables to an ASCII hash string.
  96. *
  97. * <appro@fy.chalmers.se>
  98. */
  99. #if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN)
  100. #error "DATA_ORDER must be defined!"
  101. #endif
  102. #ifndef HASH_CBLOCK
  103. #error "HASH_CBLOCK must be defined!"
  104. #endif
  105. #ifndef HASH_LONG
  106. #error "HASH_LONG must be defined!"
  107. #endif
  108. #ifndef HASH_CTX
  109. #error "HASH_CTX must be defined!"
  110. #endif
  111. #ifndef HASH_UPDATE
  112. #error "HASH_UPDATE must be defined!"
  113. #endif
  114. #ifndef HASH_TRANSFORM
  115. #error "HASH_TRANSFORM must be defined!"
  116. #endif
  117. #ifndef HASH_FINAL
  118. #error "HASH_FINAL must be defined!"
  119. #endif
  120. #ifndef HASH_BLOCK_DATA_ORDER
  121. #error "HASH_BLOCK_DATA_ORDER must be defined!"
  122. #endif
  123. /*
  124. * Engage compiler specific rotate intrinsic function if available.
  125. */
  126. #undef ROTATE
  127. # if defined(_MSC_VER)
  128. # define ROTATE(a,n) _lrotl(a,n)
  129. # elif defined(__ICC)
  130. # define ROTATE(a,n) _rotl(a,n)
  131. # elif defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM)
  132. /*
  133. * Some GNU C inline assembler templates. Note that these are
  134. * rotates by *constant* number of bits! But that's exactly
  135. * what we need here...
  136. * <appro@fy.chalmers.se>
  137. */
  138. # if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
  139. # define ROTATE(a,n) ({ register unsigned int ret; \
  140. asm ( \
  141. "roll %1,%0" \
  142. : "=r"(ret) \
  143. : "I"(n), "0"((unsigned int)(a)) \
  144. : "cc"); \
  145. ret; \
  146. })
  147. # endif /* OPENSSL_X86 || OPENSSL_X86_64 */
  148. # endif /* COMPILER */
  149. #ifndef ROTATE
  150. #define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
  151. #endif
  152. #if defined(DATA_ORDER_IS_BIG_ENDIAN)
  153. #ifndef PEDANTIC
  154. # if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM)
  155. # if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
  156. /*
  157. * This gives ~30-40% performance improvement in SHA-256 compiled
  158. * with gcc [on P4]. Well, first macro to be frank. We can pull
  159. * this trick on x86* platforms only, because these CPUs can fetch
  160. * unaligned data without raising an exception.
  161. */
  162. # define HOST_c2l(c,l) ({ unsigned int r=*((const unsigned int *)(c)); \
  163. asm ("bswapl %0":"=r"(r):"0"(r)); \
  164. (c)+=4; (l)=r; })
  165. # define HOST_l2c(l,c) ({ unsigned int r=(l); \
  166. asm ("bswapl %0":"=r"(r):"0"(r)); \
  167. *((unsigned int *)(c))=r; (c)+=4; r; })
  168. # elif defined(__aarch64__)
  169. # if defined(__BYTE_ORDER__)
  170. # if defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
  171. # define HOST_c2l(c,l) ({ unsigned int r; \
  172. asm ("rev %w0,%w1" \
  173. :"=r"(r) \
  174. :"r"(*((const unsigned int *)(c))));\
  175. (c)+=4; (l)=r; })
  176. # define HOST_l2c(l,c) ({ unsigned int r; \
  177. asm ("rev %w0,%w1" \
  178. :"=r"(r) \
  179. :"r"((unsigned int)(l)));\
  180. *((unsigned int *)(c))=r; (c)+=4; r; })
  181. # elif defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
  182. # define HOST_c2l(c,l) (void)((l)=*((const unsigned int *)(c)), (c)+=4)
  183. # define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, (l))
  184. # endif
  185. # endif
  186. # endif
  187. # endif
  188. #endif
  189. #ifndef HOST_c2l
  190. #define HOST_c2l(c,l) (void)(l =(((unsigned long)(*((c)++)))<<24), \
  191. l|=(((unsigned long)(*((c)++)))<<16), \
  192. l|=(((unsigned long)(*((c)++)))<< 8), \
  193. l|=(((unsigned long)(*((c)++))) ))
  194. #endif
  195. #ifndef HOST_l2c
  196. #define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
  197. *((c)++)=(unsigned char)(((l)>>16)&0xff), \
  198. *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
  199. *((c)++)=(unsigned char)(((l) )&0xff), \
  200. l)
  201. #endif
  202. #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
  203. #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
  204. /* See comment in DATA_ORDER_IS_BIG_ENDIAN section. */
  205. # define HOST_c2l(c,l) (void)((l)=*((const unsigned int *)(c)), (c)+=4)
  206. # define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, l)
  207. #endif
  208. #ifndef HOST_c2l
  209. #define HOST_c2l(c,l) (void)(l =(((unsigned long)(*((c)++))) ), \
  210. l|=(((unsigned long)(*((c)++)))<< 8), \
  211. l|=(((unsigned long)(*((c)++)))<<16), \
  212. l|=(((unsigned long)(*((c)++)))<<24))
  213. #endif
  214. #ifndef HOST_l2c
  215. #define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
  216. *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
  217. *((c)++)=(unsigned char)(((l)>>16)&0xff), \
  218. *((c)++)=(unsigned char)(((l)>>24)&0xff), \
  219. l)
  220. #endif
  221. #endif
  222. int HASH_UPDATE (HASH_CTX *c, const void *data_, size_t len)
  223. {
  224. const unsigned char *data=data_;
  225. unsigned char *p;
  226. HASH_LONG l;
  227. size_t n;
  228. if (len==0) return 1;
  229. l=(c->Nl+(((HASH_LONG)len)<<3))&0xffffffffUL;
  230. /* 95-05-24 eay Fixed a bug with the overflow handling, thanks to
  231. * Wei Dai <weidai@eskimo.com> for pointing it out. */
  232. if (l < c->Nl) /* overflow */
  233. c->Nh++;
  234. c->Nh+=(HASH_LONG)(len>>29); /* might cause compiler warning on 16-bit */
  235. c->Nl=l;
  236. n = c->num;
  237. if (n != 0)
  238. {
  239. p=(unsigned char *)c->data;
  240. if (len >= HASH_CBLOCK || len+n >= HASH_CBLOCK)
  241. {
  242. memcpy (p+n,data,HASH_CBLOCK-n);
  243. HASH_BLOCK_DATA_ORDER (c,p,1);
  244. n = HASH_CBLOCK-n;
  245. data += n;
  246. len -= n;
  247. c->num = 0;
  248. memset (p,0,HASH_CBLOCK); /* keep it zeroed */
  249. }
  250. else
  251. {
  252. memcpy (p+n,data,len);
  253. c->num += (unsigned int)len;
  254. return 1;
  255. }
  256. }
  257. n = len/HASH_CBLOCK;
  258. if (n > 0)
  259. {
  260. HASH_BLOCK_DATA_ORDER (c,data,n);
  261. n *= HASH_CBLOCK;
  262. data += n;
  263. len -= n;
  264. }
  265. if (len != 0)
  266. {
  267. p = (unsigned char *)c->data;
  268. c->num = (unsigned int)len;
  269. memcpy (p,data,len);
  270. }
  271. return 1;
  272. }
  273. void HASH_TRANSFORM (HASH_CTX *c, const unsigned char *data)
  274. {
  275. HASH_BLOCK_DATA_ORDER (c,data,1);
  276. }
  277. int HASH_FINAL (unsigned char *md, HASH_CTX *c)
  278. {
  279. unsigned char *p = (unsigned char *)c->data;
  280. size_t n = c->num;
  281. p[n] = 0x80; /* there is always room for one */
  282. n++;
  283. if (n > (HASH_CBLOCK-8))
  284. {
  285. memset (p+n,0,HASH_CBLOCK-n);
  286. n=0;
  287. HASH_BLOCK_DATA_ORDER (c,p,1);
  288. }
  289. memset (p+n,0,HASH_CBLOCK-8-n);
  290. p += HASH_CBLOCK-8;
  291. #if defined(DATA_ORDER_IS_BIG_ENDIAN)
  292. (void)HOST_l2c(c->Nh,p);
  293. (void)HOST_l2c(c->Nl,p);
  294. #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
  295. (void)HOST_l2c(c->Nl,p);
  296. (void)HOST_l2c(c->Nh,p);
  297. #endif
  298. p -= HASH_CBLOCK;
  299. HASH_BLOCK_DATA_ORDER (c,p,1);
  300. c->num=0;
  301. memset (p,0,HASH_CBLOCK);
  302. #ifndef HASH_MAKE_STRING
  303. #error "HASH_MAKE_STRING must be defined!"
  304. #else
  305. HASH_MAKE_STRING(c,md);
  306. #endif
  307. return 1;
  308. }
  309. #ifndef MD32_REG_T
  310. #define MD32_REG_T int
  311. #endif
  312. #if defined(__cplusplus)
  313. } /* extern C */
  314. #endif
  315. #endif /* OPENSSL_HEADER_MD32_COMMON_H */