Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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