| ... | @@ -0,0 +1,141 @@ |
| 1 | //! Xoodoo is a 384-bit permutation designed to achieve high security with high |
| 2 | //! performance across a broad range of platforms, including 64-bit Intel/AMD |
| 3 | //! server CPUs, 64-bit and 32-bit ARM smartphone CPUs, 32-bit ARM |
| 4 | //! microcontrollers, 8-bit AVR microcontrollers, FPGAs, ASICs without |
| 5 | //! side-channel protection, and ASICs with side-channel protection. |
| 6 | //! |
| 7 | //! Xoodoo is the core function of Xoodyak, a finalist of the NIST lightweight cryptography competition. |
| 8 | //! https://csrc.nist.gov/CSRC/media/Projects/Lightweight-Cryptography/documents/round-1/spec-doc/Xoodyak-spec.pdf |
| 9 | //! |
| 10 | //! It is not meant to be used directly, but as a building block for symmetric cryptography. |
| 11 | |
| 12 | const std = @import("../std.zig"); |
| 13 | const builtin = @import("builtin"); |
| 14 | const mem = std.mem; |
| 15 | const math = std.math; |
| 16 | const testing = std.testing; |
| 17 | |
| 18 | /// A Xoodoo state. |
| 19 | pub const State = struct { |
| 20 | /// Number of bytes in the state. |
| 21 | pub const block_bytes = 48; |
| 22 | |
| 23 | const rcs = [12]u32{ 0x058, 0x038, 0x3c0, 0x0d0, 0x120, 0x014, 0x060, 0x02c, 0x380, 0x0f0, 0x1a0, 0x012 }; |
| 24 | const Lane = @Vector(4, u32); |
| 25 | st: [3]Lane, |
| 26 | |
| 27 | /// Initialize a state from a slice of bytes. |
| 28 | pub fn init(initial_state: [block_bytes]u8) State { |
| 29 | var state = State{ .st = undefined }; |
| 30 | mem.copy(u8, state.asBytes(), &initial_state); |
| 31 | state.endianSwap(); |
| 32 | return state; |
| 33 | } |
| 34 | |
| 35 | // A representation of the state as 32-bit words. |
| 36 | fn asWords(self: *State) *[12]u32 { |
| 37 | return @ptrCast(*[12]u32, &self.st); |
| 38 | } |
| 39 | |
| 40 | /// A representation of the state as bytes. The byte order is architecture-dependent. |
| 41 | pub fn asBytes(self: *State) *[block_bytes]u8 { |
| 42 | return mem.asBytes(&self.st); |
| 43 | } |
| 44 | |
| 45 | /// Byte-swap words storing the bytes of a given range if the architecture is not little-endian. |
| 46 | pub fn endianSwapPartial(self: *State, from: usize, to: usize) void { |
| 47 | for (self.asWords()[from / 4 .. (to + 3) / 4]) |*w| { |
| 48 | w.* = mem.littleToNative(u32, w.*); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /// Byte-swap the entire state if the architecture is not little-endian. |
| 53 | pub fn endianSwap(self: *State) void { |
| 54 | for (self.asWords()) |*w| { |
| 55 | w.* = mem.littleToNative(u32, w.*); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /// XOR a byte into the state at a given offset. |
| 60 | pub fn addByte(self: *State, byte: u8, offset: usize) void { |
| 61 | self.endianSwapPartial(offset, offset); |
| 62 | self.asBytes()[offset] ^= byte; |
| 63 | self.endianSwapPartial(offset, offset); |
| 64 | } |
| 65 | |
| 66 | /// XOR bytes into the beginning of the state. |
| 67 | pub fn addBytes(self: *State, bytes: []const u8) void { |
| 68 | self.endianSwap(); |
| 69 | for (self.asBytes()[0..bytes.len]) |*byte, i| { |
| 70 | byte.* ^= bytes[i]; |
| 71 | } |
| 72 | self.endianSwap(); |
| 73 | } |
| 74 | |
| 75 | /// Extract the first bytes of the state. |
| 76 | pub fn extract(self: *State, out: []u8) void { |
| 77 | self.endianSwap(); |
| 78 | mem.copy(u8, out, self.asBytes()[0..out.len]); |
| 79 | self.endianSwap(); |
| 80 | } |
| 81 | |
| 82 | /// Set the words storing the bytes of a given range to zero. |
| 83 | pub fn clear(self: *State, from: usize, to: usize) void { |
| 84 | mem.set(u32, self.asWords()[from / 4 .. (to + 3) / 4], 0); |
| 85 | } |
| 86 | |
| 87 | /// Apply the Xoodoo permutation. |
| 88 | pub fn permute(self: *State) void { |
| 89 | const rot8x32 = comptime if (builtin.target.cpu.arch.endian() == .Big) |
| 90 | [_]i32{ 9, 10, 11, 8, 13, 14, 15, 12, 1, 2, 3, 0, 5, 6, 7, 4 } |
| 91 | else |
| 92 | [_]i32{ 11, 8, 9, 10, 15, 12, 13, 14, 3, 0, 1, 2, 7, 4, 5, 6 }; |
| 93 | |
| 94 | var a = self.st[0]; |
| 95 | var b = self.st[1]; |
| 96 | var c = self.st[2]; |
| 97 | inline for (rcs) |rc| { |
| 98 | var p = @shuffle(u32, a ^ b ^ c, undefined, [_]i32{ 3, 0, 1, 2 }); |
| 99 | var e = math.rotl(Lane, p, 5); |
| 100 | p = math.rotl(Lane, p, 14); |
| 101 | e ^= p; |
| 102 | a ^= e; |
| 103 | b ^= e; |
| 104 | c ^= e; |
| 105 | b = @shuffle(u32, b, undefined, [_]i32{ 3, 0, 1, 2 }); |
| 106 | c = math.rotl(Lane, c, 11); |
| 107 | a[0] ^= rc; |
| 108 | a ^= ~b & c; |
| 109 | b ^= ~c & a; |
| 110 | c ^= ~a & b; |
| 111 | b = math.rotl(Lane, b, 1); |
| 112 | c = @bitCast(Lane, @shuffle(u8, @bitCast(@Vector(16, u8), c), undefined, rot8x32)); |
| 113 | } |
| 114 | self.st[0] = a; |
| 115 | self.st[1] = b; |
| 116 | self.st[2] = c; |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | test "xoodoo" { |
| 121 | const bytes = [_]u8{0x01} ** State.block_bytes; |
| 122 | var st = State.init(bytes); |
| 123 | var out: [State.block_bytes]u8 = undefined; |
| 124 | st.permute(); |
| 125 | st.extract(&out); |
| 126 | const expected1 = [_]u8{ 51, 240, 163, 117, 43, 238, 62, 200, 114, 52, 79, 41, 48, 108, 150, 181, 24, 5, 252, 185, 235, 179, 28, 3, 116, 170, 36, 15, 232, 35, 116, 61, 110, 4, 109, 227, 91, 205, 0, 180, 179, 146, 112, 235, 96, 212, 206, 205 }; |
| 127 | try testing.expectEqualSlices(u8, &expected1, &out); |
| 128 | st.clear(0, 10); |
| 129 | st.extract(&out); |
| 130 | const expected2 = [_]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 108, 150, 181, 24, 5, 252, 185, 235, 179, 28, 3, 116, 170, 36, 15, 232, 35, 116, 61, 110, 4, 109, 227, 91, 205, 0, 180, 179, 146, 112, 235, 96, 212, 206, 205 }; |
| 131 | try testing.expectEqualSlices(u8, &expected2, &out); |
| 132 | st.addByte(1, 5); |
| 133 | st.addByte(2, 5); |
| 134 | st.extract(&out); |
| 135 | const expected3 = [_]u8{ 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 48, 108, 150, 181, 24, 5, 252, 185, 235, 179, 28, 3, 116, 170, 36, 15, 232, 35, 116, 61, 110, 4, 109, 227, 91, 205, 0, 180, 179, 146, 112, 235, 96, 212, 206, 205 }; |
| 136 | try testing.expectEqualSlices(u8, &expected3, &out); |
| 137 | st.addBytes(&bytes); |
| 138 | st.extract(&out); |
| 139 | const expected4 = [_]u8{ 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 49, 109, 151, 180, 25, 4, 253, 184, 234, 178, 29, 2, 117, 171, 37, 14, 233, 34, 117, 60, 111, 5, 108, 226, 90, 204, 1, 181, 178, 147, 113, 234, 97, 213, 207, 204 }; |
| 140 | try testing.expectEqualSlices(u8, &expected4, &out); |
| 141 | } |