mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-11 11:01:07 +00:00
Ensure P256 derived field length
This commit is contained in:
1 parent
f244572508
commit
bd93961c59
3 files changed
+5
-21
No files matched your search
@@ -511,17 +511,12 @@ class NearbyConnection {
|
||||
guard peerKey.hasEcP256PublicKey else { throw NearbyError.requiredFieldMissing("peerKey.ecP256PublicKey") }
|
||||
|
||||
let domain = Domain.instance(curve: .EC256r1)
|
||||
var clientX = peerKey.ecP256PublicKey.x
|
||||
var clientY = peerKey.ecP256PublicKey.y
|
||||
if clientX.count > 32 {
|
||||
clientX = clientX.suffix(32)
|
||||
}
|
||||
if clientY.count > 32 {
|
||||
clientY = clientY.suffix(32)
|
||||
}
|
||||
let clientX = peerKey.ecP256PublicKey.x.ensureLength(length: 32)
|
||||
let clientY = peerKey.ecP256PublicKey.y.ensureLength(length: 32)
|
||||
let key = try ECPublicKey(domain: domain, w: Point(BInt(magnitude: [UInt8](clientX)), BInt(magnitude: [UInt8](clientY))))
|
||||
|
||||
let dhs = try (privateKey?.domain.multiplyPoint(key.w, privateKey!.s).x.asMagnitudeBytes())!
|
||||
let dhsRaw = try (privateKey?.domain.multiplyPoint(key.w, privateKey!.s).x.asMagnitudeBytes())!
|
||||
let dhs = Data(dhsRaw).ensureLength(length: 32)
|
||||
var sha = SHA256()
|
||||
sha.update(data: dhs)
|
||||
let derivedSecretKey = Data(sha.finalize())
|
||||
|
||||
@@ -628,7 +628,7 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
|
||||
|
||||
var keyData = Data()
|
||||
keyData.append(contentsOf: [0, 0, 2])
|
||||
keyData.append(Data(pubKey.w.x.asSignedBytes()).suffixOfAtMost(numBytes: 32))
|
||||
keyData.append(Data(pubKey.w.x.asSignedBytes()).ensureLength(length: 32))
|
||||
|
||||
let ikm = SymmetricKey(data: keyData)
|
||||
|
||||
|
||||
@@ -31,17 +31,6 @@ extension Data {
|
||||
return data
|
||||
}
|
||||
|
||||
|
||||
func suffixOfAtMost(numBytes: Int) -> Data {
|
||||
|
||||
if count <= numBytes {
|
||||
return self
|
||||
}
|
||||
|
||||
return subdata(in: count-numBytes..<count)
|
||||
}
|
||||
|
||||
|
||||
static func dataFromUrlSafeBase64(_ str: String) -> Data? {
|
||||
var regularB64 = String(str.map {
|
||||
if $0 == "_" {
|
||||
|
||||
Reference in new issue
Block a user