used rsa coeff field in OpenPGP secret key payload
[strongswan.git] / src / libstrongswan / plugins / sha1 / sha1_prf.c
1 /*
2 * Copyright (C) 2008 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 "sha1_prf.h"
17 #include "sha1_hasher.h"
18
19 #include <arpa/inet.h>
20
21 typedef struct private_sha1_prf_t private_sha1_prf_t;
22 typedef struct private_sha1_hasher_t private_sha1_hasher_t;
23
24 /**
25 * Private data structure with hasing context.
26 */
27 struct private_sha1_hasher_t {
28 /**
29 * Public interface for this hasher.
30 */
31 sha1_hasher_t public;
32
33 /*
34 * State of the hasher. From sha1_hasher.c, do not change it!
35 */
36 u_int32_t state[5];
37 u_int32_t count[2];
38 u_int8_t buffer[64];
39 };
40
41 /**
42 * Private data structure with keyed prf context.
43 */
44 struct private_sha1_prf_t {
45
46 /**
47 * public prf interface
48 */
49 sha1_prf_t public;
50
51 /**
52 * internal used hasher
53 */
54 private_sha1_hasher_t *hasher;
55 };
56
57 /**
58 * From sha1_hasher.c
59 */
60 extern void SHA1Update(private_sha1_hasher_t* this, u_int8_t *data, u_int32_t len);
61
62 /**
63 * Implementation of prf_t.get_bytes.
64 */
65 static void get_bytes(private_sha1_prf_t *this, chunk_t seed, u_int8_t *bytes)
66 {
67 u_int32_t *hash = (u_int32_t*)bytes;
68
69 SHA1Update(this->hasher, seed.ptr, seed.len);
70
71 hash[0] = htonl(this->hasher->state[0]);
72 hash[1] = htonl(this->hasher->state[1]);
73 hash[2] = htonl(this->hasher->state[2]);
74 hash[3] = htonl(this->hasher->state[3]);
75 hash[4] = htonl(this->hasher->state[4]);
76 }
77
78 /**
79 * Implementation of prf_t.get_block_size.
80 */
81 static size_t get_block_size(private_sha1_prf_t *this)
82 {
83 return HASH_SIZE_SHA1;
84 }
85
86 /**
87 * Implementation of prf_t.allocate_bytes.
88 */
89 static void allocate_bytes(private_sha1_prf_t *this, chunk_t seed, chunk_t *chunk)
90 {
91 *chunk = chunk_alloc(HASH_SIZE_SHA1);
92 get_bytes(this, seed, chunk->ptr);
93 }
94
95 /**
96 * Implementation of prf_t.get_key_size.
97 */
98 static size_t get_key_size(private_sha1_prf_t *this)
99 {
100 return sizeof(this->hasher->state);
101 }
102
103 /**
104 * Implementation of prf_t.set_key.
105 */
106 static void set_key(private_sha1_prf_t *this, chunk_t key)
107 {
108 int i, rounds;
109 u_int32_t *iv = (u_int32_t*)key.ptr;
110
111 this->hasher->public.hasher_interface.reset(&this->hasher->public.hasher_interface);
112 rounds = min(key.len/sizeof(u_int32_t), sizeof(this->hasher->state));
113 for (i = 0; i < rounds; i++)
114 {
115 this->hasher->state[i] ^= htonl(iv[i]);
116 }
117 }
118
119 /**
120 * Implementation of prf_t.destroy.
121 */
122 static void destroy(private_sha1_prf_t *this)
123 {
124 this->hasher->public.hasher_interface.destroy(&this->hasher->public.hasher_interface);
125 free(this);
126 }
127
128 /**
129 * see header
130 */
131 sha1_prf_t *sha1_prf_create(pseudo_random_function_t algo)
132 {
133 private_sha1_prf_t *this;
134 if (algo != PRF_KEYED_SHA1)
135 {
136 return NULL;
137 }
138 this = malloc_thing(private_sha1_prf_t);
139 this->public.prf_interface.get_bytes = (void (*) (prf_t *,chunk_t,u_int8_t*))get_bytes;
140 this->public.prf_interface.allocate_bytes = (void (*) (prf_t*,chunk_t,chunk_t*))allocate_bytes;
141 this->public.prf_interface.get_block_size = (size_t (*) (prf_t*))get_block_size;
142 this->public.prf_interface.get_key_size = (size_t (*) (prf_t*))get_key_size;
143 this->public.prf_interface.set_key = (void (*) (prf_t *,chunk_t))set_key;
144 this->public.prf_interface.destroy = (void (*) (prf_t *))destroy;
145
146 this->hasher = (private_sha1_hasher_t*)sha1_hasher_create(HASH_SHA1);
147
148 return &this->public;
149 }
150