used rsa coeff field in OpenPGP secret key payload
[strongswan.git] / src / libstrongswan / plugins / fips_prf / fips_prf.c
1 /*
2 * Copyright (C) 2006 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "fips_prf.h"
17
18 #include <arpa/inet.h>
19
20 #include <debug.h>
21
22 typedef struct private_fips_prf_t private_fips_prf_t;
23
24 /**
25 * Private data of a fips_prf_t object.
26 */
27 struct private_fips_prf_t {
28 /**
29 * Public fips_prf_t interface.
30 */
31 fips_prf_t public;
32
33 /**
34 * key of prf function, "b" long
35 */
36 u_int8_t *key;
37
38 /**
39 * size of "b" in bytes
40 */
41 size_t b;
42
43 /**
44 * Keyed SHA1 prf: It does not use SHA1Final operation
45 */
46 prf_t *keyed_prf;
47
48 /**
49 * G function, either SHA1 or DES
50 */
51 void (*g)(private_fips_prf_t *this, chunk_t c, u_int8_t res[]);
52 };
53
54 /**
55 * sum = (a + b) mod 2 ^ (length * 8)
56 */
57 static void add_mod(size_t length, u_int8_t a[], u_int8_t b[], u_int8_t sum[])
58 {
59 int i, c = 0;
60
61 for(i = length - 1; i >= 0; i--)
62 {
63 u_int32_t tmp;
64
65 tmp = a[i] + b[i] + c;
66 sum[i] = 0xff & tmp;
67 c = tmp >> 8;
68 }
69 }
70
71 /**
72 * calculate "chunk mod 2^(length*8)" and save it into buffer
73 */
74 static void chunk_mod(size_t length, chunk_t chunk, u_int8_t buffer[])
75 {
76 if (chunk.len < length)
77 {
78 /* apply seed as least significant bits, others are zero */
79 memset(buffer, 0, length - chunk.len);
80 memcpy(buffer + length - chunk.len, chunk.ptr, chunk.len);
81 }
82 else
83 {
84 /* use least significant bytes from seed, as we use mod 2^b */
85 memcpy(buffer, chunk.ptr + chunk.len - length, length);
86 }
87 }
88
89 /**
90 * Implementation of prf_t.get_bytes.
91 *
92 * Test vector:
93 *
94 * key:
95 * 0xbd, 0x02, 0x9b, 0xbe, 0x7f, 0x51, 0x96, 0x0b,
96 * 0xcf, 0x9e, 0xdb, 0x2b, 0x61, 0xf0, 0x6f, 0x0f,
97 * 0xeb, 0x5a, 0x38, 0xb6
98 *
99 * seed:
100 * 0x00
101 *
102 * result:
103 * 0x20, 0x70, 0xb3, 0x22, 0x3d, 0xba, 0x37, 0x2f,
104 * 0xde, 0x1c, 0x0f, 0xfc, 0x7b, 0x2e, 0x3b, 0x49,
105 * 0x8b, 0x26, 0x06, 0x14, 0x3c, 0x6c, 0x18, 0xba,
106 * 0xcb, 0x0f, 0x6c, 0x55, 0xba, 0xbb, 0x13, 0x78,
107 * 0x8e, 0x20, 0xd7, 0x37, 0xa3, 0x27, 0x51, 0x16
108 */
109 static void get_bytes(private_fips_prf_t *this, chunk_t seed, u_int8_t w[])
110 {
111 int i;
112 u_int8_t xval[this->b];
113 u_int8_t xseed[this->b];
114 u_int8_t sum[this->b];
115 u_int8_t *xkey = this->key;
116 u_int8_t one[this->b];
117 chunk_t xval_chunk = chunk_from_buf(xval);
118
119 memset(one, 0, this->b);
120 one[this->b - 1] = 0x01;
121
122 /* 3.1 */
123 chunk_mod(this->b, seed, xseed);
124
125 /* 3.2 */
126 for (i = 0; i < 2; i++) /* twice */
127 {
128 /* a. XVAL = (XKEY + XSEED j) mod 2^b */
129 add_mod(this->b, xkey, xseed, xval);
130 DBG3("XVAL %b", xval, this->b);
131 /* b. wi = G(t, XVAL ) */
132 this->g(this, xval_chunk, &w[i * this->b]);
133 DBG3("w[%d] %b", i, &w[i * this->b], this->b);
134 /* c. XKEY = (1 + XKEY + wi) mod 2b */
135 add_mod(this->b, xkey, &w[i * this->b], sum);
136 add_mod(this->b, sum, one, xkey);
137 DBG3("XKEY %b", xkey, this->b);
138 }
139
140 /* 3.3 done already, mod q not used */
141 }
142
143 /**
144 * Implementation of prf_t.get_block_size.
145 */
146 static size_t get_block_size(private_fips_prf_t *this)
147 {
148 return 2 * this->b;
149 }
150 /**
151 * Implementation of prf_t.allocate_bytes.
152 */
153 static void allocate_bytes(private_fips_prf_t *this, chunk_t seed, chunk_t *chunk)
154 {
155 *chunk = chunk_alloc(get_block_size(this));
156 get_bytes(this, seed, chunk->ptr);
157 }
158
159 /**
160 * Implementation of prf_t.get_key_size.
161 */
162 static size_t get_key_size(private_fips_prf_t *this)
163 {
164 return this->b;
165 }
166
167 /**
168 * Implementation of prf_t.set_key.
169 */
170 static void set_key(private_fips_prf_t *this, chunk_t key)
171 {
172 /* save key as "key mod 2^b" */
173 chunk_mod(this->b, key, this->key);
174 }
175
176 /**
177 * Implementation of the G() function based on SHA1
178 */
179 void g_sha1(private_fips_prf_t *this, chunk_t c, u_int8_t res[])
180 {
181 u_int8_t buf[64];
182
183 if (c.len < sizeof(buf))
184 {
185 /* pad c with zeros */
186 memset(buf, 0, sizeof(buf));
187 memcpy(buf, c.ptr, c.len);
188 c.ptr = buf;
189 c.len = sizeof(buf);
190 }
191 else
192 {
193 /* not more than 512 bits can be G()-ed */
194 c.len = sizeof(buf);
195 }
196
197 /* use the keyed hasher, but use an empty key to use SHA1 IV */
198 this->keyed_prf->set_key(this->keyed_prf, chunk_empty);
199 this->keyed_prf->get_bytes(this->keyed_prf, c, res);
200 }
201
202 /**
203 * Implementation of prf_t.destroy.
204 */
205 static void destroy(private_fips_prf_t *this)
206 {
207 this->keyed_prf->destroy(this->keyed_prf);
208 free(this->key);
209 free(this);
210 }
211
212 /*
213 * Described in header.
214 */
215 fips_prf_t *fips_prf_create(pseudo_random_function_t algo)
216 {
217 private_fips_prf_t *this = malloc_thing(private_fips_prf_t);
218
219 this->public.prf_interface.get_bytes = (void (*) (prf_t *,chunk_t,u_int8_t*))get_bytes;
220 this->public.prf_interface.allocate_bytes = (void (*) (prf_t*,chunk_t,chunk_t*))allocate_bytes;
221 this->public.prf_interface.get_block_size = (size_t (*) (prf_t*))get_block_size;
222 this->public.prf_interface.get_key_size = (size_t (*) (prf_t*))get_key_size;
223 this->public.prf_interface.set_key = (void (*) (prf_t *,chunk_t))set_key;
224 this->public.prf_interface.destroy = (void (*) (prf_t *))destroy;
225
226 switch (algo)
227 {
228 case PRF_FIPS_SHA1_160:
229 {
230 this->g = g_sha1;
231 this->b = 20;
232 this->keyed_prf = lib->crypto->create_prf(lib->crypto, PRF_KEYED_SHA1);
233 if (this->keyed_prf == NULL)
234 {
235 free(this);
236 return NULL;
237 }
238 break;
239 }
240 case PRF_FIPS_DES:
241 /* not implemented yet */
242 default:
243 free(this);
244 return NULL;
245 }
246 this->key = malloc(this->b);
247
248 return &this->public;
249 }
250