Created framework for BLISS post-quantum signature algorithm
[strongswan.git] / src / libstrongswan / plugins / bliss / bliss_private_key.c
1 /*
2 * Copyright (C) 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 "bliss_private_key.h"
17
18 typedef struct private_bliss_private_key_t private_bliss_private_key_t;
19
20 /**
21 * Private data of a bliss_private_key_t object.
22 */
23 struct private_bliss_private_key_t {
24 /**
25 * Public interface for this signer.
26 */
27 bliss_private_key_t public;
28
29
30 /**
31 * BLISS type
32 */
33 u_int key_size;
34
35 /**
36 * reference count
37 */
38 refcount_t ref;
39 };
40
41 METHOD(private_key_t, get_type, key_type_t,
42 private_bliss_private_key_t *this)
43 {
44 return KEY_BLISS;
45 }
46
47 METHOD(private_key_t, sign, bool,
48 private_bliss_private_key_t *this, signature_scheme_t scheme,
49 chunk_t data, chunk_t *signature)
50 {
51 switch (scheme)
52 {
53 case SIGN_BLISS_I_SHA256:
54 return FALSE;
55 case SIGN_BLISS_IV_SHA384:
56 return FALSE;
57 default:
58 DBG1(DBG_LIB, "signature scheme %N not supported with BLISS",
59 signature_scheme_names, scheme);
60 return FALSE;
61 }
62 }
63
64 METHOD(private_key_t, decrypt, bool,
65 private_bliss_private_key_t *this, encryption_scheme_t scheme,
66 chunk_t crypto, chunk_t *plain)
67 {
68 DBG1(DBG_LIB, "encryption scheme %N not supported",
69 encryption_scheme_names, scheme);
70 return FALSE;
71 }
72
73 METHOD(private_key_t, get_keysize, int,
74 private_bliss_private_key_t *this)
75 {
76 return this->key_size;
77 }
78
79 METHOD(private_key_t, get_public_key, public_key_t*,
80 private_bliss_private_key_t *this)
81 {
82 public_key_t *public = NULL;
83
84 return public;
85 }
86
87 METHOD(private_key_t, get_encoding, bool,
88 private_bliss_private_key_t *this, cred_encoding_type_t type,
89 chunk_t *encoding)
90 {
91 bool success = TRUE;
92
93 *encoding = chunk_empty;
94
95 return success;
96 }
97
98 METHOD(private_key_t, get_fingerprint, bool,
99 private_bliss_private_key_t *this, cred_encoding_type_t type, chunk_t *fp)
100 {
101 bool success = FALSE;
102
103 return success;
104 }
105
106 METHOD(private_key_t, get_ref, private_key_t*,
107 private_bliss_private_key_t *this)
108 {
109 ref_get(&this->ref);
110 return &this->public.key;
111 }
112
113 METHOD(private_key_t, destroy, void,
114 private_bliss_private_key_t *this)
115 {
116 if (ref_put(&this->ref))
117 {
118 free(this);
119 }
120 }
121
122 /**
123 * Internal generic constructor
124 */
125 static private_bliss_private_key_t *bliss_private_key_create_empty(void)
126 {
127 private_bliss_private_key_t *this;
128
129 INIT(this,
130 .public = {
131 .key = {
132 .get_type = _get_type,
133 .sign = _sign,
134 .decrypt = _decrypt,
135 .get_keysize = _get_keysize,
136 .get_public_key = _get_public_key,
137 .equals = private_key_equals,
138 .belongs_to = private_key_belongs_to,
139 .get_fingerprint = _get_fingerprint,
140 .has_fingerprint = private_key_has_fingerprint,
141 .get_encoding = _get_encoding,
142 .get_ref = _get_ref,
143 .destroy = _destroy,
144 },
145 },
146 .ref = 1,
147 );
148 return this;
149 }
150
151 /**
152 * See header.
153 */
154 bliss_private_key_t *bliss_private_key_gen(key_type_t type, va_list args)
155 {
156 private_bliss_private_key_t *this;
157 u_int key_size = 1;
158
159 while (TRUE)
160 {
161 switch (va_arg(args, builder_part_t))
162 {
163 case BUILD_KEY_SIZE:
164 key_size = va_arg(args, u_int);
165 continue;
166 case BUILD_END:
167 break;
168 default:
169 return NULL;
170 }
171 break;
172 }
173
174 /* Only BLISS-I and BLISS-IV are supported */
175 if (key_size != 1 && key_size != 4)
176 {
177 return NULL;
178 }
179
180 this = bliss_private_key_create_empty();
181 this->key_size = key_size;
182
183 return &this->public;
184 }
185
186 /**
187 * See header.
188 */
189 bliss_private_key_t *bliss_private_key_load(key_type_t type, va_list args)
190 {
191 private_bliss_private_key_t *this;
192
193 while (TRUE)
194 {
195 switch (va_arg(args, builder_part_t))
196 {
197 default:
198 return NULL;
199 }
200 break;
201 }
202
203 this = bliss_private_key_create_empty();
204
205 return &this->public;
206 }
207