line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
/* rijndael - An implementation of the Rijndael cipher. |
2
|
|
|
|
|
|
|
* Copyright (C) 2000, 2001 Rafael R. Sevilla |
3
|
|
|
|
|
|
|
* |
4
|
|
|
|
|
|
|
* Currently maintained by brian d foy, |
5
|
|
|
|
|
|
|
* |
6
|
|
|
|
|
|
|
* This library is free software; you can redistribute it and/or |
7
|
|
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public |
8
|
|
|
|
|
|
|
* License as published by the Free Software Foundation; either |
9
|
|
|
|
|
|
|
* version 2 of the License, or (at your option) any later version. |
10
|
|
|
|
|
|
|
* |
11
|
|
|
|
|
|
|
* This library is distributed in the hope that it will be useful, |
12
|
|
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14
|
|
|
|
|
|
|
* Library General Public License for more details. |
15
|
|
|
|
|
|
|
* |
16
|
|
|
|
|
|
|
* You should have received a copy of the GNU Library General Public |
17
|
|
|
|
|
|
|
* License along with this library; if not, write to the Free |
18
|
|
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19
|
|
|
|
|
|
|
*/ |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
#define PERL_NO_GET_CONTEXT |
22
|
|
|
|
|
|
|
#define NEED_newCONSTSUB |
23
|
|
|
|
|
|
|
#define NEED_sv_2pv_flags |
24
|
|
|
|
|
|
|
#include "EXTERN.h" |
25
|
|
|
|
|
|
|
#include "perl.h" |
26
|
|
|
|
|
|
|
#include "XSUB.h" |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
#include "ppport.h" |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
#include "rijndael.h" |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
typedef struct cryptstate { |
33
|
|
|
|
|
|
|
RIJNDAEL_context ctx; |
34
|
|
|
|
|
|
|
UINT8 iv[RIJNDAEL_BLOCKSIZE]; |
35
|
|
|
|
|
|
|
int mode; |
36
|
|
|
|
|
|
|
} *Crypt__Rijndael; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
MODULE = Crypt::Rijndael PACKAGE = Crypt::Rijndael |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
PROTOTYPES: DISABLE |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
BOOT: |
43
|
|
|
|
|
|
|
{ |
44
|
3
|
|
|
|
|
|
HV *stash = gv_stashpvs("Crypt::Rijndael", GV_ADD); |
45
|
|
|
|
|
|
|
|
46
|
3
|
|
|
|
|
|
newCONSTSUB(stash, "keysize", newSVuv(32) ); |
47
|
3
|
|
|
|
|
|
newCONSTSUB(stash, "blocksize", newSVuv(16) ); |
48
|
3
|
|
|
|
|
|
newCONSTSUB(stash, "MODE_ECB", newSVuv(MODE_ECB) ); |
49
|
3
|
|
|
|
|
|
newCONSTSUB(stash, "MODE_CBC", newSVuv(MODE_CBC) ); |
50
|
3
|
|
|
|
|
|
newCONSTSUB(stash, "MODE_CFB", newSVuv(MODE_CFB) ); |
51
|
3
|
|
|
|
|
|
newCONSTSUB(stash, "MODE_PCBC", newSVuv(MODE_PCBC) ); |
52
|
3
|
|
|
|
|
|
newCONSTSUB(stash, "MODE_OFB", newSVuv(MODE_OFB) ); |
53
|
3
|
|
|
|
|
|
newCONSTSUB(stash, "MODE_CTR", newSVuv(MODE_CTR) ); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Crypt::Rijndael |
57
|
|
|
|
|
|
|
new(class, key, mode=MODE_ECB) |
58
|
|
|
|
|
|
|
SV * class |
59
|
|
|
|
|
|
|
SV * key |
60
|
|
|
|
|
|
|
int mode |
61
|
|
|
|
|
|
|
CODE: |
62
|
|
|
|
|
|
|
{ |
63
|
|
|
|
|
|
|
STRLEN keysize; |
64
|
|
|
|
|
|
|
|
65
|
76
|
50
|
|
|
|
|
if (!SvPOK (key)) |
66
|
0
|
|
|
|
|
|
Perl_croak(aTHX_ "key must be an untainted string scalar"); |
67
|
|
|
|
|
|
|
|
68
|
76
|
|
|
|
|
|
keysize = SvCUR(key); |
69
|
|
|
|
|
|
|
|
70
|
76
|
100
|
|
|
|
|
if (keysize != 16 && keysize != 24 && keysize != 32) |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
Perl_croak(aTHX_ "wrong key length: key must be 128, 192 or 256 bits long"); |
72
|
76
|
100
|
|
|
|
|
if (mode != MODE_ECB && mode != MODE_CBC && mode != MODE_CFB && mode != MODE_OFB && mode != MODE_CTR) |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
Perl_croak(aTHX_ "illegal mode, see documentation for valid modes"); |
74
|
|
|
|
|
|
|
|
75
|
76
|
|
|
|
|
|
Newz(0, RETVAL, 1, struct cryptstate); |
76
|
76
|
|
|
|
|
|
RETVAL->ctx.mode = RETVAL->mode = mode; |
77
|
|
|
|
|
|
|
/* set the IV to zero on initialization */ |
78
|
76
|
|
|
|
|
|
Zero(RETVAL->iv, RIJNDAEL_BLOCKSIZE, char); |
79
|
76
|
50
|
|
|
|
|
rijndael_setup(&RETVAL->ctx, keysize, (UINT8 *) SvPV_nolen(key)); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
OUTPUT: |
82
|
|
|
|
|
|
|
RETVAL |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
SV * |
85
|
|
|
|
|
|
|
set_iv(self, data) |
86
|
|
|
|
|
|
|
Crypt::Rijndael self |
87
|
|
|
|
|
|
|
SV * data |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
CODE: |
90
|
|
|
|
|
|
|
{ |
91
|
|
|
|
|
|
|
SV *res; |
92
|
|
|
|
|
|
|
STRLEN size; |
93
|
13
|
50
|
|
|
|
|
void *rawbytes = SvPV(data,size); |
94
|
|
|
|
|
|
|
|
95
|
13
|
50
|
|
|
|
|
if( size != RIJNDAEL_BLOCKSIZE ) |
96
|
0
|
|
|
|
|
|
Perl_croak(aTHX_ "set_iv: initial value must be the blocksize (%d bytes), but was %d bytes", RIJNDAEL_BLOCKSIZE, size); |
97
|
13
|
|
|
|
|
|
Copy(rawbytes, self->iv, RIJNDAEL_BLOCKSIZE, char); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
SV * |
101
|
|
|
|
|
|
|
encrypt(self, data) |
102
|
|
|
|
|
|
|
Crypt::Rijndael self |
103
|
|
|
|
|
|
|
SV * data |
104
|
|
|
|
|
|
|
ALIAS: |
105
|
|
|
|
|
|
|
decrypt = 1 |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
CODE: |
108
|
|
|
|
|
|
|
{ |
109
|
|
|
|
|
|
|
SV *res; |
110
|
|
|
|
|
|
|
STRLEN size; |
111
|
152
|
50
|
|
|
|
|
void *rawbytes = SvPV(data,size); |
112
|
|
|
|
|
|
|
|
113
|
152
|
50
|
|
|
|
|
if (size) { |
114
|
|
|
|
|
|
|
UINT8* buffer; |
115
|
|
|
|
|
|
|
|
116
|
152
|
50
|
|
|
|
|
if (size % RIJNDAEL_BLOCKSIZE) |
117
|
0
|
|
|
|
|
|
Perl_croak(aTHX_ "encrypt: datasize not multiple of blocksize (%d bytes)", RIJNDAEL_BLOCKSIZE); |
118
|
|
|
|
|
|
|
|
119
|
152
|
|
|
|
|
|
RETVAL = newSV(size); |
120
|
152
|
|
|
|
|
|
SvPOK_only(RETVAL); |
121
|
152
|
|
|
|
|
|
SvCUR_set(RETVAL, size); |
122
|
152
|
50
|
|
|
|
|
buffer = (UINT8 *)SvPV_nolen(RETVAL); |
123
|
152
|
100
|
|
|
|
|
(ix ? block_decrypt : block_encrypt) |
124
|
152
|
|
|
|
|
|
(&self->ctx, rawbytes, size, buffer, self->iv); |
125
|
152
|
|
|
|
|
|
buffer[size] = '\0'; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
else |
128
|
0
|
|
|
|
|
|
RETVAL = newSVpv ("", 0); |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
OUTPUT: |
131
|
|
|
|
|
|
|
RETVAL |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
void |
135
|
|
|
|
|
|
|
DESTROY(self) |
136
|
|
|
|
|
|
|
Crypt::Rijndael self |
137
|
|
|
|
|
|
|
CODE: |
138
|
76
|
|
|
|
|
|
Safefree(self); |