Crypt-Rijndael

 view release on metacpan or  search on metacpan

META.json  view on Meta::CPAN

{
   "abstract" : "Crypt::CBC compliant Rijndael encryption module",
   "author" : [
      "Rafael R. Sevilla",
      "brian d foy <bdfoy@cpan.org>",
      "Leon Timmermans <leont@cpan.org>"
   ],
   "dynamic_config" : 0,
   "generated_by" : "ExtUtils::MakeMaker version 7.44, CPAN::Meta::Converter version 2.150010",
   "license" : [
      "open_source"
   ],

META.yml  view on Meta::CPAN

---
abstract: 'Crypt::CBC compliant Rijndael encryption module'
author:
  - 'Rafael R. Sevilla'
  - 'brian d foy <bdfoy@cpan.org>'
  - 'Leon Timmermans <leont@cpan.org>'
build_requires:
  ExtUtils::MakeMaker: '0'
  Test::More: '0'
configure_requires:
  ExtUtils::MakeMaker: '0'
dynamic_config: 0

NEWS  view on Meta::CPAN


Changes from Version 0.04:

All modes of operation recommended by NIST as of July 2001 (ECB, CBC, CFB-128,
OFB, and CTR) are implemented, with the exception of n-bit CFB mode.  I can't
think of a way to do it without adding yet another incompatible method...

An incompatible (to Crypt::CBC) method of changing the IV's for the
modes which require it has been added.

README  view on Meta::CPAN

NAME
    Crypt::Rijndael - Crypt::CBC compliant Rijndael encryption module

VERSION
    Version 1.16

SYNOPSIS
     use Crypt::Rijndael;

     # keysize() is 32, but 24 and 16 are also possible
     # blocksize() is 16

     $cipher = Crypt::Rijndael->new( "a" x 32, Crypt::Rijndael::MODE_CBC() );

     $cipher->set_iv($iv);
     $crypted = $cipher->encrypt($plaintext);
            # - OR -
     $plaintext = $cipher->decrypt($crypted);

DESCRIPTION
    This module implements the Rijndael cipher, which has just been selected
    as the Advanced Encryption Standard.

    keysize
        Returns the keysize, which is 32 (bytes). The Rijndael cipher
        actually supports keylengths of 16, 24 or 32 bytes, but there is no
        way to communicate this to "Crypt::CBC".

    blocksize
        The blocksize for Rijndael is 16 bytes (128 bits), although the
        algorithm actually supports any blocksize that is any multiple of
        our bytes. 128 bits, is however, the AES-specified block size, so
        this is all we support.

    $cipher = Crypt::Rijndael->new( $key [, $mode] )
        Create a new "Crypt::Rijndael" cipher object with the given key
        (which must be 128, 192 or 256 bits long). The additional $mode
        argument is the encryption mode, either "MODE_ECB" (electronic
        codebook mode, the default), "MODE_CBC" (cipher block chaining, the
        same that "Crypt::CBC" does), "MODE_CFB" (128-bit cipher feedback),
        "MODE_OFB" (128-bit output feedback), or "MODE_CTR" (counter mode).

        ECB mode is very insecure (read a book on cryptography if you don't
        know why!), so you should probably use CBC mode.

    $cipher->set_iv($iv)
        This allows you to change the initial value vector used by the
        chaining modes. It is not relevant for ECB mode.

    $cipher->encrypt($data)
        Encrypt data. The size of $data must be a multiple of "blocksize"
        (16 bytes), otherwise this function will croak. Apart from that, it
        can be of (almost) any length.

    $cipher->decrypt($data)
        Decrypts $data.

  Encryption modes
    Use these constants to select the cipher type:

    MODE_CBC - Cipher Block Chaining
    MODE_CFB - Cipher feedback
    MODE_CTR - Counter mode
    MODE_ECB - Electronic cookbook mode
    MODE_OFB - Output feedback
    MODE_PCBC - ignore this one for now :)

SEE ALSO
    Crypt::CBC, http://www.csrc.nist.gov/encryption/aes/

BUGS
    Should EXPORT or EXPORT_OK the MODE constants.

AUTHOR
    Currently maintained by Leon Timmermans "leont@cpan.org".

    Previously maintained by brian d foy, "<bdfoy@cpan.org>".

    Original code by Rafael R. Sevilla.

Rijndael.pm  view on Meta::CPAN

use XSLoader;

XSLoader::load('Crypt::Rijndael', $VERSION);

1;

__END__

=head1 NAME

Crypt::Rijndael - Crypt::CBC compliant Rijndael encryption module

=head1 VERSION

Version 1.16

