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_t iv[RIJNDAEL_BLOCKSIZE]; |
35
|
|
|
|
|
|
|
} *Crypt__Rijndael; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
typedef const char* IVEC; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
MODULE = Crypt::Rijndael PACKAGE = Crypt::Rijndael |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
PROTOTYPES: DISABLE |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
BOOT: |
44
|
|
|
|
|
|
|
{ |
45
|
3
|
|
|
|
|
|
HV *stash = gv_stashpvs("Crypt::Rijndael", GV_ADD); |
46
|
|
|
|
|
|
|
|
47
|
3
|
|
|
|
|
|
newCONSTSUB(stash, "keysize", newSVuv(32) ); |
48
|
3
|
|
|
|
|
|
newCONSTSUB(stash, "blocksize", newSVuv(16) ); |
49
|
3
|
|
|
|
|
|
newCONSTSUB(stash, "MODE_ECB", newSVuv(MODE_ECB) ); |
50
|
3
|
|
|
|
|
|
newCONSTSUB(stash, "MODE_CBC", newSVuv(MODE_CBC) ); |
51
|
3
|
|
|
|
|
|
newCONSTSUB(stash, "MODE_CFB", newSVuv(MODE_CFB) ); |
52
|
3
|
|
|
|
|
|
newCONSTSUB(stash, "MODE_PCBC", newSVuv(MODE_PCBC) ); |
53
|
3
|
|
|
|
|
|
newCONSTSUB(stash, "MODE_OFB", newSVuv(MODE_OFB) ); |
54
|
3
|
|
|
|
|
|
newCONSTSUB(stash, "MODE_CTR", newSVuv(MODE_CTR) ); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
Crypt::Rijndael |
58
|
|
|
|
|
|
|
new(class, key, mode=MODE_ECB) |
59
|
|
|
|
|
|
|
SV * class |
60
|
|
|
|
|
|
|
SV * key |
61
|
|
|
|
|
|
|
int mode |
62
|
|
|
|
|
|
|
PREINIT: |
63
|
|
|
|
|
|
|
STRLEN keysize; |
64
|
|
|
|
|
|
|
CODE: |
65
|
98
|
50
|
|
|
|
|
if (!SvPOK(key)) |
66
|
0
|
|
|
|
|
|
Perl_croak(aTHX_ "Key must be an string scalar"); |
67
|
98
|
50
|
|
|
|
|
if (SvTAINTED(key)) |
|
|
0
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
Perl_croak(aTHX_ "Key must be untainted"); |
69
|
|
|
|
|
|
|
|
70
|
98
|
|
|
|
|
|
keysize = SvCUR(key); |
71
|
|
|
|
|
|
|
|
72
|
98
|
100
|
|
|
|
|
if (keysize != 16 && keysize != 24 && keysize != 32) |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
Perl_croak(aTHX_ "Wrong key length: key must be 128, 192 or 256 bits long"); |
74
|
98
|
100
|
|
|
|
|
if (mode != MODE_ECB && mode != MODE_CBC && mode != MODE_CFB && mode != MODE_OFB && mode != MODE_CTR) |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
Perl_croak(aTHX_ "Illegal mode, see documentation for valid modes"); |
76
|
|
|
|
|
|
|
|
77
|
98
|
|
|
|
|
|
Newxz(RETVAL, 1, struct cryptstate); |
78
|
98
|
|
|
|
|
|
RETVAL->ctx.mode = mode; |
79
|
98
|
50
|
|
|
|
|
rijndael_setup(&RETVAL->ctx, keysize, (uint8_t *) SvPVbyte_nolen(key)); |
80
|
|
|
|
|
|
|
OUTPUT: |
81
|
|
|
|
|
|
|
RETVAL |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
SV * |
84
|
|
|
|
|
|
|
set_iv(self, data) |
85
|
|
|
|
|
|
|
Crypt::Rijndael self |
86
|
|
|
|
|
|
|
IVEC data |
87
|
|
|
|
|
|
|
CODE: |
88
|
13
|
|
|
|
|
|
Copy(data, self->iv, RIJNDAEL_BLOCKSIZE, char); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
SV * |
92
|
|
|
|
|
|
|
encrypt(self, data, iv = self->iv) |
93
|
|
|
|
|
|
|
Crypt::Rijndael self |
94
|
|
|
|
|
|
|
SV * data |
95
|
|
|
|
|
|
|
IVEC iv |
96
|
|
|
|
|
|
|
ALIAS: |
97
|
|
|
|
|
|
|
decrypt = 1 |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
CODE: |
100
|
|
|
|
|
|
|
{ |
101
|
|
|
|
|
|
|
SV *res; |
102
|
|
|
|
|
|
|
STRLEN size; |
103
|
196
|
50
|
|
|
|
|
void *rawbytes = SvPVbyte(data,size); |
104
|
|
|
|
|
|
|
|
105
|
196
|
50
|
|
|
|
|
if (size) { |
106
|
|
|
|
|
|
|
uint8_t* buffer; |
107
|
|
|
|
|
|
|
|
108
|
196
|
100
|
|
|
|
|
if ((self->ctx.mode == MODE_ECB || self->ctx.mode == MODE_CBC) && size % RIJNDAEL_BLOCKSIZE) |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
Perl_croak(aTHX_ "encrypt: datasize not multiple of blocksize (%d bytes)", RIJNDAEL_BLOCKSIZE); |
110
|
|
|
|
|
|
|
|
111
|
196
|
|
|
|
|
|
RETVAL = newSV(size); |
112
|
196
|
|
|
|
|
|
SvPOK_only(RETVAL); |
113
|
196
|
|
|
|
|
|
SvCUR_set(RETVAL, size); |
114
|
196
|
50
|
|
|
|
|
buffer = (uint8_t *)SvPVbyte_nolen(RETVAL); |
115
|
196
|
100
|
|
|
|
|
(ix ? block_decrypt : block_encrypt) |
116
|
196
|
|
|
|
|
|
(&self->ctx, rawbytes, size, buffer, iv); |
117
|
196
|
|
|
|
|
|
buffer[size] = '\0'; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
else |
120
|
0
|
|
|
|
|
|
RETVAL = newSVpv ("", 0); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
OUTPUT: |
123
|
|
|
|
|
|
|
RETVAL |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
void |
127
|
|
|
|
|
|
|
DESTROY(self) |
128
|
|
|
|
|
|
|
Crypt::Rijndael self |
129
|
|
|
|
|
|
|
CODE: |
130
|
98
|
|
|
|
|
|
Safefree(self); |