Back to index

lightning-sunbird  0.9+nobinonly
md4.c
Go to the documentation of this file.
00001 /* vim:set ts=2 sw=2 et cindent: */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla.
00016  *
00017  * The Initial Developer of the Original Code is IBM Corporation.
00018  * Portions created by IBM Corporation are Copyright (C) 2003
00019  * IBM Corporation. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Darin Fisher <darin@meer.net>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * "clean room" MD4 implementation (see RFC 1320)
00040  */
00041 
00042 #include <string.h>
00043 #include "md4.h"
00044 
00045 typedef PRUint32 Uint32;
00046 typedef PRUint8 Uint8;
00047 
00048 /* the "conditional" function */
00049 #define F(x,y,z) (((x) & (y)) | (~(x) & (z)))
00050 
00051 /* the "majority" function */
00052 #define G(x,y,z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
00053 
00054 /* the "parity" function */
00055 #define H(x,y,z) ((x) ^ (y) ^ (z))
00056 
00057 /* rotate n-bits to the left */
00058 #define ROTL(x,n) (((x) << (n)) | ((x) >> (0x20 - n)))
00059 
00060 /* round 1: [abcd k s]: a = (a + F(b,c,d) + X[k]) <<< s */
00061 #define RD1(a,b,c,d,k,s) a += F(b,c,d) + X[k]; a = ROTL(a,s)
00062 
00063 /* round 2: [abcd k s]: a = (a + G(b,c,d) + X[k] + MAGIC) <<< s */
00064 #define RD2(a,b,c,d,k,s) a += G(b,c,d) + X[k] + 0x5A827999; a = ROTL(a,s)
00065 
00066 /* round 3: [abcd k s]: a = (a + H(b,c,d) + X[k] + MAGIC) <<< s */
00067 #define RD3(a,b,c,d,k,s) a += H(b,c,d) + X[k] + 0x6ED9EBA1; a = ROTL(a,s)
00068 
00069 /* converts from word array to byte array, len is number of bytes */
00070 static void w2b(Uint8 *out, const Uint32 *in, Uint32 len)
00071 {
00072   Uint8 *bp; const Uint32 *wp, *wpend;
00073 
00074   bp = out;
00075   wp = in;
00076   wpend = wp + (len >> 2);
00077 
00078   for (; wp != wpend; ++wp, bp += 4)
00079   {
00080     bp[0] = (Uint8) ((*wp      ) & 0xFF);
00081     bp[1] = (Uint8) ((*wp >>  8) & 0xFF);
00082     bp[2] = (Uint8) ((*wp >> 16) & 0xFF);
00083     bp[3] = (Uint8) ((*wp >> 24) & 0xFF);
00084   }
00085 }
00086 
00087 /* converts from byte array to word array, len is number of bytes */
00088 static void b2w(Uint32 *out, const Uint8 *in, Uint32 len)
00089 {
00090   Uint32 *wp; const Uint8 *bp, *bpend;
00091 
00092   wp = out;
00093   bp = in;
00094   bpend = in + len;
00095 
00096   for (; bp != bpend; bp += 4, ++wp)
00097   {
00098     *wp = (Uint32) (bp[0]      ) |
00099           (Uint32) (bp[1] <<  8) |
00100           (Uint32) (bp[2] << 16) |
00101           (Uint32) (bp[3] << 24);
00102   }
00103 }
00104 
00105 /* update state: data is 64 bytes in length */
00106 static void md4step(Uint32 state[4], const Uint8 *data)
00107 {
00108   Uint32 A, B, C, D, X[16];
00109 
00110   b2w(X, data, 64);
00111 
00112   A = state[0];
00113   B = state[1];
00114   C = state[2];
00115   D = state[3];
00116 
00117   RD1(A,B,C,D, 0,3); RD1(D,A,B,C, 1,7); RD1(C,D,A,B, 2,11); RD1(B,C,D,A, 3,19);
00118   RD1(A,B,C,D, 4,3); RD1(D,A,B,C, 5,7); RD1(C,D,A,B, 6,11); RD1(B,C,D,A, 7,19);
00119   RD1(A,B,C,D, 8,3); RD1(D,A,B,C, 9,7); RD1(C,D,A,B,10,11); RD1(B,C,D,A,11,19);
00120   RD1(A,B,C,D,12,3); RD1(D,A,B,C,13,7); RD1(C,D,A,B,14,11); RD1(B,C,D,A,15,19);
00121 
00122   RD2(A,B,C,D, 0,3); RD2(D,A,B,C, 4,5); RD2(C,D,A,B, 8, 9); RD2(B,C,D,A,12,13); 
00123   RD2(A,B,C,D, 1,3); RD2(D,A,B,C, 5,5); RD2(C,D,A,B, 9, 9); RD2(B,C,D,A,13,13); 
00124   RD2(A,B,C,D, 2,3); RD2(D,A,B,C, 6,5); RD2(C,D,A,B,10, 9); RD2(B,C,D,A,14,13); 
00125   RD2(A,B,C,D, 3,3); RD2(D,A,B,C, 7,5); RD2(C,D,A,B,11, 9); RD2(B,C,D,A,15,13); 
00126 
00127   RD3(A,B,C,D, 0,3); RD3(D,A,B,C, 8,9); RD3(C,D,A,B, 4,11); RD3(B,C,D,A,12,15);
00128   RD3(A,B,C,D, 2,3); RD3(D,A,B,C,10,9); RD3(C,D,A,B, 6,11); RD3(B,C,D,A,14,15);
00129   RD3(A,B,C,D, 1,3); RD3(D,A,B,C, 9,9); RD3(C,D,A,B, 5,11); RD3(B,C,D,A,13,15);
00130   RD3(A,B,C,D, 3,3); RD3(D,A,B,C,11,9); RD3(C,D,A,B, 7,11); RD3(B,C,D,A,15,15);
00131 
00132   state[0] += A;
00133   state[1] += B;
00134   state[2] += C;
00135   state[3] += D;
00136 }
00137 
00138 void md4sum(const Uint8 *input, Uint32 inputLen, Uint8 *result)
00139 {
00140   Uint8 final[128];
00141   Uint32 i, n, m, state[4];
00142 
00143   /* magic initial states */
00144   state[0] = 0x67452301;
00145   state[1] = 0xEFCDAB89;
00146   state[2] = 0x98BADCFE;
00147   state[3] = 0x10325476;
00148 
00149   /* compute number of complete 64-byte segments contained in input */
00150   m = inputLen >> 6;
00151 
00152   /* digest first m segments */
00153   for (i=0; i<m; ++i)
00154     md4step(state, (input + (i << 6)));
00155 
00156   /* build final buffer */
00157   n = inputLen % 64;
00158   memcpy(final, input + (m << 6), n);
00159   final[n] = 0x80;
00160   memset(final + n + 1, 0, 120 - (n + 1));
00161 
00162   inputLen = inputLen << 3;
00163   w2b(final + (n >= 56 ? 120 : 56), &inputLen, 4);
00164 
00165   md4step(state, final);
00166   if (n >= 56)
00167     md4step(state, final + 64);
00168 
00169   /* copy state to result */
00170   w2b(result, state, 16);
00171 }