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