2 * Copyright (C) 2010 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
16 #include <openssl/opensslconf.h>
18 #ifndef OPENSSL_NO_SHA1
20 #include "openssl_sha1_prf.h"
22 #include <openssl/sha.h>
24 typedef struct private_openssl_sha1_prf_t private_openssl_sha1_prf_t
;
27 * Private data of an openssl_sha1_prf_t object.
29 struct private_openssl_sha1_prf_t
{
32 * Public openssl_sha1_prf_t interface.
34 openssl_sha1_prf_t
public;
42 METHOD(prf_t
, get_bytes
, bool,
43 private_openssl_sha1_prf_t
*this, chunk_t seed
, u_int8_t
*bytes
)
45 SHA1_Update(&this->ctx
, seed
.ptr
, seed
.len
);
49 u_int32_t
*hash
= (u_int32_t
*)bytes
;
51 hash
[0] = htonl(this->ctx
.h0
);
52 hash
[1] = htonl(this->ctx
.h1
);
53 hash
[2] = htonl(this->ctx
.h2
);
54 hash
[3] = htonl(this->ctx
.h3
);
55 hash
[4] = htonl(this->ctx
.h4
);
61 METHOD(prf_t
, get_block_size
, size_t,
62 private_openssl_sha1_prf_t
*this)
64 return HASH_SIZE_SHA1
;
67 METHOD(prf_t
, allocate_bytes
, bool,
68 private_openssl_sha1_prf_t
*this, chunk_t seed
, chunk_t
*chunk
)
72 *chunk
= chunk_alloc(HASH_SIZE_SHA1
);
73 return get_bytes(this, seed
, chunk
->ptr
);
75 return get_bytes(this, seed
, NULL
);
78 METHOD(prf_t
, get_key_size
, size_t,
79 private_openssl_sha1_prf_t
*this)
81 return HASH_SIZE_SHA1
;
84 METHOD(prf_t
, set_key
, bool,
85 private_openssl_sha1_prf_t
*this, chunk_t key
)
87 SHA1_Init(&this->ctx
);
95 this->ctx
.h0
^= untoh32(key
.ptr
);
99 this->ctx
.h1
^= untoh32(key
.ptr
+ 4);
103 this->ctx
.h2
^= untoh32(key
.ptr
+ 8);
107 this->ctx
.h3
^= untoh32(key
.ptr
+ 12);
111 this->ctx
.h4
^= untoh32(key
.ptr
+ 16);
116 METHOD(prf_t
, destroy
, void,
117 private_openssl_sha1_prf_t
*this)
125 openssl_sha1_prf_t
*openssl_sha1_prf_create(pseudo_random_function_t algo
)
127 private_openssl_sha1_prf_t
*this;
129 if (algo
!= PRF_KEYED_SHA1
)
137 .get_block_size
= _get_block_size
,
138 .get_bytes
= _get_bytes
,
139 .allocate_bytes
= _allocate_bytes
,
140 .get_key_size
= _get_key_size
,
147 return &this->public;
150 #endif /* OPENSSL_NO_SHA1 */