| ... | ... | @@ -131,6 +131,54 @@ pub fn sub(comptime T: type, a: []const T, b: []const T, result: []T, endian: En |
| 131 | 131 | return @as(bool, @bitCast(borrow)); |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | fn markSecret(ptr: anytype, comptime action: enum { classify, declassify }) void { |
| 135 | const t = @typeInfo(@TypeOf(ptr)); |
| 136 | if (t != .pointer) @compileError("Pointer expected - Found: " ++ @typeName(@TypeOf(ptr))); |
| 137 | const p = t.pointer; |
| 138 | if (p.is_allowzero) @compileError("A nullable pointer is always assumed to leak information via side channels"); |
| 139 | const child = @typeInfo(p.child); |
| 140 | |
| 141 | switch (child) { |
| 142 | .void, .null, .comptime_int, .comptime_float => return, |
| 143 | .pointer => { |
| 144 | if (child.pointer.size == .Slice) { |
| 145 | @compileError("Found pointer to pointer. If the intent was to pass a slice, maybe remove the leading & in the function call"); |
| 146 | } |
| 147 | @compileError("A pointer value is always assumed leak information via side channels"); |
| 148 | }, |
| 149 | else => { |
| 150 | const mem8: *const [@sizeOf(@TypeOf(ptr.*))]u8 = @constCast(@ptrCast(ptr)); |
| 151 | if (action == .classify) { |
| 152 | std.valgrind.memcheck.makeMemUndefined(mem8); |
| 153 | } else { |
| 154 | std.valgrind.memcheck.makeMemDefined(mem8); |
| 155 | } |
| 156 | }, |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /// Mark a value as sensitive or secret, helping to detect potential side-channel vulnerabilities. |
| 161 | /// |
| 162 | /// When Valgrind is enabled, this function allows for the detection of conditional jumps or lookups |
| 163 | /// that depend on secrets or secret-derived data. Violations are reported by Valgrind as operations |
| 164 | /// relying on uninitialized values. |
| 165 | /// |
| 166 | /// If Valgrind is disabled, it has no effect. |
| 167 | /// |
| 168 | /// Use this function to verify that cryptographic operations perform constant-time arithmetic on sensitive data, |
| 169 | /// ensuring the confidentiality of secrets and preventing information leakage through side channels. |
| 170 | pub fn classify(ptr: anytype) void { |
| 171 | markSecret(ptr, .classify); |
| 172 | } |
| 173 | |
| 174 | /// Mark a value as non-sensitive or public, indicating it's safe from side-channel attacks. |
| 175 | /// |
| 176 | /// Signals that a value has been securely processed and is no longer confidential, allowing for |
| 177 | /// relaxed handling without fear of information leakage through conditional jumps or lookups. |
| 178 | pub fn declassify(ptr: anytype) void { |
| 179 | markSecret(ptr, .declassify); |
| 180 | } |
| 181 | |
| 134 | 182 | test eql { |
| 135 | 183 | const random = std.crypto.random; |
| 136 | 184 | const expect = std.testing.expect; |
| ... | ... | @@ -195,3 +243,39 @@ test "add and sub" { |
| 195 | 243 | try expectEqual(borrow, false); |
| 196 | 244 | } |
| 197 | 245 | } |
| 246 | |
| 247 | test classify { |
| 248 | const random = std.crypto.random; |
| 249 | const expect = std.testing.expect; |
| 250 | |
| 251 | var secret: [32]u8 = undefined; |
| 252 | random.bytes(&secret); |
| 253 | |
| 254 | // Input of the hash function is marked as secret |
| 255 | classify(&secret); |
| 256 | |
| 257 | var out: [32]u8 = undefined; |
| 258 | std.crypto.hash.sha3.TurboShake128(null).hash(&secret, &out, .{}); |
| 259 | |
| 260 | // Output of the hash function is derived from secret data, so |
| 261 | // it will automatically be considered secret as well. But it can be |
| 262 | // declassified; the input itself will still be considered secret. |
| 263 | declassify(&out); |
| 264 | |
| 265 | // Comparing public data in non-constant time is acceptable. |
| 266 | try expect(!std.mem.eql(u8, &out, &[_]u8{0} ** out.len)); |
| 267 | |
| 268 | // Comparing secret data must be done in constant time. The result |
| 269 | // is going to be considered as secret as well. |
| 270 | var res = std.crypto.utils.timingSafeEql([32]u8, out, secret); |
| 271 | |
| 272 | // If we want to make a conditional jump based on a secret, |
| 273 | // it has to be declassified. |
| 274 | declassify(&res); |
| 275 | try expect(!res); |
| 276 | |
| 277 | // Once a secret has been declassified, a comparison in |
| 278 | // non-constant time is fine. |
| 279 | declassify(&secret); |
| 280 | try expect(!std.mem.eql(u8, &out, &secret)); |
| 281 | } |