=head1 SYNOPSIS

 use Crypt::Rijndael;

 # keysize() is 32, but 24 and 16 are also possible
 # blocksize() is 16

 $cipher = Crypt::Rijndael->new( "a" x 32, Crypt::Rijndael::MODE_CBC() );

 $cipher->set_iv($iv);
 $crypted = $cipher->encrypt($plaintext);
 	# - OR -
 $plaintext = $cipher->decrypt($crypted);

=head1 DESCRIPTION

This module implements the Rijndael cipher, which has just been selected
as the Advanced Encryption Standard.

=over 4

=item keysize

Returns the keysize, which is 32 (bytes). The Rijndael cipher
actually supports keylengths of 16, 24 or 32 bytes, but there is no
way to communicate this to C<Crypt::CBC>.

=item blocksize

The blocksize for Rijndael is 16 bytes (128 bits), although the
algorithm actually supports any blocksize that is any multiple of
our bytes.  128 bits, is however, the AES-specified block size,
so this is all we support.

=item $cipher = Crypt::Rijndael->new( $key [, $mode] )

Create a new C<Crypt::Rijndael> cipher object with the given key
(which must be 128, 192 or 256 bits long). The additional C<$mode>
argument is the encryption mode, either C<MODE_ECB> (electronic
codebook mode, the default), C<MODE_CBC> (cipher block chaining, the
same that C<Crypt::CBC> does), C<MODE_CFB> (128-bit cipher feedback),
C<MODE_OFB> (128-bit output feedback), or C<MODE_CTR> (counter mode).

ECB mode is very insecure (read a book on cryptography if you don't
know why!), so you should probably use CBC mode.

=item $cipher->set_iv($iv)

This allows you to change the initial value vector used by the
chaining modes.  It is not relevant for ECB mode.

=item $cipher->encrypt($data)

Encrypt data. The size of C<$data> must be a multiple of C<blocksize>
(16 bytes), otherwise this function will croak. Apart from that, it

Rijndael.pm  view on Meta::CPAN

Decrypts C<$data>.

=back

=head2 Encryption modes

Use these constants to select the cipher type:

=over 4

=item MODE_CBC - Cipher Block Chaining

=item MODE_CFB - Cipher feedback

=item MODE_CTR - Counter mode

=item MODE_ECB - Electronic cookbook mode

=item MODE_OFB - Output feedback

=item MODE_PCBC - ignore this one for now :)

=back

=head1 SEE ALSO

L<Crypt::CBC>, http://www.csrc.nist.gov/encryption/aes/

=head1 BUGS

Should EXPORT or EXPORT_OK the MODE constants.

=head1 AUTHOR

Currently maintained by Leon Timmermans C<< leont@cpan.org >>.

Previously maintained by brian d foy, C<< <bdfoy@cpan.org> >>.

Rijndael.xs  view on Meta::CPAN


PROTOTYPES: DISABLE

BOOT:
{
  HV *stash = gv_stashpvs("Crypt::Rijndael", GV_ADD);

  newCONSTSUB(stash, "keysize",   newSVuv(32)        );
  newCONSTSUB(stash, "blocksize", newSVuv(16)        );
  newCONSTSUB(stash, "MODE_ECB",  newSVuv(MODE_ECB)  );
  newCONSTSUB(stash, "MODE_CBC",  newSVuv(MODE_CBC)  );
  newCONSTSUB(stash, "MODE_CFB",  newSVuv(MODE_CFB)  );
  newCONSTSUB(stash, "MODE_PCBC", newSVuv(MODE_PCBC) );
  newCONSTSUB(stash, "MODE_OFB",  newSVuv(MODE_OFB)  );
  newCONSTSUB(stash, "MODE_CTR",  newSVuv(MODE_CTR)  );
}

Crypt::Rijndael
new(class, key, mode=MODE_ECB)
	SV * class
	SV * key
	int mode
	PREINIT:

Rijndael.xs  view on Meta::CPAN

	CODE:
		if (!SvPOK(key))
			Perl_croak(aTHX_ "Key must be an string scalar");
		if (SvTAINTED(key))
			Perl_croak(aTHX_ "Key must be untainted");

		keysize = SvCUR(key);

		if (keysize != 16 && keysize != 24 && keysize != 32)
			Perl_croak(aTHX_ "Wrong key length: key must be 128, 192 or 256 bits long");
		if (mode != MODE_ECB && mode != MODE_CBC && mode != MODE_CFB && mode != MODE_OFB && mode != MODE_CTR)
			Perl_croak(aTHX_ "Illegal mode, see documentation for valid modes");

		Newxz(RETVAL, 1, struct cryptstate);
		RETVAL->ctx.mode = mode;
		rijndael_setup(&RETVAL->ctx, keysize, (uint8_t *) SvPVbyte_nolen(key));
	OUTPUT:
		RETVAL

