Debian Codegraph @

cipher/hash-common.c

raw
1 /* hash-common.c - Common code for hash algorithms
2 * Copyright (C) 2008 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #ifdef HAVE_STDINT_H
25 # include <stdint.h>
26 #endif
27
28 #include "g10lib.h"
29 #include "bufhelp.h"
30 #include "hash-common.h"
31
32
33 /* Run a selftest for hash algorithm ALGO. If the resulting digest
34 matches EXPECT/EXPECTLEN and everything else is fine as well,
35 return NULL. If an error occurs, return a static text string
36 describing the error.
37
38 DATAMODE controls what will be hashed according to this table:
39
40 0 - Hash the supplied DATA of DATALEN.
41 1 - Hash one million times a 'a'. DATA and DATALEN are ignored.
42
43 */
44 const char *
45 _gcry_hash_selftest_check_one (int algo,
46 int datamode, const void *data, size_t datalen,
47 const void *expect, size_t expectlen)
48 {
49 const char *result = NULL;
50 gcry_error_t err = 0;
51 gcry_md_hd_t hd;
52 unsigned char *digest;
53 char aaa[1000];
54 int expect_xof = 0;
55
56 if (_gcry_md_get_algo_dlen (algo) != expectlen)
57 expect_xof = 1;
58
59 err = _gcry_md_open (&hd, algo, 0);
60 if (err)
61 return "gcry_md_open failed";
62
63 switch (datamode)
64 {
65 case 0:
66 _gcry_md_write (hd, data, datalen);
67 break;
68
69 case 1: /* Hash one million times an "a". */
70 {
71 int i;
72
73 /* Write in odd size chunks so that we test the buffering. */
74 memset (aaa, 'a', 1000);
75 for (i = 0; i < 1000; i++)
76 _gcry_md_write (hd, aaa, 1000);
77 }
78 break;
79
80 default:
81 result = "invalid DATAMODE";
82 }
83
84 if (!result)
85 {
86 if (!expect_xof)
87 {
88 digest = _gcry_md_read (hd, algo);
89
90 if ( memcmp (digest, expect, expectlen) )
91 result = "digest mismatch";
92 }
93 else
94 {
95 gcry_assert(expectlen <= sizeof(aaa));
96
97 err = _gcry_md_extract (hd, algo, aaa, expectlen);
98 if (err)
99 result = "error extracting output from XOF";
100 else if ( memcmp (aaa, expect, expectlen) )
101 result = "digest mismatch";
102 }
103 }
104
105 _gcry_md_close (hd);
106
107 return result;
108 }
109
110
111 /* Common function to write a chunk of data to the transform function
112 of a hash algorithm. Note that the use of the term "block" does
113 not imply a fixed size block. Note that we explicitly allow using
114 this function after the context has been finalized; the result does
115 not have any meaning but writing after finalize is sometimes
116 helpful to mitigate timing attacks. */
117 void
118 _gcry_md_block_write (void *context, const void *inbuf_arg, size_t inlen)
119 {
120 const unsigned char *inbuf = inbuf_arg;
121 gcry_md_block_ctx_t *hd = context;
122 unsigned int stack_burn = 0;
123 unsigned int nburn;
124 const unsigned int blocksize_shift = hd->blocksize_shift;
125 const unsigned int blocksize = 1 << blocksize_shift;
126 size_t inblocks;
127 size_t copylen;
128
129 if (sizeof(hd->buf) < blocksize)
130 BUG();
131
132 if (!hd->bwrite)
133 return;
134
135 if (hd->count > blocksize)
136 {
137 /* This happens only when gcry_md_write is called after final.
138 * Writing after final is used for mitigating timing attacks. */
139 hd->count = 0;
140 }
141
142 while (hd->count)
143 {
144 if (hd->count == blocksize) /* Flush the buffer. */
145 {
146 nburn = hd->bwrite (hd, hd->buf, 1);
147 stack_burn = nburn > stack_burn ? nburn : stack_burn;
148 hd->count = 0;
149 if (!++hd->nblocks)
150 hd->nblocks_high++;
151 }
152 else
153 {
154 copylen = inlen;
155 if (copylen > blocksize - hd->count)
156 copylen = blocksize - hd->count;
157
158 if (copylen == 0)
159 break;
160
161 buf_cpy (&hd->buf[hd->count], inbuf, copylen);
162 hd->count += copylen;
163 inbuf += copylen;
164 inlen -= copylen;
165 }
166 }
167
168 if (inlen == 0)
169 return;
170
171 if (inlen >= blocksize)
172 {
173 inblocks = inlen >> blocksize_shift;
174 nburn = hd->bwrite (hd, inbuf, inblocks);
175 stack_burn = nburn > stack_burn ? nburn : stack_burn;
176 hd->count = 0;
177 hd->nblocks_high += (hd->nblocks + inblocks < inblocks);
178 hd->nblocks += inblocks;
179 inlen -= inblocks << blocksize_shift;
180 inbuf += inblocks << blocksize_shift;
181 }
182
183 if (inlen)
184 {
185 buf_cpy (hd->buf, inbuf, inlen);
186 hd->count = inlen;
187 }
188
189 if (stack_burn > 0)
190 _gcry_burn_stack (stack_burn);
191 }