Rijndael.xs

filter: Statementstmt 1.9% 2 / 102 Branchbran 1.4% 1 / 68 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 1.7% 3 / 170 SCAR 0
CC
0
Cov
0%
CRAP 0
« sha64bit.c ↑ Summary blib/lib/Filter/Rijndael.pm »
1 /*
2 * Filename : Rijndael.xs
3 *
4 */
5  
6 #include "EXTERN.h"
7 #include "perl.h"
8 #include "XSUB.h"
9  
10 #include "_rijndael.h"
11 #include "_rijndael.c"
12 #include "Rijndael.h"
13 #include "sha.c"
14 #include "sha.h"
15 #include <stdio.h>
16  
17 #ifdef FDEBUG
18 static int fdebug = 0;
19 #endif
20  
21 #ifndef INT2PTR
22 #define INT2PTR(p, i) (p) (i)
23 #endif
24  
25 /* constants specific to the encryption format */
26 #define HEADERSIZE 6
27 #define BLOCKSIZE 16
28 #define CHECKSUMSIZE 20
29 #define KEYSIZE 32
30 #define SHAALGORITHM 0
31 #define SHAMAXWRITESIZE 16384
32  
33 unsigned char checksum[CHECKSUMSIZE];
34 UINT8 iv[BLOCKSIZE];
35 RIJNDAEL_context ctx;
36  
37 static int ix2alg[] = {1,224,256,384,512,512224,512256};
38  
39 #define SET_LEN(sv,len) \
40 do { SvPVX(sv)[len] = '\0'; SvCUR_set(sv, len); } while (0)
41  
42 /* Internal defines */
43 #ifdef PERL_FILTER_EXISTS
44 # define CORE_FILTER_COUNT \
45 (PL_parser && PL_parser->rsfp_filters ? av_len(PL_parser->rsfp_filters) : 0)
46 # define PL_rsfp (PL_parser ? PL_parser->rsfp : (PerlIO *) 0)
47 #else
48 # define CORE_FILTER_COUNT \
49 (PL_rsfp_filters ? av_len(PL_rsfp_filters) : 0)
50 #endif
51  
52 #define FILTER_COUNT(s) IoPAGE(s)
53 #define FILTER_LINE_NO(s) IoLINES(s)
54 #define FIRST_TIME(s) IoLINES_LEFT(s)
55  
56 #define ENCRYPT_GV(s) IoTOP_GV(s)
57 #define ENCRYPT_SV(s) ((SV*) ENCRYPT_GV(s))
58 #define ENCRYPT_BUFFER(s) SvPVX(ENCRYPT_SV(s))
59 #define CLEAR_ENCRYPT_SV(s) SvCUR_set(ENCRYPT_SV(s), 0)
60  
61 #define DECRYPT_SV(s) s
62 #define DECRYPT_BUFFER(s) SvPVX(DECRYPT_SV(s))
63 #define CLEAR_DECRYPT_SV(s) SvCUR_set(DECRYPT_SV(s), 0)
64 #define DECRYPT_BUFFER_LEN(s) SvCUR(DECRYPT_SV(s))
65 #define DECRYPT_OFFSET(s) IoPAGE_LEN(s)
66 #define SET_DECRYPT_BUFFER_LEN(s,n) SvCUR_set(DECRYPT_SV(s), n)
67  
68 static unsigned
69 0 Decrypt(SV *in_sv, SV *out_sv)
70 {
71 /* Here is where the actual decryption takes place */
72  
73 0 unsigned char * in_buffer = (unsigned char *) SvPVX(in_sv) ;
74 unsigned char * out_buffer ;
75 0 unsigned size = SvCUR(in_sv) ;
76 0 unsigned index = size ;
77 int i ;
78  
79 /* make certain that the output buffer is big enough */
80 /* as the output from the decryption can never be larger than */
81 /* the input buffer, make it that size */
82 0 ▶ SvGROW(out_sv, size) ;
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(out_sv, size) ; 0 0 0
SvGROW(out_sv, size) ; 0 0 0
83 0 out_buffer = (unsigned char *) SvPVX(out_sv) ;
84  
85 /* call rijndael block_decrypt */
86 0 block_decrypt( &ctx, in_buffer, size, out_buffer, iv );
87  
88 /* input has been consumed, so set length to 0 */
89 0 SET_LEN(in_sv, 0) ;
90  
91 /* set decrypt buffer length */
92 0 SET_LEN(out_sv, index) ;
93  
94 /* return the size of the decrypt buffer */
95 0 return (index) ;
96 }
97  
98  
99 0 static int ReadBlock(int idx, SV *sv, unsigned int size) {
100 /* read *exactly* size bytes from the next filter */
101 0 unsigned int i = size;
102 0 while (1) {
103 0 unsigned int n = FILTER_READ(idx, sv, i) ;
104  
105 0 ▶ if (n <= 0 && i==size) /* eof/error when nothing read so far */
Branch: 2 branches
BranchTrueFalseTotal
if (n <= 0 && i==size) /* eof/error when nothing read so far */ 0 0 0
if (n <= 0 && i==size) /* eof/error when nothing read so far */ 0 0 0
106 0 return n ;
107 0 ▶ if (n <= 0) /* eof/error when something already read */
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) /* eof/error when something already read */ 0 0 0
108 0 return size - i;
109 0 ▶ if (n == i)
Branch: 1 branch
BranchTrueFalseTotal
if (n == i) 0 0 0
110 0 return size ;
111 0 i -= n ;
112 }
113 }
114  
115 static void
116 0 preDecrypt(int idx)
117 {
118 /* If the encrypted data starts with a header or needs to do some
119 initialisation it can be done here
120  
121 In this case the encrypted data has to start with a fingerprint,
122 so that is checked.
123 */
124  
125 0 ▶ SV * sv = FILTER_DATA(idx) ;
Branch: 1 branch
BranchTrueFalseTotal
SV * sv = FILTER_DATA(idx) ; 0 0 0
126 unsigned char * buffer ;
127 int i;
128 SHA *state;
129 char *result;
130 I32 bytesread;
131 unsigned char data[SHAMAXWRITESIZE];
132 0 bzero( data, SHAMAXWRITESIZE );
133 0 unsigned char unknown_file_type[] = "Unknown file type";
134  
135 /* read the header */
136 0 ▶ if (ReadBlock(idx+1, sv, HEADERSIZE + CHECKSUMSIZE) != HEADERSIZE + CHECKSUMSIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (ReadBlock(idx+1, sv, HEADERSIZE + CHECKSUMSIZE) != HEADERSIZE + CHECKSUMSIZE) 0 0 0
137 0 croak("truncated file") ;
138  
139 0 buffer = (unsigned char *) SvPVX(sv);
140  
141 /* check for fingerprint of encrypted data */
142 0 ▶ for( i = 0; i < HEADERSIZE; i++ )
Branch: 1 branch
BranchTrueFalseTotal
for( i = 0; i < HEADERSIZE; i++ ) 0 0 0
143 0 ▶ if( buffer[i] != HEADER[i] )
Branch: 1 branch
BranchTrueFalseTotal
if( buffer[i] != HEADER[i] ) 0 0 0
144 0 croak( (unsigned char *)unknown_file_type );
145  
146 /* get file checksum */
147 0 ▶ for( i = HEADERSIZE; i < HEADERSIZE + CHECKSUMSIZE; i++ )
Branch: 1 branch
BranchTrueFalseTotal
for( i = HEADERSIZE; i < HEADERSIZE + CHECKSUMSIZE; i++ ) 0 0 0
148 0 checksum[i-HEADERSIZE] = buffer[i];
149  
150 /* read file size */
151 0 ▶ int current_location = PerlIO_tell( PL_rsfp );
Branch: 1 branch
BranchTrueFalseTotal
int current_location = PerlIO_tell( PL_rsfp ); 0 0 0
152  
153 /* calculate the checksum */
154 0 ▶ if ( ( state = shaopen( ix2alg[SHAALGORITHM] ) ) == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( ( state = shaopen( ix2alg[SHAALGORITHM] ) ) == NULL ) 0 0 0
155 0 croak( "Unknown checksum algorithm" );
156  
157 0 ▶ while(( bytesread = PerlIO_read( PL_rsfp, data, SHAMAXWRITESIZE ) ) > 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
while(( bytesread = PerlIO_read( PL_rsfp, data, SHAMAXWRITESIZE ) ) > 0 ) { 0 0 0
while(( bytesread = PerlIO_read( PL_rsfp, data, SHAMAXWRITESIZE ) ) > 0 ) { 0 0 0
158 0 shawrite( data, bytesread << 3, state );
159 }
160 0 shafinish(state);
161  
162 0 result = (char *) shadigest(state);
163  
164 /* check the checksum */
165 0 ▶ for( i = 0; i < shadsize(state); i++ )
Branch: 1 branch
BranchTrueFalseTotal
for( i = 0; i < shadsize(state); i++ ) 0 0 0
166 0 ▶ if( checksum[i] != ( result[i] & 0x000000FF ) )
Branch: 1 branch
BranchTrueFalseTotal
if( checksum[i] != ( result[i] & 0x000000FF ) ) 0 0 0
167 0 croak( (unsigned char *)unknown_file_type );
168  
169 0 shaclose(state);
170  
171 /* set back file position */
172 0 ▶ PerlIO_seek( PL_rsfp, current_location, 0 );
Branch: 1 branch
BranchTrueFalseTotal
PerlIO_seek( PL_rsfp, current_location, 0 ); 0 0 0
173  
174 /* initialize Rijndael */
175 0 ctx.mode = MODE_CBC;
176 0 memset( iv, 0, BLOCKSIZE );
177 0 rijndael_setup( &ctx, KEYSIZE, KEY );
178 0 }
179  
180 static void
181 0 postDecrypt()
182 {
183 0 }
184  
185 static I32
186 0 filter_decrypt(pTHX_ int idx, SV *buf_sv, int maxlen)
187 {
188 0 ▶ SV *my_sv = FILTER_DATA(idx);
Branch: 1 branch
BranchTrueFalseTotal
SV *my_sv = FILTER_DATA(idx); 0 0 0
189 0 char *nl = "\n";
190 char *p;
191 char *out_ptr;
192 int n;
193  
194 /* check if this is the first time through */
195 0 ▶ if (FIRST_TIME(my_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (FIRST_TIME(my_sv)) { 0 0 0
196  
197 /* Mild paranoia mode - make sure that no extra filters have */
198 /* been applied on the same line as the use Filter::decrypt */
199 0 ▶ if (CORE_FILTER_COUNT > FILTER_COUNT(my_sv) )
Branch: 3 branches
BranchTrueFalseTotal
if (CORE_FILTER_COUNT > FILTER_COUNT(my_sv) ) 0 0 0
if (CORE_FILTER_COUNT > FILTER_COUNT(my_sv) ) 0 0 0
if (CORE_FILTER_COUNT > FILTER_COUNT(my_sv) ) 0 0 0
200 0 croak("too many filters") ;
201  
202 /* As this is the first time through, so deal with any */
203 /* initialisation required */
204 0 preDecrypt(idx) ;
205  
206 0 FIRST_TIME(my_sv) = FALSE ;
207 0 SET_LEN(DECRYPT_SV(my_sv), 0) ;
208 0 SET_LEN(ENCRYPT_SV(my_sv), 0) ;
209 0 DECRYPT_OFFSET(my_sv) = 0 ;
210  
211 }
212  
213 #ifdef FDEBUG
214 if (fdebug)
215 warn("**** In filter_decrypt - maxlen = %d, len buf = %d idx = %d\n",
216 maxlen, SvCUR(buf_sv), idx ) ;
217 #endif
218  
219 while (1) {
220  
221 /* anything left from last time */
222 0 ▶ if ((n = SvCUR(DECRYPT_SV(my_sv)))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((n = SvCUR(DECRYPT_SV(my_sv)))) { 0 0 0
223  
224 0 out_ptr = SvPVX(DECRYPT_SV(my_sv)) + DECRYPT_OFFSET(my_sv) ;
225  
226 0 ▶ if (maxlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (maxlen) { 0 0 0
227 /* want a block */
228 #ifdef FDEBUG
229 if (fdebug)
230 warn("BLOCK(%d): size = %d, maxlen = %d\n",
231 idx, n, maxlen) ;
232 #endif
233  
234 0 sv_catpvn(buf_sv, out_ptr, maxlen > n ? n : maxlen );
235 0 ▶ if(n <= maxlen) {
Branch: 1 branch
BranchTrueFalseTotal
if(n <= maxlen) { 0 0 0
236 0 DECRYPT_OFFSET(my_sv) = 0 ;
237 0 SET_LEN(DECRYPT_SV(my_sv), 0) ;
238 }
239 else {
240 0 DECRYPT_OFFSET(my_sv) += maxlen ;
241 0 SvCUR_set(DECRYPT_SV(my_sv), n - maxlen) ;
242 }
243 0 return SvCUR(buf_sv);
244 }
245 else {
246 /* want lines */
247 0 ▶ if ((p = ninstr(out_ptr, out_ptr + n, nl, nl + 1))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((p = ninstr(out_ptr, out_ptr + n, nl, nl + 1))) { 0 0 0
248  
249 0 sv_catpvn(buf_sv, out_ptr, p - out_ptr + 1);
250  
251 0 n = n - (p - out_ptr + 1);
252 0 DECRYPT_OFFSET(my_sv) += (p - out_ptr + 1) ;
253 0 SvCUR_set(DECRYPT_SV(my_sv), n) ;
254 #ifdef FDEBUG
255 if (fdebug)
256 warn("recycle %d - leaving %d, returning %d [%.999s]",
257 idx, n, SvCUR(buf_sv), SvPVX(buf_sv)) ;
258 #endif
259  
260 0 return SvCUR(buf_sv);
261 }
262 else /* no EOL, so append the complete buffer */
263 0 sv_catpvn(buf_sv, out_ptr, n) ;
264 }
265
266 }
267  
268  
269 0 SET_LEN(DECRYPT_SV(my_sv), 0) ;
270 0 DECRYPT_OFFSET(my_sv) = 0 ;
271  
272 /* read from the file into the encrypt buffer */
273 0 ▶ if ( (n = ReadBlock(idx+1, ENCRYPT_SV(my_sv), BLOCKSIZE)) <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if ( (n = ReadBlock(idx+1, ENCRYPT_SV(my_sv), BLOCKSIZE)) <= 0) 0 0 0
274 {
275 /* Either EOF or an error */
276  
277 #ifdef FDEBUG
278 if (fdebug)
279 warn ("filter_read %d returned %d , returning %d\n", idx, n,
280 (SvCUR(buf_sv)>0) ? SvCUR(buf_sv) : n);
281 #endif
282  
283 /* If the decrypt code needs to tidy up on EOF/error,
284 now is the time - here is a hook */
285 0 postDecrypt() ;
286  
287 0 filter_del(filter_decrypt);
288  
289
290 /* If error, return the code */
291 0 ▶ if (n < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) 0 0 0
292 0 return n ;
293  
294 /* return what we have so far else signal eof */
295 0 ▶ return (SvCUR(buf_sv)>0) ? SvCUR(buf_sv) : n;
Branch: 1 branch
BranchTrueFalseTotal
return (SvCUR(buf_sv)>0) ? SvCUR(buf_sv) : n; 0 0 0
296 }
297  
298 #ifdef FDEBUG
299 if (fdebug)
300 warn(" filter_decrypt(%d): sub-filter returned %d: '%.999s'",
301 idx, n, SvPV(my_sv,PL_na));
302 #endif
303  
304 /* Now decrypt a block */
305 0 n = Decrypt(ENCRYPT_SV(my_sv), DECRYPT_SV(my_sv)) ;
306  
307 #ifdef FDEBUG
308 if (fdebug)
309 warn("Decrypt (%d) returned %d [%.999s]\n", idx, n, SvPVX(DECRYPT_SV(my_sv)) ) ;
310 #endif
311  
312 }
313 }
314  
315  
316 MODULE = Filter::Rijndael PACKAGE = Filter::Rijndael
317  
318 PROTOTYPES: DISABLE
319  
320 BOOT:
321 /* Check for the presence of the Perl Compiler */
322 1 ▶ if (get_av("B::NULL::ISA",0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (get_av("B::NULL::ISA",0)) { 1 0 1
323 1 croak("Aborting, Compiler detected") ;
324 }
325 #ifndef BYPASS
326 /* Don't run if this module is dynamically linked */
327 if (!isALPHA(SvPV(GvSV(CvFILEGV(cv)), PL_na)[0]))
328 croak("module is dynamically linked. Recompile as a static module") ;
329 #ifdef DEBUGGING
330 /* Don't run if compiled with DEBUGGING */
331 croak("recompile without -DDEBUGGING") ;
332 #endif
333
334 /* Double check that DEBUGGING hasn't been enabled */
335 if (PL_debug)
336 croak("debugging flags detected") ;
337 #endif
338  
339  
340 void
341 import(module)
342 SV * module
343 PPCODE:
344 {
345  
346 0 SV * sv = newSV(BLOCKSIZE) ;
347  
348 /* make sure the Perl debugger isn't enabled */
349 0 ▶ if( PL_perldb )
Branch: 1 branch
BranchTrueFalseTotal
if( PL_perldb ) 0 0 0
350 0 croak("debugger disabled") ;
351  
352 0 filter_add(filter_decrypt, sv) ;
353 0 FIRST_TIME(sv) = TRUE ;
354  
355 0 ENCRYPT_GV(sv) = (GV*) newSV(BLOCKSIZE) ;
356 0 (void)SvPOK_only(DECRYPT_SV(sv));
357 0 (void)SvPOK_only(ENCRYPT_SV(sv));
358 0 SET_LEN(DECRYPT_SV(sv), 0) ;
359 0 SET_LEN(ENCRYPT_SV(sv), 0) ;
360  
361  
362 /* remember how many filters are enabled */
363 0 ▶ FILTER_COUNT(sv) = CORE_FILTER_COUNT ;
Branch: 2 branches
BranchTrueFalseTotal
FILTER_COUNT(sv) = CORE_FILTER_COUNT ; 0 0 0
FILTER_COUNT(sv) = CORE_FILTER_COUNT ; 0 0 0
364 /* and the line number */
365 0 FILTER_LINE_NO(sv) = PL_curcop->cop_line ;
366  
367 }
368  
369 void
370 unimport(...)
371 PPCODE:
372 /* filter_del(filter_decrypt); */
« sha64bit.c ↑ Summary blib/lib/Filter/Rijndael.pm »