Crypt-Rijndael_PP

 view release on metacpan or  search on metacpan

Rijndael_PP.pm  view on Meta::CPAN

	[0x15,0xe8,0xe6,0xef], [0x1c,0xe5,0xed,0xe1], [0x07,0xf2,0xf0,0xf3], [0x0e,0xff,0xfb,0xfd], 
	[0x79,0xb4,0x92,0xa7], [0x70,0xb9,0x99,0xa9], [0x6b,0xae,0x84,0xbb], [0x62,0xa3,0x8f,0xb5], 
	[0x5d,0x80,0xbe,0x9f], [0x54,0x8d,0xb5,0x91], [0x4f,0x9a,0xa8,0x83], [0x46,0x97,0xa3,0x8d]
);


# alg-ref.h
# define MAXBC				(256/32)
# define MAXKC				(256/32)
# define MAXROUNDS			14
sub MAXBC() { 8 }
sub MAXKC() { 8 }
sub MAXROUNDS() { 14 }


# alg-ref.c
use constant shifts => [
   [[0, 0], [1, 3], [2, 2], [3, 1]],   
   [[0, 0], [1, 5], [2, 4], [3, 3]],
   [[0, 0], [1, 7], [3, 5], [4, 4]]
];

#word8 mul(word8 a, word8 b) {
#   /* multiply two elements of GF(2^m)
#    * needed for MixColumn and InvMixColumn
#    */
#	if (a && b) return Alogtable[(Logtable[a] + Logtable[b])%255];
#	else return 0;
#}
sub mul($$) {
	$_[0] && $_[1] ? Algotable->[(Logtable->[$_[0]] + Logtable->[$_[1]]) % 255] : 0
}

#void KeyAddition(word8 a[4][MAXBC], word8 rk[4][MAXBC], word8 BC) {
#	/* Exor corresponding text input and round key input bytes
#	 */
#	int i, j;
#	
#	for(i = 0; i < 4; i++)
#   		for(j = 0; j < BC; j++) a[i][j] ^= rk[i][j];

Rijndael_PP.pm  view on Meta::CPAN

#	# define SC	((BC - 4) >> 1)
#	my $SC = ($BC-4)>>1;
#	for $i (1..3) {
#		my $j;
#		for $j (0..$BC-1) { $tmp[$j] = $a[$i][($j + shifts->[$SC][$i][$d]) % $BC]	}
#		for $j (0..$BC-1) { $a[$i][$j] = $tmp[$j] }
#	}
#	@_[0..3] = @a; # return by reference
#}

sub ShiftRow(@) {
	my @tmp;
	for my $i (1..3) {
		my $j;
		for $j (0..$_[5]-1) { $tmp[$j] = $_[$i][($j + shifts->[($_[5]-4)>>1][$i][$_[4]]) % $_[5]]	}
		for $j (0..$_[5]-1) { $_[$i][$j] = $tmp[$j] }
	}
}

#void Substitution(word8 a[4][MAXBC], word8 box[256], word8 BC) {
#	/* Replace every byte of the input by the byte at that place

Rijndael_PP.pm  view on Meta::CPAN

}





##################################
# api-ref.c

# define     DIR_ENCRYPT     0    /*  Are we encrpyting?  */
sub DIR_ENCRYPT() { 0 }
# define     DIR_DECRYPT     1    /*  Are we decrpyting?  */
sub DIR_DECRYPT() { 1 }
# define     MODE_ECB        1    /*  Are we ciphering in ECB mode?   */
sub MODE_ECB() { 1 }
# define     MODE_CBC        2    /*  Are we ciphering in CBC mode?   */
sub MODE_CBC() { 2 }
# define     MODE_CFB1       3    /*  Are we ciphering in 1-bit CFB mode? */
sub MODE_CFB1() { 3 }
# define     TRUE            1
sub TRUE() { 1 }
# define     FALSE           0
sub FALSE() { 0 }
# define	BITSPERBLOCK		128		/* Default number of bits in a cipher block */
sub BITSPERBLOCK() { 128 }

# /*  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  */
#define     BAD_CIPHER_MODE    -4  /*  Params struct passed to cipherInit invalid */
#define     BAD_CIPHER_STATE   -5  /*  Cipher in wrong state (e.g., not initialized) */
#define     BAD_CIPHER_INSTANCE   -7 

# /*  CHANGE POSSIBLE:  inclusion of algorithm specific defines  */
#define     MAX_KEY_SIZE	64  /* # of ASCII char's needed to represent a key */
sub MAX_KEY_SIZE() { 64 }
#define     MAX_IV_SIZE		BITSPERBLOCK/8  /* # bytes needed to represent an IV  */
sub MAX_IV_SIZE() { BITSPERBLOCK/8 }



