better split up of library files "types.h" & "definitions.h"
[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 typedef struct private_prf_plus_t private_prf_plus_t;
29
30 /**
31 * Private data of an prf_plus_t object.
32 *
33 */
34 struct private_prf_plus_t {
35 /**
36 * Public interface of prf_plus_t.
37 */
38 prf_plus_t public;
39
40 /**
41 * PRF to use.
42 */
43 prf_t *prf;
44
45 /**
46 * Initial seed.
47 */
48 chunk_t seed;
49
50 /**
51 * Buffer to store current PRF result.
52 */
53 chunk_t buffer;
54
55 /**
56 * Already given out bytes in current buffer.
57 */
58 size_t given_out;
59
60 /**
61 * Octet which will be appended to the seed.
62 */
63 u_int8_t appending_octet;
64 };
65
66 /**
67 * Implementation of prf_plus_t.get_bytes.
68 */
69 static void get_bytes(private_prf_plus_t *this, size_t length, u_int8_t *buffer)
70 {
71 chunk_t appending_chunk;
72 size_t bytes_in_round;
73 size_t total_bytes_written = 0;
74
75 appending_chunk.ptr = &(this->appending_octet);
76 appending_chunk.len = 1;
77
78 while (length > 0)
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);
85 this->given_out = 0;
86 this->appending_octet++;
87 }
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);
92
93 length -= bytes_in_round;
94 this->given_out += bytes_in_round;
95 total_bytes_written += bytes_in_round;
96 }
97 }
98
99 /**
100 * Implementation of prf_plus_t.allocate_bytes.
101 */
102 static void allocate_bytes(private_prf_plus_t *this, size_t length, chunk_t *chunk)
103 {
104 chunk->ptr = malloc(length);
105 chunk->len = length;
106 this->public.get_bytes(&(this->public), length, chunk->ptr);
107 }
108
109 /**
110 * Implementation of prf_plus_t.destroy.
111 */
112 static void destroy(private_prf_plus_t *this)
113 {
114 free(this->buffer.ptr);
115 free(this->seed.ptr);
116 free(this);
117 }
118
119 /*
120 * Description in header.
121 */
122 prf_plus_t *prf_plus_create(prf_t *prf, chunk_t seed)
123 {
124 private_prf_plus_t *this;
125 chunk_t appending_chunk;
126
127 this = malloc_thing(private_prf_plus_t);
128
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;
133
134 /* take over prf */
135 this->prf = prf;
136
137 /* allocate buffer for prf output */
138 this->buffer.len = prf->get_block_size(prf);
139 this->buffer.ptr = malloc(this->buffer.len);
140
141 this->appending_octet = 0x01;
142
143 /* clone seed */
144 this->seed.ptr = clalloc(seed.ptr, seed.len);
145 this->seed.len = seed.len;
146
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);
152 this->given_out = 0;
153 this->appending_octet++;
154
155 return &(this->public);
156 }