diff options
author | jsing <> | 2024-03-29 00:16:22 +0000 |
---|---|---|
committer | jsing <> | 2024-03-29 00:16:22 +0000 |
commit | c4ad6e34934eba0afe1f9f3a48e4e527b6eae0a7 (patch) | |
tree | ce1c0a5886b66578dfcade4e133605c30bc5c202 | |
parent | be6de3fee7ac3ea27ca201b42b1b2616f8ea7b5a (diff) | |
download | openbsd-c4ad6e34934eba0afe1f9f3a48e4e527b6eae0a7.tar.gz openbsd-c4ad6e34934eba0afe1f9f3a48e4e527b6eae0a7.tar.bz2 openbsd-c4ad6e34934eba0afe1f9f3a48e4e527b6eae0a7.zip |
Apply style(9) hammer.
The code is still a horrific mess, but at least the braces are in the right
place...
-rw-r--r-- | src/lib/libcrypto/whrlpool/wp_dgst.c | 235 |
1 files changed, 117 insertions, 118 deletions
diff --git a/src/lib/libcrypto/whrlpool/wp_dgst.c b/src/lib/libcrypto/whrlpool/wp_dgst.c index 71fd79c840..0e7c9c56d9 100644 --- a/src/lib/libcrypto/whrlpool/wp_dgst.c +++ b/src/lib/libcrypto/whrlpool/wp_dgst.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: wp_dgst.c,v 1.7 2023/09/04 08:43:41 tb Exp $ */ | 1 | /* $OpenBSD: wp_dgst.c,v 1.8 2024/03/29 00:16:22 jsing Exp $ */ |
2 | /** | 2 | /** |
3 | * The Whirlpool hashing function. | 3 | * The Whirlpool hashing function. |
4 | * | 4 | * |
@@ -58,92 +58,88 @@ | |||
58 | 58 | ||
59 | #include "wp_local.h" | 59 | #include "wp_local.h" |
60 | 60 | ||
61 | int WHIRLPOOL_Init(WHIRLPOOL_CTX *c) | 61 | int |
62 | { | 62 | WHIRLPOOL_Init(WHIRLPOOL_CTX *c) |
63 | memset (c,0,sizeof(*c)); | 63 | { |
64 | return(1); | 64 | memset (c, 0, sizeof(*c)); |
65 | } | 65 | return (1); |
66 | } | ||
66 | 67 | ||
67 | int WHIRLPOOL_Update (WHIRLPOOL_CTX *c,const void *_inp,size_t bytes) | 68 | int |
68 | { | 69 | WHIRLPOOL_Update(WHIRLPOOL_CTX *c, const void *_inp, size_t bytes) |
70 | { | ||
69 | /* Well, largest suitable chunk size actually is | 71 | /* Well, largest suitable chunk size actually is |
70 | * (1<<(sizeof(size_t)*8-3))-64, but below number | 72 | * (1<<(sizeof(size_t)*8-3))-64, but below number |
71 | * is large enough for not to care about excessive | 73 | * is large enough for not to care about excessive |
72 | * calls to WHIRLPOOL_BitUpdate... */ | 74 | * calls to WHIRLPOOL_BitUpdate... */ |
73 | size_t chunk = ((size_t)1)<<(sizeof(size_t)*8-4); | 75 | size_t chunk = ((size_t)1) << (sizeof(size_t)*8 - 4); |
74 | const unsigned char *inp = _inp; | 76 | const unsigned char *inp = _inp; |
75 | 77 | ||
76 | while (bytes>=chunk) | 78 | while (bytes >= chunk) { |
77 | { | 79 | WHIRLPOOL_BitUpdate(c, inp, chunk*8); |
78 | WHIRLPOOL_BitUpdate(c,inp,chunk*8); | ||
79 | bytes -= chunk; | 80 | bytes -= chunk; |
80 | inp += chunk; | 81 | inp += chunk; |
81 | } | 82 | } |
82 | if (bytes) | 83 | if (bytes) |
83 | WHIRLPOOL_BitUpdate(c,inp,bytes*8); | 84 | WHIRLPOOL_BitUpdate(c, inp, bytes*8); |
84 | 85 | ||
85 | return(1); | 86 | return (1); |
86 | } | 87 | } |
87 | 88 | ||
88 | void WHIRLPOOL_BitUpdate(WHIRLPOOL_CTX *c,const void *_inp,size_t bits) | 89 | void |
89 | { | 90 | WHIRLPOOL_BitUpdate(WHIRLPOOL_CTX *c, const void *_inp, size_t bits) |
91 | { | ||
90 | size_t n; | 92 | size_t n; |
91 | unsigned int bitoff = c->bitoff, | 93 | unsigned int bitoff = c->bitoff, |
92 | bitrem = bitoff%8, | 94 | bitrem = bitoff % 8, |
93 | inpgap = (8-(unsigned int)bits%8)&7; | 95 | inpgap = (8 - (unsigned int)bits % 8)&7; |
94 | const unsigned char *inp=_inp; | 96 | const unsigned char *inp = _inp; |
95 | 97 | ||
96 | /* This 256-bit increment procedure relies on the size_t | 98 | /* This 256-bit increment procedure relies on the size_t |
97 | * being natural size of CPU register, so that we don't | 99 | * being natural size of CPU register, so that we don't |
98 | * have to mask the value in order to detect overflows. */ | 100 | * have to mask the value in order to detect overflows. */ |
99 | c->bitlen[0] += bits; | 101 | c->bitlen[0] += bits; |
100 | if (c->bitlen[0] < bits) /* overflow */ | 102 | if (c->bitlen[0] < bits) /* overflow */ |
101 | { | 103 | { |
102 | n = 1; | 104 | n = 1; |
103 | do { c->bitlen[n]++; | 105 | do { |
104 | } while(c->bitlen[n]==0 | 106 | c->bitlen[n]++; |
105 | && ++n<(WHIRLPOOL_COUNTER/sizeof(size_t))); | 107 | } while (c->bitlen[n]==0 && |
106 | } | 108 | ++n < (WHIRLPOOL_COUNTER/sizeof(size_t))); |
109 | } | ||
107 | 110 | ||
108 | #ifndef OPENSSL_SMALL_FOOTPRINT | 111 | #ifndef OPENSSL_SMALL_FOOTPRINT |
109 | reconsider: | 112 | reconsider: |
110 | if (inpgap==0 && bitrem==0) /* byte-oriented loop */ | 113 | if (inpgap==0 && bitrem==0) /* byte-oriented loop */ |
111 | { | 114 | { |
112 | while (bits) | 115 | while (bits) { |
113 | { | 116 | if (bitoff == 0 && (n = bits/WHIRLPOOL_BBLOCK)) { |
114 | if (bitoff==0 && (n=bits/WHIRLPOOL_BBLOCK)) | 117 | whirlpool_block(c, inp, n); |
115 | { | 118 | inp += n*WHIRLPOOL_BBLOCK/8; |
116 | whirlpool_block(c,inp,n); | ||
117 | inp += n*WHIRLPOOL_BBLOCK/8; | ||
118 | bits %= WHIRLPOOL_BBLOCK; | 119 | bits %= WHIRLPOOL_BBLOCK; |
119 | } | 120 | } else { |
120 | else | ||
121 | { | ||
122 | unsigned int byteoff = bitoff/8; | 121 | unsigned int byteoff = bitoff/8; |
123 | 122 | ||
124 | bitrem = WHIRLPOOL_BBLOCK - bitoff;/* re-use bitrem */ | 123 | bitrem = WHIRLPOOL_BBLOCK - bitoff;/* re-use bitrem */ |
125 | if (bits >= bitrem) | 124 | if (bits >= bitrem) { |
126 | { | ||
127 | bits -= bitrem; | 125 | bits -= bitrem; |
128 | bitrem /= 8; | 126 | bitrem /= 8; |
129 | memcpy(c->data+byteoff,inp,bitrem); | 127 | memcpy(c->data + byteoff, inp, bitrem); |
130 | inp += bitrem; | 128 | inp += bitrem; |
131 | whirlpool_block(c,c->data,1); | 129 | whirlpool_block(c, c->data, 1); |
132 | bitoff = 0; | 130 | bitoff = 0; |
133 | } | 131 | } else { |
134 | else | 132 | memcpy(c->data + byteoff, inp, bits/8); |
135 | { | ||
136 | memcpy(c->data+byteoff,inp,bits/8); | ||
137 | bitoff += (unsigned int)bits; | 133 | bitoff += (unsigned int)bits; |
138 | bits = 0; | 134 | bits = 0; |
139 | } | ||
140 | c->bitoff = bitoff; | ||
141 | } | 135 | } |
136 | c->bitoff = bitoff; | ||
142 | } | 137 | } |
143 | } | 138 | } |
139 | } | ||
144 | else /* bit-oriented loop */ | 140 | else /* bit-oriented loop */ |
145 | #endif | 141 | #endif |
146 | { | 142 | { |
147 | /* | 143 | /* |
148 | inp | 144 | inp |
149 | | | 145 | | |
@@ -156,113 +152,116 @@ void WHIRLPOOL_BitUpdate(WHIRLPOOL_CTX *c,const void *_inp,size_t bits) | |||
156 | | | 152 | | |
157 | c->bitoff/8 | 153 | c->bitoff/8 |
158 | */ | 154 | */ |
159 | while (bits) | 155 | while (bits) { |
160 | { | 156 | unsigned int byteoff = bitoff/8; |
161 | unsigned int byteoff = bitoff/8; | ||
162 | unsigned char b; | 157 | unsigned char b; |
163 | 158 | ||
164 | #ifndef OPENSSL_SMALL_FOOTPRINT | 159 | #ifndef OPENSSL_SMALL_FOOTPRINT |
165 | if (bitrem==inpgap) | 160 | if (bitrem == inpgap) { |
166 | { | 161 | c->data[byteoff++] |= inp[0] & (0xff >> inpgap); |
167 | c->data[byteoff++] |= inp[0] & (0xff>>inpgap); | 162 | inpgap = 8 - inpgap; |
168 | inpgap = 8-inpgap; | ||
169 | bitoff += inpgap; bitrem = 0; /* bitoff%8 */ | 163 | bitoff += inpgap; bitrem = 0; /* bitoff%8 */ |
170 | bits -= inpgap; inpgap = 0; /* bits%8 */ | 164 | bits -= inpgap; inpgap = 0; /* bits%8 */ |
171 | inp++; | 165 | inp++; |
172 | if (bitoff==WHIRLPOOL_BBLOCK) | 166 | if (bitoff == WHIRLPOOL_BBLOCK) { |
173 | { | 167 | whirlpool_block(c, c->data, 1); |
174 | whirlpool_block(c,c->data,1); | ||
175 | bitoff = 0; | 168 | bitoff = 0; |
176 | } | 169 | } |
177 | c->bitoff = bitoff; | 170 | c->bitoff = bitoff; |
178 | goto reconsider; | 171 | goto reconsider; |
179 | } | 172 | } else |
180 | else | ||
181 | #endif | 173 | #endif |
182 | if (bits>=8) | 174 | if (bits >= 8) { |
183 | { | 175 | b = ((inp[0]<<inpgap) | (inp[1]>>(8 - inpgap))); |
184 | b = ((inp[0]<<inpgap) | (inp[1]>>(8-inpgap))); | ||
185 | b &= 0xff; | 176 | b &= 0xff; |
186 | if (bitrem) c->data[byteoff++] |= b>>bitrem; | 177 | if (bitrem) |
187 | else c->data[byteoff++] = b; | 178 | c->data[byteoff++] |= b >> bitrem; |
179 | else | ||
180 | c->data[byteoff++] = b; | ||
188 | bitoff += 8; | 181 | bitoff += 8; |
189 | bits -= 8; | 182 | bits -= 8; |
190 | inp++; | 183 | inp++; |
191 | if (bitoff>=WHIRLPOOL_BBLOCK) | 184 | if (bitoff >= WHIRLPOOL_BBLOCK) { |
192 | { | 185 | whirlpool_block(c, c->data, 1); |
193 | whirlpool_block(c,c->data,1); | 186 | byteoff = 0; |
194 | byteoff = 0; | ||
195 | bitoff %= WHIRLPOOL_BBLOCK; | 187 | bitoff %= WHIRLPOOL_BBLOCK; |
196 | } | ||
197 | if (bitrem) c->data[byteoff] = b<<(8-bitrem); | ||
198 | } | 188 | } |
189 | if (bitrem) | ||
190 | c->data[byteoff] = b << (8 - bitrem); | ||
191 | } | ||
199 | else /* remaining less than 8 bits */ | 192 | else /* remaining less than 8 bits */ |
200 | { | 193 | { |
201 | b = (inp[0]<<inpgap)&0xff; | 194 | b = (inp[0]<<inpgap)&0xff; |
202 | if (bitrem) c->data[byteoff++] |= b>>bitrem; | 195 | if (bitrem) |
203 | else c->data[byteoff++] = b; | 196 | c->data[byteoff++] |= b >> bitrem; |
197 | else | ||
198 | c->data[byteoff++] = b; | ||
204 | bitoff += (unsigned int)bits; | 199 | bitoff += (unsigned int)bits; |
205 | if (bitoff==WHIRLPOOL_BBLOCK) | 200 | if (bitoff == WHIRLPOOL_BBLOCK) { |
206 | { | 201 | whirlpool_block(c, c->data, 1); |
207 | whirlpool_block(c,c->data,1); | 202 | byteoff = 0; |
208 | byteoff = 0; | 203 | bitoff %= WHIRLPOOL_BBLOCK; |
209 | bitoff %= WHIRLPOOL_BBLOCK; | ||
210 | } | ||
211 | if (bitrem) c->data[byteoff] = b<<(8-bitrem); | ||
212 | bits = 0; | ||
213 | } | 204 | } |
214 | c->bitoff = bitoff; | 205 | if (bitrem) |
206 | c->data[byteoff] = b << (8 - bitrem); | ||
207 | bits = 0; | ||
215 | } | 208 | } |
209 | c->bitoff = bitoff; | ||
216 | } | 210 | } |
217 | } | 211 | } |
212 | } | ||
218 | 213 | ||
219 | int WHIRLPOOL_Final (unsigned char *md,WHIRLPOOL_CTX *c) | 214 | int |
220 | { | 215 | WHIRLPOOL_Final(unsigned char *md, WHIRLPOOL_CTX *c) |
221 | unsigned int bitoff = c->bitoff, | 216 | { |
222 | byteoff = bitoff/8; | 217 | unsigned int bitoff = c->bitoff, |
223 | size_t i,j,v; | 218 | byteoff = bitoff/8; |
219 | size_t i, j, v; | ||
224 | unsigned char *p; | 220 | unsigned char *p; |
225 | 221 | ||
226 | bitoff %= 8; | 222 | bitoff %= 8; |
227 | if (bitoff) c->data[byteoff] |= 0x80>>bitoff; | 223 | if (bitoff) |
228 | else c->data[byteoff] = 0x80; | 224 | c->data[byteoff] |= 0x80 >> bitoff; |
225 | else | ||
226 | c->data[byteoff] = 0x80; | ||
229 | byteoff++; | 227 | byteoff++; |
230 | 228 | ||
231 | /* pad with zeros */ | 229 | /* pad with zeros */ |
232 | if (byteoff > (WHIRLPOOL_BBLOCK/8-WHIRLPOOL_COUNTER)) | 230 | if (byteoff > (WHIRLPOOL_BBLOCK/8 - WHIRLPOOL_COUNTER)) { |
233 | { | 231 | if (byteoff < WHIRLPOOL_BBLOCK/8) |
234 | if (byteoff<WHIRLPOOL_BBLOCK/8) | 232 | memset(&c->data[byteoff], 0, WHIRLPOOL_BBLOCK/8 - byteoff); |
235 | memset(&c->data[byteoff],0,WHIRLPOOL_BBLOCK/8-byteoff); | 233 | whirlpool_block(c, c->data, 1); |
236 | whirlpool_block(c,c->data,1); | ||
237 | byteoff = 0; | 234 | byteoff = 0; |
238 | } | 235 | } |
239 | if (byteoff < (WHIRLPOOL_BBLOCK/8-WHIRLPOOL_COUNTER)) | 236 | if (byteoff < (WHIRLPOOL_BBLOCK/8 - WHIRLPOOL_COUNTER)) |
240 | memset(&c->data[byteoff],0, | 237 | memset(&c->data[byteoff], 0, |
241 | (WHIRLPOOL_BBLOCK/8-WHIRLPOOL_COUNTER)-byteoff); | 238 | (WHIRLPOOL_BBLOCK/8 - WHIRLPOOL_COUNTER) - byteoff); |
242 | /* smash 256-bit c->bitlen in big-endian order */ | 239 | /* smash 256-bit c->bitlen in big-endian order */ |
243 | p = &c->data[WHIRLPOOL_BBLOCK/8-1]; /* last byte in c->data */ | 240 | p = &c->data[WHIRLPOOL_BBLOCK/8-1]; /* last byte in c->data */ |
244 | for(i=0;i<WHIRLPOOL_COUNTER/sizeof(size_t);i++) | 241 | for (i = 0; i < WHIRLPOOL_COUNTER/sizeof(size_t); i++) |
245 | for(v=c->bitlen[i],j=0;j<sizeof(size_t);j++,v>>=8) | 242 | for (v = c->bitlen[i], j = 0; j < sizeof(size_t); j++, v >>= 8) |
246 | *p-- = (unsigned char)(v&0xff); | 243 | *p-- = (unsigned char)(v&0xff); |
247 | 244 | ||
248 | whirlpool_block(c,c->data,1); | 245 | whirlpool_block(c, c->data, 1); |
249 | 246 | ||
250 | if (md) { | 247 | if (md) { |
251 | memcpy(md,c->H.c,WHIRLPOOL_DIGEST_LENGTH); | 248 | memcpy(md, c->H.c, WHIRLPOOL_DIGEST_LENGTH); |
252 | memset(c,0,sizeof(*c)); | 249 | memset(c, 0, sizeof(*c)); |
253 | return(1); | 250 | return (1); |
254 | } | ||
255 | return(0); | ||
256 | } | 251 | } |
252 | return (0); | ||
253 | } | ||
257 | 254 | ||
258 | unsigned char *WHIRLPOOL(const void *inp, size_t bytes,unsigned char *md) | 255 | unsigned char * |
259 | { | 256 | WHIRLPOOL(const void *inp, size_t bytes, unsigned char *md) |
257 | { | ||
260 | WHIRLPOOL_CTX ctx; | 258 | WHIRLPOOL_CTX ctx; |
261 | static unsigned char m[WHIRLPOOL_DIGEST_LENGTH]; | 259 | static unsigned char m[WHIRLPOOL_DIGEST_LENGTH]; |
262 | 260 | ||
263 | if (md == NULL) md=m; | 261 | if (md == NULL) |
262 | md = m; | ||
264 | WHIRLPOOL_Init(&ctx); | 263 | WHIRLPOOL_Init(&ctx); |
265 | WHIRLPOOL_Update(&ctx,inp,bytes); | 264 | WHIRLPOOL_Update(&ctx, inp, bytes); |
266 | WHIRLPOOL_Final(md,&ctx); | 265 | WHIRLPOOL_Final(md, &ctx); |
267 | return(md); | 266 | return (md); |
268 | } | 267 | } |