source: mainline/uspace/lib/crypto/crypto.c@ 562a48b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 562a48b was 8a64320e, checked in by Martin Decky <martin@…>, 11 years ago

pre-merge coding style cleanup and code review

  • Property mode set to 100644
File size: 10.6 KB
RevLine 
[1dcc0b9]1/*
2 * Copyright (c) 2015 Jan Kolarik
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @file crypto.c
[8a64320e]30 *
[1dcc0b9]31 * Cryptographic functions library.
32 */
33
34#include <unistd.h>
35#include <str.h>
36#include <macros.h>
37#include <errno.h>
38#include <byteorder.h>
39#include "crypto.h"
40
[8a64320e]41/** Hash function procedure definition. */
42typedef void (*hash_fnc_t)(uint32_t *, uint32_t *);
[1dcc0b9]43
[8a64320e]44/** Length of HMAC block. */
45#define HMAC_BLOCK_LENGTH 64
[d7dadcb4]46
[8a64320e]47/** Ceiling for uint32_t. */
48#define ceil_uint32(val) \
49 (((val) - (uint32_t) (val)) > 0 ? \
50 (uint32_t) ((val) + 1) : (uint32_t) (val))
[d7dadcb4]51
[8a64320e]52/** Floor for uint32_t. */
53#define floor_uint32(val) \
54 (((val) - (uint32_t) (val)) < 0 ? \
55 (uint32_t) ((val) - 1) : (uint32_t) (val))
[d7dadcb4]56
[8a64320e]57/** Pick value at specified index from array or zero if out of bounds. */
58#define get_at(input, size, i) \
59 ((i) < (size) ? (input[i]) : 0)
[1dcc0b9]60
[8a64320e]61/** Init values used in SHA1 and MD5 functions. */
[d7dadcb4]62static const uint32_t hash_init[] = {
[8a64320e]63 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0
[d7dadcb4]64};
65
[8a64320e]66/** Shift amount array for MD5 algorithm. */
[d7dadcb4]67static const uint32_t md5_shift[] = {
68 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
69 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
70 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
71 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21
72};
73
[8a64320e]74/** Substitution box for MD5 algorithm. */
[d7dadcb4]75static const uint32_t md5_sbox[] = {
76 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
77 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
78 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
79 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
80 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
81 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
82 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
83 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
84 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
85 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
86 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
87 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
88 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
89 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
90 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
91 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
92};
93
[8a64320e]94/** Working procedure of MD5 cryptographic hash function.
95 *
96 * @param h Working array with interim hash parts values.
[d7dadcb4]97 * @param sched_arr Input array with scheduled values from input string.
[8a64320e]98 *
[1dcc0b9]99 */
[d7dadcb4]100static void md5_proc(uint32_t *h, uint32_t *sched_arr)
[1dcc0b9]101{
[d7dadcb4]102 uint32_t f, g, temp;
[8a64320e]103 uint32_t w[HASH_MD5 / 4];
[d7dadcb4]104
[8a64320e]105 memcpy(w, h, (HASH_MD5 / 4) * sizeof(uint32_t));
[d7dadcb4]106
[8a64320e]107 for (size_t k = 0; k < 64; k++) {
108 if (k < 16) {
[d7dadcb4]109 f = (w[1] & w[2]) | (~w[1] & w[3]);
110 g = k;
[8a64320e]111 } else if ((k >= 16) && (k < 32)) {
[d7dadcb4]112 f = (w[1] & w[3]) | (w[2] & ~w[3]);
[8a64320e]113 g = (5 * k + 1) % 16;
114 } else if ((k >= 32) && (k < 48)) {
[d7dadcb4]115 f = w[1] ^ w[2] ^ w[3];
[8a64320e]116 g = (3 * k + 5) % 16;
[d7dadcb4]117 } else {
118 f = w[2] ^ (w[1] | ~w[3]);
[8a64320e]119 g = 7 * k % 16;
[d7dadcb4]120 }
[8a64320e]121
[d7dadcb4]122 temp = w[3];
123 w[3] = w[2];
124 w[2] = w[1];
[8a64320e]125 w[1] += rotl_uint32(w[0] + f + md5_sbox[k] +
126 uint32_t_byteorder_swap(sched_arr[g]),
127 md5_shift[k]);
[d7dadcb4]128 w[0] = temp;
[1dcc0b9]129 }
[d7dadcb4]130
[8a64320e]131 for (uint8_t k = 0; k < HASH_MD5 / 4; k++)
[d7dadcb4]132 h[k] += w[k];
[1dcc0b9]133}
134
[8a64320e]135/** Working procedure of SHA-1 cryptographic hash function.
136 *
137 * @param h Working array with interim hash parts values.
[d7dadcb4]138 * @param sched_arr Input array with scheduled values from input string.
[8a64320e]139 *
[1dcc0b9]140 */
[d7dadcb4]141static void sha1_proc(uint32_t *h, uint32_t *sched_arr)
[1dcc0b9]142{
[d7dadcb4]143 uint32_t f, cf, temp;
[8a64320e]144 uint32_t w[HASH_SHA1 / 4];
[1dcc0b9]145
[8a64320e]146 for (size_t k = 16; k < 80; k++) {
[d7dadcb4]147 sched_arr[k] = rotl_uint32(
[8a64320e]148 sched_arr[k-3] ^
149 sched_arr[k-8] ^
150 sched_arr[k-14] ^
151 sched_arr[k-16],
152 1);
[d7dadcb4]153 }
[1dcc0b9]154
[8a64320e]155 memcpy(w, h, (HASH_SHA1 / 4) * sizeof(uint32_t));
156
157 for (size_t k = 0; k < 80; k++) {
158 if (k < 20) {
[d7dadcb4]159 f = (w[1] & w[2]) | (~w[1] & w[3]);
160 cf = 0x5A827999;
[8a64320e]161 } else if ((k >= 20) && (k < 40)) {
[d7dadcb4]162 f = w[1] ^ w[2] ^ w[3];
[8a64320e]163 cf = 0x6ed9eba1;
164 } else if ((k >= 40) && (k < 60)) {
[d7dadcb4]165 f = (w[1] & w[2]) | (w[1] & w[3]) | (w[2] & w[3]);
[8a64320e]166 cf = 0x8f1bbcdc;
[d7dadcb4]167 } else {
168 f = w[1] ^ w[2] ^ w[3];
[8a64320e]169 cf = 0xca62c1d6;
[d7dadcb4]170 }
[8a64320e]171
[d7dadcb4]172 temp = rotl_uint32(w[0], 5) + f + w[4] + cf + sched_arr[k];
[8a64320e]173
[d7dadcb4]174 w[4] = w[3];
175 w[3] = w[2];
176 w[2] = rotl_uint32(w[1], 30);
177 w[1] = w[0];
178 w[0] = temp;
179 }
[8a64320e]180
181 for (uint8_t k = 0; k < HASH_SHA1 / 4; k++)
[d7dadcb4]182 h[k] += w[k];
[1dcc0b9]183}
184
[8a64320e]185/** Create hash based on selected algorithm.
186 *
187 * @param input Input message byte sequence.
[d7dadcb4]188 * @param input_size Size of message sequence.
[8a64320e]189 * @param output Result hash byte sequence.
190 * @param hash_sel Hash function selector.
191 *
192 * @return EINVAL when input not specified,
193 * ENOMEM when pointer for output hash result
194 * is not allocated, otherwise EOK.
195 *
[1dcc0b9]196 */
[d7dadcb4]197int create_hash(uint8_t *input, size_t input_size, uint8_t *output,
[8a64320e]198 hash_func_t hash_sel)
[1dcc0b9]199{
[8a64320e]200 if (!input)
[1dcc0b9]201 return EINVAL;
202
[8a64320e]203 if (!output)
[1dcc0b9]204 return ENOMEM;
205
[8a64320e]206 hash_fnc_t hash_func = (hash_sel == HASH_MD5) ? md5_proc : sha1_proc;
[1dcc0b9]207
[d7dadcb4]208 /* Prepare scheduled input. */
[1dcc0b9]209 uint8_t work_input[input_size + 1];
210 memcpy(work_input, input, input_size);
211 work_input[input_size] = 0x80;
212
[8a64320e]213 // FIXME: double?
214 size_t blocks = ceil_uint32((((double) input_size + 1) / 4 + 2) / 16);
[d7dadcb4]215 uint32_t work_arr[blocks * 16];
[8a64320e]216 for (size_t i = 0; i < blocks; i++) {
217 for (size_t j = 0; j < 16; j++) {
218 work_arr[i*16 + j] =
219 (get_at(work_input, input_size + 1, i * 64 + j * 4) << 24) |
220 (get_at(work_input, input_size + 1, i * 64 + j * 4 + 1) << 16) |
221 (get_at(work_input, input_size + 1, i * 64 + j * 4 + 2) << 8) |
222 get_at(work_input, input_size + 1, i * 64 + j * 4 + 3);
[1dcc0b9]223 }
224 }
225
[8a64320e]226 uint64_t bits_size = (uint64_t) (input_size * 8);
227 if (hash_sel == HASH_MD5)
[d7dadcb4]228 bits_size = uint64_t_byteorder_swap(bits_size);
229
230 work_arr[(blocks - 1) * 16 + 14] = bits_size >> 32;
[8a64320e]231 work_arr[(blocks - 1) * 16 + 15] = bits_size & 0xffffffff;
[d7dadcb4]232
233 /* Hash computation. */
[8a64320e]234 uint32_t h[hash_sel / 4];
235 memcpy(h, hash_init, (hash_sel / 4) * sizeof(uint32_t));
[d7dadcb4]236 uint32_t sched_arr[80];
[8a64320e]237 for (size_t i = 0; i < blocks; i++) {
238 for (size_t k = 0; k < 16; k++)
239 sched_arr[k] = work_arr[i * 16 + k];
[1dcc0b9]240
[d7dadcb4]241 hash_func(h, sched_arr);
[1dcc0b9]242 }
243
[d7dadcb4]244 /* Copy hash parts into final result. */
[8a64320e]245 for (size_t i = 0; i < hash_sel / 4; i++) {
246 if (hash_sel == HASH_SHA1)
[d7dadcb4]247 h[i] = uint32_t_byteorder_swap(h[i]);
[8a64320e]248
249 memcpy(output + i * sizeof(uint32_t), &h[i], sizeof(uint32_t));
[1dcc0b9]250 }
251
252 return EOK;
253}
254
[8a64320e]255/** Hash-based message authentication code.
256 *
257 * @param key Cryptographic key sequence.
[1dcc0b9]258 * @param key_size Size of key sequence.
[8a64320e]259 * @param msg Message sequence.
[1dcc0b9]260 * @param msg_size Size of message sequence.
[8a64320e]261 * @param hash Output parameter for result hash.
[1dcc0b9]262 * @param hash_sel Hash function selector.
[8a64320e]263 *
264 * @return EINVAL when key or message not specified,
265 * ENOMEM when pointer for output hash result
266 * is not allocated, otherwise EOK.
267 *
[1dcc0b9]268 */
269int hmac(uint8_t *key, size_t key_size, uint8_t *msg, size_t msg_size,
[8a64320e]270 uint8_t *hash, hash_func_t hash_sel)
[1dcc0b9]271{
[8a64320e]272 if ((!key) || (!msg))
[1dcc0b9]273 return EINVAL;
274
[8a64320e]275 if (!hash)
[1dcc0b9]276 return ENOMEM;
277
278 uint8_t work_key[HMAC_BLOCK_LENGTH];
279 uint8_t o_key_pad[HMAC_BLOCK_LENGTH];
280 uint8_t i_key_pad[HMAC_BLOCK_LENGTH];
[d7dadcb4]281 uint8_t temp_hash[hash_sel];
[1dcc0b9]282 memset(work_key, 0, HMAC_BLOCK_LENGTH);
283
[8a64320e]284 if(key_size > HMAC_BLOCK_LENGTH)
[d7dadcb4]285 create_hash(key, key_size, work_key, hash_sel);
[8a64320e]286 else
[1dcc0b9]287 memcpy(work_key, key, key_size);
288
[8a64320e]289 for (size_t i = 0; i < HMAC_BLOCK_LENGTH; i++) {
290 o_key_pad[i] = work_key[i] ^ 0x5c;
[1dcc0b9]291 i_key_pad[i] = work_key[i] ^ 0x36;
292 }
293
[cc575ef9]294 uint8_t temp_work[HMAC_BLOCK_LENGTH + max(msg_size, hash_sel)];
[1dcc0b9]295 memcpy(temp_work, i_key_pad, HMAC_BLOCK_LENGTH);
296 memcpy(temp_work + HMAC_BLOCK_LENGTH, msg, msg_size);
297
[8a64320e]298 create_hash(temp_work, HMAC_BLOCK_LENGTH + msg_size, temp_hash,
299 hash_sel);
[1dcc0b9]300
301 memcpy(temp_work, o_key_pad, HMAC_BLOCK_LENGTH);
[d7dadcb4]302 memcpy(temp_work + HMAC_BLOCK_LENGTH, temp_hash, hash_sel);
[1dcc0b9]303
[d7dadcb4]304 create_hash(temp_work, HMAC_BLOCK_LENGTH + hash_sel, hash, hash_sel);
[1dcc0b9]305
306 return EOK;
307}
308
[8a64320e]309/** Password-Based Key Derivation Function 2.
310 *
311 * As defined in RFC 2898, using HMAC-SHA1 with 4096 iterations
312 * and 32 bytes key result used for WPA/WPA2.
313 *
314 * @param pass Password sequence.
[1dcc0b9]315 * @param pass_size Password sequence length.
[8a64320e]316 * @param salt Salt sequence to be used with password.
[1dcc0b9]317 * @param salt_size Salt sequence length.
[8a64320e]318 * @param hash Output parameter for result hash (32 byte value).
319 *
320 * @return EINVAL when pass or salt not specified,
321 * ENOMEM when pointer for output hash result
322 * is not allocated, otherwise EOK.
323 *
[1dcc0b9]324 */
[8a64320e]325int pbkdf2(uint8_t *pass, size_t pass_size, uint8_t *salt, size_t salt_size,
326 uint8_t *hash)
[1dcc0b9]327{
[8a64320e]328 if ((!pass) || (!salt))
[1dcc0b9]329 return EINVAL;
330
[8a64320e]331 if (!hash)
[1dcc0b9]332 return ENOMEM;
333
[cc575ef9]334 uint8_t work_salt[salt_size + 4];
[1dcc0b9]335 memcpy(work_salt, salt, salt_size);
[a931b7b]336 uint8_t work_hmac[HASH_SHA1];
337 uint8_t temp_hmac[HASH_SHA1];
338 uint8_t xor_hmac[HASH_SHA1];
[8a64320e]339 uint8_t temp_hash[HASH_SHA1 * 2];
[1dcc0b9]340
[8a64320e]341 for (size_t i = 0; i < 2; i++) {
342 uint32_t be_i = host2uint32_t_be(i + 1);
343
[cc575ef9]344 memcpy(work_salt + salt_size, &be_i, 4);
345 hmac(pass, pass_size, work_salt, salt_size + 4,
[8a64320e]346 work_hmac, HASH_SHA1);
[a931b7b]347 memcpy(xor_hmac, work_hmac, HASH_SHA1);
[cc575ef9]348
[8a64320e]349 for (size_t k = 1; k < 4096; k++) {
[a931b7b]350 memcpy(temp_hmac, work_hmac, HASH_SHA1);
[8a64320e]351 hmac(pass, pass_size, temp_hmac, HASH_SHA1,
352 work_hmac, HASH_SHA1);
353
354 for (size_t t = 0; t < HASH_SHA1; t++)
[1dcc0b9]355 xor_hmac[t] ^= work_hmac[t];
356 }
[8a64320e]357
358 memcpy(temp_hash + i * HASH_SHA1, xor_hmac, HASH_SHA1);
[1dcc0b9]359 }
360
361 memcpy(hash, temp_hash, PBKDF2_KEY_LENGTH);
362
363 return EOK;
364}
Note: See TracBrowser for help on using the repository browser.