mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
v test-fmt: reformat some skipped files, comment on the remaining ones
This commit is contained in:
@@ -1,16 +1,11 @@
|
||||
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// Cipher block chaining (CBC) mode.
|
||||
|
||||
// CBC provides confidentiality by xoring (chaining) each plaintext block
|
||||
// with the previous ciphertext block before applying the block cipher.
|
||||
|
||||
// See NIST SP 800-38A, pp 10-11
|
||||
|
||||
// NOTE this will be moved to crypto.cipher interface (joe-c)
|
||||
|
||||
module aes
|
||||
|
||||
import crypto.cipher
|
||||
@@ -27,10 +22,10 @@ mut:
|
||||
// internal
|
||||
fn new_aes_cbc(b AesCipher, iv []byte) AesCbc {
|
||||
return AesCbc{
|
||||
b: b,
|
||||
block_size: b.block_size(),
|
||||
iv: iv.clone(),
|
||||
tmp: []byte{len:(b.block_size()),}
|
||||
b: b
|
||||
block_size: b.block_size()
|
||||
iv: iv.clone()
|
||||
tmp: []byte{len: (b.block_size())}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,12 +39,14 @@ pub fn new_cbc(b AesCipher, iv []byte) AesCbc {
|
||||
return new_aes_cbc(b, iv)
|
||||
}
|
||||
|
||||
pub fn (x &AesCbc) block_size() int { return x.block_size }
|
||||
pub fn (x &AesCbc) block_size() int {
|
||||
return x.block_size
|
||||
}
|
||||
|
||||
pub fn (x &AesCbc) encrypt_blocks(mut dst_ []byte, src_ []byte) {
|
||||
mut dst := *dst_
|
||||
mut src := src_
|
||||
if src.len%x.block_size != 0 {
|
||||
if src.len % x.block_size != 0 {
|
||||
panic('crypto.cipher: input not full blocks')
|
||||
}
|
||||
if dst.len < src.len {
|
||||
@@ -58,14 +55,11 @@ pub fn (x &AesCbc) encrypt_blocks(mut dst_ []byte, src_ []byte) {
|
||||
if subtle.inexact_overlap(dst[..src.len], src_) {
|
||||
panic('crypto.cipher: invalid buffer overlap')
|
||||
}
|
||||
|
||||
mut iv := x.iv
|
||||
|
||||
for src.len > 0 {
|
||||
// Write the xor to dst, then encrypt in place.
|
||||
cipher.xor_bytes(mut dst[..x.block_size], src[..x.block_size], iv)
|
||||
x.b.encrypt(mut dst[..x.block_size], mut dst[..x.block_size])
|
||||
|
||||
// Move to the next block with this block as the next iv.
|
||||
iv = dst[..x.block_size]
|
||||
if x.block_size >= src.len {
|
||||
@@ -75,13 +69,12 @@ pub fn (x &AesCbc) encrypt_blocks(mut dst_ []byte, src_ []byte) {
|
||||
}
|
||||
dst = dst[x.block_size..]
|
||||
}
|
||||
|
||||
// Save the iv for the next crypt_blocks call.
|
||||
copy(x.iv, iv)
|
||||
}
|
||||
|
||||
pub fn (mut x AesCbc) decrypt_blocks(mut dst []byte, src []byte) {
|
||||
if src.len%x.block_size != 0 {
|
||||
if src.len % x.block_size != 0 {
|
||||
panic('crypto.cipher: input not full blocks')
|
||||
}
|
||||
if dst.len < src.len {
|
||||
@@ -93,33 +86,27 @@ pub fn (mut x AesCbc) decrypt_blocks(mut dst []byte, src []byte) {
|
||||
if src.len == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
// For each block, we need to xor the decrypted data with the previous block's ciphertext (the iv).
|
||||
// To avoid making a copy each time, we loop over the blocks BACKWARDS.
|
||||
mut end := src.len
|
||||
mut start := end - x.block_size
|
||||
mut prev := start - x.block_size
|
||||
|
||||
// Copy the last block of ciphertext in preparation as the new iv.
|
||||
copy(x.tmp, src.slice(start, end))
|
||||
|
||||
// Loop over all but the first block.
|
||||
for start > 0 {
|
||||
mut src_chunk := src.slice(start, end)
|
||||
x.b.decrypt(mut (*dst).slice(start, end), mut src_chunk)
|
||||
cipher.xor_bytes(mut (*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
|
||||
prev -= x.block_size
|
||||
}
|
||||
|
||||
// The first block is special because it uses the saved iv.
|
||||
mut src_chunk := src.slice(start, end)
|
||||
x.b.decrypt(mut (*dst).slice(start, end), mut src_chunk)
|
||||
cipher.xor_bytes(mut (*dst).slice(start, end), (*dst).slice(start, end), x.iv)
|
||||
|
||||
|
||||
// Set the new iv to the first block we copied earlier.
|
||||
x.iv = x.tmp
|
||||
x.tmp = x.iv
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
import crypto.aes
|
||||
|
||||
fn test_crypto_aes() {
|
||||
@@ -17,12 +16,12 @@ fn test_crypto_aes() {
|
||||
iv := ciphertext[..aes.block_size]
|
||||
ciphertext = ciphertext[aes.block_size..]
|
||||
// CBC mode always works in whole blocks.
|
||||
if ciphertext.len%aes.block_size != 0 {
|
||||
if ciphertext.len % aes.block_size != 0 {
|
||||
panic('ciphertext is not a multiple of the block size')
|
||||
}
|
||||
mode := aes.new_cbc(block, iv)
|
||||
cipher_clone := ciphertext.clone()
|
||||
mode.encrypt_blocks(mut ciphertext, cipher_clone)
|
||||
|
||||
assert ciphertext.hex() == 'c210459b514668ddc44674885e4979215265a6c44431a248421254ef357a8c2a308a8bddf5623af9df91737562041cf1'
|
||||
assert ciphertext.hex() ==
|
||||
'c210459b514668ddc44674885e4979215265a6c44431a248421254ef357a8c2a308a8bddf5623af9df91737562041cf1'
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
module aes
|
||||
|
||||
// new_cipher_generic creates and returns a new cipher.Block
|
||||
@@ -9,8 +8,8 @@ module aes
|
||||
fn new_cipher_generic(key []byte) AesCipher {
|
||||
n := key.len + 28
|
||||
mut c := AesCipher{
|
||||
enc: []u32{len:(n)}
|
||||
dec: []u32{len:(n)}
|
||||
enc: []u32{len: (n)}
|
||||
dec: []u32{len: (n)}
|
||||
}
|
||||
expand_key_generic(key, mut c.enc, mut c.dec)
|
||||
return c
|
||||
|
||||
Reference in New Issue
Block a user