RFC: Crypt::Skip32 - 32-bit block cipher based on Skipjack
am 23.09.2007 12:15:04 von Eric HammondI'm planning to upload this new module for distribution on the CPAN.
Feedback welcomed.
NAME
Crypt::Skip32 - 32-bit block cipher based on Skipjack
SYNOPSIS
use Crypt::Skip32;
$cipher = new Crypt::Skip32 $key;
$ciphertext = $cipher->encrypt($plaintext);
$plaintext = $cipher->decrypt($ciphertext);
DESCRIPTION
Skip32 is a 80-bit key, 32-bit block cipher based on Skipjack. The
Perl
code for the algorithm is a direct translation from C to Perl of
skip32.c by Greg Rose found here:
http://www.qualcomm.com.au/PublicationsDocs/skip32.c
This cipher can be handy for scrambling small (32-bit) values when
you
would like to make it not obvious what they are while keeping the
encrypted output size small.
One example where Skip32 has been useful: You have numeric
database
record ids which increment sequentially. You would like to use
them in
URLs, but you don't want to make it obvious how many X's you have
in the
database by putting the ids directly in the URLs. You can use
Skip32 to
scramble the id and put the resulting 32-bit value in the URL
(perhaps
as 8 hex digits or some other shorter encoding). When a user
requests a
URL, you can unscramble the id to retrieve the object from the
database.
Warning: A 32-bit value can only go a little over 4 billion
(American).
Plan ahead if what you need to encrypt might eventually go over
this
limit.
FUNCTIONS
new
my $cipher = new Crypt::Skip32 $key;
Creates a new Crypt::Skip32 block cipher object, using $key,
where
$key is a key of "keysize()" (10) bytes.
encrypt
my $ciphertext = $cipher->encrypt($plaintext);
Encrypt $plaintext and return the $ciphertext where $plaintext
and
$ciphertext must be of "blocksize()" (4) bytes.
decrypt
my $plaintext = $cipher->decrypt($ciphertext);
Decrypt $ciphertext and return the $plaintext where $plaintext
and
$ciphertext must be of "blocksize()" (4) bytes.
blocksize
my $blocksize = $cipher->blocksize;
Returns the size (in bytes) of the block cipher. This is
always 4
bytes (for 32 bits).
keysize
my $keysize = $cipher->keysize;
Returns the size (in bytes) of the key. This is always 10
bytes.
EXAMPLE
use Crypt::Skip32;
my $key = pack("H20", "112233445566778899AA"); # Your
secret!
my $cipher = new Crypt::Skip32 $key; # Always 10 bytes!
my $plaintext1 = pack("N", 3493209676);
my $ciphertext = $cipher->encrypt($plaintext1); # Always 4
bytes!
print "scrambled 3493209676 -> 0x", unpack("H8", $ciphertext),
"\n";
my $plaintext2 = $cipher->decrypt($ciphertext);
die "something went horribly wrong" unless $plaintext2 eq
$plaintext1;
print "blocksize: ", $cipher->blocksize, " bytes \n";
print "keysize: ", $cipher->keysize, " bytes \n";
TODO
This version of Skip32 is implemented entirely in Perl. Since
there is a
free C implementation of the algorithm, this should probably be
made
available with XS or Inline::C so that it can run a bit faster.
SEE ALSO
The original Skip32 implementation in C by Greg Rose:
http://www.qualcomm.com.au/PublicationsDocs/skip32.c
The 80-bit key, 64-bit block Skipjack cipher created by the NSA
(Perl
code maintained by Julius C. Duque):
Crypt::Skipjack
AUTHOR
Perl code maintained by Eric Hammond
http://www.anvilon.com
Original Skip32 C code written 1999-04-27 by Greg Rose, based on
an
implementation of the Skipjack algorithm written by Panu Rissanen.
COPYRIGHT AND LICENSE
(C) Copyright 2007 Eric Hammond
This library is free software; you can redistribute it and/or
modify it
under the same terms as Perl itself, either Perl version 5.8.8 or,
at
your option, any later version of Perl 5 you may have available.
The C version of Skip32 by Greg Rose is explicitly "not copyright,
no
rights reserved" and he approved of making a Perl version
available
on the CPAN.