SessionKeyGenerator
internal final class SessionKeyGenerator
A generator for session keys using specified key derivation mechanisms.
The SessionKeyGenerator
class is responsible for deriving session keys based on defined
key derivation mechanisms. These session keys are used for various purposes,
such as encryption, message authentication and security protocols.
Key Derivation Function
The key derivation function KDF(K, c)
takes the shared secret value K
and a 32-bit,
big-endian integer c
as inputs. It produces an octet string keydata
as output,
which is computed as keydata = H(K || c)
. The hash function H()
used in the key derivation
must have a bit-length greater than or equal to the derived key’s bit-length.
Note
The hash value is interpreted as a big-endian byte output.Using 3DES
To derive 128-bit (112-bit excluding parity bits) 3DES keys, the SHA-1 hash function is used, and the following additional steps must be performed:
- Use octets 1 to 8 of
keydata
to formkeydataA
and octets 9 to 16 to formkeydataB
. Additional octets are not used. - Optionally, adjust the parity bits of
keydataA
andkeydataB
to form correct DES keys.
Using AES
To derive 128-bit AES keys, the SHA-1 hash function is used, and the following additional step must be performed:
- Use octets 1 to 16 of
keydata
. Additional octets are not used.
For 192-bit and 256-bit AES keys, SHA-256 is used as the hash function.
For 192-bit AES keys, the following additional step must be performed:
- Use octets 1 to 24 of keydata; additional octets are not used.
KeyDerivationFunctionMode
The SessionKeyGenerator
includes an enum called KeyDerivationFunctionMode
, which defines different modes
for deriving session keys. Each mode corresponds to a specific use case:
ENC_MODE
: For deriving session keys used in encryption.MAC_MODE
: For deriving session keys used in message authentication.PACE_MODE
: For deriving session keys used in the PACE protocol.
See also
NFCSecureSession
, SecurityConfiguration
SecureChannel
and SecureMessaging
-
Declaration
Swift
internal init(securityConfig: SecurityConfiguration)
-
Derives a session key based on the provided key seed, nonce, and key derivation function mode.
Throws
An error if key derivation fails.
Declaration
Swift
internal func deriveKey(keySeed: [UInt8], nonce: [UInt8]? = nil, mode: KeyDerivationFunctionMode) throws -> [UInt8]
Parameters
keySeed
The key seed for key derivation.
nonce
An optional nonce value (default is nil).
mode
The key
KeyDerivationFunctionMode
.Return Value
The derived session key.
-
This enum contains all the different modes using to derive session keys.
The KDF input requires a 32-bit, big-endian integer counter
c
, and its value is different according to the session key uses. Here, the following uses/modes are defined:ENC_MODE
: To derive a session key for encryption.MAC_MODE
: To derive a session key for authentication of data.PACE_MODE
: To derive a session key for the PACE protocol.
Declaration
Swift
internal enum KeyDerivationFunctionMode : UInt8