| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | package Crypt::EAX; | ||
| 4 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | our $VERSION = "0.04"; | ||
| 7 | |||
| 8 | 1 | ▶ | use Carp qw(croak); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use Digest::CMAC; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 0 | ▶ | use Crypt::Ctr::FullWidth; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | |||
| 13 | use namespace::clean -except => [qw(meta)]; | ||
| 14 | |||
| 15 | has key => ( | ||
| 16 | isa => "Str", | ||
| 17 | is => "ro", | ||
| 18 | required => 1, | ||
| 19 | ); | ||
| 20 | |||
| 21 | has [qw(header nonce)] => ( | ||
| 22 | isa => "Str", | ||
| 23 | is => "ro", | ||
| 24 | default => '', | ||
| 25 | ); | ||
| 26 | |||
| 27 | has mode => ( | ||
| 28 | isa => "Str", | ||
| 29 | is => "rw", | ||
| 30 | ); | ||
| 31 | |||
| 32 | has N => ( | ||
| 33 | isa => "Str", | ||
| 34 | is => "rw", | ||
| 35 | lazy => 1, | ||
| 36 | default => sub { | ||
| 37 | my $self = shift; | ||
| 38 | $self->omac_t( $self->n_omac, 0, $self->nonce ); | ||
| 39 | }, | ||
| 40 | ); | ||
| 41 | |||
| 42 | has cipher => ( | ||
| 43 | #isa => "ClassName|Object", | ||
| 44 | is => "rw", | ||
| 45 | default => "Crypt::Rijndael", | ||
| 46 | ); | ||
| 47 | |||
| 48 | has fatal => ( | ||
| 49 | isa => "Bool", | ||
| 50 | is => "rw", | ||
| 51 | default => 1, | ||
| 52 | ); | ||
| 53 | |||
| 54 | has ctr => ( | ||
| 55 | isa => "Crypt::Ctr::FullWidth", | ||
| 56 | is => "ro", | ||
| 57 | lazy => 1, | ||
| 58 | default => sub { | ||
| 59 | my $self = shift; | ||
| 60 | Crypt::Ctr::FullWidth->new( $self->key, $self->cipher ); | ||
| 61 | }, | ||
| 62 | ); | ||
| 63 | |||
| 64 | has [qw(c_omac n_omac h_omac)] => ( | ||
| 65 | isa => "Digest::CMAC", | ||
| 66 | is => "ro", | ||
| 67 | lazy => 1, | ||
| 68 | default => sub { | ||
| 69 | my $self = shift; | ||
| 70 | Digest::CMAC->new( $self->key, $self->cipher ); | ||
| 71 | }, | ||
| 72 | ); | ||
| 73 | |||
| 74 | sub BUILDARGS { | ||
| 75 | my ( $class, @args ) = @_; | ||
| 76 | |||
| 77 | if ( @args == 1 ) { | ||
| 78 | return { key => $args[0] } | ||
| 79 | } elsif ( @args == 2 and $args[0] ne 'key' ) { | ||
| 80 | return { key => $args[0], cipher => $args[1] } | ||
| 81 | } else { | ||
| 82 | return $class->SUPER::BUILDARGS(@args); | ||
| 83 | } | ||
| 84 | }; | ||
| 85 | |||
| 86 | sub _cbc_k { | ||
| 87 | my ( $self, $m ) = @_; | ||
| 88 | return; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub reset { | ||
| 92 | my $self = shift; | ||
| 93 | |||
| 94 | $self->ctr->reset; | ||
| 95 | $self->c_omac->reset; | ||
| 96 | $self->h_omac->reset; | ||
| 97 | |||
| 98 | $self->ctr->set_nonce($self->N); | ||
| 99 | |||
| 100 | $self->omac_t( $self->c_omac, 2 ); | ||
| 101 | if ( length ( my $header = $self->header ) ) { | ||
| 102 | $self->omac_t( $self->h_omac, 1, $header ); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | sub BUILD { | ||
| 107 | my ( $self, $args ) = @_; | ||
| 108 | $self->omac_t( $self->h_omac, 1 ); | ||
| 109 | $self->reset; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub start { | ||
| 113 | my ( $self, $mode ) = @_; | ||
| 114 | $self->mode($mode); | ||
| 115 | } | ||
| 116 | |||
| 117 | sub encrypt_parts { | ||
| 118 | my ( $self, $plain ) = @_; | ||
| 119 | |||
| 120 | $self->start('encrypting'); | ||
| 121 | |||
| 122 | return ( $self->add_encrypt($plain), $self->finish ); | ||
| 123 | } | ||
| 124 | |||
| 125 | sub encrypt { | ||
| 126 | my ( $self, $plain ) = @_; | ||
| 127 | return join('', $self->encrypt_parts($plain) ); | ||
| 128 | } | ||
| 129 | |||
| 130 | sub decrypt_parts { | ||
| 131 | my ( $self, $ciphertext, $tag ) = @_; | ||
| 132 | |||
| 133 | $self->start('decrypting'); | ||
| 134 | |||
| 135 | my $plain = $self->add_decrypt( $ciphertext ); | ||
| 136 | |||
| 137 | if ( $self->finish($tag) ) { | ||
| 138 | return $plain; | ||
| 139 | } else { | ||
| 140 | $self->verification_failed($ciphertext, $plain, $tag); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | sub decrypt { | ||
| 145 | my ( $self, $ciphertext ) = @_; | ||
| 146 | |||
| 147 | my $blocksize = $self->blocksize; | ||
| 148 | |||
| 149 | $ciphertext =~ s/(.{$blocksize})$//s; | ||
| 150 | my $tag = $1; | ||
| 151 | |||
| 152 | $self->decrypt_parts( $ciphertext, $tag ); | ||
| 153 | } | ||
| 154 | |||
| 155 | sub verification_failed { | ||
| 156 | my $self = shift; | ||
| 157 | |||
| 158 | if ( $self->fatal ) { | ||
| 159 | croak "Verification of ciphertext failed"; | ||
| 160 | } else { | ||
| 161 | return; | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | sub add_header { | ||
| 166 | my ( $self, $plain ) = @_; | ||
| 167 | |||
| 168 | $self->h_omac->add( $plain ); | ||
| 169 | |||
| 170 | return; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub add_encrypt { | ||
| 174 | my ( $self, $plain ) = @_; | ||
| 175 | |||
| 176 | my $ciphertext = $self->ctr->encrypt($plain) || ''; | ||
| 177 | |||
| 178 | $self->c_omac->add( $ciphertext ); | ||
| 179 | |||
| 180 | return $ciphertext; | ||
| 181 | } | ||
| 182 | |||
| 183 | sub add_decrypt { | ||
| 184 | my ( $self, $ciphertext ) = @_; | ||
| 185 | |||
| 186 | $self->c_omac->add( $ciphertext ); | ||
| 187 | |||
| 188 | my $plain = $self->ctr->decrypt($ciphertext); | ||
| 189 | |||
| 190 | return $plain; | ||
| 191 | } | ||
| 192 | |||
| 193 | sub finish { | ||
| 194 | my ( $self, @args ) = @_; | ||
| 195 | |||
| 196 | die "No current mode. Did you forget to call start()?" unless $self->mode; | ||
| 197 | |||
| 198 | my $tag = $self->tag; | ||
| 199 | |||
| 200 | if ( $self->mode eq 'encrypting' ) { | ||
| 201 | return $tag; | ||
| 202 | } elsif ( $self->mode eq 'decrypting' ) { | ||
| 203 | return 1 if $tag eq $args[0]; | ||
| 204 | return; | ||
| 205 | } else { | ||
| 206 | croak "Unknown mode: " . $self->mode; | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | sub tag { | ||
| 211 | my $self = shift; | ||
| 212 | |||
| 213 | my $N = $self->N; | ||
| 214 | my $H = $self->h_omac->digest; | ||
| 215 | my $C = $self->c_omac->digest; | ||
| 216 | |||
| 217 | $self->reset; | ||
| 218 | |||
| 219 | return $N ^ $H ^ $C; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub omac_t { | ||
| 223 | my ( $self, $omac, $t, @msg ) = @_; | ||
| 224 | |||
| 225 | my $blocksize = $self->blocksize; | ||
| 226 | my $padsize = $blocksize -1; | ||
| 227 | |||
| 228 | my $num = pack("x$padsize C", $t); | ||
| 229 | |||
| 230 | $omac->add( $num ); | ||
| 231 | |||
| 232 | $omac->add( $_ ) for @msg; | ||
| 233 | |||
| 234 | return $omac->digest if defined wantarray; | ||
| 235 | } | ||
| 236 | |||
| 237 | sub blocksize { | ||
| 238 | my $self = shift; | ||
| 239 | |||
| 240 | $self->c_omac->{cipher}->blocksize; | ||
| 241 | } | ||
| 242 | |||
| 243 | __PACKAGE__->meta->make_immutable if __PACKAGE__->meta->can("make_immutable"); | ||
| 244 | |||
| 245 | __PACKAGE__; | ||
| 246 | |||
| 247 | __END__ | ||