MD2.xs

filter: Statementstmt 64.5% 93 / 144 Branchbran 34.3% 22 / 64 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 55.2% 115 / 208 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Digest/MD2.pm »
1 /*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the same terms as Perl itself.
4 *
5 * Copyright 1998-2003 Gisle Aas.
6 * Copyright 1990-1992 RSA Data Security, Inc.
7 *
8 * This code is derived from the reference implementation in RFC 1231
9 * which comes with this message:
10 *
11 * Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
12 * rights reserved.
13 *
14 * License to copy and use this software is granted for
15 * non-commercial Internet Privacy-Enhanced Mail provided that it is
16 * identified as the "RSA Data Security, Inc. MD2 Message Digest
17 * Algorithm" in all material mentioning or referencing this software
18 * or this function.
19 *
20 * RSA Data Security, Inc. makes no representations concerning either
21 * the merchantability of this software or the suitability of this
22 * software for any particular purpose. It is provided "as is"
23 * without express or implied warranty of any kind.
24 *
25 * These notices must be retained in any copies of any part of this
26 * documentation and/or software.
27  
28 */
29  
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 #include "EXTERN.h"
34 #include "perl.h"
35 #include "XSUB.h"
36 #ifdef __cplusplus
37 }
38 #endif
39  
40 #ifndef PERL_VERSION
41 # include <patchlevel.h>
42 # if !(defined(PERL_VERSION) || (SUBVERSION > 0 && defined(PATCHLEVEL)))
43 # include <could_not_find_Perl_patchlevel.h>
44 # endif
45 # define PERL_REVISION 5
46 # define PERL_VERSION PATCHLEVEL
47 # define PERL_SUBVERSION SUBVERSION
48 #endif
49  
50 #if PERL_VERSION <= 4 && !defined(PL_dowarn)
51 #define PL_dowarn dowarn
52 #endif
53  
54 #ifdef G_WARN_ON
55 #define DOWARN (PL_dowarn & G_WARN_ON)
56 #else
57 #define DOWARN PL_dowarn
58 #endif
59  
60  
61 #ifdef SvPVbyte
62 #if PERL_REVISION == 5 && PERL_VERSION < 7
63 /* SvPVbyte does not work in perl-5.6.1, borrowed version for 5.7.3 */
64 #undef SvPVbyte
65 #define SvPVbyte(sv, lp) \
66 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK) \
67 ? ((lp = SvCUR(sv)), SvPVX(sv)) : my_sv_2pvbyte(aTHX_ sv, &lp))
68  
69 static char *
70 my_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
71 {
72 sv_utf8_downgrade(sv,0);
73 return SvPV(sv,*lp);
74 }
75 #endif
76 #else
77 #define SvPVbyte SvPV
78 #endif
79  
80 typedef struct {
81 unsigned char state[16]; /* state */
82 unsigned char checksum[16]; /* checksum */
83 unsigned int count; /* number of bytes, modulo 16 */
84 unsigned char buffer[16]; /* input buffer */
85 } MD2_CTX;
86  
87  
88 /* Permutation of 0..255 constructed from the digits of pi. It gives a
89 "random" nonlinear byte substitution operation.
90 */
91 static U8 PI_SUBST[256] = {
92 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
93 19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
94 76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
95 138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
96 245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
97 148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
98 39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
99 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
100 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
101 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
102 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
103 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
104 234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
105 129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
106 8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
107 203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
108 166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
109 31, 26, 219, 153, 141, 51, 159, 17, 131, 20
110 };
111  
112 static U8 *PADDING[] = {
113 (U8 *)"",
114 (U8 *)"\001",
115 (U8 *)"\002\002",
116 (U8 *)"\003\003\003",
117 (U8 *)"\004\004\004\004",
118 (U8 *)"\005\005\005\005\005",
119 (U8 *)"\006\006\006\006\006\006",
120 (U8 *)"\007\007\007\007\007\007\007",
121 (U8 *)"\010\010\010\010\010\010\010\010",
122 (U8 *)"\011\011\011\011\011\011\011\011\011",
123 (U8 *)"\012\012\012\012\012\012\012\012\012\012",
124 (U8 *)"\013\013\013\013\013\013\013\013\013\013\013",
125 (U8 *)"\014\014\014\014\014\014\014\014\014\014\014\014",
126 (U8 *)"\015\015\015\015\015\015\015\015\015\015\015\015\015",
127 (U8 *)"\016\016\016\016\016\016\016\016\016\016\016\016\016\016",
128 (U8 *)"\017\017\017\017\017\017\017\017\017\017\017\017\017\017\017",
129 (U8 *)"\020\020\020\020\020\020\020\020\020\020\020\020\020\020\020\020"
130 };
131  
132  
133 static void
134 43 MD2Init(MD2_CTX* context)
135 {
136 43 Zero(context, 1, MD2_CTX);
137 43 context->count = 0;
138 43 }
139  
140  
141 static void
142 92 MD2Transform (U8* state, U8* checksum, U8* block)
143 {
144 unsigned int i, j, t;
145 U8 x[48];
146  
147 /* Form encryption block from state, block, state ^ block.
148 */
149 92 Copy(state, x, 16, char);
150 92 Copy(block, x+16, 16, char);
151  
152 1564 ▶ for (i = 0; i < 16; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 16; i++) 1472 92 1564
153 1472 x[i+32] = state[i] ^ block[i];
154  
155 /* Encrypt block (18 rounds).
156 */
157 92 t = 0;
158 1748 ▶ for (i = 0; i < 18; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 18; i++) { 1656 92 1748
159 81144 ▶ for (j = 0; j < 48; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < 48; j++) 79488 1656 81144
160 79488 t = x[j] ^= PI_SUBST[t];
161 1656 t = (t + i) & 0xff;
162 }
163  
164 92 Copy(x, state, 16, char); /* Save new state */
165  
166 /* Update checksum.
167 */
168 92 t = checksum[15];
169 1564 ▶ for (i = 0; i < 16; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 16; i++) 1472 92 1564
170 1472 t = checksum[i] ^= PI_SUBST[block[i] ^ t];
171  
172 92 Zero(x, 1, x); /* Zeroize sensitive information. */
173 92 }
174  
175  
176 static void
177 84 MD2Update (MD2_CTX* context, U8 *input, STRLEN inputLen)
178 {
179 unsigned int i, index, partLen;
180  
181 /* Update number of bytes mod 16 */
182 84 index = context->count;
183 84 context->count = (index + inputLen) & 0xf;
184  
185 84 partLen = 16 - index;
186  
187 /* Transform as many times as possible.
188 */
189 84 ▶ if (inputLen >= partLen) {
Branch: 1 branch
BranchTrueFalseTotal
if (inputLen >= partLen) { 68 16 84
190 68 Copy(input, context->buffer+index, partLen, char);
191 68 MD2Transform (context->state, context->checksum, context->buffer);
192  
193 92 ▶ for (i = partLen; i + 15 < inputLen; i += 16)
Branch: 1 branch
BranchTrueFalseTotal
for (i = partLen; i + 15 < inputLen; i += 16) 24 68 92
194 24 MD2Transform (context->state, context->checksum, &input[i]);
195  
196 68 index = 0;
197 }
198 else
199 16 i = 0;
200  
201 /* Buffer remaining input */
202 84 Copy(input+i, context->buffer + index, inputLen-i, char);
203 84 }
204  
205  
206 static void
207 28 MD2Final (U8* digest, MD2_CTX *context)
208 {
209 unsigned int index, padLen;
210  
211 /* Pad out to multiple of 16 */
212 28 index = context->count;
213 28 padLen = 16 - index;
214 28 MD2Update (context, PADDING[padLen], padLen);
215  
216 /* Extend with checksum */
217 28 MD2Update (context, context->checksum, 16);
218  
219 28 Copy(context->state, digest, 16, char); /* Store state in digest */
220 28 Zero(context, 1, MD2_CTX); /* Zeroize sensitive information. */
221 28 }
222  
223 /*----------------------------------------------------------------*/
224 #ifndef INT2PTR
225 #define INT2PTR(any,d) (any)(d)
226 #endif
227  
228  
229 44 static MD2_CTX* get_md2_ctx(SV* sv)
230 {
231 44 ▶ if (sv_derived_from(sv, "Digest::MD2"))
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(sv, "Digest::MD2")) 44 0 44
232 44 return INT2PTR(MD2_CTX*, SvIV(SvRV(sv)));
233 0 croak("Not a reference to a Digest::MD2 object");
234 return (MD2_CTX*)0; /* some compilers insist on a return value */
235 }
236  
237  
238 14 static char* hex_16(const unsigned char* from, char* to)
239 {
240 static char *hexdigits = "0123456789abcdef";
241 14 const unsigned char *end = from + 16;
242 14 char *d = to;
243  
244 238 ▶ while (from < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (from < end) { 224 14 238
245 224 *d++ = hexdigits[(*from >> 4)];
246 224 *d++ = hexdigits[(*from & 0x0F)];
247 224 from++;
248 }
249 14 *d = '\0';
250 14 return to;
251 }
252  
253 0 static char* base64_16(const unsigned char* from, char* to)
254 {
255 static char* base64 =
256 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
257 0 const unsigned char *end = from + 16;
258 unsigned char c1, c2, c3;
259 0 char *d = to;
260  
261 while (1) {
262 0 c1 = *from++;
263 0 *d++ = base64[c1>>2];
264 0 ▶ if (from == end) {
Branch: 1 branch
BranchTrueFalseTotal
if (from == end) { 0 0 0
265 0 *d++ = base64[(c1 & 0x3) << 4];
266 0 break;
267 }
268 0 c2 = *from++;
269 0 c3 = *from++;
270 0 *d++ = base64[((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)];
271 0 *d++ = base64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)];
272 0 *d++ = base64[c3 & 0x3F];
273 }
274 0 *d = '\0';
275 0 return to;
276 }
277  
278 /* Formats */
279 #define F_BIN 0
280 #define F_HEX 1
281 #define F_B64 2
282  
283 28 static SV* make_mortal_sv(const unsigned char *src, int type)
284 {
285 STRLEN len;
286 char result[33];
287 char *ret;
288
289 28 switch (type) {
290 14 case F_BIN:
291 14 ret = (char*)src;
292 14 len = 16;
293 14 break;
294 14 case F_HEX:
295 14 ret = hex_16(src, result);
296 14 len = 32;
297 14 break;
298 0 case F_B64:
299 0 ret = base64_16(src, result);
300 0 len = 22;
301 0 break;
302 0 default:
303 0 croak("Bad convertion type (%d)", type);
304 break;
305 }
306 28 return sv_2mortal(newSVpv(ret,len));
307 }
308  
309  
310 /********************************************************************/
311  
312 typedef PerlIO* InputStream;
313  
314 MODULE = Digest::MD2 PACKAGE = Digest::MD2
315  
316 PROTOTYPES: DISABLE
317  
318 void
319 new(xclass)
320 SV* xclass
321 PREINIT:
322 MD2_CTX* context;
323 PPCODE:
324 15 ▶ if (!SvROK(xclass)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(xclass)) { 15 0 15
325 STRLEN my_na;
326 15 char *sclass = SvPV(xclass, my_na);
327 15 New(55, context, 1, MD2_CTX);
328 15 ST(0) = sv_newmortal();
329 15 sv_setref_pv(ST(0), sclass, (void*)context);
330 15 SvREADONLY_on(SvRV(ST(0)));
331 } else {
332 0 context = get_md2_ctx(xclass);
333 }
334 15 MD2Init(context);
335 15 XSRETURN(1);
336  
337 void
338 clone(self)
339 SV* self
340 PREINIT:
341 0 MD2_CTX* cont = get_md2_ctx(self);
342 0 char *myname = sv_reftype(SvRV(self),TRUE);
343 MD2_CTX* context;
344 PPCODE:
345 STRLEN my_na;
346 0 New(55, context, 1, MD2_CTX);
347 0 ST(0) = sv_newmortal();
348 0 sv_setref_pv(ST(0), myname , (void*)context);
349 0 SvREADONLY_on(SvRV(ST(0)));
350 0 memcpy(context,cont,sizeof(MD2_CTX));
351 0 XSRETURN(1);
352  
353 void
354 DESTROY(context)
355 MD2_CTX* context
356 CODE:
357 15 Safefree(context);
358  
359 void
360 add(self, ...)
361 SV* self
362 PREINIT:
363 14 MD2_CTX* context = get_md2_ctx(self);
364 int i;
365 unsigned char *data;
366 STRLEN len;
367 PPCODE:
368 28 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 14 14 28
369 14 data = (unsigned char *)(SvPVbyte(ST(i), len));
370 14 MD2Update(context, data, len);
371 }
372 14 XSRETURN(1); /* self */
373  
374 void
375 addfile(self, fh)
376 SV* self
377 InputStream fh
378 PREINIT:
379 1 MD2_CTX* context = get_md2_ctx(self);
380 unsigned char buffer[4096];
381 int n;
382 CODE:
383 1 ▶ if (fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (fh) { 0 1 1
384 /* Process blocks until EOF */
385 0 ▶ while ( (n = PerlIO_read(fh, buffer, sizeof(buffer))) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while ( (n = PerlIO_read(fh, buffer, sizeof(buffer))) > 0) { 0 0 0
386 0 MD2Update(context, buffer, n);
387 }
388 0 ▶ if (PerlIO_error(fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (PerlIO_error(fh)) { 0 0 0
389 0 croak("Reading from filehandle failed");
390 }
391 }
392 else {
393 1 croak("No filehandle passed");
394 }
395 0 XSRETURN(1); /* self */
396  
397 void
398 digest(context)
399 MD2_CTX* context
400 ALIAS:
401 Digest::MD2::digest = F_BIN
402 Digest::MD2::hexdigest = F_HEX
403 Digest::MD2::b64digest = F_B64
404 PREINIT:
405 unsigned char digeststr[16];
406 PPCODE:
407 14 MD2Final(digeststr, context);
408 14 MD2Init(context); /* In case it is reused */
409 14 ST(0) = make_mortal_sv(digeststr, ix);
410 14 XSRETURN(1);
411  
412 void
413 md2(...)
414 ALIAS:
415 Digest::MD2::md2 = F_BIN
416 Digest::MD2::md2_hex = F_HEX
417 Digest::MD2::md2_base64 = F_B64
418 PREINIT:
419 MD2_CTX ctx;
420 int i;
421 unsigned char *data;
422 STRLEN len;
423 unsigned char digeststr[16];
424 PPCODE:
425 14 MD2Init(&ctx);
426  
427 14 ▶ if (DOWARN) {
Branch: 1 branch
BranchTrueFalseTotal
if (DOWARN) { 0 14 14
428 0 char *msg = 0;
429 0 ▶ if (items == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 1) { 0 0 0
430 0 ▶ if (SvROK(ST(0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(ST(0))) { 0 0 0
431 0 SV* sv = SvRV(ST(0));
432 0 ▶ if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD2"))
Branch: 8 branches
BranchTrueFalseTotal
if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD2")) 0 0 0
if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD2")) 0 0 0
if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD2")) 0 0 0
if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD2")) 0 0 0
if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD2")) 0 0 0
if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD2")) 0 0 0
if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD2")) 0 0 0
if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD2")) 0 0 0
433 0 msg = "probably called as method";
434 else
435 0 msg = "called with reference argument";
436 }
437 }
438 0 ▶ else if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (items > 1) { 0 0 0
439 0 data = (unsigned char *)SvPVbyte(ST(0), len);
440 0 ▶ if (len == 11 && memEQ("Digest::MD2", data, 11)) {
Branch: 2 branches
BranchTrueFalseTotal
if (len == 11 && memEQ("Digest::MD2", data, 11)) { 0 0 0
if (len == 11 && memEQ("Digest::MD2", data, 11)) { 0 0 0
441 0 msg = "probably called as class method";
442 }
443 }
444 0 ▶ if (msg) {
Branch: 1 branch
BranchTrueFalseTotal
if (msg) { 0 0 0
445 0 ▶ char *f = (ix == F_BIN) ? "md2" :
Branch: 1 branch
BranchTrueFalseTotal
char *f = (ix == F_BIN) ? "md2" : 0 0 0
446 0 ▶ (ix == F_HEX) ? "md2_hex" : "md2_base64";
Branch: 1 branch
BranchTrueFalseTotal
(ix == F_HEX) ? "md2_hex" : "md2_base64"; 0 0 0
447 0 warn("&Digest::MD2::%s function %s", f, msg);
448 }
449 }
450  
451 28 ▶ for (i = 0; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < items; i++) { 14 14 28
452 14 data = (unsigned char *)(SvPVbyte(ST(i), len));
453 14 MD2Update(&ctx, data, len);
454 }
455 14 MD2Final(digeststr, &ctx);
456 14 ST(0) = make_mortal_sv(digeststr, ix);
457 14 XSRETURN(1);
↑ Summary blib/lib/Digest/MD2.pm »