#int makeKey(keyInstance *key, BYTE direction, int keyLen, char *keyMaterial)
#{
#	word8 k[4][MAXKC];
#	int i, j, t;

sub makeKey($$$$) {

	my ($key, $direction, $keyLen, $keyMaterial) = @_;
	
	#$keyLen = length $keyMaterial; # hey, it's perl :-)
	
	my @k;
	
#	
#	if (key == NULL) {
#		return BAD_KEY_INSTANCE;

Rijndael_PP.pm  view on Meta::CPAN

	
	return TRUE;
}



#int cipherInit(cipherInstance *cipher, BYTE mode, char *IV)
#{
#	int i, j, t;

sub cipherInit($$;$) {
	
	my ($cipher, $mode, $IV) = @_;
	
	my $i;

#	
#	if ((mode == MODE_ECB) || (mode == MODE_CBC) || (mode == MODE_CFB1)) {
#		cipher->mode = mode;
#	} else {
#		return BAD_CIPHER_MODE;

Rijndael_PP.pm  view on Meta::CPAN





#int blockEncrypt(cipherInstance *cipher,
#	keyInstance *key, BYTE *input, int inputLen, BYTE *outBuffer)
#{
#	int i, j, t, numBlocks;
#	word8 block[4][MAXBC];

sub blockEncrypt($$$$$) {

	my ($cipher, $key, $input, $inputLen, $outBuffer) = @_;
	
	my ($i,$j,$t,$numBlocks,@block);

#
#	
#        /* check parameter consistency: */
#       if (key == NULL ||
#                key->direction != DIR_ENCRYPT ||

Rijndael_PP.pm  view on Meta::CPAN





#int blockDecrypt(cipherInstance *cipher,
#	keyInstance *key, BYTE *input, int inputLen, BYTE *outBuffer)
#{
#	int i, j, t, numBlocks;
#	word8 block[4][MAXBC];

sub blockDecrypt($$$$$) {
	
	my ($cipher, $key, $input, $inputLen, $outBuffer) = @_;
	
	my ($i,$j,$t,$numBlocks,@block);

#
#	if (cipher == NULL ||
#		key == NULL ||
#		key->direction == DIR_ENCRYPT ||
#		cipher->blockLen != key->blockLen) {

Rijndael_PP.pm  view on Meta::CPAN

# *	Only used in the Intermediate Value Known Answer Test.	
# *
# *	Returns:
# *		TRUE - on success
# *		BAD_CIPHER_STATE - cipher in bad state (e.g., not initialized)
# */
#int cipherUpdateRounds(cipherInstance *cipher,
#	keyInstance *key, BYTE *input, int inputLen, BYTE *outBuffer, int rounds)
#{
	
sub cipherUpdateRounds($$$$$$) {
	
	my ($cipher, $key, $input, $inputLen, $outBuffer, $rounds) = @_;
		
#	int j, t;
#	word8 block[4][MAXBC];

	my ($j,$t,@block);

#
#	if (cipher == NULL ||

Rijndael_PP.pm  view on Meta::CPAN

			my $res = blockDecrypt($self->{cipherInst}, $self->{DEkeyInst}, \@block, $self->{blocksize}, $tmp);
		};
		die $@ if $@;
		$out .= pack 'C'x($self->{blocksize}/8), @$tmp;
	}
	return unless defined $out;
	$out;
}


sub rijndael_encrypt($$$;$$) {
	my ($key, $mode, $data, $keysize, $blocksize) = @_;

	$keysize = 128 unless defined $keysize;
	$blocksize = 128 unless defined $blocksize;
	
	my $keyInst = { blockLen => $blocksize };
	
	if (length $key < $keysize/4) {
		$key .= "0" x ($keysize/4 - length $key);
		warn "rijndael_encrypt: padded key to keysize ($keysize)\n";

Rijndael_PP.pm  view on Meta::CPAN

		die $@ if $@;
		$out .= pack 'C'x($blocksize/8), @$tmp;
	}
	
	return unless defined $out;
	$out;
}



sub rijndael_decrypt($$$;$$) {
	my ($key, $mode, $data, $keysize, $blocksize) = @_;
	
	$keysize = 128 unless defined $keysize;
	$blocksize = 128 unless defined $blocksize;
	
	my $keyInst = { blockLen => $blocksize };

	if (length $key < ($keysize/4)) {
		$key .= "0" x ($keysize/4 - length $key);
		warn "rijndael_decrypt: padded key to keysize ($keysize)\n";



( run in 0.238 second using v1.01-cache-2.11-cpan-1f129e94a17 )