Skip to content

Commit a9423b5

Browse files
authored
crypto: move up AEAD interface from experimental namespace to cipher.AEAD (vlang#26632)
1 parent 343f94c commit a9423b5

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

vlib/crypto/cipher/cipher.v

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,26 @@ pub interface BlockMode {
4747
// maintains state and does not reset at each crypt_blocks call.
4848
}
4949

50+
// AEAD provides an authenticated encryption with associated data for encryption (decryption).
51+
pub 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+
5070
// Utility routines
5171

5272
// fn dup(p []u8) []u8 {

vlib/x/crypto/chacha20poly1305/chacha20poly1305.v

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,12 @@
1010
// Arbitrary length additional authenticated data (AAD)
1111
module chacha20poly1305
1212

13+
import crypto.cipher
1314
import encoding.binary
1415
import crypto.internal.subtle
1516
import x.crypto.chacha20
1617
import x.crypto.poly1305
1718

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-
4219
// key_size is the size of key (in bytes) which the Chacha20Poly1305 AEAD accepts.
4320
pub const key_size = 32
4421

@@ -79,7 +56,7 @@ mut:
7956

8057
// new creates a new Chacha20Poly1305 AEAD instance with given 32 bytes of key
8158
// 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 {
8360
if key.len != key_size {
8461
return error('chacha20poly1305: bad key size')
8562
}

vlib/x/crypto/chacha20poly1305/psiv.v

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// See the detail on the [A Robust Variant of ChaCha20-Poly1305](https://eprint.iacr.org/2025/222).
1010
module chacha20poly1305
1111

12+
import crypto.cipher
1213
import encoding.binary
1314
import crypto.internal.subtle
1415
import x.crypto.chacha20
@@ -70,7 +71,7 @@ pub fn psiv_decrypt(ciphertext []u8, key []u8, nonce []u8, ad []u8) ![]u8 {
7071
// Chacha20Poly1305RE is a Chacha20Poly1305 opaque with nonce-misuse resistent
7172
// and key-commiting AEAD scheme with PSIV construct.
7273
@[noinit]
73-
pub struct Chacha20Poly1305RE implements AEAD {
74+
pub struct Chacha20Poly1305RE implements cipher.AEAD {
7475
mut:
7576
// flag that marked this instance should not be used again, set on .free call
7677
done bool

0 commit comments

Comments
 (0)