4 * @brief Implementation of prf_plus_t.
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
28 typedef struct private_prf_plus_t private_prf_plus_t
;
31 * Private data of an prf_plus_t object.
34 struct private_prf_plus_t
{
36 * Public interface of prf_plus_t.
51 * Buffer to store current PRF result.
56 * Already given out bytes in current buffer.
61 * Octet which will be appended to the seed.
63 u_int8_t appending_octet
;
67 * Implementation of prf_plus_t.get_bytes.
69 static void get_bytes(private_prf_plus_t
*this, size_t length
, u_int8_t
*buffer
)
71 chunk_t appending_chunk
;
72 size_t bytes_in_round
;
73 size_t total_bytes_written
= 0;
75 appending_chunk
.ptr
= &(this->appending_octet
);
76 appending_chunk
.len
= 1;
79 { /* still more to do... */
80 if (this->buffer
.len
== this->given_out
)
81 { /* no bytes left in buffer, get next*/
82 this->prf
->get_bytes(this->prf
, this->buffer
, NULL
);
83 this->prf
->get_bytes(this->prf
, this->seed
, NULL
);
84 this->prf
->get_bytes(this->prf
, appending_chunk
, this->buffer
.ptr
);
86 this->appending_octet
++;
88 /* how many bytes can we write in this round ? */
89 bytes_in_round
= min(length
, this->buffer
.len
- this->given_out
);
90 /* copy bytes from buffer with offset */
91 memcpy(buffer
+ total_bytes_written
, this->buffer
.ptr
+ this->given_out
, bytes_in_round
);
93 length
-= bytes_in_round
;
94 this->given_out
+= bytes_in_round
;
95 total_bytes_written
+= bytes_in_round
;
100 * Implementation of prf_plus_t.allocate_bytes.
102 static void allocate_bytes(private_prf_plus_t
*this, size_t length
, chunk_t
*chunk
)
104 chunk
->ptr
= malloc(length
);
106 this->public.get_bytes(&(this->public), length
, chunk
->ptr
);
110 * Implementation of prf_plus_t.destroy.
112 static void destroy(private_prf_plus_t
*this)
114 free(this->buffer
.ptr
);
115 free(this->seed
.ptr
);
120 * Description in header.
122 prf_plus_t
*prf_plus_create(prf_t
*prf
, chunk_t seed
)
124 private_prf_plus_t
*this;
125 chunk_t appending_chunk
;
127 this = malloc_thing(private_prf_plus_t
);
129 /* set public methods */
130 this->public.get_bytes
= (void (*)(prf_plus_t
*,size_t,u_int8_t
*))get_bytes
;
131 this->public.allocate_bytes
= (void (*)(prf_plus_t
*,size_t,chunk_t
*))allocate_bytes
;
132 this->public.destroy
= (void (*)(prf_plus_t
*))destroy
;
137 /* allocate buffer for prf output */
138 this->buffer
.len
= prf
->get_block_size(prf
);
139 this->buffer
.ptr
= malloc(this->buffer
.len
);
141 this->appending_octet
= 0x01;
144 this->seed
.ptr
= clalloc(seed
.ptr
, seed
.len
);
145 this->seed
.len
= seed
.len
;
147 /* do the first run */
148 appending_chunk
.ptr
= &(this->appending_octet
);
149 appending_chunk
.len
= 1;
150 this->prf
->get_bytes(this->prf
, this->seed
, NULL
);
151 this->prf
->get_bytes(this->prf
, appending_chunk
, this->buffer
.ptr
);
153 this->appending_octet
++;
155 return &(this->public);