Fix #193: Pidgin Status changes stop working (III)
[siplcs.git] / src / core / md4.c
blob7470433818201ab77b36e702d26026db85e73f5e
1 /* vim:set ts=2 sw=2 et cindent: */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Mozilla.
17 * The Initial Developer of the Original Code is IBM Corporation.
18 * Portions created by IBM Corporation are Copyright (C) 2003
19 * IBM Corporation. All Rights Reserved.
21 * Contributor(s):
22 * Darin Fisher <darin@meer.net>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 * Non-functional changes to remove dependencies on NSS headers
41 * Changes are flagged with __SIPE__REMOVE_NSS_DEPENDENCIES__
43 * Added Coverity warning suppression
45 * Copyright (C) 2011-12 SIPE Project <http://sipe.sourceforge.net/>
49 * "clean room" MD4 implementation (see RFC 1320)
52 #ifdef __SIPE__REMOVE_NSS_DEPENDENCIES__
53 #else
54 #include <glib.h>
55 #endif /* __SIPE__REMOVE_NSS_DEPENDENCIES__ */
57 #include <string.h>
58 #include "md4.h"
60 typedef PRUint32 Uint32;
61 typedef PRUint8 Uint8;
63 /* the "conditional" function */
64 #define F(x,y,z) (((x) & (y)) | (~(x) & (z)))
66 /* the "majority" function */
67 #define G(x,y,z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
69 /* the "parity" function */
70 #define H(x,y,z) ((x) ^ (y) ^ (z))
72 /* rotate n-bits to the left */
73 #define ROTL(x,n) (((x) << (n)) | ((x) >> (0x20 - n)))
75 /* round 1: [abcd k s]: a = (a + F(b,c,d) + X[k]) <<< s */
76 #define RD1(a,b,c,d,k,s) a += F(b,c,d) + X[k]; a = ROTL(a,s)
78 /* round 2: [abcd k s]: a = (a + G(b,c,d) + X[k] + MAGIC) <<< s */
79 #define RD2(a,b,c,d,k,s) a += G(b,c,d) + X[k] + 0x5A827999; a = ROTL(a,s)
81 /* round 3: [abcd k s]: a = (a + H(b,c,d) + X[k] + MAGIC) <<< s */
82 #define RD3(a,b,c,d,k,s) a += H(b,c,d) + X[k] + 0x6ED9EBA1; a = ROTL(a,s)
84 /* converts from word array to byte array, len is number of bytes */
85 static void w2b(Uint8 *out, const Uint32 *in, Uint32 len)
87 Uint8 *bp; const Uint32 *wp, *wpend;
89 bp = out;
90 wp = in;
91 wpend = wp + (len >> 2);
93 for (; wp != wpend; ++wp, bp += 4)
95 bp[0] = (Uint8) ((*wp ) & 0xFF);
96 bp[1] = (Uint8) ((*wp >> 8) & 0xFF);
97 bp[2] = (Uint8) ((*wp >> 16) & 0xFF);
98 bp[3] = (Uint8) ((*wp >> 24) & 0xFF);
102 /* converts from byte array to word array, len is number of bytes */
103 static void b2w(Uint32 *out, const Uint8 *in, Uint32 len)
105 Uint32 *wp; const Uint8 *bp, *bpend;
107 wp = out;
108 bp = in;
109 bpend = in + len;
111 for (; bp != bpend; bp += 4, ++wp)
113 *wp = (Uint32) (bp[0] ) |
114 (Uint32) (bp[1] << 8) |
115 (Uint32) (bp[2] << 16) |
116 (Uint32) (bp[3] << 24);
120 /* update state: data is 64 bytes in length */
121 static void md4step(Uint32 state[4], const Uint8 *data)
123 Uint32 A, B, C, D, X[16];
125 b2w(X, data, 64);
127 A = state[0];
128 B = state[1];
129 C = state[2];
130 D = state[3];
132 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);
133 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);
134 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);
135 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);
137 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);
138 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);
139 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);
140 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);
142 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);
143 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);
144 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);
145 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);
147 state[0] += A;
148 state[1] += B;
149 state[2] += C;
150 state[3] += D;
153 void md4sum(const Uint8 *input, Uint32 inputLen, Uint8 *result)
155 Uint8 final[128];
156 Uint32 i, n, m, state[4];
158 /* magic initial states */
159 state[0] = 0x67452301;
160 state[1] = 0xEFCDAB89;
161 state[2] = 0x98BADCFE;
162 state[3] = 0x10325476;
164 /* compute number of complete 64-byte segments contained in input */
165 m = inputLen >> 6;
167 /* digest first m segments */
168 for (i=0; i<m; ++i)
169 md4step(state, (input + (i << 6)));
171 /* build final buffer */
172 n = inputLen % 64;
173 memcpy(final, input + (m << 6), n);
174 final[n] = 0x80;
175 memset(final + n + 1, 0, 120 - (n + 1));
177 inputLen = inputLen << 3;
178 /* This code is correct: w2b() only accesses 4 bytes starting from &inputLen */
179 /* coverity[array_vs_singleton : FALSE] */
180 w2b(final + (n >= 56 ? 120 : 56), &inputLen, 4);
182 md4step(state, final);
183 if (n >= 56)
184 md4step(state, final + 64);
186 /* copy state to result */
187 w2b(result, state, 16);