updated copyright information
[strongswan.git] / src / libstrongswan / crypto / prf_plus.c
1 /**
2 * @file prf_plus.c
3 *
4 * @brief Implementation of prf_plus_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
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>.
17 *
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
21 * for more details.
22 */
23
24 #include <string.h>
25
26 #include "prf_plus.h"
27
28 #include <definitions.h>
29
30 typedef struct private_prf_plus_t private_prf_plus_t;
31
32 /**
33 * Private data of an prf_plus_t object.
34 *
35 */
36 struct private_prf_plus_t {
37 /**
38 * Public interface of prf_plus_t.
39 */
40 prf_plus_t public;
41
42 /**
43 * PRF to use.
44 */
45 prf_t *prf;
46
47 /**
48 * Initial seed.
49 */
50 chunk_t seed;
51
52 /**
53 * Buffer to store current PRF result.
54 */
55 chunk_t buffer;
56
57 /**
58 * Already given out bytes in current buffer.
59 */
60 size_t given_out;
61
62 /**
63 * Octet which will be appended to the seed.
64 */
65 u_int8_t appending_octet;
66 };
67
68 /**
69 * Implementation of prf_plus_t.get_bytes.
70 */
71 static void get_bytes(private_prf_plus_t *this, size_t length, u_int8_t *buffer)
72 {
73 chunk_t appending_chunk;
74 size_t bytes_in_round;
75 size_t total_bytes_written = 0;
76
77 appending_chunk.ptr = &(this->appending_octet);
78 appending_chunk.len = 1;
79
80 while (length > 0)
81 { /* still more to do... */
82 if (this->buffer.len == this->given_out)
83 { /* no bytes left in buffer, get next*/
84 this->prf->get_bytes(this->prf, this->buffer, NULL);
85 this->prf->get_bytes(this->prf, this->seed, NULL);
86 this->prf->get_bytes(this->prf, appending_chunk, this->buffer.ptr);
87 this->given_out = 0;
88 this->appending_octet++;
89 }
90 /* how many bytes can we write in this round ? */
91 bytes_in_round = min(length, this->buffer.len - this->given_out);
92 /* copy bytes from buffer with offset */
93 memcpy(buffer + total_bytes_written, this->buffer.ptr + this->given_out, bytes_in_round);
94
95 length -= bytes_in_round;
96 this->given_out += bytes_in_round;
97 total_bytes_written += bytes_in_round;
98 }
99 }
100
101 /**
102 * Implementation of prf_plus_t.allocate_bytes.
103 */
104 static void allocate_bytes(private_prf_plus_t *this, size_t length, chunk_t *chunk)
105 {
106 chunk->ptr = malloc(length);
107 chunk->len = length;
108 this->public.get_bytes(&(this->public), length, chunk->ptr);
109 }
110
111 /**
112 * Implementation of prf_plus_t.destroy.
113 */
114 static void destroy(private_prf_plus_t *this)
115 {
116 free(this->buffer.ptr);
117 free(this->seed.ptr);
118 free(this);
119 }
120
121 /*
122 * Description in header.
123 */
124 prf_plus_t *prf_plus_create(prf_t *prf, chunk_t seed)
125 {
126 private_prf_plus_t *this;
127 chunk_t appending_chunk;
128
129 this = malloc_thing(private_prf_plus_t);
130
131 /* set public methods */
132 this->public.get_bytes = (void (*)(prf_plus_t *,size_t,u_int8_t*))get_bytes;
133 this->public.allocate_bytes = (void (*)(prf_plus_t *,size_t,chunk_t*))allocate_bytes;
134 this->public.destroy = (void (*)(prf_plus_t *))destroy;
135
136 /* take over prf */
137 this->prf = prf;
138
139 /* allocate buffer for prf output */
140 this->buffer.len = prf->get_block_size(prf);
141 this->buffer.ptr = malloc(this->buffer.len);
142
143 this->appending_octet = 0x01;
144
145 /* clone seed */
146 this->seed.ptr = clalloc(seed.ptr, seed.len);
147 this->seed.len = seed.len;
148
149 /* do the first run */
150 appending_chunk.ptr = &(this->appending_octet);
151 appending_chunk.len = 1;
152 this->prf->get_bytes(this->prf, this->seed, NULL);
153 this->prf->get_bytes(this->prf, appending_chunk, this->buffer.ptr);
154 this->given_out = 0;
155 this->appending_octet++;
156
157 return &(this->public);
158 }