e67b020dd01160917f6be1d9c564410422fd4abc
[platform/upstream/connman.git] / src / shared / sha1.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2012  Intel Corporation. All rights reserved.
6  *
7  *
8  *  This library is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU Lesser General Public
10  *  License as published by the Free Software Foundation; either
11  *  version 2.1 of the License, or (at your option) any later version.
12  *
13  *  This library is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *  Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Lesser General Public
19  *  License along with this library; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <string.h>
29
30 #include <glib.h>
31
32 #include "src/shared/sha1.h"
33
34 #define SHA1_MAC_LEN 20
35
36 static void __hmac_sha1(GChecksum *checksum, const void *key, size_t key_len,
37                         const void *data, size_t data_len, void *output)
38 {
39         unsigned char ipad[64];
40         unsigned char opad[64];
41         unsigned char digest[SHA1_MAC_LEN];
42         size_t length;
43         int i;
44
45         /* if key is longer than 64 bytes reset it to key=SHA1(key) */
46         if (key_len > 64) {
47                 g_checksum_update(checksum, key, key_len);
48                 length = sizeof(digest);
49                 g_checksum_get_digest(checksum, digest, &length);
50
51                 g_checksum_reset(checksum);
52
53                 key = digest;
54                 key_len = SHA1_MAC_LEN;
55         }
56
57         /* start out by storing key in pads */
58         memset(ipad, 0, sizeof(ipad));
59         memset(opad, 0, sizeof(opad));
60         memcpy(ipad, key, key_len);
61         memcpy(opad, key, key_len);
62
63         /* XOR key with ipad and opad values */
64         for (i = 0; i < 64; i++) {
65                 ipad[i] ^= 0x36;
66                 opad[i] ^= 0x5c;
67         }
68
69         /* perform inner SHA1 */
70         g_checksum_update(checksum, ipad, sizeof(ipad));
71         g_checksum_update(checksum, data, data_len);
72         length = sizeof(digest);
73         g_checksum_get_digest(checksum, digest, &length);
74
75         g_checksum_reset(checksum);
76
77         /* perform outer SHA1 */
78         g_checksum_update(checksum, opad, sizeof(opad));
79         g_checksum_update(checksum, digest, length);
80         length = sizeof(digest);
81         g_checksum_get_digest(checksum, output, &length);
82
83         g_checksum_reset(checksum);
84 }
85
86 int hmac_sha1(const void *key, size_t key_len,
87                 const void *data, size_t data_len, void *output, size_t size)
88 {
89         GChecksum *checksum;
90
91         checksum = g_checksum_new(G_CHECKSUM_SHA1);
92
93         __hmac_sha1(checksum, key, key_len, data, data_len, output);
94
95         g_checksum_free(checksum);
96
97         return 0;
98 }
99
100 static void F(GChecksum *checksum, const char *password, size_t password_len,
101                                 const char *salt, size_t salt_len,
102                                 unsigned int iterations, unsigned int count,
103                                                         unsigned char *digest)
104 {
105         unsigned char tmp1[SHA1_MAC_LEN];
106         unsigned char tmp2[SHA1_MAC_LEN];
107         unsigned char buf[36];
108         unsigned int i, j;
109
110         memcpy(buf, salt, salt_len);
111         buf[salt_len + 0] = (count >> 24) & 0xff;
112         buf[salt_len + 1] = (count >> 16) & 0xff;
113         buf[salt_len + 2] = (count >> 8) & 0xff;
114         buf[salt_len + 3] = count & 0xff;
115
116         __hmac_sha1(checksum, password, password_len,
117                                         buf, salt_len + 4, tmp1);
118         memcpy(digest, tmp1, SHA1_MAC_LEN);
119
120         for (i = 1; i < iterations; i++) {
121                 __hmac_sha1(checksum, password, password_len,
122                                                 tmp1, SHA1_MAC_LEN, tmp2);
123                 memcpy(tmp1, tmp2, SHA1_MAC_LEN);
124
125                 for (j = 0; j < SHA1_MAC_LEN; j++)
126                         digest[j] ^= tmp2[j];
127         }
128 }
129
130 int pbkdf2_sha1(const void *password, size_t password_len,
131                         const void *salt, size_t salt_len,
132                         unsigned int iterations, void *output, size_t size)
133 {
134         GChecksum *checksum;
135         unsigned char *ptr = output;
136         unsigned char digest[SHA1_MAC_LEN];
137         unsigned int i;
138
139         checksum = g_checksum_new(G_CHECKSUM_SHA1);
140
141         for (i = 1; size > 0; i++) {
142                 size_t len;
143
144                 F(checksum, password, password_len, salt, salt_len,
145                                                 iterations, i, digest);
146
147                 len = size > SHA1_MAC_LEN ? SHA1_MAC_LEN : size;
148                 memcpy(ptr, digest, len);
149
150                 ptr += len;
151                 size -= len;
152         }
153
154         g_checksum_free(checksum);
155
156         return 0;
157 }
158
159 int prf_sha1(const void *key, size_t key_len,
160                 const void *prefix, size_t prefix_len,
161                 const void *data, size_t data_len, void *output, size_t size)
162 {
163         GChecksum *checksum;
164         unsigned char input[1024];
165         size_t input_len;
166         unsigned int i, offset = 0;
167
168         checksum = g_checksum_new(G_CHECKSUM_SHA1);
169
170         memcpy(input, prefix, prefix_len);
171         input[prefix_len] = 0;
172
173         memcpy(input + prefix_len + 1, data, data_len);
174         input[prefix_len + 1 + data_len] = 0;
175
176         input_len = prefix_len + 1 + data_len + 1;
177
178         for (i = 0; i < (size + 19) / 20; i++) {
179                 __hmac_sha1(checksum, key, key_len, input, input_len,
180                                                         output + offset);
181
182                 offset += 20;
183                 input[input_len - 1]++;
184         }
185
186         g_checksum_free(checksum);
187
188         return 0;
189 }