stdlib: add the swift_clearSensitive runtime function

This commit is contained in:
Erik Eckstein
2024-04-05 16:48:18 +02:00
parent 3097cceba0
commit ce33d47a4c
8 changed files with 43 additions and 0 deletions

View File

@@ -338,3 +338,16 @@ func arc4random_buf(buf: UnsafeMutableRawPointer, nbytes: Int)
public func swift_stdlib_random(_ buf: UnsafeMutableRawPointer, _ nbytes: Int) {
arc4random_buf(buf: buf, nbytes: nbytes)
}
@_cdecl("swift_clearSensitive")
@inline(never)
public func swift_clearSensitive(buf: UnsafeMutableRawPointer, nbytes: Int) {
// TODO: use memset_s if available
// Though, it shouldn't make too much difference because the `@inline(never)` should prevent
// the optimizer from removing the loop below.
let bytePtr = buf.assumingMemoryBound(to: UInt8.self)
for i in 0..<nbytes {
bytePtr[i] = 0
}
}