]> git.ipfire.org Git - people/ms/strongswan.git/blob - programs/charon/lib/crypto/prf_plus.c
- import of strongswan-2.7.0
[people/ms/strongswan.git] / programs / charon / lib / 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 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
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>.
16 *
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
20 * for more details.
21 */
22
23 #include <string.h>
24
25 #include "prf_plus.h"
26
27 #include <definitions.h>
28
29 typedef struct private_prf_plus_t private_prf_plus_t;
30
31 /**
32 * Private data of an prf_plus_t object.
33 *
34 */
35 struct private_prf_plus_t {
36 /**
37 * Public interface of prf_plus_t.
38 */
39 prf_plus_t public;
40
41 /**
42 * PRF to use.
43 */
44 prf_t *prf;
45
46 /**
47 * Initial seed.
48 */
49 chunk_t seed;
50
51 /**
52 * Buffer to store current PRF result.
53 */
54 chunk_t buffer;
55
56 /**
57 * Already given out bytes in current buffer.
58 */
59 size_t given_out;
60
61 /**
62 * Octet which will be appended to the seed.
63 */
64 u_int8_t appending_octet;
65 };
66
67 /**
68 * Implementation of prf_plus_t.get_bytes.
69 */
70 static void get_bytes(private_prf_plus_t *this, size_t length, u_int8_t *buffer)
71 {
72 chunk_t appending_chunk;
73 size_t bytes_in_round;
74 size_t total_bytes_written = 0;
75
76 appending_chunk.ptr = &(this->appending_octet);
77 appending_chunk.len = 1;
78
79 while (length > 0)
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);
86 this->given_out = 0;
87 this->appending_octet++;
88 }
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);
93
94 length -= bytes_in_round;
95 this->given_out += bytes_in_round;
96 total_bytes_written += bytes_in_round;
97 }
98 }
99
100 /**
101 * Implementation of prf_plus_t.allocate_bytes.
102 */
103 static void allocate_bytes(private_prf_plus_t *this, size_t length, chunk_t *chunk)
104 {
105 chunk->ptr = malloc(length);
106 chunk->len = length;
107 this->public.get_bytes(&(this->public), length, chunk->ptr);
108 }
109
110 /**
111 * Implementation of prf_plus_t.destroy.
112 */
113 static void destroy(private_prf_plus_t *this)
114 {
115 free(this->buffer.ptr);
116 free(this->seed.ptr);
117 free(this);
118 }
119
120 /*
121 * Description in header.
122 */
123 prf_plus_t *prf_plus_create(prf_t *prf, chunk_t seed)
124 {
125 private_prf_plus_t *this;
126 chunk_t appending_chunk;
127
128 this = malloc_thing(private_prf_plus_t);
129
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;
134
135 /* take over prf */
136 this->prf = prf;
137
138 /* allocate buffer for prf output */
139 this->buffer.len = prf->get_block_size(prf);
140 this->buffer.ptr = malloc(this->buffer.len);
141
142 this->appending_octet = 0x01;
143
144 /* clone seed */
145 this->seed.ptr = clalloc(seed.ptr, seed.len);
146 this->seed.len = seed.len;
147
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);
153 this->given_out = 0;
154 this->appending_octet++;
155
156 return &(this->public);
157 }