Tizen 2.1 base
[platform/core/system/sync-agent.git] / src / framework / security-assistant / md5.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include <string.h>
19 #include "utility/sync_util.h"
20 #include "security-assistant/md5.h"
21
22 #ifndef SYNC_AGENT_LOG
23 #undef LOG_TAG
24 #define LOG_TAG "AF_SA"
25 #endif
26
27 static gint _ie = 0x44332211;
28 static union sa_endian_u {
29         gint i;
30         gchar b[4];
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')
34
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);
37
38 void sa_md5_get_digest(const char *buffer, int buffer_size, unsigned char *digest)
39 {
40         _EXTERN_FUNC_ENTER;
41
42         sa_md5_context_s ctx;
43
44         sa_md5_init(&ctx);
45         sa_md5_update(&ctx, buffer, buffer_size);
46         sa_md5_final(&ctx, digest);
47
48         _EXTERN_FUNC_EXIT;
49 }
50
51 void sa_md5_init(sa_md5_context_s * ctx)
52 {
53         _EXTERN_FUNC_ENTER;
54
55         ctx->buf[0] = 0x67452301;
56         ctx->buf[1] = 0xefcdab89;
57         ctx->buf[2] = 0x98badcfe;
58         ctx->buf[3] = 0x10325476;
59
60         ctx->bits[0] = 0;
61         ctx->bits[1] = 0;
62
63         if (SA_IS_BIG_ENDIAN()) {
64                 ctx->doByteReverse = 1;
65         } else {
66                 ctx->doByteReverse = 0;
67         }
68
69         _EXTERN_FUNC_EXIT;
70 }
71
72 void sa_md5_update(sa_md5_context_s * ctx, const char *buf, unsigned long int len)
73 {
74         _EXTERN_FUNC_ENTER;
75
76         unsigned int t;
77
78         /* Update bitcount */
79
80         t = ctx->bits[0];
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;
84
85         t = (t >> 3) & 0x3f;    /* Bytes already in shsInfo->data */
86
87         /* Handle any leading odd-sized chunks */
88
89         if (t) {
90                 unsigned char *p = (unsigned char *)ctx->in + t;
91
92                 t = 64 - t;
93                 if (len < t) {
94                         memcpy(p, buf, len);
95                         return;
96                 }
97                 memcpy(p, buf, t);
98                 if (ctx->doByteReverse)
99                         _byte_reverse(ctx->in, 16);
100                 _md5_transform(ctx->buf, (const long unsigned int *)ctx->in);
101                 buf += t;
102                 len -= t;
103         }
104         /* Process data in 64-byte chunks */
105
106         while (len >= 64) {
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);
111                 buf += 64;
112                 len -= 64;
113         }
114
115         /* Handle any remaining bytes of data. */
116
117         memcpy(ctx->in, buf, len);
118
119         _EXTERN_FUNC_EXIT;
120 }
121
122 static void _md5_transform(unsigned long int buf[4], const unsigned long int in[16])
123 {
124         _INNER_FUNC_ENTER;
125
126         register unsigned int a, b, c, d;
127
128         a = buf[0];
129         b = buf[1];
130         c = buf[2];
131         d = buf[3];
132
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);
149
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);
166
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);
183
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);
200
201         buf[0] += a;
202         buf[1] += b;
203         buf[2] += c;
204         buf[3] += d;
205
206         _INNER_FUNC_EXIT;
207 }
208
209 static void _byte_reverse(unsigned char *buf, unsigned int longs)
210 {
211         _INNER_FUNC_ENTER;
212
213         unsigned int t;
214         do {
215                 t = (unsigned int)((unsigned int)buf[3] << 8 | buf[2]) << 16 | ((unsigned int)buf[1] << 8 | buf[0]);
216                 *(unsigned int *)buf = t;
217                 buf += 4;
218         } while (--longs);
219
220         _INNER_FUNC_EXIT;
221 }
222
223 void sa_md5_final(sa_md5_context_s * ctx, unsigned char *digest)
224 {
225         _EXTERN_FUNC_ENTER;
226
227         unsigned int count;
228         unsigned char *p;
229
230         /* Compute number of bytes mod 64 */
231         count = (ctx->bits[0] >> 3) & 0x3F;
232
233         /* Set the first char of padding to 0x80.  This is safe since there is
234            always at least one byte free */
235         p = ctx->in + count;
236         *p++ = 0x80;
237
238         /* Bytes of padding needed to make 64 bytes */
239         count = 64 - 1 - count;
240
241         /* Pad out to 56 mod 64 */
242         if (count < 8) {
243                 /* Two lots of padding:  Pad the first block to 64 bytes */
244                 memset(p, 0, count);
245                 if (ctx->doByteReverse)
246                         _byte_reverse(ctx->in, 16);
247                 _md5_transform(ctx->buf, (const long unsigned int *)ctx->in);
248
249                 /* Now fill the next block with 56 bytes */
250                 memset(ctx->in, 0, 56);
251         } else {
252                 /* Pad block to 56 bytes */
253                 memset(p, 0, count - 8);
254         }
255         if (ctx->doByteReverse)
256                 _byte_reverse(ctx->in, 14);
257
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];
261
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);
266
267         _EXTERN_FUNC_EXIT;
268 }