| 1 | // Copyright 2020 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package subtlescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/ |
| 16 | |
| 17 | import ( |
| 18 | "crypto/cipherscip-go gomod github.com/golang/go/src go1.24.0 `crypto/cipher`/" |
| 19 | "errorsscip-go gomod github.com/golang/go/src go1.24.0 errors/" |
| 20 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 21 | |
| 22 | "golang.org/x/crypto/chacha20poly1305scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/" |
| 23 | "github.com/tink-crypto/tink-go/v2/internal/randomscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/random`/" |
| 24 | "github.com/tink-crypto/tink-go/v2/tinkscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/" |
| 25 | ) |
| 26 | |
| 27 | // XChaCha20Poly1305 is an implementation of AEAD interface. |
| 28 | type XChaCha20Poly1305scip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305# struct { |
| 29 | cipherscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#cipher. cipherscip-go gomod github.com/golang/go/src go1.24.0 `crypto/cipher`/.AEADscip-go gomod github.com/golang/go/src go1.24.0 `crypto/cipher`/AEAD# |
| 30 | } |
| 31 | |
| 32 | // Assert that XChaCha20Poly1305 implements the AEAD interface. |
| 33 | var _ tinkscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/.AEADscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/AEAD# = (*XChaCha20Poly1305scip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#)(nil) |
| 34 | |
| 35 | // NewXChaCha20Poly1305 returns an XChaCha20Poly1305 instance. |
| 36 | // The key argument should be a 32-bytes key. |
| 37 | func NewXChaCha20Poly1305scip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/NewXChaCha20Poly1305().(key []byte) (*XChaCha20Poly1305scip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#, error) { |
| 38 | if len(key) != chacha20poly1305scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/.KeySizescip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/KeySize. { |
| 39 | return nil, errorsscip-go gomod github.com/golang/go/src go1.24.0 errors/.Newscip-go gomod github.com/golang/go/src go1.24.0 errors/New().("xchacha20poly1305: bad key length") |
| 40 | } |
| 41 | c, err := chacha20poly1305scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/.NewXscip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/NewX().(key) |
| 42 | if err != nil { |
| 43 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("xchacha20poly1305: failed to create AEAD, error: %v", err) |
| 44 | } |
| 45 | return &XChaCha20Poly1305scip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#{cipherscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#cipher.: c}, nil |
| 46 | } |
| 47 | |
| 48 | // Encrypt encrypts plaintext with associatedData. |
| 49 | // |
| 50 | // The resulting ciphertext consists of two parts: |
| 51 | // 1. the nonce used for encryption |
| 52 | // 2. the actual ciphertext |
| 53 | func (x *XChaCha20Poly1305scip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#) Encryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#Encrypt().(plaintext []byte, associatedData []byte) ([]byte, error) { |
| 54 | if len(plaintext) > maxIntscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/maxInt.-chacha20poly1305scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/.NonceSizeXscip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/NonceSizeX.-chacha20poly1305scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/.Overheadscip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/Overhead. { |
| 55 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("xchacha20poly1305: plaintext too long") |
| 56 | } |
| 57 | |
| 58 | // Make the capacity of dst large enough so that both the nonce and the ciphertext fit inside. |
| 59 | dst := make([]byte, chacha20poly1305scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/.NonceSizeXscip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/NonceSizeX., chacha20poly1305scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/.NonceSizeXscip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/NonceSizeX.+len(plaintext)+x.cipherscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#cipher..Overheadscip-go gomod github.com/golang/go/src go1.24.0 `crypto/cipher`/AEAD#Overhead().()) |
| 60 | nonce := dst[:chacha20poly1305scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/.NonceSizeXscip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/NonceSizeX.] |
| 61 | randomscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/random`/.MustRandscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/random`/MustRand().(nonce) |
| 62 | // Seal appends the ciphertext to dst. So the final output is: nonce || ciphertext. |
| 63 | return x.cipherscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#cipher..Sealscip-go gomod github.com/golang/go/src go1.24.0 `crypto/cipher`/AEAD#Seal().(dst, nonce, plaintext, associatedData), nil |
| 64 | } |
| 65 | |
| 66 | // Decrypt decrypts ciphertext with associatedData. |
| 67 | // |
| 68 | // ciphertext consists of two parts: |
| 69 | // 1. the nonce used for encryption |
| 70 | // 2. the actual ciphertext |
| 71 | func (x *XChaCha20Poly1305scip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#) Decryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#Decrypt().(ciphertext []byte, associatedData []byte) ([]byte, error) { |
| 72 | if len(ciphertext) < chacha20poly1305scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/.NonceSizeXscip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/NonceSizeX.+chacha20poly1305scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/.Overheadscip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/Overhead. { |
| 73 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("xchacha20poly1305: ciphertext too short") |
| 74 | } |
| 75 | nonce := ciphertext[:chacha20poly1305scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/.NonceSizeXscip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/NonceSizeX.] |
| 76 | pt, err := x.cipherscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#cipher..Openscip-go gomod github.com/golang/go/src go1.24.0 `crypto/cipher`/AEAD#Open().(nil, nonce, ciphertext[chacha20poly1305scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/.NonceSizeXscip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/NonceSizeX.:], associatedData) |
| 77 | if err != nil { |
| 78 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("XChaCha20Poly1305.Decrypt: %s", err) |
| 79 | } |
| 80 | return pt, nil |
| 81 | } |