4 * @brief Implementation of prf_plus_t.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 #include <definitions.h>
29 typedef struct private_prf_plus_t private_prf_plus_t
;
32 * Private data of an prf_plus_t object.
35 struct private_prf_plus_t
{
37 * Public interface of prf_plus_t.
52 * Buffer to store current PRF result.
57 * Already given out bytes in current buffer.
62 * Octet which will be appended to the seed.
64 u_int8_t appending_octet
;
68 * Implementation of prf_plus_t.get_bytes.
70 static void get_bytes(private_prf_plus_t
*this, size_t length
, u_int8_t
*buffer
)
72 chunk_t appending_chunk
;
73 size_t bytes_in_round
;
74 size_t total_bytes_written
= 0;
76 appending_chunk
.ptr
= &(this->appending_octet
);
77 appending_chunk
.len
= 1;
80 { /* still more to do... */
81 if (this->buffer
.len
== this->given_out
)
82 { /* no bytes left in buffer, get next*/
83 this->prf
->get_bytes(this->prf
, this->buffer
, NULL
);
84 this->prf
->get_bytes(this->prf
, this->seed
, NULL
);
85 this->prf
->get_bytes(this->prf
, appending_chunk
, this->buffer
.ptr
);
87 this->appending_octet
++;
89 /* how many bytes can we write in this round ? */
90 bytes_in_round
= min(length
, this->buffer
.len
- this->given_out
);
91 /* copy bytes from buffer with offset */
92 memcpy(buffer
+ total_bytes_written
, this->buffer
.ptr
+ this->given_out
, bytes_in_round
);
94 length
-= bytes_in_round
;
95 this->given_out
+= bytes_in_round
;
96 total_bytes_written
+= bytes_in_round
;
101 * Implementation of prf_plus_t.allocate_bytes.
103 static void allocate_bytes(private_prf_plus_t
*this, size_t length
, chunk_t
*chunk
)
105 chunk
->ptr
= malloc(length
);
107 this->public.get_bytes(&(this->public), length
, chunk
->ptr
);
111 * Implementation of prf_plus_t.destroy.
113 static void destroy(private_prf_plus_t
*this)
115 free(this->buffer
.ptr
);
116 free(this->seed
.ptr
);
121 * Description in header.
123 prf_plus_t
*prf_plus_create(prf_t
*prf
, chunk_t seed
)
125 private_prf_plus_t
*this;
126 chunk_t appending_chunk
;
128 this = malloc_thing(private_prf_plus_t
);
130 /* set public methods */
131 this->public.get_bytes
= (void (*)(prf_plus_t
*,size_t,u_int8_t
*))get_bytes
;
132 this->public.allocate_bytes
= (void (*)(prf_plus_t
*,size_t,chunk_t
*))allocate_bytes
;
133 this->public.destroy
= (void (*)(prf_plus_t
*))destroy
;
138 /* allocate buffer for prf output */
139 this->buffer
.len
= prf
->get_block_size(prf
);
140 this->buffer
.ptr
= malloc(this->buffer
.len
);
142 this->appending_octet
= 0x01;
145 this->seed
.ptr
= clalloc(seed
.ptr
, seed
.len
);
146 this->seed
.len
= seed
.len
;
148 /* do the first run */
149 appending_chunk
.ptr
= &(this->appending_octet
);
150 appending_chunk
.len
= 1;
151 this->prf
->get_bytes(this->prf
, this->seed
, NULL
);
152 this->prf
->get_bytes(this->prf
, appending_chunk
, this->buffer
.ptr
);
154 this->appending_octet
++;
156 return &(this->public);