]> Cypherpunks.ru repositories - gogost.git/blob - src/cypherpunks.ru/gogost/gost341194/pbkdf2_test.go
ed16bd26337431eede38b83088e07a2f29105d3d
[gogost.git] / src / cypherpunks.ru / gogost / gost341194 / pbkdf2_test.go
1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2019 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 package gost341194
18
19 import (
20         "bytes"
21         "hash"
22         "testing"
23
24         "cypherpunks.ru/gogost/gost28147"
25         "golang.org/x/crypto/pbkdf2"
26 )
27
28 func PBKDF2Hash() hash.Hash {
29         return New(&gost28147.SboxIdGostR341194CryptoProParamSet)
30 }
31
32 // Test vectors for PBKDF2 taken from
33 // http://tc26.ru/methods/containers_v1/Addition_to_PKCS5_v1_0.pdf test vectors
34 func TestPBKDF2Vectors(t *testing.T) {
35         t.Run("1", func(t *testing.T) {
36                 if bytes.Compare(pbkdf2.Key(
37                         []byte("password"),
38                         []byte("salt"),
39                         1,
40                         32,
41                         PBKDF2Hash,
42                 ), []byte{
43                         0x73, 0x14, 0xe7, 0xc0, 0x4f, 0xb2, 0xe6, 0x62,
44                         0xc5, 0x43, 0x67, 0x42, 0x53, 0xf6, 0x8b, 0xd0,
45                         0xb7, 0x34, 0x45, 0xd0, 0x7f, 0x24, 0x1b, 0xed,
46                         0x87, 0x28, 0x82, 0xda, 0x21, 0x66, 0x2d, 0x58,
47                 }) != 0 {
48                         t.FailNow()
49                 }
50         })
51
52         t.Run("2", func(t *testing.T) {
53                 if bytes.Compare(pbkdf2.Key(
54                         []byte("password"),
55                         []byte("salt"),
56                         2,
57                         32,
58                         PBKDF2Hash,
59                 ), []byte{
60                         0x99, 0x0d, 0xfa, 0x2b, 0xd9, 0x65, 0x63, 0x9b,
61                         0xa4, 0x8b, 0x07, 0xb7, 0x92, 0x77, 0x5d, 0xf7,
62                         0x9f, 0x2d, 0xb3, 0x4f, 0xef, 0x25, 0xf2, 0x74,
63                         0x37, 0x88, 0x72, 0xfe, 0xd7, 0xed, 0x1b, 0xb3,
64                 }) != 0 {
65                         t.FailNow()
66                 }
67         })
68
69         t.Run("4096", func(t *testing.T) {
70                 if bytes.Compare(pbkdf2.Key(
71                         []byte("password"),
72                         []byte("salt"),
73                         4096,
74                         32,
75                         PBKDF2Hash,
76                 ), []byte{
77                         0x1f, 0x18, 0x29, 0xa9, 0x4b, 0xdf, 0xf5, 0xbe,
78                         0x10, 0xd0, 0xae, 0xb3, 0x6a, 0xf4, 0x98, 0xe7,
79                         0xa9, 0x74, 0x67, 0xf3, 0xb3, 0x11, 0x16, 0xa5,
80                         0xa7, 0xc1, 0xaf, 0xff, 0x9d, 0xea, 0xda, 0xfe,
81                 }) != 0 {
82                         t.FailNow()
83                 }
84         })
85
86         /*
87                 t.Run("16777216", func(t *testing.T) {
88                         if bytes.Compare(pbkdf2.Key(
89                                 []byte("password"),
90                                 []byte("salt"),
91                                 16777216,
92                                 32,
93                                 PBKDF2Hash,
94                         ), []byte{
95                                 0xa5, 0x7a, 0xe5, 0xa6, 0x08, 0x83, 0x96, 0xd1,
96                                 0x20, 0x85, 0x0c, 0x5c, 0x09, 0xde, 0x0a, 0x52,
97                                 0x51, 0x00, 0x93, 0x8a, 0x59, 0xb1, 0xb5, 0xc3,
98                                 0xf7, 0x81, 0x09, 0x10, 0xd0, 0x5f, 0xcd, 0x97,
99                         }) != 0 {
100                                 t.FailNow()
101                         }
102                 })
103         */
104
105         t.Run("many", func(t *testing.T) {
106                 if bytes.Compare(pbkdf2.Key(
107                         []byte("passwordPASSWORDpassword"),
108                         []byte("saltSALTsaltSALTsaltSALTsaltSALTsalt"),
109                         4096,
110                         40,
111                         PBKDF2Hash,
112                 ), []byte{
113                         0x78, 0x83, 0x58, 0xc6, 0x9c, 0xb2, 0xdb, 0xe2,
114                         0x51, 0xa7, 0xbb, 0x17, 0xd5, 0xf4, 0x24, 0x1f,
115                         0x26, 0x5a, 0x79, 0x2a, 0x35, 0xbe, 0xcd, 0xe8,
116                         0xd5, 0x6f, 0x32, 0x6b, 0x49, 0xc8, 0x50, 0x47,
117                         0xb7, 0x63, 0x8a, 0xcb, 0x47, 0x64, 0xb1, 0xfd,
118                 }) != 0 {
119                         t.FailNow()
120                 }
121         })
122
123         t.Run("zero byte", func(t *testing.T) {
124                 if bytes.Compare(pbkdf2.Key(
125                         []byte("pass\x00word"),
126                         []byte("sa\x00lt"),
127                         4096,
128                         20,
129                         PBKDF2Hash,
130                 ), []byte{
131                         0x43, 0xe0, 0x6c, 0x55, 0x90, 0xb0, 0x8c, 0x02,
132                         0x25, 0x24, 0x23, 0x73, 0x12, 0x7e, 0xdf, 0x9c,
133                         0x8e, 0x9c, 0x32, 0x91,
134                 }) != 0 {
135                         t.FailNow()
136                 }
137         })
138 }