Debian Codegraph @

aead/aesgcm/aead.go

raw symbols SCIP
1 // Copyright 2024 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 aesgcmscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/
16
17 import (
18 "bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/"
19 "crypto/aesscip-go gomod github.com/golang/go/src go1.24.0 `crypto/aes`/"
20 "crypto/cipherscip-go gomod github.com/golang/go/src go1.24.0 `crypto/cipher`/"
21 "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/"
22
23 "github.com/tink-crypto/tink-go/v2/insecuresecretdataaccessscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/insecuresecretdataaccess`/"
24 "github.com/tink-crypto/tink-go/v2/internal/aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/aead`/"
25 "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`/"
26 "github.com/tink-crypto/tink-go/v2/keyscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/key`/"
27 "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`/"
28 )
29
30 const (
31 // ivSize is the acceptable IV size defined by RFC 5116.
32 ivSizescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/ivSize. = 12
33 // tagSize is the acceptable tag size defined by RFC 5116.
34 tagSizescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/tagSize. = 16
35 )
36
37 // fullAEAD is an implementation of the [tink.AEAD] interface with AES-GCM.
38 //
39 // It implements RFC 5116 Section 5.1 and 5.2 and adds a prefix to the
40 // ciphertext.
41 type fullAEADscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/fullAEAD# struct {
42 cipherscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/fullAEAD#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#
43 prefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/fullAEAD#prefix. []byte
44 }
45
46 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# = (*fullAEADscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/fullAEAD#)(nil)
47
48 // Encrypt encrypts plaintext with associatedData.
49 //
50 // The returned ciphertext is of the form:
51 //
52 // prefix || iv || ciphertext || tag
53 //
54 // where prefix is the key's output prefix, iv is a random 12-byte IV,
55 // ciphertext is the encrypted plaintext, and tag is a 16-byte tag.
56 func (a *fullAEADscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/fullAEAD#) Encryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/fullAEAD#Encrypt().(plaintext, associatedData []byte) ([]byte, error) {
57 if err := aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/aead`/.CheckAESGCMPlaintextSizescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/aead`/CheckAESGCMPlaintextSize().(uint64(len(plaintext))); err != nil {
58 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().("aesgcm.Encrypt: %v", err)
59 }
60 dst := make([]byte, len(a.prefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/fullAEAD#prefix.)+ivSizescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/ivSize., len(a.prefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/fullAEAD#prefix.)+ivSizescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/ivSize.+len(plaintext)+tagSizescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/tagSize.)
61 copy(dst, a.prefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/fullAEAD#prefix.)
62 iv := dst[len(a.prefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/fullAEAD#prefix.):]
63 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().(iv)
64 return a.cipherscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/fullAEAD#cipher..Sealscip-go gomod github.com/golang/go/src go1.24.0 `crypto/cipher`/AEAD#Seal().(dst, iv, plaintext, associatedData), nil
65 }
66
67 // Decrypt decrypts ciphertext with associatedData.
68 //
69 // The ciphertext is assumed to be of the form:
70 //
71 // <prefix> || iv || ciphertext || tag
72 //
73 // where prefix is the key's output prefix, iv is the 12-byte IV, ciphertext is
74 // the encrypted plaintext, and tag is the 16-byte tag.
75 // prefix must match the key's output prefix. The prefix may be empty.
76 func (a *fullAEADscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/fullAEAD#) Decryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/fullAEAD#Decrypt().(ciphertext, associatedData []byte) ([]byte, error) {
77 if len(ciphertext) < len(a.prefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/fullAEAD#prefix.)+ivSizescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/ivSize.+tagSizescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/tagSize. {
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().("aesgcm.Decrypt: ciphertext with size %d is too short", len(ciphertext))
79 }
80 prefix := ciphertext[:len(a.prefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/fullAEAD#prefix.)]
81 if !bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.Equalscip-go gomod github.com/golang/go/src go1.24.0 bytes/Equal().(prefix, a.prefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/fullAEAD#prefix.) {
82 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().("aesgcm.Decrypt: ciphertext prefix does not match")
83 }
84 iv := ciphertext[len(a.prefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/fullAEAD#prefix.) : len(a.prefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/fullAEAD#prefix.)+ivSizescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/ivSize.]
85 ciphertextWithTag := ciphertext[len(a.prefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/fullAEAD#prefix.)+ivSizescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/ivSize.:]
86 plaintextLen := len(ciphertextWithTag) - tagSizescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/tagSize.
87 output := make([]byte, 0, plaintextLen)
88 pt, err := a.cipherscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/fullAEAD#cipher..Openscip-go gomod github.com/golang/go/src go1.24.0 `crypto/cipher`/AEAD#Open().(output, iv, ciphertextWithTag, associatedData)
89 if err != nil {
90 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().("aesgcm.Decrypt: %v", err)
91 }
92 return pt, nil
93 }
94
95 // NewAEAD creates a [tink.AEAD] from a [Key].
96 func NewAEADscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/NewAEAD().(k *Keyscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/Key#) (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#, error) {
97 if err := aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/aead`/.ValidateAESKeySizescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/aead`/ValidateAESKeySize().(uint32(k.parametersscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/Key#parameters..KeySizeInBytesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/Parameters#KeySizeInBytes().())); err != nil {
98 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().("aesgcm.NewAEAD: %v", err)
99 }
100 if k.parametersscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/Key#parameters..IVSizeInBytesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/Parameters#IVSizeInBytes().() != ivSizescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/ivSize. {
101 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().("aesgcm.NewAEAD: unsupported IV size: got %v, want %v", k.parametersscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/Key#parameters..IVSizeInBytesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/Parameters#IVSizeInBytes().(), ivSizescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/ivSize.)
102 }
103 if k.parametersscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/Key#parameters..TagSizeInBytesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/Parameters#TagSizeInBytes().() != tagSizescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/tagSize. {
104 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().("aesgcm.NewAEAD: unsupported tag size: got %v, want %v", k.parametersscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/Key#parameters..TagSizeInBytesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/Parameters#TagSizeInBytes().(), tagSizescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/tagSize.)
105 }
106 c, err := aesscip-go gomod github.com/golang/go/src go1.24.0 `crypto/aes`/.NewCipherscip-go gomod github.com/golang/go/src go1.24.0 `crypto/aes`/NewCipher().(k.KeyBytesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/Key#KeyBytes().().Datascip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/secretdata`/Bytes#Data().(insecuresecretdataaccessscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/insecuresecretdataaccess`/.Tokenscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/insecuresecretdataaccess`/Token#{}))
107 if err != nil {
108 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().("aesgcm.NewAEAD: failed to initialize cipher")
109 }
110 aeadCipher, err := cipherscip-go gomod github.com/golang/go/src go1.24.0 `crypto/cipher`/.NewGCMscip-go gomod github.com/golang/go/src go1.24.0 `crypto/cipher`/NewGCM().(c)
111 if err != nil {
112 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().("aesgcm.NewAEAD: failed to create cipher.AEAD")
113 }
114 return &fullAEADscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/fullAEAD#{
115 cipherscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/fullAEAD#cipher.: aeadCipher,
116 prefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/fullAEAD#prefix.: k.OutputPrefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/Key#OutputPrefix().(),
117 }, nil
118 }
119
120 // primitiveConstructor creates a [fullAEAD] from a [key.Key].
121 //
122 // The key must be of type [aesgcm.Key].
123 func primitiveConstructorscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/primitiveConstructor().(k keyscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/key`/.Keyscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/key`/Key#) (any, error) {
124 that, ok := k.(*Keyscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/Key#)
125 if !ok {
126 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().("key is of type %T; needed *aesgcm.Key", k)
127 }
128 return NewAEADscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/aesgcm`/NewAEAD().(that)
129 }