structstd.crypto.salsa20.Box[src]

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.

Values

Constantpublic_length[src]

Length (in bytes) of a public key.

Source Code

Source code
pub const public_length = 32

Constantsecret_length[src]

Length (in bytes) of a secret key.

Source Code

Source code
pub const secret_length = 32

Constantkey_length[src]

Key length in bytes.

Source Code

Source code
pub const key_length = XSalsa20.key_length

Constantseed_length[src]

Seed (for key pair creation) length in bytes.

Source Code

Source code
pub const seed_length = 32

Constantnonce_length[src]

Nonce length in bytes.

Source Code

Source code
pub const nonce_length = XSalsa20.nonce_length

Constantmac_length[src]

Source Code

Source code
pub const mac_length = 16

Functions

FunctioncreateSharedSecret[src]

pub fn createSharedSecret(public_key: [public_length]u8, secret_key: [secret_length]u8) (IdentityElementError || WeakPublicKeyError)![shared_length]u8

Compute a secret suitable for secretbox given a recipient's public key and a sender's secret key.

Parameters

public_key: [public_length]u8
secret_key: [secret_length]u8

Source Code

Source code
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);
}

Functionseal[src]

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

Encrypt and authenticate a message using a recipient's public key public_key and a sender's secret_key.

Parameters

c: []u8
m: []const u8
npub: [nonce_length]u8
public_key: [public_length]u8
secret_key: [secret_length]u8

Source Code

Source code
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);
}

Functionopen[src]

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

Verify and decrypt a message using a recipient's secret key public_key and a sender's public_key.

Parameters

m: []u8
c: []const u8
npub: [nonce_length]u8
public_key: [public_length]u8
secret_key: [secret_length]u8

Source Code

Source code
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);
}

Source Code

Source code
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);
    }
}