2019-07-25 17:49:57 +02:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// 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-26 16:48:49 +02:00
|
|
|
|
2019-07-25 17:49:57 +02:00
|
|
|
module aes
|
|
|
|
|
|
|
|
import (
|
|
|
|
crypto.internal.subtle
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// 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
|
|
|
)
|
|
|
|
|
|
|
|
// A cipher is an instance of AES encryption using a particular key.
|
|
|
|
struct AesCipher {
|
|
|
|
enc []u32
|
|
|
|
dec []u32
|
|
|
|
}
|
|
|
|
|
|
|
|
// new_cipher creates and returns a new cipher.Block.
|
|
|
|
// The key argument should be the AES key,
|
|
|
|
// either 16, 24, or 32 bytes to select
|
|
|
|
// AES-128, AES-192, or AES-256.
|
|
|
|
pub fn new_cipher(key []byte) AesCipher {
|
|
|
|
k := key.len
|
2019-10-27 10:36:38 +01:00
|
|
|
match k {
|
|
|
|
16, 24, 32 {
|
2019-07-25 17:49:57 +02:00
|
|
|
// break
|
2019-10-27 10:36:38 +01:00
|
|
|
} else {
|
2019-07-25 17:49:57 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-10-24 13:48:20 +02:00
|
|
|
pub fn (c &AesCipher) block_size() int { return block_size }
|
2019-07-25 17:49:57 +02:00
|
|
|
|
|
|
|
pub fn (c &AesCipher) encrypt(dst, src []byte) {
|
2019-10-24 13:48:20 +02:00
|
|
|
if src.len < block_size {
|
2019-07-25 17:49:57 +02:00
|
|
|
panic('crypto.aes: input not full block')
|
|
|
|
}
|
2019-10-24 13:48:20 +02:00
|
|
|
if dst.len < 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]) {
|
2019-10-27 08:03:15 +01:00
|
|
|
if subtle.inexact_overlap(dst[..block_size], src[..block_size]) {
|
2019-07-25 17:49:57 +02:00
|
|
|
panic('crypto.aes: invalid buffer overlap')
|
|
|
|
}
|
|
|
|
// for now use generic version
|
|
|
|
encrypt_block_generic(c.enc, dst, src)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (c &AesCipher) decrypt(dst, src []byte) {
|
2019-10-24 13:48:20 +02:00
|
|
|
if src.len < block_size {
|
2019-07-25 17:49:57 +02:00
|
|
|
panic('crypto.aes: input not full block')
|
|
|
|
}
|
2019-10-24 13:48:20 +02:00
|
|
|
if dst.len < block_size {
|
2019-07-25 17:49:57 +02:00
|
|
|
panic('crypto.aes: output not full block')
|
|
|
|
}
|
2019-10-27 08:03:15 +01:00
|
|
|
if subtle.inexact_overlap(dst[..block_size], src[..block_size]) {
|
2019-07-25 17:49:57 +02:00
|
|
|
panic('crypto.aes: invalid buffer overlap')
|
|
|
|
}
|
|
|
|
// for now use generic version
|
|
|
|
decrypt_block_generic(c.dec, dst, src)
|
|
|
|
}
|