Signal-iOS/SignalServiceKit/Curve25519/PublicKey.swift
Jordan Rose cbaae3fb08 Tighten up checks for PublicKey.init(keyBytes:)
For historical reasons libsignal's PublicKey.init(_:) allows trailing
data, because the type byte specifies how much of the data is relevant
anyway. init(keyBytes:), however, should not be so lax, since it's
assuming the key data is a Curve25519 public point.
2024-05-20 10:18:42 -07:00

22 lines
605 B
Swift

//
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import Foundation
import LibSignalClient
extension PublicKey {
public convenience init(keyData: Data) throws {
if keyData.count != Constants.keyLengthDJB {
throw SignalError.invalidKey("invalid number of public key bytes (expected \(Constants.keyLengthDJB), was \(keyData.count))")
}
try self.init([Constants.keyTypeDJB] + keyData)
}
public enum Constants {
public static let keyTypeDJB: UInt8 = 0x05
public static let keyLengthDJB: Int = 32
}
}