KeyAgreementAlgorithm
internal enum KeyAgreementAlgorithm
The Key Agreement is a cryptographic method that allows users to compute shared secret key, after their public keys have been exchanged, allowing the use of a cryptographic algorithm.
-
Diffie-Hellman Key Agreement
Declaration
Swift
case DH
-
Elliptic-Curve Diffie-Hellman Key Agreement
Declaration
Swift
case ECDH
-
Generate a new key pair for the specified
KeyAgreementAlgorithm
andStandardizedDomainParameters
.Important
The returned reference to the key pair has to be freed by the caller. The pointer will reference a
EVP_PKEY
structure (so useEVP_PKEY_free
then).Declaration
Swift
internal static func generateKeyPair(for algorithm: KeyAgreementAlgorithm, using params: StandardizedDomainParameters) throws -> OpaquePointer
Parameters
algorithm
The used
KeyAgreementAlgorithm
(DH or ECDH).params
The used
StandardizedDomainParameters
. It may be a standardized elliptic curve for ECDH or an algebric set for DH. Parameters includes the prime p, the generator g and the size of the prime-order subgroup generated by g.Return Value
A reference to the generated key pair.
-
Generate a new key pair using the specified
StandardizedDomainParameters
.Important
The returned reference to the key pair has to be freed by the caller. The pointer will reference a
EVP_PKEY
structure (so useEVP_PKEY_free
then).Declaration
Swift
internal func generateKeyPair(params: StandardizedDomainParameters) throws -> OpaquePointer
Parameters
params
The used
StandardizedDomainParameters
. It may be a standardized elliptic curve for ECDH or an algebric set for DH. Parameters includes the prime p, the generator g and the size of the prime-order subgroup generated by g.Return Value
A reference to the generated key pair.
-
Generate a key pair based on the parameters of an existing public key.
Throws
An error if key pair generation fails.
Important
The returned reference to the key pair has to be freed by the caller. The pointer will reference a
EVP_PKEY
structure (so useEVP_PKEY_free
then).Declaration
Swift
internal static func generateKeyPair(withParamsFrom publicKey: OpaquePointer) throws -> OpaquePointer
Parameters
publicKey
A reference to an existing public key.
Return Value
A reference to the generated key pair.
-
Compute the shared secret from the personal private key and the external public key using the specified
KeyAgreementAlgorithm
.Tip: If you want to convert the returned reference to the shared secret into a byte representation as
[UInt8]
, you can useconvertToBytes(key:keyPair:for:)
orconvertToBytes(key:keyPair:)
functions.
Important
The returned reference to the shared secret has to be freed by the caller. If the used algorithm is DH, the pointer will reference a
BN
structure (so useBN_free
then), otherwise the pointer will reference aEC_POINT
stucture (so useEC_POINT_free
then). If you don’t know the usedKeyAgreementAlgorithm
or it is dynamic, you can also callfree(sharedSecret:)
orfree(sharedSecret:for:)
functions.Declaration
Swift
internal static func computeSharedSecret(personalKeyPair: OpaquePointer, externalPublicKey: [UInt8], using algorithm: KeyAgreementAlgorithm) throws -> OpaquePointer
Parameters
personalKeyPair
The personal key pair object containg personal public/private keys and used params.
externalPublicKey
The bytes representing the external public key.
algorithm
The used
KeyAgreementAlgorithm
(DH or ECDH)Return Value
A reference to the computed shared secret.
-
Compute the shared secret from the personal private key and the external public key.
Tip: If you want to convert the returned reference to the shared secret into a byte representation as
[UInt8]
, you can useconvertToBytes(key:keyPair:for:)
orconvertToBytes(key:keyPair:)
functions.
Important
The returned reference to the shared secret has to be freed by the caller. If the used algorithm is DH, the pointer will reference a
BN
structure (so useBN_free
then), otherwise the pointer will reference aEC_POINT
stucture (so useEC_POINT_free
then). If you don’t know the usedKeyAgreementAlgorithm
or it is dynamic, you can also callfree(sharedSecret:)
orfree(sharedSecret:for:)
functions.Declaration
Swift
internal func computeSharedSecret(personalKeyPair: OpaquePointer, externalPublicKey: [UInt8]) throws -> OpaquePointer
Parameters
personalKeyPair
The personal key pair object containg personal public/private keys and used params.
externalPublicKey
The bytes representing the external public key.
Return Value
A reference to the computed shared secret.
-
Extract and return the public key from an
OpaquePointer
key pair.Throws
An error if the extraction process fails or if the key type is not supported.
Declaration
Swift
internal static func extractPublicKey(from keyPair: OpaquePointer) throws -> [UInt8]
Parameters
keyPair
An
OpaquePointer
representing a key pair.Return Value
An array of bytes representing the extracted public key.
-
Extract and return the public key from an
OpaquePointer
key pair.Throws
An error if the extraction process fails.
Declaration
Swift
internal func extractPublicKey(from keyPair: OpaquePointer) throws -> [UInt8]
Parameters
keyPair
An
OpaquePointer
representing a key pair.Return Value
An array of bytes representing the extracted public key.
-
Decode a public key from a byte array using the specified
OpaquePointer
parameters.Throws
An error if the decoding process fails or if the key type is not supported.
Important
The returned reference to the public key has to be freed by the caller. The pointer will reference a
EVP_PKEY
structure (so useEVP_PKEY_free
then).Declaration
Swift
internal static func decodePublicKey(from bytes: [UInt8], withParams params: OpaquePointer) throws -> OpaquePointer
Parameters
bytes
The byte array representing the public key.
params
OpaquePointer
parameters for the decoding operation.Return Value
An
OpaquePointer
representing the decoded public key. -
Decode a public key from a byte array using the specified
OpaquePointer
parameters.Throws
An error if the decoding process fails.
Important
The returned reference to the public key has to be freed by the caller. The pointer will reference a
EVP_PKEY
structure (so useEVP_PKEY_free
then).Declaration
Swift
internal func decodePublicKey(from bytes: [UInt8], withParams params: OpaquePointer) throws -> OpaquePointer
Parameters
bytes
The byte array representing the public key.
params
OpaquePointer
parameters for the decoding operation.Return Value
An
OpaquePointer
representing the decoded public key. -
Convert a key into an array of bytes using the specified
KeyAgreementAlgorithm
.Throws
An error if the conversion fails.
Tip:
keyPair
is required just using ECDH.
Declaration
Swift
internal static func convertToBytes(key: OpaquePointer, keyPair: OpaquePointer? = nil, for algorithm: KeyAgreementAlgorithm) throws -> [UInt8]
Parameters
key
A key as an
OpaquePointer
.keyPair
A key pair as an
OpaquePointer?
(defaultnil
).algorithm
The
KeyAgreementAlgorithm
used to perform the conversion.Return Value
An array of bytes representing the shared secret.
-
Convert a key into an array of bytes.
Throws
An error if the conversion fails.
Tip:
keyPair
is required just using ECDH.
Declaration
Swift
internal func convertToBytes(key: OpaquePointer, keyPair: OpaquePointer? = nil) throws -> [UInt8]
Parameters
sharedSecret
A key as an
OpaquePointer
.keyPair
A key pair as an
OpaquePointer?
(defaultnil
).Return Value
An array of bytes representing the shared secret.
-
Free the shared secret reference computed with the given
KeyAgreementAlgorithm
.Declaration
Swift
internal static func free(sharedSecret: OpaquePointer, for algorithm: KeyAgreementAlgorithm)
Parameters
sharedSecret
The shared secret reference.
algorithm
The
KeyAgreementAlgorithm
used to compute the secret. -
Free the shared secret reference.
Declaration
Swift
internal func free(sharedSecret: OpaquePointer)
Parameters
sharedSecret
The shared secret reference.