| 1 |
|
| /* $Id: MD4.xs,v 1.4 2013/03/14 05:51:35 mikem Exp mikem $ */ |
| 2 |
|
| |
| 3 |
|
| /* |
| 4 |
|
| * This library is free software; you can redistribute it and/or |
| 5 |
|
| * modify it under the same terms as Perl itself. |
| 6 |
|
| * |
| 7 |
|
| * Copyright 1998-2000 Gisle Aas. |
| 8 |
|
| * Copyright 1995-1996 Neil Winton. |
| 9 |
|
| * Copyright 1991-1992 RSA Data Security, Inc. |
| 10 |
|
| * |
| 11 |
|
| * This code is derived from Neil Winton's MD4-1.7 Perl module, |
| 12 |
|
| * and Gisle Aas's Digest::MD5 module. The MD4 algorithm is |
| 13 |
|
| * derived from the reference implementation in RFC 1320 which |
| 14 |
|
| * comes with this message: |
| 15 |
|
| * |
| 16 |
|
| * Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved. |
| 17 |
|
| * |
| 18 |
|
| * License to copy and use this software is granted provided that it |
| 19 |
|
| * is identified as the "RSA Data Security, Inc. MD4 Message-Digest |
| 20 |
|
| * Algorithm" in all material mentioning or referencing this software |
| 21 |
|
| * or this function. |
| 22 |
|
| * |
| 23 |
|
| * License is also granted to make and use derivative works provided |
| 24 |
|
| * that such works are identified as "derived from the RSA Data |
| 25 |
|
| * Security, Inc. MD4 Message-Digest Algorithm" in all material |
| 26 |
|
| * mentioning or referencing the derived work. |
| 27 |
|
| * |
| 28 |
|
| * RSA Data Security, Inc. makes no representations concerning either |
| 29 |
|
| * the merchantability of this software or the suitability of this |
| 30 |
|
| * software for any particular purpose. It is provided "as is" |
| 31 |
|
| * without express or implied warranty of any kind. |
| 32 |
|
| * |
| 33 |
|
| * These notices must be retained in any copies of any part of this |
| 34 |
|
| * documentation and/or software. |
| 35 |
|
| */ |
| 36 |
|
| |
| 37 |
|
| #ifdef __cplusplus |
| 38 |
|
| extern "C" { |
| 39 |
|
| #endif |
| 40 |
|
| #include "EXTERN.h" |
| 41 |
|
| #include "perl.h" |
| 42 |
|
| #include "XSUB.h" |
| 43 |
|
| #ifdef __cplusplus |
| 44 |
|
| } |
| 45 |
|
| #endif |
| 46 |
|
| |
| 47 |
|
| #ifndef PERL_VERSION |
| 48 |
|
| # include <patchlevel.h> |
| 49 |
|
| # if !(defined(PERL_VERSION) || (SUBVERSION > 0 && defined(PATCHLEVEL))) |
| 50 |
|
| # include <could_not_find_Perl_patchlevel.h> |
| 51 |
|
| # endif |
| 52 |
|
| # define PERL_REVISION 5 |
| 53 |
|
| # define PERL_VERSION PATCHLEVEL |
| 54 |
|
| # define PERL_SUBVERSION SUBVERSION |
| 55 |
|
| #endif |
| 56 |
|
| |
| 57 |
|
| #if PERL_VERSION <= 4 && !defined(PL_dowarn) |
| 58 |
|
| #define PL_dowarn dowarn |
| 59 |
|
| #endif |
| 60 |
|
| |
| 61 |
|
| #ifdef G_WARN_ON |
| 62 |
|
| #define DOWARN (PL_dowarn & G_WARN_ON) |
| 63 |
|
| #else |
| 64 |
|
| #define DOWARN PL_dowarn |
| 65 |
|
| #endif |
| 66 |
|
| |
| 67 |
|
| #ifdef SvPVbyte |
| 68 |
|
| #if PERL_REVISION == 5 && PERL_VERSION < 7 |
| 69 |
|
| /* SvPVbyte does not work in perl-5.6.1, borrowed version for 5.7.3 */ |
| 70 |
|
| #undef SvPVbyte |
| 71 |
|
| #define SvPVbyte(sv, lp) \ |
| 72 |
|
| ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK) \ |
| 73 |
|
| ? ((lp = SvCUR(sv)), SvPVX(sv)) : my_sv_2pvbyte(aTHX_ sv, &lp)) |
| 74 |
|
| |
| 75 |
|
| static char * |
| 76 |
|
| my_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp) |
| 77 |
|
| { |
| 78 |
|
| sv_utf8_downgrade(sv,0); |
| 79 |
|
| return SvPV(sv,*lp); |
| 80 |
|
| } |
| 81 |
|
| #endif |
| 82 |
|
| #else |
| 83 |
|
| #define SvPVbyte SvPV |
| 84 |
|
| #endif |
| 85 |
|
| |
| 86 |
|
| /* Perl does not guarantee that U32 is exactly 32 bits. Some system |
| 87 |
|
| * has no integral type with exactly 32 bits. For instance, A Cray has |
| 88 |
|
| * short, int and long all at 64 bits so we need to apply this macro |
| 89 |
|
| * to reduce U32 values to 32 bits at appropriate places. If U32 |
| 90 |
|
| * really does have 32 bits then this is a no-op. |
| 91 |
|
| */ |
| 92 |
|
| #if BYTEORDER > 0x4321 || defined(TRUNCATE_U32) |
| 93 |
|
| #define TRUNC32(x) ((x) &= 0xFFFFffff) |
| 94 |
|
| #else |
| 95 |
|
| #define TRUNC32(x) /*nothing*/ |
| 96 |
|
| #endif |
| 97 |
|
| |
| 98 |
|
| #define MD4_CTX_SIGNATURE 200003166 |
| 99 |
|
| |
| 100 |
|
| /* This stucture keeps the current state of algorithm. |
| 101 |
|
| */ |
| 102 |
|
| typedef struct { |
| 103 |
|
| U32 signature; /* safer cast in get_md4_ctx() */ |
| 104 |
|
| U32 state[4]; /* state (ABCD) */ |
| 105 |
|
| U32 count[2]; /* number of bits, modulo 2^64 (lsb first) */ |
| 106 |
|
| unsigned char buffer[64]; /* input buffer */ |
| 107 |
|
| } MD4_CTX; |
| 108 |
|
| |
| 109 |
|
| #define POINTER void* |
| 110 |
|
| |
| 111 |
|
| /* Padding is added at the end of the message in order to fill a |
| 112 |
|
| * complete 64 byte block (- 8 bytes for the message length). The |
| 113 |
|
| * padding is also the reason the buffer in MD4_CTX have to be |
| 114 |
|
| * 128 bytes. |
| 115 |
|
| */ |
| 116 |
|
| static unsigned char PADDING[64] = { |
| 117 |
|
| 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 118 |
|
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 119 |
|
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 120 |
|
| }; |
| 121 |
|
| |
| 122 |
|
| /* Constants for MD4Transform routine. |
| 123 |
|
| */ |
| 124 |
|
| #define S11 3 |
| 125 |
|
| #define S12 7 |
| 126 |
|
| #define S13 11 |
| 127 |
|
| #define S14 19 |
| 128 |
|
| #define S21 3 |
| 129 |
|
| #define S22 5 |
| 130 |
|
| #define S23 9 |
| 131 |
|
| #define S24 13 |
| 132 |
|
| #define S31 3 |
| 133 |
|
| #define S32 9 |
| 134 |
|
| #define S33 11 |
| 135 |
|
| #define S34 15 |
| 136 |
|
| |
| 137 |
|
| /* F, G, H and I are basic MD4 functions. |
| 138 |
|
| */ |
| 139 |
|
| #define F(x, y, z) (((x) & (y)) | ((~x) & (z))) |
| 140 |
|
| #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) |
| 141 |
|
| #define H(x, y, z) ((x) ^ (y) ^ (z)) |
| 142 |
|
| |
| 143 |
|
| /* ROTATE_LEFT rotates x left n bits. |
| 144 |
|
| */ |
| 145 |
|
| #define ROTATE_LEFT(x, n) (((x) << (n) | ((x) >> (32-(n))))) |
| 146 |
|
| |
| 147 |
|
| /* FF, GG and HH are transformations for rounds 1, 2 and 3 */ |
| 148 |
|
| /* Rotation is separate from addition to prevent recomputation */ |
| 149 |
|
| #define FF(a, b, c, d, x, s) { \ |
| 150 |
|
| (a) += F ((b), (c), (d)) + (x); \ |
| 151 |
|
| TRUNC32((a)); \ |
| 152 |
|
| (a) = ROTATE_LEFT ((a), (s)); \ |
| 153 |
|
| TRUNC32((a)); \ |
| 154 |
|
| } |
| 155 |
|
| #define GG(a, b, c, d, x, s) { \ |
| 156 |
|
| (a) += G ((b), (c), (d)) + (x) + (U32)0x5a827999; \ |
| 157 |
|
| TRUNC32((a)); \ |
| 158 |
|
| (a) = ROTATE_LEFT ((a), (s)); \ |
| 159 |
|
| TRUNC32((a)); \ |
| 160 |
|
| } |
| 161 |
|
| #define HH(a, b, c, d, x, s) { \ |
| 162 |
|
| (a) += H ((b), (c), (d)) + (x) + (U32)0x6ed9eba1; \ |
| 163 |
|
| TRUNC32((a)); \ |
| 164 |
|
| (a) = ROTATE_LEFT ((a), (s)); \ |
| 165 |
|
| TRUNC32((a)); \ |
| 166 |
|
| } |
| 167 |
|
| |
| 168 |
|
| /* Encodes input (UINT4) into output (unsigned char). Assumes len is |
| 169 |
|
| a multiple of 4. |
| 170 |
|
| */ |
| 171 |
1174 |
| static void Encode (output, input, len) |
| 172 |
|
| unsigned char *output; |
| 173 |
|
| U32 *input; |
| 174 |
|
| unsigned int len; |
| 175 |
|
| { |
| 176 |
|
| unsigned int i, j; |
| 177 |
|
| |
| 178 |
4696 |
▶ | for (i = 0, j = 0; j < len; i++, j += 4) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0, j = 0; j < len; i++, j += 4) { |
3522 |
1174 |
4696 |
|
| 179 |
3522 |
| output[j] = (unsigned char)(input[i] & 0xff); |
| 180 |
3522 |
| output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); |
| 181 |
3522 |
| output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); |
| 182 |
3522 |
| output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); |
| 183 |
|
| } |
| 184 |
1174 |
| } |
| 185 |
|
| |
| 186 |
|
| /* Decodes input (unsigned char) into output (UINT4). Assumes len is |
| 187 |
|
| a multiple of 4. |
| 188 |
|
| */ |
| 189 |
12719 |
| static void Decode (output, input, len) |
| 190 |
|
| |
| 191 |
|
| U32 *output; |
| 192 |
|
| unsigned char *input; |
| 193 |
|
| unsigned int len; |
| 194 |
|
| { |
| 195 |
|
| unsigned int i, j; |
| 196 |
|
| |
| 197 |
216223 |
▶ | for (i = 0, j = 0; j < len; i++, j += 4) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0, j = 0; j < len; i++, j += 4) |
203504 |
12719 |
216223 |
|
| 198 |
203504 |
| output[i] = ((U32)input[j]) | (((U32)input[j+1]) << 8) | |
| 199 |
203504 |
| (((U32)input[j+2]) << 16) | (((U32)input[j+3]) << 24); |
| 200 |
12719 |
| } |
| 201 |
|
| |
| 202 |
|
| static void |
| 203 |
887 |
| MD4Init(MD4_CTX *context) |
| 204 |
|
| { |
| 205 |
887 |
| context->count[0] = context->count[1] = 0; |
| 206 |
|
| |
| 207 |
|
| /* Load magic initialization constants. |
| 208 |
|
| */ |
| 209 |
887 |
| context->state[0] = 0x67452301; |
| 210 |
887 |
| context->state[1] = 0xefcdab89; |
| 211 |
887 |
| context->state[2] = 0x98badcfe; |
| 212 |
887 |
| context->state[3] = 0x10325476; |
| 213 |
887 |
| } |
| 214 |
|
| |
| 215 |
|
| |
| 216 |
|
| /* MD4 basic transformation. Transforms state based on block. |
| 217 |
|
| */ |
| 218 |
12719 |
| static void MD4Transform (state, block) |
| 219 |
|
| U32 state[4]; |
| 220 |
|
| unsigned char block[64]; |
| 221 |
|
| { |
| 222 |
12719 |
| U32 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; |
| 223 |
|
| |
| 224 |
12719 |
| Decode (x, block, 64); |
| 225 |
|
| |
| 226 |
|
| /* Round 1 */ |
| 227 |
12719 |
| FF (a, b, c, d, x[ 0], S11); /* 1 */ |
| 228 |
12719 |
| FF (d, a, b, c, x[ 1], S12); /* 2 */ |
| 229 |
12719 |
| FF (c, d, a, b, x[ 2], S13); /* 3 */ |
| 230 |
12719 |
| FF (b, c, d, a, x[ 3], S14); /* 4 */ |
| 231 |
12719 |
| FF (a, b, c, d, x[ 4], S11); /* 5 */ |
| 232 |
12719 |
| FF (d, a, b, c, x[ 5], S12); /* 6 */ |
| 233 |
12719 |
| FF (c, d, a, b, x[ 6], S13); /* 7 */ |
| 234 |
12719 |
| FF (b, c, d, a, x[ 7], S14); /* 8 */ |
| 235 |
12719 |
| FF (a, b, c, d, x[ 8], S11); /* 9 */ |
| 236 |
12719 |
| FF (d, a, b, c, x[ 9], S12); /* 10 */ |
| 237 |
12719 |
| FF (c, d, a, b, x[10], S13); /* 11 */ |
| 238 |
12719 |
| FF (b, c, d, a, x[11], S14); /* 12 */ |
| 239 |
12719 |
| FF (a, b, c, d, x[12], S11); /* 13 */ |
| 240 |
12719 |
| FF (d, a, b, c, x[13], S12); /* 14 */ |
| 241 |
12719 |
| FF (c, d, a, b, x[14], S13); /* 15 */ |
| 242 |
12719 |
| FF (b, c, d, a, x[15], S14); /* 16 */ |
| 243 |
|
| |
| 244 |
|
| /* Round 2 */ |
| 245 |
12719 |
| GG (a, b, c, d, x[ 0], S21); /* 17 */ |
| 246 |
12719 |
| GG (d, a, b, c, x[ 4], S22); /* 18 */ |
| 247 |
12719 |
| GG (c, d, a, b, x[ 8], S23); /* 19 */ |
| 248 |
12719 |
| GG (b, c, d, a, x[12], S24); /* 20 */ |
| 249 |
12719 |
| GG (a, b, c, d, x[ 1], S21); /* 21 */ |
| 250 |
12719 |
| GG (d, a, b, c, x[ 5], S22); /* 22 */ |
| 251 |
12719 |
| GG (c, d, a, b, x[ 9], S23); /* 23 */ |
| 252 |
12719 |
| GG (b, c, d, a, x[13], S24); /* 24 */ |
| 253 |
12719 |
| GG (a, b, c, d, x[ 2], S21); /* 25 */ |
| 254 |
12719 |
| GG (d, a, b, c, x[ 6], S22); /* 26 */ |
| 255 |
12719 |
| GG (c, d, a, b, x[10], S23); /* 27 */ |
| 256 |
12719 |
| GG (b, c, d, a, x[14], S24); /* 28 */ |
| 257 |
12719 |
| GG (a, b, c, d, x[ 3], S21); /* 29 */ |
| 258 |
12719 |
| GG (d, a, b, c, x[ 7], S22); /* 30 */ |
| 259 |
12719 |
| GG (c, d, a, b, x[11], S23); /* 31 */ |
| 260 |
12719 |
| GG (b, c, d, a, x[15], S24); /* 32 */ |
| 261 |
|
| |
| 262 |
|
| /* Round 3 */ |
| 263 |
12719 |
| HH (a, b, c, d, x[ 0], S31); /* 33 */ |
| 264 |
12719 |
| HH (d, a, b, c, x[ 8], S32); /* 34 */ |
| 265 |
12719 |
| HH (c, d, a, b, x[ 4], S33); /* 35 */ |
| 266 |
12719 |
| HH (b, c, d, a, x[12], S34); /* 36 */ |
| 267 |
12719 |
| HH (a, b, c, d, x[ 2], S31); /* 37 */ |
| 268 |
12719 |
| HH (d, a, b, c, x[10], S32); /* 38 */ |
| 269 |
12719 |
| HH (c, d, a, b, x[ 6], S33); /* 39 */ |
| 270 |
12719 |
| HH (b, c, d, a, x[14], S34); /* 40 */ |
| 271 |
12719 |
| HH (a, b, c, d, x[ 1], S31); /* 41 */ |
| 272 |
12719 |
| HH (d, a, b, c, x[ 9], S32); /* 42 */ |
| 273 |
12719 |
| HH (c, d, a, b, x[ 5], S33); /* 43 */ |
| 274 |
12719 |
| HH (b, c, d, a, x[13], S34); /* 44 */ |
| 275 |
12719 |
| HH (a, b, c, d, x[ 3], S31); /* 45 */ |
| 276 |
12719 |
| HH (d, a, b, c, x[11], S32); /* 46 */ |
| 277 |
12719 |
| HH (c, d, a, b, x[ 7], S33); /* 47 */ |
| 278 |
12719 |
| HH (b, c, d, a, x[15], S34); /* 48 */ |
| 279 |
|
| |
| 280 |
12719 |
| state[0] += a; TRUNC32(state[0]); |
| 281 |
12719 |
| state[1] += b; TRUNC32(state[1]); |
| 282 |
12719 |
| state[2] += c; TRUNC32(state[2]); |
| 283 |
12719 |
| state[3] += d; TRUNC32(state[3]); |
| 284 |
12719 |
| } |
| 285 |
|
| |
| 286 |
|
| #ifdef MD4_DEBUG |
| 287 |
|
| static char* |
| 288 |
|
| ctx_dump(MD4_CTX* ctx) |
| 289 |
|
| { |
| 290 |
|
| static char buf[1024]; |
| 291 |
|
| sprintf(buf, "{A=%x,B=%x,C=%x,D=%x,%d,%d}", |
| 292 |
|
| ctx->state[0], ctx->state[1], ctx->state[2], ctx->state[3], |
| 293 |
|
| ctx->count[0], ctx->count[1])); |
| 294 |
|
| return buf; |
| 295 |
|
| } |
| 296 |
|
| #endif |
| 297 |
|
| |
| 298 |
|
| |
| 299 |
|
| static void |
| 300 |
215274 |
| MD4Update(MD4_CTX* context, const U8* input, STRLEN inputLen) |
| 301 |
|
| { |
| 302 |
|
| unsigned int i, index, partLen; |
| 303 |
|
| |
| 304 |
|
| /* Compute number of bytes mod 64 */ |
| 305 |
215274 |
| index = (unsigned int)((context->count[0] >> 3) & 0x3F); |
| 306 |
|
| /* Update number of bits */ |
| 307 |
215274 |
| if ((context->count[0] += ((U32)inputLen << 3)) |
| 308 |
215274 |
▶ | < ((U32)inputLen << 3)) |
Branch: 1 branch
| Branch | True | False | Total |
| < ((U32)inputLen << 3)) |
0 |
215274 |
215274 |
|
| 309 |
0 |
| context->count[1]++; |
| 310 |
215274 |
| context->count[1] += ((U32)inputLen >> 29); |
| 311 |
|
| |
| 312 |
215274 |
| partLen = 64 - index; |
| 313 |
|
| |
| 314 |
|
| /* Transform as many times as possible. |
| 315 |
|
| */ |
| 316 |
215274 |
▶ | if (inputLen >= partLen) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (inputLen >= partLen) { |
4145 |
211129 |
215274 |
|
| 317 |
4145 |
| Copy(input, &context->buffer[index], partLen, U8); |
| 318 |
4145 |
| MD4Transform (context->state, context->buffer); |
| 319 |
|
| |
| 320 |
12719 |
▶ | for (i = partLen; i + 63 < inputLen; i += 64) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = partLen; i + 63 < inputLen; i += 64) |
8574 |
4145 |
12719 |
|
| 321 |
8574 |
| MD4Transform (context->state, &input[i]); |
| 322 |
|
| |
| 323 |
4145 |
| index = 0; |
| 324 |
|
| } |
| 325 |
|
| else |
| 326 |
211129 |
| i = 0; |
| 327 |
|
| |
| 328 |
|
| /* Buffer remaining input */ |
| 329 |
215274 |
| Copy(&input[i], &context->buffer[index], inputLen-i, U8); |
| 330 |
215274 |
| } |
| 331 |
|
| |
| 332 |
|
| |
| 333 |
|
| static void |
| 334 |
587 |
| MD4Final(U8* digest, MD4_CTX *context) |
| 335 |
|
| { |
| 336 |
|
| unsigned char bits[8]; |
| 337 |
|
| unsigned int index, padLen; |
| 338 |
|
| |
| 339 |
|
| /* Save number of bits */ |
| 340 |
587 |
| Encode (bits, context->count, 8); |
| 341 |
|
| |
| 342 |
|
| /* Pad out to 56 mod 64. |
| 343 |
|
| */ |
| 344 |
587 |
| index = (unsigned int)((context->count[0] >> 3) & 0x3f); |
| 345 |
587 |
▶ | padLen = (index < 56) ? (56 - index) : (120 - index); |
Branch: 1 branch
| Branch | True | False | Total |
| padLen = (index < 56) ? (56 - index) : (120 - index); |
511 |
76 |
587 |
|
| 346 |
587 |
| MD4Update (context, PADDING, padLen); |
| 347 |
|
| |
| 348 |
|
| /* Append length (before padding) */ |
| 349 |
587 |
| MD4Update (context, bits, 8); |
| 350 |
|
| /* Store state in digest */ |
| 351 |
587 |
| Encode (digest, context->state, 16); |
| 352 |
|
| |
| 353 |
587 |
| } |
| 354 |
|
| |
| 355 |
|
| #ifndef INT2PTR |
| 356 |
|
| #define INT2PTR(any,d) (any)(d) |
| 357 |
|
| #endif |
| 358 |
|
| |
| 359 |
61193 |
| static MD4_CTX* get_md4_ctx(SV* sv) |
| 360 |
|
| { |
| 361 |
61193 |
▶ | if (SvROK(sv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(sv)) { |
61193 |
0 |
61193 |
|
| 362 |
61193 |
| sv = SvRV(sv); |
| 363 |
61193 |
▶ | if (SvIOK(sv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvIOK(sv)) { |
61193 |
0 |
61193 |
|
| 364 |
61193 |
| MD4_CTX* ctx = INT2PTR(MD4_CTX*, SvIV(sv)); |
| 365 |
61193 |
▶ | if (ctx && ctx->signature == MD4_CTX_SIGNATURE) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx && ctx->signature == MD4_CTX_SIGNATURE) { |
61193 |
0 |
61193 |
| if (ctx && ctx->signature == MD4_CTX_SIGNATURE) { |
61193 |
0 |
61193 |
|
| 366 |
61193 |
| return ctx; |
| 367 |
|
| } |
| 368 |
|
| } |
| 369 |
|
| } |
| 370 |
0 |
| croak("Not a reference to a Digest::MD4 object"); |
| 371 |
|
| return (MD4_CTX*)0; /* some compilers insist on a return value */ |
| 372 |
|
| } |
| 373 |
|
| |
| 374 |
|
| |
| 375 |
286 |
| static char* hex_16(const unsigned char* from, char* to) |
| 376 |
|
| { |
| 377 |
|
| static char *hexdigits = "0123456789abcdef"; |
| 378 |
286 |
| const unsigned char *end = from + 16; |
| 379 |
286 |
| char *d = to; |
| 380 |
|
| |
| 381 |
4862 |
▶ | while (from < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (from < end) { |
4576 |
286 |
4862 |
|
| 382 |
4576 |
| *d++ = hexdigits[(*from >> 4)]; |
| 383 |
4576 |
| *d++ = hexdigits[(*from & 0x0F)]; |
| 384 |
4576 |
| from++; |
| 385 |
|
| } |
| 386 |
286 |
| *d = '\0'; |
| 387 |
286 |
| return to; |
| 388 |
|
| } |
| 389 |
|
| |
| 390 |
15 |
| static char* base64_16(const unsigned char* from, char* to) |
| 391 |
|
| { |
| 392 |
|
| static char* base64 = |
| 393 |
|
| "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 394 |
15 |
| const unsigned char *end = from + 16; |
| 395 |
|
| unsigned char c1, c2, c3; |
| 396 |
15 |
| char *d = to; |
| 397 |
|
| |
| 398 |
|
| while (1) { |
| 399 |
90 |
| c1 = *from++; |
| 400 |
90 |
| *d++ = base64[c1>>2]; |
| 401 |
90 |
▶ | if (from == end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (from == end) { |
15 |
75 |
90 |
|
| 402 |
15 |
| *d++ = base64[(c1 & 0x3) << 4]; |
| 403 |
15 |
| break; |
| 404 |
|
| } |
| 405 |
75 |
| c2 = *from++; |
| 406 |
75 |
| c3 = *from++; |
| 407 |
75 |
| *d++ = base64[((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)]; |
| 408 |
75 |
| *d++ = base64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)]; |
| 409 |
75 |
| *d++ = base64[c3 & 0x3F]; |
| 410 |
|
| } |
| 411 |
15 |
| *d = '\0'; |
| 412 |
15 |
| return to; |
| 413 |
|
| } |
| 414 |
|
| |
| 415 |
|
| /* Formats */ |
| 416 |
|
| #define F_BIN 0 |
| 417 |
|
| #define F_HEX 1 |
| 418 |
|
| #define F_B64 2 |
| 419 |
|
| |
| 420 |
587 |
| static SV* make_mortal_sv(const unsigned char *src, int type) |
| 421 |
|
| { |
| 422 |
|
| STRLEN len; |
| 423 |
|
| char result[33]; |
| 424 |
|
| char *ret; |
| 425 |
|
| |
| 426 |
587 |
| switch (type) { |
| 427 |
286 |
| case F_BIN: |
| 428 |
286 |
| ret = (char*)src; |
| 429 |
286 |
| len = 16; |
| 430 |
286 |
| break; |
| 431 |
286 |
| case F_HEX: |
| 432 |
286 |
| ret = hex_16(src, result); |
| 433 |
286 |
| len = 32; |
| 434 |
286 |
| break; |
| 435 |
15 |
| case F_B64: |
| 436 |
15 |
| ret = base64_16(src, result); |
| 437 |
15 |
| len = 22; |
| 438 |
15 |
| break; |
| 439 |
0 |
| default: |
| 440 |
0 |
| croak("Bad convertion type (%d)", type); |
| 441 |
|
| break; |
| 442 |
|
| } |
| 443 |
587 |
| return sv_2mortal(newSVpv(ret,len)); |
| 444 |
|
| } |
| 445 |
|
| |
| 446 |
|
| |
| 447 |
|
| /********************************************************************/ |
| 448 |
|
| |
| 449 |
|
| typedef PerlIO* InputStream; |
| 450 |
|
| |
| 451 |
|
| MODULE = Digest::MD4 PACKAGE = Digest::MD4 |
| 452 |
|
| |
| 453 |
|
| PROTOTYPES: DISABLE |
| 454 |
|
| |
| 455 |
|
| void |
| 456 |
|
| new(xclass) |
| 457 |
|
| SV* xclass |
| 458 |
|
| PREINIT: |
| 459 |
|
| MD4_CTX* context; |
| 460 |
|
| PPCODE: |
| 461 |
299 |
▶ | if (!SvROK(xclass)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvROK(xclass)) { |
299 |
0 |
299 |
|
| 462 |
|
| STRLEN my_na; |
| 463 |
299 |
| char *sclass = SvPV(xclass, my_na); |
| 464 |
299 |
| New(55, context, 1, MD4_CTX); |
| 465 |
299 |
| context->signature = MD4_CTX_SIGNATURE; |
| 466 |
299 |
| ST(0) = sv_newmortal(); |
| 467 |
299 |
| sv_setref_pv(ST(0), sclass, (void*)context); |
| 468 |
299 |
| SvREADONLY_on(SvRV(ST(0))); |
| 469 |
|
| } else { |
| 470 |
0 |
| context = get_md4_ctx(xclass); |
| 471 |
|
| } |
| 472 |
299 |
| MD4Init(context); |
| 473 |
299 |
| XSRETURN(1); |
| 474 |
|
| |
| 475 |
|
| void |
| 476 |
|
| clone(self) |
| 477 |
|
| SV* self |
| 478 |
|
| PREINIT: |
| 479 |
5 |
| MD4_CTX* cont = get_md4_ctx(self); |
| 480 |
5 |
| const char *myname = sv_reftype(SvRV(self),TRUE); |
| 481 |
|
| MD4_CTX* context; |
| 482 |
|
| PPCODE: |
| 483 |
5 |
| New(55, context, 1, MD4_CTX); |
| 484 |
5 |
| ST(0) = sv_newmortal(); |
| 485 |
5 |
| sv_setref_pv(ST(0), myname , (void*)context); |
| 486 |
5 |
| SvREADONLY_on(SvRV(ST(0))); |
| 487 |
5 |
| memcpy(context,cont,sizeof(MD4_CTX)); |
| 488 |
5 |
| XSRETURN(1); |
| 489 |
|
| |
| 490 |
|
| void |
| 491 |
|
| DESTROY(context) |
| 492 |
|
| MD4_CTX* context |
| 493 |
|
| CODE: |
| 494 |
304 |
| Safefree(context); |
| 495 |
|
| |
| 496 |
|
| void |
| 497 |
|
| add(self, ...) |
| 498 |
|
| SV* self |
| 499 |
|
| PREINIT: |
| 500 |
60566 |
| MD4_CTX* context = get_md4_ctx(self); |
| 501 |
|
| int i; |
| 502 |
|
| unsigned char *data; |
| 503 |
|
| STRLEN len; |
| 504 |
|
| PPCODE: |
| 505 |
214051 |
▶ | for (i = 1; i < items; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < items; i++) { |
153485 |
60566 |
214051 |
|
| 506 |
153485 |
| data = (unsigned char *)(SvPVbyte(ST(i), len)); |
| 507 |
153485 |
| MD4Update(context, data, len); |
| 508 |
|
| } |
| 509 |
60566 |
| XSRETURN(1); /* self */ |
| 510 |
|
| |
| 511 |
|
| void |
| 512 |
|
| addfile(self, fh) |
| 513 |
|
| SV* self |
| 514 |
|
| InputStream fh |
| 515 |
|
| PREINIT: |
| 516 |
16 |
| MD4_CTX* context = get_md4_ctx(self); |
| 517 |
16 |
| STRLEN fill = (context->count[0] >> 3) & 0x3F; |
| 518 |
|
| unsigned char buffer[4096]; |
| 519 |
|
| int n; |
| 520 |
|
| CODE: |
| 521 |
16 |
▶ | if (fh) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fh) { |
15 |
1 |
16 |
|
| 522 |
15 |
▶ | if (fill) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fill) { |
0 |
15 |
15 |
|
| 523 |
|
| /* The MD4Update() function is faster if it can work with |
| 524 |
|
| * complete blocks. This will fill up any buffered block |
| 525 |
|
| * first. |
| 526 |
|
| */ |
| 527 |
0 |
| STRLEN missing = 64 - fill; |
| 528 |
0 |
▶ | if ( (n = PerlIO_read(fh, buffer, missing)) > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( (n = PerlIO_read(fh, buffer, missing)) > 0) |
0 |
0 |
0 |
|
| 529 |
0 |
| MD4Update(context, buffer, n); |
| 530 |
|
| else |
| 531 |
0 |
| XSRETURN(1); /* self */ |
| 532 |
|
| } |
| 533 |
|
| |
| 534 |
|
| /* Process blocks until EOF or error */ |
| 535 |
66 |
▶ | while ( (n = PerlIO_read(fh, buffer, sizeof(buffer))) > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ( (n = PerlIO_read(fh, buffer, sizeof(buffer))) > 0) { |
51 |
15 |
66 |
|
| 536 |
51 |
| MD4Update(context, buffer, n); |
| 537 |
|
| } |
| 538 |
|
| |
| 539 |
15 |
▶ | if (PerlIO_error(fh)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (PerlIO_error(fh)) { |
0 |
15 |
15 |
|
| 540 |
0 |
| croak("Reading from filehandle failed"); |
| 541 |
|
| } |
| 542 |
|
| } |
| 543 |
|
| else { |
| 544 |
1 |
| croak("No filehandle passed"); |
| 545 |
|
| } |
| 546 |
15 |
| XSRETURN(1); /* self */ |
| 547 |
|
| |
| 548 |
|
| void |
| 549 |
|
| digest(context) |
| 550 |
|
| MD4_CTX* context |
| 551 |
|
| ALIAS: |
| 552 |
|
| Digest::MD4::digest = F_BIN |
| 553 |
|
| Digest::MD4::hexdigest = F_HEX |
| 554 |
|
| Digest::MD4::b64digest = F_B64 |
| 555 |
|
| PREINIT: |
| 556 |
|
| unsigned char digeststr[16]; |
| 557 |
|
| PPCODE: |
| 558 |
302 |
| MD4Final(digeststr, context); |
| 559 |
302 |
| MD4Init(context); /* In case it is reused */ |
| 560 |
302 |
| ST(0) = make_mortal_sv(digeststr, ix); |
| 561 |
302 |
| XSRETURN(1); |
| 562 |
|
| |
| 563 |
|
| void |
| 564 |
|
| md4(...) |
| 565 |
|
| ALIAS: |
| 566 |
|
| Digest::MD4::md4 = F_BIN |
| 567 |
|
| Digest::MD4::md4_hex = F_HEX |
| 568 |
|
| Digest::MD4::md4_base64 = F_B64 |
| 569 |
|
| PREINIT: |
| 570 |
|
| MD4_CTX ctx; |
| 571 |
|
| int i; |
| 572 |
|
| unsigned char *data; |
| 573 |
|
| STRLEN len; |
| 574 |
|
| unsigned char digeststr[16]; |
| 575 |
|
| PPCODE: |
| 576 |
286 |
| MD4Init(&ctx); |
| 577 |
|
| |
| 578 |
286 |
▶ | if (DOWARN) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (DOWARN) { |
9 |
277 |
286 |
|
| 579 |
9 |
| char *msg = 0; |
| 580 |
9 |
▶ | if (items == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items == 1) { |
9 |
0 |
9 |
|
| 581 |
9 |
▶ | if (SvROK(ST(0))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(ST(0))) { |
0 |
9 |
9 |
|
| 582 |
0 |
| SV* sv = SvRV(ST(0)); |
| 583 |
0 |
▶ | if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD4")) |
Branch: 8 branches
| Branch | True | False | Total |
| if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD4")) |
0 |
0 |
0 |
| if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD4")) |
0 |
0 |
0 |
| if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD4")) |
0 |
0 |
0 |
| if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD4")) |
0 |
0 |
0 |
| if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD4")) |
0 |
0 |
0 |
| if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD4")) |
0 |
0 |
0 |
| if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD4")) |
0 |
0 |
0 |
| if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD4")) |
0 |
0 |
0 |
|
| 584 |
0 |
| msg = "probably called as method"; |
| 585 |
|
| else |
| 586 |
0 |
| msg = "called with reference argument"; |
| 587 |
|
| } |
| 588 |
|
| } |
| 589 |
0 |
▶ | else if (items > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (items > 1) { |
0 |
0 |
0 |
|
| 590 |
0 |
| data = (unsigned char *)SvPVbyte(ST(0), len); |
| 591 |
0 |
▶ | if (len == 11 && memEQ("Digest::MD4", data, 11)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == 11 && memEQ("Digest::MD4", data, 11)) { |
0 |
0 |
0 |
| if (len == 11 && memEQ("Digest::MD4", data, 11)) { |
0 |
0 |
0 |
|
| 592 |
0 |
| msg = "probably called as class method"; |
| 593 |
|
| } |
| 594 |
|
| } |
| 595 |
9 |
▶ | if (msg) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg) { |
0 |
9 |
9 |
|
| 596 |
0 |
▶ | char *f = (ix == F_BIN) ? "md4" : |
Branch: 1 branch
| Branch | True | False | Total |
| char *f = (ix == F_BIN) ? "md4" : |
0 |
0 |
0 |
|
| 597 |
0 |
▶ | (ix == F_HEX) ? "md4_hex" : "md4_base64"; |
Branch: 1 branch
| Branch | True | False | Total |
| (ix == F_HEX) ? "md4_hex" : "md4_base64"; |
0 |
0 |
0 |
|
| 598 |
0 |
| warn("&Digest::MD4::%s function %s", f, msg); |
| 599 |
|
| } |
| 600 |
|
| } |
| 601 |
|
| |
| 602 |
60850 |
▶ | for (i = 0; i < items; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < items; i++) { |
60565 |
285 |
60850 |
|
| 603 |
60565 |
| data = (unsigned char *)(SvPVbyte(ST(i), len)); |
| 604 |
60564 |
| MD4Update(&ctx, data, len); |
| 605 |
|
| } |
| 606 |
285 |
| MD4Final(digeststr, &ctx); |
| 607 |
285 |
| ST(0) = make_mortal_sv(digeststr, ix); |
| 608 |
285 |
| XSRETURN(1); |