File: HKDFTests.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (147 lines) | stat: -rw-r--r-- 7,208 bytes parent folder | download
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
//===----------------------------------------------------------------------===//
//
// 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 XCTest

#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
// Skip tests that require @testable imports of CryptoKit.
#else
#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@testable import CryptoKit
#else
@testable import Crypto
#endif

// Test Vectors are coming from https://tools.ietf.org/html/rfc5869
class HKDFTests: XCTestCase {
    struct RFCTestVector: Codable {
        var hash: String
        var inputSecret: [UInt8]
        var salt: [UInt8]
        var sharedInfo: [UInt8]
        var outputLength: Int
        var pseudoRandomKey: [UInt8]
        var outputKeyMaterial: [UInt8]
        
        enum CodingKeys: String, CodingKey {
            case hash = "Hash"
            case inputSecret = "IKM"
            case salt
            case sharedInfo = "info"
            case outputLength = "L"
            case pseudoRandomKey = "PRK"
            case outputKeyMaterial = "OKM"
        }
    }
    
    func expandExtractTesting<H: HashFunction>(_ vector: RFCTestVector, hash: H.Type) {
        let (contiguousSalt, discontiguousSalt) = vector.salt.asDataProtocols()
        let (contiguousSharedInfo, discontiguousSharedInfo) = vector.sharedInfo.asDataProtocols()
        
        let PRK1 = HKDF<H>.extract(inputKeyMaterial: SymmetricKey(data: vector.inputSecret),
                                   salt: contiguousSalt)
        let PRK2 = HKDF<H>.extract(inputKeyMaterial: SymmetricKey(data: vector.inputSecret),
                                   salt: discontiguousSalt)
        
        let OKM1 = HKDF<H>.expand(pseudoRandomKey: PRK1, info: contiguousSharedInfo,
                                  outputByteCount: vector.outputLength)
        let OKM2 = HKDF<H>.expand(pseudoRandomKey: PRK1, info: discontiguousSharedInfo,
                                  outputByteCount: vector.outputLength)
        let OKM3 = HKDF<H>.expand(pseudoRandomKey: PRK2, info: contiguousSharedInfo,
                                  outputByteCount: vector.outputLength)
        let OKM4 = HKDF<H>.expand(pseudoRandomKey: PRK2, info: discontiguousSharedInfo,
                                  outputByteCount: vector.outputLength)
        
        XCTAssertEqual(Data(PRK1.digest), Data(vector.pseudoRandomKey))
        XCTAssertEqual(Data(PRK2.digest), Data(vector.pseudoRandomKey))
        
        let expectedOKM = SymmetricKey(data: vector.outputKeyMaterial)
        XCTAssertEqual(OKM1, expectedOKM)
        XCTAssertEqual(OKM2, expectedOKM)
        XCTAssertEqual(OKM3, expectedOKM)
        XCTAssertEqual(OKM4, expectedOKM)
    }
    
    func oneshotTesting<H: HashFunction>(_ vector: RFCTestVector, hash: H.Type) {
        let (contiguousSalt, discontiguousSalt) = vector.salt.asDataProtocols()
        let (contiguousSharedInfo, discontiguousSharedInfo) = vector.sharedInfo.asDataProtocols()
        
        let OKM1 = HKDF<H>.deriveKey(inputKeyMaterial: SymmetricKey(data: vector.inputSecret),
                                     salt: contiguousSalt,
                                     info: vector.sharedInfo, outputByteCount: vector.outputLength)
        let OKM2 = HKDF<H>.deriveKey(inputKeyMaterial: SymmetricKey(data: vector.inputSecret),
                                     salt: contiguousSalt, info: vector.sharedInfo,
                                     outputByteCount: vector.outputLength)
        let OKM3 = HKDF<H>.deriveKey(inputKeyMaterial: SymmetricKey(data: vector.inputSecret),
                                     salt: discontiguousSalt, info: contiguousSharedInfo,
                                     outputByteCount: vector.outputLength)
        let OKM4 = HKDF<H>.deriveKey(inputKeyMaterial: SymmetricKey(data: vector.inputSecret),
                                     salt: discontiguousSalt, info: discontiguousSharedInfo,
                                     outputByteCount: vector.outputLength)
        
        let expectedOKM = SymmetricKey(data: vector.outputKeyMaterial)
        XCTAssertEqual(OKM1, expectedOKM)
        XCTAssertEqual(OKM2, expectedOKM)
        XCTAssertEqual(OKM3, expectedOKM)
        XCTAssertEqual(OKM4, expectedOKM)
    }
    
    func sharedSecretTesting<H: HashFunction>(_ vector: RFCTestVector, hash: H.Type) {
        let ss = SharedSecret(ss: SecureBytes(bytes: vector.inputSecret))
        let (contiguousSalt, discontiguousSalt) = vector.salt.asDataProtocols()
        let (contiguousSharedInfo, discontiguousSharedInfo) = vector.sharedInfo.asDataProtocols()
        
        let firstKey = ss.hkdfDerivedSymmetricKey(using: H.self, salt: contiguousSalt,
                                                  sharedInfo: contiguousSharedInfo, outputByteCount: vector.outputLength)
        let secondKey = ss.hkdfDerivedSymmetricKey(using: H.self, salt: contiguousSalt,
                                                   sharedInfo: discontiguousSharedInfo, outputByteCount: vector.outputLength)
        let thirdKey = ss.hkdfDerivedSymmetricKey(using: H.self, salt: discontiguousSalt,
                                                  sharedInfo: contiguousSharedInfo, outputByteCount: vector.outputLength)
        let fourthKey = ss.hkdfDerivedSymmetricKey(using: H.self, salt: discontiguousSalt,
                                                   sharedInfo: discontiguousSharedInfo, outputByteCount: vector.outputLength)
        
        let expectedKey = SymmetricKey(data: vector.outputKeyMaterial)
        XCTAssertEqual(firstKey, expectedKey)
        XCTAssertEqual(secondKey, expectedKey)
        XCTAssertEqual(thirdKey, expectedKey)
        XCTAssertEqual(fourthKey, expectedKey)
    }
    
    func testRFCVector<H: HashFunction>(_ vector: RFCTestVector, hash: H.Type) throws {
        sharedSecretTesting(vector, hash: hash)
        oneshotTesting(vector, hash: hash)
        expandExtractTesting(vector, hash: hash)
    }
    
    func testRfcTestVectorsSHA1() throws {
        var decoder = try orFail { try RFCVectorDecoder(bundleType: self, fileName: "rfc-5869-HKDF-SHA1") }
        let vectors = try orFail { try decoder.decode([RFCTestVector].self) }
        
        for vector in vectors {
            precondition(vector.hash == "SHA-1")
            try orFail { try self.testRFCVector(vector, hash: Insecure.SHA1.self) }
        }
    }
    
    func testRfcTestVectorsSHA256() throws {
        var decoder = try orFail { try RFCVectorDecoder(bundleType: self, fileName: "rfc-5869-HKDF-SHA256") }
        let vectors = try orFail { try decoder.decode([RFCTestVector].self) }
        
        for vector in vectors {
            precondition(vector.hash == "SHA-256")
            try orFail { try self.testRFCVector(vector, hash: SHA256.self) }
        }
    }
}

#endif // CRYPTO_IN_SWIFTPM