| 1 | // Copyright 2025 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 tagtest |
| 6 | |
| 7 | import ( |
| 8 | "crypto/ecdh" |
| 9 | "crypto/subtle" |
| 10 | "fmt" |
| 11 | |
| 12 | "filippo.io/age" |
| 13 | "filippo.io/age/internal/format" |
| 14 | "filippo.io/age/tag" |
| 15 | "filippo.io/hpke" |
| 16 | "filippo.io/nistec" |
| 17 | ) |
| 18 | |
| 19 | type ClassicIdentity struct { |
| 20 | k hpke.PrivateKey |
| 21 | } |
| 22 | |
| 23 | var _ age.Identity = &ClassicIdentity{} |
| 24 | |
| 25 | func NewClassicIdentity(seed string) *ClassicIdentity { |
| 26 | k, err := hpke.DHKEM(ecdh.P256()).DeriveKeyPair([]byte(seed)) |
| 27 | if err != nil { |
| 28 | panic(fmt.Sprintf("failed to generate key: %v", err)) |
| 29 | } |
| 30 | return &ClassicIdentity{k: k} |
| 31 | } |
| 32 | |
| 33 | func (i *ClassicIdentity) Recipient() *tag.Recipient { |
| 34 | uncompressed := i.k.PublicKey().Bytes() |
| 35 | p, err := nistec.NewP256Point().SetBytes(uncompressed) |
| 36 | if err != nil { |
| 37 | panic(fmt.Sprintf("failed to parse public key: %v", err)) |
| 38 | } |
| 39 | r, err := tag.NewClassicRecipient(p.BytesCompressed()) |
| 40 | if err != nil { |
| 41 | panic(fmt.Sprintf("failed to create recipient: %v", err)) |
| 42 | } |
| 43 | return r |
| 44 | } |
| 45 | |
| 46 | func (i *ClassicIdentity) Unwrap(ss []*age.Stanza) ([]byte, error) { |
| 47 | for _, s := range ss { |
| 48 | if s.Type != "p256tag" { |
| 49 | continue |
| 50 | } |
| 51 | if len(s.Args) != 2 { |
| 52 | return nil, fmt.Errorf("malformed stanza") |
| 53 | } |
| 54 | tagArg, err := format.DecodeString(s.Args[0]) |
| 55 | if err != nil { |
| 56 | return nil, fmt.Errorf("malformed tag: %v", err) |
| 57 | } |
| 58 | if len(tagArg) != 4 { |
| 59 | return nil, fmt.Errorf("invalid tag length: %d", len(tagArg)) |
| 60 | } |
| 61 | enc, err := format.DecodeString(s.Args[1]) |
| 62 | if err != nil { |
| 63 | return nil, fmt.Errorf("malformed encapsulated key: %v", err) |
| 64 | } |
| 65 | if len(enc) != 65 { |
| 66 | return nil, fmt.Errorf("invalid encapsulated key length: %d", len(enc)) |
| 67 | } |
| 68 | if len(s.Body) != 32 { |
| 69 | return nil, fmt.Errorf("invalid encrypted file key length: %d", len(s.Body)) |
| 70 | } |
| 71 | |
| 72 | expTag, err := i.Recipient().Tag(enc) |
| 73 | if err != nil { |
| 74 | return nil, fmt.Errorf("failed to compute tag: %v", err) |
| 75 | } |
| 76 | if subtle.ConstantTimeCompare(tagArg, expTag[:4]) != 1 { |
| 77 | continue |
| 78 | } |
| 79 | |
| 80 | r, err := hpke.NewRecipient(enc, i.k, hpke.HKDFSHA256(), hpke.ChaCha20Poly1305(), []byte("age-encryption.org/p256tag")) |
| 81 | if err != nil { |
| 82 | return nil, fmt.Errorf("failed to unwrap file key: %v", err) |
| 83 | } |
| 84 | fileKey, err := r.Open(nil, s.Body) |
| 85 | if err != nil { |
| 86 | // The tag is only a short hint, and might collide with the tag of a |
| 87 | // different recipient. |
| 88 | continue |
| 89 | } |
| 90 | return fileKey, nil |
| 91 | } |
| 92 | return nil, age.ErrIncorrectIdentity |
| 93 | } |
| 94 | |
| 95 | type HybridIdentity struct { |
| 96 | k hpke.PrivateKey |
| 97 | } |
| 98 | |
| 99 | var _ age.Identity = &HybridIdentity{} |
| 100 | |
| 101 | func NewHybridIdentity(seed string) *HybridIdentity { |
| 102 | k, err := hpke.MLKEM768P256().DeriveKeyPair([]byte(seed)) |
| 103 | if err != nil { |
| 104 | panic(fmt.Sprintf("failed to generate key: %v", err)) |
| 105 | } |
| 106 | return &HybridIdentity{k: k} |
| 107 | } |
| 108 | |
| 109 | func (i *HybridIdentity) Recipient() *tag.Recipient { |
| 110 | r, err := tag.NewHybridRecipient(i.k.PublicKey().Bytes()) |
| 111 | if err != nil { |
| 112 | panic(fmt.Sprintf("failed to create recipient: %v", err)) |
| 113 | } |
| 114 | return r |
| 115 | } |
| 116 | |
| 117 | func (i *HybridIdentity) Unwrap(ss []*age.Stanza) ([]byte, error) { |
| 118 | for _, s := range ss { |
| 119 | if s.Type != "mlkem768p256tag" { |
| 120 | continue |
| 121 | } |
| 122 | if len(s.Args) != 2 { |
| 123 | return nil, fmt.Errorf("malformed stanza") |
| 124 | } |
| 125 | tagArg, err := format.DecodeString(s.Args[0]) |
| 126 | if err != nil { |
| 127 | return nil, fmt.Errorf("malformed tag: %v", err) |
| 128 | } |
| 129 | if len(tagArg) != 4 { |
| 130 | return nil, fmt.Errorf("invalid tag length: %d", len(tagArg)) |
| 131 | } |
| 132 | enc, err := format.DecodeString(s.Args[1]) |
| 133 | if err != nil { |
| 134 | return nil, fmt.Errorf("malformed encapsulated key: %v", err) |
| 135 | } |
| 136 | if len(enc) != 1153 { |
| 137 | return nil, fmt.Errorf("invalid encapsulated key length: %d", len(enc)) |
| 138 | } |
| 139 | if len(s.Body) != 32 { |
| 140 | return nil, fmt.Errorf("invalid encrypted file key length: %d", len(s.Body)) |
| 141 | } |
| 142 | |
| 143 | expTag, err := i.Recipient().Tag(enc) |
| 144 | if err != nil { |
| 145 | return nil, fmt.Errorf("failed to compute tag: %v", err) |
| 146 | } |
| 147 | if subtle.ConstantTimeCompare(tagArg, expTag[:4]) != 1 { |
| 148 | continue |
| 149 | } |
| 150 | |
| 151 | r, err := hpke.NewRecipient(enc, i.k, hpke.HKDFSHA256(), hpke.ChaCha20Poly1305(), []byte("age-encryption.org/mlkem768p256tag")) |
| 152 | if err != nil { |
| 153 | return nil, fmt.Errorf("failed to unwrap file key: %v", err) |
| 154 | } |
| 155 | fileKey, err := r.Open(nil, s.Body) |
| 156 | if err != nil { |
| 157 | // The tag is only a short hint, and might collide with the tag of a |
| 158 | // different recipient. |
| 159 | continue |
| 160 | } |
| 161 | return fileKey, nil |
| 162 | } |
| 163 | return nil, age.ErrIncorrectIdentity |
| 164 | } |