Small cleanup.

This commit is contained in:
Coda Hale 2013-08-20 17:39:06 -07:00
parent ce0a68115f
commit 2c93b6d329

View file

@ -80,10 +80,10 @@ func bits2octets(in []byte, q *big.Int, qlen, rolen int) []byte {
// https://tools.ietf.org/html/rfc6979#section-3.2 // https://tools.ietf.org/html/rfc6979#section-3.2
func generateSecret(q, x *big.Int, alg HashAlgorithm, hash []byte, test func(*big.Int) bool) { func generateSecret(q, x *big.Int, alg HashAlgorithm, hash []byte, test func(*big.Int) bool) {
// Step A
qlen := q.BitLen() qlen := q.BitLen()
holen := alg().Size() holen := alg().Size()
rolen := (qlen + 7) >> 3 rolen := (qlen + 7) >> 3
bx := append(int2octets(x, rolen), bits2octets(hash, q, qlen, rolen)...)
// Step B // Step B
v := bytes.Repeat([]byte{0x01}, holen) v := bytes.Repeat([]byte{0x01}, holen)
@ -92,9 +92,6 @@ func generateSecret(q, x *big.Int, alg HashAlgorithm, hash []byte, test func(*bi
k := bytes.Repeat([]byte{0x00}, holen) k := bytes.Repeat([]byte{0x00}, holen)
// Step D // Step D
b := int2octets(x, rolen)
bh := bits2octets(hash, q, qlen, rolen)
bx := append(b, bh...)
k = alg.mac(k, append(append(v, 0x00), bx...)) k = alg.mac(k, append(append(v, 0x00), bx...))
@ -107,6 +104,7 @@ func generateSecret(q, x *big.Int, alg HashAlgorithm, hash []byte, test func(*bi
// Step G // Step G
v = alg.mac(k, v) v = alg.mac(k, v)
// Step H
for { for {
// Step H1 // Step H1
t := make([]byte, 0) t := make([]byte, 0)
@ -117,12 +115,12 @@ func generateSecret(q, x *big.Int, alg HashAlgorithm, hash []byte, test func(*bi
t = append(t, v...) t = append(t, v...)
} }
// Step H3
secret := bits2int(t, qlen) secret := bits2int(t, qlen)
if secret.Cmp(big.NewInt(1)) >= 0 && secret.Cmp(q) < 0 && test(secret) { if secret.Cmp(big.NewInt(1)) >= 0 && secret.Cmp(q) < 0 && test(secret) {
return return
} }
k = alg.mac(k, append(v, 0x00)) k = alg.mac(k, append(v, 0x00))
v = alg.mac(k, v) v = alg.mac(k, v)
} }
} }