You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In both SECP256k1.swift and Data+Extension.swift there is a section of code to initialize a buffer with random data.
This is the excerpt form Data+Extension, though the code in SECP256k1 is virtually identical
staticfunc randomBytes(length:Int)->Data?{
for _ in 0...1024{vardata=Data(repeating:0, count: length)letresult= data.withUnsafeMutableBytes{(body:UnsafeMutableRawBufferPointer)->Int32?in
if let bodyAddress = body.baseAddress, body.count >0{letpointer= bodyAddress.assumingMemoryBound(to:UInt8.self)returnSecRandomCopyBytes(kSecRandomDefault,32, pointer)}else{returnnil}}
if let notNilResult = result, notNilResult == errSecSuccess {return data
}}returnnil}
The problem is at the line of calling SecRandomCopyBytes, it is being called with a fixed count of 32, instead of the input length, as a result anything less that 32 bytes there will be an overrun beyond the end of the buffer, conversely if length > 32 only the first 32bytes will get initialized [while safe, probably not the desired outcome]
An overrun actually appears to happen with this code inside EthereumKeystoreV3 (perhaps other places as well)
fileprivatefunc encryptDataToStorage(_ password:String, keyData:Data?, dkLen:Int=32, N:Int=4096, R:Int=6, P:Int=1, aesMode:String="aes-128-cbc")throws{
if (keyData ==nil){throwAbstractKeystoreError.encryptionError("Encryption without key data")}letsaltLen=32;
guard let saltData =Data.randomBytes(length: saltLen)else{throwAbstractKeystoreError.noEntropyError
}
guard let derivedKey =scrypt(password: password, salt: saltData, length: dkLen, N: N, R: R, P: P)else{throwAbstractKeystoreError.keyDerivationError
}letlast16bytes=Data(derivedKey[(derivedKey.count -16)...(derivedKey.count -1)])letencryptionKey=Data(derivedKey[0...15])
guard let IV =Data.randomBytes(length:16)else{throwAbstractKeystoreError.noEntropyError
}...
in the above section, the 1st call to Data.randomBytes() is safe,as the buffer is 32 bytes, but on the 2nd call the buffer is only 16 (or at least the applicable portion), resuting in the 16bytes beyond the buffer being written to.
The text was updated successfully, but these errors were encountered:
In both SECP256k1.swift and Data+Extension.swift there is a section of code to initialize a buffer with random data.
This is the excerpt form Data+Extension, though the code in SECP256k1 is virtually identical
The problem is at the line of calling SecRandomCopyBytes, it is being called with a fixed count of 32, instead of the input length, as a result anything less that 32 bytes there will be an overrun beyond the end of the buffer, conversely if length > 32 only the first 32bytes will get initialized [while safe, probably not the desired outcome]
An overrun actually appears to happen with this code inside EthereumKeystoreV3 (perhaps other places as well)
in the above section, the 1st call to
Data.randomBytes()
is safe,as the buffer is 32 bytes, but on the 2nd call the buffer is only 16 (or at least the applicable portion), resuting in the 16bytes beyond the buffer being written to.The text was updated successfully, but these errors were encountered: