-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24_AlgorithmAgility.Tests.ps1
More file actions
124 lines (97 loc) · 4.63 KB
/
24_AlgorithmAgility.Tests.ps1
File metadata and controls
124 lines (97 loc) · 4.63 KB
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
BeforeAll {
. $PSScriptRoot/24_AlgorithmAgility.ps1
}
Describe 'ProfileFactory' {
It 'Returns the default CryptoProfile for default aliases' {
$profiles = @('default', 'CryptoProfile', 'v1', '1')
foreach ($name in $profiles) {
$profile = [ProfileFactory]::Create($name)
$profile.GetType().Name | Should -Be 'CryptoProfile'
$profile.CipherAlgorithm | Should -Be 'AES-256-GCM'
$profile.HashAlgorithm | Should -Be 'SHA-256'
$profile.KdfIterations | Should -Be 200000
}
}
It 'Returns a Fips140Profile for FIPS aliases' {
$profiles = @('fips140', 'Fips140Profile', 'FIPS140-2024', 'fips')
foreach ($name in $profiles) {
$profile = [ProfileFactory]::Create($name)
$profile.GetType().Name | Should -Be 'Fips140Profile'
$profile.CipherAlgorithm | Should -Be 'AES-256-GCM'
$profile.HashAlgorithm | Should -Be 'SHA-384'
$profile.KdfAlgorithm | Should -Be 'PBKDF2-SHA256'
$profile.KdfIterations | Should -Be 310000
}
}
It 'Throws for unknown profile names' {
{ [ProfileFactory]::Create('unknown-profile') } |
Should -Throw -ExceptionType ([System.ArgumentException])
}
}
Describe 'AgileEncryptor' {
Context 'Hash behavior' {
It 'Uses the profile hash algorithm for the default profile' {
$profile = [ProfileFactory]::Create('default')
$encryptor = [AgileEncryptor]::new($profile)
$data = [System.Text.Encoding]::UTF8.GetBytes('hash me')
$actual = $encryptor.Hash($data)
$expected = [System.Security.Cryptography.SHA256]::Create().ComputeHash($data)
$actual.Length | Should -Be 32
$actual | Should -Be $expected
}
It 'Uses the profile hash algorithm for the FIPS profile' {
$profile = [ProfileFactory]::Create('fips140')
$encryptor = [AgileEncryptor]::new($profile)
$data = [System.Text.Encoding]::UTF8.GetBytes('hash me')
$actual = $encryptor.Hash($data)
$expected = [System.Security.Cryptography.SHA384]::Create().ComputeHash($data)
$actual.Length | Should -Be 48
$actual | Should -Be $expected
}
It 'Hashes null input as an empty byte array' {
$encryptor = [AgileEncryptor]::new([ProfileFactory]::Create('default'))
$actual = $encryptor.Hash($null)
$expected = $encryptor.Hash([byte[]]::new(0))
$actual | Should -Be $expected
$actual.Length | Should -Be 32
}
It 'Changes hash output when the profile changes' {
$data = [System.Text.Encoding]::UTF8.GetBytes('profile swap')
$defaultEncryptor = [AgileEncryptor]::new([ProfileFactory]::Create('default'))
$fipsEncryptor = [AgileEncryptor]::new([ProfileFactory]::Create('fips140'))
$defaultHash = $defaultEncryptor.Hash($data)
$fipsHash = $fipsEncryptor.Hash($data)
$defaultHash.Length | Should -Be 32
$fipsHash.Length | Should -Be 48
$defaultHash | Should -Not -Be $fipsHash
}
}
Context 'Encryption package metadata' {
It 'Stores the profile version in the encrypted package' {
$profile = [ProfileFactory]::Create('default')
$encryptor = [AgileEncryptor]::new($profile)
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('versioned payload')
$package = $encryptor.Encrypt($plaintext)
$package.Profile | Should -Be $profile.Version
$package.Algorithm | Should -Be $profile.CipherAlgorithm
}
It 'Round-trips ciphertext with the FIPS profile' {
$profile = [ProfileFactory]::Create('fips140')
$encryptor = [AgileEncryptor]::new($profile)
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('round trip')
$package = $encryptor.Encrypt($plaintext)
$decrypted = $encryptor.Decrypt($package)
$package.Profile | Should -Be $profile.Version
$decrypted | Should -Be $plaintext
}
It 'Round-trips ciphertext with the default profile' {
$profile = [ProfileFactory]::Create('default')
$encryptor = [AgileEncryptor]::new($profile)
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('round trip')
$package = $encryptor.Encrypt($plaintext)
$decrypted = $encryptor.Decrypt($package)
$package.Profile | Should -Be $profile.Version
$decrypted | Should -Be $plaintext
}
}
}