1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/lib/freebl/md2.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,268 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#ifdef FREEBL_NO_DEPEND 1.9 +#include "stubs.h" 1.10 +#endif 1.11 + 1.12 +#include "prerr.h" 1.13 +#include "secerr.h" 1.14 + 1.15 +#include "prtypes.h" 1.16 + 1.17 +#include "blapi.h" 1.18 + 1.19 +#define MD2_DIGEST_LEN 16 1.20 +#define MD2_BUFSIZE 16 1.21 +#define MD2_X_SIZE 48 /* The X array, [CV | INPUT | TMP VARS] */ 1.22 +#define MD2_CV 0 /* index into X for chaining variables */ 1.23 +#define MD2_INPUT 16 /* index into X for input */ 1.24 +#define MD2_TMPVARS 32 /* index into X for temporary variables */ 1.25 +#define MD2_CHECKSUM_SIZE 16 1.26 + 1.27 +struct MD2ContextStr { 1.28 + unsigned char checksum[MD2_BUFSIZE]; 1.29 + unsigned char X[MD2_X_SIZE]; 1.30 + PRUint8 unusedBuffer; 1.31 +}; 1.32 + 1.33 +static const PRUint8 MD2S[256] = { 1.34 + 0051, 0056, 0103, 0311, 0242, 0330, 0174, 0001, 1.35 + 0075, 0066, 0124, 0241, 0354, 0360, 0006, 0023, 1.36 + 0142, 0247, 0005, 0363, 0300, 0307, 0163, 0214, 1.37 + 0230, 0223, 0053, 0331, 0274, 0114, 0202, 0312, 1.38 + 0036, 0233, 0127, 0074, 0375, 0324, 0340, 0026, 1.39 + 0147, 0102, 0157, 0030, 0212, 0027, 0345, 0022, 1.40 + 0276, 0116, 0304, 0326, 0332, 0236, 0336, 0111, 1.41 + 0240, 0373, 0365, 0216, 0273, 0057, 0356, 0172, 1.42 + 0251, 0150, 0171, 0221, 0025, 0262, 0007, 0077, 1.43 + 0224, 0302, 0020, 0211, 0013, 0042, 0137, 0041, 1.44 + 0200, 0177, 0135, 0232, 0132, 0220, 0062, 0047, 1.45 + 0065, 0076, 0314, 0347, 0277, 0367, 0227, 0003, 1.46 + 0377, 0031, 0060, 0263, 0110, 0245, 0265, 0321, 1.47 + 0327, 0136, 0222, 0052, 0254, 0126, 0252, 0306, 1.48 + 0117, 0270, 0070, 0322, 0226, 0244, 0175, 0266, 1.49 + 0166, 0374, 0153, 0342, 0234, 0164, 0004, 0361, 1.50 + 0105, 0235, 0160, 0131, 0144, 0161, 0207, 0040, 1.51 + 0206, 0133, 0317, 0145, 0346, 0055, 0250, 0002, 1.52 + 0033, 0140, 0045, 0255, 0256, 0260, 0271, 0366, 1.53 + 0034, 0106, 0141, 0151, 0064, 0100, 0176, 0017, 1.54 + 0125, 0107, 0243, 0043, 0335, 0121, 0257, 0072, 1.55 + 0303, 0134, 0371, 0316, 0272, 0305, 0352, 0046, 1.56 + 0054, 0123, 0015, 0156, 0205, 0050, 0204, 0011, 1.57 + 0323, 0337, 0315, 0364, 0101, 0201, 0115, 0122, 1.58 + 0152, 0334, 0067, 0310, 0154, 0301, 0253, 0372, 1.59 + 0044, 0341, 0173, 0010, 0014, 0275, 0261, 0112, 1.60 + 0170, 0210, 0225, 0213, 0343, 0143, 0350, 0155, 1.61 + 0351, 0313, 0325, 0376, 0073, 0000, 0035, 0071, 1.62 + 0362, 0357, 0267, 0016, 0146, 0130, 0320, 0344, 1.63 + 0246, 0167, 0162, 0370, 0353, 0165, 0113, 0012, 1.64 + 0061, 0104, 0120, 0264, 0217, 0355, 0037, 0032, 1.65 + 0333, 0231, 0215, 0063, 0237, 0021, 0203, 0024 1.66 +}; 1.67 + 1.68 +SECStatus 1.69 +MD2_Hash(unsigned char *dest, const char *src) 1.70 +{ 1.71 + unsigned int len; 1.72 + MD2Context *cx = MD2_NewContext(); 1.73 + if (!cx) { 1.74 + PORT_SetError(PR_OUT_OF_MEMORY_ERROR); 1.75 + return SECFailure; 1.76 + } 1.77 + MD2_Begin(cx); 1.78 + MD2_Update(cx, (const unsigned char *)src, PORT_Strlen(src)); 1.79 + MD2_End(cx, dest, &len, MD2_DIGEST_LEN); 1.80 + MD2_DestroyContext(cx, PR_TRUE); 1.81 + return SECSuccess; 1.82 +} 1.83 + 1.84 +MD2Context * 1.85 +MD2_NewContext(void) 1.86 +{ 1.87 + MD2Context *cx = (MD2Context *)PORT_ZAlloc(sizeof(MD2Context)); 1.88 + if (cx == NULL) { 1.89 + PORT_SetError(PR_OUT_OF_MEMORY_ERROR); 1.90 + return NULL; 1.91 + } 1.92 + return cx; 1.93 +} 1.94 + 1.95 +void 1.96 +MD2_DestroyContext(MD2Context *cx, PRBool freeit) 1.97 +{ 1.98 + if (freeit) 1.99 + PORT_ZFree(cx, sizeof(*cx)); 1.100 +} 1.101 + 1.102 +void 1.103 +MD2_Begin(MD2Context *cx) 1.104 +{ 1.105 + memset(cx, 0, sizeof(*cx)); 1.106 + cx->unusedBuffer = MD2_BUFSIZE; 1.107 +} 1.108 + 1.109 +static void 1.110 +md2_compress(MD2Context *cx) 1.111 +{ 1.112 + int j; 1.113 + unsigned char P; 1.114 + P = cx->checksum[MD2_CHECKSUM_SIZE-1]; 1.115 + /* Compute the running checksum, and set the tmp variables to be 1.116 + * CV[i] XOR input[i] 1.117 + */ 1.118 +#define CKSUMFN(n) \ 1.119 + P = cx->checksum[n] ^ MD2S[cx->X[MD2_INPUT+n] ^ P]; \ 1.120 + cx->checksum[n] = P; \ 1.121 + cx->X[MD2_TMPVARS+n] = cx->X[n] ^ cx->X[MD2_INPUT+n]; 1.122 + CKSUMFN(0); 1.123 + CKSUMFN(1); 1.124 + CKSUMFN(2); 1.125 + CKSUMFN(3); 1.126 + CKSUMFN(4); 1.127 + CKSUMFN(5); 1.128 + CKSUMFN(6); 1.129 + CKSUMFN(7); 1.130 + CKSUMFN(8); 1.131 + CKSUMFN(9); 1.132 + CKSUMFN(10); 1.133 + CKSUMFN(11); 1.134 + CKSUMFN(12); 1.135 + CKSUMFN(13); 1.136 + CKSUMFN(14); 1.137 + CKSUMFN(15); 1.138 + /* The compression function. */ 1.139 +#define COMPRESS(n) \ 1.140 + P = cx->X[n] ^ MD2S[P]; \ 1.141 + cx->X[n] = P; 1.142 + P = 0x00; 1.143 + for (j=0; j<18; j++) { 1.144 + COMPRESS(0); 1.145 + COMPRESS(1); 1.146 + COMPRESS(2); 1.147 + COMPRESS(3); 1.148 + COMPRESS(4); 1.149 + COMPRESS(5); 1.150 + COMPRESS(6); 1.151 + COMPRESS(7); 1.152 + COMPRESS(8); 1.153 + COMPRESS(9); 1.154 + COMPRESS(10); 1.155 + COMPRESS(11); 1.156 + COMPRESS(12); 1.157 + COMPRESS(13); 1.158 + COMPRESS(14); 1.159 + COMPRESS(15); 1.160 + COMPRESS(16); 1.161 + COMPRESS(17); 1.162 + COMPRESS(18); 1.163 + COMPRESS(19); 1.164 + COMPRESS(20); 1.165 + COMPRESS(21); 1.166 + COMPRESS(22); 1.167 + COMPRESS(23); 1.168 + COMPRESS(24); 1.169 + COMPRESS(25); 1.170 + COMPRESS(26); 1.171 + COMPRESS(27); 1.172 + COMPRESS(28); 1.173 + COMPRESS(29); 1.174 + COMPRESS(30); 1.175 + COMPRESS(31); 1.176 + COMPRESS(32); 1.177 + COMPRESS(33); 1.178 + COMPRESS(34); 1.179 + COMPRESS(35); 1.180 + COMPRESS(36); 1.181 + COMPRESS(37); 1.182 + COMPRESS(38); 1.183 + COMPRESS(39); 1.184 + COMPRESS(40); 1.185 + COMPRESS(41); 1.186 + COMPRESS(42); 1.187 + COMPRESS(43); 1.188 + COMPRESS(44); 1.189 + COMPRESS(45); 1.190 + COMPRESS(46); 1.191 + COMPRESS(47); 1.192 + P = (P + j) % 256; 1.193 + } 1.194 + cx->unusedBuffer = MD2_BUFSIZE; 1.195 +} 1.196 + 1.197 +void 1.198 +MD2_Update(MD2Context *cx, const unsigned char *input, unsigned int inputLen) 1.199 +{ 1.200 + PRUint32 bytesToConsume; 1.201 + 1.202 + /* Fill the remaining input buffer. */ 1.203 + if (cx->unusedBuffer != MD2_BUFSIZE) { 1.204 + bytesToConsume = PR_MIN(inputLen, cx->unusedBuffer); 1.205 + memcpy(&cx->X[MD2_INPUT + (MD2_BUFSIZE - cx->unusedBuffer)], 1.206 + input, bytesToConsume); 1.207 + if (cx->unusedBuffer + bytesToConsume >= MD2_BUFSIZE) 1.208 + md2_compress(cx); 1.209 + inputLen -= bytesToConsume; 1.210 + input += bytesToConsume; 1.211 + } 1.212 + 1.213 + /* Iterate over 16-byte chunks of the input. */ 1.214 + while (inputLen >= MD2_BUFSIZE) { 1.215 + memcpy(&cx->X[MD2_INPUT], input, MD2_BUFSIZE); 1.216 + md2_compress(cx); 1.217 + inputLen -= MD2_BUFSIZE; 1.218 + input += MD2_BUFSIZE; 1.219 + } 1.220 + 1.221 + /* Copy any input that remains into the buffer. */ 1.222 + if (inputLen) 1.223 + memcpy(&cx->X[MD2_INPUT], input, inputLen); 1.224 + cx->unusedBuffer = MD2_BUFSIZE - inputLen; 1.225 +} 1.226 + 1.227 +void 1.228 +MD2_End(MD2Context *cx, unsigned char *digest, 1.229 + unsigned int *digestLen, unsigned int maxDigestLen) 1.230 +{ 1.231 + PRUint8 padStart; 1.232 + if (maxDigestLen < MD2_BUFSIZE) { 1.233 + PORT_SetError(SEC_ERROR_INVALID_ARGS); 1.234 + return; 1.235 + } 1.236 + padStart = MD2_BUFSIZE - cx->unusedBuffer; 1.237 + memset(&cx->X[MD2_INPUT + padStart], cx->unusedBuffer, 1.238 + cx->unusedBuffer); 1.239 + md2_compress(cx); 1.240 + memcpy(&cx->X[MD2_INPUT], cx->checksum, MD2_BUFSIZE); 1.241 + md2_compress(cx); 1.242 + *digestLen = MD2_DIGEST_LEN; 1.243 + memcpy(digest, &cx->X[MD2_CV], MD2_DIGEST_LEN); 1.244 +} 1.245 + 1.246 +unsigned int 1.247 +MD2_FlattenSize(MD2Context *cx) 1.248 +{ 1.249 + return sizeof(*cx); 1.250 +} 1.251 + 1.252 +SECStatus 1.253 +MD2_Flatten(MD2Context *cx, unsigned char *space) 1.254 +{ 1.255 + memcpy(space, cx, sizeof(*cx)); 1.256 + return SECSuccess; 1.257 +} 1.258 + 1.259 +MD2Context * 1.260 +MD2_Resurrect(unsigned char *space, void *arg) 1.261 +{ 1.262 + MD2Context *cx = MD2_NewContext(); 1.263 + if (cx) 1.264 + memcpy(cx, space, sizeof(*cx)); 1.265 + return cx; 1.266 +} 1.267 + 1.268 +void MD2_Clone(MD2Context *dest, MD2Context *src) 1.269 +{ 1.270 + memcpy(dest, src, sizeof *dest); 1.271 +}