| 1 | package Palm::Zetetic::Strip::CryptV10; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Carp; | ||
| 5 | use Digest::SHA256; | ||
| 6 | use Crypt::Rijndael; | ||
| 7 | |||
| 8 | use vars qw(@ISA $VERSION); | ||
| 9 | |||
| 10 | require Exporter; | ||
| 11 | |||
| 12 | @ISA = qw(Palm::Raw); | ||
| 13 | $VERSION = "1.02"; | ||
| 14 | |||
| 15 | sub new | ||
| 16 | { | ||
| 17 | my $class = shift; | ||
| 18 | my ($plaintext_key) = @_; | ||
| 19 | my $hashed_key; | ||
| 20 | my $self = {}; | ||
| 21 | |||
| 22 | bless $self, $class; | ||
| 23 | $hashed_key = $self->hash($plaintext_key); | ||
| 24 | |||
| 25 | $self->{hashed_key} = $hashed_key; | ||
| 26 | $self->{cipher} = new Crypt::Rijndael($hashed_key); | ||
| 27 | return $self; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub get_hashed_key | ||
| 31 | { | ||
| 32 | my ($self) = @_; | ||
| 33 | return $self->{hashed_key}; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub encrypt | ||
| 37 | { | ||
| 38 | confess("Not yet implemented"); | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | # Use Cipher Block Chaining (CBC) to decrypt | ||
| 43 | |||
| 44 | sub decrypt | ||
| 45 | { | ||
| 46 | my ($self, $ciphertext) = @_; | ||
| 47 | my $feedback; | ||
| 48 | my $encrypted_block; | ||
| 49 | my $decrypted_block; | ||
| 50 | my $plaintext; | ||
| 51 | my $cipher; | ||
| 52 | |||
| 53 | $cipher = $self->{cipher}; | ||
| 54 | # Initialize feedback from first block | ||
| 55 | $feedback = substr($ciphertext, 0, 16, ""); | ||
| 56 | |||
| 57 | $plaintext = ""; | ||
| 58 | while(1) | ||
| 59 | { | ||
| 60 | $encrypted_block = substr($ciphertext, 0, 16, ""); | ||
| 61 | last if ($encrypted_block eq ""); | ||
| 62 | $decrypted_block = $cipher->decrypt($encrypted_block); | ||
| 63 | $decrypted_block = $decrypted_block ^ $feedback; | ||
| 64 | $plaintext .= $decrypted_block; | ||
| 65 | $feedback = $encrypted_block; | ||
| 66 | } | ||
| 67 | |||
| 68 | return $plaintext; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub hash | ||
| 72 | { | ||
| 73 | my ($self, $string) = @_; | ||
| 74 | my $hash; | ||
| 75 | |||
| 76 | if ($Digest::SHA256::VERSION == "0.01") | ||
| 77 | { | ||
| 78 | my $digest; | ||
| 79 | |||
| 80 | $digest = Digest::SHA256::new(); | ||
| 81 | $hash = $digest->hash($string); | ||
| 82 | |||
| 83 | # For some reason SHA256 returns 512 bits. Truncate to | ||
| 84 | # 256 bits. The last 256 bits appear to be garbage. | ||
| 85 | $hash = substr($hash, 0, 32); | ||
| 86 | } | ||
| 87 | |||
| 88 | return $hash; | ||
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | 1; |