SV *
set_iv(self, data)

Rijndael.xs  view on Meta::CPAN


	CODE:
		{
		SV *res;
		STRLEN size;
		void *rawbytes = SvPVbyte(data,size);

		if (size) {
			uint8_t* buffer;

			if ((self->ctx.mode == MODE_ECB || self->ctx.mode == MODE_CBC) && size % RIJNDAEL_BLOCKSIZE)
				Perl_croak(aTHX_ "encrypt: datasize not multiple of blocksize (%d bytes)", RIJNDAEL_BLOCKSIZE);

			RETVAL = newSV(size);
			SvPOK_only(RETVAL);
			SvCUR_set(RETVAL, size);
			buffer = (uint8_t *)SvPVbyte_nolen(RETVAL);
			(ix ? block_decrypt : block_encrypt)
				(&self->ctx, rawbytes, size, buffer, iv);
			buffer[size] = '\0';
		}

_rijndael.c  view on Meta::CPAN

    xor_bytes_to(block, block_no(input, nblocks), leftover, block_no(output, nblocks));
/*  increment_counter(counter); */
  }
}

void block_encrypt(const RIJNDAEL_context *ctx, const uint8_t *input, int inputlen, uint8_t *output, const uint8_t *iv) {
  switch (ctx->mode) {
  case MODE_ECB:		/* electronic code book */
    ecb_encrypt(ctx, input, inputlen, output);
    break;
  case MODE_CBC:		/* Cipher block chaining */
    /* set initial value */
    cbc_encrypt(ctx, input, inputlen, output, iv);
    break;
  case MODE_CFB:		/* 128-bit cipher feedback */
    cfb_encrypt(ctx, input, inputlen, output, iv);
    break;
  case MODE_OFB:		/* 128-bit output feedback */
    ofb_encrypt(ctx, input, inputlen, output, iv);
    break;
  case MODE_CTR:		/* counter */

_rijndael.c  view on Meta::CPAN

    xor_bytes_to(block, block_no(input, i), leftover, block_no(output, i));
    memcpy(block, block_no(input, i), leftover);
  }
}

