local expect = require "cc.expect".expect local chacha20 = require "ccryptolib.chacha20" local poly1305 = require "ccryptolib.poly1305" local bxor = bit32.bxor local bor = bit32.bor local function encrypt(key, nonce, message, aad, rounds) expect(1, key, "string") assert(#key == 32, "key length must be 32") expect(2, nonce, "string") assert(#nonce == 12, "nonce length must be 12") expect(3, message, "string") expect(4, aad, "string") expect(5, rounds, "number", "nil") rounds = rounds or 20 -- Generate auth key and encrypt. local msgLong = ("\0"):rep(64) .. message local ctxLong = chacha20.crypt(key, nonce, msgLong, rounds, 0) local authKey = ctxLong:sub(1, 32) local ciphertext = ctxLong:sub(65) -- Authenticate. local pad1 = ("\0"):rep(-#aad % 16) local pad2 = ("\0"):rep(-#ciphertext % 16) local aadLen = ("