The introduced SHA1_NOFINAL hasher was not sufficient for EAP-AKA,
[strongswan.git] / src / libstrongswan / crypto / prf_plus.c
1 /*
2 * Copyright (C) 2005-2006 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * $Id$
17 */
18
19 #include <string.h>
20
21 #include "prf_plus.h"
22
23 typedef struct private_prf_plus_t private_prf_plus_t;
24
25 /**
26 * Private data of an prf_plus_t object.
27 *
28 */
29 struct private_prf_plus_t {
30 /**
31 * Public interface of prf_plus_t.
32 */
33 prf_plus_t public;
34
35 /**
36 * PRF to use.
37 */
38 prf_t *prf;
39
40 /**
41 * Initial seed.
42 */
43 chunk_t seed;
44
45 /**
46 * Buffer to store current PRF result.
47 */
48 chunk_t buffer;
49
50 /**
51 * Already given out bytes in current buffer.
52 */
53 size_t given_out;
54
55 /**
56 * Octet which will be appended to the seed.
57 */
58 u_int8_t appending_octet;
59 };
60
61 /**
62 * Implementation of prf_plus_t.get_bytes.
63 */
64 static void get_bytes(private_prf_plus_t *this, size_t length, u_int8_t *buffer)
65 {
66 chunk_t appending_chunk;
67 size_t bytes_in_round;
68 size_t total_bytes_written = 0;
69
70 appending_chunk.ptr = &(this->appending_octet);
71 appending_chunk.len = 1;
72
73 while (length > 0)
74 { /* still more to do... */
75 if (this->buffer.len == this->given_out)
76 { /* no bytes left in buffer, get next*/
77 this->prf->get_bytes(this->prf, this->buffer, NULL);
78 this->prf->get_bytes(this->prf, this->seed, NULL);
79 this->prf->get_bytes(this->prf, appending_chunk, this->buffer.ptr);
80 this->given_out = 0;
81 this->appending_octet++;
82 }
83 /* how many bytes can we write in this round ? */
84 bytes_in_round = min(length, this->buffer.len - this->given_out);
85 /* copy bytes from buffer with offset */
86 memcpy(buffer + total_bytes_written, this->buffer.ptr + this->given_out, bytes_in_round);
87
88 length -= bytes_in_round;
89 this->given_out += bytes_in_round;
90 total_bytes_written += bytes_in_round;
91 }
92 }
93
94 /**
95 * Implementation of prf_plus_t.allocate_bytes.
96 */
97 static void allocate_bytes(private_prf_plus_t *this, size_t length, chunk_t *chunk)
98 {
99 chunk->ptr = malloc(length);
100 chunk->len = length;
101 this->public.get_bytes(&(this->public), length, chunk->ptr);
102 }
103
104 /**
105 * Implementation of prf_plus_t.destroy.
106 */
107 static void destroy(private_prf_plus_t *this)
108 {
109 free(this->buffer.ptr);
110 free(this->seed.ptr);
111 free(this);
112 }
113
114 /*
115 * Description in header.
116 */
117 prf_plus_t *prf_plus_create(prf_t *prf, chunk_t seed)
118 {
119 private_prf_plus_t *this;
120 chunk_t appending_chunk;
121
122 this = malloc_thing(private_prf_plus_t);
123
124 /* set public methods */
125 this->public.get_bytes = (void (*)(prf_plus_t *,size_t,u_int8_t*))get_bytes;
126 this->public.allocate_bytes = (void (*)(prf_plus_t *,size_t,chunk_t*))allocate_bytes;
127 this->public.destroy = (void (*)(prf_plus_t *))destroy;
128
129 /* take over prf */
130 this->prf = prf;
131
132 /* allocate buffer for prf output */
133 this->buffer.len = prf->get_block_size(prf);
134 this->buffer.ptr = malloc(this->buffer.len);
135
136 this->appending_octet = 0x01;
137
138 /* clone seed */
139 this->seed.ptr = clalloc(seed.ptr, seed.len);
140 this->seed.len = seed.len;
141
142 /* do the first run */
143 appending_chunk.ptr = &(this->appending_octet);
144 appending_chunk.len = 1;
145 this->prf->get_bytes(this->prf, this->seed, NULL);
146 this->prf->get_bytes(this->prf, appending_chunk, this->buffer.ptr);
147 this->given_out = 0;
148 this->appending_octet++;
149
150 return &(this->public);
151 }