std.crypto.isap.Ascon
类型
std.crypto.isap.AuthenticationError
别名
std.crypto.isap.crypto
别名
别名目标 std.crypto
std.crypto.isap.debug
别名
别名目标 std.debug
std.crypto.isap.IsapA128A
容器
ISAPv2 是强化抵御侧信道和故障攻击的认证加密系统。 https://csrc.nist.gov/CSRC/media/Projects/lightweight-cryptography/documents/round-2/spec-doc-rnd2/isap-spec-round2.pdf
注意,ISAP 不适合高性能应用。
不过:
- 如果威胁模型允许攻击者物理访问设备,
- 或需要抵御微码/硬件级侧信道攻击,
- 或担忧 rowhammer 等由软件诱发的故障攻击,
则可以考虑使用 ISAP 处理高度敏感的数据。
字段
st: Ascon
std.crypto.isap.IsapA128A.absorb
函数
fn absorb(isap: *IsapA128A, m: []const u8) void
参数
isap: *IsapA128A
m: []const u8
std.crypto.isap.IsapA128A.decrypt
函数
m:消息
c:密文
tag:认证标签
ad:关联数据
npub:公开 nonce
k:私钥
断言 c.len == m.len。
若返回错误,m 的内容未定义。
pub fn decrypt(m: []u8, c: []const u8, tag: [tag_length]u8, ad: []const u8, npub: [nonce_length]u8, key: [key_length]u8) AuthenticationError!void
参数
错误
- AuthenticationFailed AuthenticationError
std.crypto.isap.IsapA128A.encrypt
函数
pub fn encrypt(c: []u8, tag: *[tag_length]u8, m: []const u8, ad: []const u8, npub: [nonce_length]u8, key: [key_length]u8) void
参数
c: []u8
tag: *[tag_length]u8
m: []const u8
ad: []const u8
npub: [nonce_length]u8
key: [key_length]u8
std.crypto.isap.IsapA128A.iv1
常量
std.crypto.isap.IsapA128A.iv2
常量
std.crypto.isap.IsapA128A.iv3
常量
std.crypto.isap.IsapA128A.key_length
常量
std.crypto.isap.IsapA128A.mac
函数
fn mac(c: []const u8, ad: []const u8, npub: [16]u8, key: [16]u8) [16]u8
参数
c: []const u8
ad: []const u8
npub: [16]u8
key: [16]u8
std.crypto.isap.IsapA128A.nonce_length
常量
std.crypto.isap.IsapA128A.tag_length
常量
usize
std.crypto.isap.IsapA128A.trickle
函数
fn trickle(k: [16]u8, iv: [8]u8, y: []const u8, comptime out_len: usize) [out_len]u8
参数
k: [16]u8
iv: [8]u8
y: []const u8
out_len: usize
std.crypto.isap.IsapA128A.xor
函数
fn xor(out: []u8, in: []const u8, npub: [16]u8, key: [16]u8) void
参数
out: []u8
in: []const u8
npub: [16]u8
key: [16]u8
std.crypto.isap.math
别名
别名目标 std.math
std.crypto.isap.mem
别名
别名目标 std.mem
std.crypto.isap.std
别名
别名目标 std
std.crypto.isap.testing
别名
别名目标 std.testing
源码
std/crypto/isap.zig
const std = @import("std");
const crypto = std.crypto;
const debug = std.debug;
const mem = std.mem;
const math = std.math;
const testing = std.testing;
const Ascon = crypto.core.Ascon(.big);
const AuthenticationError = crypto.errors.AuthenticationError;
/// ISAPv2 是强化抵御侧信道和故障攻击的认证加密系统。
/// https://csrc.nist.gov/CSRC/media/Projects/lightweight-cryptography/documents/round-2/spec-doc-rnd2/isap-spec-round2.pdf
///
/// 注意,ISAP 不适合高性能应用。
///
/// 不过:
/// - 如果威胁模型允许攻击者物理访问设备,
/// - 或需要抵御微码/硬件级侧信道攻击,
/// - 或担忧 rowhammer 等由软件诱发的故障攻击,
///
/// 则可以考虑使用 ISAP 处理高度敏感的数据。
pub const IsapA128A = struct {
pub const key_length = 16;
pub const nonce_length = 16;
pub const tag_length: usize = 16;
const iv1 = [_]u8{ 0x01, 0x80, 0x40, 0x01, 0x0c, 0x01, 0x06, 0x0c };
const iv2 = [_]u8{ 0x02, 0x80, 0x40, 0x01, 0x0c, 0x01, 0x06, 0x0c };
const iv3 = [_]u8{ 0x03, 0x80, 0x40, 0x01, 0x0c, 0x01, 0x06, 0x0c };
st: Ascon,
fn absorb(isap: *IsapA128A, m: []const u8) void {
var i: usize = 0;
while (true) : (i += 8) {
const left = m.len - i;
if (left >= 8) {
isap.st.addBytes(m[i..][0..8]);
isap.st.permute();
if (left == 8) {
isap.st.addByte(0x80, 0);
isap.st.permute();
break;
}
} else {
var padded: [8]u8 = @splat(0);
@memcpy(padded[0..left], m[i..]);
padded[left] = 0x80;
isap.st.addBytes(&padded);
isap.st.permute();
break;
}
}
}
fn trickle(k: [16]u8, iv: [8]u8, y: []const u8, comptime out_len: usize) [out_len]u8 {
var isap = IsapA128A{
.st = Ascon.initFromWords(.{
mem.readInt(u64, k[0..8], .big),
mem.readInt(u64, k[8..16], .big),
mem.readInt(u64, iv[0..8], .big),
0,
0,
}),
};
isap.st.permute();
var i: usize = 0;
while (i < y.len * 8 - 1) : (i += 1) {
const cur_byte_pos = i / 8;
const cur_bit_pos: u3 = @truncate(7 - (i % 8));
const cur_bit = ((y[cur_byte_pos] >> cur_bit_pos) & 1) << 7;
isap.st.addByte(cur_bit, 0);
isap.st.permuteR(1);
}
const cur_bit = (y[y.len - 1] & 1) << 7;
isap.st.addByte(cur_bit, 0);
isap.st.permute();
var out: [out_len]u8 = undefined;
isap.st.extractBytes(&out);
isap.st.secureZero();
return out;
}
fn mac(c: []const u8, ad: []const u8, npub: [16]u8, key: [16]u8) [16]u8 {
var isap = IsapA128A{
.st = Ascon.initFromWords(.{
mem.readInt(u64, npub[0..8], .big),
mem.readInt(u64, npub[8..16], .big),
mem.readInt(u64, iv1[0..], .big),
0,
0,
}),
};
isap.st.permute();
isap.absorb(ad);
isap.st.addByte(1, Ascon.block_bytes - 1);
isap.absorb(c);
var y: [16]u8 = undefined;
isap.st.extractBytes(&y);
const nb = trickle(key, iv2, y[0..], 16);
isap.st.setBytes(&nb);
isap.st.permute();
var tag: [16]u8 = undefined;
isap.st.extractBytes(&tag);
isap.st.secureZero();
return tag;
}
fn xor(out: []u8, in: []const u8, npub: [16]u8, key: [16]u8) void {
debug.assert(in.len == out.len);
const nb = trickle(key, iv3, npub[0..], 24);
var isap = IsapA128A{
.st = Ascon.initFromWords(.{
mem.readInt(u64, nb[0..8], .big),
mem.readInt(u64, nb[8..16], .big),
mem.readInt(u64, nb[16..24], .big),
mem.readInt(u64, npub[0..8], .big),
mem.readInt(u64, npub[8..16], .big),
}),
};
isap.st.permuteR(6);
var i: usize = 0;
while (true) : (i += 8) {
const left = in.len - i;
if (left >= 8) {
isap.st.xorBytes(out[i..][0..8], in[i..][0..8]);
if (left == 8) {
break;
}
isap.st.permuteR(6);
} else {
isap.st.xorBytes(out[i..], in[i..]);
break;
}
}
isap.st.secureZero();
}
pub fn encrypt(c: []u8, tag: *[tag_length]u8, m: []const u8, ad: []const u8, npub: [nonce_length]u8, key: [key_length]u8) void {
xor(c, m, npub, key);
tag.* = mac(c, ad, npub, key);
}
/// `m`:消息
/// `c`:密文
/// `tag`:认证标签
/// `ad`:关联数据
/// `npub`:公开 nonce
/// `k`:私钥
/// 断言 `c.len == m.len`。
///
/// 若返回错误,`m` 的内容未定义。
pub fn decrypt(m: []u8, c: []const u8, tag: [tag_length]u8, ad: []const u8, npub: [nonce_length]u8, key: [key_length]u8) AuthenticationError!void {
var computed_tag = mac(c, ad, npub, key);
const verify = crypto.timing_safe.eql([tag_length]u8, computed_tag, tag);
if (!verify) {
crypto.secureZero(u8, &computed_tag);
@memset(m, undefined);
return error.AuthenticationFailed;
}
xor(m, c, npub, key);
}
};
test "ISAP" {
const k: [16]u8 = @splat(1);
const n: [16]u8 = @splat(2);
var tag: [16]u8 = undefined;
const ad = "ad";
var msg = "test";
var c: [msg.len]u8 = undefined;
IsapA128A.encrypt(c[0..], &tag, msg[0..], ad, n, k);
try testing.expect(mem.eql(u8, &[_]u8{ 0x8f, 0x68, 0x03, 0x8d }, c[0..]));
try testing.expect(mem.eql(u8, &[_]u8{ 0x6c, 0x25, 0xe8, 0xe2, 0xe1, 0x1f, 0x38, 0xe9, 0x80, 0x75, 0xde, 0xd5, 0x2d, 0xb2, 0x31, 0x82 }, tag[0..]));
try IsapA128A.decrypt(c[0..], c[0..], tag, ad, n, k);
try testing.expect(mem.eql(u8, msg, c[0..]));
}