summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2014-06-03 20:02:34 -0400
committerOri Bernstein <ori@eigenstate.org>2015-08-24 22:10:06 -0700
commit6137499d82a91ff950f9f3da7e48c2388a76d507 (patch)
tree45a89f6575721783774a90f6c4b0c45fa2f381f1
parent8f779f0802ffab46227e01c6ecd1a1187474d96f (diff)
downloadmc-6137499d82a91ff950f9f3da7e48c2388a76d507.tar.gz
Add support for sha224.
It's just truncated sha256.
-rw-r--r--libcryptohash/sha256.myr69
-rw-r--r--libcryptohash/test/sha224-test.myr22
-rw-r--r--libcryptohash/test/tests1
3 files changed, 92 insertions, 0 deletions
diff --git a/libcryptohash/sha256.myr b/libcryptohash/sha256.myr
index 4389d65..f92e773 100644
--- a/libcryptohash/sha256.myr
+++ b/libcryptohash/sha256.myr
@@ -2,11 +2,17 @@ use std
pkg cryptohash =
type sha256
+ type sha224
const sha256 : (data : byte[:] -> byte[32])
const sha256init : (st : sha256# -> void)
const sha256add : (st : sha256#, data : byte[:] -> void)
const sha256fin : (st : sha256# -> byte[32])
+
+ const sha224 : (data : byte[:] -> byte[28])
+ const sha224init : (st : sha224# -> void)
+ const sha224add : (st : sha224#, data : byte[:] -> void)
+ const sha224fin : (st : sha224# -> byte[28])
;;
type sha256 = struct
@@ -72,6 +78,69 @@ const sha256fin = {st
-> r
}
+type sha224 = struct
+ x : uint32[8]
+ tail : byte[64]
+ msglen : uint64
+;;
+
+const sha224 = {data
+ var st
+
+ sha224init(&st)
+ sha224add(&st, data)
+ -> sha224fin(&st)
+}
+
+const sha224init = {st
+ st.x[0] = 0xc1059ed8
+ st.x[1] = 0x367cd507
+ st.x[2] = 0x3070dd17
+ st.x[3] = 0xf70e5939
+ st.x[4] = 0xffc00b31
+ st.x[5] = 0x68581511
+ st.x[6] = 0x64f98fa7
+ st.x[7] = 0xbefa4fa4
+ st.msglen = 0
+}
+
+const sha224add = {st, data
+ var n, ntail
+
+ ntail = st.msglen % 64
+ st.msglen += data.len
+ if ntail > 0
+ n = 64 - ntail
+ std.slcp(st.tail[n:], data[:n])
+ data = data[n:]
+ step(st.x[:], st.tail[:])
+ ;;
+
+ while data.len >= 64
+ step(st.x[:], data[:64])
+ data = data[64:]
+ ;;
+
+ ntail = st.msglen % 64
+ std.slcp(st.tail[:ntail], data)
+}
+
+const sha224fin = {st
+ var r : byte[28]
+
+ tail(st.x[:], st.msglen, st.tail[:])
+
+ pack(r[0:4], st.x[0])
+ pack(r[4:8], st.x[1])
+ pack(r[8:12], st.x[2])
+ pack(r[12:16], st.x[3])
+ pack(r[16:20], st.x[4])
+ pack(r[20:24], st.x[5])
+ pack(r[24:28], st.x[6])
+ -> r
+}
+
+
const tail = {x, msglen, tail
var ntail
diff --git a/libcryptohash/test/sha224-test.myr b/libcryptohash/test/sha224-test.myr
new file mode 100644
index 0000000..db3d575
--- /dev/null
+++ b/libcryptohash/test/sha224-test.myr
@@ -0,0 +1,22 @@
+use std
+use cryptohash
+
+const main = {
+ print(cryptohash.sha224(""))
+ print(cryptohash.sha224("h"))
+ /* 64 byte block */
+ print(cryptohash.sha224("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
+ /* tail spanning */
+ print(cryptohash.sha224("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbb"))
+}
+
+const print = {b
+ for x in b
+ if (x <= 0xf)
+ std.put("0%xb", x)
+ else
+ std.put("%xb", x)
+ ;;
+ ;;
+ std.put("\n")
+}
diff --git a/libcryptohash/test/tests b/libcryptohash/test/tests
index cfa0a5a..8ddc203 100644
--- a/libcryptohash/test/tests
+++ b/libcryptohash/test/tests
@@ -23,3 +23,4 @@ mkdir -p tmpout
B md5-test C
B sha1-test C
B sha256-test C
+B sha224-test C