2022-01-04 10:21:08 +01:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2019-07-25 17:49:57 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-08-02 06:37:19 +02:00
|
|
|
// Based off: https://github.com/golang/go/blob/master/src/crypto/aes
|
|
|
|
// Last commit: https://github.com/golang/go/commit/691a2d457ab1bf03bd46d4b69e0f93b8993c0055
|
2019-07-25 17:49:57 +02:00
|
|
|
module aes
|
|
|
|
|
2022-01-08 16:08:46 +01:00
|
|
|
import crypto.cipher
|
2020-04-26 13:49:31 +02:00
|
|
|
import crypto.internal.subtle
|
2019-07-25 17:49:57 +02:00
|
|
|
|
2019-12-06 13:24:53 +01:00
|
|
|
pub const (
|
2019-07-25 17:49:57 +02:00
|
|
|
// The AES block size in bytes.
|
2019-10-24 13:48:20 +02:00
|
|
|
block_size = 16
|
2019-07-25 17:49:57 +02:00
|
|
|
)
|
|
|
|
|
2021-01-23 13:33:49 +01:00
|
|
|
// AesCipher represents an AES encryption using a particular key.
|
2022-01-07 12:28:50 +01:00
|
|
|
// It follows the API of golang's `cipher.Block` and is designed to
|
|
|
|
// handle only one block of data at a time. In most cases, you
|
|
|
|
// probably want to encrypt and decrypt using [[AesCbc](#AesCbc)]
|
2019-07-25 17:49:57 +02:00
|
|
|
struct AesCipher {
|
2022-01-08 16:08:46 +01:00
|
|
|
block_size int = aes.block_size
|
2020-06-30 14:19:22 +02:00
|
|
|
mut:
|
2019-07-25 17:49:57 +02:00
|
|
|
enc []u32
|
|
|
|
dec []u32
|
|
|
|
}
|
|
|
|
|
2022-01-07 12:28:50 +01:00
|
|
|
// new_cipher creates and returns a new [[AesCipher](#AesCipher)].
|
2019-07-25 17:49:57 +02:00
|
|
|
// The key argument should be the AES key,
|
|
|
|
// either 16, 24, or 32 bytes to select
|
|
|
|
// AES-128, AES-192, or AES-256.
|
2022-04-15 14:35:35 +02:00
|
|
|
pub fn new_cipher(key []u8) cipher.Block {
|
2019-07-25 17:49:57 +02:00
|
|
|
k := key.len
|
2019-10-27 10:36:38 +01:00
|
|
|
match k {
|
2020-11-11 17:16:45 +01:00
|
|
|
16, 24, 32 {
|
|
|
|
// break
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
panic('crypto.aes: invalid key size ' + k.str())
|
|
|
|
// return error('crypto.aes: invalid key size ' + k.str())
|
|
|
|
}
|
2019-10-27 10:36:38 +01:00
|
|
|
}
|
2019-07-25 17:49:57 +02:00
|
|
|
// for now use generic version
|
|
|
|
return new_cipher_generic(key)
|
|
|
|
}
|
|
|
|
|
2021-01-23 13:33:49 +01:00
|
|
|
// block_size returns the block size of the checksum in bytes.
|
2020-11-11 17:16:45 +01:00
|
|
|
pub fn (c &AesCipher) block_size() int {
|
2021-05-08 12:32:29 +02:00
|
|
|
return aes.block_size
|
2020-11-11 17:16:45 +01:00
|
|
|
}
|
2019-07-25 17:49:57 +02:00
|
|
|
|
2022-01-07 12:28:50 +01:00
|
|
|
// encrypt encrypts the first block of data in `src` to `dst`.
|
|
|
|
// NOTE: `dst` and `src` are both mutable for performance reasons.
|
|
|
|
// NOTE: `dst` and `src` must both be pre-allocated to the correct length.
|
|
|
|
// NOTE: `dst` and `src` may be the same (overlapping entirely).
|
2022-04-15 14:35:35 +02:00
|
|
|
pub fn (c &AesCipher) encrypt(mut dst []u8, src []u8) {
|
2021-05-08 12:32:29 +02:00
|
|
|
if src.len < aes.block_size {
|
2019-07-25 17:49:57 +02:00
|
|
|
panic('crypto.aes: input not full block')
|
|
|
|
}
|
2021-05-08 12:32:29 +02:00
|
|
|
if dst.len < aes.block_size {
|
2019-07-25 17:49:57 +02:00
|
|
|
panic('crypto.aes: output not full block')
|
|
|
|
}
|
2019-10-24 13:48:20 +02:00
|
|
|
// if subtle.inexact_overlap(dst[:block_size], src[:block_size]) {
|
2022-01-08 16:08:46 +01:00
|
|
|
if subtle.inexact_overlap(dst[..aes.block_size], src[..aes.block_size]) {
|
2019-07-25 17:49:57 +02:00
|
|
|
panic('crypto.aes: invalid buffer overlap')
|
|
|
|
}
|
|
|
|
// for now use generic version
|
2020-06-30 14:19:22 +02:00
|
|
|
encrypt_block_generic(c.enc, mut dst, src)
|
2019-07-25 17:49:57 +02:00
|
|
|
}
|
|
|
|
|
2022-01-07 12:28:50 +01:00
|
|
|
// decrypt decrypts the first block of data in `src` to `dst`.
|
|
|
|
// NOTE: `dst` and `src` are both mutable for performance reasons.
|
|
|
|
// NOTE: `dst` and `src` must both be pre-allocated to the correct length.
|
|
|
|
// NOTE: `dst` and `src` may be the same (overlapping entirely).
|
2022-04-15 14:35:35 +02:00
|
|
|
pub fn (c &AesCipher) decrypt(mut dst []u8, src []u8) {
|
2021-05-08 12:32:29 +02:00
|
|
|
if src.len < aes.block_size {
|
2019-07-25 17:49:57 +02:00
|
|
|
panic('crypto.aes: input not full block')
|
|
|
|
}
|
2021-05-08 12:32:29 +02:00
|
|
|
if dst.len < aes.block_size {
|
2019-07-25 17:49:57 +02:00
|
|
|
panic('crypto.aes: output not full block')
|
|
|
|
}
|
2022-01-08 16:08:46 +01:00
|
|
|
if subtle.inexact_overlap(dst[..aes.block_size], src[..aes.block_size]) {
|
2019-07-25 17:49:57 +02:00
|
|
|
panic('crypto.aes: invalid buffer overlap')
|
|
|
|
}
|
|
|
|
// for now use generic version
|
2020-06-30 14:19:22 +02:00
|
|
|
decrypt_block_generic(c.dec, mut dst, src)
|
2019-07-25 17:49:57 +02:00
|
|
|
}
|