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

crypto: use new copy & clone

This commit is contained in:
joe-conigliaro 2019-07-30 00:33:35 +10:00 committed by Alexander Medvednikov
parent 6701c3b263
commit d87030972e
7 changed files with 60 additions and 86 deletions

View File

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

View File

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

View File

@ -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<n; i++ {
d.x.set(i+d.nx, p[i])
}
n := copy(d.x.right(d.nx), p)
d.nx += n
if d.nx == BlockSize {
block(d, d.x)
@ -84,10 +80,7 @@ pub fn (d mut Digest) write(p []byte) ?int {
}
}
if p.len > 0 {
d.nx = int(math.min(f64(d.x.len), f64(p.len)))
for i:=0; i<d.nx; i++ {
d.x.set(i, p[i])
}
d.nx = copy(d.x, p)
}
return nn
}

View File

@ -8,8 +8,10 @@
module md5
import math.bits
import encoding.binary
import (
math.bits
encoding.binary
)
fn block_generic(dig &Digest, p []byte) {
// load state

View File

@ -11,7 +11,6 @@
module sha1
import math
import encoding.binary
const(
@ -63,10 +62,7 @@ pub fn (d mut Digest) write(p []byte) ?int {
d.len += u64(nn)
if d.nx > 0 {
n := int(math.min(f64(d.x.len), f64(p.len)))
for i:=0; i<n; i++ {
d.x.set(i+d.nx, p[i])
}
n := copy(d.x.right(d.nx), p)
d.nx += n
if d.nx == Chunk {
block(d, d.x)
@ -88,10 +84,7 @@ pub fn (d mut Digest) write(p []byte) ?int {
}
}
if p.len > 0 {
d.nx = int(math.min(f64(d.x.len), f64(p.len)))
for i:=0; i<d.nx; i++ {
d.x.set(i, p[i])
}
d.nx = copy(d.x, p)
}
return nn
}

View File

@ -9,7 +9,6 @@
module sha256
import math
import encoding.binary
const (
@ -96,10 +95,7 @@ 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<n; i++ {
d.x.set(i+d.nx, p[i])
}
n := copy(d.x.right(d.nx), p)
d.nx += n
if d.nx == Chunk {
block(d, d.x)
@ -121,10 +117,7 @@ fn (d mut Digest) write(p []byte) ?int {
}
}
if p.len > 0 {
d.nx = int(math.min(f64(d.x.len), f64(p.len)))
for i:=0; i<d.nx; i++ {
d.x.set(i, p[i])
}
d.nx = copy(d.x, p)
}
return nn
}
@ -198,7 +191,8 @@ pub fn sum224(data []byte) []byte {
mut d := new224()
d.write(data)
sum := d.checksum()
sum224 := sum.left(Size224)
mut sum224 := [byte(0); Size224]
copy(sum224, sum.left(Size224))
return sum224
}

View File

@ -9,9 +9,10 @@
module sha512
import math
import crypto
import encoding.binary
import (
crypto
encoding.binary
)
const (
// Size is the size, in bytes, of a SHA-512 checksum.
@ -78,41 +79,41 @@ fn (d mut Digest) reset() {
d.x = [byte(0); Chunk]
switch d.function {
case crypto.Hash.SHA384:
d.h[0] = u64(Init0_384)
d.h[1] = u64(Init1_384)
d.h[2] = u64(Init2_384)
d.h[3] = u64(Init3_384)
d.h[4] = u64(Init4_384)
d.h[5] = u64(Init5_384)
d.h[6] = u64(Init6_384)
d.h[7] = u64(Init7_384)
d.h[0] = Init0_384
d.h[1] = Init1_384
d.h[2] = Init2_384
d.h[3] = Init3_384
d.h[4] = Init4_384
d.h[5] = Init5_384
d.h[6] = Init6_384
d.h[7] = Init7_384
case crypto.Hash.SHA512_224:
d.h[0] = u64(Init0_224)
d.h[1] = u64(Init1_224)
d.h[2] = u64(Init2_224)
d.h[3] = u64(Init3_224)
d.h[4] = u64(Init4_224)
d.h[5] = u64(Init5_224)
d.h[6] = u64(Init6_224)
d.h[7] = u64(Init7_224)
d.h[0] = Init0_224
d.h[1] = Init1_224
d.h[2] = Init2_224
d.h[3] = Init3_224
d.h[4] = Init4_224
d.h[5] = Init5_224
d.h[6] = Init6_224
d.h[7] = Init7_224
case crypto.Hash.SHA512_256:
d.h[0] = u64(Init0_256)
d.h[1] = u64(Init1_256)
d.h[2] = u64(Init2_256)
d.h[3] = u64(Init3_256)
d.h[4] = u64(Init4_256)
d.h[5] = u64(Init5_256)
d.h[6] = u64(Init6_256)
d.h[7] = u64(Init7_256)
d.h[0] = Init0_256
d.h[1] = Init1_256
d.h[2] = Init2_256
d.h[3] = Init3_256
d.h[4] = Init4_256
d.h[5] = Init5_256
d.h[6] = Init6_256
d.h[7] = Init7_256
default:
d.h[0] = u64(Init0)
d.h[1] = u64(Init1)
d.h[2] = u64(Init2)
d.h[3] = u64(Init3)
d.h[4] = u64(Init4)
d.h[5] = u64(Init5)
d.h[6] = u64(Init6)
d.h[7] = u64(Init7)
d.h[0] = Init0
d.h[1] = Init1
d.h[2] = Init2
d.h[3] = Init3
d.h[4] = Init4
d.h[5] = Init5
d.h[6] = Init6
d.h[7] = Init7
}
d.nx = 0
d.len = u64(0)
@ -148,10 +149,7 @@ 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<n; i++ {
d.x.set(i+d.nx, p[i])
}
n := copy(d.x.right(d.nx), p)
d.nx += n
if d.nx == Chunk {
block(d, d.x)
@ -173,10 +171,7 @@ fn (d mut Digest) write(p []byte) ?int {
}
}
if p.len > 0 {
d.nx = int(math.min(f64(d.x.len), f64(p.len)))
for i:=0; i<d.nx; i++ {
d.x.set(i, p[i])
}
d.nx = copy(d.x, p)
}
return nn
}
@ -257,7 +252,8 @@ pub fn sum384(data []byte) []byte {
mut d := _new(crypto.Hash.SHA384)
d.write(data)
sum := d.checksum()
sum384 := sum.left(Size384)
mut sum384 := [byte(0); Size384]
copy(sum384, sum.left(Size384))
return sum384
}
@ -266,7 +262,8 @@ pub fn sum512_224(data []byte) []byte {
mut d := _new(crypto.Hash.SHA512_224)
d.write(data)
sum := d.checksum()
sum224 := sum.left(Size224)
mut sum224 := [byte(0); Size224]
copy(sum224, sum.left(Size224))
return sum224
}
@ -275,7 +272,8 @@ pub fn sum512_256(data []byte) []byte {
mut d := _new(crypto.Hash.SHA512_256)
d.write(data)
sum := d.checksum()
sum256 := sum.left(Size256)
mut sum256 := [byte(0); Size256]
copy(sum256, sum.left(Size256))
return sum256
}