wp_dgst.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**
  2. * The Whirlpool hashing function.
  3. *
  4. * <P>
  5. * <b>References</b>
  6. *
  7. * <P>
  8. * The Whirlpool algorithm was developed by
  9. * <a href="mailto:pbarreto@scopus.com.br">Paulo S. L. M. Barreto</a> and
  10. * <a href="mailto:vincent.rijmen@cryptomathic.com">Vincent Rijmen</a>.
  11. *
  12. * See
  13. * P.S.L.M. Barreto, V. Rijmen,
  14. * ``The Whirlpool hashing function,''
  15. * NESSIE submission, 2000 (tweaked version, 2001),
  16. * <https://www.cosic.esat.kuleuven.ac.be/nessie/workshop/submissions/whirlpool.zip>
  17. *
  18. * Based on "@version 3.0 (2003.03.12)" by Paulo S.L.M. Barreto and
  19. * Vincent Rijmen. Lookup "reference implementations" on
  20. * <http://planeta.terra.com.br/informatica/paulobarreto/>
  21. *
  22. * =============================================================================
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
  25. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  26. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  31. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  32. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  33. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  34. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. /*
  38. * OpenSSL-specific implementation notes.
  39. *
  40. * WHIRLPOOL_Update as well as one-stroke WHIRLPOOL both expect
  41. * number of *bytes* as input length argument. Bit-oriented routine
  42. * as specified by authors is called WHIRLPOOL_BitUpdate[!] and
  43. * does not have one-stroke counterpart.
  44. *
  45. * WHIRLPOOL_BitUpdate implements byte-oriented loop, essentially
  46. * to serve WHIRLPOOL_Update. This is done for performance.
  47. *
  48. * Unlike authors' reference implementation, block processing
  49. * routine whirlpool_block is designed to operate on multi-block
  50. * input. This is done for perfomance.
  51. */
  52. #include "wp_locl.h"
  53. #include <string.h>
  54. int WHIRLPOOL_Init(WHIRLPOOL_CTX *c)
  55. {
  56. memset(c, 0, sizeof(*c));
  57. return (1);
  58. }
  59. int WHIRLPOOL_Update(WHIRLPOOL_CTX *c, const void *_inp, size_t bytes)
  60. {
  61. /*
  62. * Well, largest suitable chunk size actually is
  63. * (1<<(sizeof(size_t)*8-3))-64, but below number is large enough for not
  64. * to care about excessive calls to WHIRLPOOL_BitUpdate...
  65. */
  66. size_t chunk = ((size_t)1) << (sizeof(size_t) * 8 - 4);
  67. const unsigned char *inp = _inp;
  68. while (bytes >= chunk) {
  69. WHIRLPOOL_BitUpdate(c, inp, chunk * 8);
  70. bytes -= chunk;
  71. inp += chunk;
  72. }
  73. if (bytes)
  74. WHIRLPOOL_BitUpdate(c, inp, bytes * 8);
  75. return (1);
  76. }
  77. void WHIRLPOOL_BitUpdate(WHIRLPOOL_CTX *c, const void *_inp, size_t bits)
  78. {
  79. size_t n;
  80. unsigned int bitoff = c->bitoff,
  81. bitrem = bitoff % 8, inpgap = (8 - (unsigned int)bits % 8) & 7;
  82. const unsigned char *inp = _inp;
  83. /*
  84. * This 256-bit increment procedure relies on the size_t being natural
  85. * size of CPU register, so that we don't have to mask the value in order
  86. * to detect overflows.
  87. */
  88. c->bitlen[0] += bits;
  89. if (c->bitlen[0] < bits) { /* overflow */
  90. n = 1;
  91. do {
  92. c->bitlen[n]++;
  93. } while (c->bitlen[n] == 0
  94. && ++n < (WHIRLPOOL_COUNTER / sizeof(size_t)));
  95. }
  96. #ifndef OPENSSL_SMALL_FOOTPRINT
  97. reconsider:
  98. if (inpgap == 0 && bitrem == 0) { /* byte-oriented loop */
  99. while (bits) {
  100. if (bitoff == 0 && (n = bits / WHIRLPOOL_BBLOCK)) {
  101. whirlpool_block(c, inp, n);
  102. inp += n * WHIRLPOOL_BBLOCK / 8;
  103. bits %= WHIRLPOOL_BBLOCK;
  104. } else {
  105. unsigned int byteoff = bitoff / 8;
  106. bitrem = WHIRLPOOL_BBLOCK - bitoff; /* re-use bitrem */
  107. if (bits >= bitrem) {
  108. bits -= bitrem;
  109. bitrem /= 8;
  110. memcpy(c->data + byteoff, inp, bitrem);
  111. inp += bitrem;
  112. whirlpool_block(c, c->data, 1);
  113. bitoff = 0;
  114. } else {
  115. memcpy(c->data + byteoff, inp, bits / 8);
  116. bitoff += (unsigned int)bits;
  117. bits = 0;
  118. }
  119. c->bitoff = bitoff;
  120. }
  121. }
  122. } else /* bit-oriented loop */
  123. #endif
  124. {
  125. /*-
  126. inp
  127. |
  128. +-------+-------+-------
  129. |||||||||||||||||||||
  130. +-------+-------+-------
  131. +-------+-------+-------+-------+-------
  132. |||||||||||||| c->data
  133. +-------+-------+-------+-------+-------
  134. |
  135. c->bitoff/8
  136. */
  137. while (bits) {
  138. unsigned int byteoff = bitoff / 8;
  139. unsigned char b;
  140. #ifndef OPENSSL_SMALL_FOOTPRINT
  141. if (bitrem == inpgap) {
  142. c->data[byteoff++] |= inp[0] & (0xff >> inpgap);
  143. inpgap = 8 - inpgap;
  144. bitoff += inpgap;
  145. bitrem = 0; /* bitoff%8 */
  146. bits -= inpgap;
  147. inpgap = 0; /* bits%8 */
  148. inp++;
  149. if (bitoff == WHIRLPOOL_BBLOCK) {
  150. whirlpool_block(c, c->data, 1);
  151. bitoff = 0;
  152. }
  153. c->bitoff = bitoff;
  154. goto reconsider;
  155. } else
  156. #endif
  157. if (bits >= 8) {
  158. b = ((inp[0] << inpgap) | (inp[1] >> (8 - inpgap)));
  159. b &= 0xff;
  160. if (bitrem)
  161. c->data[byteoff++] |= b >> bitrem;
  162. else
  163. c->data[byteoff++] = b;
  164. bitoff += 8;
  165. bits -= 8;
  166. inp++;
  167. if (bitoff >= WHIRLPOOL_BBLOCK) {
  168. whirlpool_block(c, c->data, 1);
  169. byteoff = 0;
  170. bitoff %= WHIRLPOOL_BBLOCK;
  171. }
  172. if (bitrem)
  173. c->data[byteoff] = b << (8 - bitrem);
  174. } else { /* remaining less than 8 bits */
  175. b = (inp[0] << inpgap) & 0xff;
  176. if (bitrem)
  177. c->data[byteoff++] |= b >> bitrem;
  178. else
  179. c->data[byteoff++] = b;
  180. bitoff += (unsigned int)bits;
  181. if (bitoff == WHIRLPOOL_BBLOCK) {
  182. whirlpool_block(c, c->data, 1);
  183. byteoff = 0;
  184. bitoff %= WHIRLPOOL_BBLOCK;
  185. }
  186. if (bitrem)
  187. c->data[byteoff] = b << (8 - bitrem);
  188. bits = 0;
  189. }
  190. c->bitoff = bitoff;
  191. }
  192. }
  193. }
  194. int WHIRLPOOL_Final(unsigned char *md, WHIRLPOOL_CTX *c)
  195. {
  196. unsigned int bitoff = c->bitoff, byteoff = bitoff / 8;
  197. size_t i, j, v;
  198. unsigned char *p;
  199. bitoff %= 8;
  200. if (bitoff)
  201. c->data[byteoff] |= 0x80 >> bitoff;
  202. else
  203. c->data[byteoff] = 0x80;
  204. byteoff++;
  205. /* pad with zeros */
  206. if (byteoff > (WHIRLPOOL_BBLOCK / 8 - WHIRLPOOL_COUNTER)) {
  207. if (byteoff < WHIRLPOOL_BBLOCK / 8)
  208. memset(&c->data[byteoff], 0, WHIRLPOOL_BBLOCK / 8 - byteoff);
  209. whirlpool_block(c, c->data, 1);
  210. byteoff = 0;
  211. }
  212. if (byteoff < (WHIRLPOOL_BBLOCK / 8 - WHIRLPOOL_COUNTER))
  213. memset(&c->data[byteoff], 0,
  214. (WHIRLPOOL_BBLOCK / 8 - WHIRLPOOL_COUNTER) - byteoff);
  215. /* smash 256-bit c->bitlen in big-endian order */
  216. p = &c->data[WHIRLPOOL_BBLOCK / 8 - 1]; /* last byte in c->data */
  217. for (i = 0; i < WHIRLPOOL_COUNTER / sizeof(size_t); i++)
  218. for (v = c->bitlen[i], j = 0; j < sizeof(size_t); j++, v >>= 8)
  219. *p-- = (unsigned char)(v & 0xff);
  220. whirlpool_block(c, c->data, 1);
  221. if (md) {
  222. memcpy(md, c->H.c, WHIRLPOOL_DIGEST_LENGTH);
  223. memset(c, 0, sizeof(*c));
  224. return (1);
  225. }
  226. return (0);
  227. }
  228. unsigned char *WHIRLPOOL(const void *inp, size_t bytes, unsigned char *md)
  229. {
  230. WHIRLPOOL_CTX ctx;
  231. static unsigned char m[WHIRLPOOL_DIGEST_LENGTH];
  232. if (md == NULL)
  233. md = m;
  234. WHIRLPOOL_Init(&ctx);
  235. WHIRLPOOL_Update(&ctx, inp, bytes);
  236. WHIRLPOOL_Final(md, &ctx);
  237. return (md);
  238. }