structstd.crypto.salsa20.SecretBox[src]

NaCl-compatible secretbox API.

A secretbox contains both an encrypted message and an authentication tag to verify that it hasn't been tampered with. A secret key shared by all the recipients must be already known in order to use this API.

Nonces are 192-bit large and can safely be chosen with a random number generator.

Values

Constantkey_length[src]

Key length in bytes.

Source Code

Source code
pub const key_length = XSalsa20.key_length

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

Functionseal[src]

pub fn seal(c: []u8, m: []const u8, npub: [nonce_length]u8, k: [key_length]u8) void

Encrypt and authenticate m using a nonce npub and a key k. c must be exactly tag_length longer than m, as it will store both the ciphertext and the authentication tag.

Parameters

c: []u8
m: []const u8
npub: [nonce_length]u8
k: [key_length]u8

Source Code

Source code
pub fn seal(c: []u8, m: []const u8, npub: [nonce_length]u8, k: [key_length]u8) void {
    debug.assert(c.len == tag_length + m.len);
    XSalsa20Poly1305.encrypt(c[tag_length..], c[0..tag_length], m, "", npub, k);
}

Functionopen[src]

pub fn open(m: []u8, c: []const u8, npub: [nonce_length]u8, k: [key_length]u8) AuthenticationError!void

Verify and decrypt c using a nonce npub and a key k. m must be exactly tag_length smaller than c, as c includes an authentication tag in addition to the encrypted message.

Parameters

m: []u8
c: []const u8
npub: [nonce_length]u8
k: [key_length]u8

Source Code

Source code
pub fn open(m: []u8, c: []const u8, npub: [nonce_length]u8, k: [key_length]u8) AuthenticationError!void {
    if (c.len < tag_length) {
        return error.AuthenticationFailed;
    }
    debug.assert(m.len == c.len - tag_length);
    return XSalsa20Poly1305.decrypt(m, c[tag_length..], c[0..tag_length].*, "", npub, k);
}

Source Code

Source code
pub const SecretBox = struct {
    /// Key length in bytes.
    pub const key_length = XSalsa20Poly1305.key_length;
    /// Nonce length in bytes.
    pub const nonce_length = XSalsa20Poly1305.nonce_length;
    /// Authentication tag length in bytes.
    pub const tag_length = XSalsa20Poly1305.tag_length;

    /// Encrypt and authenticate `m` using a nonce `npub` and a key `k`.
    /// `c` must be exactly `tag_length` longer than `m`, as it will store both the ciphertext and the authentication tag.
    pub fn seal(c: []u8, m: []const u8, npub: [nonce_length]u8, k: [key_length]u8) void {
        debug.assert(c.len == tag_length + m.len);
        XSalsa20Poly1305.encrypt(c[tag_length..], c[0..tag_length], m, "", npub, k);
    }

    /// Verify and decrypt `c` using a nonce `npub` and a key `k`.
    /// `m` must be exactly `tag_length` smaller than `c`, as `c` includes an authentication tag in addition to the encrypted message.
    pub fn open(m: []u8, c: []const u8, npub: [nonce_length]u8, k: [key_length]u8) AuthenticationError!void {
        if (c.len < tag_length) {
            return error.AuthenticationFailed;
        }
        debug.assert(m.len == c.len - tag_length);
        return XSalsa20Poly1305.decrypt(m, c[tag_length..], c[0..tag_length].*, "", npub, k);
    }
}