wp_dgst.c 8.4 KB

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