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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
import Foundation
extension P256.KeyAgreement.PrivateKey: HPKEDiffieHellmanPrivateKeyGeneration {
/// Creates a NIST P-256 elliptic curve private key for use with Diffie-Hellman key exchange.
public init() {
self.init(compactRepresentable: false)
}
}
extension P256.KeyAgreement.PublicKey: HPKEDiffieHellmanPublicKey {
/// The type of the ephemeral private key associated with this public key.
public typealias EphemeralPrivateKey = P256.KeyAgreement.PrivateKey
static func validatekem(_ kem: HPKE.KEM) throws {
switch kem {
case .P256_HKDF_SHA256: do {}
default: do {
throw HPKE.Errors.inconsistentCiphersuiteAndKey
}
}
}
/// Creates a NIST P-256 elliptic curve public key for use with Diffie-Hellman key exchange.
///
/// - Parameters:
/// - serialization: The serialized bytes of the public key.
/// - kem: The key encapsulation mechanism to use with the public key.
/// - Throws: ``CryptoKit/HPKE/Errors/inconsistentCiphersuiteAndKey`` if the key encapsulation mechanism requested is incompatible with this public key.
public init<D>(_ serialization: D, kem: HPKE.KEM) throws where D: ContiguousBytes {
try Self.validatekem(kem)
try self.init(x963Representation: serialization)
}
/// Creates a serialized representation of the public key.
///
/// - Parameters:
/// - kem: The Key Encapsulation Mechanism to use with the public key.
/// - Returns: The serialized representation of the public key.
/// - Throws: ``CryptoKit/HPKE/Errors/inconsistentCiphersuiteAndKey`` if the key encapsulation mechanism requested is incompatible with this public key.
public func hpkeRepresentation(kem: HPKE.KEM) throws -> Data {
try Self.validatekem(kem)
return self.x963Representation
}
/// The type of the ephemeral private key associated with this public key.
public typealias HPKEEphemeralPrivateKey = P256.KeyAgreement.PrivateKey
}
extension P384.KeyAgreement.PrivateKey: HPKEDiffieHellmanPrivateKeyGeneration {
/// Creates a NIST P-384 elliptic curve private key for use with Diffie-Hellman key exchange.
public init() {
self.init(compactRepresentable: false)
}
}
extension P384.KeyAgreement.PublicKey: HPKEDiffieHellmanPublicKey {
/// The type of the ephemeral private key associated with this public key.
public typealias EphemeralPrivateKey = P384.KeyAgreement.PrivateKey
static func validatekem(_ kem: HPKE.KEM) throws {
switch kem {
case .P384_HKDF_SHA384: do {}
default: do {
throw HPKE.Errors.inconsistentCiphersuiteAndKey
}
}
}
/// Creates a NIST P-384 elliptic curve public key for use with Diffie-Hellman key exchange.
///
/// - Parameters:
/// - serialization: The serialized bytes of the public key.
/// - kem: The Key Encapsulation Mechanism to use with the public key.
///
/// - Throws: ``CryptoKit/HPKE/Errors/inconsistentCiphersuiteAndKey`` if the key encapsulation mechanism requested is incompatible with this public key.
public init<D>(_ serialization: D, kem: HPKE.KEM) throws where D: ContiguousBytes {
try Self.validatekem(kem)
try self.init(x963Representation: serialization)
}
/// Creates a serialized representation of the public key.
///
/// - Parameters:
/// - kem: The Key Encapsulation Mechanism to use with the public key.
///
/// - Returns: The serialized representation of the public key.
///
/// - Throws: ``CryptoKit/HPKE/Errors/inconsistentCiphersuiteAndKey`` if the key encapsulation mechanism requested is incompatible with this public key.
public func hpkeRepresentation(kem: HPKE.KEM) throws -> Data {
try Self.validatekem(kem)
return self.x963Representation
}
}
extension P521.KeyAgreement.PrivateKey: HPKEDiffieHellmanPrivateKeyGeneration {
/// Creates a NIST P-521 elliptic curve private key for use with Diffie-Hellman key exchange.
public init() {
self.init(compactRepresentable: false)
}
}
extension P521.KeyAgreement.PublicKey: HPKEDiffieHellmanPublicKey {
/// The type of the ephemeral private key associated with this public key.
public typealias EphemeralPrivateKey = P521.KeyAgreement.PrivateKey
static func validatekem(_ kem: HPKE.KEM) throws {
switch kem {
case .P521_HKDF_SHA512: do {}
default: do {
throw HPKE.Errors.inconsistentCiphersuiteAndKey
}
}
}
/// Creates a NIST P-521 elliptic curve public key for use with Diffie-Hellman key exchange.
///
/// - Parameters:
/// - serialization: The serialized bytes of the public key.
/// - kem: The Key Encapsulation Mechanism to use with the public key.
///
/// - Throws: ``CryptoKit/HPKE/Errors/inconsistentCiphersuiteAndKey`` if the key encapsulation mechanism requested is incompatible with this public key.
public init<D>(_ serialization: D, kem: HPKE.KEM) throws where D: ContiguousBytes {
try Self.validatekem(kem)
try self.init(x963Representation: serialization)
}
/// Creates a serialized representation of the public key.
///
/// - Parameters:
/// - kem: The Key Encapsulation Mechanism to use with the public key.
///
/// - Returns: The serialized representation of the public key.
///
/// - Throws: ``CryptoKit/HPKE/Errors/inconsistentCiphersuiteAndKey`` if the key encapsulation mechanism requested is incompatible with this public key.
public func hpkeRepresentation(kem: HPKE.KEM) throws -> Data {
try Self.validatekem(kem)
return self.x963Representation
}
/// The type of the ephemeral private key associated with this public key.
public typealias HPKEEphemeralPrivateKey = P521.KeyAgreement.PrivateKey
}
#endif // Linux or !SwiftPM
|