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