forked from External/grumble
grumble, pkg/cryptstate: make it possible to use CryptState as a non-pointer member.
This commit is contained in:
parent
4c4c4a1174
commit
d6b71c5553
6 changed files with 27 additions and 32 deletions
|
|
@ -41,7 +41,7 @@ type Client struct {
|
||||||
disconnected bool
|
disconnected bool
|
||||||
|
|
||||||
lastResync int64
|
lastResync int64
|
||||||
crypt *cryptstate.CryptState
|
crypt cryptstate.CryptState
|
||||||
codecs []int32
|
codecs []int32
|
||||||
opus bool
|
opus bool
|
||||||
udp bool
|
udp bool
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
// Grumble - an implementation of Murmur in Go
|
// Copyright (c) 2010-2012 The Grumble Authors
|
||||||
// Copyright (c) 2010 The Grumble Authors
|
|
||||||
// The use of this source code is goverened by a BSD-style
|
// 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.
|
||||||
|
|
||||||
|
|
@ -10,6 +9,7 @@ import (
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"errors"
|
"errors"
|
||||||
|
"io"
|
||||||
"mumbleapp.com/grumble/pkg/cryptstate/ocb2"
|
"mumbleapp.com/grumble/pkg/cryptstate/ocb2"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
@ -36,23 +36,28 @@ type CryptState struct {
|
||||||
cipher cipher.Block
|
cipher cipher.Block
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() (cs *CryptState, err error) {
|
func (cs *CryptState) GenerateKey() error {
|
||||||
cs = new(CryptState)
|
_, 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) {
|
_, err = io.ReadFull(rand.Reader, cs.DecryptIV[0:])
|
||||||
rand.Read(cs.RawKey[0:])
|
if err != nil {
|
||||||
rand.Read(cs.EncryptIV[0:])
|
return err
|
||||||
rand.Read(cs.DecryptIV[0:])
|
}
|
||||||
|
|
||||||
cs.cipher, err = aes.NewCipher(cs.RawKey[0:])
|
cs.cipher, err = aes.NewCipher(cs.RawKey[0:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *CryptState) SetKey(key []byte, eiv []byte, div []byte) (err error) {
|
func (cs *CryptState) SetKey(key []byte, eiv []byte, div []byte) (err error) {
|
||||||
|
|
|
||||||
|
|
@ -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
|
package cryptstate
|
||||||
|
|
||||||
import (
|
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,
|
0x1f, 0x2a, 0x9b, 0xd0, 0x2d, 0xa6, 0x8e, 0x46, 0x26, 0x85, 0x83, 0xe9, 0x14, 0x2a, 0xff, 0x2a,
|
||||||
}
|
}
|
||||||
|
|
||||||
cs, err := New()
|
cs := CryptState{}
|
||||||
if err != nil {
|
|
||||||
t.Errorf("%v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
out := make([]byte, 19)
|
out := make([]byte, 19)
|
||||||
cs.SetKey(key[0:], eiv[0:], div[0:])
|
cs.SetKey(key[0:], eiv[0:], div[0:])
|
||||||
cs.Encrypt(out[0:], msg[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,
|
0x1f, 0x2a, 0x9b, 0xd0, 0x2d, 0xa6, 0x8e, 0x46, 0x26, 0x85, 0x83, 0xe9, 0x14, 0x2a, 0xff, 0x2a,
|
||||||
}
|
}
|
||||||
|
|
||||||
cs, err := New()
|
cs := CryptState{}
|
||||||
if err != nil {
|
|
||||||
t.Errorf("%v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
out := make([]byte, 15)
|
out := make([]byte, 15)
|
||||||
cs.SetKey(key[0:], div[0:], eiv[0:])
|
cs.SetKey(key[0:], div[0:], eiv[0:])
|
||||||
cs.Decrypt(out[0:], crypted[0:])
|
cs.Decrypt(out[0:], crypted[0:])
|
||||||
|
|
|
||||||
|
|
@ -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
|
// 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.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
// 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
|
package ocb2
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,6 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"mumbleapp.com/grumble/pkg/ban"
|
"mumbleapp.com/grumble/pkg/ban"
|
||||||
"mumbleapp.com/grumble/pkg/blobstore"
|
"mumbleapp.com/grumble/pkg/blobstore"
|
||||||
"mumbleapp.com/grumble/pkg/cryptstate"
|
|
||||||
"mumbleapp.com/grumble/pkg/freezer"
|
"mumbleapp.com/grumble/pkg/freezer"
|
||||||
"mumbleapp.com/grumble/pkg/htmlfilter"
|
"mumbleapp.com/grumble/pkg/htmlfilter"
|
||||||
"mumbleapp.com/grumble/pkg/logtarget"
|
"mumbleapp.com/grumble/pkg/logtarget"
|
||||||
|
|
@ -505,11 +504,6 @@ func (server *Server) handleAuthenticate(client *Client, msg *Message) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup the cryptstate for the client.
|
// Setup the cryptstate for the client.
|
||||||
client.crypt, err = cryptstate.New()
|
|
||||||
if err != nil {
|
|
||||||
client.Panicf("%v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = client.crypt.GenerateKey()
|
err = client.crypt.GenerateKey()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
client.Panicf("%v", err)
|
client.Panicf("%v", err)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue