util.hashes: Add SHA3 bindings
authorKim Alvefur <zash@zash.se>
Thu, 10 Sep 2020 21:58:24 +0200
changeset 12568 36e769c64054
parent 12567 d9a4e28689eb
child 12569 2e0c7f5cd8f8
util.hashes: Add SHA3 bindings
spec/util_hashes_spec.lua
teal-src/util/hashes.d.tl
util-src/hashes.c
--- a/spec/util_hashes_spec.lua	Thu Sep 10 21:58:24 2020 +0200
+++ b/spec/util_hashes_spec.lua	Thu Sep 10 21:58:24 2020 +0200
@@ -53,3 +53,18 @@
 end);
 
 
+describe("SHA-3", function ()
+	describe("256", function ()
+		it("works", function ()
+			local expected = "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a"
+			assert.equal(expected, hashes.sha3_256("", true));
+		end);
+	end);
+	describe("512", function ()
+		it("works", function ()
+			local expected = "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26"
+			assert.equal(expected, hashes.sha3_512("", true));
+		end);
+	end);
+end);
+
--- a/teal-src/util/hashes.d.tl	Thu Sep 10 21:58:24 2020 +0200
+++ b/teal-src/util/hashes.d.tl	Thu Sep 10 21:58:24 2020 +0200
@@ -9,6 +9,8 @@
 	sha384 : hash
 	sha512 : hash
 	md5 : hash
+	sha3_256 : hash
+	sha3_512 : hash
 	blake2s256 : hash
 	blake2b512 : hash
 	hmac_sha1 : hmac
@@ -17,6 +19,8 @@
 	hmac_sha384  :hmac
 	hmac_sha512 : hmac
 	hmac_md5 : hmac
+	hmac_sha3_256 : hmac
+	hmac_sha3_512 : hmac
 	scram_Hi_sha1 : kdf
 	pbkdf2_hmac_sha1 : kdf
 	pbkdf2_hmac_sha256 : kdf
--- a/util-src/hashes.c	Thu Sep 10 21:58:24 2020 +0200
+++ b/util-src/hashes.c	Thu Sep 10 21:58:24 2020 +0200
@@ -120,6 +120,13 @@
 	return Levp_hash(L, EVP_blake2b512());
 }
 
+static int Lsha3_256(lua_State *L) {
+	return Levp_hash(L, EVP_sha3_256());
+}
+
+static int Lsha3_512(lua_State *L) {
+	return Levp_hash(L, EVP_sha3_512());
+}
 
 struct hash_desc {
 	int (*Init)(void *);
@@ -198,6 +205,14 @@
 	return Levp_hmac(L, EVP_md5());
 }
 
+static int Lhmac_sha3_256(lua_State *L) {
+	return Levp_hmac(L, EVP_sha3_256());
+}
+
+static int Lhmac_sha3_512(lua_State *L) {
+	return Levp_hmac(L, EVP_sha3_512());
+}
+
 static int Lhmac_blake2s256(lua_State *L) {
 	return Levp_hmac(L, EVP_blake2s256());
 }
@@ -251,6 +266,8 @@
 	{ "sha384",		Lsha384		},
 	{ "sha512",		Lsha512		},
 	{ "md5",		Lmd5		},
+	{ "sha3_256",		Lsha3_256	},
+	{ "sha3_512",		Lsha3_512	},
 	{ "blake2s256",		Lblake2s256	},
 	{ "blake2b512",		Lblake2b512	},
 	{ "hmac_sha1",		Lhmac_sha1	},
@@ -259,6 +276,8 @@
 	{ "hmac_sha384",	Lhmac_sha384	},
 	{ "hmac_sha512",	Lhmac_sha512	},
 	{ "hmac_md5",		Lhmac_md5	},
+	{ "hmac_sha3_256",	Lhmac_sha3_256	},
+	{ "hmac_sha3_512",	Lhmac_sha3_512	},
 	{ "hmac_blake2s256",	Lhmac_blake2s256	},
 	{ "hmac_blake2b512",	Lhmac_blake2b512	},
 	{ "scram_Hi_sha1",	Lpbkdf2_sha1	}, /* COMPAT */