generateRandBytes -> generateCipherStream

This commit is contained in:
Olaoluwa Osuntokun
2015-10-14 23:42:39 -07:00
parent b1dfeb3724
commit 05d034e3ef

View File

@@ -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