Files
go-i2p/lib/crypto/tunnel.go

59 lines
1.4 KiB
Go
Raw Normal View History

package crypto
2016-01-29 07:22:31 -05:00
import (
2016-01-29 07:22:31 -05:00
"crypto/aes"
"crypto/cipher"
)
2016-08-04 19:48:25 -07:00
type TunnelData [1028]byte
// A symetric key for encrypting tunnel messages
type TunnelKey [32]byte
2016-01-29 07:22:31 -05:00
// The initialization vector for a tunnel message
type TunnelIV []byte
type Tunnel struct {
2016-01-29 07:22:31 -05:00
layerKey cipher.Block
ivKey cipher.Block
}
func NewTunnelCrypto(layerKey, ivKey TunnelKey) (t *Tunnel, err error) {
2024-10-18 21:58:50 -04:00
log.Debug("Creating new Tunnel crypto")
2016-01-29 07:22:31 -05:00
t = new(Tunnel)
t.layerKey, err = aes.NewCipher(layerKey[:])
if err == nil {
t.ivKey, err = aes.NewCipher(ivKey[:])
}
if err != nil {
// error happened we don't need t
2024-10-18 21:58:50 -04:00
//log.WithError(err).Error("Failed to create Tunnel crypto")
2016-01-29 07:22:31 -05:00
t = nil
2024-10-18 21:58:50 -04:00
} else {
log.Debug("Tunnel crypto created successfully")
2016-01-29 07:22:31 -05:00
}
return
}
// encrypt tunnel data in place
func (t *Tunnel) Encrypt(td *TunnelData) {
2024-10-18 21:58:50 -04:00
log.Debug("Encrypting Tunnel data")
2016-01-29 07:22:31 -05:00
data := *td
t.ivKey.Encrypt(data[16:1024], data[16:1024])
layerBlock := cipher.NewCBCEncrypter(t.layerKey, data[:16])
layerBlock.CryptBlocks(data[16:1024], data[16:1024])
t.ivKey.Encrypt(data[16:1024], data[16:1024])
2024-10-18 21:58:50 -04:00
log.Debug("Tunnel data encrypted successfully")
}
func (t *Tunnel) Decrypt(td *TunnelData) {
2024-10-18 21:58:50 -04:00
log.Debug("Decrypting Tunnel data")
2016-01-29 07:22:31 -05:00
data := *td
t.ivKey.Decrypt(data[16:1024], data[16:1024])
layerBlock := cipher.NewCBCDecrypter(t.layerKey, data[:16])
layerBlock.CryptBlocks(data[16:1024], data[16:1024])
t.ivKey.Decrypt(data[16:1024], data[16:1024])
2024-10-18 21:58:50 -04:00
log.Debug("Tunnel data decrypted successfully")
}