Link to this headingBlake2
- Improved Version of [Blake](/Crypto/Hash Functions/Blake)
- Not vulnerable to length extension because the final block compressed with the ChaCha function contains the final message length.
- Since adding any information into this would increase that number, the previous hash would not match the next compression round
Example:
=
#blake2s('hello') = b'19213bacc58dee6dbde3ceb9a47cbb330b3d86f8cca8997eb00be456f140ca25'
=
#blake2b('hello') = b'e4cfa39a3d37be31c59609e807970799caa68a19bfaa15135f165085e01d41a65ba1e1b146aeb6bd0092b49eac214c103ccfa3a365954bbbe52f74a2b3620c94'
Link to this headingBlake2s
- 256-bit hash optimized for 32-bit microprocessors
Link to this headingBlake2b
- 512-bit hash optimized for 64-bit microprocessors
Link to this headingImplementation
=
= 0
= False
#Copy the Buffers in to the IV array
=
#Deal with key, salt and personalization if required
=
^= | |
=
^= |
=
^=
^=
=
^=
^=
#Blake 2s
=
=
= 32
= 10
= // * 8
=
#Blake2b
=
=
= 64
= 12
= // * 8
=
#raise ValueError("Invalid Blake2 Version {}".format(self.version))
#Convert to bytes if not already
=
#Set Final Length
=
#Pad the data to a multiple of the block size
return
#Calculate indexes from Permutation table and round_index and offset
=
=
#Modified first part to include message and round xor
=
=
=
=
=
=
#Modified first part to include message and round xor
=
=
=
=
=
=
return
#Start the compress function
#Create the start of the temp chunks
=
#print(f"message: {[hex(x) for x in temp_chunk]}")
#Start setting up the temp buffers
= +
^=
^=
#Do not xor currentlength when it is the last block and there is more than one block
^=
'''
Resulting temp_buffers looks like this
|IV |IV |IV |IV |
|IV |IV |IV |IV |
|Const ^ Salt |Const ^ Salt |Const ^ Salt |Const ^ Salt |
|Const ^ len[0] |Const ^ len[0] |Const ^ len[1] |Const ^ len[1] |
'''
#print([hex(x) for x in temp_buffers], self.xor_block, hex(self.current_length))
#Do ChaCha rounds with modifications
#Do Each Column
, , , =
, , , =
, , , =
, , , =
#Do Each Diagonal
, , , =
, , , =
, , , =
, , , =
#print([hex(x) for x in temp_buffers])
#Update Buffers
^= ^
#print([hex(x) for x in self.buffers])
#If has a key then set the first block to the key and add padding to the blocksize
=
= +
#Setup message with padding and length data
=
#Operate on each of the chunks
=
#Update the current_length
+=
#Fix Edge Case for padding goes into the next block
#Last Block
= True
=
#Compress the message Chunk
#Convert Integers to Byte string
= b
+=
return
return
"""
print("key = b''")
for x in [224, 256, 384, 512]:
test = Blake2(x, output_size=32)
print(f"BLAKE2s-{x}(\"\"): {test.hash_digest(b'test')}")
#BLAKE2s-224(""): 1fa1291e65248b37b3433475b2a0dd63d54a11ecc4e3e034e7bc1ef4
#BLAKE2s-256(""): 69217a3079908094e11121d042354a7c1f55b6482ca1a51e1b250dfd1ed0eef9
#BLAKE2s-384(""): b32811423377f52d7862286ee1a72ee540524380fda1724a6f25d7978c6fd3244a6caf0498812673c5e05ef583825100
#BLAKE2s-512(""): 786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce
print("\nKey = b'test'")
for x in [224, 256, 384, 512]:
test = Blake2(x, b'test')
print(f"BLAKE2s-{x}(\"\"): {test.hash_digest(b'')}")
#BLAKE2s-224(""): 8032c4a9d7b92692af7100c65319d233abfdfed4f4cdc5de0e5006dc
#BLAKE2s-256(""): e97e5a6ee41f36c29634dcddadc6edc7352a950ec5cb7610058ff63ea7bc4b80
#BLAKE2s-384(""): d998f718982498f390fb3fab366f3f94eb35d0c22ce9f4b2cfde96eb171072d91f071d6617bce70a21967155ff49a8cc
#BLAKE2s-512(""): af007b40b85039c1ac7ca29c4a484e3a614a9fead502fdf5693733ec52d768bc8915b3700a04ae607866141eda16322c9b85b433ccc09f9abd2825c4c23b4f31
"""
=
=