Implemented ntru_trits class
[strongswan.git] / src / libstrongswan / plugins / ntru / ntru_trits.c
1 /*
2 * Copyright (C) 2013 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_mgf1.h"
18
19 #include "ntru_crypto/ntru_crypto_ntru_convert.h"
20
21 #include <utils/debug.h>
22 #include <utils/test.h>
23
24 typedef struct private_ntru_trits_t private_ntru_trits_t;
25
26 /**
27 * Private data of an ntru_trits_t object.
28 */
29 struct private_ntru_trits_t {
30
31 /**
32 * Public ntru_trits_t interface.
33 */
34 ntru_trits_t public;
35
36 /**
37 * Size of the trits array
38 */
39 size_t trits_len;
40
41 /**
42 * Array containing a trit per octet
43 */
44 uint8_t *trits;
45
46 };
47
48 METHOD(ntru_trits_t, get_size, size_t,
49 private_ntru_trits_t *this)
50 {
51 return this->trits_len;
52 }
53
54 METHOD(ntru_trits_t, get_trits, uint8_t*,
55 private_ntru_trits_t *this)
56 {
57 return this->trits;
58 }
59
60 METHOD(ntru_trits_t, destroy, void,
61 private_ntru_trits_t *this)
62 {
63 memwipe(this->trits, this->trits_len);
64 free(this->trits);
65 free(this);
66 }
67
68 /*
69 * Described in header.
70 */
71 ntru_trits_t *ntru_trits_create(size_t len, hash_algorithm_t alg, chunk_t seed)
72 {
73 private_ntru_trits_t *this;
74 uint8_t octets[HASH_SIZE_SHA512], buf[5], *trits;
75 size_t hash_len, octet_count = 0, trits_needed, i;
76 ntru_mgf1_t *mgf1;
77
78 DBG2(DBG_LIB, "MGF1 is seeded with %u bytes", seed.len);
79 mgf1 = ntru_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 generates %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);