diff options
| author | jsing <> | 2023-05-28 13:53:08 +0000 |
|---|---|---|
| committer | jsing <> | 2023-05-28 13:53:08 +0000 |
| commit | 525264cba68648274a8a172fb85bb140cf4c001d (patch) | |
| tree | 4e3fb8d27480aade966148edbfdebf083160053d /src | |
| parent | 5e6b0540238c981b8b8d04ce22ec411533860222 (diff) | |
| download | openbsd-525264cba68648274a8a172fb85bb140cf4c001d.tar.gz openbsd-525264cba68648274a8a172fb85bb140cf4c001d.tar.bz2 openbsd-525264cba68648274a8a172fb85bb140cf4c001d.zip | |
Inline HASH_MAKE_STRING for SHA256.
No change to generated assembly.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libcrypto/sha/sha256.c | 71 |
1 files changed, 37 insertions, 34 deletions
diff --git a/src/lib/libcrypto/sha/sha256.c b/src/lib/libcrypto/sha/sha256.c index d1a16c0446..689ee89454 100644 --- a/src/lib/libcrypto/sha/sha256.c +++ b/src/lib/libcrypto/sha/sha256.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: sha256.c,v 1.16 2023/05/27 18:39:03 jsing Exp $ */ | 1 | /* $OpenBSD: sha256.c,v 1.17 2023/05/28 13:53:08 jsing Exp $ */ |
| 2 | /* ==================================================================== | 2 | /* ==================================================================== |
| 3 | * Copyright (c) 1998-2011 The OpenSSL Project. All rights reserved. | 3 | * Copyright (c) 1998-2011 The OpenSSL Project. All rights reserved. |
| 4 | * | 4 | * |
| @@ -155,34 +155,6 @@ SHA224_Final(unsigned char *md, SHA256_CTX *c) | |||
| 155 | #define HASH_LONG SHA_LONG | 155 | #define HASH_LONG SHA_LONG |
| 156 | #define HASH_CTX SHA256_CTX | 156 | #define HASH_CTX SHA256_CTX |
| 157 | #define HASH_CBLOCK SHA_CBLOCK | 157 | #define HASH_CBLOCK SHA_CBLOCK |
| 158 | /* | ||
| 159 | * Note that FIPS180-2 discusses "Truncation of the Hash Function Output." | ||
| 160 | * default: case below covers for it. It's not clear however if it's | ||
| 161 | * permitted to truncate to amount of bytes not divisible by 4. I bet not, | ||
| 162 | * but if it is, then default: case shall be extended. For reference. | ||
| 163 | * Idea behind separate cases for pre-defined lengths is to let the | ||
| 164 | * compiler decide if it's appropriate to unroll small loops. | ||
| 165 | */ | ||
| 166 | #define HASH_MAKE_STRING(c, s) do { \ | ||
| 167 | unsigned long ll; \ | ||
| 168 | unsigned int nn; \ | ||
| 169 | switch ((c)->md_len) \ | ||
| 170 | { case SHA224_DIGEST_LENGTH: \ | ||
| 171 | for (nn=0;nn<SHA224_DIGEST_LENGTH/4;nn++) \ | ||
| 172 | { ll=(c)->h[nn]; HOST_l2c(ll,(s)); } \ | ||
| 173 | break; \ | ||
| 174 | case SHA256_DIGEST_LENGTH: \ | ||
| 175 | for (nn=0;nn<SHA256_DIGEST_LENGTH/4;nn++) \ | ||
| 176 | { ll=(c)->h[nn]; HOST_l2c(ll,(s)); } \ | ||
| 177 | break; \ | ||
| 178 | default: \ | ||
| 179 | if ((c)->md_len > SHA256_DIGEST_LENGTH) \ | ||
| 180 | return 0; \ | ||
| 181 | for (nn=0;nn<(c)->md_len/4;nn++) \ | ||
| 182 | { ll=(c)->h[nn]; HOST_l2c(ll,(s)); } \ | ||
| 183 | break; \ | ||
| 184 | } \ | ||
| 185 | } while (0) | ||
| 186 | 158 | ||
| 187 | #define HASH_BLOCK_DATA_ORDER sha256_block_data_order | 159 | #define HASH_BLOCK_DATA_ORDER sha256_block_data_order |
| 188 | #ifndef SHA256_ASM | 160 | #ifndef SHA256_ASM |
| @@ -261,6 +233,8 @@ SHA256_Final(unsigned char *md, HASH_CTX *c) | |||
| 261 | { | 233 | { |
| 262 | unsigned char *p = (unsigned char *)c->data; | 234 | unsigned char *p = (unsigned char *)c->data; |
| 263 | size_t n = c->num; | 235 | size_t n = c->num; |
| 236 | unsigned long ll; | ||
| 237 | unsigned int nn; | ||
| 264 | 238 | ||
| 265 | p[n] = 0x80; /* there is always room for one */ | 239 | p[n] = 0x80; /* there is always room for one */ |
| 266 | n++; | 240 | n++; |
| @@ -285,11 +259,40 @@ SHA256_Final(unsigned char *md, HASH_CTX *c) | |||
| 285 | c->num = 0; | 259 | c->num = 0; |
| 286 | memset (p, 0, SHA_CBLOCK); | 260 | memset (p, 0, SHA_CBLOCK); |
| 287 | 261 | ||
| 288 | #ifndef HASH_MAKE_STRING | 262 | /* |
| 289 | #error "HASH_MAKE_STRING must be defined!" | 263 | * Note that FIPS180-2 discusses "Truncation of the Hash Function Output." |
| 290 | #else | 264 | * default: case below covers for it. It's not clear however if it's |
| 291 | HASH_MAKE_STRING(c, md); | 265 | * permitted to truncate to amount of bytes not divisible by 4. I bet not, |
| 292 | #endif | 266 | * but if it is, then default: case shall be extended. For reference. |
| 267 | * Idea behind separate cases for pre-defined lengths is to let the | ||
| 268 | * compiler decide if it's appropriate to unroll small loops. | ||
| 269 | */ | ||
| 270 | do { | ||
| 271 | switch (c->md_len) { | ||
| 272 | case SHA224_DIGEST_LENGTH: | ||
| 273 | for (nn = 0; nn < SHA224_DIGEST_LENGTH / 4; nn++) { | ||
| 274 | ll = c->h[nn]; | ||
| 275 | HOST_l2c(ll, md); | ||
| 276 | } | ||
| 277 | break; | ||
| 278 | |||
| 279 | case SHA256_DIGEST_LENGTH: | ||
| 280 | for (nn = 0; nn < SHA256_DIGEST_LENGTH / 4; nn++) { | ||
| 281 | ll = c->h[nn]; | ||
| 282 | HOST_l2c(ll, md); | ||
| 283 | } | ||
| 284 | break; | ||
| 285 | |||
| 286 | default: | ||
| 287 | if (c->md_len > SHA256_DIGEST_LENGTH) | ||
| 288 | return 0; | ||
| 289 | for (nn = 0; nn < c->md_len / 4; nn++) { | ||
| 290 | ll = c->h[nn]; | ||
| 291 | HOST_l2c(ll, md); | ||
| 292 | } | ||
| 293 | break; | ||
| 294 | } | ||
| 295 | } while (0); | ||
| 293 | 296 | ||
| 294 | return 1; | 297 | return 1; |
| 295 | } | 298 | } |
