2020-02-03 07:00:36 +03:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-07-25 18:49:57 +03:00
|
|
|
// 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() {
|
|
|
|
// TEST CBC
|
|
|
|
key := '6368616e676520746869732070617373'.bytes()
|
|
|
|
mut ciphertext := '73c86d43a9d700a253a96c85b0f6b03ac9792e0e757f869cca306bd3cba1c62b'.bytes()
|
|
|
|
block := aes.new_cipher(key)
|
|
|
|
// The IV needs to be unique, but not secure. Therefore it's common to
|
|
|
|
// include it at the beginning of the ciphertext.
|
2019-10-24 14:48:20 +03:00
|
|
|
if ciphertext.len < aes.block_size {
|
2019-07-25 18:49:57 +03:00
|
|
|
panic('ciphertext too short')
|
|
|
|
}
|
2019-10-27 10:03:15 +03:00
|
|
|
iv := ciphertext[..aes.block_size]
|
|
|
|
ciphertext = ciphertext[aes.block_size..]
|
2019-07-25 18:49:57 +03:00
|
|
|
// CBC mode always works in whole blocks.
|
2020-10-15 00:39:09 +03:00
|
|
|
if ciphertext.len % aes.block_size != 0 {
|
2019-07-25 18:49:57 +03:00
|
|
|
panic('ciphertext is not a multiple of the block size')
|
|
|
|
}
|
2019-12-06 15:24:53 +03:00
|
|
|
mode := aes.new_cbc(block, iv)
|
2020-07-10 19:04:51 +03:00
|
|
|
cipher_clone := ciphertext.clone()
|
|
|
|
mode.encrypt_blocks(mut ciphertext, cipher_clone)
|
2020-10-15 00:39:09 +03:00
|
|
|
assert ciphertext.hex() ==
|
|
|
|
'c210459b514668ddc44674885e4979215265a6c44431a248421254ef357a8c2a308a8bddf5623af9df91737562041cf1'
|
2019-07-25 18:49:57 +03:00
|
|
|
}
|