Exemplo n.º 1
0
func (s *cipherTestSuite) TestMessage() {
	var (
		assert = s.Assertions
		c      = s.cipher
	)

	var (
		ka  cipherset.Key
		kb  cipherset.Key
		sa  cipherset.State
		box []byte
		msg []byte
		err error
	)

	ka, err = c.GenerateKey()
	assert.NoError(err)
	assert.NotNil(ka)

	sa, err = c.NewState(ka)
	assert.NoError(err)
	assert.NotNil(sa)
	assert.False(sa.CanEncryptMessage())
	assert.False(sa.CanEncryptHandshake())
	assert.False(sa.CanDecryptMessage())
	assert.True(sa.CanDecryptHandshake())
	assert.True(sa.NeedsRemoteKey())

	kb, err = c.GenerateKey()
	assert.NoError(err)
	assert.NotNil(kb)

	err = sa.SetRemoteKey(kb)
	assert.NoError(err)
	assert.True(sa.CanEncryptMessage())
	assert.True(sa.CanEncryptHandshake())
	assert.True(sa.CanDecryptMessage())
	assert.True(sa.CanDecryptHandshake())
	assert.False(sa.NeedsRemoteKey())

	box, err = sa.EncryptMessage([]byte("Hello World!"))
	assert.NoError(err)
	assert.NotNil(box)

	msg, err = c.DecryptMessage(kb, ka, box)
	assert.NoError(err)
	assert.NotNil(msg)
	assert.Equal([]byte("Hello World!"), msg)
}
Exemplo n.º 2
0
func (s *cipherTestSuite) TestHandshake() {
	var (
		assert = s.Assertions
		c      = s.cipher
	)

	var (
		ka  cipherset.Key
		kb  cipherset.Key
		sa  cipherset.State
		sb  cipherset.State
		ha  cipherset.Handshake
		hb  cipherset.Handshake
		box []byte
		err error
		ok  bool
	)

	ka, err = c.GenerateKey()
	assert.NoError(err)
	assert.NotNil(ka)

	sa, err = c.NewState(ka)
	assert.NoError(err)
	assert.NotNil(sa)
	assert.False(sa.CanEncryptMessage())
	assert.False(sa.CanEncryptHandshake())
	assert.False(sa.CanDecryptMessage())
	assert.True(sa.CanDecryptHandshake())
	assert.True(sa.NeedsRemoteKey())

	kb, err = c.GenerateKey()
	assert.NoError(err)
	assert.NotNil(kb)

	err = sa.SetRemoteKey(kb)
	assert.NoError(err)
	assert.True(sa.CanEncryptMessage())
	assert.True(sa.CanEncryptHandshake())
	assert.True(sa.CanDecryptMessage())
	assert.True(sa.CanDecryptHandshake())
	assert.False(sa.NeedsRemoteKey())

	box, err = sa.EncryptHandshake(1, cipherset.Parts{0x01: "foobarzzzzfoobarzzzzfoobarzzzzfoobarzzzzfoobarzzzz34"})
	assert.NoError(err)
	assert.NotNil(box)

	hb, err = c.DecryptHandshake(kb, box)
	assert.NoError(err)
	if assert.NotNil(hb) {
		assert.Equal(ka.Public(), hb.PublicKey().Public())
		assert.Equal(cipherset.Parts{0x01: "foobarzzzzfoobarzzzzfoobarzzzzfoobarzzzzfoobarzzzz34"}, hb.Parts())
		assert.Equal(uint32(1), hb.At())
	}

	sb, err = c.NewState(kb)
	assert.NoError(err)
	if assert.NotNil(sb) {
		assert.False(sb.CanEncryptMessage())
		assert.False(sb.CanEncryptHandshake())
		assert.False(sb.CanDecryptMessage())
		assert.True(sb.CanDecryptHandshake())
		assert.True(sb.NeedsRemoteKey())
	}

	if sb != nil && hb != nil {
		ok = sb.ApplyHandshake(hb)
		assert.True(ok)
		assert.True(sb.CanEncryptMessage())
		assert.True(sb.CanEncryptHandshake())
		assert.True(sb.CanDecryptMessage())
		assert.True(sb.CanDecryptHandshake())
		assert.False(sb.NeedsRemoteKey())
	}

	box, err = sb.EncryptHandshake(1, cipherset.Parts{0x01: "foobarzzzzfoobarzzzzfoobarzzzzfoobarzzzzfoobarzzzz34"})
	assert.NoError(err)
	assert.NotNil(box)

	ha, err = c.DecryptHandshake(ka, box)
	assert.NoError(err)
	assert.NotNil(ha)
	assert.Equal(kb.Public(), ha.PublicKey().Public())
	assert.Equal(cipherset.Parts{0x01: "foobarzzzzfoobarzzzzfoobarzzzzfoobarzzzzfoobarzzzz34"}, ha.Parts())
	assert.Equal(uint32(1), ha.At())

	ok = sa.ApplyHandshake(ha)
	assert.True(ok)
	assert.True(sa.CanEncryptMessage())
	assert.True(sa.CanEncryptHandshake())
	assert.True(sa.CanDecryptMessage())
	assert.True(sa.CanDecryptHandshake())
	assert.False(sa.NeedsRemoteKey())
}