v / vlib / crypto / cipher / cipher.v
76 lines · 69 sloc · 3.47 KB · a9423b51df5a1d4f136cffe6326c9a84beba5902
Raw
1// The source code refers to the go standard library, which will be combined with AES in the future.
2
3module cipher
4
5// A Block represents an implementation of block cipher
6// using a given key. It provides the capability to encrypt
7// or decrypt individual blocks. The mode implementations
8// extend that capability to streams of blocks.
9pub interface Block {
10 block_size int // block_size returns the cipher's block size.
11 encrypt(mut dst []u8, src []u8) // Encrypt encrypts the first block in src into dst.
12 // Dst and src must overlap entirely or not at all.
13 decrypt(mut dst []u8, src []u8) // Decrypt decrypts the first block in src into dst.
14 // Dst and src must overlap entirely or not at all.
15}
16
17// A Stream represents a stream cipher.
18pub interface Stream {
19mut:
20 // xor_key_stream XORs each byte in the given slice with a byte from the
21 // cipher's key stream. Dst and src must overlap entirely or not at all.
22 //
23 // If len(dst) < len(src), xor_key_stream should panic. It is acceptable
24 // to pass a dst bigger than src, and in that case, xor_key_stream will
25 // only update dst[:len(src)] and will not touch the rest of dst.
26 //
27 // Multiple calls to xor_key_stream behave as if the concatenation of
28 // the src buffers was passed in a single run. That is, Stream
29 // maintains state and does not reset at each xor_key_stream call.
30 xor_key_stream(mut dst []u8, src []u8)
31}
32
33// A BlockMode represents a block cipher running in a block-based mode (CBC,
34// ECB etc).
35pub interface BlockMode {
36 block_size int // block_size returns the mode's block size.
37 crypt_blocks(mut dst []u8, src []u8) // crypt_blocks encrypts or decrypts a number of blocks. The length of
38 // src must be a multiple of the block size. Dst and src must overlap
39 // entirely or not at all.
40 //
41 // If len(dst) < len(src), crypt_blocks should panic. It is acceptable
42 // to pass a dst bigger than src, and in that case, crypt_blocks will
43 // only update dst[:len(src)] and will not touch the rest of dst.
44 //
45 // Multiple calls to crypt_blocks behave as if the concatenation of
46 // the src buffers was passed in a single run. That is, BlockMode
47 // maintains state and does not reset at each crypt_blocks call.
48}
49
50// AEAD provides an authenticated encryption with associated data for encryption (decryption).
51pub interface AEAD {
52 // nonce_size returns the size of nonce (in bytes) used by this AEAD that must be
53 // passed to `.encrypt` or `.decrypt`.
54 nonce_size() int
55 // overhead returns the maximum difference between the lengths of a plaintext and its ciphertext.
56 overhead() int
57 // encrypt encrypts and authenticates the provided plaintext along with the nonce and
58 // additional data in `ad`. The nonce must be `nonce_size()` bytes long and unique
59 // for all time, for a given key. It returns encrypted (and authenticated) ciphertext bytes
60 // where its encoded form is up to implementation and not dictated by the interfaces.
61 // Commonly, its contains encrypted text plus some authentication tag, and maybe some other bytes.
62 encrypt(plaintext []u8, nonce []u8, ad []u8) ![]u8
63 // decrypt decrypts and authenticates (verifies) the provided ciphertext along with a nonce, and
64 // additional data. The nonce must be `nonce_size()` bytes long and both it and the additional data
65 // must match the value passed to `encrypt`.
66 // Its returns the verified plaintext on success, or errors on fails.
67 decrypt(ciphertext []u8, nonce []u8, ad []u8) ![]u8
68}
69
70// Utility routines
71
72// fn dup(p []u8) []u8 {
73// q := make([]u8, p.len)
74// copy(mut q, p)
75// return q
76// }
77