Skip to content

Commit

Permalink
Showing 9 changed files with 45 additions and 303 deletions.
228 changes: 0 additions & 228 deletions p2p/security/noise/c.out

This file was deleted.

16 changes: 8 additions & 8 deletions p2p/security/noise/crypto.go
Original file line number Diff line number Diff line change
@@ -10,18 +10,18 @@ func (s *secureSession) Encrypt(plaintext []byte) (ciphertext []byte, err error)
if s.xx_complete {
if s.initiator {
cs := s.xx_ns.CS1()
cs, ciphertext = xx.EncryptWithAd(cs, nil, plaintext)
_, ciphertext = xx.EncryptWithAd(cs, nil, plaintext)
} else {
cs := s.xx_ns.CS2()
cs, ciphertext = xx.EncryptWithAd(cs, nil, plaintext)
_, ciphertext = xx.EncryptWithAd(cs, nil, plaintext)
}
} else if s.ik_complete {
if s.initiator {
cs := s.ik_ns.CS1()
cs, ciphertext = ik.EncryptWithAd(cs, nil, plaintext)
_, ciphertext = ik.EncryptWithAd(cs, nil, plaintext)
} else {
cs := s.ik_ns.CS2()
cs, ciphertext = ik.EncryptWithAd(cs, nil, plaintext)
_, ciphertext = ik.EncryptWithAd(cs, nil, plaintext)
}
} else {
return nil, errors.New("encrypt err: haven't completed handshake")
@@ -35,18 +35,18 @@ func (s *secureSession) Decrypt(ciphertext []byte) (plaintext []byte, err error)
if s.xx_complete {
if s.initiator {
cs := s.xx_ns.CS2()
cs, plaintext, ok = xx.DecryptWithAd(cs, nil, ciphertext)
_, plaintext, ok = xx.DecryptWithAd(cs, nil, ciphertext)
} else {
cs := s.xx_ns.CS1()
cs, plaintext, ok = xx.DecryptWithAd(cs, nil, ciphertext)
_, plaintext, ok = xx.DecryptWithAd(cs, nil, ciphertext)
}
} else if s.ik_complete {
if s.initiator {
cs := s.ik_ns.CS2()
cs, plaintext, ok = ik.DecryptWithAd(cs, nil, ciphertext)
_, plaintext, ok = ik.DecryptWithAd(cs, nil, ciphertext)
} else {
cs := s.ik_ns.CS1()
cs, plaintext, ok = ik.DecryptWithAd(cs, nil, ciphertext)
_, plaintext, ok = ik.DecryptWithAd(cs, nil, ciphertext)
}
} else {
return nil, errors.New("decrypt err: haven't completed handshake")
13 changes: 13 additions & 0 deletions p2p/security/noise/crypto_test.go
Original file line number Diff line number Diff line change
@@ -27,6 +27,19 @@ func TestEncryptAndDecrypt_InitToResp(t *testing.T) {
} else if err != nil {
t.Fatal(err)
}

plaintext = []byte("goodbye")
ciphertext, err = initConn.Encrypt(plaintext)
if err != nil {
t.Fatal(err)
}

result, err = respConn.Decrypt(ciphertext)
if !bytes.Equal(plaintext, result) {
t.Fatalf("got %x expected %x", result, plaintext)
} else if err != nil {
t.Fatal(err)
}
}

func TestEncryptAndDecrypt_RespToInit(t *testing.T) {
15 changes: 1 addition & 14 deletions p2p/security/noise/ik/IK.noise.go
Original file line number Diff line number Diff line change
@@ -170,9 +170,6 @@ func (mb *MessageBuffer) Encode1() []byte {
enc = append(enc, mb.ne[:]...)
enc = append(enc, mb.ciphertext...)

// log.Debug("XX_Encode1", "ne", mb.ne)
// log.Debug("XX_Encode1", "ns", mb.ns)

return enc
}

@@ -182,31 +179,23 @@ func Decode0(in []byte) (*MessageBuffer, error) {
return nil, errors.New("cannot decode stage 0 MessageBuffer: length less than 32 bytes")
}

//log.Debug("XX_Decode0", "in", in)
mb := new(MessageBuffer)
copy(mb.ne[:], in[:32])
mb.ns = in[32:80]
mb.ciphertext = in[80:]
//log.Debug("XX_Decode0", "mb", mb)

return mb, nil
}

// Decodes messages at stage 1 into MessageBuffer
func Decode1(in []byte) (*MessageBuffer, error) {
if len(in) < 80 {
return nil, errors.New("cannot decode stage 1/2 MessageBuffer: length less than 96 bytes")
return nil, errors.New("cannot decode stage 1 MessageBuffer: length less than 96 bytes")
}

// log.Debug("XX_Decode1", "in", in)
// log.Debug("XX_Decode1", "ns", in[32:80])

mb := new(MessageBuffer)
copy(mb.ne[:], in[:32])
//mb.ns = in[32:80]
mb.ciphertext = in[32:]
// copy(mb.ns,)
// copy(mb.ciphertext,)

return mb, nil
}
@@ -523,7 +512,6 @@ func SendMessage(session *NoiseSession, message []byte) (*NoiseSession, MessageB
}
if session.mc == 1 {
session.h, messageBuffer, session.cs1, session.cs2 = writeMessageB(&session.hs, message)
//session.hs = handshakestate{}
}
session.mc = session.mc + 1
return session, messageBuffer
@@ -537,7 +525,6 @@ func RecvMessage(session *NoiseSession, message *MessageBuffer) (*NoiseSession,
}
if session.mc == 1 {
session.h, plaintext, valid, session.cs1, session.cs2 = readMessageB(&session.hs, message)
//session.hs = handshakestate{}
}
session.mc = session.mc + 1
return session, plaintext, valid
Loading

0 comments on commit 45182a4

Please sign in to comment.