Browse Source

Added Whirlpool and sha3 digests.
Updated docs.

woollybah 5 years ago
parent
commit
f1577b61de

+ 18 - 0
digest.mod/doc/intro.bbdoc

@@ -0,0 +1,18 @@
+### Supported Digests
+
+| Name | Size of Message Digest (bytes) |
+|---|---|
+| #Crypto.WhirlpoolDigest          | 64 |
+| #Crypto.SHA3Digest ([TSHA3_512])  | 64 |
+| #Crypto.SHA512Digest             | 64 |
+| #Crypto.SHA3Digest ([TSHA3_384])  | 48 |
+| #Crypto.SHA3Digest ([TSHA3_256])  | 32 |
+| #Crypto.SHA256Digest             | 32 |
+| #Crypto.SHA3Digest ([TSHA3_224])  | 28 |
+| #Crypto.SHA1Digest               | 20 |
+| #Crypto.MD5Digest                | 16 |
+
+[TSHA3_512]: ../crypto.sha3digest/tsha3_512
+[TSHA3_384]: ../crypto.sha3digest/tsha3_384
+[TSHA3_256]: ../crypto.sha3digest/tsha3_256
+[TSHA3_224]: ../crypto.sha3digest/tsha3_224

+ 89 - 0
digest.mod/tests/test.bmx

@@ -6,6 +6,8 @@ Import Crypto.sha1digest
 Import Crypto.sha256digest
 Import Crypto.sha512digest
 Import Crypto.crc32
+Import Crypto.WhirlpoolDigest
+Import Crypto.sha3digest
 Import BRL.MaxUnit
 
 New TTestSuite.run()
@@ -32,6 +34,29 @@ Type TDigestTest Extends TTest
 	Global CRC32_HASH_ARRAY:Byte[] = [65, 79, 163, 57]
 	Const CRC32_INT:Int = 1095738169
 	
+	Const WHIRLPOOL_HASH_STRING:String = "b97de512e91e3828b40d2b0fdce9ceb3c4a71f9bea8d88e75c4fa854df36725fd2b52eb6544edcacd6f8beddfea403cb55ae31f03ad62a5ef54e42ee82c3fb35"
+	Global WHIRLPOOL_HASH_ARRAY:Byte[] = [185, 125, 229, 18, 233, 30, 56, 40, 180, 13, 43, 15, 220, 233, 206, 179, 196, 167, 31, 155, 234, 141, ..
+										136, 231, 92, 79, 168, 84, 223, 54, 114, 95, 210, 181, 46, 182, 84, 78, 220, 172, 214, 248, 190, 221, ..
+										254, 164, 3, 203, 85, 174, 49, 240, 58, 214, 42, 94, 245, 78, 66, 238, 130, 195, 251, 53]
+
+	Const SHA3_512_HASH_STRING:String = "01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450"
+	Global SHA3_512_HASH_ARRAY:Byte[] = [1, 222, 221, 93, 228, 239, 20, 100, 36, 69, 186, 95, 91, 151, 193, 94, 71, 185, 173, 147, 19, 38, 228, ..
+										176, 114, 124, 217, 76, 239, 196, 79, 255, 35, 240, 123, 245, 67, 19, 153, 57, 180, 145, 40, 202, 244, ..
+										54, 220, 27, 222, 229, 79, 203, 36, 2, 58, 8, 217, 64, 63, 155, 75, 240, 212, 80]
+	
+	Const SHA3_384_HASH_STRING:String = "7063465e08a93bce31cd89d2e3ca8f602498696e253592ed26f07bf7e703cf328581e1471a7ba7ab119b1a9ebdf8be41"
+	Global SHA3_384_HASH_ARRAY:Byte[] = [112, 99, 70, 94, 8, 169, 59, 206, 49, 205, 137, 210, 227, 202, 143, 96, 36, 152, 105, 110, 37, 53, ..
+										146, 237, 38, 240, 123, 247, 231, 3, 207, 50, 133, 129, 225, 71, 26, 123, 167, 171, 17, 155, 26, 158, ..
+										189, 248, 190, 65]
+	
+	Const SHA3_256_HASH_STRING:String = "69070dda01975c8c120c3aada1b282394e7f032fa9cf32f4cb2259a0897dfc04"
+	Global SHA3_256_HASH_ARRAY:Byte[] = [105, 7, 13, 218, 1, 151, 92, 140, 18, 12, 58, 173, 161, 178, 130, 57, 78, 127, 3, 47, 169, 207, 50, ..
+										244, 203, 34, 89, 160, 137, 125, 252, 4]
+	
+	Const SHA3_224_HASH_STRING:String = "d15dadceaa4d5d7bb3b48f446421d542e08ad8887305e28d58335795"
+	Global SHA3_224_HASH_ARRAY:Byte[] = [209, 93, 173, 206, 170, 77, 93, 123, 179, 180, 143, 68, 100, 33, 213, 66, 224, 138, 216, 136, 115, 5, ..
+										226, 141, 88, 51, 87, 149]
+
 	Method testMD5() { test }
 	
 		Local digest:TMessageDigest = GetMessageDigest("MD5")
