Sync SHA2 changes from OpenBSD This adds support for SHA224 and SHA512-256.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
diff --git a/include/sha2.h b/include/sha2.h
index 0387ea3..f8cb14a 100644
--- a/include/sha2.h
+++ b/include/sha2.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: sha2.h,v 1.6 2004/06/22 01:57:30 jfb Exp $ */
+/* $OpenBSD: sha2.h,v 1.10 2016/09/03 17:00:29 tedu Exp $ */
/*
* FILE: sha2.h
@@ -41,7 +41,10 @@
#include <stdint.h>
-/*** SHA-256/384/512 Various Length Definitions ***********************/
+/*** SHA-224/256/384/512 Various Length Definitions ***********************/
+#define SHA224_BLOCK_LENGTH 64
+#define SHA224_DIGEST_LENGTH 28
+#define SHA224_DIGEST_STRING_LENGTH (SHA224_DIGEST_LENGTH * 2 + 1)
#define SHA256_BLOCK_LENGTH 64
#define SHA256_DIGEST_LENGTH 32
#define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
@@ -51,9 +54,12 @@
#define SHA512_BLOCK_LENGTH 128
#define SHA512_DIGEST_LENGTH 64
#define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1)
+#define SHA512_256_BLOCK_LENGTH 128
+#define SHA512_256_DIGEST_LENGTH 32
+#define SHA512_256_DIGEST_STRING_LENGTH (SHA512_256_DIGEST_LENGTH * 2 + 1)
-/*** SHA-256/384/512 Context Structure *******************************/
+/*** SHA-224/256/384/512 Context Structure *******************************/
typedef struct _SHA2_CTX {
union {
uint32_t st32[8];
@@ -67,6 +73,16 @@ typedef struct _SHA2_CTX {
extern "C" {
#endif
+void SHA224Init(SHA2_CTX *);
+void SHA224Transform(uint32_t state[8], const uint8_t [SHA224_BLOCK_LENGTH]);
+void SHA224Update(SHA2_CTX *, const uint8_t *, size_t);
+void SHA224Pad(SHA2_CTX *);
+void SHA224Final(uint8_t [SHA224_DIGEST_LENGTH], SHA2_CTX *);
+char *SHA224End(SHA2_CTX *, char *);
+char *SHA224File(const char *, char *);
+char *SHA224FileChunk(const char *, char *, off_t, off_t);
+char *SHA224Data(const uint8_t *, size_t, char *);
+
void SHA256Init(SHA2_CTX *);
void SHA256Transform(uint32_t state[8], const uint8_t [SHA256_BLOCK_LENGTH]);
void SHA256Update(SHA2_CTX *, const uint8_t *, size_t);
@@ -97,6 +113,16 @@ char *SHA512File(const char *, char *);
char *SHA512FileChunk(const char *, char *, off_t, off_t);
char *SHA512Data(const uint8_t *, size_t, char *);
+void SHA512_256Init(SHA2_CTX *);
+void SHA512_256Transform(uint64_t state[8], const uint8_t [SHA512_256_BLOCK_LENGTH]);
+void SHA512_256Update(SHA2_CTX *, const uint8_t *, size_t);
+void SHA512_256Pad(SHA2_CTX *);
+void SHA512_256Final(uint8_t [SHA512_256_DIGEST_LENGTH], SHA2_CTX *);
+char *SHA512_256End(SHA2_CTX *, char *);
+char *SHA512_256File(const char *, char *);
+char *SHA512_256FileChunk(const char *, char *, off_t, off_t);
+char *SHA512_256Data(const uint8_t *, size_t, char *);
+
#ifdef __cplusplus
}
#endif
diff --git a/src/Makefile.am b/src/Makefile.am
index d9363a9..85b385c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -19,9 +19,11 @@ libmd_la_helper_sources = \
md5hl.c \
rmd160hl.c \
sha1hl.c \
+ sha224hl.c \
sha256hl.c \
sha384hl.c \
sha512hl.c \
+ sha512_256hl.c \
$(nil)
EXTRA_libmd_la_DEPENDENCIES = \
@@ -74,7 +76,7 @@ DISTCLEANFILES = \
# Generate a simple libtool symbol export list to be used as a fallback if
# there is no version script support.
libmd.sym: libmd.map
- $(AM_V_GEN) $(SED) -ne 's/^[[:space:]]\{1,\}\([A-Za-z0-9]\{1,\}\);/\1/p' libmd.map > $@
+ $(AM_V_GEN) $(SED) -ne 's/^[[:space:]]\{1,\}\([A-Za-z0-9_]\{1,\}\);/\1/p' libmd.map > $@
md2hl.c: helper.c
$(AM_V_GEN) $(SED) -e 's/hashinc/md2.h/g' -e 's/HASH/MD2/g' helper.c > $@
@@ -91,6 +93,10 @@ rmd160hl.c: helper.c
sha1hl.c: helper.c
$(AM_V_GEN) $(SED) -e 's/hashinc/sha1.h/g' -e 's/HASH/SHA1/g' helper.c > $@
+sha224hl.c: helper.c
+ $(AM_V_GEN) $(SED) -e 's/hashinc/sha2.h/g' -e 's/HASH/SHA224/g' \
+ -e 's/SHA[0-9][0-9][0-9]_CTX/SHA2_CTX/g' helper.c > $@
+
sha256hl.c: helper.c
$(AM_V_GEN) $(SED) -e 's/hashinc/sha2.h/g' -e 's/HASH/SHA256/g' \
-e 's/SHA[0-9][0-9][0-9]_CTX/SHA2_CTX/g' helper.c > $@
@@ -103,6 +109,10 @@ sha512hl.c: helper.c
$(AM_V_GEN) $(SED) -e 's/hashinc/sha2.h/g' -e 's/HASH/SHA512/g' \
-e 's/SHA[0-9][0-9][0-9]_CTX/SHA2_CTX/g' helper.c > $@
+sha512_256hl.c: helper.c
+ $(AM_V_GEN) $(SED) -e 's/hashinc/sha2.h/g' -e 's/HASH/SHA512_256/g' \
+ -e 's/SHA512_256_CTX/SHA2_CTX/g' helper.c > $@
+
runtimelibdir = $(libdir)
install-exec-hook:
diff --git a/src/libmd.map b/src/libmd.map
index fd4d579..6689cb8 100644
--- a/src/libmd.map
+++ b/src/libmd.map
@@ -82,3 +82,26 @@ global:
local:
*;
};
+
+LIBMD_0.1 {
+global:
+ SHA224Data;
+ SHA224End;
+ SHA224File;
+ SHA224FileChunk;
+ SHA224Final;
+ SHA224Init;
+ SHA224Pad;
+ SHA224Transform;
+ SHA224Update;
+
+ SHA512_256Data;
+ SHA512_256End;
+ SHA512_256File;
+ SHA512_256FileChunk;
+ SHA512_256Final;
+ SHA512_256Init;
+ SHA512_256Pad;
+ SHA512_256Transform;
+ SHA512_256Update;
+} LIBMD_0.0;
diff --git a/src/sha2.c b/src/sha2.c
index 5ffa883..ed72df6 100644
--- a/src/sha2.c
+++ b/src/sha2.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sha2.c,v 1.12 2008/09/06 12:00:19 djm Exp $ */
+/* $OpenBSD: sha2.c,v 1.28 2019/07/23 12:35:22 dtucker Exp $ */
/*
* FILE: sha2.c
@@ -55,9 +55,15 @@
* #define SHA2_UNROLL_TRANSFORM
*
*/
+#ifndef SHA2_SMALL
+#if defined(__amd64__) || defined(__i386__)
+#define SHA2_UNROLL_TRANSFORM
+#endif
+#endif
-/*** SHA-256/384/512 Various Length Definitions ***********************/
+/*** SHA-224/256/384/512 Various Length Definitions ***********************/
/* NOTE: Most of these are in sha2.h */
+#define SHA224_SHORT_BLOCK_LENGTH (SHA224_BLOCK_LENGTH - 8)
#define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8)
#define SHA384_SHORT_BLOCK_LENGTH (SHA384_BLOCK_LENGTH - 16)
#define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16)
@@ -110,22 +116,22 @@
* Bit shifting and rotation (used by the six SHA-XYZ logical functions:
*
* NOTE: The naming of R and S appears backwards here (R is a SHIFT and
- * S is a ROTATION) because the SHA-256/384/512 description document
+ * S is a ROTATION) because the SHA-224/256/384/512 description document
* (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
* same "backwards" definition.
*/
-/* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
+/* Shift-right (used in SHA-224, SHA-256, SHA-384, and SHA-512): */
#define R(b,x) ((x) >> (b))
-/* 32-bit Rotate-right (used in SHA-256): */
+/* 32-bit Rotate-right (used in SHA-224 and SHA-256): */
#define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b))))
/* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
#define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b))))
-/* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
+/* Two of six logical functions used in SHA-224, SHA-256, SHA-384, and SHA-512: */
#define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
#define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
-/* Four of six logical functions used in SHA-256: */
+/* Four of six logical functions used in SHA-224 and SHA-256: */
#define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x)))
#define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x)))
#define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x)))
@@ -139,7 +145,7 @@
/*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
-/* Hash constant words K for SHA-256: */
+/* Hash constant words K for SHA-224 and SHA-256: */
static const uint32_t K256[64] = {
0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
@@ -171,7 +177,6 @@ static const uint32_t sha256_initial_hash_value[8] = {
0x5be0cd19UL
};
-#ifndef SHA256_ONLY
/* Hash constant words K for SHA-384 and SHA-512: */
static const uint64_t K512[80] = {
0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
@@ -216,6 +221,31 @@ static const uint64_t K512[80] = {
0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
};
+/* Initial hash value H for SHA-512 */
+static const uint64_t sha512_initial_hash_value[8] = {
+ 0x6a09e667f3bcc908ULL,
+ 0xbb67ae8584caa73bULL,
+ 0x3c6ef372fe94f82bULL,
+ 0xa54ff53a5f1d36f1ULL,
+ 0x510e527fade682d1ULL,
+ 0x9b05688c2b3e6c1fULL,
+ 0x1f83d9abfb41bd6bULL,
+ 0x5be0cd19137e2179ULL
+};
+
+#if !defined(SHA2_SMALL)
+/* Initial hash value H for SHA-224: */
+static const uint32_t sha224_initial_hash_value[8] = {
+ 0xc1059ed8UL,
+ 0x367cd507UL,
+ 0x3070dd17UL,
+ 0xf70e5939UL,
+ 0xffc00b31UL,
+ 0x68581511UL,
+ 0x64f98fa7UL,
+ 0xbefa4fa4UL
+};
+
/* Initial hash value H for SHA-384 */
static const uint64_t sha384_initial_hash_value[8] = {
0xcbbb9d5dc1059ed8ULL,
@@ -228,25 +258,76 @@ static const uint64_t sha384_initial_hash_value[8] = {
0x47b5481dbefa4fa4ULL
};
-/* Initial hash value H for SHA-512 */
-static const uint64_t sha512_initial_hash_value[8] = {
- 0x6a09e667f3bcc908ULL,
- 0xbb67ae8584caa73bULL,
- 0x3c6ef372fe94f82bULL,
- 0xa54ff53a5f1d36f1ULL,
- 0x510e527fade682d1ULL,
- 0x9b05688c2b3e6c1fULL,
- 0x1f83d9abfb41bd6bULL,
- 0x5be0cd19137e2179ULL
+/* Initial hash value H for SHA-512-256 */
+static const uint64_t sha512_256_initial_hash_value[8] = {
+ 0x22312194fc2bf72cULL,
+ 0x9f555fa3c84c64c2ULL,
+ 0x2393b86b6f53b151ULL,
+ 0x963877195940eabdULL,
+ 0x96283ee2a88effe3ULL,
+ 0xbe5e1e2553863992ULL,
+ 0x2b0199fc2c85b8aaULL,
+ 0x0eb72ddc81c52ca2ULL
};
-#endif /* SHA256_ONLY */
+
+/*** SHA-224: *********************************************************/
+void
+SHA224Init(SHA2_CTX *context)
+{
+ memcpy(context->state.st32, sha224_initial_hash_value,
+ sizeof(sha224_initial_hash_value));
+ memset(context->buffer, 0, sizeof(context->buffer));
+ context->bitcount[0] = 0;
+}
+
+#ifdef libmd_strong_alias
+libmd_strong_alias(SHA224Transform, SHA256Transform);
+libmd_strong_alias(SHA224Update, SHA256Update);
+libmd_strong_alias(SHA224Pad, SHA256Pad);
+#else
+void
+SHA224Transform(uint64_t state[8], const uint8_t data[SHA256_BLOCK_LENGTH])
+{
+ SHA256Transform(state, data);
+}
+
+void
+SHA224Update(SHA2_CTX *context, const uint8_t *data, size_t len)
+{
+ SHA256Update(context, data, len);
+}
+
+void
+SHA224Pad(SHA2_CTX *context)
+{
+ SHA256Pad(context);
+}
+#endif
+
+void
+SHA224Final(uint8_t digest[SHA224_DIGEST_LENGTH], SHA2_CTX *context)
+{
+#ifndef WORDS_BIGENDIAN
+ int i;
+#endif
+
+ SHA224Pad(context);
+
+#ifndef WORDS_BIGENDIAN
+ /* Convert TO host byte order */
+ for (i = 0; i < 7; i++)
+ BE_32_TO_8(digest + i * 4, context->state.st32[i]);
+#else
+ memcpy(digest, context->state.st32, SHA224_DIGEST_LENGTH);
+#endif
+ memset(context, 0, sizeof(*context));
+}
+#endif /* !defined(SHA2_SMALL) */
/*** SHA-256: *********************************************************/
void
SHA256Init(SHA2_CTX *context)
{
- if (context == NULL)
- return;
memcpy(context->state.st32, sha256_initial_hash_value,
sizeof(sha256_initial_hash_value));
memset(context->buffer, 0, sizeof(context->buffer));
@@ -435,7 +516,7 @@ SHA256Update(SHA2_CTX *context, const uint8_t *data, size_t len)
} else {
/* The buffer is not yet full */
memcpy(&context->buffer[usedspace], data, len);
- context->bitcount[0] += len << 3;
+ context->bitcount[0] += (uint64_t)len << 3;
/* Clean up: */
usedspace = freespace = 0;
return;
@@ -503,31 +584,27 @@ SHA256Pad(SHA2_CTX *context)
void
SHA256Final(uint8_t digest[SHA256_DIGEST_LENGTH], SHA2_CTX *context)
{
+#ifndef WORDS_BIGENDIAN
+ int i;
+#endif
+
SHA256Pad(context);
- /* If no digest buffer is passed, we don't bother doing this: */
- if (digest != NULL) {
#ifndef WORDS_BIGENDIAN
- int i;
-
- /* Convert TO host byte order */
- for (i = 0; i < 8; i++)
- BE_32_TO_8(digest + i * 4, context->state.st32[i]);
+ /* Convert TO host byte order */
+ for (i = 0; i < 8; i++)
+ BE_32_TO_8(digest + i * 4, context->state.st32[i]);
#else
- memcpy(digest, context->state.st32, SHA256_DIGEST_LENGTH);
+ memcpy(digest, context->state.st32, SHA256_DIGEST_LENGTH);
#endif
- memset(context, 0, sizeof(*context));
- }
+ memset(context, 0, sizeof(*context));
}
-#ifndef SHA256_ONLY
/*** SHA-512: *********************************************************/
void
SHA512Init(SHA2_CTX *context)
{
- if (context == NULL)
- return;
memcpy(context->state.st64, sha512_initial_hash_value,
sizeof(sha512_initial_hash_value));
memset(context->buffer, 0, sizeof(context->buffer));
@@ -785,30 +862,28 @@ SHA512Pad(SHA2_CTX *context)
void
SHA512Final(uint8_t digest[SHA512_DIGEST_LENGTH], SHA2_CTX *context)
{
+#ifndef WORDS_BIGENDIAN
+ int i;
+#endif
+
SHA512Pad(context);
- /* If no digest buffer is passed, we don't bother doing this: */
- if (digest != NULL) {
#ifndef WORDS_BIGENDIAN
- int i;
-
- /* Convert TO host byte order */
- for (i = 0; i < 8; i++)
- BE_64_TO_8(digest + i * 8, context->state.st64[i]);
+ /* Convert TO host byte order */
+ for (i = 0; i < 8; i++)
+ BE_64_TO_8(digest + i * 8, context->state.st64[i]);
#else
- memcpy(digest, context->state.st64, SHA512_DIGEST_LENGTH);
+ memcpy(digest, context->state.st64, SHA512_DIGEST_LENGTH);
#endif
- memset(context, 0, sizeof(*context));
- }
+ memset(context, 0, sizeof(*context));
}
+#if !defined(SHA2_SMALL)
/*** SHA-384: *********************************************************/
void
SHA384Init(SHA2_CTX *context)
{
- if (context == NULL)
- return;
memcpy(context->state.st64, sha384_initial_hash_value,
sizeof(sha384_initial_hash_value));
memset(context->buffer, 0, sizeof(context->buffer));
@@ -842,22 +917,74 @@ SHA384Pad(SHA2_CTX *context)
void
SHA384Final(uint8_t digest[SHA384_DIGEST_LENGTH], SHA2_CTX *context)
{
+#ifndef WORDS_BIGENDIAN
+ int i;
+#endif
+
SHA384Pad(context);
- /* If no digest buffer is passed, we don't bother doing this: */
- if (digest != NULL) {
#ifndef WORDS_BIGENDIAN
- int i;
+ /* Convert TO host byte order */
+ for (i = 0; i < 6; i++)
+ BE_64_TO_8(digest + i * 8, context->state.st64[i]);
+#else
+ memcpy(digest, context->state.st64, SHA384_DIGEST_LENGTH);
+#endif
+ /* Zero out state data */
+ memset(context, 0, sizeof(*context));
+}
- /* Convert TO host byte order */
- for (i = 0; i < 6; i++)
- BE_64_TO_8(digest + i * 8, context->state.st64[i]);
+/*** SHA-512/256: *********************************************************/
+void
+SHA512_256Init(SHA2_CTX *context)
+{
+ memcpy(context->state.st64, sha512_256_initial_hash_value,
+ sizeof(sha512_256_initial_hash_value));
+ memset(context->buffer, 0, sizeof(context->buffer));
+ context->bitcount[0] = context->bitcount[1] = 0;
+}
+
+#ifdef libmd_strong_alias
+libmd_strong_alias(SHA512_256Transform, SHA512Transform);
+libmd_strong_alias(SHA512_256Update, SHA512Update);
+libmd_strong_alias(SHA512_256Pad, SHA512Pad);
#else
- memcpy(digest, context->state.st64, SHA384_DIGEST_LENGTH);
+void
+SHA512_256Transform(uint64_t state[8], const uint8_t data[SHA512_256_BLOCK_LENGTH])
+{
+ SHA512Transform(state, data);
+}
+
+void
+SHA512_256Update(SHA2_CTX *context, const uint8_t *data, size_t len)
+{
+ SHA512Update(context, data, len);
+}
+
+void
+SHA512_256Pad(SHA2_CTX *context)
+{
+ SHA512Pad(context);
+}
#endif
- }
+void
+SHA512_256Final(uint8_t digest[SHA512_256_DIGEST_LENGTH], SHA2_CTX *context)
+{
+#ifndef WORDS_BIGENDIAN
+ int i;
+#endif
+
+ SHA512_256Pad(context);
+
+#ifndef WORDS_BIGENDIAN
+ /* Convert TO host byte order */
+ for (i = 0; i < 4; i++)
+ BE_64_TO_8(digest + i * 8, context->state.st64[i]);
+#else
+ memcpy(digest, context->state.st64, SHA512_256_DIGEST_LENGTH);
+#endif
/* Zero out state data */
memset(context, 0, sizeof(*context));
}
-#endif /* SHA256_ONLY */
+#endif /* !defined(SHA2_SMALL) */