wp_dgst.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /**
  10. * The Whirlpool hashing function.
  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 performance.
  51. */
  52. #include <openssl/crypto.h>
  53. #include "wp_local.h"
  54. #include <string.h>
  55. int WHIRLPOOL_Init(WHIRLPOOL_CTX *c)
  56. {
  57. memset(c, 0, sizeof(*c));
  58. return 1;
  59. }
  60. int WHIRLPOOL_Update(WHIRLPOOL_CTX *c, const void *_inp, size_t bytes)
  61. {
  62. /*
  63. * Well, largest suitable chunk size actually is
  64. * (1<<(sizeof(size_t)*8-3))-64, but below number is large enough for not
  65. * to care about excessive calls to WHIRLPOOL_BitUpdate...
  66. */
  67. size_t chunk = ((size_t)1) << (sizeof(size_t) * 8 - 4);
  68. const unsigned char *inp = _inp;
  69. while (bytes >= chunk) {
  70. WHIRLPOOL_BitUpdate(c, inp, chunk * 8);
  71. bytes -= chunk;
  72. inp += chunk;
  73. }
  74. if (bytes)
  75. WHIRLPOOL_BitUpdate(c, inp, bytes * 8);
  76. return 1;
  77. }
  78. void WHIRLPOOL_BitUpdate(WHIRLPOOL_CTX *c, const void *_inp, size_t bits)
  79. {
  80. size_t n;
  81. unsigned int bitoff = c->bitoff,
  82. bitrem = bitoff % 8, inpgap = (8 - (unsigned int)bits % 8) & 7;
  83. const unsigned char *inp = _inp;
  84. /*
  85. * This 256-bit increment procedure relies on the size_t being natural
  86. * size of CPU register, so that we don't have to mask the value in order
  87. * to detect overflows.
  88. */
  89. c->bitlen[0] += bits;
  90. if (c->bitlen[0] < bits) { /* overflow */
  91. n = 1;
  92. do {
  93. c->bitlen[n]++;
  94. } while (c->bitlen[n] == 0
  95. && ++n < (WHIRLPOOL_COUNTER / sizeof(size_t)));
  96. }
  97. #ifndef OPENSSL_SMALL_FOOTPRINT
  98. reconsider:
  99. if (inpgap == 0 && bitrem == 0) { /* byte-oriented loop */
  100. while (bits) {
  101. if (bitoff == 0 && (n = bits / WHIRLPOOL_BBLOCK)) {
  102. whirlpool_block(c, inp, n);
  103. inp += n * WHIRLPOOL_BBLOCK / 8;
  104. bits %= WHIRLPOOL_BBLOCK;
  105. } else {
  106. unsigned int byteoff = bitoff / 8;
  107. bitrem = WHIRLPOOL_BBLOCK - bitoff; /* re-use bitrem */
  108. if (bits >= bitrem) {
  109. bits -= bitrem;
  110. bitrem /= 8;
  111. memcpy(c->data + byteoff, inp, bitrem);
  112. inp += bitrem;
  113. whirlpool_block(c, c->data, 1);
  114. bitoff = 0;
  115. } else {
  116. memcpy(c->data + byteoff, inp, bits / 8);
  117. bitoff += (unsigned int)bits;
  118. bits = 0;
  119. }
  120. c->bitoff = bitoff;
  121. }
  122. }
  123. } else /* bit-oriented loop */
  124. #endif
  125. {
  126. /*-
  127. inp
  128. |
  129. +-------+-------+-------
  130. |||||||||||||||||||||
  131. +-------+-------+-------
  132. +-------+-------+-------+-------+-------
  133. |||||||||||||| c->data
  134. +-------+-------+-------+-------+-------
  135. |
  136. c->bitoff/8
  137. */
  138. while (bits) {
  139. unsigned int byteoff = bitoff / 8;
  140. unsigned char b;
  141. #ifndef OPENSSL_SMALL_FOOTPRINT
  142. if (bitrem == inpgap) {
  143. c->data[byteoff++] |= inp[0] & (0xff >> inpgap);
  144. inpgap = 8 - inpgap;
  145. bitoff += inpgap;
  146. bitrem = 0; /* bitoff%8 */
  147. bits -= inpgap;
  148. inpgap = 0; /* bits%8 */
  149. inp++;
  150. if (bitoff == WHIRLPOOL_BBLOCK) {
  151. whirlpool_block(c, c->data, 1);
  152. bitoff = 0;
  153. }
  154. c->bitoff = bitoff;
  155. goto reconsider;
  156. } else
  157. #endif
  158. if (bits > 8) {
  159. b = ((inp[0] << inpgap) | (inp[1] >> (8 - inpgap)));
  160. b &= 0xff;
  161. if (bitrem)
  162. c->data[byteoff++] |= b >> bitrem;
  163. else
  164. c->data[byteoff++] = b;
  165. bitoff += 8;
  166. bits -= 8;
  167. inp++;
  168. if (bitoff >= WHIRLPOOL_BBLOCK) {
  169. whirlpool_block(c, c->data, 1);
  170. byteoff = 0;
  171. bitoff %= WHIRLPOOL_BBLOCK;
  172. }
  173. if (bitrem)
  174. c->data[byteoff] = b << (8 - bitrem);
  175. } else { /* remaining less than or equal to 8 bits */
  176. b = (inp[0] << inpgap) & 0xff;
  177. if (bitrem)
  178. c->data[byteoff++] |= b >> bitrem;
  179. else
  180. c->data[byteoff++] = b;
  181. bitoff += (unsigned int)bits;
  182. if (bitoff == WHIRLPOOL_BBLOCK) {
  183. whirlpool_block(c, c->data, 1);
  184. byteoff = 0;
  185. bitoff %= WHIRLPOOL_BBLOCK;
  186. }
  187. if (bitrem)
  188. c->data[byteoff] = b << (8 - bitrem);
  189. bits = 0;
  190. }
  191. c->bitoff = bitoff;
  192. }
  193. }
  194. }
  195. int WHIRLPOOL_Final(unsigned char *md, WHIRLPOOL_CTX *c)
  196. {
  197. unsigned int bitoff = c->bitoff, byteoff = bitoff / 8;
  198. size_t i, j, v;
  199. unsigned char *p;
  200. bitoff %= 8;
  201. if (bitoff)
  202. c->data[byteoff] |= 0x80 >> bitoff;
  203. else
  204. c->data[byteoff] = 0x80;
  205. byteoff++;
  206. /* pad with zeros */
  207. if (byteoff > (WHIRLPOOL_BBLOCK / 8 - WHIRLPOOL_COUNTER)) {
  208. if (byteoff < WHIRLPOOL_BBLOCK / 8)
  209. memset(&c->data[byteoff], 0, WHIRLPOOL_BBLOCK / 8 - byteoff);
  210. whirlpool_block(c, c->data, 1);
  211. byteoff = 0;
  212. }
  213. if (byteoff < (WHIRLPOOL_BBLOCK / 8 - WHIRLPOOL_COUNTER))
  214. memset(&c->data[byteoff], 0,
  215. (WHIRLPOOL_BBLOCK / 8 - WHIRLPOOL_COUNTER) - byteoff);
  216. /* smash 256-bit c->bitlen in big-endian order */
  217. p = &c->data[WHIRLPOOL_BBLOCK / 8 - 1]; /* last byte in c->data */
  218. for (i = 0; i < WHIRLPOOL_COUNTER / sizeof(size_t); i++)
  219. for (v = c->bitlen[i], j = 0; j < sizeof(size_t); j++, v >>= 8)
  220. *p-- = (unsigned char)(v & 0xff);
  221. whirlpool_block(c, c->data, 1);
  222. if (md) {
  223. memcpy(md, c->H.c, WHIRLPOOL_DIGEST_LENGTH);
  224. OPENSSL_cleanse(c, sizeof(*c));
  225. return 1;
  226. }
  227. return 0;
  228. }
  229. unsigned char *WHIRLPOOL(const void *inp, size_t bytes, unsigned char *md)
  230. {
  231. WHIRLPOOL_CTX ctx;
  232. static unsigned char m[WHIRLPOOL_DIGEST_LENGTH];
  233. if (md == NULL)
  234. md = m;
  235. WHIRLPOOL_Init(&ctx);
  236. WHIRLPOOL_Update(&ctx, inp, bytes);
  237. WHIRLPOOL_Final(md, &ctx);
  238. return md;
  239. }