@@ -118,4 +143,68 @@ Type TDigestTest Extends TTest
 	
 	End Method
 
+	Method testWhirlpool() { test }
+	
+		Local digest:TMessageDigest = GetMessageDigest("WHIRLPOOL")
+	
+		assertEquals(WHIRLPOOL_HASH_STRING, digest.Digest(TEST_PHRASE))
+	
+		Local bytes:Byte[] = digest.DigestBytes(TEST_PHRASE)
+
+		assertEquals(WHIRLPOOL_HASH_ARRAY.length, bytes.length)
+		
+		For Local i:Int = 0 Until WHIRLPOOL_HASH_ARRAY.length
+			assertEquals(WHIRLPOOL_HASH_ARRAY[i], bytes[i])
+		Next
+	
+	End Method
+
+	Method testSHA3512() { test }
+	
+		Local digest:TMessageDigest = GetMessageDigest("SHA3-512")
+	
+		assertEquals(SHA3_512_HASH_STRING, digest.Digest(TEST_PHRASE))
+	
+		Local bytes:Byte[] = digest.DigestBytes(TEST_PHRASE)
+
+		assertEquals(SHA3_512_HASH_ARRAY.length, bytes.length)
+		
+		For Local i:Int = 0 Until SHA3_512_HASH_ARRAY.length
+			assertEquals(SHA3_512_HASH_ARRAY[i], bytes[i])
+		Next
+	
+	End Method
+
+	Method testSHA3384() { test }
+	
+		Local digest:TMessageDigest = GetMessageDigest("SHA3-384")
+	
+		assertEquals(SHA3_384_HASH_STRING, digest.Digest(TEST_PHRASE))
+	
+		Local bytes:Byte[] = digest.DigestBytes(TEST_PHRASE)
+
+		assertEquals(SHA3_384_HASH_ARRAY.length, bytes.length)
+		
+		For Local i:Int = 0 Until SHA3_384_HASH_ARRAY.length
+			assertEquals(SHA3_384_HASH_ARRAY[i], bytes[i])
+		Next
+	
+	End Method
+
+	Method testSHA3256() { test }
+	
+		Local digest:TMessageDigest = GetMessageDigest("SHA3-256")
+	
+		assertEquals(SHA3_256_HASH_STRING, digest.Digest(TEST_PHRASE))
+	
+		Local bytes:Byte[] = digest.DigestBytes(TEST_PHRASE)
+
+		assertEquals(SHA3_256_HASH_ARRAY.length, bytes.length)
+		
+		For Local i:Int = 0 Until SHA3_256_HASH_ARRAY.length
+			assertEquals(SHA3_256_HASH_ARRAY[i], bytes[i])
+		Next
+	
+	End Method
+
 End Type

+ 48 - 0
sha3digest.mod/common.bmx

