2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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>.
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
19 #include "sha1_hasher.h"
21 #include <arpa/inet.h>
23 typedef struct private_sha1_prf_t private_sha1_prf_t
;
24 typedef struct private_sha1_hasher_t private_sha1_hasher_t
;
27 * Private data structure with hasing context.
29 struct private_sha1_hasher_t
{
31 * Public interface for this hasher.
36 * State of the hasher. From sha1_hasher.c, do not change it!
44 * Private data structure with keyed prf context.
46 struct private_sha1_prf_t
{
49 * public prf interface
54 * internal used hasher
56 private_sha1_hasher_t
*hasher
;
62 extern void SHA1Update(private_sha1_hasher_t
* this, u_int8_t
*data
, u_int32_t len
);
65 * Implementation of prf_t.get_bytes.
67 static void get_bytes(private_sha1_prf_t
*this, chunk_t seed
, u_int8_t
*bytes
)
69 u_int32_t
*hash
= (u_int32_t
*)bytes
;
71 SHA1Update(this->hasher
, seed
.ptr
, seed
.len
);
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]);
81 * Implementation of prf_t.get_block_size.
83 static size_t get_block_size(private_sha1_prf_t
*this)
85 return HASH_SIZE_SHA1
;
89 * Implementation of prf_t.allocate_bytes.
91 static void allocate_bytes(private_sha1_prf_t
*this, chunk_t seed
, chunk_t
*chunk
)
93 *chunk
= chunk_alloc(HASH_SIZE_SHA1
);
94 get_bytes(this, seed
, chunk
->ptr
);
98 * Implementation of prf_t.get_key_size.
100 static size_t get_key_size(private_sha1_prf_t
*this)
102 return sizeof(this->hasher
->state
);
106 * Implementation of prf_t.set_key.
108 static void set_key(private_sha1_prf_t
*this, chunk_t key
)
111 u_int32_t
*iv
= (u_int32_t
*)key
.ptr
;
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
++)
117 this->hasher
->state
[i
] ^= htonl(iv
[i
]);
122 * Implementation of prf_t.destroy.
124 static void destroy(private_sha1_prf_t
*this)
126 this->hasher
->public.hasher_interface
.destroy(&this->hasher
->public.hasher_interface
);
133 sha1_prf_t
*sha1_prf_create(pseudo_random_function_t algo
)
135 private_sha1_prf_t
*this;
136 if (algo
!= PRF_KEYED_SHA1
)
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
;
148 this->hasher
= (private_sha1_hasher_t
*)sha1_hasher_create(HASH_SHA1
);
150 return &this->public;