5 * Copyright (C) 2012 Intel Corporation. All rights reserved.
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.
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.
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
32 #include "src/shared/sha1.h"
34 #define SHA1_MAC_LEN 20
36 static void __hmac_sha1(GChecksum *checksum, const void *key, size_t key_len,
37 const void *data, size_t data_len, void *output)
39 unsigned char ipad[64];
40 unsigned char opad[64];
41 unsigned char digest[SHA1_MAC_LEN];
45 /* if key is longer than 64 bytes reset it to key=SHA1(key) */
47 g_checksum_update(checksum, key, key_len);
48 length = sizeof(digest);
49 g_checksum_get_digest(checksum, digest, &length);
51 g_checksum_reset(checksum);
54 key_len = SHA1_MAC_LEN;
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);
63 /* XOR key with ipad and opad values */
64 for (i = 0; i < 64; i++) {
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);
75 g_checksum_reset(checksum);
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);
83 g_checksum_reset(checksum);
86 int hmac_sha1(const void *key, size_t key_len,
87 const void *data, size_t data_len, void *output, size_t size)
91 checksum = g_checksum_new(G_CHECKSUM_SHA1);
93 __hmac_sha1(checksum, key, key_len, data, data_len, output);
95 g_checksum_free(checksum);
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)
105 unsigned char tmp1[SHA1_MAC_LEN];
106 unsigned char tmp2[SHA1_MAC_LEN];
107 unsigned char buf[36];
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;
116 __hmac_sha1(checksum, password, password_len,
117 buf, salt_len + 4, tmp1);
118 memcpy(digest, tmp1, SHA1_MAC_LEN);
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);
125 for (j = 0; j < SHA1_MAC_LEN; j++)
126 digest[j] ^= tmp2[j];
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)
135 unsigned char *ptr = output;
136 unsigned char digest[SHA1_MAC_LEN];
139 checksum = g_checksum_new(G_CHECKSUM_SHA1);
141 for (i = 1; size > 0; i++) {
144 F(checksum, password, password_len, salt, salt_len,
145 iterations, i, digest);
147 len = size > SHA1_MAC_LEN ? SHA1_MAC_LEN : size;
148 memcpy(ptr, digest, len);
154 g_checksum_free(checksum);
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)
164 unsigned char input[1024];
166 unsigned int i, offset = 0;
168 checksum = g_checksum_new(G_CHECKSUM_SHA1);
170 memcpy(input, prefix, prefix_len);
171 input[prefix_len] = 0;
173 memcpy(input + prefix_len + 1, data, data_len);
174 input[prefix_len + 1 + data_len] = 0;
176 input_len = prefix_len + 1 + data_len + 1;
178 for (i = 0; i < (size + 19) / 20; i++) {
179 __hmac_sha1(checksum, key, key_len, input, input_len,
183 input[input_len - 1]++;
186 g_checksum_free(checksum);