| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package josecipher |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"crypto" |
| 22 |
"testing" |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
func TestVectorConcatKDF(t *testing.T) { |
| 27 |
z := []byte{ |
| 28 |
158, 86, 217, 29, 129, 113, 53, 211, 114, 131, 66, 131, 191, 132, |
| 29 |
38, 156, 251, 49, 110, 163, 218, 128, 106, 72, 246, 218, 167, 121, |
| 30 |
140, 254, 144, 196} |
| 31 |
|
| 32 |
algID := []byte{0, 0, 0, 7, 65, 49, 50, 56, 71, 67, 77} |
| 33 |
|
| 34 |
ptyUInfo := []byte{0, 0, 0, 5, 65, 108, 105, 99, 101} |
| 35 |
ptyVInfo := []byte{0, 0, 0, 3, 66, 111, 98} |
| 36 |
|
| 37 |
supPubInfo := []byte{0, 0, 0, 128} |
| 38 |
supPrivInfo := []byte{} |
| 39 |
|
| 40 |
expected := []byte{ |
| 41 |
86, 170, 141, 234, 248, 35, 109, 32, 92, 34, 40, 205, 113, 167, 16, 26} |
| 42 |
|
| 43 |
ckdf := NewConcatKDF(crypto.SHA256, z, algID, ptyUInfo, ptyVInfo, supPubInfo, supPrivInfo) |
| 44 |
|
| 45 |
out0 := make([]byte, 9) |
| 46 |
out1 := make([]byte, 7) |
| 47 |
|
| 48 |
read0, err := ckdf.Read(out0) |
| 49 |
if err != nil { |
| 50 |
t.Error("error when reading from concat kdf reader", err) |
| 51 |
return |
| 52 |
} |
| 53 |
|
| 54 |
read1, err := ckdf.Read(out1) |
| 55 |
if err != nil { |
| 56 |
t.Error("error when reading from concat kdf reader", err) |
| 57 |
return |
| 58 |
} |
| 59 |
|
| 60 |
if read0+read1 != len(out0)+len(out1) { |
| 61 |
t.Error("did not receive enough bytes from concat kdf reader") |
| 62 |
return |
| 63 |
} |
| 64 |
|
| 65 |
out := []byte{} |
| 66 |
out = append(out, out0...) |
| 67 |
out = append(out, out1...) |
| 68 |
|
| 69 |
if bytes.Compare(out, expected) != 0 { |
| 70 |
t.Error("did not receive expected output from concat kdf reader") |
| 71 |
return |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
func TestCache(t *testing.T) { |
| 76 |
z := []byte{ |
| 77 |
158, 86, 217, 29, 129, 113, 53, 211, 114, 131, 66, 131, 191, 132, |
| 78 |
38, 156, 251, 49, 110, 163, 218, 128, 106, 72, 246, 218, 167, 121, |
| 79 |
140, 254, 144, 196} |
| 80 |
|
| 81 |
algID := []byte{1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4} |
| 82 |
|
| 83 |
ptyUInfo := []byte{1, 2, 3, 4} |
| 84 |
ptyVInfo := []byte{4, 3, 2, 1} |
| 85 |
|
| 86 |
supPubInfo := []byte{} |
| 87 |
supPrivInfo := []byte{} |
| 88 |
|
| 89 |
outputs := [][]byte{} |
| 90 |
|
| 91 |
|
| 92 |
chunkSizes := []int{1, 2, 4, 8, 16, 32, 64, 128, 256, 512} |
| 93 |
|
| 94 |
for _, c := range chunkSizes { |
| 95 |
out := make([]byte, 1024) |
| 96 |
reader := NewConcatKDF(crypto.SHA256, z, algID, ptyUInfo, ptyVInfo, supPubInfo, supPrivInfo) |
| 97 |
|
| 98 |
for i := 0; i < 1024; i += c { |
| 99 |
_, _ = reader.Read(out[i : i+c]) |
| 100 |
} |
| 101 |
|
| 102 |
outputs = append(outputs, out) |
| 103 |
} |
| 104 |
|
| 105 |
for i := range outputs { |
| 106 |
if bytes.Compare(outputs[i], outputs[(i+1)%len(outputs)]) != 0 { |
| 107 |
t.Error("not all outputs from KDF matched") |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
func benchmarkKDF(b *testing.B, total int) { |
| 113 |
z := []byte{ |
| 114 |
158, 86, 217, 29, 129, 113, 53, 211, 114, 131, 66, 131, 191, 132, |
| 115 |
38, 156, 251, 49, 110, 163, 218, 128, 106, 72, 246, 218, 167, 121, |
| 116 |
140, 254, 144, 196} |
| 117 |
|
| 118 |
algID := []byte{1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4} |
| 119 |
|
| 120 |
ptyUInfo := []byte{1, 2, 3, 4} |
| 121 |
ptyVInfo := []byte{4, 3, 2, 1} |
| 122 |
|
| 123 |
supPubInfo := []byte{} |
| 124 |
supPrivInfo := []byte{} |
| 125 |
|
| 126 |
out := make([]byte, total) |
| 127 |
reader := NewConcatKDF(crypto.SHA256, z, algID, ptyUInfo, ptyVInfo, supPubInfo, supPrivInfo) |
| 128 |
|
| 129 |
b.ResetTimer() |
| 130 |
b.SetBytes(int64(total)) |
| 131 |
for i := 0; i < b.N; i++ { |
| 132 |
_, _ = reader.Read(out) |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
func BenchmarkConcatKDF_1k(b *testing.B) { |
| 137 |
benchmarkKDF(b, 1024) |
| 138 |
} |
| 139 |
|
| 140 |
func BenchmarkConcatKDF_64k(b *testing.B) { |
| 141 |
benchmarkKDF(b, 65536) |
| 142 |
} |
| 143 |
|
| 144 |
func BenchmarkConcatKDF_1MB(b *testing.B) { |
| 145 |
benchmarkKDF(b, 1048576) |
| 146 |
} |
| 147 |
|
| 148 |
func BenchmarkConcatKDF_64MB(b *testing.B) { |
| 149 |
benchmarkKDF(b, 67108864) |
| 150 |
} |