From d87030972eb347f588ad6e56ca02198837d60b43 Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Tue, 30 Jul 2019 00:33:35 +1000 Subject: [PATCH] crypto: use new copy & clone --- vlib/crypto/aes/aes_cbc.v | 12 ++-- vlib/crypto/aes/const.v | 2 - vlib/crypto/md5/md5.v | 11 +--- vlib/crypto/md5/md5block_generic.v | 6 +- vlib/crypto/sha1/sha1.v | 11 +--- vlib/crypto/sha256/sha256.v | 14 ++--- vlib/crypto/sha512/sha512.v | 90 +++++++++++++++--------------- 7 files changed, 60 insertions(+), 86 deletions(-) diff --git a/vlib/crypto/aes/aes_cbc.v b/vlib/crypto/aes/aes_cbc.v index c3971366d0..abca512bc2 100644 --- a/vlib/crypto/aes/aes_cbc.v +++ b/vlib/crypto/aes/aes_cbc.v @@ -30,8 +30,7 @@ fn _new_cbc(b AesCipher, iv []byte) AesCbc { return AesCbc{ b: b, block_size: b.block_size(), - // TODO: make b.iv copy of iv - iv: iv, + iv: iv.clone(), tmp: [byte(0); b.block_size()], } } @@ -77,8 +76,7 @@ pub fn (x &AesCbc) encrypt_blocks(dst, src []byte) { } // Save the iv for the next crypt_blocks call. - // TODO: make x.iv a copy of iv - x.iv = iv + copy(x.iv, iv) } pub fn (x &AesCbc) decrypt_blocks(dst, src []byte) { @@ -102,8 +100,7 @@ pub fn (x &AesCbc) decrypt_blocks(dst, src []byte) { mut prev := start - x.block_size // Copy the last block of ciphertext in preparation as the new iv. - // TODO: copy - x.tmp = src.slice(start, end) + copy(x.tmp, src.slice(start, end)) // Loop over all but the first block. for start > 0 { @@ -129,6 +126,5 @@ fn (x &AesCbc) set_iv(iv []byte) { if iv.len != x.iv.len { panic('cipher: incorrect length IV') } - // TODO: make x.iv a copy of iv - x.iv = iv + copy(x.iv, iv) } diff --git a/vlib/crypto/aes/const.v b/vlib/crypto/aes/const.v index 9cb1ab6f79..1ed6b4526c 100644 --- a/vlib/crypto/aes/const.v +++ b/vlib/crypto/aes/const.v @@ -93,7 +93,6 @@ const ( ) // Lookup tables for encryption. -// These can be recomputed by adapting the tests in aes_test.go. const ( Te0 = [ @@ -235,7 +234,6 @@ const ( ) // Lookup tables for decryption. -// These can be recomputed by adapting the tests in aes_test.go. const ( Td0 = [ 0x51f4a750, 0x7e416553, 0x1a17a4c3, 0x3a275e96, 0x3bab6bcb, 0x1f9d45f1, 0xacfa58ab, 0x4be30393, diff --git a/vlib/crypto/md5/md5.v b/vlib/crypto/md5/md5.v index 4d669447d4..d0dc630d87 100644 --- a/vlib/crypto/md5/md5.v +++ b/vlib/crypto/md5/md5.v @@ -11,7 +11,6 @@ module md5 -import math import encoding.binary const ( @@ -59,10 +58,7 @@ pub fn (d mut Digest) write(p []byte) ?int { nn := p.len d.len += u64(nn) if d.nx > 0 { - n := int(math.min(f64(d.x.len), f64(p.len))) - for i:=0; i 0 { - d.nx = int(math.min(f64(d.x.len), f64(p.len))) - for i:=0; i 0 { - n := int(math.min(f64(d.x.len), f64(p.len))) - for i:=0; i 0 { - d.nx = int(math.min(f64(d.x.len), f64(p.len))) - for i:=0; i 0 { - n := int(math.min(f64(d.x.len), f64(p.len))) - for i:=0; i 0 { - d.nx = int(math.min(f64(d.x.len), f64(p.len))) - for i:=0; i 0 { - n := int(math.min(f64(d.x.len), f64(p.len))) - for i:=0; i 0 { - d.nx = int(math.min(f64(d.x.len), f64(p.len))) - for i:=0; i