5d8fc599ddd29a0ffa0940fd4443c6778866581c
[strongswan.git] / src / libstrongswan / plugins / ntru / ntru_trits.c
1 /*
2 * Copyright (C) 2013-2014 Andreas Steffen
3 * HSR 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 "ntru_trits.h"
17 #include "ntru_convert.h"
18
19 #include <crypto/mgf1/mgf1.h>
20 #include <utils/debug.h>
21 #include <utils/test.h>
22
23 typedef struct private_ntru_trits_t private_ntru_trits_t;
24
25 /**
26 * Private data of an ntru_trits_t object.
27 */
28 struct private_ntru_trits_t {
29
30 /**
31 * Public ntru_trits_t interface.
32 */
33 ntru_trits_t public;
34
35 /**
36 * Size of the trits array
37 */
38 size_t trits_len;
39
40 /**
41 * Array containing a trit per octet
42 */
43 uint8_t *trits;
44
45 };
46
47 METHOD(ntru_trits_t, get_size, size_t,
48 private_ntru_trits_t *this)
49 {
50 return this->trits_len;
51 }
52
53 METHOD(ntru_trits_t, get_trits, uint8_t*,
54 private_ntru_trits_t *this)
55 {
56 return this->trits;
57 }
58
59 METHOD(ntru_trits_t, destroy, void,
60 private_ntru_trits_t *this)
61 {
62 memwipe(this->trits, this->trits_len);
63 free(this->trits);
64 free(this);
65 }
66
67 /*
68 * Described in header.
69 */
70 ntru_trits_t *ntru_trits_create(size_t len, hash_algorithm_t alg, chunk_t seed)
71 {
72 private_ntru_trits_t *this;
73 uint8_t octets[HASH_SIZE_SHA512], buf[5], *trits;
74 size_t hash_len, octet_count = 0, trits_needed, i;
75 mgf1_t *mgf1;
76
77 DBG2(DBG_LIB, "mgf1 based on %N is seeded with %u octets",
78 hash_algorithm_short_names, alg, seed.len);
79 mgf1 = mgf1_create(alg, seed, TRUE);
80 if (!mgf1)
81 {
82 return NULL;
83 }
84 i = hash_len = mgf1->get_hash_size(mgf1);
85
86 INIT(this,
87 .public = {
88 .get_size = _get_size,
89 .get_trits = _get_trits,
90 .destroy = _destroy,
91 },
92 .trits_len = len,
93 .trits = malloc(len),
94 );
95
96 trits = this->trits;
97 trits_needed = this->trits_len;
98
99 while (trits_needed > 0)
100 {
101 if (i == hash_len)
102 {
103 /* get another block from MGF1 */
104 if (!mgf1->get_mask(mgf1, hash_len, octets))
105 {
106 mgf1->destroy(mgf1);
107 destroy(this);
108 return NULL;
109 }
110 octet_count += hash_len;
111 i = 0;
112 }
113 if (octets[i] < 243) /* 243 = 3^5 */
114 {
115 ntru_octet_2_trits(octets[i], (trits_needed < 5) ? buf : trits);
116 if (trits_needed < 5)
117 {
118 memcpy(trits, buf, trits_needed);
119 break;
120 }
121 trits += 5;
122 trits_needed -= 5;
123 }
124 i++;
125 }
126 DBG2(DBG_LIB, "mgf1 generated %u octets to extract %u trits",
127 octet_count, len);
128 mgf1->destroy(mgf1);
129
130 return &this->public;
131 }
132
133 EXPORT_FUNCTION_FOR_TESTS(ntru, ntru_trits_create);