struct Box [src]
Alias for std.crypto.salsa20.Box
NaCl-compatible box API.
A secretbox contains both an encrypted message and an authentication tag to verify that it hasn't been tampered with.
This construction uses public-key cryptography. A shared secret doesn't have to be known in advance by both parties.
Instead, a message is encrypted using a sender's secret key and a recipient's public key,
and is decrypted using the recipient's secret key and the sender's public key.
Nonces are 192-bit large and can safely be chosen with a random number generator.
Members
- createSharedSecret (Function)
- KeyPair (struct)
- nonce_length (Constant)
- open (Function)
- public_length (Constant)
- seal (Function)
- secret_length (Constant)
- seed_length (Constant)
- shared_length (Constant)
- tag_length (Constant)
Source
pub const Box = struct {
/// Public key length in bytes.
pub const public_length = X25519.public_length;
/// Secret key length in bytes.
pub const secret_length = X25519.secret_length;
/// Shared key length in bytes.
pub const shared_length = XSalsa20Poly1305.key_length;
/// Seed (for key pair creation) length in bytes.
pub const seed_length = X25519.seed_length;
/// Nonce length in bytes.
pub const nonce_length = XSalsa20Poly1305.nonce_length;
/// Authentication tag length in bytes.
pub const tag_length = XSalsa20Poly1305.tag_length;
/// A key pair.
pub const KeyPair = X25519.KeyPair;
/// Compute a secret suitable for `secretbox` given a recipient's public key and a sender's secret key.
pub fn createSharedSecret(public_key: [public_length]u8, secret_key: [secret_length]u8) (IdentityElementError || WeakPublicKeyError)![shared_length]u8 {
const p = try X25519.scalarmult(secret_key, public_key);
const zero = [_]u8{0} ** 16;
return SalsaImpl(20).hsalsa(zero, p);
}
/// Encrypt and authenticate a message using a recipient's public key `public_key` and a sender's `secret_key`.
pub fn seal(c: []u8, m: []const u8, npub: [nonce_length]u8, public_key: [public_length]u8, secret_key: [secret_length]u8) (IdentityElementError || WeakPublicKeyError)!void {
const shared_key = try createSharedSecret(public_key, secret_key);
return SecretBox.seal(c, m, npub, shared_key);
}
/// Verify and decrypt a message using a recipient's secret key `public_key` and a sender's `public_key`.
pub fn open(m: []u8, c: []const u8, npub: [nonce_length]u8, public_key: [public_length]u8, secret_key: [secret_length]u8) (IdentityElementError || WeakPublicKeyError || AuthenticationError)!void {
const shared_key = try createSharedSecret(public_key, secret_key);
return SecretBox.open(m, c, npub, shared_key);
}
}