1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vlib.crypto+other: fix mut args

This commit is contained in:
joe-conigliaro
2019-08-07 21:37:07 +10:00
committed by Alexander Medvednikov
parent 818c4a14e5
commit 511a3d3901
15 changed files with 53 additions and 43 deletions

View File

@@ -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')
}

View File

@@ -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'
}

View File

@@ -160,7 +160,7 @@ fn rotw(w u32) u32 { return u32(w<<u32(8)) | 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

View File

@@ -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
}