mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-20 21:30:06 +02:00
generateRandBytes -> generateCipherStream
This commit is contained in:
21
sphinx.go
21
sphinx.go
@@ -133,7 +133,7 @@ func GenerateSphinxHeader(dest []byte, identifier [securityParameter]byte,
|
|||||||
// Encrypt the header for the final hop with the shared secret the
|
// Encrypt the header for the final hop with the shared secret the
|
||||||
// destination will eventually derive, then pad the message out to full
|
// destination will eventually derive, then pad the message out to full
|
||||||
// size with the "random" filler bytes.
|
// size with the "random" filler bytes.
|
||||||
streamBytes := generateRandBytes(generateKey("rho", hopSharedSecrets[numHops-1]))
|
streamBytes := generateCipherStream(generateKey("rho", hopSharedSecrets[numHops-1]), numStreamBytes)
|
||||||
xor(mixHeader, mixHeader, streamBytes[:(2*(numMaxHops-numHops)+3)*securityParameter])
|
xor(mixHeader, mixHeader, streamBytes[:(2*(numMaxHops-numHops)+3)*securityParameter])
|
||||||
mixHeader = append(mixHeader, filler...)
|
mixHeader = append(mixHeader, filler...)
|
||||||
|
|
||||||
@@ -159,7 +159,7 @@ func GenerateSphinxHeader(dest []byte, identifier [securityParameter]byte,
|
|||||||
// Mix header itself.
|
// Mix header itself.
|
||||||
b.Write(mixHeader[:(2*numMaxHops-1)*securityParameter])
|
b.Write(mixHeader[:(2*numMaxHops-1)*securityParameter])
|
||||||
|
|
||||||
streamBytes := generateRandBytes(generateKey("rho", hopSharedSecrets[i]))
|
streamBytes := generateCipherStream(generateKey("rho", hopSharedSecrets[i]), numStreamBytes)
|
||||||
xor(mixHeader, b.Bytes(), streamBytes[:(2*numMaxHops+1)*securityParameter])
|
xor(mixHeader, b.Bytes(), streamBytes[:(2*numMaxHops+1)*securityParameter])
|
||||||
headerMac = calcMac(generateKey("mu", hopSharedSecrets[i]), mixHeader)
|
headerMac = calcMac(generateKey("mu", hopSharedSecrets[i]), mixHeader)
|
||||||
}
|
}
|
||||||
@@ -194,7 +194,8 @@ func generateHeaderPadding(numHops int, sharedSecrets [][sharedSecretSize]byte)
|
|||||||
tempBuf.Write(filler)
|
tempBuf.Write(filler)
|
||||||
tempBuf.Write(padding)
|
tempBuf.Write(padding)
|
||||||
|
|
||||||
streamBytes := generateRandBytes(generateKey("rho", sharedSecrets[i-1]))
|
streamBytes := generateCipherStream(generateKey("rho", sharedSecrets[i-1]),
|
||||||
|
numStreamBytes)
|
||||||
|
|
||||||
xor(filler, tempBuf.Bytes(), streamBytes[slice:])
|
xor(filler, tempBuf.Bytes(), streamBytes[slice:])
|
||||||
}
|
}
|
||||||
@@ -322,25 +323,21 @@ func generateKey(keyType string, sharedKey [sharedSecretSize]byte) [securityPara
|
|||||||
|
|
||||||
// generateRandBytes...
|
// generateRandBytes...
|
||||||
// generates
|
// generates
|
||||||
func generateRandBytes(key [securityParameter]byte) [numStreamBytes]byte {
|
func generateCipherStream(key [securityParameter]byte, numBytes uint) []byte {
|
||||||
var r [numStreamBytes]byte
|
|
||||||
|
|
||||||
block, _ := aes.NewCipher(key[:])
|
block, _ := aes.NewCipher(key[:])
|
||||||
|
|
||||||
// We use AES in CTR mode to generate a psuedo randmom stream of bytes
|
// We use AES in CTR mode to generate a psuedo randmom stream of bytes
|
||||||
// by encrypting a plaintext of all zeroes.
|
// by encrypting a plaintext of all zeroes.
|
||||||
randBytes := make([]byte, numStreamBytes)
|
cipherStream := make([]byte, numBytes)
|
||||||
plainText := bytes.Repeat([]byte{0}, numStreamBytes)
|
plainText := bytes.Repeat([]byte{0}, int(numBytes))
|
||||||
|
|
||||||
// Our IV is just zero....
|
// Our IV is just zero....
|
||||||
iv := bytes.Repeat([]byte{0}, aes.BlockSize)
|
iv := bytes.Repeat([]byte{0}, aes.BlockSize)
|
||||||
|
|
||||||
stream := cipher.NewCTR(block, iv)
|
stream := cipher.NewCTR(block, iv)
|
||||||
stream.XORKeyStream(randBytes, plainText)
|
stream.XORKeyStream(cipherStream, plainText)
|
||||||
|
|
||||||
copy(r[:], randBytes)
|
return cipherStream
|
||||||
|
|
||||||
return r
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ComputeBlindingFactor for the next hop given the ephemeral pubKey and
|
// ComputeBlindingFactor for the next hop given the ephemeral pubKey and
|
||||||
|
Reference in New Issue
Block a user