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