Add a return value to prf_t.allocate_bytes()
[strongswan.git] / src / libstrongswan / plugins / openssl / openssl_sha1_prf.c
1 /*
2 * Copyright (C) 2010 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 "openssl_sha1_prf.h"
17
18 #include <openssl/sha.h>
19
20 typedef struct private_openssl_sha1_prf_t private_openssl_sha1_prf_t;
21
22 /**
23 * Private data of an openssl_sha1_prf_t object.
24 */
25 struct private_openssl_sha1_prf_t {
26
27 /**
28 * Public openssl_sha1_prf_t interface.
29 */
30 openssl_sha1_prf_t public;
31
32 /**
33 * SHA1 context
34 */
35 SHA_CTX ctx;
36 };
37
38 METHOD(prf_t, get_bytes, bool,
39 private_openssl_sha1_prf_t *this, chunk_t seed, u_int8_t *bytes)
40 {
41 SHA1_Update(&this->ctx, seed.ptr, seed.len);
42
43 if (bytes)
44 {
45 u_int32_t *hash = (u_int32_t*)bytes;
46
47 hash[0] = htonl(this->ctx.h0);
48 hash[1] = htonl(this->ctx.h1);
49 hash[2] = htonl(this->ctx.h2);
50 hash[3] = htonl(this->ctx.h3);
51 hash[4] = htonl(this->ctx.h4);
52 }
53
54 return TRUE;
55 }
56
57 METHOD(prf_t, get_block_size, size_t,
58 private_openssl_sha1_prf_t *this)
59 {
60 return HASH_SIZE_SHA1;
61 }
62
63 METHOD(prf_t, allocate_bytes, bool,
64 private_openssl_sha1_prf_t *this, chunk_t seed, chunk_t *chunk)
65 {
66 if (chunk)
67 {
68 *chunk = chunk_alloc(HASH_SIZE_SHA1);
69 return get_bytes(this, seed, chunk->ptr);
70 }
71 return get_bytes(this, seed, NULL);
72 }
73
74 METHOD(prf_t, get_key_size, size_t,
75 private_openssl_sha1_prf_t *this)
76 {
77 return HASH_SIZE_SHA1;
78 }
79
80 METHOD(prf_t, set_key, void,
81 private_openssl_sha1_prf_t *this, chunk_t key)
82 {
83 SHA1_Init(&this->ctx);
84
85 if (key.len >= 4)
86 {
87 this->ctx.h0 ^= untoh32(key.ptr);
88 }
89 if (key.len >= 8)
90 {
91 this->ctx.h1 ^= untoh32(key.ptr + 4);
92 }
93 if (key.len >= 12)
94 {
95 this->ctx.h2 ^= untoh32(key.ptr + 8);
96 }
97 if (key.len >= 16)
98 {
99 this->ctx.h3 ^= untoh32(key.ptr + 12);
100 }
101 if (key.len >= 20)
102 {
103 this->ctx.h4 ^= untoh32(key.ptr + 16);
104 }
105 }
106
107 METHOD(prf_t, destroy, void,
108 private_openssl_sha1_prf_t *this)
109 {
110 free(this);
111 }
112
113 /**
114 * See header
115 */
116 openssl_sha1_prf_t *openssl_sha1_prf_create(pseudo_random_function_t algo)
117 {
118 private_openssl_sha1_prf_t *this;
119
120 if (algo != PRF_KEYED_SHA1)
121 {
122 return NULL;
123 }
124
125 INIT(this,
126 .public = {
127 .prf = {
128 .get_block_size = _get_block_size,
129 .get_bytes = _get_bytes,
130 .allocate_bytes = _allocate_bytes,
131 .get_key_size = _get_key_size,
132 .set_key = _set_key,
133 .destroy = _destroy,
134 },
135 },
136 );
137
138 return &this->public;
139 }
140