diff options
Diffstat (limited to 'src/include/taler_rsa.h')
-rw-r--r-- | src/include/taler_rsa.h | 360 |
1 files changed, 0 insertions, 360 deletions
diff --git a/src/include/taler_rsa.h b/src/include/taler_rsa.h deleted file mode 100644 index 1d263ae09..000000000 --- a/src/include/taler_rsa.h +++ /dev/null | |||
@@ -1,360 +0,0 @@ | |||
1 | /* NOTE: this is obsolete logic, we should migrate to the | ||
2 | GNUNET_CRYPTO_rsa-API as soon as possible */ | ||
3 | |||
4 | /* | ||
5 | This file is part of TALER | ||
6 | (C) 2014 Christian Grothoff (and other contributing authors) | ||
7 | |||
8 | TALER is free software; you can redistribute it and/or modify it under the | ||
9 | terms of the GNU General Public License as published by the Free Software | ||
10 | Foundation; either version 3, or (at your option) any later version. | ||
11 | |||
12 | TALER is distributed in the hope that it will be useful, but WITHOUT ANY | ||
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | ||
14 | A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
15 | |||
16 | You should have received a copy of the GNU General Public License along with | ||
17 | TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> | ||
18 | */ | ||
19 | |||
20 | /** | ||
21 | * @file include/taler_rsa.h | ||
22 | * @brief RSA key management utilities. Some code is taken from gnunet-0.9.5a | ||
23 | * @author Sree Harsha Totakura <sreeharsha@totakura.in> | ||
24 | * | ||
25 | * Authors of the gnunet code: | ||
26 | * Christian Grothoff | ||
27 | * Krista Bennett | ||
28 | * Gerd Knorr <kraxel@bytesex.org> | ||
29 | * Ioana Patrascu | ||
30 | * Tzvetan Horozov | ||
31 | */ | ||
32 | |||
33 | #ifndef TALER_RSA_H | ||
34 | #define TALER_RSA_H | ||
35 | |||
36 | #include <gnunet/gnunet_common.h> | ||
37 | #include <gnunet/gnunet_crypto_lib.h> | ||
38 | |||
39 | /** | ||
40 | * Length of an RSA KEY (n,e,len), 2048 bit (=256 octests) key n, 2 byte e | ||
41 | */ | ||
42 | #define TALER_RSA_KEY_LENGTH 258 | ||
43 | |||
44 | /** | ||
45 | * @brief Length of RSA encrypted data (2048 bit) | ||
46 | * | ||
47 | * We currently do not handle encryption of data | ||
48 | * that can not be done in a single call to the | ||
49 | * RSA methods (read: large chunks of data). | ||
50 | * We should never need that, as we can use | ||
51 | * the GNUNET_CRYPTO_hash for larger pieces of data for signing, | ||
52 | * and for encryption, we only need to encode sessionkeys! | ||
53 | */ | ||
54 | #define TALER_RSA_DATA_ENCODING_LENGTH 256 | ||
55 | |||
56 | /** | ||
57 | * The private information of an RSA key pair. | ||
58 | */ | ||
59 | struct TALER_RSA_PrivateKey; | ||
60 | |||
61 | |||
62 | GNUNET_NETWORK_STRUCT_BEGIN | ||
63 | |||
64 | /** | ||
65 | * GNUnet mandates a certain format for the encoding | ||
66 | * of private RSA key information that is provided | ||
67 | * by the RSA implementations. This format is used | ||
68 | * to serialize a private RSA key (typically when | ||
69 | * writing it to disk). | ||
70 | */ | ||
71 | struct TALER_RSA_PrivateKeyBinaryEncoded | ||
72 | { | ||
73 | /** | ||
74 | * Total size of the structure, in bytes, in big-endian! | ||
75 | */ | ||
76 | uint16_t len GNUNET_PACKED; | ||
77 | uint16_t sizen GNUNET_PACKED; /* in big-endian! */ | ||
78 | uint16_t sizee GNUNET_PACKED; /* in big-endian! */ | ||
79 | uint16_t sized GNUNET_PACKED; /* in big-endian! */ | ||
80 | uint16_t sizep GNUNET_PACKED; /* in big-endian! */ | ||
81 | uint16_t sizeq GNUNET_PACKED; /* in big-endian! */ | ||
82 | uint16_t sizedmp1 GNUNET_PACKED; /* in big-endian! */ | ||
83 | uint16_t sizedmq1 GNUNET_PACKED; /* in big-endian! */ | ||
84 | /* followed by the actual values */ | ||
85 | }; | ||
86 | GNUNET_NETWORK_STRUCT_END | ||
87 | |||
88 | |||
89 | /** | ||
90 | * @brief an RSA signature | ||
91 | */ | ||
92 | struct TALER_RSA_Signature | ||
93 | { | ||
94 | unsigned char sig[TALER_RSA_DATA_ENCODING_LENGTH]; | ||
95 | }; | ||
96 | |||
97 | GNUNET_NETWORK_STRUCT_BEGIN | ||
98 | /** | ||
99 | * @brief header of what an RSA signature signs | ||
100 | * this must be followed by "size - 8" bytes of | ||
101 | * the actual signed data | ||
102 | */ | ||
103 | struct TALER_RSA_SignaturePurpose | ||
104 | { | ||
105 | /** | ||
106 | * How many bytes does this signature sign? | ||
107 | * (including this purpose header); in network | ||
108 | * byte order (!). | ||
109 | */ | ||
110 | uint32_t size GNUNET_PACKED; | ||
111 | |||
112 | /** | ||
113 | * What does this signature vouch for? This | ||
114 | * must contain a GNUNET_SIGNATURE_PURPOSE_XXX | ||
115 | * constant (from gnunet_signatures.h). In | ||
116 | * network byte order! | ||
117 | */ | ||
118 | uint32_t purpose GNUNET_PACKED; | ||
119 | |||
120 | }; | ||
121 | |||
122 | |||
123 | struct TALER_RSA_BlindedSignaturePurpose | ||
124 | { | ||
125 | unsigned char data[TALER_RSA_DATA_ENCODING_LENGTH]; | ||
126 | }; | ||
127 | |||
128 | |||
129 | /** | ||
130 | * @brief A public key. | ||
131 | */ | ||
132 | struct TALER_RSA_PublicKeyBinaryEncoded | ||
133 | { | ||
134 | /** | ||
135 | * In big-endian, must be GNUNET_CRYPTO_RSA_KEY_LENGTH+4 | ||
136 | */ | ||
137 | uint16_t len GNUNET_PACKED; | ||
138 | |||
139 | /** | ||
140 | * Size of n in key; in big-endian! | ||
141 | */ | ||
142 | uint16_t sizen GNUNET_PACKED; | ||
143 | |||
144 | /** | ||
145 | * The key itself, contains n followed by e. | ||
146 | */ | ||
147 | unsigned char key[TALER_RSA_KEY_LENGTH]; | ||
148 | |||
149 | /** | ||
150 | * Padding (must be 0) | ||
151 | */ | ||
152 | uint16_t padding GNUNET_PACKED; | ||
153 | }; | ||
154 | |||
155 | GNUNET_NETWORK_STRUCT_END | ||
156 | |||
157 | /** | ||
158 | * Create a new private key. Caller must free return value. | ||
159 | * | ||
160 | * @return fresh private key | ||
161 | */ | ||
162 | struct TALER_RSA_PrivateKey * | ||
163 | TALER_RSA_key_create (); | ||
164 | |||
165 | |||
166 | /** | ||
167 | * Free memory occupied by the private key. | ||
168 | * | ||
169 | * @param key pointer to the memory to free | ||
170 | */ | ||
171 | void | ||
172 | TALER_RSA_key_free (struct TALER_RSA_PrivateKey *key); | ||
173 | |||
174 | |||
175 | /** | ||
176 | * Encode the private key in a format suitable for | ||
177 | * storing it into a file. | ||
178 | * @return encoding of the private key | ||
179 | */ | ||
180 | struct TALER_RSA_PrivateKeyBinaryEncoded * | ||
181 | TALER_RSA_encode_key (const struct TALER_RSA_PrivateKey *hostkey); | ||
182 | |||
183 | |||
184 | /** | ||
185 | * Extract the public key of the given private key. | ||
186 | * | ||
187 | * @param priv the private key | ||
188 | * @param pub where to write the public key | ||
189 | */ | ||
190 | void | ||
191 | TALER_RSA_key_get_public (const struct TALER_RSA_PrivateKey *priv, | ||
192 | struct TALER_RSA_PublicKeyBinaryEncoded *pub); | ||
193 | |||
194 | |||
195 | /** | ||
196 | * Decode the private key from the data-format back | ||
197 | * to the "normal", internal format. | ||
198 | * | ||
199 | * @param buf the buffer where the private key data is stored | ||
200 | * @param len the length of the data in 'buffer' | ||
201 | * @return NULL on error | ||
202 | */ | ||
203 | struct TALER_RSA_PrivateKey * | ||
204 | TALER_RSA_decode_key (const char *buf, uint16_t len); | ||
205 | |||
206 | |||
207 | /** | ||
208 | * Convert a public key to a string. | ||
209 | * | ||
210 | * @param pub key to convert | ||
211 | * @return string representing 'pub' | ||
212 | */ | ||
213 | char * | ||
214 | TALER_RSA_public_key_to_string (const struct TALER_RSA_PublicKeyBinaryEncoded *pub); | ||
215 | |||
216 | |||
217 | /** | ||
218 | * Convert a string representing a public key to a public key. | ||
219 | * | ||
220 | * @param enc encoded public key | ||
221 | * @param enclen number of bytes in enc (without 0-terminator) | ||
222 | * @param pub where to store the public key | ||
223 | * @return GNUNET_OK on success | ||
224 | */ | ||
225 | int | ||
226 | TALER_RSA_public_key_from_string (const char *enc, | ||
227 | size_t enclen, | ||
228 | struct TALER_RSA_PublicKeyBinaryEncoded *pub); | ||
229 | |||
230 | |||
231 | /** | ||
232 | * Sign a given block.h | ||
233 | * | ||
234 | * @param key private key to use for the signing | ||
235 | * @param msg the message | ||
236 | * @param size the size of the message | ||
237 | * @param sig where to write the signature | ||
238 | * @return GNUNET_SYSERR on error, GNUNET_OK on success | ||
239 | */ | ||
240 | int | ||
241 | TALER_RSA_sign (const struct TALER_RSA_PrivateKey *key, | ||
242 | const void *msg, | ||
243 | size_t size, | ||
244 | struct TALER_RSA_Signature *sig); | ||
245 | |||
246 | |||
247 | /** | ||
248 | * Verify signature with the given hash. | ||
249 | * | ||
250 | * @param hash the hash code to verify against the signature | ||
251 | * @param sig signature that is being validated | ||
252 | * @param publicKey public key of the signer | ||
253 | * @returns GNUNET_OK if ok, GNUNET_SYSERR if invalid | ||
254 | */ | ||
255 | int | ||
256 | TALER_RSA_hash_verify (const struct GNUNET_HashCode *hash, | ||
257 | const struct TALER_RSA_Signature *sig, | ||
258 | const struct TALER_RSA_PublicKeyBinaryEncoded *publicKey); | ||
259 | |||
260 | |||
261 | /** | ||
262 | * Verify signature on the given message | ||
263 | * | ||
264 | * @param msg the message | ||
265 | * @param size the size of the message | ||
266 | * @param sig signature that is being validated | ||
267 | * @param publicKey public key of the signer | ||
268 | * @returns GNUNET_OK if ok, GNUNET_SYSERR if invalid | ||
269 | */ | ||
270 | int | ||
271 | TALER_RSA_verify (const void *msg, size_t size, | ||
272 | const struct TALER_RSA_Signature *sig, | ||
273 | const struct TALER_RSA_PublicKeyBinaryEncoded *publicKey); | ||
274 | |||
275 | /** | ||
276 | * Key used to blind a message | ||
277 | */ | ||
278 | struct TALER_RSA_BlindingKey; | ||
279 | |||
280 | /** | ||
281 | * Create a blinding key | ||
282 | * | ||
283 | * @return the newly created blinding key | ||
284 | */ | ||
285 | struct TALER_RSA_BlindingKey * | ||
286 | TALER_RSA_blinding_key_create (); | ||
287 | |||
288 | |||
289 | /** | ||
290 | * Destroy a blinding key | ||
291 | * | ||
292 | * @param bkey the blinding key to destroy | ||
293 | */ | ||
294 | void | ||
295 | TALER_RSA_blinding_key_destroy (struct TALER_RSA_BlindingKey *bkey); | ||
296 | |||
297 | |||
298 | /** | ||
299 | * Binary encoding for TALER_RSA_BlindingKey | ||
300 | */ | ||
301 | struct TALER_RSA_BlindingKeyBinaryEncoded | ||
302 | { | ||
303 | unsigned char data[TALER_RSA_DATA_ENCODING_LENGTH]; | ||
304 | }; | ||
305 | |||
306 | |||
307 | /** | ||
308 | * Encode a blinding key | ||
309 | * | ||
310 | * @param bkey the blinding key to encode | ||
311 | * @param bkey_enc where to store the encoded binary key | ||
312 | * @return #GNUNET_OK upon successful encoding; #GNUNET_SYSERR upon failure | ||
313 | */ | ||
314 | int | ||
315 | TALER_RSA_blinding_key_encode (struct TALER_RSA_BlindingKey *bkey, | ||
316 | struct TALER_RSA_BlindingKeyBinaryEncoded *bkey_enc); | ||
317 | |||
318 | |||
319 | /** | ||
320 | * Decode a blinding key from its encoded form | ||
321 | * | ||
322 | * @param bkey_enc the encoded blinding key | ||
323 | * @return the decoded blinding key; NULL upon error | ||
324 | */ | ||
325 | struct TALER_RSA_BlindingKey * | ||
326 | TALER_RSA_blinding_key_decode (struct TALER_RSA_BlindingKeyBinaryEncoded *bkey_enc); | ||
327 | |||
328 | |||
329 | /** | ||
330 | * Blinds the given message with the given blinding key | ||
331 | * | ||
332 | * @param msg the message | ||
333 | * @param size the size of the message | ||
334 | * @param bkey the blinding key | ||
335 | * @param pkey the public key of the signer | ||
336 | * @return the blinding signature purpose; NULL upon any error | ||
337 | */ | ||
338 | struct TALER_RSA_BlindedSignaturePurpose * | ||
339 | TALER_RSA_message_blind (const void *msg, size_t size, | ||
340 | struct TALER_RSA_BlindingKey *bkey, | ||
341 | struct TALER_RSA_PublicKeyBinaryEncoded *pkey); | ||
342 | |||
343 | |||
344 | /** | ||
345 | * Unblind a signature made on blinding signature purpose. The signature | ||
346 | * purpose should have been generated with TALER_RSA_message_blind() function. | ||
347 | * | ||
348 | * @param sig the signature made on the blinded signature purpose | ||
349 | * @param bkey the blinding key used to blind the signature purpose | ||
350 | * @param pkey the public key of the signer | ||
351 | * @return GNUNET_SYSERR upon error; GNUNET_OK upon success. | ||
352 | */ | ||
353 | int | ||
354 | TALER_RSA_unblind (struct TALER_RSA_Signature *sig, | ||
355 | struct TALER_RSA_BlindingKey *bkey, | ||
356 | struct TALER_RSA_PublicKeyBinaryEncoded *pkey); | ||
357 | |||
358 | #endif /* TALER_RSA_H */ | ||
359 | |||
360 | /* end of include/taler_rsa.h */ | ||