From 511a3d390169d10811bfc499037a485728235286 Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Wed, 7 Aug 2019 21:37:07 +1000 Subject: [PATCH] vlib.crypto+other: fix mut args --- compiler/main.v | 2 +- vlib/crypto/aes/aes_cbc.v | 11 ++++++----- vlib/crypto/aes/aes_test.v | 2 +- vlib/crypto/aes/block_generic.v | 2 +- vlib/crypto/aes/cypher_generic.v | 4 ++-- vlib/crypto/cipher/xor_generic.v | 2 +- vlib/crypto/md5/md5.v | 11 ++++++----- vlib/crypto/rc4/rc4.v | 4 ++-- vlib/crypto/rc4/rc4_test.v | 2 +- vlib/crypto/sha1/sha1.v | 13 +++++++------ vlib/crypto/sha1/sha1block_generic.v | 3 ++- vlib/crypto/sha256/sha256.v | 13 +++++++------ vlib/crypto/sha256/sha256block_generic.v | 4 +++- vlib/crypto/sha512/sha512.v | 19 ++++++++++--------- vlib/crypto/sha512/sha512block_generic.v | 4 +++- 15 files changed, 53 insertions(+), 43 deletions(-) diff --git a/compiler/main.v b/compiler/main.v index 4b2851e07e..46aa0088bc 100644 --- a/compiler/main.v +++ b/compiler/main.v @@ -747,7 +747,7 @@ fn (v mut V) cc() { } $if windows { if v.os == .msvc { - cc_msvc(v) + cc_msvc(mut v) return } } diff --git a/vlib/crypto/aes/aes_cbc.v b/vlib/crypto/aes/aes_cbc.v index 968025b026..56bd9ae5a6 100644 --- a/vlib/crypto/aes/aes_cbc.v +++ b/vlib/crypto/aes/aes_cbc.v @@ -47,7 +47,8 @@ pub fn new_cbc(b AesCipher, iv []byte) AesCbc { pub fn (x &AesCbc) block_size() int { return x.block_size } -pub fn (x &AesCbc) encrypt_blocks(dst, src []byte) { +pub fn (x mut AesCbc) encrypt_blocks(dst mut []byte, src_ []byte) { + mut src := src_ if src.len%x.block_size != 0 { panic('crypto.cipher: input not full blocks') } @@ -72,14 +73,14 @@ pub fn (x &AesCbc) encrypt_blocks(dst, src []byte) { } else { src = src.right(x.block_size) } - dst = dst.right(x.block_size) + *dst = dst.right(x.block_size) } // Save the iv for the next crypt_blocks call. copy(x.iv, iv) } -pub fn (x &AesCbc) decrypt_blocks(dst mut []byte, src []byte) { +pub fn (x mut AesCbc) decrypt_blocks(dst mut []byte, src []byte) { if src.len%x.block_size != 0 { panic('crypto.cipher: input not full blocks') } @@ -105,7 +106,7 @@ pub fn (x &AesCbc) decrypt_blocks(dst mut []byte, src []byte) { // Loop over all but the first block. for start > 0 { x.b.decrypt(dst.slice(start, end), src.slice(start, end)) - cipher.xor_bytes(dst.slice(start, end), dst.slice(start, end), src.slice(prev, start)) + cipher.xor_bytes(mut dst.slice(start, end), dst.slice(start, end), src.slice(prev, start)) end = start start = prev @@ -122,7 +123,7 @@ pub fn (x &AesCbc) decrypt_blocks(dst mut []byte, src []byte) { x.tmp = x.iv } -fn (x &AesCbc) set_iv(iv []byte) { +fn (x mut AesCbc) set_iv(iv []byte) { if iv.len != x.iv.len { panic('cipher: incorrect length IV') } diff --git a/vlib/crypto/aes/aes_test.v b/vlib/crypto/aes/aes_test.v index f187f0fdf7..c614746cb1 100644 --- a/vlib/crypto/aes/aes_test.v +++ b/vlib/crypto/aes/aes_test.v @@ -21,7 +21,7 @@ fn test_crypto_aes() { panic('ciphertext is not a multiple of the block size') } mode := aes.new_cbc(block, iv) - mode.encrypt_blocks(ciphertext, ciphertext) + mode.encrypt_blocks(mut ciphertext, ciphertext) assert ciphertext.hex() == 'c210459b514668ddc44674885e4979215265a6c44431a248421254ef357a8c2a308a8bddf5623af9df91737562041cf1' } diff --git a/vlib/crypto/aes/block_generic.v b/vlib/crypto/aes/block_generic.v index a799187666..8a5c0e7c75 100644 --- a/vlib/crypto/aes/block_generic.v +++ b/vlib/crypto/aes/block_generic.v @@ -160,7 +160,7 @@ fn rotw(w u32) u32 { return u32(w<>u32(24)) } // Key expansion algorithm. See FIPS-197, Figure 11. // Their rcon[i] is our powx[i-1] << 24. -fn expand_key_generic(key []byte, enc, dec []u32) { +fn expand_key_generic(key []byte, enc mut []u32, dec mut []u32) { // Encryption key setup. mut i := 0 nk := key.len / 4 diff --git a/vlib/crypto/aes/cypher_generic.v b/vlib/crypto/aes/cypher_generic.v index f9abbc0bf0..813d4a093b 100644 --- a/vlib/crypto/aes/cypher_generic.v +++ b/vlib/crypto/aes/cypher_generic.v @@ -13,10 +13,10 @@ import ( // this is the generiv v version, no arch optimisations fn new_cipher_generic(key []byte) AesCipher { n := key.len + 28 - c := AesCipher{ + mut c := AesCipher{ enc: [u32(0); n] dec: [u32(0); n] } - expand_key_generic(key, c.enc, c.dec) + expand_key_generic(key, mut c.enc, mut c.dec) return c } diff --git a/vlib/crypto/cipher/xor_generic.v b/vlib/crypto/cipher/xor_generic.v index 12d6a497c9..30b92b5331 100644 --- a/vlib/crypto/cipher/xor_generic.v +++ b/vlib/crypto/cipher/xor_generic.v @@ -31,6 +31,6 @@ pub fn safe_xor_bytes(dst mut []byte, a, b []byte, n int) { // fast_xor_words XORs multiples of 4 or 8 bytes (depending on architecture.) // The slice arguments a and b are assumed to be of equal length. -pub fn xor_words(dst, a, b []byte) { +pub fn xor_words(dst mut []byte, a, b []byte) { safe_xor_bytes(mut dst, a, b, b.len) } diff --git a/vlib/crypto/md5/md5.v b/vlib/crypto/md5/md5.v index 5d22b52a44..0b2afd0f93 100644 --- a/vlib/crypto/md5/md5.v +++ b/vlib/crypto/md5/md5.v @@ -55,14 +55,15 @@ pub fn new() *Digest { return d } -pub fn (d mut Digest) write(p mut []byte) ?int { +pub fn (d mut Digest) write(p_ []byte) ?int { + mut p := p_ nn := p.len d.len += u64(nn) if d.nx > 0 { n := copy(d.x.right(d.nx), p) d.nx += n if d.nx == BlockSize { - block(d, d.x) + block(mut d, d.x) d.nx = 0 } if n >= p.len { @@ -73,7 +74,7 @@ pub fn (d mut Digest) write(p mut []byte) ?int { } if p.len >= BlockSize { n := p.len &~ (BlockSize - 1) - block(d, p.left(n)) + block(mut d, p.left(n)) if n >= p.len { p = []byte } else { @@ -107,7 +108,7 @@ pub fn (d mut Digest) checksum() []byte { tmp[0] = 0x80 pad := (55 - int(d.len)) % 64 // calculate number of padding bytes binary.little_endian_put_u64(mut tmp.right(1+pad), u64(d.len< 0 { @@ -145,15 +146,15 @@ fn (d mut Digest) checksum() []byte { mut tmp := [byte(0); 64] tmp[0] = 0x80 if int(len)%64 < 56 { - d.write(mut tmp.left(56-int(len)%64)) + d.write(tmp.left(56-int(len)%64)) } else { - d.write(mut tmp.left(64+56-int(len)%64)) + d.write(tmp.left(64+56-int(len)%64)) } // Length in bits. len <<= u64(3) binary.big_endian_put_u64(mut tmp, len) - d.write(mut tmp.left(8)) + d.write(tmp.left(8)) if d.nx != 0 { panic('d.nx != 0') @@ -183,14 +184,14 @@ pub fn sum(data []byte) []byte { // sum256 returns the SHA256 checksum of the data. pub fn sum256(data []byte) []byte { mut d := new() - d.write(mut data) + d.write(data) return d.checksum() } // sum224 returns the SHA224 checksum of the data. pub fn sum224(data []byte) []byte { mut d := new224() - d.write(mut data) + d.write(data) sum := d.checksum() mut sum224 := [byte(0); Size224] copy(sum224, sum.left(Size224)) diff --git a/vlib/crypto/sha256/sha256block_generic.v b/vlib/crypto/sha256/sha256block_generic.v index c5bebef24e..8589704814 100644 --- a/vlib/crypto/sha256/sha256block_generic.v +++ b/vlib/crypto/sha256/sha256block_generic.v @@ -80,7 +80,9 @@ const ( ] ) -fn block_generic(dig mut Digest, p []byte) { +fn block_generic(dig mut Digest, p_ []byte) { + mut p := p_ + mut w := [u32(0); 64] mut h0 := dig.h[0] diff --git a/vlib/crypto/sha512/sha512.v b/vlib/crypto/sha512/sha512.v index 1cf0d2a511..3b84befb19 100644 --- a/vlib/crypto/sha512/sha512.v +++ b/vlib/crypto/sha512/sha512.v @@ -146,7 +146,8 @@ fn new384() *Digest { return _new(crypto.Hash.SHA384) } -fn (d mut Digest) write(p mut []byte) ?int { +fn (d mut Digest) write(p_ []byte) ?int { + mut p := p_ nn := p.len d.len += u64(nn) if d.nx > 0 { @@ -209,9 +210,9 @@ fn (d mut Digest) checksum() []byte { tmp[0] = 0x80 if int(len)%128 < 112 { - d.write(mut tmp.left(112-int(len)%128)) + d.write(tmp.left(112-int(len)%128)) } else { - d.write(mut tmp.left(128+112-int(len)%128)) + d.write(tmp.left(128+112-int(len)%128)) } // Length in bits. @@ -219,7 +220,7 @@ fn (d mut Digest) checksum() []byte { binary.big_endian_put_u64(mut tmp, u64(0)) // upper 64 bits are always zero, because len variable has type u64 binary.big_endian_put_u64(mut tmp.right(8), len) - d.write(mut tmp.left(16)) + d.write(tmp.left(16)) if d.nx != 0 { panic('d.nx != 0') @@ -244,14 +245,14 @@ fn (d mut Digest) checksum() []byte { // sum512 returns the SHA512 checksum of the data. pub fn sum512(data []byte) []byte { mut d := _new(crypto.Hash.SHA512) - d.write(mut data) + d.write(data) return d.checksum() } // sum384 returns the SHA384 checksum of the data. pub fn sum384(data []byte) []byte { mut d := _new(crypto.Hash.SHA384) - d.write(mut data) + d.write(data) sum := d.checksum() mut sum384 := [byte(0); Size384] copy(sum384, sum.left(Size384)) @@ -261,7 +262,7 @@ pub fn sum384(data []byte) []byte { // sum512_224 returns the Sum512/224 checksum of the data. pub fn sum512_224(data []byte) []byte { mut d := _new(crypto.Hash.SHA512_224) - d.write(mut data) + d.write(data) sum := d.checksum() mut sum224 := [byte(0); Size224] copy(sum224, sum.left(Size224)) @@ -271,7 +272,7 @@ pub fn sum512_224(data []byte) []byte { // Sum512_256 returns the Sum512/256 checksum of the data. pub fn sum512_256(data []byte) []byte { mut d := _new(crypto.Hash.SHA512_256) - d.write(mut data) + d.write(data) sum := d.checksum() mut sum256 := [byte(0); Size256] copy(sum256, sum.left(Size256)) @@ -281,7 +282,7 @@ pub fn sum512_256(data []byte) []byte { fn block(dig mut Digest, p []byte) { // For now just use block_generic until we have specific // architecture optimized versions - block_generic(mut dig, mut p) + block_generic(mut dig, p) } pub fn (d &Digest) size() int { diff --git a/vlib/crypto/sha512/sha512block_generic.v b/vlib/crypto/sha512/sha512block_generic.v index e5cf405815..a2a7a9a9a0 100644 --- a/vlib/crypto/sha512/sha512block_generic.v +++ b/vlib/crypto/sha512/sha512block_generic.v @@ -94,7 +94,9 @@ const( ] ) -fn block_generic(dig mut Digest, p mut []byte) { +fn block_generic(dig mut Digest, p_ []byte) { + mut p := p_ + mut w := [u64(0); 80] mut h0 := dig.h[0]