Emulex Logo
OneCoreā„¢ Storage SDK Release 11.2
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ocs_md5.c
Go to the documentation of this file.
1 /*
2  * This code implements the MD5 message-digest algorithm.
3  * The algorithm is due to Ron Rivest. This code was
4  * written by Colin Plumb in 1993, no copyright is claimed.
5  * This code is in the public domain; do with it what you wish.
6  *
7  * Equivalent code is available from RSA Data Security, Inc.
8  * This code has been tested against that, and is equivalent,
9  * except that you don't need to include two pages of legalese
10  * with every copy.
11  *
12  * To compute the message digest of a chunk of bytes, declare an
13  * MD5Context structure, pass it to MD5Init, call MD5Update as
14  * needed on buffers full of bytes, and then call MD5Final, which
15  * will fill a supplied 16-byte array with the digest.
16  *
17  * Changed so as no longer to depend on Colin Plumb's `usual.h' header
18  * definitions; now uses stuff from dpkg's config.h.
19  * - Ian Jackson <ijackson@nyx.cs.du.edu>.
20  * Still in the public domain.
21  */
22 
23 /*
24  * Copyright (c) 2011-2015, Emulex
25  * All rights reserved.
26  *
27  * Redistribution and use in source and binary forms, with or without
28  * modification, are permitted provided that the following conditions are met:
29  *
30  * 1. Redistributions of source code must retain the above copyright notice,
31  * this list of conditions and the following disclaimer.
32  *
33  * 2. Redistributions in binary form must reproduce the above copyright notice,
34  * this list of conditions and the following disclaimer in the documentation
35  * and/or other materials provided with the distribution.
36  *
37  * 3. Neither the name of the copyright holder nor the names of its contributors
38  * may be used to endorse or promote products derived from this software
39  * without specific prior written permission.
40  *
41  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
42  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
45  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
46  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
47  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
48  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
49  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
51  * POSSIBILITY OF SUCH DAMAGE.
52  *
53  */
54 
55 /**
56  * @file
57  * Implements the standard MD5 routines.
58  */
59 
60 #include "ocs_os.h"
61 #include "ocs_md5.h"
62 
63 static void ocs_md5transform(uint32_t buf[4], uint32_t const in[16]);
64 
65 /*
66  * @brief MD5 initialization. Begins an MD5 operation, writing a new context.
67  *
68  * @param context pointer to a MD5 context structure.
69  */
71 {
72  ctx->buf[0] = 0x67452301;
73  ctx->buf[1] = 0xefcdab89;
74  ctx->buf[2] = 0x98badcfe;
75  ctx->buf[3] = 0x10325476;
76 
77  ctx->bytes[0] = 0;
78  ctx->bytes[1] = 0;
79 }
80 
81 /*
82  * @brief MD5 block update operation.
83  * Continues an MD5 message-digest operation, processing another
84  * message block, and updating the context.
85  *
86  * @param context pointer to a MD5 context structure.
87  * @param input pointer to the new message block.
88  * @param inputLen The number of bytes to process.
89  */
90 void ocs_md5_update(ocs_md5_ctx_t *ctx, md5byte const *buf, unsigned len)
91 {
92  uint32_t t;
93 
94  /* Update byte count */
95  t = ctx->bytes[0];
96  if ((ctx->bytes[0] = t + len) < t)
97  ctx->bytes[1]++; /* Carry from low to high */
98 
99  t = 64 - (t & 0x3f); /* Space available in ctx->in (at least 1) */
100  if (t > len) {
101  memcpy((md5byte *)ctx->in + 64 - t, buf, len);
102  return;
103  }
104  /* First chunk is an odd size */
105  ocs_memcpy((md5byte *)ctx->in + 64 - t, buf, t);
106  /* byteSwap(ctx->in, 16); */
107  ocs_md5transform(ctx->buf, ctx->in);
108  buf += t;
109  len -= t;
110 
111  /* Process data in 64-byte chunks */
112  while (len >= 64) {
113  ocs_memcpy(ctx->in, buf, 64);
114  /* byteSwap(ctx->in, 16); */
115  ocs_md5transform(ctx->buf, ctx->in);
116  buf += 64;
117  len -= 64;
118  }
119 
120  /* Handle any remaining bytes of data. */
121  ocs_memcpy(ctx->in, buf, len);
122 }
123 
124 /*
125  * @brief MD5 finalization.
126  * Ends an MD5 message-digest operation, writing the the message digest
127  * and zeroizing the context.
128  *
129  * @param digest Returns the calculated MD5 digest.
130  * @param context pointer to a MD5 context structure.
131  */
132 void ocs_md5_final(md5byte digest[16], ocs_md5_ctx_t *ctx)
133 {
134  int count = ctx->bytes[0] & 0x3f; /* Number of bytes in ctx->in */
135  md5byte *p = (md5byte *)ctx->in + count;
136 
137  /* Set the first char of padding to 0x80. There is always room. */
138  *p++ = 0x80;
139 
140  /* Bytes of padding needed to make 56 bytes (-8..55) */
141  count = 56 - 1 - count;
142 
143  if (count < 0) { /* Padding forces an extra block */
144  ocs_memset(p, 0, count + 8);
145  /* byteSwap(ctx->in, 16); */
146  ocs_md5transform(ctx->buf, ctx->in);
147  p = (md5byte *)ctx->in;
148  count = 56;
149  }
150  ocs_memset(p, 0, count);
151  /* byteSwap(ctx->in, 14); */
152 
153  /* Append length in bits and transform */
154  ctx->in[14] = ctx->bytes[0] << 3;
155  ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
156  ocs_md5transform(ctx->buf, ctx->in);
157 
158  /* byteSwap(ctx->buf, 4); */
159  ocs_memcpy(digest, ctx->buf, 16);
160  ocs_memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
161 }
162 
163 /* The four core functions - F1 is optimized somewhat */
164 
165 /* #define F1(x, y, z) (x & y | ~x & z) */
166 #define F1(x, y, z) (z ^ (x & (y ^ z)))
167 #define F2(x, y, z) F1(z, x, y)
168 #define F3(x, y, z) (x ^ y ^ z)
169 #define F4(x, y, z) (y ^ (x | ~z))
170 
171 /* This is the central step in the MD5 algorithm. */
172 #define MD5STEP(f,w,x,y,z,in,s) \
173  (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
174 
175 /*
176  * The core of the MD5 algorithm, this alters an existing MD5 hash to
177  * reflect the addition of 16 longwords of new data. MD5Update blocks
178  * the data and converts bytes into longwords for this routine.
179  */
180 void
181 ocs_md5transform(uint32_t buf[4], uint32_t const in[16])
182 {
183  uint32_t a, b, c, d;
184 
185  a = buf[0];
186  b = buf[1];
187  c = buf[2];
188  d = buf[3];
189 
190  MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
191  MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
192  MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
193  MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
194  MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
195  MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
196  MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
197  MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
198  MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
199  MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
200  MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
201  MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
202  MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
203  MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
204  MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
205  MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
206 
207  MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
208  MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
209  MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
210  MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
211  MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
212  MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
213  MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
214  MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
215  MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
216  MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
217  MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
218  MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
219  MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
220  MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
221  MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
222  MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
223 
224  MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
225  MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
226  MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
227  MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
228  MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
229  MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
230  MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
231  MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
232  MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
233  MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
234  MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
235  MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
236  MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
237  MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
238  MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
239  MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
240 
241  MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
242  MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
243  MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
244  MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
245  MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
246  MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
247  MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
248  MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
249  MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
250  MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
251  MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
252  MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
253  MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
254  MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
255  MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
256  MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
257 
258  buf[0] += a;
259  buf[1] += b;
260  buf[2] += c;
261  buf[3] += d;
262 }
uint32_t in[16]
Definition: ocs_md5.h:71
uint32_t buf[4]
Definition: ocs_md5.h:69
#define F4(x, y, z)
Definition: ocs_md5.c:169
#define F2(x, y, z)
Definition: ocs_md5.c:167
void ocs_md5_final(md5byte digest[16], ocs_md5_ctx_t *ctx)
Definition: ocs_md5.c:132
#define MD5STEP(f, w, x, y, z, in, s)
Definition: ocs_md5.c:172
uint32_t bytes[2]
Definition: ocs_md5.h:70
void ocs_md5_init(ocs_md5_ctx_t *ctx)
Definition: ocs_md5.c:70
#define F1(x, y, z)
Definition: ocs_md5.c:166
#define md5byte
Definition: ocs_md5.h:65
static void ocs_md5transform(uint32_t buf[4], uint32_t const in[16])
Definition: ocs_md5.c:181
void ocs_md5_update(ocs_md5_ctx_t *ctx, md5byte const *buf, unsigned len)
Definition: ocs_md5.c:90
#define F3(x, y, z)
Definition: ocs_md5.c:168