3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include "utility/sync_util.h"
20 #include "security-assistant/md5.h"
22 #ifndef SYNC_AGENT_LOG
24 #define LOG_TAG "AF_SA"
27 static gint _ie = 0x44332211;
28 static union sa_endian_u {
31 } *_endian = (union sa_endian_u *)&_ie;
32 #define SA_IS_BIG_ENDIAN() (_endian->b[0] == '\x44')
33 #define SA_IS_LITTLE_ENDIAN() (_endian->b[0] == '\x11')
35 static void _md5_transform(unsigned long int buf[4], const unsigned long int in[16]);
36 static void _byte_reverse(unsigned char *buf, unsigned int longs);
38 void sa_md5_get_digest(const char *buffer, int buffer_size, unsigned char *digest)
45 sa_md5_update(&ctx, buffer, buffer_size);
46 sa_md5_final(&ctx, digest);
51 void sa_md5_init(sa_md5_context_s * ctx)
55 ctx->buf[0] = 0x67452301;
56 ctx->buf[1] = 0xefcdab89;
57 ctx->buf[2] = 0x98badcfe;
58 ctx->buf[3] = 0x10325476;
63 if (SA_IS_BIG_ENDIAN()) {
64 ctx->doByteReverse = 1;
66 ctx->doByteReverse = 0;
72 void sa_md5_update(sa_md5_context_s * ctx, const char *buf, unsigned long int len)
81 if ((ctx->bits[0] = t + ((unsigned int)len << 3)) < t)
82 ctx->bits[1]++; /* Carry from low to high */
83 ctx->bits[1] += len >> 29;
85 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
87 /* Handle any leading odd-sized chunks */
90 unsigned char *p = (unsigned char *)ctx->in + t;
98 if (ctx->doByteReverse)
99 _byte_reverse(ctx->in, 16);
100 _md5_transform(ctx->buf, (const long unsigned int *)ctx->in);
104 /* Process data in 64-byte chunks */
107 memcpy(ctx->in, buf, 64);
108 if (ctx->doByteReverse)
109 _byte_reverse(ctx->in, 16);
110 _md5_transform(ctx->buf, (const long unsigned int *)ctx->in);
115 /* Handle any remaining bytes of data. */
117 memcpy(ctx->in, buf, len);
122 static void _md5_transform(unsigned long int buf[4], const unsigned long int in[16])
126 register unsigned int a, b, c, d;
133 SA_MD5_STEP(SA_F1, a, b, c, d, in[0] + 0xd76aa478, 7);
134 SA_MD5_STEP(SA_F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
135 SA_MD5_STEP(SA_F1, c, d, a, b, in[2] + 0x242070db, 17);
136 SA_MD5_STEP(SA_F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
137 SA_MD5_STEP(SA_F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
138 SA_MD5_STEP(SA_F1, d, a, b, c, in[5] + 0x4787c62a, 12);
139 SA_MD5_STEP(SA_F1, c, d, a, b, in[6] + 0xa8304613, 17);
140 SA_MD5_STEP(SA_F1, b, c, d, a, in[7] + 0xfd469501, 22);
141 SA_MD5_STEP(SA_F1, a, b, c, d, in[8] + 0x698098d8, 7);
142 SA_MD5_STEP(SA_F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
143 SA_MD5_STEP(SA_F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
144 SA_MD5_STEP(SA_F1, b, c, d, a, in[11] + 0x895cd7be, 22);
145 SA_MD5_STEP(SA_F1, a, b, c, d, in[12] + 0x6b901122, 7);
146 SA_MD5_STEP(SA_F1, d, a, b, c, in[13] + 0xfd987193, 12);
147 SA_MD5_STEP(SA_F1, c, d, a, b, in[14] + 0xa679438e, 17);
148 SA_MD5_STEP(SA_F1, b, c, d, a, in[15] + 0x49b40821, 22);
150 SA_MD5_STEP(SA_F2, a, b, c, d, in[1] + 0xf61e2562, 5);
151 SA_MD5_STEP(SA_F2, d, a, b, c, in[6] + 0xc040b340, 9);
152 SA_MD5_STEP(SA_F2, c, d, a, b, in[11] + 0x265e5a51, 14);
153 SA_MD5_STEP(SA_F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
154 SA_MD5_STEP(SA_F2, a, b, c, d, in[5] + 0xd62f105d, 5);
155 SA_MD5_STEP(SA_F2, d, a, b, c, in[10] + 0x02441453, 9);
156 SA_MD5_STEP(SA_F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
157 SA_MD5_STEP(SA_F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
158 SA_MD5_STEP(SA_F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
159 SA_MD5_STEP(SA_F2, d, a, b, c, in[14] + 0xc33707d6, 9);
160 SA_MD5_STEP(SA_F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
161 SA_MD5_STEP(SA_F2, b, c, d, a, in[8] + 0x455a14ed, 20);
162 SA_MD5_STEP(SA_F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
163 SA_MD5_STEP(SA_F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
164 SA_MD5_STEP(SA_F2, c, d, a, b, in[7] + 0x676f02d9, 14);
165 SA_MD5_STEP(SA_F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
167 SA_MD5_STEP(SA_F3, a, b, c, d, in[5] + 0xfffa3942, 4);
168 SA_MD5_STEP(SA_F3, d, a, b, c, in[8] + 0x8771f681, 11);
169 SA_MD5_STEP(SA_F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
170 SA_MD5_STEP(SA_F3, b, c, d, a, in[14] + 0xfde5380c, 23);
171 SA_MD5_STEP(SA_F3, a, b, c, d, in[1] + 0xa4beea44, 4);
172 SA_MD5_STEP(SA_F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
173 SA_MD5_STEP(SA_F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
174 SA_MD5_STEP(SA_F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
175 SA_MD5_STEP(SA_F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
176 SA_MD5_STEP(SA_F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
177 SA_MD5_STEP(SA_F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
178 SA_MD5_STEP(SA_F3, b, c, d, a, in[6] + 0x04881d05, 23);
179 SA_MD5_STEP(SA_F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
180 SA_MD5_STEP(SA_F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
181 SA_MD5_STEP(SA_F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
182 SA_MD5_STEP(SA_F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
184 SA_MD5_STEP(SA_F4, a, b, c, d, in[0] + 0xf4292244, 6);
185 SA_MD5_STEP(SA_F4, d, a, b, c, in[7] + 0x432aff97, 10);
186 SA_MD5_STEP(SA_F4, c, d, a, b, in[14] + 0xab9423a7, 15);
187 SA_MD5_STEP(SA_F4, b, c, d, a, in[5] + 0xfc93a039, 21);
188 SA_MD5_STEP(SA_F4, a, b, c, d, in[12] + 0x655b59c3, 6);
189 SA_MD5_STEP(SA_F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
190 SA_MD5_STEP(SA_F4, c, d, a, b, in[10] + 0xffeff47d, 15);
191 SA_MD5_STEP(SA_F4, b, c, d, a, in[1] + 0x85845dd1, 21);
192 SA_MD5_STEP(SA_F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
193 SA_MD5_STEP(SA_F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
194 SA_MD5_STEP(SA_F4, c, d, a, b, in[6] + 0xa3014314, 15);
195 SA_MD5_STEP(SA_F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
196 SA_MD5_STEP(SA_F4, a, b, c, d, in[4] + 0xf7537e82, 6);
197 SA_MD5_STEP(SA_F4, d, a, b, c, in[11] + 0xbd3af235, 10);
198 SA_MD5_STEP(SA_F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
199 SA_MD5_STEP(SA_F4, b, c, d, a, in[9] + 0xeb86d391, 21);
209 static void _byte_reverse(unsigned char *buf, unsigned int longs)
215 t = (unsigned int)((unsigned int)buf[3] << 8 | buf[2]) << 16 | ((unsigned int)buf[1] << 8 | buf[0]);
216 *(unsigned int *)buf = t;
223 void sa_md5_final(sa_md5_context_s * ctx, unsigned char *digest)
230 /* Compute number of bytes mod 64 */
231 count = (ctx->bits[0] >> 3) & 0x3F;
233 /* Set the first char of padding to 0x80. This is safe since there is
234 always at least one byte free */
238 /* Bytes of padding needed to make 64 bytes */
239 count = 64 - 1 - count;
241 /* Pad out to 56 mod 64 */
243 /* Two lots of padding: Pad the first block to 64 bytes */
245 if (ctx->doByteReverse)
246 _byte_reverse(ctx->in, 16);
247 _md5_transform(ctx->buf, (const long unsigned int *)ctx->in);
249 /* Now fill the next block with 56 bytes */
250 memset(ctx->in, 0, 56);
252 /* Pad block to 56 bytes */
253 memset(p, 0, count - 8);
255 if (ctx->doByteReverse)
256 _byte_reverse(ctx->in, 14);
258 /* Append length in bits and transform */
259 ((unsigned int *)ctx->in)[14] = ctx->bits[0];
260 ((unsigned int *)ctx->in)[15] = ctx->bits[1];
262 _md5_transform(ctx->buf, (const long unsigned int *)ctx->in);
263 if (ctx->doByteReverse)
264 _byte_reverse((unsigned char *)ctx->buf, 4);
265 memcpy(digest, ctx->buf, 16);