session should same encryption on every req
@@ -47,8 +47,25 @@ type NetworkSessionImpl struct {
localNodeID string
remoteNodeID string
- blockEncrypter cipher.BlockMode
- blockDecrypter cipher.BlockMode
+ blockEncrypter cbcMode
+ blockDecrypter cbcMode
+}
+
+// cbcMode is an interface for block ciphers using cipher block chaining.
+// we use it to expose SetIV
+type cbcMode interface {
+ cipher.BlockMode
+ SetIV([]byte)
+}
+
+// resetIV sets the iv to the session ID. it is called after encrpyt decrypt.
+func (n *NetworkSessionImpl) resetIV() {
+ n.blockEncrypter.SetIV(n.id)
+ n.blockDecrypter.SetIV(n.id)
+}
+
+func(n *NetworkSessionImpl) isAuth(node LocalNode) {
+
}
//LocalNodeID returns the session's local node id.
@@ -110,6 +127,7 @@ func (n *NetworkSessionImpl) Encrypt(in []byte) ([]byte, error) {
paddedIn := crypto.AddPKCSPadding(in)
out := make([]byte, len(paddedIn))
n.blockEncrypter.CryptBlocks(out, paddedIn)
+ n.resetIV()
return out, nil
}
@@ -125,13 +143,13 @@ func (n *NetworkSessionImpl) Decrypt(in []byte) ([]byte, error) {
if err != nil {
return nil, err
}
-
+ n.resetIV()
return clearText, nil
}
// NewNetworkSession creates a new network session based on provided data
func NewNetworkSession(id, keyE, keyM, pubKey []byte, localNodeID, remoteNodeID string) (NetworkSession, error) {
- s := &NetworkSessionImpl{
+ n := &NetworkSessionImpl{
id: id,
keyE: keyE,
keyM: keyM,
@@ -149,8 +167,8 @@ func NewNetworkSession(id, keyE, keyM, pubKey []byte, localNodeID, remoteNodeID
return nil, err
}
- s.blockEncrypter = cipher.NewCBCEncrypter(blockCipher, s.id)
- s.blockDecrypter = cipher.NewCBCDecrypter(blockCipher, s.id)
+ n.blockEncrypter = cipher.NewCBCEncrypter(blockCipher, n.id).(cbcMode)
+ n.blockDecrypter = cipher.NewCBCDecrypter(blockCipher, n.id).(cbcMode)
- return s, nil
+ return n, nil
}
