Archived
1
0
This commit is contained in:
Jeff Becker 2017-08-25 10:43:29 -04:00
parent cc5d94ee5f
commit d61228215e
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05
8 changed files with 528 additions and 538 deletions

View File

@ -26,7 +26,7 @@ func CryptoBox(msg, nounce, pk, sk []byte) ([]byte, error) {
return nil, err
}
if len(nounce) != int(C.crypto_box_macbytes()) {
err := errors.New ("len(nounce) != crypto_box_macbytes()")
err := errors.New("len(nounce) != crypto_box_macbytes()")
return nil, err
}

View File

@ -17,10 +17,9 @@ import (
// wrapper arround malloc/free
type Buffer struct {
ptr unsafe.Pointer;
length C.int;
size C.size_t;
ptr unsafe.Pointer
length C.int
size C.size_t
}
// wrapper arround nacl.malloc
@ -35,7 +34,7 @@ func Malloc(size int) *Buffer {
func malloc(size C.size_t) *Buffer {
ptr := C.malloc(size)
C.sodium_memzero(ptr, size)
buffer := &Buffer{ptr: ptr, size: size , length: C.int(size)}
buffer := &Buffer{ptr: ptr, size: size, length: C.int(size)}
return buffer
}

View File

@ -44,7 +44,7 @@ func GenSignKeypair() *KeyPair {
pk := malloc(pk_len)
res := C.crypto_sign_keypair(pk.uchar(), sk.uchar())
if res == 0 {
return &KeyPair{pk,sk}
return &KeyPair{pk, sk}
}
pk.Free()
sk.Free()
@ -78,7 +78,7 @@ func GetSignPubkey(sk []byte) ([]byte, error) {
func LoadSignKey(seed []byte) *KeyPair {
seed_len := C.crypto_sign_seedbytes()
if C.size_t(len(seed)) != seed_len {
return nil
panic(fmt.Sprintf("Bad seed length %d bytes", len(seed)))
}
seedbuff := NewBuffer(seed)
defer seedbuff.Free()
@ -102,14 +102,13 @@ func GenBoxKeypair() *KeyPair {
pk := malloc(pk_len)
res := C.crypto_box_keypair(pk.uchar(), sk.uchar())
if res == 0 {
return &KeyPair{pk,sk}
return &KeyPair{pk, sk}
}
pk.Free()
sk.Free()
return nil
}
// get public key from secret key
func GetBoxPubkey(sk []byte) []byte {
sk_len := C.crypto_box_seedbytes()
@ -170,7 +169,6 @@ func CryptoSignPublicLen() int {
return int(C.crypto_sign_publickeybytes())
}
func CryptoSignSecretLen() int {
return int(C.crypto_sign_secretkeybytes())
}

View File

@ -35,7 +35,6 @@ func CryptoSignPrivKeySize() int {
return int(C.crypto_sign_secretkeybytes())
}
// initialize sodium
func init() {
status := C.sodium_init()

View File

@ -6,7 +6,6 @@ package nacl
// #include <sodium.h>
import "C"
// sign data detached with secret key sk
func CryptoSignDetached(msg, sk []byte) []byte {
msgbuff := NewBuffer(msg)
@ -31,7 +30,6 @@ func CryptoSignDetached(msg, sk []byte) []byte {
return nil
}
// sign data with secret key sk
// return detached sig
// this uses crypto_sign instead pf crypto_sign_detached
@ -45,7 +43,7 @@ func CryptoSignFucky(msg, sk []byte) []byte {
}
// allocate the signed message buffer
sig := malloc(C.crypto_sign_bytes()+msgbuff.size)
sig := malloc(C.crypto_sign_bytes() + msgbuff.size)
defer sig.Free()
// compute signature
siglen := C.ulonglong(0)

View File

@ -10,7 +10,6 @@ import (
// TOY encrypted authenticated stream protocol like tls
var BadHandshake = errors.New("Bad handshake")
var ShortWrite = errors.New("short write")
var ShortRead = errors.New("short read")
@ -47,8 +46,8 @@ func (cs *CryptoStream) Close() (err error) {
func (cs *CryptoStream) Write(data []byte) (n int, err error) {
// let's split it up
for n < len(data) && err == nil {
if n + cs.mtu < len(data) {
err = cs.writeSegment(data[n:n+cs.mtu])
if n+cs.mtu < len(data) {
err = cs.writeSegment(data[n : n+cs.mtu])
n += cs.mtu
} else {
err = cs.writeSegment(data[n:])
@ -75,7 +74,7 @@ func (cs *CryptoStream) readSegment() (s []byte, err error) {
var stream_read int
var seg []byte
nl := NounceLen()
msg := make([]byte, cs.mtu + nl)
msg := make([]byte, cs.mtu+nl)
stream_read, err = cs.stream.Read(msg)
seg, err = CryptoBoxOpen(msg[:stream_read], cs.rx_nonce, cs.secret(), cs.remote_pk)
if err == nil {
@ -90,7 +89,7 @@ func (cs *CryptoStream) readSegment() (s []byte, err error) {
func (cs *CryptoStream) writeSegment(data []byte) (err error) {
var segment []byte
nl := NounceLen()
msg := make([]byte, len(data) + nl)
msg := make([]byte, len(data)+nl)
// generate next nounce
nextNounce := NewBoxNounce()
copy(msg, nextNounce)
@ -138,7 +137,7 @@ func verifyHandshake(hs, pk []byte) (valid bool) {
nl := NounceLen()
if bytes.Equal(pk, hs[ml:ml+pl]) {
// check signature
msg := hs[0:ml+pl+nl]
msg := hs[0 : ml+pl+nl]
sig := hs[ml+pl+nl:]
valid = CryptoVerifyFucky(msg, sig, pk)
}
@ -150,7 +149,7 @@ func verifyHandshake(hs, pk []byte) (valid bool) {
func getPubkey(hs []byte) (pk []byte) {
ml := len(protocol_magic)
pl := CryptoSignPublicLen()
pk = hs[ml:ml+pl]
pk = hs[ml : ml+pl]
return
}
@ -176,7 +175,7 @@ func getNounce(hs []byte) (n []byte) {
ml := len(protocol_magic)
pl := CryptoSignPublicLen()
nl := NounceLen()
n = hs[ml+pl:ml+pl+nl]
n = hs[ml+pl : ml+pl+nl]
return
}
@ -201,7 +200,7 @@ func (cs *CryptoStream) Handshake() (err error) {
copy(cs.remote_pk, pk)
}
if ! verifyHandshake(buff, cs.remote_pk) {
if !verifyHandshake(buff, cs.remote_pk) {
// verification failed
err = BadHandshake
return
@ -211,7 +210,6 @@ func (cs *CryptoStream) Handshake() (err error) {
return
}
// create a client
func Client(stream io.ReadWriteCloser, local_sk, remote_pk []byte) (c *CryptoStream) {
c = &CryptoStream{
@ -227,7 +225,6 @@ func Client(stream io.ReadWriteCloser, local_sk, remote_pk []byte) (c *CryptoStr
return c
}
type CryptoConn struct {
stream *CryptoStream
conn net.Conn
@ -297,8 +294,8 @@ func (cl *CryptoListener) acceptInbound() {
func (cl *CryptoListener) runChans() {
for {
select {
case c := <- cl.handshake:
go func(){
case c := <-cl.handshake:
go func() {
s := &CryptoStream{
stream: c,
mtu: DefaultMTU,
@ -323,8 +320,8 @@ func (cl *CryptoListener) runChans() {
// accept inbound authenticated and trusted connections
func (cl *CryptoListener) Accept() (c net.Conn, err error) {
var ok bool
c, ok = <- cl.accepted
if ! ok {
c, ok = <-cl.accepted
if !ok {
err = Closed
}
return

View File

@ -6,7 +6,6 @@ package nacl
// #include <sodium.h>
import "C"
// verify a fucky detached sig
func CryptoVerifyFucky(msg, sig, pk []byte) bool {
var smsg []byte