crypto: use new copy & clone

pull/1369/head
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{ return AesCbc{
b: b, b: b,
block_size: b.block_size(), block_size: b.block_size(),
// TODO: make b.iv copy of iv iv: iv.clone(),
iv: iv,
tmp: [byte(0); b.block_size()], 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. // Save the iv for the next crypt_blocks call.
// TODO: make x.iv a copy of iv copy(x.iv, iv)
x.iv = iv
} }
pub fn (x &AesCbc) decrypt_blocks(dst, src []byte) { 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 mut prev := start - x.block_size
// Copy the last block of ciphertext in preparation as the new iv. // Copy the last block of ciphertext in preparation as the new iv.
// TODO: copy copy(x.tmp, src.slice(start, end))
x.tmp = src.slice(start, end)
// Loop over all but the first block. // Loop over all but the first block.
for start > 0 { for start > 0 {
@ -129,6 +126,5 @@ fn (x &AesCbc) set_iv(iv []byte) {
if iv.len != x.iv.len { if iv.len != x.iv.len {
panic('cipher: incorrect length IV') panic('cipher: incorrect length IV')
} }
// TODO: make x.iv a copy of iv copy(x.iv, iv)
x.iv = iv
} }

View File

@ -93,7 +93,6 @@ const (
) )
// Lookup tables for encryption. // Lookup tables for encryption.
// These can be recomputed by adapting the tests in aes_test.go.
const ( const (
Te0 = [ Te0 = [
@ -235,7 +234,6 @@ const (
) )
// Lookup tables for decryption. // Lookup tables for decryption.
// These can be recomputed by adapting the tests in aes_test.go.
const ( const (
Td0 = [ Td0 = [
0x51f4a750, 0x7e416553, 0x1a17a4c3, 0x3a275e96, 0x3bab6bcb, 0x1f9d45f1, 0xacfa58ab, 0x4be30393, 0x51f4a750, 0x7e416553, 0x1a17a4c3, 0x3a275e96, 0x3bab6bcb, 0x1f9d45f1, 0xacfa58ab, 0x4be30393,

View File

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

View File

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

View File

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

View File

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

View File

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