|
| 1 | +//go:build linux |
| 2 | + |
| 3 | +package openssl_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "crypto/sha1" |
| 8 | + "crypto/sha256" |
| 9 | + "hash" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/golang-fips/openssl/v2" |
| 13 | +) |
| 14 | + |
| 15 | +type testVector struct { |
| 16 | + password string |
| 17 | + salt string |
| 18 | + iter int |
| 19 | + output []byte |
| 20 | +} |
| 21 | + |
| 22 | +// Test vectors from RFC 6070, http://tools.ietf.org/html/rfc6070 |
| 23 | +var sha1TestVectors = []testVector{ |
| 24 | + { |
| 25 | + "password", |
| 26 | + "salt", |
| 27 | + 1, |
| 28 | + []byte{ |
| 29 | + 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71, |
| 30 | + 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06, |
| 31 | + 0x2f, 0xe0, 0x37, 0xa6, |
| 32 | + }, |
| 33 | + }, |
| 34 | + { |
| 35 | + "password", |
| 36 | + "salt", |
| 37 | + 2, |
| 38 | + []byte{ |
| 39 | + 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c, |
| 40 | + 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0, |
| 41 | + 0xd8, 0xde, 0x89, 0x57, |
| 42 | + }, |
| 43 | + }, |
| 44 | + { |
| 45 | + "password", |
| 46 | + "salt", |
| 47 | + 4096, |
| 48 | + []byte{ |
| 49 | + 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a, |
| 50 | + 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0, |
| 51 | + 0x65, 0xa4, 0x29, 0xc1, |
| 52 | + }, |
| 53 | + }, |
| 54 | + { |
| 55 | + "passwordPASSWORDpassword", |
| 56 | + "saltSALTsaltSALTsaltSALTsaltSALTsalt", |
| 57 | + 4096, |
| 58 | + []byte{ |
| 59 | + 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b, |
| 60 | + 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a, |
| 61 | + 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70, |
| 62 | + 0x38, |
| 63 | + }, |
| 64 | + }, |
| 65 | + { |
| 66 | + "pass\000word", |
| 67 | + "sa\000lt", |
| 68 | + 4096, |
| 69 | + []byte{ |
| 70 | + 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d, |
| 71 | + 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3, |
| 72 | + }, |
| 73 | + }, |
| 74 | +} |
| 75 | + |
| 76 | +// Test vectors from |
| 77 | +// http://stackoverflow.com/questions/5130513/pbkdf2-hmac-sha2-test-vectors |
| 78 | +// except the first one, which is not copied from elsewhere. |
| 79 | +var sha256TestVectors = []testVector{ |
| 80 | + { |
| 81 | + "", |
| 82 | + "salt", |
| 83 | + 1, |
| 84 | + []byte{ |
| 85 | + 0xf1, 0x35, 0xc2, 0x79, 0x93, 0xba, 0xf9, 0x87, |
| 86 | + 0x73, 0xc5, 0xcd, 0xb4, 0x0a, 0x57, 0x06, 0xce, |
| 87 | + 0x6a, 0x34, 0x5c, 0xde, 0x61, |
| 88 | + }, |
| 89 | + }, |
| 90 | + { |
| 91 | + "password", |
| 92 | + "salt", |
| 93 | + 1, |
| 94 | + []byte{ |
| 95 | + 0x12, 0x0f, 0xb6, 0xcf, 0xfc, 0xf8, 0xb3, 0x2c, |
| 96 | + 0x43, 0xe7, 0x22, 0x52, 0x56, 0xc4, 0xf8, 0x37, |
| 97 | + 0xa8, 0x65, 0x48, 0xc9, |
| 98 | + }, |
| 99 | + }, |
| 100 | + { |
| 101 | + "password", |
| 102 | + "salt", |
| 103 | + 2, |
| 104 | + []byte{ |
| 105 | + 0xae, 0x4d, 0x0c, 0x95, 0xaf, 0x6b, 0x46, 0xd3, |
| 106 | + 0x2d, 0x0a, 0xdf, 0xf9, 0x28, 0xf0, 0x6d, 0xd0, |
| 107 | + 0x2a, 0x30, 0x3f, 0x8e, |
| 108 | + }, |
| 109 | + }, |
| 110 | + { |
| 111 | + "password", |
| 112 | + "salt", |
| 113 | + 4096, |
| 114 | + []byte{ |
| 115 | + 0xc5, 0xe4, 0x78, 0xd5, 0x92, 0x88, 0xc8, 0x41, |
| 116 | + 0xaa, 0x53, 0x0d, 0xb6, 0x84, 0x5c, 0x4c, 0x8d, |
| 117 | + 0x96, 0x28, 0x93, 0xa0, |
| 118 | + }, |
| 119 | + }, |
| 120 | + { |
| 121 | + "passwordPASSWORDpassword", |
| 122 | + "saltSALTsaltSALTsaltSALTsaltSALTsalt", |
| 123 | + 4096, |
| 124 | + []byte{ |
| 125 | + 0x34, 0x8c, 0x89, 0xdb, 0xcb, 0xd3, 0x2b, 0x2f, |
| 126 | + 0x32, 0xd8, 0x14, 0xb8, 0x11, 0x6e, 0x84, 0xcf, |
| 127 | + 0x2b, 0x17, 0x34, 0x7e, 0xbc, 0x18, 0x00, 0x18, |
| 128 | + 0x1c, |
| 129 | + }, |
| 130 | + }, |
| 131 | + { |
| 132 | + "pass\000word", |
| 133 | + "sa\000lt", |
| 134 | + 4096, |
| 135 | + []byte{ |
| 136 | + 0x89, 0xb6, 0x9d, 0x05, 0x16, 0xf8, 0x29, 0x89, |
| 137 | + 0x3c, 0x69, 0x62, 0x26, 0x65, 0x0a, 0x86, 0x87, |
| 138 | + }, |
| 139 | + }, |
| 140 | +} |
| 141 | + |
| 142 | +func testHash(t *testing.T, h func() hash.Hash, hashName string, vectors []testVector) { |
| 143 | + for i, v := range vectors { |
| 144 | + o, err := openssl.PBKDF2([]byte(v.password), []byte(v.salt), v.iter, len(v.output), h) |
| 145 | + if err != nil { |
| 146 | + t.Errorf("%s %d: %s", hashName, i, err) |
| 147 | + continue |
| 148 | + } |
| 149 | + if !bytes.Equal(o, v.output) { |
| 150 | + t.Errorf("%s %d: expected %x, got %x", hashName, i, v.output, o) |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +func TestWithHMACSHA1(t *testing.T) { |
| 156 | + testHash(t, openssl.NewSHA1, "SHA1", sha1TestVectors) |
| 157 | +} |
| 158 | + |
| 159 | +func TestWithHMACSHA256(t *testing.T) { |
| 160 | + testHash(t, openssl.NewSHA256, "SHA256", sha256TestVectors) |
| 161 | +} |
| 162 | + |
| 163 | +var sink uint8 |
| 164 | + |
| 165 | +func benchmark(b *testing.B, h func() hash.Hash) { |
| 166 | + password := make([]byte, h().Size()) |
| 167 | + salt := make([]byte, 8) |
| 168 | + var err error |
| 169 | + for i := 0; i < b.N; i++ { |
| 170 | + password, err = openssl.PBKDF2(password, salt, 4096, len(password), h) |
| 171 | + if err != nil { |
| 172 | + b.Fatal(err) |
| 173 | + } |
| 174 | + } |
| 175 | + sink += password[0] |
| 176 | +} |
| 177 | + |
| 178 | +func BenchmarkHMACSHA1(b *testing.B) { |
| 179 | + benchmark(b, sha1.New) |
| 180 | +} |
| 181 | + |
| 182 | +func BenchmarkHMACSHA256(b *testing.B) { |
| 183 | + benchmark(b, sha256.New) |
| 184 | +} |
0 commit comments