Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * SHA1 T-PRF for EAP-FAST |
| 3 | * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * Alternatively, this software may be distributed under the terms of BSD |
| 10 | * license. |
| 11 | * |
| 12 | * See README and COPYING for more details. |
| 13 | */ |
| 14 | |
| 15 | #include "includes.h" |
| 16 | |
| 17 | #include "common.h" |
| 18 | #include "sha1.h" |
| 19 | #include "crypto.h" |
| 20 | |
| 21 | /** |
| 22 | * sha1_t_prf - EAP-FAST Pseudo-Random Function (T-PRF) |
| 23 | * @key: Key for PRF |
| 24 | * @key_len: Length of the key in bytes |
| 25 | * @label: A unique label for each purpose of the PRF |
| 26 | * @seed: Seed value to bind into the key |
| 27 | * @seed_len: Length of the seed |
| 28 | * @buf: Buffer for the generated pseudo-random key |
| 29 | * @buf_len: Number of bytes of key to generate |
| 30 | * Returns: 0 on success, -1 of failure |
| 31 | * |
| 32 | * This function is used to derive new, cryptographically separate keys from a |
| 33 | * given key for EAP-FAST. T-PRF is defined in RFC 4851, Section 5.5. |
| 34 | */ |
| 35 | int sha1_t_prf(const u8 *key, size_t key_len, const char *label, |
| 36 | const u8 *seed, size_t seed_len, u8 *buf, size_t buf_len) |
| 37 | { |
| 38 | unsigned char counter = 0; |
| 39 | size_t pos, plen; |
| 40 | u8 hash[SHA1_MAC_LEN]; |
| 41 | size_t label_len = os_strlen(label); |
| 42 | u8 output_len[2]; |
| 43 | const unsigned char *addr[5]; |
| 44 | size_t len[5]; |
| 45 | |
| 46 | addr[0] = hash; |
| 47 | len[0] = 0; |
| 48 | addr[1] = (unsigned char *) label; |
| 49 | len[1] = label_len + 1; |
| 50 | addr[2] = seed; |
| 51 | len[2] = seed_len; |
| 52 | addr[3] = output_len; |
| 53 | len[3] = 2; |
| 54 | addr[4] = &counter; |
| 55 | len[4] = 1; |
| 56 | |
| 57 | output_len[0] = (buf_len >> 8) & 0xff; |
| 58 | output_len[1] = buf_len & 0xff; |
| 59 | pos = 0; |
| 60 | while (pos < buf_len) { |
| 61 | counter++; |
| 62 | plen = buf_len - pos; |
| 63 | if (hmac_sha1_vector(key, key_len, 5, addr, len, hash)) |
| 64 | return -1; |
| 65 | if (plen >= SHA1_MAC_LEN) { |
| 66 | os_memcpy(&buf[pos], hash, SHA1_MAC_LEN); |
| 67 | pos += SHA1_MAC_LEN; |
| 68 | } else { |
| 69 | os_memcpy(&buf[pos], hash, plen); |
| 70 | break; |
| 71 | } |
| 72 | len[0] = SHA1_MAC_LEN; |
| 73 | } |
| 74 | |
| 75 | return 0; |
| 76 | } |