1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
/*
* Copyright (C) 2013 Andreas Steffen
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "ntru_mgf1.h"
#include <crypto/hashers/hasher.h>
#include <utils/debug.h>
#include <utils/test.h>
typedef struct private_ntru_mgf1_t private_ntru_mgf1_t;
/**
* Private data of an ntru_mgf1_t object.
*/
struct private_ntru_mgf1_t {
/**
* Public ntru_mgf1_t interface.
*/
ntru_mgf1_t public;
/**
* Hasher the MGF1 Mask Generation Function is based on
*/
hasher_t *hasher;
/**
* Counter
*/
u_int32_t counter;
/**
* Set if counter has reached 2^32
*/
bool overflow;
/**
* Current state to be hashed
*/
chunk_t state;
/**
* Position of the 4 octet counter string
*/
u_char *ctr_str;
};
METHOD(ntru_mgf1_t, get_hash_size, size_t,
private_ntru_mgf1_t *this)
{
return this->hasher->get_hash_size(this->hasher);
}
METHOD(ntru_mgf1_t, get_mask, bool,
private_ntru_mgf1_t *this, size_t mask_len, u_char *mask)
{
u_char buf[HASH_SIZE_SHA512];
size_t hash_len;
hash_len = this->hasher->get_hash_size(this->hasher);
while (mask_len > 0)
{
/* detect overflow, set counter string and increment counter */
if (this->overflow)
{
return FALSE;
}
htoun32(this->ctr_str, this->counter++);
if (this->counter == 0)
{
this->overflow = TRUE;
}
/* get the next or final mask block from the hash function */
if (!this->hasher->get_hash(this->hasher, this->state,
(mask_len < hash_len) ? buf : mask))
{
return FALSE;
}
if (mask_len < hash_len)
{
memcpy(mask, buf, mask_len);
return TRUE;
}
mask_len -= hash_len;
mask += hash_len;
}
return TRUE;
}
METHOD(ntru_mgf1_t, allocate_mask, bool,
private_ntru_mgf1_t *this, size_t mask_len, chunk_t *mask)
{
if (mask_len == 0)
{
*mask = chunk_empty;
return TRUE;
}
*mask = chunk_alloc(mask_len);
return get_mask(this, mask_len, mask->ptr);
}
METHOD(ntru_mgf1_t, destroy, void,
private_ntru_mgf1_t *this)
{
this->hasher->destroy(this->hasher);
chunk_clear(&this->state);
free(this);
}
/*
* Described in header.
*/
ntru_mgf1_t *ntru_mgf1_create(hash_algorithm_t alg, chunk_t seed,
bool hash_seed)
{
private_ntru_mgf1_t *this;
hasher_t *hasher;
size_t state_len;
if (seed.len == 0)
{
DBG1(DBG_LIB, "empty seed for MGF1");
return NULL;
}
hasher = lib->crypto->create_hasher(lib->crypto, alg);
if (!hasher)
{
DBG1(DBG_LIB, "failed to create %N hasher for MGF1",
hash_algorithm_names, alg);
return NULL;
}
state_len = (hash_seed ? hasher->get_hash_size(hasher) : seed.len) + 4;
INIT(this,
.public = {
.get_hash_size = _get_hash_size,
.allocate_mask = _allocate_mask,
.get_mask = _get_mask,
.destroy = _destroy,
},
.hasher = hasher,
.state = chunk_alloc(state_len),
);
/* determine position of the 4 octet counter string */
this->ctr_str = this->state.ptr + state_len - 4;
if (hash_seed)
{
if (!hasher->get_hash(hasher, seed, this->state.ptr))
{
DBG1(DBG_LIB, "failed to hash seed for MGF1");
destroy(this);
return NULL;
}
}
else
{
memcpy(this->state.ptr, seed.ptr, seed.len);
}
return &this->public;
}
EXPORT_FUNCTION_FOR_TESTS(ntru, ntru_mgf1_create);
|