File: chacha20poly1305_insecure_nonce.go

package info (click to toggle)
golang-github-tink-crypto-tink-go 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,952 kB
  • sloc: sh: 864; makefile: 6
file content (73 lines) | stat: -rw-r--r-- 2,576 bytes parent folder | download
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package aead

import (
	"errors"
	"fmt"

	"golang.org/x/crypto/chacha20poly1305"
)

const (
	// ChaCha20Poly1305InsecureNonceSize is the size of the nonce.
	ChaCha20Poly1305InsecureNonceSize = chacha20poly1305.NonceSize
	// ChaCha20Poly1305InsecureTagSize is the size of the tag.
	ChaCha20Poly1305InsecureTagSize = chacha20poly1305.Overhead

	maxPlaintextSize = maxInt - ChaCha20Poly1305InsecureNonceSize - ChaCha20Poly1305InsecureTagSize
)

// ChaCha20Poly1305InsecureNonce is an insecure implementation of the AEAD
// interface that permits the user to set the nonce.
type ChaCha20Poly1305InsecureNonce struct {
	Key []byte
}

// NewChaCha20Poly1305InsecureNonce returns a ChaCha20Poly1305InsecureNonce instance.
// The key argument should be a 32-bytes key.
func NewChaCha20Poly1305InsecureNonce(key []byte) (*ChaCha20Poly1305InsecureNonce, error) {
	if len(key) != chacha20poly1305.KeySize {
		return nil, errors.New("bad key length")
	}
	return &ChaCha20Poly1305InsecureNonce{Key: key}, nil
}

// Encrypt encrypts plaintext with nonce and associatedData.
func (ca *ChaCha20Poly1305InsecureNonce) Encrypt(dst, nonce, plaintext, associatedData []byte) ([]byte, error) {
	if len(plaintext) > maxPlaintextSize {
		return nil, fmt.Errorf("plaintext too long")
	}
	c, err := chacha20poly1305.New(ca.Key)
	if err != nil {
		return nil, err
	}
	return c.Seal(dst, nonce, plaintext, associatedData), nil
}

// Decrypt decrypts ciphertext with nonce and associatedData.
func (ca *ChaCha20Poly1305InsecureNonce) Decrypt(nonce, ciphertext, associatedData []byte) ([]byte, error) {
	if len(nonce) != ChaCha20Poly1305InsecureNonceSize {
		return nil, fmt.Errorf("invalid nonce length: got %d, want %d", len(nonce), ChaCha20Poly1305InsecureNonceSize)
	}
	if len(ciphertext) < ChaCha20Poly1305InsecureTagSize {
		return nil, fmt.Errorf("ciphertext too short")
	}
	c, err := chacha20poly1305.New(ca.Key)
	if err != nil {
		return nil, err
	}
	return c.Open(nil, nonce, ciphertext, associatedData)
}