Chlorure/sodium/secret_stream.go

64 lines
1.5 KiB
Go
Raw Permalink Normal View History

2020-06-01 13:42:45 +00:00
package sodium
2020-06-01 12:20:34 +00:00
/*
#cgo CFLAGS: -g -Wall
#cgo LDFLAGS: -lsodium
#include <sodium.h>
*/
import "C"
2020-06-01 13:42:45 +00:00
import "unsafe"
2020-06-01 12:20:34 +00:00
2020-06-01 13:42:45 +00:00
const KeyLen int = C.crypto_secretstream_xchacha20poly1305_KEYBYTES
const BlockSize int = 16 * 1024 // 16 KiB
2020-06-01 12:20:34 +00:00
2020-06-01 13:42:45 +00:00
func GenerateKey() [KeyLen]byte {
var key [KeyLen]byte
keywrap := (*C.uchar)(unsafe.Pointer(&key[0]))
C.crypto_secretstream_xchacha20poly1305_keygen(keywrap)
return key
}
/*
// Maybe use io.Pipe() for io.Writer
2020-06-01 12:20:34 +00:00
2020-06-01 13:42:45 +00:00
type SecretStreamEncode struct {
src io.Reader
state C.crypto_secretstream_xchacha20poly1305_state
}
2020-06-01 12:20:34 +00:00
2020-06-01 13:42:45 +00:00
type SecretStreamDecode struct {
src io.Reader
state C.crypto_secretstream_xchacha20poly1305_state
}
func NewSecretStreamEncode(key []byte, src io.Reader) *SecretStream {
// it's ok to call init multiple times according to sodium doc
2020-06-01 12:20:34 +00:00
ret := C.sodium_init()
2020-06-01 13:42:45 +00:00
if ret < 0 { log.Panic("Failed to init sodium.") }
s := { src: src}
2020-06-01 12:20:34 +00:00
var header [C.crypto_secretstream_xchacha20poly1305_HEADERBYTES]C.uchar
2020-06-01 13:42:45 +00:00
C.crypto_secretstream_xchacha20poly1305_init_push(&s.state, &header[0], &key[0])
// @FIXME do something with header
return &s
}
// Encode
func (s *SecretStream) Read(p []byte) (n int, err error) {
s/
}
*/
/*
func main() {
2020-06-01 12:20:34 +00:00
log.Print("key", key)
log.Print("header", header)
var plain [block_size]C.uchar
var c1 [block_size + C.crypto_secretstream_xchacha20poly1305_ABYTES]C.uchar
C.crypto_secretstream_xchacha20poly1305_push(&state, &c1[0], nil, &plain[0], C.ulonglong(len(plain)), nil, 0, 0)
log.Print("c1", c1)
}
2020-06-01 13:42:45 +00:00
*/