From d266ab392bcded9ad1a245b9241560d3ca1b7136 Mon Sep 17 00:00:00 2001 From: Miguel Oliveira Date: Fri, 9 Jun 2023 14:31:42 -0300 Subject: [PATCH] Add string utilities --- ccryptolib/util.lua | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 ccryptolib/util.lua diff --git a/ccryptolib/util.lua b/ccryptolib/util.lua new file mode 100644 index 0000000..0f2df8e --- /dev/null +++ b/ccryptolib/util.lua @@ -0,0 +1,45 @@ +--- General utilities for handling byte strings. + +local expect = require "cc.expect".expect +local random = require "cryptolib.random" +local poly1305 = require "ccryptolib.poly1305" + +--- Returns the hexadecimal version of a string. +--- @param str string A string. +--- @return string hex The hexadecimal version of the string. +local function toHex(str) + expect(1, str, "string") + return ("%02x"):rep(#str):format(str:byte(1, -1)) +end + +--- Converts back a string from hexadecimal. +--- @param hex string A hexadecimal string. +--- @return string? str The original string, or nil if the input is invalid. +local function fromHex(hex) + expect(1, hex, "string") + local out = {} + local n = 0 + for c in hex:gmatch("%x%x") do + n = n + 1 + out[n] = tonumber(c, 16) + end + if 2 * n == #hex then return string.char(table.unpack(out)) end +end + +--- Compares two strings while mitigating secret leakage through timing. +--- @param a string +--- @param b string +--- @return boolean eq Whether a == b. +local function compare(a, b) + expect(1, a, "string") + expect(2, b, "string") + if #a ~= #b then return false end + local kaux = random.random(32) + return poly1305.mac(kaux, a) == poly1305.mac(kaux, b) +end + +return { + toHex = toHex, + fromHex = fromHex, + compare = compare, +}