@@ -0,0 +1,48 @@
+'
+'  Copyright (C) 2019-2020 Bruce A Henderson
+'
+'  This software is provided 'as-is', without any express or implied
+'  warranty.  In no event will the authors be held liable for any damages
+'  arising from the use of this software.
+'
+'  Permission is granted to anyone to use this software for any purpose,
+'  including commercial applications, and to alter it and redistribute it
+'  freely, subject to the following restrictions:
+'
+'  1. The origin of this software must not be misrepresented; you must not
+'     claim that you wrote the original software. If you use this software
+'     in a product, an acknowledgment in the product documentation would be
+'     appreciated but is not required.
+'  2. Altered source versions must be plainly marked as such, and must not be
+'     misrepresented as being the original software.
+'  3. This notice may not be removed or altered from any source distribution.
+'
+SuperStrict
+
+Import Crypto.Digest
+
+
+Import "../libtomcrypt.mod/libtomcrypt/src/headers/*.h"
+Import "../libtomcrypt.mod/libtomcrypt/src/hashes/sha3.c"
+Import "../libtomcrypt.mod/libtomcrypt/src/hashes/sha3_test.c"
+
+Import "glue.c"
+
+
+Extern
+
+	Function bmx_digest_sha3_process:Int(handle:Byte Ptr, buf:Byte Ptr, length:Int)
+
+	Function bmx_digest_sha3_512_init:Byte Ptr()
+	Function bmx_digest_sha3_512_done:Int(handle:Byte Ptr, out:Byte[])
+
+	Function bmx_digest_sha3_384_init:Byte Ptr()
+	Function bmx_digest_sha3_384_done:Int(handle:Byte Ptr, out:Byte[])
+
+	Function bmx_digest_sha3_256_init:Byte Ptr()
+	Function bmx_digest_sha3_256_done:Int(handle:Byte Ptr, out:Byte[])
+
+	Function bmx_digest_sha3_224_init:Byte Ptr()
+	Function bmx_digest_sha3_224_done:Int(handle:Byte Ptr, out:Byte[])
+
+End Extern

+ 81 - 0
sha3digest.mod/glue.c

