aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstrongswan/plugins/bliss/bliss_signature.c
blob: e603da3993ce80f4c4022acd7bebc5f4c7b9c6de (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * Copyright (C) 2014 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 "bliss_signature.h"
#include "bliss_bitpacker.h"
#include "bliss_huffman_coder.h"


typedef struct private_bliss_signature_t private_bliss_signature_t;

/**
 * Private data of a bliss_signature_t object.
 */
struct private_bliss_signature_t {
	/**
	 * Public interface for this signer.
	 */
	bliss_signature_t public;

	/**
	 * BLISS signature parameter set
	 */
	bliss_param_set_t *set;

	/**
	 * BLISS signature vector z1 of size n
	 */
	int32_t *z1;

	/**
	 * BLISS signature vector z2d of size n
	 */
	int16_t *z2d;

	/**
	 * Indices of sparse BLISS challenge vector c of size kappa
	 */
	uint16_t *c_indices;

};

METHOD(bliss_signature_t, get_encoding, chunk_t,
	private_bliss_signature_t *this)
{
	bliss_bitpacker_t *packer;
	bliss_huffman_coder_t *coder;
	bliss_huffman_code_t *code;
	int32_t z1;
	uint32_t z1_sign;
	uint16_t z2d_bits;
	chunk_t encoding = chunk_empty;
	int i;

	z2d_bits = this->set->z1_bits - this->set->d;

	/* Get Huffman code for this BLISS parameter set */
	code = bliss_huffman_code_get_by_id(this->set->id);
	if (!code)
	{
		DBG1(DBG_LIB, "no Huffman code found for parameter set %N",
			 bliss_param_set_id_names, this->set->id);
		return chunk_empty;
	}

	packer = bliss_bitpacker_create(this->set->n * this->set->z1_bits +
									this->set->n * z2d_bits +
									this->set->kappa * this->set->n_bits);
	coder = bliss_huffman_coder_create(code, packer);

	for (i = 0; i < this->set->n; i++)
	{
		/* determine and remove the sign of z1[i]*/
		z1_sign = this->z1[i] < 0;
		z1 = z1_sign ? -this->z1[i] : this->z1[i];

		if (!packer->write_bits(packer, z1_sign, 1) ||
			!packer->write_bits(packer, z1 & 0xff, 8) ||
			!coder->encode(coder, z1 >> 8, this->z2d[i]))
		{
			goto end;
		}
	}
	for (i = 0; i < this->set->kappa; i++)
	{
		if (!packer->write_bits(packer, this->c_indices[i], this->set->n_bits))
		{
			goto end;
		}
	}
	encoding = packer->extract_buf(packer);

	DBG2(DBG_LIB, "efficiency of Huffman coder is %6.4f bits/tuple (%u bits)",
				   coder->get_bits(coder)/(double)(this->set->n), 
				   coder->get_bits(coder));
	DBG2(DBG_LIB, "generated BLISS signature (%u bits encoded in %u bytes)",
				   packer->get_bits(packer), encoding.len);

	end:
	coder->destroy(coder);
	packer->destroy(packer);
	return encoding;
}

METHOD(bliss_signature_t, get_parameters, void,
	private_bliss_signature_t *this, int32_t **z1, int16_t **z2d,
	uint16_t **c_indices)
{
	*z1 = this->z1;
	*z2d = this->z2d;
	*c_indices = this->c_indices;
}

METHOD(bliss_signature_t, destroy, void,
	private_bliss_signature_t *this)
{
	free(this->z1);
	free(this->z2d);
	free(this->c_indices);
	free(this);
}

/**
 * See header.
 */
bliss_signature_t *bliss_signature_create(bliss_param_set_t *set)
{
	private_bliss_signature_t *this;

	INIT(this,
		.public = {
			.get_encoding = _get_encoding,
			.get_parameters = _get_parameters,
			.destroy = _destroy,
		},
		.set = set,
		.z1  = malloc(set->n * sizeof(int32_t)),
		.z2d = malloc(set->n * sizeof(int16_t)),
		.c_indices = malloc(set->n * sizeof(uint16_t)),
	);

	return &this->public;
}

/**
 * See header.
 */
bliss_signature_t *bliss_signature_create_from_data(bliss_param_set_t *set,
													chunk_t encoding)
{
	private_bliss_signature_t *this;
	bliss_bitpacker_t *packer;
	bliss_huffman_coder_t *coder;
	bliss_huffman_code_t *code;
	uint32_t z1_sign, z1_low, value;
	int32_t z1;
	int16_t z2;
	int i;

	/* Get Huffman code for this BLISS parameter set */
	code = bliss_huffman_code_get_by_id(set->id);
	if (!code)
	{
		DBG1(DBG_LIB, "no Huffman code found for parameter set %N",
			 bliss_param_set_id_names, set->id);
		return NULL;
	}

	if (encoding.len == 0)
	{
		DBG1(DBG_LIB, "zero length BLISS signature");
		return NULL;
	}

	INIT(this,
		.public = {
			.get_encoding = _get_encoding,
			.get_parameters = _get_parameters,
			.destroy = _destroy,
		},
		.set = set,
		.z1  = malloc(set->n * sizeof(int32_t)),
		.z2d = malloc(set->n * sizeof(int16_t)),
		.c_indices = malloc(set->n * sizeof(uint16_t)),
	);

	packer = bliss_bitpacker_create_from_data(encoding);
	coder = bliss_huffman_coder_create(code, packer);

	for (i = 0; i < set->n; i++)
	{
		if (!packer->read_bits(packer, &z1_sign, 1) ||
			!packer->read_bits(packer, &z1_low, 8) ||
			!coder->decode(coder, &z1, &z2))
		{
			DBG1(DBG_LIB, "truncated BLISS signature encoding of z1/z2");
			coder->destroy(coder);
			packer->destroy(packer);
			destroy(this);
			return NULL;
		}
		z1 = (z1 << 8) + z1_low;
		this->z1[i] = z1_sign ? -z1 : z1;
		this->z2d[i] = z2;
	}
	coder->destroy(coder);

	for (i = 0; i < set->kappa; i++)
	{
		if (!packer->read_bits(packer, &value, set->n_bits))
		{
			DBG1(DBG_LIB, "truncated BLISS signature encoding of c_indices");
			packer->destroy(packer);
			destroy(this);
			return NULL;
		}
		this->c_indices[i] = value;
	}
	packer->destroy(packer);

	return &this->public;
}