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