void block_decrypt(const RIJNDAEL_context *ctx, const uint8_t *input, int inputlen, uint8_t *output, const uint8_t *iv) {
  switch (ctx->mode) {
  case MODE_ECB:
    ecb_decrypt(ctx, input, inputlen, output);
    break;
  case MODE_CBC:
    cbc_decrypt(ctx, input, inputlen, output, iv);
    break;
  case MODE_CFB:		/* 128-bit cipher feedback */
    cfb_decrypt(ctx, input, inputlen, output, iv);
    break;
  case MODE_OFB:		/* 128-bit output feedback */
    ofb_encrypt(ctx, input, inputlen, output, iv);
    break;
  case MODE_CTR:		/* counter */
    ctr_encrypt(ctx, input, inputlen, output, iv);

rijndael.h  view on Meta::CPAN

#include <stdlib.h>
#include <sys/types.h>

/* Other block sizes and key lengths are possible, but in the context of
 * the ssh protocols, 256 bits is the default. 
 */
#define RIJNDAEL_BLOCKSIZE 16
#define RIJNDAEL_KEYSIZE   32

#define     MODE_ECB        1    /*  Are we ciphering in ECB mode?   */
#define     MODE_CBC        2    /*  Are we ciphering in CBC mode?   */
#define     MODE_CFB        3    /*  Are we ciphering in 128-bit CFB mode? */
#define     MODE_PCBC       4    /*  Are we ciphering in PCBC mode? */
#define     MODE_OFB        5    /*  Are we ciphering in 128-bit OFB mode? */
#define     MODE_CTR        6    /*  Are we ciphering in counter mode? */

/* Allow keys of size 128 <= bits <= 256 */

#define RIJNDAEL_MIN_KEYSIZE 16
#define RIJNDAEL_MAX_KEYSIZE 32

typedef struct {
  uint32_t keys[60];		/* maximum size of key schedule */

t/00_load.t  view on Meta::CPAN

#! perl
use strict;
use warnings;

use Test::More tests => 40;
use Crypt::Rijndael;

my %flag_for = (
	ecb => Crypt::Rijndael::MODE_ECB,
	cbc => Crypt::Rijndael::MODE_CBC,
	cfb => Crypt::Rijndael::MODE_CFB,
	ofb => Crypt::Rijndael::MODE_OFB,
	ctr => Crypt::Rijndael::MODE_CTR,
);

sub is_crypted {
	my %args = @_;
	local $Test::Builder::Level = $Test::Builder::Level + 1;

	my $cipher = Crypt::Rijndael->new(pack('H*', $args{key}), $flag_for{ $args{mode} });

t/00_load.t  view on Meta::CPAN


is_crypted(
	name => 'ECB-AES-256',
	key => "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
	mode => 'ecb',
	plaintext => "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710",
	ciphertext => "f3eed1bdb5d2a03c064b5a7e3db181f8591ccb10d410ed26dc5ba74a31362870b6ed21b99ca6f4f9f153e7b1beafed1d23304b7a39f9f3ff067d8d8f9e24ecc7",
);

is_crypted(
	name => 'CBC-AES-128',
	key => "2b7e151628aed2a6abf7158809cf4f3c",
	mode => 'cbc',
	iv => "000102030405060708090a0b0c0d0e0f",
	plaintext => "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710",
	ciphertext => "7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b273bed6b8e3c1743b7116e69e222295163ff1caa1681fac09120eca307586e1a7",
);

is_crypted(
	name => 'CBC-AES-192',
	key => "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b",
	mode => 'cbc',
	iv => "000102030405060708090a0b0c0d0e0f",
	plaintext => "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710",
	ciphertext => "4f021db243bc633d7178183a9fa071e8b4d9ada9ad7dedf4e5e738763f69145a571b242012fb7ae07fa9baac3df102e008b0e27988598881d920a9e64f5615cd",
);

is_crypted(
	name => 'CBC-AES-256',
	key => "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
	mode => 'cbc',
	iv => "000102030405060708090a0b0c0d0e0f",
	plaintext => "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710",
	ciphertext => "f58c4c04d6e5f1ba779eabfb5f7bfbd69cfc4e967edb808d679f777bc6702c7d39f23369a9d9bacfa530e26304231461b2eb05e2c39be9fcda6c19078c6a9d1b",
);

is_crypted(
	name => 'CFB-AES-128',
	key => "2b7e151628aed2a6abf7158809cf4f3c",

t/modes.t  view on Meta::CPAN

#!/usr/bin/perl

use Test::More tests => 90;

use Crypt::Rijndael;

ok(defined &Crypt::Rijndael::blocksize);
is(Crypt::Rijndael->blocksize, 16);

foreach my $a ( 0 .. 10 ) {
	my $hash = crypt_decrypt(Crypt::Rijndael::MODE_CBC);
	is($hash->{plain}, $hash->{data}, "Decrypted text matches plain text for cbc-$a");
}

foreach my $a ( 0 .. 10 ) {
	my $hash = crypt_decrypt(Crypt::Rijndael::MODE_CFB);
	is($hash->{plain}, $hash->{data}, "Decrypted text matches plain text for cfb-$a");
}

foreach my $a ( 0 .. 10 ) {
	my ($plain, $data) = crypt_decrypt_partial(Crypt::Rijndael::MODE_CFB);

t/modes.t  view on Meta::CPAN

	my $hash = crypt_decrypt(Crypt::Rijndael::MODE_OFB );
	is($hash->{plain}, $hash->{data}, "Decrypted text matches plain text for ofb-$a");
}

foreach my $a ( 0 .. 10 ) {
	my ($plain, $data) = crypt_decrypt_partial(Crypt::Rijndael::MODE_OFB);
	is($plain, $data, "Decrypted text matches plain text for ofb-$a-partial");
}

TODO: {
	todo_skip "PCBC is not a legal mode (yet)", 11;
	
	foreach my $a ( 0 .. 10 ) {
		my $hash = crypt_decrypt(Crypt::Rijndael::MODE_PCBC);
		is($hash->{plain}, $hash->{data}, "Decrypted text matches plain text");
	}

};

sub crypt_decrypt {
	my $mode   = shift;

	my $key    = make_string(32);
	my $c      = Crypt::Rijndael->new($key, $mode);

t/rt/27632.t  view on Meta::CPAN

use Digest::MD5 qw(md5_hex);

use Test::More 'no_plan';

my $class = 'Crypt::Rijndael';

my $key       = 'abcdefghijklmnop';

my $in_plain  = 'a' x 32;

my $cipher = $class->new( $key, Crypt::Rijndael::MODE_CBC ); 
isa_ok( $cipher, $class );

$cipher->set_iv('a' x 16); 

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# encrypt
diag( "-" x 50 ) if $ENV{DEBUG};

my $crypt  = $cipher->encrypt( $in_plain );



( run in 0.890 second using v1.01-cache-2.11-cpan-e1769b4cff6 )