No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

273 líneas
8.6 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 <assert.h>
  52. #if defined(__cplusplus)
  53. extern "C" {
  54. #endif
  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. * uint8_t data[HASH_CBLOCK];
  73. * unsigned 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
  79. * SHA-512).
  80. *
  81. * |HASH_UPDATE| must be defined as the name of the "Update" function to
  82. * generate.
  83. *
  84. * |HASH_TRANSFORM| must be defined as the the name of the "Transform"
  85. * function to generate.
  86. *
  87. * |HASH_FINAL| must be defined as the name of "Final" function to generate.
  88. *
  89. * |HASH_BLOCK_DATA_ORDER| must be defined as the name of the "Block" function.
  90. * That function must be implemented manually. It must be capable of operating
  91. * on *unaligned* input data in its original (data) byte order. It must have
  92. * this signature:
  93. *
  94. * void HASH_BLOCK_DATA_ORDER(uint32_t *state, const uint8_t *data,
  95. * size_t num);
  96. *
  97. * It must update the hash state |state| with |num| blocks of data from |data|,
  98. * where each block is |HASH_CBLOCK| bytes; i.e. |data| points to a array of
  99. * |HASH_CBLOCK * num| bytes. |state| points to the |h| member of a |HASH_CTX|,
  100. * and so will have |<chaining length> / sizeof(uint32_t)| elements.
  101. *
  102. * |HASH_MAKE_STRING(c, s)| must be defined as a block statement that converts
  103. * the hash state |c->h| into the output byte order, storing the result in |s|.
  104. */
  105. #if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN)
  106. #error "DATA_ORDER must be defined!"
  107. #endif
  108. #ifndef HASH_CBLOCK
  109. #error "HASH_CBLOCK must be defined!"
  110. #endif
  111. #ifndef HASH_CTX
  112. #error "HASH_CTX must be defined!"
  113. #endif
  114. #ifndef HASH_UPDATE
  115. #error "HASH_UPDATE must be defined!"
  116. #endif
  117. #ifndef HASH_TRANSFORM
  118. #error "HASH_TRANSFORM must be defined!"
  119. #endif
  120. #ifndef HASH_FINAL
  121. #error "HASH_FINAL must be defined!"
  122. #endif
  123. #ifndef HASH_BLOCK_DATA_ORDER
  124. #error "HASH_BLOCK_DATA_ORDER must be defined!"
  125. #endif
  126. #ifndef HASH_MAKE_STRING
  127. #error "HASH_MAKE_STRING must be defined!"
  128. #endif
  129. #if defined(DATA_ORDER_IS_BIG_ENDIAN)
  130. #define HOST_c2l(c, l) \
  131. do { \
  132. (l) = (((uint32_t)(*((c)++))) << 24); \
  133. (l) |= (((uint32_t)(*((c)++))) << 16); \
  134. (l) |= (((uint32_t)(*((c)++))) << 8); \
  135. (l) |= (((uint32_t)(*((c)++)))); \
  136. } while (0)
  137. #define HOST_l2c(l, c) \
  138. do { \
  139. *((c)++) = (uint8_t)(((l) >> 24) & 0xff); \
  140. *((c)++) = (uint8_t)(((l) >> 16) & 0xff); \
  141. *((c)++) = (uint8_t)(((l) >> 8) & 0xff); \
  142. *((c)++) = (uint8_t)(((l)) & 0xff); \
  143. } while (0)
  144. #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
  145. #define HOST_c2l(c, l) \
  146. do { \
  147. (l) = (((uint32_t)(*((c)++)))); \
  148. (l) |= (((uint32_t)(*((c)++))) << 8); \
  149. (l) |= (((uint32_t)(*((c)++))) << 16); \
  150. (l) |= (((uint32_t)(*((c)++))) << 24); \
  151. } while (0)
  152. #define HOST_l2c(l, c) \
  153. do { \
  154. *((c)++) = (uint8_t)(((l)) & 0xff); \
  155. *((c)++) = (uint8_t)(((l) >> 8) & 0xff); \
  156. *((c)++) = (uint8_t)(((l) >> 16) & 0xff); \
  157. *((c)++) = (uint8_t)(((l) >> 24) & 0xff); \
  158. } while (0)
  159. #endif /* DATA_ORDER */
  160. int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len) {
  161. const uint8_t *data = data_;
  162. if (len == 0) {
  163. return 1;
  164. }
  165. uint32_t l = c->Nl + (((uint32_t)len) << 3);
  166. if (l < c->Nl) {
  167. /* Handle carries. */
  168. c->Nh++;
  169. }
  170. c->Nh += (uint32_t)(len >> 29);
  171. c->Nl = l;
  172. size_t n = c->num;
  173. if (n != 0) {
  174. if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
  175. memcpy(c->data + n, data, HASH_CBLOCK - n);
  176. HASH_BLOCK_DATA_ORDER(c->h, c->data, 1);
  177. n = HASH_CBLOCK - n;
  178. data += n;
  179. len -= n;
  180. c->num = 0;
  181. /* Keep |c->data| zeroed when unused. */
  182. memset(c->data, 0, HASH_CBLOCK);
  183. } else {
  184. memcpy(c->data + n, data, len);
  185. c->num += (unsigned)len;
  186. return 1;
  187. }
  188. }
  189. n = len / HASH_CBLOCK;
  190. if (n > 0) {
  191. HASH_BLOCK_DATA_ORDER(c->h, data, n);
  192. n *= HASH_CBLOCK;
  193. data += n;
  194. len -= n;
  195. }
  196. if (len != 0) {
  197. c->num = (unsigned)len;
  198. memcpy(c->data, data, len);
  199. }
  200. return 1;
  201. }
  202. void HASH_TRANSFORM(HASH_CTX *c, const uint8_t *data) {
  203. HASH_BLOCK_DATA_ORDER(c->h, data, 1);
  204. }
  205. int HASH_FINAL(uint8_t *md, HASH_CTX *c) {
  206. /* |c->data| always has room for at least one byte. A full block would have
  207. * been consumed. */
  208. size_t n = c->num;
  209. assert(n < HASH_CBLOCK);
  210. c->data[n] = 0x80;
  211. n++;
  212. /* Fill the block with zeros if there isn't room for a 64-bit length. */
  213. if (n > (HASH_CBLOCK - 8)) {
  214. memset(c->data + n, 0, HASH_CBLOCK - n);
  215. n = 0;
  216. HASH_BLOCK_DATA_ORDER(c->h, c->data, 1);
  217. }
  218. memset(c->data + n, 0, HASH_CBLOCK - 8 - n);
  219. /* Append a 64-bit length to the block and process it. */
  220. uint8_t *p = c->data + HASH_CBLOCK - 8;
  221. #if defined(DATA_ORDER_IS_BIG_ENDIAN)
  222. HOST_l2c(c->Nh, p);
  223. HOST_l2c(c->Nl, p);
  224. #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
  225. HOST_l2c(c->Nl, p);
  226. HOST_l2c(c->Nh, p);
  227. #endif
  228. assert(p == c->data + HASH_CBLOCK);
  229. HASH_BLOCK_DATA_ORDER(c->h, c->data, 1);
  230. c->num = 0;
  231. memset(c->data, 0, HASH_CBLOCK);
  232. HASH_MAKE_STRING(c, md);
  233. return 1;
  234. }
  235. #if defined(__cplusplus)
  236. } /* extern C */
  237. #endif
  238. #endif /* OPENSSL_HEADER_MD32_COMMON_H */