Skip to content
This repository was archived by the owner on Sep 6, 2022. It is now read-only.

Commit 1262f60

Browse files
remove deprecated key stretching struct / function (#203)
This was needed for secio.
1 parent d28ef63 commit 1262f60

File tree

2 files changed

+2
-127
lines changed

2 files changed

+2
-127
lines changed

crypto/key.go

-105
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,16 @@ package crypto
55

66
import (
77
"crypto/elliptic"
8-
"crypto/hmac"
98
"crypto/rand"
10-
"crypto/sha1"
11-
"crypto/sha512"
129
"crypto/subtle"
1310
"encoding/base64"
1411
"errors"
1512
"fmt"
16-
"hash"
1713
"io"
1814

1915
pb "github.com/libp2p/go-libp2p-core/crypto/pb"
2016

2117
"github.com/gogo/protobuf/proto"
22-
"github.com/minio/sha256-simd"
2318
)
2419

2520
const (
@@ -170,106 +165,6 @@ func GenerateEKeyPair(curveName string) ([]byte, GenSharedKey, error) {
170165
return pubKey, done, nil
171166
}
172167

173-
// StretchedKeys ...
174-
type StretchedKeys struct {
175-
IV []byte
176-
MacKey []byte
177-
CipherKey []byte
178-
}
179-
180-
// PENDING DEPRECATION: KeyStretcher() will be deprecated with secio; for new
181-
// code, please use PBKDF2 (golang.org/x/crypto/pbkdf2) instead.
182-
// KeyStretcher returns a set of keys for each party by stretching the shared key.
183-
// (myIV, theirIV, myCipherKey, theirCipherKey, myMACKey, theirMACKey).
184-
// This function accepts the following cipher types:
185-
// - AES-128
186-
// - AES-256
187-
// The function will panic upon receiving an unknown cipherType
188-
func KeyStretcher(cipherType string, hashType string, secret []byte) (StretchedKeys, StretchedKeys) {
189-
var cipherKeySize int
190-
var ivSize int
191-
switch cipherType {
192-
case "AES-128":
193-
ivSize = 16
194-
cipherKeySize = 16
195-
case "AES-256":
196-
ivSize = 16
197-
cipherKeySize = 32
198-
default:
199-
panic("Unrecognized cipher, programmer error?")
200-
}
201-
202-
hmacKeySize := 20
203-
204-
seed := []byte("key expansion")
205-
206-
result := make([]byte, 2*(ivSize+cipherKeySize+hmacKeySize))
207-
208-
var h func() hash.Hash
209-
210-
switch hashType {
211-
case "SHA1":
212-
h = sha1.New
213-
case "SHA256":
214-
h = sha256.New
215-
case "SHA512":
216-
h = sha512.New
217-
default:
218-
panic("Unrecognized hash function, programmer error?")
219-
}
220-
221-
m := hmac.New(h, secret)
222-
// note: guaranteed to never return an error
223-
m.Write(seed)
224-
225-
a := m.Sum(nil)
226-
227-
j := 0
228-
for j < len(result) {
229-
m.Reset()
230-
231-
// note: guaranteed to never return an error.
232-
m.Write(a)
233-
m.Write(seed)
234-
235-
b := m.Sum(nil)
236-
237-
todo := len(b)
238-
239-
if j+todo > len(result) {
240-
todo = len(result) - j
241-
}
242-
243-
copy(result[j:j+todo], b)
244-
245-
j += todo
246-
247-
m.Reset()
248-
249-
// note: guaranteed to never return an error.
250-
m.Write(a)
251-
252-
a = m.Sum(nil)
253-
}
254-
255-
half := len(result) / 2
256-
r1 := result[:half]
257-
r2 := result[half:]
258-
259-
var k1 StretchedKeys
260-
var k2 StretchedKeys
261-
262-
k1.IV = r1[0:ivSize]
263-
k1.CipherKey = r1[ivSize : ivSize+cipherKeySize]
264-
k1.MacKey = r1[ivSize+cipherKeySize:]
265-
266-
k2.IV = r2[0:ivSize]
267-
k2.CipherKey = r2[ivSize : ivSize+cipherKeySize]
268-
k2.MacKey = r2[ivSize+cipherKeySize:]
269-
270-
return k1, k2
271-
}
272-
273168
// UnmarshalPublicKey converts a protobuf serialized public key into its
274169
// representative object
275170
func UnmarshalPublicKey(data []byte) (PubKey, error) {

crypto/key_test.go

+2-22
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import (
1313
"reflect"
1414
"testing"
1515

16-
btcec "github.com/btcsuite/btcd/btcec"
16+
"github.com/btcsuite/btcd/btcec"
1717
. "github.com/libp2p/go-libp2p-core/crypto"
1818
pb "github.com/libp2p/go-libp2p-core/crypto/pb"
1919
"github.com/libp2p/go-libp2p-core/test"
20-
sha256 "github.com/minio/sha256-simd"
20+
"github.com/minio/sha256-simd"
2121
)
2222

2323
func TestKeys(t *testing.T) {
@@ -304,23 +304,3 @@ func TestUnknownCurveErrors(t *testing.T) {
304304
t.Fatal("expected invalid key type to error")
305305
}
306306
}
307-
308-
func TestPanicOnUnknownCipherType(t *testing.T) {
309-
passed := false
310-
defer func() {
311-
if !passed {
312-
t.Fatal("expected known cipher and hash to succeed")
313-
}
314-
err := recover()
315-
errStr, ok := err.(string)
316-
if !ok {
317-
t.Fatal("expected string in panic")
318-
}
319-
if errStr != "Unrecognized cipher, programmer error?" {
320-
t.Fatal("expected \"Unrecognized cipher, programmer error?\"")
321-
}
322-
}()
323-
KeyStretcher("AES-256", "SHA1", []byte("foo"))
324-
passed = true
325-
KeyStretcher("Fooba", "SHA1", []byte("foo"))
326-
}

0 commit comments

Comments
 (0)