| 1 | package Langertha::Skeid::Secret; | ||||||||||
| 2 | 47 | our $VERSION = '0.003'; | |||||||||
| 3 | # ABSTRACT: Constant-time comparison for keys, tokens and signatures | ||||||||||
| 4 | 47 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 47 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 47 | ▶ | use Digest::SHA (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | |||||||||||
| 9 | 147 | ▶ | sub equal { | ||||||||
|
Subroutine
equal: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 10 | 147 | my ($class, $given, $want) = @_; | |||||||||
| 11 | 147 | ▶ | my $have = Digest::SHA::sha256(defined($given) ? "$given" : ''); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 12 | 147 | ▶ | my $need = Digest::SHA::sha256(defined($want) ? "$want" : ''); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 13 | 147 | my $diff = 0; | |||||||||
| 14 | 147 | $diff |= ord(substr($have, $_, 1)) ^ ord(substr($need, $_, 1)) for 0 .. length($need) - 1; | |||||||||
| 15 | 147 | ▶ | return $diff == 0 ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | } | ||||||||||
| 17 | |||||||||||
| 18 | |||||||||||
| 19 | 47 | 1; | |||||||||
| 20 | |||||||||||
| 21 | __END__ | ||||||||||
| 22 | |||||||||||
| 23 | =pod | ||||||||||
| 24 | |||||||||||
| 25 | =encoding UTF-8 | ||||||||||
| 26 | |||||||||||
| 27 | =head1 NAME | ||||||||||
| 28 | |||||||||||
| 29 | Langertha::Skeid::Secret - Constant-time comparison for keys, tokens and signatures | ||||||||||
| 30 | |||||||||||
| 31 | =head1 VERSION | ||||||||||
| 32 | |||||||||||
| 33 | version 0.003 | ||||||||||
| 34 | |||||||||||
| 35 | =head1 SYNOPSIS | ||||||||||
| 36 | |||||||||||
| 37 | use Langertha::Skeid::Secret; | ||||||||||
| 38 | Langertha::Skeid::Secret->equal($presented_token, $admin_api_key) or return deny(); | ||||||||||
| 39 | |||||||||||
| 40 | =head1 DESCRIPTION | ||||||||||
| 41 | |||||||||||
| 42 | Every place Skeid checks a presented secret -- the admin API key, the registry read key, a | ||||||||||
| 43 | registry snapshot signature -- compares through L</equal>, never with C<eq>/C<ne>, so a caller | ||||||||||
| 44 | cannot recover the secret one character at a time from response timing. | ||||||||||
| 45 | |||||||||||
| 46 | =head2 equal | ||||||||||
| 47 | |||||||||||
| 48 | my $ok = Langertha::Skeid::Secret->equal($given, $want); | ||||||||||
| 49 | |||||||||||
| 50 | True (1) when both strings are equal, else 0. It compares the SHA-256 digests of both strings | ||||||||||
| 51 | byte by byte without short-circuiting, so the time depends neither on where they first differ | ||||||||||
| 52 | nor on either string's length. C<undef> compares as the empty string; callers that must reject | ||||||||||
| 53 | an empty or missing secret check that before calling. | ||||||||||
| 54 | |||||||||||
| 55 | =head1 SEE ALSO | ||||||||||
| 56 | |||||||||||
| 57 | L<Langertha::Skeid::Proxy> (admin and registry bearer checks), L<Langertha::Skeid::Registry/verify> | ||||||||||
| 58 | |||||||||||
| 59 | =head1 SUPPORT | ||||||||||
| 60 | |||||||||||
| 61 | =head2 Issues | ||||||||||
| 62 | |||||||||||
| 63 | Please report bugs and feature requests on GitHub at | ||||||||||
| 64 | L<https://github.com/Getty/langertha-skeid/issues>. | ||||||||||
| 65 | |||||||||||
| 66 | =head2 IRC | ||||||||||
| 67 | |||||||||||
| 68 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||
| 69 | |||||||||||
| 70 | =head1 CONTRIBUTING | ||||||||||
| 71 | |||||||||||
| 72 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||
| 73 | |||||||||||
| 74 | =head1 AUTHOR | ||||||||||
| 75 | |||||||||||
| 76 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||
| 77 | |||||||||||
| 78 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 79 | |||||||||||
| 80 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||
| 81 | |||||||||||
| 82 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 83 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 84 | |||||||||||
| 85 | =cut | ||||||||||