Age/Stanza/X25519.pm

filter: Statementstmt 100% 52 / 52 Branchbran 100% 6 / 6 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 94.8% 73 / 77 CC 5 SCAR 6.9
CC
5
Cov
97%
BUILD
4.0
unwrap
2.0
BEGIN
1.0
CRAP 2.0
« Age/Stanza.pm ↑ Summary
1 package Crypt::Age::Stanza::X25519;
2 # ABSTRACT: X25519 recipient stanza for age encryption
3 7 our $VERSION = '0.005';
4 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Crypt::Age::Keys;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Crypt::Age::Primitives;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Crypt::Age::Stanza;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 7 extends 'Crypt::Age::Stanza';
13  
14 7 has '+type' => (
15 default => 'X25519',
16 );
17  
18 7 has ephemeral_public => (
19 is => 'ro',
20 );
21  
22  
23 # c2sp.org/age, X25519 recipient type: "The identity implementation MUST ignore
24 # any stanza that does not have X25519 as the first argument, and MUST otherwise
25 # reject any stanza that has more or less than two arguments, or where the second
26 # argument is not a canonical base64 encoding of a 32-byte value. It MUST check
27 # that the body length is exactly 32 bytes before attempting to decrypt it, to
28 # mitigate partitioning oracle attacks."
29 #
30 # The spec counts the type as the first of those two arguments; our args
31 # attribute holds everything after the type, so "exactly two" is exactly one
32 # here.
33 #
34 # The checks live in the constructor rather than in Header::parse or in unwrap:
35 #
36 # * Header::parse already dispatches on the type string to choose this class,
37 # so validating here fires while the header is being parsed. The header is
38 # rejected before any identity is consulted, which is the header failure the
39 # spec asks for -- not the "this identity does not match, try the next
40 # stanza" that unwrap returning undef means.
41 # * Class dispatch scopes the rules exactly right. A stanza of an unrecognized
42 # type is built as a plain Crypt::Age::Stanza and MUST be ignored rather
43 # than rejected, so it never reaches this code and needs no exemption.
44 # * body is 'ro', so the 32-byte invariant holds for the object's lifetime.
45 # That is what keeps the eval in unwrap honest: a wrong-length body can no
46 # longer be constructed, so the only croak that eval still swallows is the
47 # AEAD authentication failure, which genuinely means "wrong identity".
48 #
49 # The base64 canonicality rules -- padding, alphabet, impossible lengths,
50 # non-canonical trailing bits -- are already enforced by
51 # Stanza::decode_base64_no_padding and are deliberately not repeated here; its
52 # croak propagates from the call below. Every message names the reason and never
53 # the value: args and body are key material.
54  
55 2953 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=13.9
Pod documented
56 2953 my ($self) = @_;
57  
58 croak "Invalid X25519 stanza: expected exactly one argument after the type"
59 2953 ▶ unless @{$self->args} == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->args;} == 1 3 2950 2953
60  
61 2950 my $ephemeral_share = Crypt::Age::Stanza::decode_base64_no_padding($self->args->[0]);
62 2949 ▶ croak "Invalid X25519 stanza: argument does not decode to a 32-byte value"
Branch: 1 branch
BranchTrueFalseTotal
unless length $ephemeral_share == 32 5 2944 2949
63 unless length($ephemeral_share) == 32;
64  
65 2944 ▶ croak "Invalid X25519 stanza: body is not exactly 32 bytes"
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->body == 32 3 2941 2944
66 unless length($self->body) == 32;
67  
68 2941 return;
69 }
70  
71  
72 49 ▶sub wrap {
Subroutine wrap: called CC=1 SCAR=0.0
Pod documented
73 49 my ($class, $file_key, $recipient_public_key) = @_;
74  
75 # Decode recipient public key (Bech32 -> raw bytes)
76 49 my $recipient_public = Crypt::Age::Keys->decode_public_key($recipient_public_key);
77  
78 # Generate ephemeral keypair
79 48 my ($ephemeral_public, $ephemeral_private) =
80 Crypt::Age::Primitives->x25519_generate_keypair;
81  
82 # Compute shared secret
83 48 my $shared_secret = Crypt::Age::Primitives->x25519_shared_secret(
84 $ephemeral_private,
85 $recipient_public
86 );
87  
88 # Derive wrap key
89 48 my $wrap_key = Crypt::Age::Primitives->derive_wrap_key(
90 $shared_secret,
91 $ephemeral_public,
92 $recipient_public
93 );
94  
95 # Wrap file key
96 48 my $wrapped_key = Crypt::Age::Primitives->wrap_file_key($wrap_key, $file_key);
97  
98 # Create stanza
99 48 return $class->new(
100 args => [Crypt::Age::Stanza::encode_base64_no_padding($ephemeral_public)],
101 body => $wrapped_key,
102 ephemeral_public => $ephemeral_public,
103 );
104 }
105  
106  
107 87 ▶sub identity_keys {
Subroutine identity_keys: called CC=1 SCAR=0.0
Pod documented
108 87 my ($self, $identity_secret_key) = @_;
109  
110 # Decode identity secret key (Bech32 -> raw bytes)
111 87 my $identity_private = Crypt::Age::Keys->decode_secret_key($identity_secret_key);
112  
113 # Get recipient's public key from identity
114 87 my $pk = Crypt::PK::X25519->new;
115 87 $pk->import_key_raw($identity_private, 'private');
116  
117 return {
118 87 private => $identity_private,
119 public => $pk->export_key_raw('public'),
120 };
121 }
122  
123  
124 603 ▶sub unwrap {
Subroutine unwrap: called CC=2 SCAR=7.0
Pod documented
125 603 my ($self, $identity_secret_key, $identity_keys) = @_;
126  
127 # Deriving the identity's public key costs a scalar multiplication, and
128 # neither it nor the Bech32 decode depends on this stanza -- so the caller
129 # is allowed to do it once per identity and hand the result down, which is
130 # what Header::unwrap_file_key does for a header with many stanzas
131 # (CVE-2026-85783). Falling back to deriving it here keeps the documented
132 # one-argument call in the SYNOPSIS working, and keeps an invalid Bech32
133 # identity failing at exactly the point it always did: on the first X25519
134 # stanza the identity is tried against, not earlier.
135 603 ▶ $identity_keys //= $self->identity_keys($identity_secret_key);
Condition33%1/3
$identity_keys //= $self->identity_keys($identity_secret_key)
l!l&&r!l&&!r
60300
MC/DC0%0/2
$identity_keys //= $self->identity_keys($identity_secret_key)
$identity_keys $self->identity_keys($identity_secret_key)
Truth table33%1/3
A //= B
A= $identity_keysB= $self->identity_keys($identity_secret_key)
ABresult
1X1
011
000
136 603 my $identity_private = $identity_keys->{private};
137 603 my $recipient_public = $identity_keys->{public};
138  
139 # Decode ephemeral public key from stanza args
140 603 my $ephemeral_public = Crypt::Age::Stanza::decode_base64_no_padding($self->args->[0]);
141  
142 # Compute shared secret
143 603 my $shared_secret = Crypt::Age::Primitives->x25519_shared_secret(
144 $identity_private,
145 $ephemeral_public
146 );
147  
148 # Derive wrap key
149 601 my $wrap_key = Crypt::Age::Primitives->derive_wrap_key(
150 $shared_secret,
151 $ephemeral_public,
152 $recipient_public
153 );
154  
155 # Unwrap file key. This eval must swallow one thing only: the AEAD
156 # authentication failure that means "this identity is not the one this
157 # stanza was written for", which the caller reads as undef and answers by
158 # trying the next identity or stanza. Everything structural is already a
159 # header failure by the time we get here -- BUILD guarantees a 32-byte body,
160 # so unwrap_file_key's own length croak is unreachable, and the wrap key is
161 # a fixed-length HKDF output. Widening what is inside this eval would turn a
162 # malformed header back into a silent "no match".
163 601 my $file_key = eval {
164 601 Crypt::Age::Primitives->unwrap_file_key($wrap_key, $self->body);
165 };
166  
167 601 return $file_key; # Returns undef if unwrap failed
168 }
169  
170  
171  
172 7 1;
173  
174 __END__
« Age/Stanza.pm ↑ Summary