Crypt-Serpent
view release on metacpan or search on metacpan
-- Removed the line continuation characters from serpentsboxes.h so
that it'll compile on Solaris and FreeBSD.
1.0 March 28, 2002
-- Fixed a significant bug regarding the use of the makeKey function.
-- Incorporated AES test vectors into test.pl and t/00_load.t.
0.50 November 12, 2001
-- Made the distribution Crypt::CBC compliant.
0.01 September 24, 2001
-- Right now, this module only supports ECB mode. I'll try
to get CBC and CFB working shortly.
NAME
Crypt::Serpent - Crypt::CBC compliant Serpent block cipher encryption module
SYNOPSIS
use Crypt::Serpent;
my $cipher = new Crypt::Serpent $key;
my $ciphertext = $cipher->encrypt($plaintext);
my $plaintext = $cipher->decrypt($ciphertext);
=head1 NAME
Crypt::Serpent - Crypt::CBC compliant Serpent block cipher encryption module
=head1 SYNOPSIS
use Crypt::Serpent;
my $cipher = new Crypt::Serpent $key;
my $ciphertext = $cipher->encrypt($plaintext);
my $plaintext = $cipher->decrypt($ciphertext);
return TRUE;
}
int cipherInit(cipherInstance *cipher, BYTE mode, char *IV)
{
int i;
int rc;
if((mode != MODE_ECB) &&
(mode != MODE_CBC) &&
(mode != MODE_CFB1))
return BAD_CIPHER_MODE;
cipher->mode = mode; /* MODE_ECB, MODE_CBC, or MODE_CFB1 */
cipher->blockSize=128;
if(mode != MODE_ECB)
{
rc=serpent_convert_from_string(cipher->blockSize, IV, cipher->IV);
if(rc<=0)
return BAD_CIPHER_STATE;
}
return TRUE;
}
* (tested on Pentium 133MMX)
*/
switch(cipher->mode)
{
case MODE_ECB:
for(b=0; b<inputLen; b+=128, input+=16, outBuffer+=16)
serpent_encrypt(input, outBuffer, key->subkeys);
return inputLen;
case MODE_CBC:
t[0] = ((unsigned long*)cipher->IV)[0];
t[1] = ((unsigned long*)cipher->IV)[1];
t[2] = ((unsigned long*)cipher->IV)[2];
t[3] = ((unsigned long*)cipher->IV)[3];
for(b=0; b<inputLen; b+=128, input+=16, outBuffer+=16)
{
t[0] ^= ((unsigned long*)input)[0];
t[1] ^= ((unsigned long*)input)[1];
t[2] ^= ((unsigned long*)input)[2];
t[3] ^= ((unsigned long*)input)[3];
unsigned long t[4];
int i, b;
switch(cipher->mode)
{
case MODE_ECB:
for(b=0; b<inputLen; b+=128, input+=16, outBuffer+=16)
serpent_decrypt(input, outBuffer, key->subkeys);
return inputLen;
case MODE_CBC:
t[0] = ((unsigned long*)cipher->IV)[0];
t[1] = ((unsigned long*)cipher->IV)[1];
t[2] = ((unsigned long*)cipher->IV)[2];
t[3] = ((unsigned long*)cipher->IV)[3];
for(b=0; b<inputLen; b+=128, input+=16, outBuffer+=16)
{
serpent_decrypt(input, outBuffer, key->subkeys);
((unsigned long*)outBuffer)[0] ^= t[0];
((unsigned long*)outBuffer)[1] ^= t[1];
((unsigned long*)outBuffer)[2] ^= t[2];
#include <stdio.h>
/* Defines:
Add any additional defines you need
*/
#define DIR_ENCRYPT 0 /* Are we encrpyting? */
#define DIR_DECRYPT 1 /* Are we decrpyting? */
#define MODE_ECB 1 /* Are we ciphering in ECB mode? */
#define MODE_CBC 2 /* Are we ciphering in CBC mode? */
#define MODE_CFB1 3 /* Are we ciphering in 1-bit CFB mode? */
#define TRUE 1
#define FALSE 0
/* Error Codes - CHANGE POSSIBLE: inclusion of additional error codes */
#define BAD_KEY_DIR -1 /* Key direction is invalid, e;g;,
unknown value */
#define BAD_KEY_MAT -2 /* Key material not of correct
length */
#define BAD_KEY_INSTANCE -3 /* Key passed is not valid */
char keyMaterial[MAX_KEY_SIZE+1]; /* Raw key data in ASCII, e.g.,
what the user types or KAT values)*/
/* The following parameters are algorithm dependent, replace or
add as necessary */
unsigned long key[8]; /* The key in binary */
unsigned long subkeys[33][4]; /* Serpent subkeys */
} keyInstance;
/* The structure for cipher information */
typedef struct {
BYTE mode; /* MODE_ECB, MODE_CBC, or MODE_CFB1 */
char IV[MAX_IV_SIZE]; /* A possible Initialization Vector for
ciphering */
/* Add any algorithm specific parameters needed here */
int blockSize; /* Sample: Handles non-128 bit block sizes
(if available) */
} cipherInstance;
/* Function protoypes */
int makeKey(keyInstance *key, BYTE direction, int keyLen,
( run in 1.435 second using v1.01-cache-2.11-cpan-e1769b4cff6 )