1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
/* rijndael - An implementation of the Rijndael cipher.
* Copyright (C) 2000, 2001 Rafael R. Sevilla <sevillar@team.ph.inter.net>
*
* Currently maintained by brian d foy, <bdfoy@cpan.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
/* try to be compatible with older perls */
/* SvPV_nolen() macro first defined in 5.005_55 */
/* this is slow, not threadsafe, but works */
#include "patchlevel.h"
#if (PATCHLEVEL == 4) || ((PATCHLEVEL == 5) && (SUBVERSION < 55))
static STRLEN nolen_na;
# define SvPV_nolen(sv) SvPV ((sv), nolen_na)
#endif
#include "rijndael.h"
typedef struct cryptstate {
RIJNDAEL_context ctx;
UINT8 iv[RIJNDAEL_BLOCKSIZE];
int mode;
} *Crypt__Rijndael;
MODULE = Crypt::Rijndael PACKAGE = Crypt::Rijndael
PROTOTYPES: ENABLE
# newCONSTSUB is here as of 5.004_70
BOOT:
{
#if (PATCHLEVEL > 4) || ((PATCHLEVEL == 4) && (SUBVERSION >= 70))
HV *stash = gv_stashpv("Crypt::Rijndael", 0);
newCONSTSUB (stash, "keysize", newSViv (32) );
newCONSTSUB (stash, "blocksize", newSViv (16) );
newCONSTSUB (stash, "MODE_ECB", newSViv (MODE_ECB) );
newCONSTSUB (stash, "MODE_CBC", newSViv (MODE_CBC) );
newCONSTSUB (stash, "MODE_CFB", newSViv (MODE_CFB) );
newCONSTSUB (stash, "MODE_PCBC", newSViv (MODE_PCBC) );
newCONSTSUB (stash, "MODE_OFB", newSViv (MODE_OFB) );
newCONSTSUB (stash, "MODE_CTR", newSViv (MODE_CTR) );
#endif
}
#if (PATCHLEVEL < 4) || ((PATCHLEVEL == 4) && (SUBVERSION < 70))
int
keysize(...)
CODE:
RETVAL=32;
OUTPUT:
RETVAL
int
blocksize(...)
CODE:
RETVAL=16;
OUTPUT:
RETVAL
int
MODE_ECB(...)
CODE:
RETVAL=MODE_ECB;
OUTPUT:
RETVAL
int
MODE_CBC(...)
CODE:
RETVAL=MODE_CBC;
OUTPUT:
RETVAL
int
MODE_CFB(...)
CODE:
RETVAL=MODE_CFB;
OUTPUT:
RETVAL
int
MODE_PCBC(...)
CODE:
RETVAL=MODE_PCBC;
OUTPUT:
RETVAL
int
MODE_OFB(...)
CODE:
RETVAL=MODE_OFB;
OUTPUT:
RETVAL
int
MODE_CTR(...)
CODE:
RETVAL=MODE_CTR;
OUTPUT:
RETVAL
#endif
Crypt::Rijndael
new(class, key, mode=MODE_ECB)
SV * class
SV * key
int mode
CODE:
{
STRLEN keysize;
if (!SvPOK (key))
croak("key must be an untainted string scalar");
keysize = SvCUR(key);
if (keysize != 16 && keysize != 24 && keysize != 32)
croak ("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)
croak ("illegal mode, see documentation for valid modes");
Newz(0, RETVAL, 1, struct cryptstate);
RETVAL->ctx.mode = RETVAL->mode = mode;
/* set the IV to zero on initialization */
memset(RETVAL->iv, 0, RIJNDAEL_BLOCKSIZE);
rijndael_setup(&RETVAL->ctx, keysize, (UINT8 *) SvPV_nolen(key));
}
OUTPUT:
RETVAL
SV *
set_iv(self, data)
Crypt::Rijndael self
SV * data
CODE:
{
SV *res;
STRLEN size;
void *rawbytes = SvPV(data,size);
if( size != RIJNDAEL_BLOCKSIZE )
croak( "set_iv: initial value must be the blocksize (%d bytes), but was %d bytes", RIJNDAEL_BLOCKSIZE, size );
memcpy(self->iv, rawbytes, RIJNDAEL_BLOCKSIZE);
}
SV *
encrypt(self, data)
Crypt::Rijndael self
SV * data
ALIAS:
decrypt = 1
CODE:
{
SV *res;
STRLEN size;
void *rawbytes = SvPV(data,size);
if (size) {
if (size % RIJNDAEL_BLOCKSIZE)
croak ("encrypt: datasize not multiple of blocksize (%d bytes)", RIJNDAEL_BLOCKSIZE);
RETVAL = NEWSV (0, size);
SvPOK_only (RETVAL);
SvCUR_set (RETVAL, size);
(ix ? block_decrypt : block_encrypt)
(&self->ctx, rawbytes, size, (UINT8 *) SvPV_nolen(RETVAL), self->iv);
} else
RETVAL = newSVpv ("", 0);
}
OUTPUT:
RETVAL
void
DESTROY(self)
Crypt::Rijndael self
CODE:
Safefree(self);
|