|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2017 All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package bccsp |
| 18 | + |
| 19 | +import ( |
| 20 | + "reflect" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | +) |
| 26 | + |
| 27 | +func TestAESOpts(t *testing.T) { |
| 28 | + test := func(ephemeral bool) { |
| 29 | + for _, opts := range []KeyGenOpts{ |
| 30 | + &AES128KeyGenOpts{ephemeral}, |
| 31 | + &AES192KeyGenOpts{ephemeral}, |
| 32 | + &AES256KeyGenOpts{ephemeral}, |
| 33 | + } { |
| 34 | + expectedAlgorithm := reflect.TypeOf(opts).String()[7:13] |
| 35 | + assert.Equal(t, expectedAlgorithm, opts.Algorithm()) |
| 36 | + assert.Equal(t, ephemeral, opts.Ephemeral()) |
| 37 | + } |
| 38 | + } |
| 39 | + test(true) |
| 40 | + test(false) |
| 41 | + |
| 42 | + opts := &AESKeyGenOpts{true} |
| 43 | + assert.Equal(t, "AES", opts.Algorithm()) |
| 44 | + assert.True(t, opts.Ephemeral()) |
| 45 | + opts.Temporary = false |
| 46 | + assert.False(t, opts.Ephemeral()) |
| 47 | +} |
| 48 | + |
| 49 | +func TestRSAOpts(t *testing.T) { |
| 50 | + test := func(ephemeral bool) { |
| 51 | + for _, opts := range []KeyGenOpts{ |
| 52 | + &RSA1024KeyGenOpts{ephemeral}, |
| 53 | + &RSA2048KeyGenOpts{ephemeral}, |
| 54 | + &RSA3072KeyGenOpts{ephemeral}, |
| 55 | + &RSA4096KeyGenOpts{ephemeral}, |
| 56 | + } { |
| 57 | + expectedAlgorithm := reflect.TypeOf(opts).String()[7:14] |
| 58 | + assert.Equal(t, expectedAlgorithm, opts.Algorithm()) |
| 59 | + assert.Equal(t, ephemeral, opts.Ephemeral()) |
| 60 | + } |
| 61 | + } |
| 62 | + test(true) |
| 63 | + test(false) |
| 64 | +} |
| 65 | + |
| 66 | +func TestECDSAOpts(t *testing.T) { |
| 67 | + test := func(ephemeral bool) { |
| 68 | + for _, opts := range []KeyGenOpts{ |
| 69 | + &ECDSAP256KeyGenOpts{ephemeral}, |
| 70 | + &ECDSAP384KeyGenOpts{ephemeral}, |
| 71 | + } { |
| 72 | + expectedAlgorithm := reflect.TypeOf(opts).String()[7:16] |
| 73 | + assert.Equal(t, expectedAlgorithm, opts.Algorithm()) |
| 74 | + assert.Equal(t, ephemeral, opts.Ephemeral()) |
| 75 | + } |
| 76 | + } |
| 77 | + test(true) |
| 78 | + test(false) |
| 79 | + |
| 80 | + test = func(ephemeral bool) { |
| 81 | + for _, opts := range []KeyGenOpts{ |
| 82 | + &ECDSAKeyGenOpts{ephemeral}, |
| 83 | + &ECDSAPKIXPublicKeyImportOpts{ephemeral}, |
| 84 | + &ECDSAPrivateKeyImportOpts{ephemeral}, |
| 85 | + &ECDSAGoPublicKeyImportOpts{ephemeral}, |
| 86 | + } { |
| 87 | + assert.Equal(t, "ECDSA", opts.Algorithm()) |
| 88 | + assert.Equal(t, ephemeral, opts.Ephemeral()) |
| 89 | + } |
| 90 | + } |
| 91 | + test(true) |
| 92 | + test(false) |
| 93 | + |
| 94 | + opts := &ECDSAReRandKeyOpts{Temporary: true} |
| 95 | + assert.True(t, opts.Ephemeral()) |
| 96 | + opts.Temporary = false |
| 97 | + assert.False(t, opts.Ephemeral()) |
| 98 | + assert.Equal(t, "ECDSA_RERAND", opts.Algorithm()) |
| 99 | + assert.Empty(t, opts.ExpansionValue()) |
| 100 | +} |
| 101 | + |
| 102 | +func TestHashOpts(t *testing.T) { |
| 103 | + for _, ho := range []HashOpts{&SHA256Opts{}, &SHA384Opts{}, &SHA3_256Opts{}, &SHA3_384Opts{}} { |
| 104 | + s := strings.Replace(reflect.TypeOf(ho).String(), "*bccsp.", "", -1) |
| 105 | + algorithm := strings.Replace(s, "Opts", "", -1) |
| 106 | + assert.Equal(t, algorithm, ho.Algorithm()) |
| 107 | + ho2, err := GetHashOpt(algorithm) |
| 108 | + assert.NoError(t, err) |
| 109 | + assert.Equal(t, ho.Algorithm(), ho2.Algorithm()) |
| 110 | + } |
| 111 | + _, err := GetHashOpt("foo") |
| 112 | + assert.Error(t, err) |
| 113 | + assert.Contains(t, err.Error(), "hash function not recognized") |
| 114 | + |
| 115 | + assert.Equal(t, "SHA", (&SHAOpts{}).Algorithm()) |
| 116 | +} |
| 117 | + |
| 118 | +func TestHMAC(t *testing.T) { |
| 119 | + opts := &HMACTruncated256AESDeriveKeyOpts{Arg: []byte("arg")} |
| 120 | + assert.False(t, opts.Ephemeral()) |
| 121 | + opts.Temporary = true |
| 122 | + assert.True(t, opts.Ephemeral()) |
| 123 | + assert.Equal(t, "HMAC_TRUNCATED_256", opts.Algorithm()) |
| 124 | + assert.Equal(t, []byte("arg"), opts.Argument()) |
| 125 | + |
| 126 | + opts2 := &HMACDeriveKeyOpts{Arg: []byte("arg")} |
| 127 | + assert.False(t, opts2.Ephemeral()) |
| 128 | + opts2.Temporary = true |
| 129 | + assert.True(t, opts2.Ephemeral()) |
| 130 | + assert.Equal(t, "HMAC", opts2.Algorithm()) |
| 131 | + assert.Equal(t, []byte("arg"), opts2.Argument()) |
| 132 | +} |
| 133 | + |
| 134 | +func TestKeyGenOpts(t *testing.T) { |
| 135 | + expectedAlgorithms := map[reflect.Type]string{ |
| 136 | + reflect.TypeOf(&HMACImportKeyOpts{}): "HMAC", |
| 137 | + reflect.TypeOf(&RSAKeyGenOpts{}): "RSA", |
| 138 | + reflect.TypeOf(&RSAGoPublicKeyImportOpts{}): "RSA", |
| 139 | + reflect.TypeOf(&X509PublicKeyImportOpts{}): "X509Certificate", |
| 140 | + reflect.TypeOf(&AES256ImportKeyOpts{}): "AES", |
| 141 | + } |
| 142 | + test := func(ephemeral bool) { |
| 143 | + for _, opts := range []KeyGenOpts{ |
| 144 | + &HMACImportKeyOpts{ephemeral}, |
| 145 | + &RSAKeyGenOpts{ephemeral}, |
| 146 | + &RSAGoPublicKeyImportOpts{ephemeral}, |
| 147 | + &X509PublicKeyImportOpts{ephemeral}, |
| 148 | + &AES256ImportKeyOpts{ephemeral}, |
| 149 | + } { |
| 150 | + expectedAlgorithm := expectedAlgorithms[reflect.TypeOf(opts)] |
| 151 | + assert.Equal(t, expectedAlgorithm, opts.Algorithm()) |
| 152 | + assert.Equal(t, ephemeral, opts.Ephemeral()) |
| 153 | + } |
| 154 | + } |
| 155 | + test(true) |
| 156 | + test(false) |
| 157 | +} |
0 commit comments