wp_dgst.c 6.9 KB

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