pax_global_header00006660000000000000000000000064147265254250014526gustar00rootroot0000000000000052 comment=3afc4917ac41a3ce8ddd285ce4aca6b3c668b0c8 xsecretbox-1.0.5/000077500000000000000000000000001472652542500137175ustar00rootroot00000000000000xsecretbox-1.0.5/.gitignore000066400000000000000000000004231472652542500157060ustar00rootroot00000000000000# 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.5/LICENSE000066400000000000000000000020611472652542500147230ustar00rootroot00000000000000MIT License Copyright (c) 2018-2023 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.5/README.md000066400000000000000000000002201472652542500151700ustar00rootroot00000000000000# xsecretbox Go implementation of crypto_secretbox_xchacha20poly1305 Uses [aead/chacha20poly1305](https://github.com/aead/chacha20poly1305). xsecretbox-1.0.5/go.mod000066400000000000000000000002041472652542500150210ustar00rootroot00000000000000module github.com/jedisct1/xsecretbox go 1.23.4 require golang.org/x/crypto v0.31.0 require golang.org/x/sys v0.28.0 // indirect xsecretbox-1.0.5/go.sum000066400000000000000000000004701472652542500150530ustar00rootroot00000000000000golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= xsecretbox-1.0.5/sharedkey.go000066400000000000000000000012161472652542500162250ustar00rootroot00000000000000package 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.5/xsecretbox.go000066400000000000000000000051441472652542500164400ustar00rootroot00000000000000package 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.5/xsecretbox_test.go000066400000000000000000000031321472652542500174720ustar00rootroot00000000000000package 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) } }