Any of the four NTRU parameter sets can be selected
[strongswan.git] / src / libstrongswan / plugins / ntru / ntru_plugin.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_plugin.h"
17 #include "ntru_ke.h"
18
19 #include <library.h>
20 #include <utils/debug.h>
21
22 typedef struct private_ntru_plugin_t private_ntru_plugin_t;
23
24 rng_t *rng;
25
26 bool ntru_plugin_get_entropy(ENTROPY_CMD cmd, uint8_t *out)
27 {
28 switch (cmd)
29 {
30 case INIT:
31 return TRUE;
32 case GET_NUM_BYTES_PER_BYTE_OF_ENTROPY:
33 /* Here we return the number of bytes needed from the entropy
34 * source to obtain 8 bits of entropy. Maximum is 8.
35 */
36 if (!out)
37 {
38 return FALSE;
39 }
40 *out = 1; /* this is a perfectly random source */
41 return TRUE;
42 case GET_BYTE_OF_ENTROPY:
43 if (!out)
44 {
45 return FALSE;
46 }
47 if (!rng || !rng->get_bytes(rng, 1, out))
48 {
49 return FALSE;
50 }
51 return TRUE;
52 default:
53 return FALSE;
54 }
55 }
56
57 /**
58 * Create/Destroy True Random Generator
59 */
60 static bool create_random(private_ntru_plugin_t *this,
61 plugin_feature_t *feature, bool reg, void *data)
62 {
63 if (reg)
64 {
65 rng = lib->crypto->create_rng(lib->crypto, RNG_TRUE);
66 if (!rng)
67 {
68 return FALSE;
69 }
70 }
71 else
72 {
73 rng->destroy(rng);
74 }
75 return TRUE;
76 }
77
78 /**
79 * private data of ntru_plugin
80 */
81 struct private_ntru_plugin_t {
82
83 /**
84 * public functions
85 */
86 ntru_plugin_t public;
87 };
88
89 METHOD(plugin_t, get_name, char*,
90 private_ntru_plugin_t *this)
91 {
92 return "ntru";
93 }
94
95 METHOD(plugin_t, get_features, int,
96 private_ntru_plugin_t *this, plugin_feature_t *features[])
97 {
98 int count = 0;
99
100 static plugin_feature_t f_ke[] = {
101 PLUGIN_REGISTER(DH, ntru_ke_create),
102 PLUGIN_PROVIDE(DH, NTRU_112_BIT),
103 PLUGIN_PROVIDE(DH, NTRU_128_BIT),
104 PLUGIN_PROVIDE(DH, NTRU_192_BIT),
105 PLUGIN_PROVIDE(DH, NTRU_256_BIT),
106 };
107 static plugin_feature_t f_rng[] = {
108 PLUGIN_CALLBACK((plugin_feature_callback_t)create_random, NULL),
109 PLUGIN_PROVIDE(CUSTOM, "ntru-rng"),
110 PLUGIN_DEPENDS(RNG, RNG_TRUE),
111 };
112 static plugin_feature_t f[countof(f_ke) + countof(f_rng)] = {};
113
114 plugin_features_add(f, f_ke, countof(f_ke), &count);
115 plugin_features_add(f, f_rng, countof(f_rng), &count);
116
117 *features = f;
118 return count;
119 }
120
121 METHOD(plugin_t, destroy, void,
122 private_ntru_plugin_t *this)
123 {
124 free(this);
125 }
126
127 /*
128 * see header file
129 */
130 plugin_t *ntru_plugin_create()
131 {
132 private_ntru_plugin_t *this;
133
134 INIT(this,
135 .public = {
136 .plugin = {
137 .get_name = _get_name,
138 .get_features = _get_features,
139 .destroy = _destroy,
140 },
141 },
142 );
143
144
145 return &this->public.plugin;
146 }