md32_common.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. #include <openssl/base.h>
  49. #include <assert.h>
  50. #include "../../internal.h"
  51. #if defined(__cplusplus)
  52. extern "C" {
  53. #endif
  54. // This is a generic 32-bit "collector" for message digest algorithms. It
  55. // collects input character stream into chunks of 32-bit values and invokes the
  56. // block function that performs the actual hash calculations. To make use of
  57. // this mechanism, the following macros must be defined before including
  58. // md32_common.h.
  59. //
  60. // One of |DATA_ORDER_IS_BIG_ENDIAN| or |DATA_ORDER_IS_LITTLE_ENDIAN| must be
  61. // defined to specify the byte order of the input stream.
  62. //
  63. // |HASH_CBLOCK| must be defined as the integer block size, in bytes.
  64. //
  65. // |HASH_CTX| must be defined as the name of the context structure, which must
  66. // have at least the following members:
  67. //
  68. // typedef struct <name>_state_st {
  69. // uint32_t h[<chaining length> / sizeof(uint32_t)];
  70. // uint32_t Nl, Nh;
  71. // uint8_t data[HASH_CBLOCK];
  72. // unsigned num;
  73. // ...
  74. // } <NAME>_CTX;
  75. //
  76. // <chaining length> is the output length of the hash in bytes, before
  77. // any truncation (e.g. 64 for SHA-224 and SHA-256, 128 for SHA-384 and
  78. // 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. #if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN)
  104. #error "DATA_ORDER must be defined!"
  105. #endif
  106. #ifndef HASH_CBLOCK
  107. #error "HASH_CBLOCK must be defined!"
  108. #endif
  109. #ifndef HASH_CTX
  110. #error "HASH_CTX must be defined!"
  111. #endif
  112. #ifndef HASH_UPDATE
  113. #error "HASH_UPDATE must be defined!"
  114. #endif
  115. #ifndef HASH_TRANSFORM
  116. #error "HASH_TRANSFORM must be defined!"
  117. #endif
  118. #ifndef HASH_FINAL
  119. #error "HASH_FINAL must be defined!"
  120. #endif
  121. #ifndef HASH_BLOCK_DATA_ORDER
  122. #error "HASH_BLOCK_DATA_ORDER must be defined!"
  123. #endif
  124. #ifndef HASH_MAKE_STRING
  125. #error "HASH_MAKE_STRING must be defined!"
  126. #endif
  127. #if defined(DATA_ORDER_IS_BIG_ENDIAN)
  128. #define HOST_c2l(c, l) \
  129. do { \
  130. (l) = (((uint32_t)(*((c)++))) << 24); \
  131. (l) |= (((uint32_t)(*((c)++))) << 16); \
  132. (l) |= (((uint32_t)(*((c)++))) << 8); \
  133. (l) |= (((uint32_t)(*((c)++)))); \
  134. } while (0)
  135. #define HOST_l2c(l, c) \
  136. do { \
  137. *((c)++) = (uint8_t)(((l) >> 24) & 0xff); \
  138. *((c)++) = (uint8_t)(((l) >> 16) & 0xff); \
  139. *((c)++) = (uint8_t)(((l) >> 8) & 0xff); \
  140. *((c)++) = (uint8_t)(((l)) & 0xff); \
  141. } while (0)
  142. #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
  143. #define HOST_c2l(c, l) \
  144. do { \
  145. (l) = (((uint32_t)(*((c)++)))); \
  146. (l) |= (((uint32_t)(*((c)++))) << 8); \
  147. (l) |= (((uint32_t)(*((c)++))) << 16); \
  148. (l) |= (((uint32_t)(*((c)++))) << 24); \
  149. } while (0)
  150. #define HOST_l2c(l, c) \
  151. do { \
  152. *((c)++) = (uint8_t)(((l)) & 0xff); \
  153. *((c)++) = (uint8_t)(((l) >> 8) & 0xff); \
  154. *((c)++) = (uint8_t)(((l) >> 16) & 0xff); \
  155. *((c)++) = (uint8_t)(((l) >> 24) & 0xff); \
  156. } while (0)
  157. #endif // DATA_ORDER
  158. int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len) {
  159. const uint8_t *data = data_;
  160. if (len == 0) {
  161. return 1;
  162. }
  163. uint32_t l = c->Nl + (((uint32_t)len) << 3);
  164. if (l < c->Nl) {
  165. // Handle carries.
  166. c->Nh++;
  167. }
  168. c->Nh += (uint32_t)(len >> 29);
  169. c->Nl = l;
  170. size_t n = c->num;
  171. if (n != 0) {
  172. if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
  173. OPENSSL_memcpy(c->data + n, data, HASH_CBLOCK - n);
  174. HASH_BLOCK_DATA_ORDER(c->h, c->data, 1);
  175. n = HASH_CBLOCK - n;
  176. data += n;
  177. len -= n;
  178. c->num = 0;
  179. // Keep |c->data| zeroed when unused.
  180. OPENSSL_memset(c->data, 0, HASH_CBLOCK);
  181. } else {
  182. OPENSSL_memcpy(c->data + n, data, len);
  183. c->num += (unsigned)len;
  184. return 1;
  185. }
  186. }
  187. n = len / HASH_CBLOCK;
  188. if (n > 0) {
  189. HASH_BLOCK_DATA_ORDER(c->h, data, n);
  190. n *= HASH_CBLOCK;
  191. data += n;
  192. len -= n;
  193. }
  194. if (len != 0) {
  195. c->num = (unsigned)len;
  196. OPENSSL_memcpy(c->data, data, len);
  197. }
  198. return 1;
  199. }
  200. void HASH_TRANSFORM(HASH_CTX *c, const uint8_t *data) {
  201. HASH_BLOCK_DATA_ORDER(c->h, data, 1);
  202. }
  203. int HASH_FINAL(uint8_t *md, HASH_CTX *c) {
  204. // |c->data| always has room for at least one byte. A full block would have
  205. // been consumed.
  206. size_t n = c->num;
  207. assert(n < HASH_CBLOCK);
  208. c->data[n] = 0x80;
  209. n++;
  210. // Fill the block with zeros if there isn't room for a 64-bit length.
  211. if (n > (HASH_CBLOCK - 8)) {
  212. OPENSSL_memset(c->data + n, 0, HASH_CBLOCK - n);
  213. n = 0;
  214. HASH_BLOCK_DATA_ORDER(c->h, c->data, 1);
  215. }
  216. OPENSSL_memset(c->data + n, 0, HASH_CBLOCK - 8 - n);
  217. // Append a 64-bit length to the block and process it.
  218. uint8_t *p = c->data + HASH_CBLOCK - 8;
  219. #if defined(DATA_ORDER_IS_BIG_ENDIAN)
  220. HOST_l2c(c->Nh, p);
  221. HOST_l2c(c->Nl, p);
  222. #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
  223. HOST_l2c(c->Nl, p);
  224. HOST_l2c(c->Nh, p);
  225. #endif
  226. assert(p == c->data + HASH_CBLOCK);
  227. HASH_BLOCK_DATA_ORDER(c->h, c->data, 1);
  228. c->num = 0;
  229. OPENSSL_memset(c->data, 0, HASH_CBLOCK);
  230. HASH_MAKE_STRING(c, md);
  231. return 1;
  232. }
  233. #if defined(__cplusplus)
  234. } // extern C
  235. #endif