|
10 | 10 | // Arbitrary length additional authenticated data (AAD) |
11 | 11 | module chacha20poly1305 |
12 | 12 |
|
| 13 | +import crypto.cipher |
13 | 14 | import encoding.binary |
14 | 15 | import crypto.internal.subtle |
15 | 16 | import x.crypto.chacha20 |
16 | 17 | import x.crypto.poly1305 |
17 | 18 |
|
18 | | -// This interface was a proposed draft for Authenticated Encryption with Additional Data (AEAD) |
19 | | -// interface `AEAD` likes discussion at discord channel. |
20 | | -// see https://discord.com/channels/592103645835821068/592320321995014154/1206029352412778577 |
21 | | -// But its little modified to be more v-idiomatic. |
22 | | -// Note: This interface should be more appropriately located in `crypto.cipher`, we can move |
23 | | -// it into `crypto.cipher` later. |
24 | | -// Authenticated Encryption with Additional Data (AEAD) interface |
25 | | -pub interface AEAD { |
26 | | - // nonce_size return the nonce size (in bytes) used by this AEAD algorithm that should be |
27 | | - // passed to `.encrypt` or `.decrypt`. |
28 | | - nonce_size() int |
29 | | - // overhead returns the maximum difference between the lengths of a plaintext and its ciphertext. |
30 | | - overhead() int |
31 | | - // encrypt encrypts and authenticates the provided plaintext along with a nonce, and |
32 | | - // to be authenticated additional data in `ad`. |
33 | | - // It returns ciphertext bytes where its encoded form is up to implementation and |
34 | | - // not dictated by the interfaces. |
35 | | - // Usually its contains encrypted text plus some authentication tag, and maybe some other bytes. |
36 | | - encrypt(plaintext []u8, nonce []u8, ad []u8) ![]u8 |
37 | | - // decrypt decrypts and authenticates (verifies) the provided ciphertext along with a nonce, and |
38 | | - // additional data. If verified successfully, it returns the plaintext and error otherwise. |
39 | | - decrypt(ciphertext []u8, nonce []u8, ad []u8) ![]u8 |
40 | | -} |
41 | | - |
42 | 19 | // key_size is the size of key (in bytes) which the Chacha20Poly1305 AEAD accepts. |
43 | 20 | pub const key_size = 32 |
44 | 21 |
|
|
79 | 56 |
|
80 | 57 | // new creates a new Chacha20Poly1305 AEAD instance with given 32 bytes of key |
81 | 58 | // and the nonce size in nsize. The nsize should be 8, 12 or 24 length, otherwise it would return error. |
82 | | -pub fn new(key []u8, nsize int, opt chacha20.Options) !&AEAD { |
| 59 | +pub fn new(key []u8, nsize int, opt chacha20.Options) !&cipher.AEAD { |
83 | 60 | if key.len != key_size { |
84 | 61 | return error('chacha20poly1305: bad key size') |
85 | 62 | } |
|
0 commit comments