pax_global_header00006660000000000000000000000064140306027020014504gustar00rootroot0000000000000052 comment=7cb86b57caf0ffef316f49c341f62f2dc2c97876 xsecretbox-1.0.2/000077500000000000000000000000001403060270200136725ustar00rootroot00000000000000xsecretbox-1.0.2/.gitignore000066400000000000000000000004231403060270200156610ustar00rootroot00000000000000# Binaries for programs and plugins *.exe *.dll *.so *.dylib # Test binary, build with `go test -c` *.test # Output of the go coverage tool, specifically when used with LiteIDE *.out # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 .glide/ xsecretbox-1.0.2/LICENSE000066400000000000000000000020611403060270200146760ustar00rootroot00000000000000MIT License Copyright (c) 2018-2021 Frank Denis Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. xsecretbox-1.0.2/README.md000066400000000000000000000002201403060270200151430ustar00rootroot00000000000000# xsecretbox Go implementation of crypto_secretbox_xchacha20poly1305 Uses [aead/chacha20poly1305](https://github.com/aead/chacha20poly1305). xsecretbox-1.0.2/go.mod000066400000000000000000000001571403060270200150030ustar00rootroot00000000000000module github.com/jedisct1/xsecretbox go 1.16 require golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 xsecretbox-1.0.2/go.sum000066400000000000000000000014701403060270200150270ustar00rootroot00000000000000golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 h1:It14KIkyBFYkHkwZ7k45minvA9aorojkyjGk9KJ5B/w= golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= xsecretbox-1.0.2/sharedkey.go000066400000000000000000000012171403060270200162010ustar00rootroot00000000000000package xsecretbox import ( crypto_rand "crypto/rand" "golang.org/x/crypto/chacha20" "golang.org/x/crypto/curve25519" ) // SharedKey computes a shared secret compatible with the one used by `crypto_box_xchacha20poly1305`` func SharedKey(secretKey [32]byte, publicKey [32]byte) ([32]byte, error) { dhKey, err := curve25519.X25519(secretKey[:], publicKey[:]) var subKey []byte if err == nil { var nonce [16]byte subKey, err = chacha20.HChaCha20(dhKey[:], nonce[:]) } var key [32]byte if err != nil { if _, err2 := crypto_rand.Read(key[:]); err != nil { return key, err2 } return key, err } copy(key[:], subKey) return key, nil } xsecretbox-1.0.2/xsecretbox.go000066400000000000000000000051441403060270200164130ustar00rootroot00000000000000package xsecretbox import ( "crypto/subtle" "errors" "golang.org/x/crypto/chacha20" "golang.org/x/crypto/poly1305" ) const ( // KeySize is what the name suggests KeySize = 32 // NonceSize is what the name suggests NonceSize = 24 // TagSize is what the name suggests TagSize = 16 ) // Seal does what the name suggests func Seal(out, nonce, message, key []byte) []byte { if len(nonce) != NonceSize { panic("unsupported nonce size") } if len(key) != KeySize { panic("unsupported key size") } var firstBlock [64]byte cipher, _ := chacha20.NewUnauthenticatedCipher(key, nonce) cipher.XORKeyStream(firstBlock[:], firstBlock[:]) var polyKey [32]byte copy(polyKey[:], firstBlock[:32]) ret, out := sliceForAppend(out, TagSize+len(message)) firstMessageBlock := message if len(firstMessageBlock) > 32 { firstMessageBlock = firstMessageBlock[:32] } tagOut := out out = out[poly1305.TagSize:] for i, x := range firstMessageBlock { out[i] = firstBlock[32+i] ^ x } message = message[len(firstMessageBlock):] ciphertext := out out = out[len(firstMessageBlock):] cipher.SetCounter(1) cipher.XORKeyStream(out, message) var tag [TagSize]byte hash := poly1305.New(&polyKey) hash.Write(ciphertext) hash.Sum(tag[:0]) copy(tagOut, tag[:]) return ret } // Open does what the name suggests func Open(out, nonce, box, key []byte) ([]byte, error) { if len(nonce) != NonceSize { panic("unsupported nonce size") } if len(key) != KeySize { panic("unsupported key size") } if len(box) < TagSize { return nil, errors.New("ciphertext is too short") } var firstBlock [64]byte cipher, _ := chacha20.NewUnauthenticatedCipher(key, nonce) cipher.XORKeyStream(firstBlock[:], firstBlock[:]) var polyKey [32]byte copy(polyKey[:], firstBlock[:32]) var tag [TagSize]byte ciphertext := box[TagSize:] hash := poly1305.New(&polyKey) hash.Write(ciphertext) hash.Sum(tag[:0]) if subtle.ConstantTimeCompare(tag[:], box[:TagSize]) != 1 { return nil, errors.New("ciphertext authentication failed") } ret, out := sliceForAppend(out, len(ciphertext)) firstMessageBlock := ciphertext if len(firstMessageBlock) > 32 { firstMessageBlock = firstMessageBlock[:32] } for i, x := range firstMessageBlock { out[i] = firstBlock[32+i] ^ x } ciphertext = ciphertext[len(firstMessageBlock):] out = out[len(firstMessageBlock):] cipher.SetCounter(1) cipher.XORKeyStream(out, ciphertext) return ret, nil } func sliceForAppend(in []byte, n int) (head, tail []byte) { if total := len(in) + n; cap(in) >= total { head = in[:total] } else { head = make([]byte, total) copy(head, in) } tail = head[len(in):] return } xsecretbox-1.0.2/xsecretbox_test.go000066400000000000000000000031331403060270200174460ustar00rootroot00000000000000package xsecretbox import ( "bytes" "testing" ) func TestSecretbox(t *testing.T) { key := [32]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31} nonce := [24]byte{23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0} src := []byte{140, 141, 142, 143, 144, 145, 146, 147, 148, 149} expected := []byte{25, 114, 237, 248, 200, 53, 16, 193, 191, 225, 40, 120, 196, 190, 99, 230, 191, 167, 58, 195, 11, 125, 132, 23, 176, 194} dst := Seal(nil, nonce[:], src[:], key[:]) if !bytes.Equal(expected, dst) { t.Errorf("got %x instead of %x", expected, dst) } dec, err := Open(nil, nonce[:], dst[:], key[:]) if err != nil || !bytes.Equal(src, dec) { t.Errorf("got %x instead of %x", dec, src) } dst[0]++ _, err = Open(nil, nonce[:], dst[:], key[:]) if err == nil { t.Errorf("tag validation failed") } _, _ = SharedKey(key, key) } func TestSharedKey(t *testing.T) { pk := [32]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31} sk := [32]byte{10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41} expected := [32]byte{232, 47, 181, 56, 165, 130, 234, 207, 242, 65, 46, 117, 170, 172, 99, 173, 45, 8, 54, 163, 111, 2, 123, 52, 156, 119, 254, 132, 205, 210, 96, 217} shared, err := SharedKey(sk, pk) if err != nil { t.Errorf("got an error: %v", err) } if !bytes.Equal(expected[:], shared[:]) { t.Errorf("got %x instead of %x", expected, shared) } }