-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathDH.swift
88 lines (78 loc) · 2.84 KB
/
DH.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
// DH.swift
// GigaBitcoin/secp256k1.swift
//
// Modifications Copyright (c) 2022 GigaBitcoin LLC
// Distributed under the MIT software license
//
// See the accompanying file LICENSE for information
//
//
// NOTICE: THIS FILE HAS BEEN MODIFIED BY GigaBitcoin LLC
// UNDER COMPLIANCE WITH THE APACHE 2.0 LICENSE FROM THE
// ORIGINAL WORK OF THE COMPANY Apple Inc.
//
// THE FOLLOWING IS THE COPYRIGHT OF THE ORIGINAL DOCUMENT:
//
//
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftCrypto open source project
//
// Copyright (c) 2019-2020 Apple Inc. and the SwiftCrypto project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.md for the list of SwiftCrypto project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
/// A protocol representing a Diffie-Hellman Key Agreement Key.
protocol DiffieHellmanKeyAgreement {
/// The public key share type to perform the DH Key Agreement.
associatedtype P
/// The public key associated with this instance.
var publicKey: P { get }
/// Performs a Diffie-Hellman Key Agreement.
///
/// - Parameter publicKeyShare: The public key share of the other party.
/// - Returns: The resulting shared secret as a `SharedSecret` instance.
/// - Throws: An error if the key agreement fails.
func sharedSecretFromKeyAgreement(with publicKeyShare: P) throws -> SharedSecret
}
/// A Key Agreement Result.
///
/// A `SharedSecret` has to go through a Key Derivation Function before being able to use by a symmetric key operation.
public struct SharedSecret: ContiguousBytes {
var ss: SecureBytes
// An enum that represents the format of the shared secret
let format: secp256k1.Format
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
try ss.withUnsafeBytes(body)
}
}
extension SharedSecret: Hashable {
public func hash(into hasher: inout Hasher) {
ss.withUnsafeBytes { hasher.combine(bytes: $0) }
}
}
/// Extension providing constant-time comparison and custom string representation for `SharedSecret`.
extension SharedSecret: CustomStringConvertible, Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool {
safeCompare(lhs, rhs)
}
public static func == <D: DataProtocol>(lhs: Self, rhs: D) -> Bool {
if rhs.regions.count != 1 {
let rhsContiguous = Data(rhs)
return safeCompare(lhs, rhsContiguous)
} else {
return safeCompare(lhs, rhs.regions.first!)
}
}
/// A string representation of the `SharedSecret` object.
public var description: String {
"\(Self.self): \(ss.hexString)"
}
}