| 1 | 3 | ▶ | use v5.36; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||
| 3 | package Protocol::HAP::SRP; | ||||||||||||||||||||||||||||||
| 4 | 3 | our $VERSION = '0.1.0'; | |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||
| 6 | # Prefer the GMP backend. The 3072-bit modular exponentiation of | ||||||||||||||||||||||||||||||
| 7 | # SRP is impractically slow in the pure-Perl Calc backend. It | ||||||||||||||||||||||||||||||
| 8 | # takes seconds per operation, and far more under emulation. | ||||||||||||||||||||||||||||||
| 9 | # 'try' falls back to Calc silently when Math::BigInt::GMP is | ||||||||||||||||||||||||||||||
| 10 | # not installed. Thus the module stays correct everywhere. | ||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Math::BigInt try => 'GMP'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Digest::SHA qw(sha512); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Protocol::HAP::Crypto; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Protocol::HAP::SetupCode qw(normalize_setup_code); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||
| 16 | # SRP-6a implementation for HAP | ||||||||||||||||||||||||||||||
| 17 | # The module uses the 3072-bit group from RFC 5054. | ||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||
| 19 | # The group is HAP policy, not a primitive. HAP-Pairing names this | ||||||||||||||||||||||||||||||
| 20 | # group and no other, so the constants live with the protocol that | ||||||||||||||||||||||||||||||
| 21 | # chose them and not with the cryptography that computes over them. | ||||||||||||||||||||||||||||||
| 22 | 3 | our $N_3072 = pack( 'H*', | |||||||||||||||||||||||||||||
| 23 | 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' | ||||||||||||||||||||||||||||||
| 24 | . '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' | ||||||||||||||||||||||||||||||
| 25 | . 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' | ||||||||||||||||||||||||||||||
| 26 | . 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' | ||||||||||||||||||||||||||||||
| 27 | . 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' | ||||||||||||||||||||||||||||||
| 28 | . 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' | ||||||||||||||||||||||||||||||
| 29 | . '83655D23DCA3AD961C62F356208552BB9ED529077096966D' | ||||||||||||||||||||||||||||||
| 30 | . '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' | ||||||||||||||||||||||||||||||
| 31 | . 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' | ||||||||||||||||||||||||||||||
| 32 | . 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' | ||||||||||||||||||||||||||||||
| 33 | . '15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64' | ||||||||||||||||||||||||||||||
| 34 | . 'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7' | ||||||||||||||||||||||||||||||
| 35 | . 'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B' | ||||||||||||||||||||||||||||||
| 36 | . 'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C' | ||||||||||||||||||||||||||||||
| 37 | . 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31' | ||||||||||||||||||||||||||||||
| 38 | . '43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF' ); | ||||||||||||||||||||||||||||||
| 39 | 3 | our $g = 5; | |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||
| 41 | # N_len: Length of N in bytes (3072 bits / 8 = 384 bytes) | ||||||||||||||||||||||||||||||
| 42 | 3 | ▶ | use constant N_LEN => 384; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||
| 44 | # _bigint_to_bytes($bigint, $length = undef) - Convert BigInt to bytes | ||||||||||||||||||||||||||||||
| 45 | # The function removes the '0x' prefix from as_hex(). It pads | ||||||||||||||||||||||||||||||
| 46 | # the bytes to a fixed length if the caller gives $length. | ||||||||||||||||||||||||||||||
| 47 | 48 | ▶ | sub _bigint_to_bytes ( $bigint, $length = undef ) | ||||||||||||||||||||||||||||
|
Subroutine
_bigint_to_bytes: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||
| 48 | 48 | { | |||||||||||||||||||||||||||||
| 49 | 48 | my $hex = $bigint->as_hex(); | |||||||||||||||||||||||||||||
| 50 | 48 | $hex =~ s/^0x//; # Remove the 0x prefix | |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||
| 52 | # Make the number of hex digits even | ||||||||||||||||||||||||||||||
| 53 | 48 | ▶ | $hex = '0' . $hex if length($hex) % 2; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||
| 55 | 48 | my $bytes = pack( 'H*', $hex ); | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||
| 57 | # Pad with zeros on the left if the caller gives a length | ||||||||||||||||||||||||||||||
| 58 | 48 | ▶ | if ( defined $length && length($bytes) < $length ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $length and length $bytes < $length
MC/DC100%2/2
defined $length and length $bytes < $length
defined $length
length $bytes < $length
Truth table100%3/3
A and B
A= defined $lengthB= length $bytes < $length
| |||||||||||||||||||||||||||||||
| 59 | 20 | $bytes = ( "\x00" x ( $length - length($bytes) ) ) . $bytes; | |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||
| 62 | 48 | return $bytes; | |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||
| 65 | 15 | ▶ | sub new ( $class, %args ) | ||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||
| 68 | my $self = bless { | ||||||||||||||||||||||||||||||
| 69 | username => $args{username} // 'Pair-Setup', | ||||||||||||||||||||||||||||||
| 70 | password => normalize_setup_code( $args{password} ) | ||||||||||||||||||||||||||||||
| 71 | // $args{password}, | ||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||
| 73 | # The RFC 5054 3072-bit group. The exchange methods fill | ||||||||||||||||||||||||||||||
| 74 | # in the session state as it becomes known. | ||||||||||||||||||||||||||||||
| 75 | 15 | ▶ | N => Math::BigInt->from_hex( | ||||||||||||||||||||||||||||
|
Condition80%4/5
$args{"username"} // "Pair-Setup"
normalize_setup_code($args{"password"}) // $args{"password"}
MC/DC100%1/1
$args{"username"} // "Pair-Setup"
$args{"username"}
MC/DC0%0/2
normalize_setup_code($args{"password"}) // $args{"password"}
normalize_setup_code($args{"password"})
$args{"password"}
Truth table100%2/2
A
A= $args{"username"}
Truth table66%2/3
A // B
A= normalize_setup_code($args{"password"})B= $args{"password"}
| |||||||||||||||||||||||||||||||
| 76 | unpack( 'H*', $Protocol::HAP::SRP::N_3072 ) | ||||||||||||||||||||||||||||||
| 77 | ), | ||||||||||||||||||||||||||||||
| 78 | g => Math::BigInt->new($Protocol::HAP::SRP::g), | ||||||||||||||||||||||||||||||
| 79 | }, $class; | ||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||
| 81 | 15 | return $self; | |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||
| 84 | 12 | ▶ | sub generate_salt ($self) | ||||||||||||||||||||||||||||
|
Subroutine
generate_salt: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||||
| 86 | 12 | $self->{salt} = Protocol::HAP::Crypto->random_bytes(16); | |||||||||||||||||||||||||||||
| 87 | 12 | return $self->{salt}; | |||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||
| 90 | 10 | ▶ | sub compute_verifier ( $self, $salt = undef ) | ||||||||||||||||||||||||||||
|
Subroutine
compute_verifier: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 91 | 10 | { | |||||||||||||||||||||||||||||
| 92 | 10 | ▶ | $salt //= $self->{salt}; | ||||||||||||||||||||||||||||
|
Condition33%1/3
$salt //= $self->{"salt"}
MC/DC0%0/2
$salt //= $self->{"salt"}
$salt
$self->{"salt"}
Truth table33%1/3
A //= B
A= $saltB= $self->{"salt"}
| |||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||
| 94 | # x = H(salt | H(username | ":" | password)) | ||||||||||||||||||||||||||||||
| 95 | 10 | my $inner = sha512( $self->{username} . ':' . $self->{password} ); | |||||||||||||||||||||||||||||
| 96 | 10 | my $x_bytes = sha512( $salt . $inner ); | |||||||||||||||||||||||||||||
| 97 | 10 | my $x = Math::BigInt->from_hex( unpack( 'H*', $x_bytes ) ); | |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||
| 99 | # v = g^x mod N | ||||||||||||||||||||||||||||||
| 100 | # bmodpow mutates its invocant. Thus work on a copy of g. | ||||||||||||||||||||||||||||||
| 101 | 10 | my $v = $self->{g}->copy->bmodpow( $x, $self->{N} ); | |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||
| 103 | 10 | $self->{v} = $v; | |||||||||||||||||||||||||||||
| 104 | 10 | return $v; | |||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||
| 107 | 8 | ▶ | sub generate_server_public ($self) | ||||||||||||||||||||||||||||
|
Subroutine
generate_server_public: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 108 | { | ||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||
| 110 | # Generate a random b (256 bits) | ||||||||||||||||||||||||||||||
| 111 | 8 | my $b_bytes = Protocol::HAP::Crypto->random_bytes(32); | |||||||||||||||||||||||||||||
| 112 | 8 | $self->{b} = Math::BigInt->from_hex( unpack( 'H*', $b_bytes ) ); | |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||
| 114 | # k = H(N | PAD(g)) | ||||||||||||||||||||||||||||||
| 115 | # PAD(g) pads g to the same length as N (384 bytes for 3072-bit) | ||||||||||||||||||||||||||||||
| 116 | 8 | my $N_bytes = _bigint_to_bytes( $self->{N} ); | |||||||||||||||||||||||||||||
| 117 | 8 | my $N_len = length($N_bytes); | |||||||||||||||||||||||||||||
| 118 | 8 | my $g_padded = _bigint_to_bytes( $self->{g}, $N_len ); | |||||||||||||||||||||||||||||
| 119 | 8 | my $k_bytes = sha512( $N_bytes . $g_padded ); | |||||||||||||||||||||||||||||
| 120 | 8 | my $k = Math::BigInt->from_hex( unpack( 'H*', $k_bytes ) ); | |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||
| 122 | # B = (k*v + g^b) mod N | ||||||||||||||||||||||||||||||
| 123 | # bmodpow mutates its invocant. Thus work on a copy of g. | ||||||||||||||||||||||||||||||
| 124 | my $B = | ||||||||||||||||||||||||||||||
| 125 | ( $k * $self->{v} + | ||||||||||||||||||||||||||||||
| 126 | $self->{g}->copy->bmodpow( $self->{b}, $self->{N} ) ) | ||||||||||||||||||||||||||||||
| 127 | 8 | % $self->{N}; | |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||
| 129 | 8 | $self->{B} = $B; | |||||||||||||||||||||||||||||
| 130 | 8 | return $B; | |||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||
| 133 | # $self->server_public_bytes(): | ||||||||||||||||||||||||||||||
| 134 | # Return B as it goes on the wire: N_LEN bytes, zero-padded on | ||||||||||||||||||||||||||||||
| 135 | # the left. | ||||||||||||||||||||||||||||||
| 136 | # | ||||||||||||||||||||||||||||||
| 137 | # About one B in 256 is small enough that its natural encoding | ||||||||||||||||||||||||||||||
| 138 | # is 383 bytes. A caller that packs the number itself sends a | ||||||||||||||||||||||||||||||
| 139 | # short public key, and the controller computes u over different | ||||||||||||||||||||||||||||||
| 140 | # bytes than the accessory does. The pairing then fails at M4, | ||||||||||||||||||||||||||||||
| 141 | # for one attempt in 256, with no diagnosis. Thus the padding | ||||||||||||||||||||||||||||||
| 142 | # belongs here, beside N_LEN, and not at the call site. | ||||||||||||||||||||||||||||||
| 143 | 3 | ▶ | sub server_public_bytes ($self) | ||||||||||||||||||||||||||||
|
Subroutine
server_public_bytes: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 144 | { | ||||||||||||||||||||||||||||||
| 145 | 3 | ▶ | return unless defined $self->{B}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||
| 147 | 2 | return _bigint_to_bytes( $self->{B}, N_LEN ); | |||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||
| 150 | 7 | ▶ | sub compute_session_key ( $self, $A_bytes ) | ||||||||||||||||||||||||||||
|
Subroutine
compute_session_key: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 151 | { | ||||||||||||||||||||||||||||||
| 152 | 7 | my $A = Math::BigInt->from_hex( unpack( 'H*', $A_bytes ) ); | |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||
| 154 | # Security: make sure that A mod N != 0. This is an SRP-6a | ||||||||||||||||||||||||||||||
| 155 | # requirement per HAP-Pairing.md §2.6. A malicious | ||||||||||||||||||||||||||||||
| 156 | # controller can send A = 0, N, or 2N to make the shared | ||||||||||||||||||||||||||||||
| 157 | # secret predictable and thus bypass authentication. | ||||||||||||||||||||||||||||||
| 158 | 7 | ▶ | return if ( $A % $self->{N} )->is_zero(); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||
| 160 | 5 | $self->{A} = $A; | |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||
| 162 | # u = H(PAD(A) | PAD(B)) | ||||||||||||||||||||||||||||||
| 163 | # Pad both A and B to N_LEN per HAP-Pairing.md §2.6 and the | ||||||||||||||||||||||||||||||
| 164 | # SRP-6a spec. | ||||||||||||||||||||||||||||||
| 165 | 5 | my $A_padded = _bigint_to_bytes( $self->{A}, N_LEN ); | |||||||||||||||||||||||||||||
| 166 | 5 | my $B_padded = _bigint_to_bytes( $self->{B}, N_LEN ); | |||||||||||||||||||||||||||||
| 167 | 5 | my $u_bytes = sha512( $A_padded . $B_padded ); | |||||||||||||||||||||||||||||
| 168 | 5 | my $u = Math::BigInt->from_hex( unpack( 'H*', $u_bytes ) ); | |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||
| 170 | # S = (A * v^u)^b mod N | ||||||||||||||||||||||||||||||
| 171 | # bmodpow mutates its invocant. Thus work on a copy of v. | ||||||||||||||||||||||||||||||
| 172 | # The multiplication result is a fresh object. It is safe | ||||||||||||||||||||||||||||||
| 173 | # to mutate. | ||||||||||||||||||||||||||||||
| 174 | my $S = | ||||||||||||||||||||||||||||||
| 175 | ( $self->{A} * $self->{v}->copy->bmodpow( $u, $self->{N} ) ) | ||||||||||||||||||||||||||||||
| 176 | 5 | ->bmodpow( $self->{b}, $self->{N} ); | |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||
| 178 | 5 | $self->{S} = $S; | |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||
| 180 | # K = H(S) | ||||||||||||||||||||||||||||||
| 181 | 5 | my $S_bytes = _bigint_to_bytes($S); | |||||||||||||||||||||||||||||
| 182 | 5 | $self->{K} = sha512($S_bytes); | |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||
| 184 | 5 | return $self->{K}; | |||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||
| 187 | 1 | ▶ | sub verify_client_proof ( $self, $M1_client ) | ||||||||||||||||||||||||||||
|
Subroutine
verify_client_proof: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 188 | { | ||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||
| 190 | # M1 = H(H(N) XOR H(g) | H(username) | salt | A | B | K) | ||||||||||||||||||||||||||||||
| 191 | # Use the string xor operator. v5.36 enables the 'bitwise' | ||||||||||||||||||||||||||||||
| 192 | # feature. Under that feature, plain ^ is numeric-only. | ||||||||||||||||||||||||||||||
| 193 | 1 | my $N_hash = sha512( _bigint_to_bytes( $self->{N} ) ); | |||||||||||||||||||||||||||||
| 194 | 1 | my $g_hash = sha512( _bigint_to_bytes( $self->{g} ) ); | |||||||||||||||||||||||||||||
| 195 | 1 | my $xor = $N_hash ^. $g_hash; | |||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||
| 197 | 1 | my $user_hash = sha512( $self->{username} ); | |||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||
| 199 | # M1 = H(H(N) XOR H(g) | H(I) | s | PAD(A) | PAD(B) | K) | ||||||||||||||||||||||||||||||
| 200 | # Pad A and B to N_LEN per HAP-Pairing.md §2.5. | ||||||||||||||||||||||||||||||
| 201 | 1 | my $A_bytes = _bigint_to_bytes( $self->{A}, N_LEN ); | |||||||||||||||||||||||||||||
| 202 | 1 | my $B_bytes = _bigint_to_bytes( $self->{B}, N_LEN ); | |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||
| 204 | my $M1 = | ||||||||||||||||||||||||||||||
| 205 | sha512( $xor | ||||||||||||||||||||||||||||||
| 206 | . $user_hash | ||||||||||||||||||||||||||||||
| 207 | . $self->{salt} | ||||||||||||||||||||||||||||||
| 208 | . $A_bytes | ||||||||||||||||||||||||||||||
| 209 | . $B_bytes | ||||||||||||||||||||||||||||||
| 210 | 1 | . $self->{K} ); | |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||
| 212 | 1 | $self->{M1} = $M1; | |||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||
| 214 | 1 | return $M1 eq $M1_client; | |||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | sub generate_server_proof ($self) | ||||||||||||||||||||||||||||
|
Subroutine
generate_server_proof: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 218 | { | ||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||
| 220 | # M2 = H(PAD(A) | M1 | K) | ||||||||||||||||||||||||||||||
| 221 | # Pad A to N_LEN per HAP-Pairing.md §2.6. | ||||||||||||||||||||||||||||||
| 222 | 0 | my $A_bytes = _bigint_to_bytes( $self->{A}, N_LEN ); | |||||||||||||||||||||||||||||
| 223 | 0 | my $M2 = sha512( $A_bytes . $self->{M1} . $self->{K} ); | |||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||
| 225 | 0 | $self->{M2} = $M2; | |||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||
| 227 | 0 | return $M2; | |||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||
| 230 | # $self->session_key(): | ||||||||||||||||||||||||||||||
| 231 | # Return the shared session key K (64 bytes). | ||||||||||||||||||||||||||||||
| 232 | 1 | ▶ | sub session_key ($self) | ||||||||||||||||||||||||||||
|
Subroutine
session_key: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 233 | { | ||||||||||||||||||||||||||||||
| 234 | 1 | return $self->{K}; | |||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||
| 237 | package Protocol::HAP::SRP::Client; | ||||||||||||||||||||||||||||||
| 238 | 3 | our $VERSION = '0.1.0'; | |||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||
| 240 | # The imports of the accessory package above do not reach this one: | ||||||||||||||||||||||||||||||
| 241 | # Perl imports into a package, not into a file. | ||||||||||||||||||||||||||||||
| 242 | 3 | ▶ | use Digest::SHA qw(sha512); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 243 | 3 | ▶ | use Protocol::HAP::SetupCode qw(normalize_setup_code); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||
| 245 | # The controller role of the same exchange (HAP-Pairing.md §2.5). It | ||||||||||||||||||||||||||||||
| 246 | # reuses the group, the N_LEN padding rule, and the byte conversion | ||||||||||||||||||||||||||||||
| 247 | # of the accessory role above, so the two sides cannot drift. One | ||||||||||||||||||||||||||||||
| 248 | # module holds both, and the conformance vectors exercise them | ||||||||||||||||||||||||||||||
| 249 | # against each other. | ||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||
| 251 | # One definition of the padding rule: the accessory package's. | ||||||||||||||||||||||||||||||
| 252 | 3 | ▶ | use constant N_LEN => Protocol::HAP::SRP::N_LEN; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||
| 254 | 0 | ▶ | sub _b2i ($bytes) | ||||||||||||||||||||||||||||
|
Subroutine
_b2i: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 255 | { | ||||||||||||||||||||||||||||||
| 256 | 0 | return Math::BigInt->from_hex( unpack( 'H*', $bytes ) ); | |||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||
| 259 | 0 | ▶ | sub new ( $class, %args ) | ||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||
| 260 | { | ||||||||||||||||||||||||||||||
| 261 | my $password = normalize_setup_code( $args{password} ) | ||||||||||||||||||||||||||||||
| 262 | 0 | ▶ | // $args{password}; | ||||||||||||||||||||||||||||
|
Condition0%0/3
normalize_setup_code($args{"password"}) // $args{"password"}
MC/DC0%0/2
normalize_setup_code($args{"password"}) // $args{"password"}
normalize_setup_code($args{"password"})
$args{"password"}
Truth table0%0/3
A // B
A= normalize_setup_code($args{"password"})B= $args{"password"}
| |||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||
| 264 | my $self = bless { | ||||||||||||||||||||||||||||||
| 265 | 0 | ▶ | username => $args{username} // 'Pair-Setup', | ||||||||||||||||||||||||||||
|
Condition0%0/2
$args{"username"} // "Pair-Setup"
MC/DC0%0/1
$args{"username"} // "Pair-Setup"
$args{"username"}
Truth table0%0/2
A
A= $args{"username"}
| |||||||||||||||||||||||||||||||
| 266 | password => $password, | ||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||
| 268 | N => _b2i($Protocol::HAP::SRP::N_3072), | ||||||||||||||||||||||||||||||
| 269 | g => Math::BigInt->new($Protocol::HAP::SRP::g), | ||||||||||||||||||||||||||||||
| 270 | }, $class; | ||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||
| 272 | 0 | return $self; | |||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||
| 275 | # $self->compute_public(): | ||||||||||||||||||||||||||||||
| 276 | # Generate the ephemeral secret a and return the padded public | ||||||||||||||||||||||||||||||
| 277 | # value A = g^a mod N (384 bytes). | ||||||||||||||||||||||||||||||
| 278 | 0 | ▶ | sub compute_public ($self) | ||||||||||||||||||||||||||||
|
Subroutine
compute_public: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 279 | { | ||||||||||||||||||||||||||||||
| 280 | 0 | my $a_bytes = Protocol::HAP::Crypto->random_bytes(32); | |||||||||||||||||||||||||||||
| 281 | 0 | $self->{a} = _b2i($a_bytes); | |||||||||||||||||||||||||||||
| 282 | 0 | $self->{A} = $self->{g}->copy->bmodpow( $self->{a}, $self->{N} ); | |||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||
| 284 | 0 | return Protocol::HAP::SRP::_bigint_to_bytes( $self->{A}, N_LEN ); | |||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||
| 287 | # $self->compute_proof($salt, $B_bytes): | ||||||||||||||||||||||||||||||
| 288 | # Complete the client side with the server's salt and public | ||||||||||||||||||||||||||||||
| 289 | # key B. Derive the session key K and return the client proof | ||||||||||||||||||||||||||||||
| 290 | # M1. Return undef if B mod N == 0, which is a bogus server | ||||||||||||||||||||||||||||||
| 291 | # value. | ||||||||||||||||||||||||||||||
| 292 | 0 | ▶ | sub compute_proof ( $self, $salt, $B_bytes ) | ||||||||||||||||||||||||||||
|
Subroutine
compute_proof: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||
| 293 | { | ||||||||||||||||||||||||||||||
| 294 | 0 | ▶ | die 'SRP: compute_public not called' unless defined $self->{A}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||
| 296 | # The accessory package's byte conversion, under a short name | ||||||||||||||||||||||||||||||
| 297 | # for the formulas below | ||||||||||||||||||||||||||||||
| 298 | 0 | my $i2b = \&Protocol::HAP::SRP::_bigint_to_bytes; | |||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||
| 300 | 0 | my $N = $self->{N}; | |||||||||||||||||||||||||||||
| 301 | 0 | my $g = $self->{g}; | |||||||||||||||||||||||||||||
| 302 | 0 | my $B = _b2i($B_bytes); | |||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||
| 304 | 0 | ▶ | return if ( $B % $N )->is_zero(); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||
| 306 | # u = H(PAD(A) | PAD(B)) | ||||||||||||||||||||||||||||||
| 307 | my $u = | ||||||||||||||||||||||||||||||
| 308 | 0 | _b2i( sha512( $i2b->( $self->{A}, N_LEN ) . $i2b->( $B, N_LEN ) ) ); | |||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||
| 310 | # x = H(s | H(I ":" P)), k = H(N | PAD(g)) | ||||||||||||||||||||||||||||||
| 311 | 0 | my $x = _b2i( | |||||||||||||||||||||||||||||
| 312 | sha512( $salt . sha512("$self->{username}:$self->{password}") ) | ||||||||||||||||||||||||||||||
| 313 | ); | ||||||||||||||||||||||||||||||
| 314 | 0 | my $k = _b2i( sha512( $i2b->($N) . $i2b->( $g, N_LEN ) ) ); | |||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||
| 316 | # S = (B - k*g^x)^(a + u*x) mod N | ||||||||||||||||||||||||||||||
| 317 | 0 | my $gx = $g->copy->bmodpow( $x, $N ); | |||||||||||||||||||||||||||||
| 318 | 0 | my $base = ( $B - ( $k * $gx ) ) % $N; | |||||||||||||||||||||||||||||
| 319 | 0 | my $S = $base->bmodpow( $self->{a} + $u * $x, $N ); | |||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||
| 321 | 0 | $self->{K} = sha512( $i2b->($S) ); | |||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||
| 323 | # M1 = H(H(N) xor H(g) | H(I) | s | PAD(A) | PAD(B) | K) | ||||||||||||||||||||||||||||||
| 324 | $self->{M1} = | ||||||||||||||||||||||||||||||
| 325 | sha512( ( sha512( $i2b->($N) ) ^. sha512( $i2b->($g) ) ) | ||||||||||||||||||||||||||||||
| 326 | . sha512( $self->{username} ) | ||||||||||||||||||||||||||||||
| 327 | . $salt | ||||||||||||||||||||||||||||||
| 328 | . $i2b->( $self->{A}, N_LEN ) | ||||||||||||||||||||||||||||||
| 329 | . $i2b->( $B, N_LEN ) | ||||||||||||||||||||||||||||||
| 330 | 0 | . $self->{K} ); | |||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||
| 332 | 0 | return $self->{M1}; | |||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||
| 335 | # $self->verify_server_proof($M2): | ||||||||||||||||||||||||||||||
| 336 | # Check the server proof M2 = H(PAD(A) | M1 | K). | ||||||||||||||||||||||||||||||
| 337 | 0 | ▶ | sub verify_server_proof ( $self, $M2 ) | ||||||||||||||||||||||||||||
|
Subroutine
verify_server_proof: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||
| 338 | { | ||||||||||||||||||||||||||||||
| 339 | 0 | ▶ | die 'SRP: compute_proof not called' unless defined $self->{M1}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||
| 341 | my $expected = | ||||||||||||||||||||||||||||||
| 342 | sha512( Protocol::HAP::SRP::_bigint_to_bytes( $self->{A}, N_LEN ) | ||||||||||||||||||||||||||||||
| 343 | . $self->{M1} | ||||||||||||||||||||||||||||||
| 344 | 0 | . $self->{K} ); | |||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||
| 346 | 0 | ▶ | return $expected eq ( $M2 // '' ); | ||||||||||||||||||||||||||||
|
Condition0%0/2
$M2 // ""
MC/DC0%0/1
$M2 // ""
$M2
Truth table0%0/2
A
A= $M2
| |||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||
| 349 | # $self->session_key(): | ||||||||||||||||||||||||||||||
| 350 | # Return the shared session key K (64 bytes). | ||||||||||||||||||||||||||||||
| 351 | 0 | ▶ | sub session_key ($self) | ||||||||||||||||||||||||||||
|
Subroutine
session_key: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 352 | { | ||||||||||||||||||||||||||||||
| 353 | 0 | return $self->{K}; | |||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||
| 356 | 3 | 1; | |||||||||||||||||||||||||||||