@@ -0,0 +1,81 @@
+/*
+  Copyright (C) 2019-2020 Bruce A Henderson
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+#include "tomcrypt.h"
+#include "brl.mod/blitz.mod/blitz.h"
+
+int bmx_digest_sha3_process(hash_state * state, char * buf, int length) {
+	return sha3_process(state, buf, length);
+}
+
+hash_state * bmx_digest_sha3_512_init() {
+	hash_state * state = malloc(sizeof(hash_state));
+	sha3_512_init(state);
+	return state;
+}
+
+int bmx_digest_sha3_512_done(hash_state * state, BBArray * out) {
+	BBBYTE * p = (BBBYTE**)BBARRAYDATA(out, 1);
+
+	int res = sha3_done(state, p);
+	sha3_512_init(state);
+	return res;
+}
+
+hash_state * bmx_digest_sha3_384_init() {
+	hash_state * state = malloc(sizeof(hash_state));
+	sha3_384_init(state);
+	return state;
+}
+
+int bmx_digest_sha3_384_done(hash_state * state, BBArray * out) {
+	BBBYTE * p = (BBBYTE**)BBARRAYDATA(out, 1);
+
+	int res = sha3_done(state, p);
+	sha3_384_init(state);
+	return res;
+}
+
+hash_state * bmx_digest_sha3_256_init() {
+	hash_state * state = malloc(sizeof(hash_state));
+	sha3_256_init(state);
+	return state;
+}
+
+int bmx_digest_sha3_256_done(hash_state * state, BBArray * out) {
+	BBBYTE * p = (BBBYTE**)BBARRAYDATA(out, 1);
+
+	int res = sha3_done(state, p);
+	sha3_256_init(state);
+	return res;
+}
+
+hash_state * bmx_digest_sha3_224_init() {
+	hash_state * state = malloc(sizeof(hash_state));
+	sha3_224_init(state);
+	return state;
+}
+
+int bmx_digest_sha3_224_done(hash_state * state, BBArray * out) {
+	BBBYTE * p = (BBBYTE**)BBARRAYDATA(out, 1);
+
+	int res = sha3_done(state, p);
+	sha3_224_init(state);
+	return res;
+}

+ 180 - 0
sha3digest.mod/sha3digest.bmx

@@ -0,0 +1,180 @@
+'
+'  Copyright (C) 2019-2020 Bruce A Henderson
+'
+'  This software is provided 'as-is', without any express or implied
+'  warranty.  In no event will the authors be held liable for any damages
+'  arising from the use of this software.
+'
+'  Permission is granted to anyone to use this software for any purpose,
+'  including commercial applications, and to alter it and redistribute it
+'  freely, subject to the following restrictions:
+'
+'  1. The origin of this software must not be misrepresented; you must not
+'     claim that you wrote the original software. If you use this software
+'     in a product, an acknowledgment in the product documentation would be
+'     appreciated but is not required.
+'  2. Altered source versions must be plainly marked as such, and must not be
+'     misrepresented as being the original software.
+'  3. This notice may not be removed or altered from any source distribution.
+'
+SuperStrict
+
+Rem
+bbdoc: SHA-3 Digest
+about: 
+See <https://en.wikipedia.org/wiki/SHA-3>
+End Rem
+Module Crypto.SHA3Digest
+
+ModuleInfo "CC_OPTS: -DLTC_NO_TEST -DLTC_NO_FILE"
+ModuleInfo "CC_OPTS: -DLTC_SHA3"
+
+Import "common.bmx"
+
+New TSHA3DigestRegister
+
+Rem
+bbdoc: An SHA3-512 message digest.
+End Rem
+Type TSHA3_512 Extends TMessageDigest
+
+	Method New()
+		digestPtr = bmx_digest_sha3_512_init()
+	End Method
+
+	Method OutBytes:Int() Override
+		Return 64
+	End Method
+	
+	Rem
+	bbdoc: Updates the hash with @dataLen bytes of data.
+	End Rem
+	Method Update:Int(data:Byte Ptr, dataLen:Int) Override
+		Return bmx_digest_sha3_process(digestPtr, data, dataLen)
+	End Method
+	
+	Rem
+	bbdoc: Finishes hashing and produces the digest, filling @digest with the hashed bytes.
+	about: The hashing state is reset, ready to create a new digest.
+	End Rem
+	Method Finish:Int(digest:Byte[]) Override
+		Assert digest.length >= 64, "Byte array must be at least 64 bytes."
+		Return bmx_digest_sha3_512_done(digestPtr, digest)
+	End Method
+
+End Type
+
+Rem
+bbdoc: An SHA3-384 message digest.
+End Rem
+Type TSHA3_384 Extends TMessageDigest
+
+	Method New()
+		digestPtr = bmx_digest_sha3_384_init()
+	End Method
+
+	Method OutBytes:Int() Override
+		Return 48
+	End Method
+	
+	Rem
+	bbdoc: Updates the hash with @dataLen bytes of data.
+	End Rem
+	Method Update:Int(data:Byte Ptr, dataLen:Int) Override
+		Return bmx_digest_sha3_process(digestPtr, data, dataLen)
+	End Method
+	
+	Rem
+	bbdoc: Finishes hashing and produces the digest, filling @digest with the hashed bytes.
+	about: The hashing state is reset, ready to create a new digest.
+	End Rem
+	Method Finish:Int(digest:Byte[]) Override
+		Assert digest.length >= 48, "Byte array must be at least 48 bytes."
+		Return bmx_digest_sha3_384_done(digestPtr, digest)
+	End Method
+
+End Type
+
+Rem
+bbdoc: An SHA3-256 message digest.
+End Rem
+Type TSHA3_256 Extends TMessageDigest
+
+	Method New()
+		digestPtr = bmx_digest_sha3_256_init()
+	End Method
+
+	Method OutBytes:Int() Override
+		Return 32
+	End Method
+	
+	Rem
+	bbdoc: Updates the hash with @dataLen bytes of data.
+	End Rem
+	Method Update:Int(data:Byte Ptr, dataLen:Int) Override
+		Return bmx_digest_sha3_process(digestPtr, data, dataLen)
+	End Method
+	
+	Rem
+	bbdoc: Finishes hashing and produces the digest, filling @digest with the hashed bytes.
+	about: The hashing state is reset, ready to create a new digest.
+	End Rem
+	Method Finish:Int(digest:Byte[]) Override
+		Assert digest.length >= 32, "Byte array must be at least 32 bytes."
+		Return bmx_digest_sha3_256_done(digestPtr, digest)
+	End Method
+
+End Type
+
+Rem
+bbdoc: An SHA3-224 message digest.
+End Rem
+Type TSHA3_224 Extends TMessageDigest
+
+	Method New()
+		digestPtr = bmx_digest_sha3_224_init()
+	End Method
+
+	Method OutBytes:Int() Override
+		Return 28
+	End Method
+	
+	Rem
+	bbdoc: Updates the hash with @dataLen bytes of data.
+	End Rem
+	Method Update:Int(data:Byte Ptr, dataLen:Int) Override
+		Return bmx_digest_sha3_process(digestPtr, data, dataLen)
+	End Method
+	
+	Rem
+	bbdoc: Finishes hashing and produces the digest, filling @digest with the hashed bytes.
+	about: The hashing state is reset, ready to create a new digest.
+	End Rem
+	Method Finish:Int(digest:Byte[]) Override
+		Assert digest.length >= 28, "Byte array must be at least 28 bytes."
+		Return bmx_digest_sha3_224_done(digestPtr, digest)
+	End Method
+
+End Type
+
+Type TSHA3DigestRegister Extends TDigestRegister
+
+	Method GetDigest:TMessageDigest( name:String ) Override
+		name = name.ToUpper()
+		Select name
+			Case "SHA3-512", "SHA3_512", "SHA3512"
+				Return New TSHA3_512
+			Case "SHA3-384", "SHA3_384", "SHA3384"
+				Return New TSHA3_384
+			Case "SHA3-256", "SHA3_256", "SHA3256"
+				Return New TSHA3_256
+			Case "SHA3-224", "SHA3_224", "SHA3224"
+				Return New TSHA3_224
+		End Select
+	End Method
+
+	Method ToString:String() Override
+		Return "SHA3-512, SHA3-384, SHA3-256, SHA3-224"
+	End Method
+
+End Type

+ 38 - 0
whirlpooldigest.mod/common.bmx

@@ -0,0 +1,38 @@
+'
+'  Copyright (C) 2019-2020 Bruce A Henderson
+'
+'  This software is provided 'as-is', without any express or implied
+'  warranty.  In no event will the authors be held liable for any damages
+'  arising from the use of this software.
+'
+'  Permission is granted to anyone to use this software for any purpose,
+'  including commercial applications, and to alter it and redistribute it
+'  freely, subject to the following restrictions:
+'
+'  1. The origin of this software must not be misrepresented; you must not
+'     claim that you wrote the original software. If you use this software
+'     in a product, an acknowledgment in the product documentation would be
+'     appreciated but is not required.
+'  2. Altered source versions must be plainly marked as such, and must not be
+'     misrepresented as being the original software.
+'  3. This notice may not be removed or altered from any source distribution.
+'
+SuperStrict
+
+Import Crypto.Digest
+
+
+Import "../libtomcrypt.mod/libtomcrypt/src/headers/*.h"
+Import "../libtomcrypt.mod/libtomcrypt/src/hashes/whirl/whirl.c"
+Import "../libtomcrypt.mod/libtomcrypt/src/hashes/whirl/whirltab.c"
+
+Import "glue.c"
+
+
+Extern
+
+	Function bmx_digest_whirlpool_init:Byte Ptr()
+	Function bmx_digest_whirlpool_process:Int(handle:Byte Ptr, buf:Byte Ptr, length:Int)
+	Function bmx_digest_whirlpool_done:Int(handle:Byte Ptr, out:Byte[])
+
+End Extern

+ 39 - 0
whirlpooldigest.mod/glue.c

@@ -0,0 +1,39 @@
+/*
+  Copyright (C) 2019-2020 Bruce A Henderson
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+#include "tomcrypt.h"
+#include "brl.mod/blitz.mod/blitz.h"
+
+hash_state * bmx_digest_whirlpool_init() {
+	hash_state * state = malloc(sizeof(hash_state));
+	whirlpool_init(state);
+	return state;
+}
+
+int bmx_digest_whirlpool_process(hash_state * state, char * buf, int length) {
+	return whirlpool_process(state, buf, length);
+}
+
+int bmx_digest_whirlpool_done(hash_state * state, BBArray * out) {
+	BBBYTE * p = (BBBYTE**)BBARRAYDATA(out, 1);
+
+	int res = whirlpool_done(state, p);
+	whirlpool_init(state);
+	return res;
+}

+ 77 - 0
whirlpooldigest.mod/whirlpooldigest.bmx

@@ -0,0 +1,77 @@
+'
+'  Copyright (C) 2019-2020 Bruce A Henderson
+'
+'  This software is provided 'as-is', without any express or implied
+'  warranty.  In no event will the authors be held liable for any damages
+'  arising from the use of this software.
+'
+'  Permission is granted to anyone to use this software for any purpose,
+'  including commercial applications, and to alter it and redistribute it
+'  freely, subject to the following restrictions:
+'
+'  1. The origin of this software must not be misrepresented; you must not
+'     claim that you wrote the original software. If you use this software
+'     in a product, an acknowledgment in the product documentation would be
+'     appreciated but is not required.
+'  2. Altered source versions must be plainly marked as such, and must not be
+'     misrepresented as being the original software.
+'  3. This notice may not be removed or altered from any source distribution.
+'
+SuperStrict
+
+Rem
+bbdoc: WHILRPOOL Digest
+End Rem
+Module Crypto.WhirlpoolDigest
+
+ModuleInfo "CC_OPTS: -DLTC_NO_TEST -DLTC_NO_FILE"
+ModuleInfo "CC_OPTS: -DLTC_WHIRLPOOL"
+
+Import "common.bmx"
+
+New TWhirlpoolDigestRegister
+
+Rem
+bbdoc: A WHIRPOOL message digest.
+End Rem
+Type TWhirlpool Extends TMessageDigest
+
+	Method New()
+		digestPtr = bmx_digest_whirlpool_init()
+	End Method
+
+	Method OutBytes:Int() Override
+		Return 64
+	End Method
+	
+	Rem
+	bbdoc: Updates the hash with @dataLen bytes of data.
+	End Rem
+	Method Update:Int(data:Byte Ptr, dataLen:Int) Override
+		Return bmx_digest_whirlpool_process(digestPtr, data, dataLen)
+	End Method
+	
+	Rem
+	bbdoc: Finishes hashing and produces the digest, filling @digest with the hashed bytes.
+	about: The hashing state is reset, ready to create a new digest.
+	End Rem
+	Method Finish:Int(digest:Byte[]) Override
+		Assert digest.length >= 64, "Byte array must be at least 16 bytes."
+		Return bmx_digest_whirlpool_done(digestPtr, digest)
+	End Method
+
+End Type
+
+Type TWhirlpoolDigestRegister Extends TDigestRegister
+
+	Method GetDigest:TMessageDigest( name:String ) Override
+		If name.ToUpper() = "WHIRLPOOL" Then
+			Return New TWhirlpool
+		End If
+	End Method
+
+	Method ToString:String() Override
+		Return "WHIRLPOOL"
+	End Method
+	
+End Type