Debian Codegraph @

primitives.go

raw symbols SCIP
1 // Copyright 2019 The age Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package agescip-go gomod filippo.io/age . `filippo.io/age`/
6
7 import (
8 "crypto/hmacscip-go gomod github.com/golang/go/src . `crypto/hmac`/"
9 "crypto/sha256scip-go gomod github.com/golang/go/src . `crypto/sha256`/"
10 "errorsscip-go gomod github.com/golang/go/src . errors/"
11 "ioscip-go gomod github.com/golang/go/src . io/"
12
13 "filippo.io/age/internal/formatscip-go gomod filippo.io/age . `filippo.io/age/internal/format`/"
14 "golang.org/x/crypto/chacha20poly1305scip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/chacha20poly1305`/"
15 "golang.org/x/crypto/hkdfscip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/hkdf`/"
16 )
17
18 // aeadEncrypt encrypts a message with a one-time key.
19 func aeadEncryptscip-go gomod filippo.io/age . `filippo.io/age`/aeadEncrypt().(key, plaintext []byte) ([]byte, error) {
20 aead, err := chacha20poly1305scip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/chacha20poly1305`/.Newscip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/chacha20poly1305`/New().(key)
21 if err != nil {
22 return nil, err
23 }
24 // The nonce is fixed because this function is only used in places where the
25 // spec guarantees each key is only used once (by deriving it from values
26 // that include fresh randomness), allowing us to save the overhead.
27 // For the code that encrypts the actual payload, look at the
28 // filippo.io/age/internal/stream package.
29 nonce := make([]byte, chacha20poly1305scip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/chacha20poly1305`/.NonceSizescip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/chacha20poly1305`/NonceSize.)
30 return aead.Sealscip-go gomod github.com/golang/go/src . `crypto/cipher`/AEAD#Seal().(nil, nonce, plaintext, nil), nil
31 }
32
33 var errIncorrectCiphertextSizescip-go gomod filippo.io/age . `filippo.io/age`/errIncorrectCiphertextSize. = errorsscip-go gomod github.com/golang/go/src . errors/.Newscip-go gomod github.com/golang/go/src . errors/New().("encrypted value has unexpected length")
34
35 // aeadDecrypt decrypts a message of an expected fixed size.
36 //
37 // The message size is limited to mitigate multi-key attacks, where a ciphertext
38 // can be crafted that decrypts successfully under multiple keys. Short
39 // ciphertexts can only target two keys, which has limited impact.
40 func aeadDecryptscip-go gomod filippo.io/age . `filippo.io/age`/aeadDecrypt().(key []byte, size int, ciphertext []byte) ([]byte, error) {
41 aead, err := chacha20poly1305scip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/chacha20poly1305`/.Newscip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/chacha20poly1305`/New().(key)
42 if err != nil {
43 return nil, err
44 }
45 if len(ciphertext) != size+aead.Overheadscip-go gomod github.com/golang/go/src . `crypto/cipher`/AEAD#Overhead().() {
46 return nil, errIncorrectCiphertextSizescip-go gomod filippo.io/age . `filippo.io/age`/errIncorrectCiphertextSize.
47 }
48 nonce := make([]byte, chacha20poly1305scip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/chacha20poly1305`/.NonceSizescip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/chacha20poly1305`/NonceSize.)
49 return aead.Openscip-go gomod github.com/golang/go/src . `crypto/cipher`/AEAD#Open().(nil, nonce, ciphertext, nil)
50 }
51
52 func headerMACscip-go gomod filippo.io/age . `filippo.io/age`/headerMAC().(fileKey []byte, hdr *formatscip-go gomod filippo.io/age . `filippo.io/age/internal/format`/.Headerscip-go gomod filippo.io/age . `filippo.io/age/internal/format`/Header#) ([]byte, error) {
53 h := hkdfscip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/hkdf`/.Newscip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/hkdf`/New().(sha256scip-go gomod github.com/golang/go/src . `crypto/sha256`/.Newscip-go gomod github.com/golang/go/src . `crypto/sha256`/New()., fileKey, nil, []byte("header"))
54 hmacKey := make([]byte, 32)
55 if _, err := ioscip-go gomod github.com/golang/go/src . io/.ReadFullscip-go gomod github.com/golang/go/src . io/ReadFull().(h, hmacKey); err != nil {
56 return nil, err
57 }
58 hh := hmacscip-go gomod github.com/golang/go/src . `crypto/hmac`/.Newscip-go gomod github.com/golang/go/src . `crypto/hmac`/New().(sha256scip-go gomod github.com/golang/go/src . `crypto/sha256`/.Newscip-go gomod github.com/golang/go/src . `crypto/sha256`/New()., hmacKey)
59 if err := hdr.MarshalWithoutMACscip-go gomod filippo.io/age . `filippo.io/age/internal/format`/Header#MarshalWithoutMAC().(hh); err != nil {
60 return nil, err
61 }
62 return hh.Sumscip-go gomod github.com/golang/go/src . hash/Hash#Sum().(nil), nil
63 }
64
65 func streamKeyscip-go gomod filippo.io/age . `filippo.io/age`/streamKey().(fileKey, nonce []byte) []byte {
66 h := hkdfscip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/hkdf`/.Newscip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/hkdf`/New().(sha256scip-go gomod github.com/golang/go/src . `crypto/sha256`/.Newscip-go gomod github.com/golang/go/src . `crypto/sha256`/New()., fileKey, nonce, []byte("payload"))
67 streamKey := make([]byte, chacha20poly1305scip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/chacha20poly1305`/.KeySizescip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/chacha20poly1305`/KeySize.)
68 if _, err := ioscip-go gomod github.com/golang/go/src . io/.ReadFullscip-go gomod github.com/golang/go/src . io/ReadFull().(h, streamKey); err != nil {
69 panic("age: internal error: failed to read from HKDF: " + err.Error())
70 }
71 return streamKey
72 }