| 1 | package Crypt::AllOrNothing; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 3 | ▶ | use Crypt::OpenSSL::AES; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 3 | ▶ | use Crypt::AllOrNothing::Util qw/:all/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 3 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 3 | ▶ | use Crypt::Random qw/makerandom/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | =head1 NAME | ||||||||||||||
| 11 | |||||||||||||||
| 12 | Crypt::AllOrNothing - All-Or-Nothing Encryption | ||||||||||||||
| 13 | |||||||||||||||
| 14 | =head1 VERSION | ||||||||||||||
| 15 | |||||||||||||||
| 16 | Version 0.10 | ||||||||||||||
| 17 | |||||||||||||||
| 18 | =cut | ||||||||||||||
| 19 | |||||||||||||||
| 20 | 3 | our $VERSION = '0.10'; | |||||||||||||
| 21 | |||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||
| 23 | |||||||||||||||
| 24 | use Crypt::AllOrNothing; | ||||||||||||||
| 25 | |||||||||||||||
| 26 | my $AllOrNothing = Crypt::AllOrNothing->new(K_0=>$K_0); | ||||||||||||||
| 27 | my $K_0 = $AllOrNothing->get_K_0(); | ||||||||||||||
| 28 | my $K_0 = $AllOrNothing->set_K_0($K_0); | ||||||||||||||
| 29 | my $size = $AllOrNothing->get_size(); | ||||||||||||||
| 30 | my @AllOrNothing_text = $AllOrNothing->encrypt(text=>$plaintext); | ||||||||||||||
| 31 | my $plaintext = $AllOrNothing->decrypt(text=>@AllOrNothing_text); | ||||||||||||||
| 32 | |||||||||||||||
| 33 | =head1 CONSTANTS | ||||||||||||||
| 34 | |||||||||||||||
| 35 | $SIZE = 128 bits; | ||||||||||||||
| 36 | |||||||||||||||
| 37 | =cut | ||||||||||||||
| 38 | |||||||||||||||
| 39 | 3 | my $SIZE = 128; | |||||||||||||
| 40 | |||||||||||||||
| 41 | =head1 FUNCTIONS | ||||||||||||||
| 42 | |||||||||||||||
| 43 | =head2 new(K_0=>$K_0) | ||||||||||||||
| 44 | |||||||||||||||
| 45 | Create AllOrNothing object. Good idea to provide K_0, which must be ascii encoded(characters) and of length given in size. | ||||||||||||||
| 46 | If K_0 is not given, you must retrieve it later with $AllOrNothing->K_0() else you will not be able to decrypt the message. | ||||||||||||||
| 47 | |||||||||||||||
| 48 | =cut | ||||||||||||||
| 49 | |||||||||||||||
| 50 | 1 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=5 SCAR=20.9
Pod
documented
| |||||||||||||||
| 51 | 1 | my $class = shift; | |||||||||||||
| 52 | 1 | my %params = @_; | |||||||||||||
| 53 | 1 | my $self; | |||||||||||||
| 54 | ######## | ||||||||||||||
| 55 | #Check params | ||||||||||||||
| 56 | ######## | ||||||||||||||
| 57 | 1 | ▶ | if (!exists $params{size}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 58 | 1 | $self->{size} = $SIZE; | |||||||||||||
| 59 | } elsif ($params{size} =~ /^(128)$/) { | ||||||||||||||
| 60 | 0 | $self->{size} = $params{size}; | |||||||||||||
| 61 | } else { | ||||||||||||||
| 62 | 0 | croak "Given size($params{size}) is invalid, valid sizes are 128 256 512 1024"; | |||||||||||||
| 63 | } | ||||||||||||||
| 64 | |||||||||||||||
| 65 | 1 | ▶ | if (!exists $params{K_0}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 66 | #carp 'No K_0 given. One will be automatically generated. You must manually get this value with $AllOrNothing->get_K_0() to decrypt.'; | ||||||||||||||
| 67 | 1 | $self->{K_0}=randomValue(size=>$self->{size}, 'return'=>'ascii'); | |||||||||||||
| 68 | } elsif (length $params{K_0} == $self->{size}/8) { | ||||||||||||||
| 69 | 0 | $self->{K_0}=$params{K_0}; | |||||||||||||
| 70 | } else { | ||||||||||||||
| 71 | 0 | croak "Given K_0 was invalid, if given in new() it must be the correct length and characters(not int, hex, or base64)"; | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | |||||||||||||||
| 74 | 1 | bless $self, $class; | |||||||||||||
| 75 | } | ||||||||||||||
| 76 | |||||||||||||||
| 77 | =head2 K_0(K_0=>$K_0) | ||||||||||||||
| 78 | |||||||||||||||
| 79 | get/set K_0. K_0 must be ascii encoded(characters) and length given in size. | ||||||||||||||
| 80 | |||||||||||||||
| 81 | =cut | ||||||||||||||
| 82 | |||||||||||||||
| 83 | 1 | ▶ | sub K_0 { | ||||||||||||
|
Subroutine
K_0: called CC=3 SCAR=16.7
Pod
documented
| |||||||||||||||
| 84 | 1 | my $self = shift; | |||||||||||||
| 85 | 1 | ▶ | if (@_) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 86 | 0 | my $tmp_K_0 = shift; | |||||||||||||
| 87 | 0 | ▶ | if (length $tmp_K_0 != $self->{size}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 88 | 0 | carp 'K_0 passed is not correct length(must be equal to $AllOrNothing->size()), K_0 will not be set'; | |||||||||||||
| 89 | } else { | ||||||||||||||
| 90 | 0 | $self->{K_0} = $tmp_K_0; | |||||||||||||
| 91 | } | ||||||||||||||
| 92 | } | ||||||||||||||
| 93 | 1 | return $self->{K_0}; | |||||||||||||
| 94 | } | ||||||||||||||
| 95 | |||||||||||||||
| 96 | =head2 size() | ||||||||||||||
| 97 | |||||||||||||||
| 98 | get size. | ||||||||||||||
| 99 | |||||||||||||||
| 100 | =cut | ||||||||||||||
| 101 | |||||||||||||||
| 102 | 1 | ▶ | sub size { | ||||||||||||
|
Subroutine
size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 103 | 1 | my $self = shift; | |||||||||||||
| 104 | 1 | return $self->{size}; | |||||||||||||
| 105 | } | ||||||||||||||
| 106 | |||||||||||||||
| 107 | =head2 @ciphertext = encrypt(plaintext=>$plaintext, padding=>$padding) | ||||||||||||||
| 108 | |||||||||||||||
| 109 | encrypt plaintext with All Or Nothing transform to array of messages. Optionally pass padding which will be used internally | ||||||||||||||
| 110 | |||||||||||||||
| 111 | =cut | ||||||||||||||
| 112 | |||||||||||||||
| 113 | 1 | ▶ | sub encrypt { | ||||||||||||
|
Subroutine
encrypt: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||
| 114 | 1 | my $self = shift; | |||||||||||||
| 115 | 1 | my %params = @_; | |||||||||||||
| 116 | |||||||||||||||
| 117 | #break packets into length size packets | ||||||||||||||
| 118 | 1 | my @message = breakString(string=>$params{plaintext}, size=>$self->{size}/8); | |||||||||||||
| 119 | |||||||||||||||
| 120 | #create K_Prime | ||||||||||||||
| 121 | 1 | my $K_Prime = randomValue(size=>$self->{size}, 'return'=>'ascii'); | |||||||||||||
| 122 | 1 | ▶ | my $cipher_K_Prime = Crypt::OpenSSL::AES->new($K_Prime) or croak "Could not create AES cipher with K_Prime"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 123 | |||||||||||||||
| 124 | #add length and padding to message | ||||||||||||||
| 125 | 1 | addLength_andPad(array=>\@message, size=>$self->{size}/8, padding=>$params{padding}); | |||||||||||||
| 126 | |||||||||||||||
| 127 | #encrypt | ||||||||||||||
| 128 | #m_Prime_sub_i = m_sub_i xor E(K_Prime, i) for i=1..s | ||||||||||||||
| 129 | #i must be made to a character and padded to size 'size' bits | ||||||||||||||
| 130 | 1 | my @messagePrime = (); | |||||||||||||
| 131 | 1 | ▶ | for (my $i=0; $i<scalar @message; $i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 132 | 2 | push @messagePrime, $message[$i] ^ ($cipher_K_Prime->encrypt((sprintf("%c",0x00) x ($self->{size}/8-4)) . pack("L",$i))); | |||||||||||||
| 133 | } | ||||||||||||||
| 134 | #m_Prime_sub_s_Prime = K_Prime xor h_1 xor h_2 xor ... h_s | ||||||||||||||
| 135 | #h_i = E(K_sub_0, m_Prime_sub_i xor i) for i=1..s | ||||||||||||||
| 136 | #i must be made to be 'size' bits | ||||||||||||||
| 137 | #K_sub_0 is a fixed publicly known encryption key | ||||||||||||||
| 138 | 1 | ▶ | my $cipher_K_0 = Crypt::OpenSSL::AES->new($self->{K_0}) or croak "Could not create AES cipher with K_0"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 139 | 1 | my $m_Prime_sub_s = $K_Prime; | |||||||||||||
| 140 | 1 | ▶ | for (my $i=0; $i<scalar @message; $i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 141 | 2 | $m_Prime_sub_s ^= $cipher_K_0->encrypt($messagePrime[$i]^((sprintf("%c",0x00) x ($self->{size}/8-4)) . pack("L",$i))); | |||||||||||||
| 142 | } | ||||||||||||||
| 143 | 1 | push @messagePrime, $m_Prime_sub_s; | |||||||||||||
| 144 | 1 | return @messagePrime; | |||||||||||||
| 145 | } | ||||||||||||||
| 146 | |||||||||||||||
| 147 | =head2 $plaintext = decrypt(cryptotext=>\@cryptotext) | ||||||||||||||
| 148 | |||||||||||||||
| 149 | decrypt cryptotext array with All Or Nothing transform to plaintext | ||||||||||||||
| 150 | |||||||||||||||
| 151 | =cut | ||||||||||||||
| 152 | |||||||||||||||
| 153 | 1 | ▶ | sub decrypt { | ||||||||||||
|
Subroutine
decrypt: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||
| 154 | 1 | my $self = shift; | |||||||||||||
| 155 | 1 | my %params = @_; | |||||||||||||
| 156 | 1 | ▶ | my $cipher_K_0 = Crypt::OpenSSL::AES->new($self->{K_0}) or croak "Could not create AES cipher with K_0"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 157 | #get K_Prime | ||||||||||||||
| 158 | 1 | my $m_prime_sub_s_prime = $params{cryptotext}->[$#{$params{cryptotext}}]; | |||||||||||||
| 159 | 1 | my $add_tmp = ''; | |||||||||||||
| 160 | 1 | ▶ | for (my $i=0;$i<$#{$params{cryptotext}};$i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 161 | 2 | $add_tmp ^= $cipher_K_0->encrypt($params{cryptotext}->[$i]^((sprintf("%c",0x00) x ($self->{size}/8-4)) . pack("L",$i))); | |||||||||||||
| 162 | } | ||||||||||||||
| 163 | 1 | my $K_prime = $m_prime_sub_s_prime ^ $add_tmp; | |||||||||||||
| 164 | 1 | pop @{$params{cryptotext}}; | |||||||||||||
| 165 | 1 | ▶ | my $cipher_K_prime = Crypt::OpenSSL::AES->new($K_prime) or die "did not work to create K_Prime AES cipher"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 166 | 1 | my @m=(); | |||||||||||||
| 167 | 1 | ▶ | for (my $i=0;$i<scalar @{$params{cryptotext}};$i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 168 | 2 | push @m, $params{cryptotext}->[$i]^$cipher_K_prime->encrypt(((sprintf("%c",0x00) x 12) . pack("L",$i))); | |||||||||||||
| 169 | } | ||||||||||||||
| 170 | 1 | remLength_andPad(array=>\@m); | |||||||||||||
| 171 | 1 | my $plaintext=''; | |||||||||||||
| 172 | 1 | for (@m) { | |||||||||||||
| 173 | 2 | $plaintext .= $_; | |||||||||||||
| 174 | } | ||||||||||||||
| 175 | 1 | return $plaintext; | |||||||||||||
| 176 | } | ||||||||||||||
| 177 | |||||||||||||||
| 178 | =head1 AUTHOR | ||||||||||||||
| 179 | |||||||||||||||
| 180 | Timothy Zander, C<< <timothy.zander at alum.rpi.edu> >> | ||||||||||||||
| 181 | |||||||||||||||
| 182 | The All Or Nothing Encryption and Package Transform algorithm was developed by Ronald Rivest | ||||||||||||||
| 183 | |||||||||||||||
| 184 | =head1 BUGS | ||||||||||||||
| 185 | |||||||||||||||
| 186 | Please report any bugs or feature requests to | ||||||||||||||
| 187 | C<bug-crypt-aon at rt.cpan.org>, or through the web interface at | ||||||||||||||
| 188 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Crypt-AllOrNothing>. | ||||||||||||||
| 189 | I will be notified, and then you'll automatically be notified of progress on | ||||||||||||||
| 190 | your bug as I make changes. | ||||||||||||||
| 191 | |||||||||||||||
| 192 | =head1 SUPPORT | ||||||||||||||
| 193 | |||||||||||||||
| 194 | You can find documentation for this module with the perldoc command. | ||||||||||||||
| 195 | |||||||||||||||
| 196 | perldoc Crypt::AllOrNothing | ||||||||||||||
| 197 | |||||||||||||||
| 198 | You can also look for information at: | ||||||||||||||
| 199 | |||||||||||||||
| 200 | =over 4 | ||||||||||||||
| 201 | |||||||||||||||
| 202 | =item * Original Paper by Ronald Rivest | ||||||||||||||
| 203 | |||||||||||||||
| 204 | L<http://people.csail.mit.edu/rivest/fusion.pdf> | ||||||||||||||
| 205 | |||||||||||||||
| 206 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||
| 207 | |||||||||||||||
| 208 | L<http://annocpan.org/dist/Crypt-AllOrNothing> | ||||||||||||||
| 209 | |||||||||||||||
| 210 | =item * CPAN Ratings | ||||||||||||||
| 211 | |||||||||||||||
| 212 | L<http://cpanratings.perl.org/d/Crypt-AllOrNothing> | ||||||||||||||
| 213 | |||||||||||||||
| 214 | =item * RT: CPAN's request tracker | ||||||||||||||
| 215 | |||||||||||||||
| 216 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Crypt-AllOrNothing> | ||||||||||||||
| 217 | |||||||||||||||
| 218 | =item * Search CPAN | ||||||||||||||
| 219 | |||||||||||||||
| 220 | L<http://search.cpan.org/dist/Crypt-AllOrNothing> | ||||||||||||||
| 221 | |||||||||||||||
| 222 | =back | ||||||||||||||
| 223 | |||||||||||||||
| 224 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||
| 225 | |||||||||||||||
| 226 | Thanks to Prof. Bulent Yener at RPI for his assistance. | ||||||||||||||
| 227 | |||||||||||||||
| 228 | =head1 COPYRIGHT & LICENSE | ||||||||||||||
| 229 | |||||||||||||||
| 230 | Copyright 2007 Timothy Zander, all rights reserved. | ||||||||||||||
| 231 | |||||||||||||||
| 232 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||
| 233 | under the same terms as Perl itself. | ||||||||||||||
| 234 | |||||||||||||||
| 235 | =cut | ||||||||||||||
| 236 | |||||||||||||||
| 237 | 3 | 1; # End of Crypt::AllOrNothing | |||||||||||||