ref: d7340a02cef50b6f3ae1344b71a6f78922002652
parent: 515b8e89c1a6c0859eaefbf7835a4e6497a57e0b
author: Ori Bernstein <ori@eigenstate.org>
date: Tue Jun 3 18:34:51 EDT 2014
Add support for sha384.
--- a/libcryptohash/sha512.myr
+++ b/libcryptohash/sha512.myr
@@ -7,6 +7,11 @@
const sha512init : (st : sha512# -> void)
const sha512add : (st : sha512#, data : byte[:] -> void)
const sha512fin : (st : sha512# -> byte[64])
+
+ const sha384 : (data : byte[:] -> byte[48])
+ const sha384init : (st : sha384# -> void)
+ const sha384add : (st : sha384#, data : byte[:] -> void)
+ const sha384fin : (st : sha384# -> byte[48])
;;
@@ -16,6 +21,12 @@
msglen : uint64
;;
+type sha384 = struct
+ x : uint64[8]
+ tail : byte[128]
+ msglen : uint64
+;;
+
const sha512 = {data
var st
@@ -72,6 +83,62 @@
pack(r[56:64], st.x[7])
-> r
}
+
+const sha384 = {data
+ var st
+
+ sha384init(&st)
+ sha384add(&st, data)
+ -> sha384fin(&st)
+}
+
+const sha384init = {st
+ st.x[0] = 0xCBBB9D5DC1059ED8ul
+ st.x[1] = 0x629A292A367CD507ul
+ st.x[2] = 0x9159015A3070DD17ul
+ st.x[3] = 0x152FECD8F70E5939ul
+ st.x[4] = 0x67332667FFC00B31ul
+ st.x[5] = 0x8EB44A8768581511ul
+ st.x[6] = 0xDB0C2E0D64F98FA7ul
+ st.x[7] = 0x47B5481DBEFA4FA4ul
+ st.msglen = 0
+}
+
+const sha384add = {st, data
+ var n, ntail
+
+ ntail = st.msglen % 128
+ st.msglen += data.len
+ if ntail > 0
+ n = 128 - ntail
+ std.slcp(st.tail[n:], data[:n])
+ data = data[n:]
+ step(st.x[:], st.tail[:])
+ ;;
+
+ while data.len >= 128
+ step(st.x[:], data[:128])
+ data = data[128:]
+ ;;
+
+ ntail = st.msglen % 128
+ std.slcp(st.tail[:ntail], data)
+}
+
+const sha384fin = {st
+ var r : byte[48]
+
+ tail(st.x[:], st.msglen, st.tail[:])
+
+ pack(r[ 0: 8], st.x[0])
+ pack(r[ 8:16], st.x[1])
+ pack(r[16:24], st.x[2])
+ pack(r[24:32], st.x[3])
+ pack(r[32:40], st.x[4])
+ pack(r[40:48], st.x[5])
+ -> r
+}
+
const tail = {x, msglen, tail
var ntail
--- /dev/null
+++ b/libcryptohash/test/data/sha384-test-expected
@@ -1,0 +1,4 @@
+38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b
+a4eb0778c79fce94c02126543cba398d645b2fd4c6ff6a02eecc026bbe0cc0dd666279722b7615bc15b4c9126b941c04
+2e404b9339da795776e510d96930b3be2904c500395b8cb7413334b82d4dec413b4b8113045a05bbbcff846f027423f6
+f8f4b55a0fb1ac8506d2e5195c714a1ad16c3bf61ad8b2d544344b105a49a77ff3b8eb61e8f970a71864e9dad87042b1
--- a/libcryptohash/test/tests
+++ b/libcryptohash/test/tests
@@ -25,3 +25,4 @@
B sha256-test C
B sha224-test C
B sha512-test C
+B sha384-test C