grumble, pkg/cryptstate: make it possible to use CryptState as a non-pointer member.

This commit is contained in:
Mikkel Krautz 2012-12-02 12:59:30 +01:00
parent 4c4c4a1174
commit d6b71c5553
6 changed files with 27 additions and 32 deletions

View file

@ -41,7 +41,7 @@ type Client struct {
disconnected bool
lastResync int64
crypt *cryptstate.CryptState
crypt cryptstate.CryptState
codecs []int32
opus bool
udp bool

View file

@ -1,5 +1,4 @@
// Grumble - an implementation of Murmur in Go
// Copyright (c) 2010 The Grumble Authors
// Copyright (c) 2010-2012 The Grumble Authors
// The use of this source code is goverened by a BSD-style
// license that can be found in the LICENSE-file.
@ -10,6 +9,7 @@ import (
"crypto/cipher"
"crypto/rand"
"errors"
"io"
"mumbleapp.com/grumble/pkg/cryptstate/ocb2"
"time"
)
@ -36,23 +36,28 @@ type CryptState struct {
cipher cipher.Block
}
func New() (cs *CryptState, err error) {
cs = new(CryptState)
func (cs *CryptState) GenerateKey() error {
_, err := io.ReadFull(rand.Reader, cs.RawKey[0:])
if err != nil {
return err
}
return
}
_, err = io.ReadFull(rand.Reader, cs.EncryptIV[0:])
if err != nil {
return err
}
func (cs *CryptState) GenerateKey() (err error) {
rand.Read(cs.RawKey[0:])
rand.Read(cs.EncryptIV[0:])
rand.Read(cs.DecryptIV[0:])
_, err = io.ReadFull(rand.Reader, cs.DecryptIV[0:])
if err != nil {
return err
}
cs.cipher, err = aes.NewCipher(cs.RawKey[0:])
if err != nil {
return
return err
}
return
return nil
}
func (cs *CryptState) SetKey(key []byte, eiv []byte, div []byte) (err error) {

View file

@ -1,3 +1,7 @@
// Copyright (c) 2010-2012 The Grumble Authors
// The use of this source code is goverened by a BSD-style
// license that can be found in the LICENSE-file.
package cryptstate
import (
@ -26,11 +30,7 @@ func TestEncrypt(t *testing.T) {
0x1f, 0x2a, 0x9b, 0xd0, 0x2d, 0xa6, 0x8e, 0x46, 0x26, 0x85, 0x83, 0xe9, 0x14, 0x2a, 0xff, 0x2a,
}
cs, err := New()
if err != nil {
t.Errorf("%v", err)
}
cs := CryptState{}
out := make([]byte, 19)
cs.SetKey(key[0:], eiv[0:], div[0:])
cs.Encrypt(out[0:], msg[0:])
@ -64,11 +64,7 @@ func TestDecrypt(t *testing.T) {
0x1f, 0x2a, 0x9b, 0xd0, 0x2d, 0xa6, 0x8e, 0x46, 0x26, 0x85, 0x83, 0xe9, 0x14, 0x2a, 0xff, 0x2a,
}
cs, err := New()
if err != nil {
t.Errorf("%v", err)
}
cs := CryptState{}
out := make([]byte, 15)
cs.SetKey(key[0:], div[0:], eiv[0:])
cs.Decrypt(out[0:], crypted[0:])

View file

@ -1,4 +1,4 @@
// Copyright (c) 2012 The Grumble Authors
// Copyright (c) 2010-2012 The Grumble Authors
// The use of this source code is goverened by a BSD-style
// license that can be found in the LICENSE-file.

View file

@ -1,6 +1,6 @@
// Copyright (c) 2012 The Grumble Authors
// Copyright (c) 2010-2012 The Grumble Authors
// The use of this source code is goverened by a BSD-style
// license that can be found in the LICENSE-file.
// license that can be found in the LICENSE-file.git l
package ocb2

View file

@ -19,7 +19,6 @@ import (
"log"
"mumbleapp.com/grumble/pkg/ban"
"mumbleapp.com/grumble/pkg/blobstore"
"mumbleapp.com/grumble/pkg/cryptstate"
"mumbleapp.com/grumble/pkg/freezer"
"mumbleapp.com/grumble/pkg/htmlfilter"
"mumbleapp.com/grumble/pkg/logtarget"
@ -505,11 +504,6 @@ func (server *Server) handleAuthenticate(client *Client, msg *Message) {
}
// Setup the cryptstate for the client.
client.crypt, err = cryptstate.New()
if err != nil {
client.Panicf("%v", err)
return
}
err = client.crypt.GenerateKey()
if err != nil {
client.Panicf("%v", err)