1/******************************************************************************* 2Copyright (C) Marvell International Ltd. and its affiliates 3 4This software file (the "File") is owned and distributed by Marvell 5International Ltd. and/or its affiliates ("Marvell") under the following 6alternative licensing terms. Once you have made an election to distribute the 7File under one of the following license alternatives, please (i) delete this 8introductory statement regarding license alternatives, (ii) delete the two 9license alternatives that you have not elected to use and (iii) preserve the 10Marvell copyright notice above. 11 12******************************************************************************** 13Marvell Commercial License Option 14 15If you received this File from Marvell and you have entered into a commercial 16license agreement (a "Commercial License") with Marvell, the File is licensed 17to you under the terms of the applicable Commercial License. 18 19******************************************************************************** 20Marvell GPL License Option 21 22If you received this File from Marvell, you may opt to use, redistribute and/or 23modify this File in accordance with the terms and conditions of the General 24Public License Version 2, June 1991 (the "GPL License"), a copy of which is 25available along with the File in the license.txt file or by writing to the Free 26Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 or 27on the worldwide web at http://www.gnu.org/licenses/gpl.txt. 28 29THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE IMPLIED 30WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY 31DISCLAIMED. The GPL License provides additional details about this warranty 32disclaimer. 33******************************************************************************** 34Marvell BSD License Option 35 36If you received this File from Marvell, you may opt to use, redistribute and/or 37modify this File under the following licensing terms. 38Redistribution and use in source and binary forms, with or without modification, 39are permitted provided that the following conditions are met: 40 41 * Redistributions of source code must retain the above copyright notice, 42 this list of conditions and the following disclaimer. 43 44 * Redistributions in binary form must reproduce the above copyright 45 notice, this list of conditions and the following disclaimer in the 46 documentation and/or other materials provided with the distribution. 47 48 * Neither the name of Marvell nor the names of its contributors may be 49 used to endorse or promote products derived from this software without 50 specific prior written permission. 51 52THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 53ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 54WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 55DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 56ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 57(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 58LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 59ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 60(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 61SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 62 63*******************************************************************************/ 64 65#include "mvOs.h" 66#include "mvMD5.h" 67 68static void mvMD5Transform(MV_U32 buf[4], MV_U32 const in[MV_MD5_MAC_LEN]); 69 70#ifdef MV_CPU_LE 71#define mvByteReverse(buf, len) /* Nothing */ 72#else 73static void mvByteReverse(unsigned char *buf, unsigned longs); 74 75/* 76 * Note: this code is harmless on little-endian machines. 77 */ 78static void mvByteReverse(unsigned char *buf, unsigned longs) 79{ 80 MV_U32 t; 81 82 do 83 { 84 t = (MV_U32) ((unsigned) buf[3] << 8 | buf[2]) << 16 | 85 ((unsigned) buf[1] << 8 | buf[0]); 86 *(MV_U32 *) buf = t; 87 buf += 4; 88 } while (--longs); 89} 90#endif 91 92/* 93 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious 94 * initialization constants. 95 */ 96void mvMD5Init(MV_MD5_CONTEXT *ctx) 97{ 98 ctx->buf[0] = 0x67452301; 99 ctx->buf[1] = 0xefcdab89; 100 ctx->buf[2] = 0x98badcfe; 101 ctx->buf[3] = 0x10325476; 102 103 ctx->bits[0] = 0; 104 ctx->bits[1] = 0; 105} 106 107/* 108 * Update context to reflect the concatenation of another buffer full 109 * of bytes. 110 */ 111void mvMD5Update(MV_MD5_CONTEXT *ctx, unsigned char const *buf, unsigned len) 112{ 113 MV_U32 t; 114 115 /* Update bitcount */ 116 117 t = ctx->bits[0]; 118 if ((ctx->bits[0] = t + ((MV_U32) len << 3)) < t) 119 ctx->bits[1]++; /* Carry from low to high */ 120 ctx->bits[1] += len >> 29; 121 122 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ 123 124 /* Handle any leading odd-sized chunks */ 125 126 if (t) 127 { 128 unsigned char *p = (unsigned char *) ctx->in + t; 129 130 t = 64 - t; 131 if (len < t) 132 { 133 memcpy(p, buf, len); 134 return; 135 } 136 memcpy(p, buf, t); 137 mvByteReverse(ctx->in, MV_MD5_MAC_LEN); 138 mvMD5Transform(ctx->buf, (MV_U32 *) ctx->in); 139 buf += t; 140 len -= t; 141 } 142 /* Process data in 64-byte chunks */ 143 144 while (len >= 64) 145 { 146 memcpy(ctx->in, buf, 64); 147 mvByteReverse(ctx->in, MV_MD5_MAC_LEN); 148 mvMD5Transform(ctx->buf, (MV_U32 *) ctx->in); 149 buf += 64; 150 len -= 64; 151 } 152 153 /* Handle any remaining bytes of data. */ 154 155 memcpy(ctx->in, buf, len); 156} 157 158/* 159 * Final wrapup - pad to 64-byte boundary with the bit pattern 160 * 1 0* (64-bit count of bits processed, MSB-first) 161 */ 162void mvMD5Final(unsigned char digest[MV_MD5_MAC_LEN], MV_MD5_CONTEXT *ctx) 163{ 164 unsigned count; 165 unsigned char *p; 166 167 /* Compute number of bytes mod 64 */ 168 count = (ctx->bits[0] >> 3) & 0x3F; 169 170 /* Set the first char of padding to 0x80. This is safe since there is 171 always at least one byte free */ 172 p = ctx->in + count; 173 *p++ = 0x80; 174 175 /* Bytes of padding needed to make 64 bytes */ 176 count = 64 - 1 - count; 177 178 /* Pad out to 56 mod 64 */ 179 if (count < 8) 180 { 181 /* Two lots of padding: Pad the first block to 64 bytes */ 182 memset(p, 0, count); 183 mvByteReverse(ctx->in, MV_MD5_MAC_LEN); 184 mvMD5Transform(ctx->buf, (MV_U32 *) ctx->in); 185 186 /* Now fill the next block with 56 bytes */ 187 memset(ctx->in, 0, 56); 188 } 189 else 190 { 191 /* Pad block to 56 bytes */ 192 memset(p, 0, count - 8); 193 } 194 mvByteReverse(ctx->in, 14); 195 196 /* Append length in bits and transform */ 197 ((MV_U32 *) ctx->in)[14] = ctx->bits[0]; 198 ((MV_U32 *) ctx->in)[15] = ctx->bits[1]; 199 200 mvMD5Transform(ctx->buf, (MV_U32 *) ctx->in); 201 mvByteReverse((unsigned char *) ctx->buf, 4); 202 memcpy(digest, ctx->buf, MV_MD5_MAC_LEN); 203 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */ 204} 205 206/* The four core functions - F1 is optimized somewhat */ 207 208/* #define F1(x, y, z) (x & y | ~x & z) */ 209#define F1(x, y, z) (z ^ (x & (y ^ z))) 210#define F2(x, y, z) F1(z, x, y) 211#define F3(x, y, z) (x ^ y ^ z) 212#define F4(x, y, z) (y ^ (x | ~z)) 213 214/* This is the central step in the MD5 algorithm. */ 215#define MD5STEP(f, w, x, y, z, data, s) \ 216 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) 217 218/* 219 * The core of the MD5 algorithm, this alters an existing MD5 hash to 220 * reflect the addition of 16 longwords of new data. MD5Update blocks 221 * the data and converts bytes into longwords for this routine. 222 */ 223static void mvMD5Transform(MV_U32 buf[4], MV_U32 const in[MV_MD5_MAC_LEN]) 224{ 225 register MV_U32 a, b, c, d; 226 227 a = buf[0]; 228 b = buf[1]; 229 c = buf[2]; 230 d = buf[3]; 231 232 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); 233 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); 234 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); 235 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); 236 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); 237 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); 238 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); 239 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); 240 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); 241 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); 242 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); 243 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); 244 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); 245 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); 246 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); 247 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); 248 249 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); 250 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); 251 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); 252 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); 253 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); 254 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); 255 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); 256 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); 257 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); 258 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); 259 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); 260 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); 261 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); 262 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); 263 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); 264 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); 265 266 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); 267 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); 268 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); 269 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); 270 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); 271 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); 272 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); 273 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); 274 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); 275 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); 276 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); 277 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); 278 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); 279 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); 280 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); 281 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); 282 283 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); 284 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); 285 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); 286 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); 287 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); 288 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); 289 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); 290 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); 291 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); 292 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); 293 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); 294 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); 295 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); 296 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); 297 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); 298 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); 299 300 buf[0] += a; 301 buf[1] += b; 302 buf[2] += c; 303 buf[3] += d; 304} 305 306void mvMD5(unsigned char const *buf, unsigned len, unsigned char* digest) 307{ 308 MV_MD5_CONTEXT ctx; 309 310 mvMD5Init(&ctx); 311 mvMD5Update(&ctx, buf, len); 312 mvMD5Final(digest, &ctx); 313} 314 315 316void mvHmacMd5(unsigned char const* text, int text_len, 317 unsigned char const* key, int key_len, 318 unsigned char* digest) 319{ 320 int i; 321 MV_MD5_CONTEXT ctx; 322 unsigned char k_ipad[64+1]; /* inner padding - key XORd with ipad */ 323 unsigned char k_opad[64+1]; /* outer padding - key XORd with opad */ 324 325 /* start out by storing key in pads */ 326 memset(k_ipad, 0, 64); 327 memcpy(k_ipad, key, key_len); 328 memset(k_opad, 0, 64); 329 memcpy(k_opad, key, key_len); 330 331 /* XOR key with ipad and opad values */ 332 for (i=0; i<64; i++) 333 { 334 k_ipad[i] ^= 0x36; 335 k_opad[i] ^= 0x5c; 336 } 337 338 /* perform inner MD5 */ 339 mvMD5Init(&ctx); /* init ctx for 1st pass */ 340 mvMD5Update(&ctx, k_ipad, 64); /* start with inner pad */ 341 mvMD5Update(&ctx, text, text_len); /* then text of datagram */ 342 mvMD5Final(digest, &ctx); /* finish up 1st pass */ 343 344 /* perform outer MD5 */ 345 mvMD5Init(&ctx); /* init ctx for 2nd pass */ 346 mvMD5Update(&ctx, k_opad, 64); /* start with outer pad */ 347 mvMD5Update(&ctx, digest, 16); /* then results of 1st hash */ 348 mvMD5Final(digest, &ctx); /* finish up 2nd pass */ 349} 350