From 891d2a0184697fe9789ebbbf53dee083929e858d Mon Sep 17 00:00:00 2001 From: Roman <90373295+sibkod@users.noreply.github.com> Date: Thu, 15 Aug 2024 14:45:14 +0700 Subject: [PATCH 1/7] pbkdf2 --- vlib/crypto/pbkdf2/pbkdf2.v | 56 ++++++++++++++++++++++++++++++++ vlib/crypto/pbkdf2/pbkdf2_test.v | 16 +++++++++ 2 files changed, 72 insertions(+) create mode 100644 vlib/crypto/pbkdf2/pbkdf2.v create mode 100644 vlib/crypto/pbkdf2/pbkdf2_test.v diff --git a/vlib/crypto/pbkdf2/pbkdf2.v b/vlib/crypto/pbkdf2/pbkdf2.v new file mode 100644 index 00000000000000..8b283fbc3bfbdb --- /dev/null +++ b/vlib/crypto/pbkdf2/pbkdf2.v @@ -0,0 +1,56 @@ +module pbkdf2 + +import crypto.hmac +import crypto.sha256 +import crypto.sha512 +import hash + + +pub fn key(password []u8, salt []u8, count int, key_length int, h hash.Hash) ![]u8 { + mut fun := fn(b []u8) []u8 { + return []u8{} + } + mut block_size := 0 + mut size := 0 + match h { + sha256.Digest { + fun = sha256.sum256 + block_size = sha256.block_size + size = sha256.size + } + sha512.Digest { + fun = sha512.sum512 + block_size = sha512.block_size + size = sha512.size + } + else { + panic("Unsupported hash") + } + } + + hash_length := size + block_count := (key_length + hash_length - 1) / hash_length + mut output := []u8{} + mut last := []u8{} + mut buf := []u8{len: 4, init: 0} + for i := 1; i <= block_count; i++ { + last << salt + + buf[0] = u8(i >> 24) + buf[1] = u8(i >> 16) + buf[2] = u8(i >> 8) + buf[3] = u8(i) + + last << buf + mut xorsum := hmac.new( password, last, fun, block_size) + mut last_hash := xorsum.clone() + for j := 1; j < count; j++ { + last_hash = hmac.new( password,last_hash, fun, block_size) + for k in 0 .. xorsum.len { + xorsum[k] ^= last_hash[k] + } + } + output << xorsum + } + return output[..key_length] +} diff --git a/vlib/crypto/pbkdf2/pbkdf2_test.v b/vlib/crypto/pbkdf2/pbkdf2_test.v new file mode 100644 index 00000000000000..57b014b49a2c3b --- /dev/null +++ b/vlib/crypto/pbkdf2/pbkdf2_test.v @@ -0,0 +1,16 @@ +module pbkdf2 + +import crypto.sha512 +import crypto.sha256 + + +const data= "test" +const password = "123456" + +fn test_sha512() { + assert key(data.bytes(), password.bytes(), 1000, 64, sha512.new())! == [u8(149) 155 168 16 77 243 26 192 128 222 29 139 38 173 131 82 73 152 197 253 66 64 11 103 32 110 95 116 143 4 104 70 176 24 99 48 224 77 47 184 193 59 98 191 18 172 4 119 83 93 198 101 118 131 223 150 215 172 170 166 205 187 247 160] +} + +fn test_sha256() { + assert key(data.bytes(), password.bytes(), 1000, 32, sha256.new())! == [u8(110) 95 68 212 254 34 114 21 43 19 155 141 36 158 236 51 16 244 85 107 245 172 219 25 128 109 111 18 25 14 9 149] +} From 370d9109915f7d78727ceb7e6daa8702570c0923 Mon Sep 17 00:00:00 2001 From: Roman <90373295+sibkod@users.noreply.github.com> Date: Thu, 15 Aug 2024 14:55:47 +0700 Subject: [PATCH 2/7] pbkdf2 fmt --- vlib/crypto/pbkdf2/pbkdf2.v | 9 ++- vlib/crypto/pbkdf2/pbkdf2_test.v | 107 +++++++++++++++++++++++++++++-- 2 files changed, 106 insertions(+), 10 deletions(-) diff --git a/vlib/crypto/pbkdf2/pbkdf2.v b/vlib/crypto/pbkdf2/pbkdf2.v index 8b283fbc3bfbdb..54e4ec9c15c659 100644 --- a/vlib/crypto/pbkdf2/pbkdf2.v +++ b/vlib/crypto/pbkdf2/pbkdf2.v @@ -5,9 +5,8 @@ import crypto.sha256 import crypto.sha512 import hash - pub fn key(password []u8, salt []u8, count int, key_length int, h hash.Hash) ![]u8 { - mut fun := fn(b []u8) []u8 { + mut fun := fn (b []u8) []u8 { return []u8{} } mut block_size := 0 @@ -24,7 +23,7 @@ pub fn key(password []u8, salt []u8, count int, key_length int, h hash.Hash) ![] size = sha512.size } else { - panic("Unsupported hash") + panic('Unsupported hash') } } @@ -42,10 +41,10 @@ pub fn key(password []u8, salt []u8, count int, key_length int, h hash.Hash) ![] buf[3] = u8(i) last << buf - mut xorsum := hmac.new( password, last, fun, block_size) + mut xorsum := hmac.new(password, last, fun, block_size) mut last_hash := xorsum.clone() for j := 1; j < count; j++ { - last_hash = hmac.new( password,last_hash, fun, block_size) + last_hash = hmac.new(password, last_hash, fun, block_size) for k in 0 .. xorsum.len { xorsum[k] ^= last_hash[k] } diff --git a/vlib/crypto/pbkdf2/pbkdf2_test.v b/vlib/crypto/pbkdf2/pbkdf2_test.v index 57b014b49a2c3b..eeccf7594ec4e1 100644 --- a/vlib/crypto/pbkdf2/pbkdf2_test.v +++ b/vlib/crypto/pbkdf2/pbkdf2_test.v @@ -3,14 +3,111 @@ module pbkdf2 import crypto.sha512 import crypto.sha256 - -const data= "test" -const password = "123456" +const data = 'test' +const password = '123456' fn test_sha512() { - assert key(data.bytes(), password.bytes(), 1000, 64, sha512.new())! == [u8(149) 155 168 16 77 243 26 192 128 222 29 139 38 173 131 82 73 152 197 253 66 64 11 103 32 110 95 116 143 4 104 70 176 24 99 48 224 77 47 184 193 59 98 191 18 172 4 119 83 93 198 101 118 131 223 150 215 172 170 166 205 187 247 160] + assert key(pbkdf2.data.bytes(), pbkdf2.password.bytes(), 1000, 64, sha512.new())! == [ + u8(149), + 155, + 168, + 16, + 77, + 243, + 26, + 192, + 128, + 222, + 29, + 139, + 38, + 173, + 131, + 82, + 73, + 152, + 197, + 253, + 66, + 64, + 11, + 103, + 32, + 110, + 95, + 116, + 143, + 4, + 104, + 70, + 176, + 24, + 99, + 48, + 224, + 77, + 47, + 184, + 193, + 59, + 98, + 191, + 18, + 172, + 4, + 119, + 83, + 93, + 198, + 101, + 118, + 131, + 223, + 150, + 215, + 172, + 170, + 166, + 205, + 187, + 247, + 160, + ] } fn test_sha256() { - assert key(data.bytes(), password.bytes(), 1000, 32, sha256.new())! == [u8(110) 95 68 212 254 34 114 21 43 19 155 141 36 158 236 51 16 244 85 107 245 172 219 25 128 109 111 18 25 14 9 149] + assert key(pbkdf2.data.bytes(), pbkdf2.password.bytes(), 1000, 32, sha256.new())! == [ + u8(110), + 95, + 68, + 212, + 254, + 34, + 114, + 21, + 43, + 19, + 155, + 141, + 36, + 158, + 236, + 51, + 16, + 244, + 85, + 107, + 245, + 172, + 219, + 25, + 128, + 109, + 111, + 18, + 25, + 14, + 9, + 149, + ] } From f94a937f5af03c600b22f52661c2943c6455854c Mon Sep 17 00:00:00 2001 From: Roman <90373295+sibkod@users.noreply.github.com> Date: Thu, 15 Aug 2024 19:26:39 +0700 Subject: [PATCH 3/7] Update vlib/crypto/pbkdf2/pbkdf2.v Co-authored-by: Delyan Angelov --- vlib/crypto/pbkdf2/pbkdf2.v | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vlib/crypto/pbkdf2/pbkdf2.v b/vlib/crypto/pbkdf2/pbkdf2.v index 54e4ec9c15c659..77c072fb90d578 100644 --- a/vlib/crypto/pbkdf2/pbkdf2.v +++ b/vlib/crypto/pbkdf2/pbkdf2.v @@ -31,7 +31,7 @@ pub fn key(password []u8, salt []u8, count int, key_length int, h hash.Hash) ![] block_count := (key_length + hash_length - 1) / hash_length mut output := []u8{} mut last := []u8{} - mut buf := []u8{len: 4, init: 0} + mut buf := []u8{len: 4} for i := 1; i <= block_count; i++ { last << salt From 9d68003d757f4dcdeccb72ba393b5678114aad2e Mon Sep 17 00:00:00 2001 From: Roman <90373295+sibkod@users.noreply.github.com> Date: Thu, 15 Aug 2024 19:28:56 +0700 Subject: [PATCH 4/7] pbkdf2 fmt --- vlib/crypto/pbkdf2/pbkdf2.v | 2 + vlib/crypto/pbkdf2/pbkdf2_test.v | 108 ++----------------------------- 2 files changed, 9 insertions(+), 101 deletions(-) diff --git a/vlib/crypto/pbkdf2/pbkdf2.v b/vlib/crypto/pbkdf2/pbkdf2.v index 77c072fb90d578..08b87898715bca 100644 --- a/vlib/crypto/pbkdf2/pbkdf2.v +++ b/vlib/crypto/pbkdf2/pbkdf2.v @@ -5,6 +5,8 @@ import crypto.sha256 import crypto.sha512 import hash +// key derives a key from the password, salt and iteration count +// example pbkdf2.key('test'.bytes(), '123456'.bytes(), 1000, 64, sha512.new() pub fn key(password []u8, salt []u8, count int, key_length int, h hash.Hash) ![]u8 { mut fun := fn (b []u8) []u8 { return []u8{} diff --git a/vlib/crypto/pbkdf2/pbkdf2_test.v b/vlib/crypto/pbkdf2/pbkdf2_test.v index eeccf7594ec4e1..1d60e55dec91a4 100644 --- a/vlib/crypto/pbkdf2/pbkdf2_test.v +++ b/vlib/crypto/pbkdf2/pbkdf2_test.v @@ -1,113 +1,19 @@ -module pbkdf2 import crypto.sha512 import crypto.sha256 +import vlib.crypto.pbkdf2 const data = 'test' const password = '123456' + fn test_sha512() { - assert key(pbkdf2.data.bytes(), pbkdf2.password.bytes(), 1000, 64, sha512.new())! == [ - u8(149), - 155, - 168, - 16, - 77, - 243, - 26, - 192, - 128, - 222, - 29, - 139, - 38, - 173, - 131, - 82, - 73, - 152, - 197, - 253, - 66, - 64, - 11, - 103, - 32, - 110, - 95, - 116, - 143, - 4, - 104, - 70, - 176, - 24, - 99, - 48, - 224, - 77, - 47, - 184, - 193, - 59, - 98, - 191, - 18, - 172, - 4, - 119, - 83, - 93, - 198, - 101, - 118, - 131, - 223, - 150, - 215, - 172, - 170, - 166, - 205, - 187, - 247, - 160, - ] + // vfmt off + assert pbkdf2.key(data.bytes(), password.bytes(), 1000, 64, sha512.new())! == [u8(149) 155 168 16 77 243 26 192 128 222 29 139 38 173 131 82 73 152 197 253 66 64 11 103 32 110 95 116 143 4 104 70 176 24 99 48 224 77 47 184 193 59 98 191 18 172 4 119 83 93 198 101 118 131 223 150 215 172 170 166 205 187 247 160] } + fn test_sha256() { - assert key(pbkdf2.data.bytes(), pbkdf2.password.bytes(), 1000, 32, sha256.new())! == [ - u8(110), - 95, - 68, - 212, - 254, - 34, - 114, - 21, - 43, - 19, - 155, - 141, - 36, - 158, - 236, - 51, - 16, - 244, - 85, - 107, - 245, - 172, - 219, - 25, - 128, - 109, - 111, - 18, - 25, - 14, - 9, - 149, - ] + // vfmt off + assert pbkdf2.key(data.bytes(), password.bytes(), 1000, 32, sha256.new())! == [u8(110) 95 68 212 254 34 114 21 43 19 155 141 36 158 236 51 16 244 85 107 245 172 219 25 128 109 111 18 25 14 9 149] } From 22e78baa788348c3e4c4df7aad6344ac826b1b8b Mon Sep 17 00:00:00 2001 From: Roman <90373295+sibkod@users.noreply.github.com> Date: Thu, 15 Aug 2024 19:49:11 +0700 Subject: [PATCH 5/7] pbkdf2 fmt --- vlib/crypto/pbkdf2/pbkdf2.v | 2 +- vlib/crypto/pbkdf2/pbkdf2_test.v | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/vlib/crypto/pbkdf2/pbkdf2.v b/vlib/crypto/pbkdf2/pbkdf2.v index 08b87898715bca..518ade62590cdc 100644 --- a/vlib/crypto/pbkdf2/pbkdf2.v +++ b/vlib/crypto/pbkdf2/pbkdf2.v @@ -6,7 +6,7 @@ import crypto.sha512 import hash // key derives a key from the password, salt and iteration count -// example pbkdf2.key('test'.bytes(), '123456'.bytes(), 1000, 64, sha512.new() +// example pbkdf2.key('test'.bytes(), '123456'.bytes(), 1000, 64, sha512.new()) pub fn key(password []u8, salt []u8, count int, key_length int, h hash.Hash) ![]u8 { mut fun := fn (b []u8) []u8 { return []u8{} diff --git a/vlib/crypto/pbkdf2/pbkdf2_test.v b/vlib/crypto/pbkdf2/pbkdf2_test.v index 1d60e55dec91a4..35ad0cbd7da8b0 100644 --- a/vlib/crypto/pbkdf2/pbkdf2_test.v +++ b/vlib/crypto/pbkdf2/pbkdf2_test.v @@ -1,19 +1,18 @@ - import crypto.sha512 import crypto.sha256 -import vlib.crypto.pbkdf2 +import crypto.pbkdf2 const data = 'test' const password = '123456' - fn test_sha512() { // vfmt off assert pbkdf2.key(data.bytes(), password.bytes(), 1000, 64, sha512.new())! == [u8(149) 155 168 16 77 243 26 192 128 222 29 139 38 173 131 82 73 152 197 253 66 64 11 103 32 110 95 116 143 4 104 70 176 24 99 48 224 77 47 184 193 59 98 191 18 172 4 119 83 93 198 101 118 131 223 150 215 172 170 166 205 187 247 160] + // vfmt on } - fn test_sha256() { // vfmt off assert pbkdf2.key(data.bytes(), password.bytes(), 1000, 32, sha256.new())! == [u8(110) 95 68 212 254 34 114 21 43 19 155 141 36 158 236 51 16 244 85 107 245 172 219 25 128 109 111 18 25 14 9 149] + // vfmt on } From f77db87afe4194e2350947c9283b58197e656db1 Mon Sep 17 00:00:00 2001 From: Roman <90373295+sibkod@users.noreply.github.com> Date: Thu, 15 Aug 2024 21:45:37 +0700 Subject: [PATCH 6/7] Update pbkdf2.v --- vlib/crypto/pbkdf2/pbkdf2.v | 1 + 1 file changed, 1 insertion(+) diff --git a/vlib/crypto/pbkdf2/pbkdf2.v b/vlib/crypto/pbkdf2/pbkdf2.v index 518ade62590cdc..7f03717c35554f 100644 --- a/vlib/crypto/pbkdf2/pbkdf2.v +++ b/vlib/crypto/pbkdf2/pbkdf2.v @@ -1,3 +1,4 @@ +//Based off: https://golang.org/x/crypto/pbkdf2 module pbkdf2 import crypto.hmac From 5ebb93a1b4a3d15170c979a8ec8a41226fed65a7 Mon Sep 17 00:00:00 2001 From: Roman <90373295+sibkod@users.noreply.github.com> Date: Thu, 15 Aug 2024 22:10:19 +0700 Subject: [PATCH 7/7] pbkdf2 fmt --- vlib/crypto/pbkdf2/pbkdf2.v | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vlib/crypto/pbkdf2/pbkdf2.v b/vlib/crypto/pbkdf2/pbkdf2.v index 7f03717c35554f..a321c4573e21f8 100644 --- a/vlib/crypto/pbkdf2/pbkdf2.v +++ b/vlib/crypto/pbkdf2/pbkdf2.v @@ -1,4 +1,4 @@ -//Based off: https://golang.org/x/crypto/pbkdf2 +// Based off: https://golang.org/x/crypto/pbkdf2 module pbkdf2 import crypto.hmac