summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjsing <>2023-04-14 10:45:15 +0000
committerjsing <>2023-04-14 10:45:15 +0000
commit3e308f6a6cd5c9ecce0f92a26ab43e5f3caa6a0c (patch)
tree0c2cc5494f42b9056f080802ba999645a01d7e0e
parent38e781696b993f5328727fb10c9758dbf229fa61 (diff)
downloadopenbsd-3e308f6a6cd5c9ecce0f92a26ab43e5f3caa6a0c.tar.gz
openbsd-3e308f6a6cd5c9ecce0f92a26ab43e5f3caa6a0c.tar.bz2
openbsd-3e308f6a6cd5c9ecce0f92a26ab43e5f3caa6a0c.zip
Add support for truncated SHA512 variants.
This adds support for SHA512/224 and SHA512/256, as specified in FIPS FIPS 180-4. These are truncated versions of the SHA512 hash. ok tb@
-rw-r--r--src/lib/libcrypto/sha/sha512.c81
-rw-r--r--src/lib/libcrypto/sha/sha_internal.h36
2 files changed, 115 insertions, 2 deletions
diff --git a/src/lib/libcrypto/sha/sha512.c b/src/lib/libcrypto/sha/sha512.c
index a702d7d23b..4a4194350b 100644
--- a/src/lib/libcrypto/sha/sha512.c
+++ b/src/lib/libcrypto/sha/sha512.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: sha512.c,v 1.33 2023/04/14 10:41:34 jsing Exp $ */ 1/* $OpenBSD: sha512.c,v 1.34 2023/04/14 10:45:15 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 *
@@ -62,6 +62,7 @@
62#include <openssl/sha.h> 62#include <openssl/sha.h>
63 63
64#include "crypto_internal.h" 64#include "crypto_internal.h"
65#include "sha_internal.h"
65 66
66#if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA512) 67#if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA512)
67 68
@@ -547,6 +548,19 @@ SHA512_Final(unsigned char *md, SHA512_CTX *c)
547 548
548 /* Let compiler decide if it's appropriate to unroll... */ 549 /* Let compiler decide if it's appropriate to unroll... */
549 switch (c->md_len) { 550 switch (c->md_len) {
551 case SHA512_224_DIGEST_LENGTH:
552 for (n = 0; n < SHA512_224_DIGEST_LENGTH/8; n++) {
553 crypto_store_htobe64(md, c->h[n]);
554 md += 8;
555 }
556 crypto_store_htobe32(md, c->h[n] >> 32);
557 break;
558 case SHA512_256_DIGEST_LENGTH:
559 for (n = 0; n < SHA512_256_DIGEST_LENGTH/8; n++) {
560 crypto_store_htobe64(md, c->h[n]);
561 md += 8;
562 }
563 break;
550 case SHA384_DIGEST_LENGTH: 564 case SHA384_DIGEST_LENGTH:
551 for (n = 0; n < SHA384_DIGEST_LENGTH/8; n++) { 565 for (n = 0; n < SHA384_DIGEST_LENGTH/8; n++) {
552 crypto_store_htobe64(md, c->h[n]); 566 crypto_store_htobe64(md, c->h[n]);
@@ -559,7 +573,6 @@ SHA512_Final(unsigned char *md, SHA512_CTX *c)
559 md += 8; 573 md += 8;
560 } 574 }
561 break; 575 break;
562 /* ... as well as make sure md_len is not abused. */
563 default: 576 default:
564 return 0; 577 return 0;
565 } 578 }
@@ -585,4 +598,68 @@ SHA512(const unsigned char *d, size_t n, unsigned char *md)
585 return (md); 598 return (md);
586} 599}
587 600
601int
602SHA512_224_Init(SHA512_CTX *c)
603{
604 memset(c, 0, sizeof(*c));
605
606 /* FIPS 180-4 section 5.3.6.1. */
607 c->h[0] = U64(0x8c3d37c819544da2);
608 c->h[1] = U64(0x73e1996689dcd4d6);
609 c->h[2] = U64(0x1dfab7ae32ff9c82);
610 c->h[3] = U64(0x679dd514582f9fcf);
611 c->h[4] = U64(0x0f6d2b697bd44da8);
612 c->h[5] = U64(0x77e36f7304c48942);
613 c->h[6] = U64(0x3f9d85a86a1d36c8);
614 c->h[7] = U64(0x1112e6ad91d692a1);
615
616 c->md_len = SHA512_224_DIGEST_LENGTH;
617
618 return 1;
619}
620
621int
622SHA512_224_Update(SHA512_CTX *c, const void *data, size_t len)
623{
624 return SHA512_Update(c, data, len);
625}
626
627int
628SHA512_224_Final(unsigned char *md, SHA512_CTX *c)
629{
630 return SHA512_Final(md, c);
631}
632
633int
634SHA512_256_Init(SHA512_CTX *c)
635{
636 memset(c, 0, sizeof(*c));
637
638 /* FIPS 180-4 section 5.3.6.2. */
639 c->h[0] = U64(0x22312194fc2bf72c);
640 c->h[1] = U64(0x9f555fa3c84c64c2);
641 c->h[2] = U64(0x2393b86b6f53b151);
642 c->h[3] = U64(0x963877195940eabd);
643 c->h[4] = U64(0x96283ee2a88effe3);
644 c->h[5] = U64(0xbe5e1e2553863992);
645 c->h[6] = U64(0x2b0199fc2c85b8aa);
646 c->h[7] = U64(0x0eb72ddc81c52ca2);
647
648 c->md_len = SHA512_256_DIGEST_LENGTH;
649
650 return 1;
651}
652
653int
654SHA512_256_Update(SHA512_CTX *c, const void *data, size_t len)
655{
656 return SHA512_Update(c, data, len);
657}
658
659int
660SHA512_256_Final(unsigned char *md, SHA512_CTX *c)
661{
662 return SHA512_Final(md, c);
663}
664
588#endif /* !OPENSSL_NO_SHA512 */ 665#endif /* !OPENSSL_NO_SHA512 */
diff --git a/src/lib/libcrypto/sha/sha_internal.h b/src/lib/libcrypto/sha/sha_internal.h
new file mode 100644
index 0000000000..c479993185
--- /dev/null
+++ b/src/lib/libcrypto/sha/sha_internal.h
@@ -0,0 +1,36 @@
1/* $OpenBSD: sha_internal.h,v 1.1 2023/04/14 10:45:15 jsing Exp $ */
2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <openssl/sha.h>
19
20#ifndef HEADER_SHA_INTERNAL_H
21#define HEADER_SHA_INTERNAL_H
22
23#define SHA512_224_DIGEST_LENGTH 28
24#define SHA512_256_DIGEST_LENGTH 32
25
26int SHA512_224_Init(SHA512_CTX *c);
27int SHA512_224_Update(SHA512_CTX *c, const void *data, size_t len)
28 __attribute__ ((__bounded__(__buffer__,2,3)));
29int SHA512_224_Final(unsigned char *md, SHA512_CTX *c);
30
31int SHA512_256_Init(SHA512_CTX *c);
32int SHA512_256_Update(SHA512_CTX *c, const void *data, size_t len)
33 __attribute__ ((__bounded__(__buffer__,2,3)));
34int SHA512_256_Final(unsigned char *md, SHA512_CTX *c);
35
36#endif