2022-01-07 14:51:37 +03:00
|
|
|
// The source code refers to the go standard library, which will be combined with AES in the future.
|
|
|
|
|
|
|
|
module cipher
|
|
|
|
|
|
|
|
// A Block represents an implementation of block cipher
|
|
|
|
// using a given key. It provides the capability to encrypt
|
|
|
|
// or decrypt individual blocks. The mode implementations
|
|
|
|
// extend that capability to streams of blocks.
|
|
|
|
interface Block {
|
|
|
|
block_size int // block_size returns the cipher's block size.
|
2022-04-15 15:35:35 +03:00
|
|
|
encrypt(mut dst []u8, src []u8) // Encrypt encrypts the first block in src into dst.
|
2022-01-07 14:51:37 +03:00
|
|
|
// Dst and src must overlap entirely or not at all.
|
2022-04-15 15:35:35 +03:00
|
|
|
decrypt(mut dst []u8, src []u8) // Decrypt decrypts the first block in src into dst.
|
2022-01-07 14:51:37 +03:00
|
|
|
// Dst and src must overlap entirely or not at all.
|
|
|
|
}
|
|
|
|
|
|
|
|
// A Stream represents a stream cipher.
|
|
|
|
interface Stream {
|
|
|
|
// xor_key_stream XORs each byte in the given slice with a byte from the
|
|
|
|
// cipher's key stream. Dst and src must overlap entirely or not at all.
|
|
|
|
//
|
|
|
|
// If len(dst) < len(src), xor_key_stream should panic. It is acceptable
|
|
|
|
// to pass a dst bigger than src, and in that case, xor_key_stream will
|
|
|
|
// only update dst[:len(src)] and will not touch the rest of dst.
|
|
|
|
//
|
|
|
|
// Multiple calls to xor_key_stream behave as if the concatenation of
|
|
|
|
// the src buffers was passed in a single run. That is, Stream
|
|
|
|
// maintains state and does not reset at each xor_key_stream call.
|
2022-04-15 15:35:35 +03:00
|
|
|
xor_key_stream(mut dst []u8, src []u8)
|
2022-01-07 14:51:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// A BlockMode represents a block cipher running in a block-based mode (CBC,
|
|
|
|
// ECB etc).
|
|
|
|
interface BlockMode {
|
|
|
|
block_size int // block_size returns the mode's block size.
|
2022-04-15 15:35:35 +03:00
|
|
|
crypt_blocks(mut dst []u8, src []u8) // crypt_blocks encrypts or decrypts a number of blocks. The length of
|
2022-01-07 14:51:37 +03:00
|
|
|
// src must be a multiple of the block size. Dst and src must overlap
|
|
|
|
// entirely or not at all.
|
|
|
|
//
|
|
|
|
// If len(dst) < len(src), crypt_blocks should panic. It is acceptable
|
|
|
|
// to pass a dst bigger than src, and in that case, crypt_blocks will
|
|
|
|
// only update dst[:len(src)] and will not touch the rest of dst.
|
|
|
|
//
|
|
|
|
// Multiple calls to crypt_blocks behave as if the concatenation of
|
|
|
|
// the src buffers was passed in a single run. That is, BlockMode
|
|
|
|
// maintains state and does not reset at each crypt_blocks call.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Utility routines
|
|
|
|
|
2022-04-15 15:35:35 +03:00
|
|
|
// fn dup(p []u8) []u8 {
|
|
|
|
// q := make([]u8, p.len)
|
2022-03-09 21:26:00 +03:00
|
|
|
// copy(mut q, p)
|
2022-01-07 14:51:37 +03:00
|
|
|
// return q
|
|
|
|
// }
|