GNUnet 0.25.2-11-g84e94e98c
 
Loading...
Searching...
No Matches
crypto_pkey.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2013, 2016, 2021 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
27#include "platform.h"
28#include "gnunet_util_lib.h"
29
30
33{
34 switch (type)
35 {
38 return GNUNET_OK;
39 default:
40 return GNUNET_SYSERR;
41 }
42 return GNUNET_SYSERR;
43}
44
45
46void
48{
49 switch (ntohl (key->type))
50 {
53 break;
56 break;
57 default:
58 GNUNET_break (0);
59 }
60}
61
62
63ssize_t
66{
67 switch (ntohl (key->type))
68 {
70 return sizeof (key->type) + sizeof (key->ecdsa_key);
71 break;
73 return sizeof (key->type) + sizeof (key->eddsa_key);
74 break;
75 default:
77 "Got key type %u\n", ntohl (key->type));
78 GNUNET_break (0);
79 }
80 return -1;
81}
82
83
84ssize_t
87{
88 switch (ntohl (key->type))
89 {
91 return sizeof (key->type) + sizeof (key->ecdsa_key);
93 return sizeof (key->type) + sizeof (key->eddsa_key);
94 default:
95 GNUNET_break (0);
96 }
97 return -1;
98}
99
100
103 size_t len,
104 struct
106 key,
107 size_t *kb_read)
108{
109 ssize_t length;
110 if (len < sizeof (key->type))
111 return GNUNET_SYSERR;
112 GNUNET_memcpy (&key->type,
113 buffer,
114 sizeof (key->type));
116 if (len < length)
117 return GNUNET_SYSERR;
118 if (length < 0)
119 return GNUNET_SYSERR;
120 GNUNET_memcpy (&key->ecdsa_key,
121 buffer + sizeof (key->type),
122 length - sizeof (key->type));
123 *kb_read = length;
124 return GNUNET_OK;
125}
126
127
128ssize_t
131 key,
132 void*buffer,
133 size_t len)
134{
135 const ssize_t length = GNUNET_CRYPTO_public_key_get_length (key);
136 if (len < length)
137 return -1;
138 if (length < 0)
139 return -2;
140 GNUNET_memcpy (buffer, &(key->type), sizeof (key->type));
141 GNUNET_memcpy (buffer + sizeof (key->type), &(key->ecdsa_key), length
142 - sizeof (key->type));
143 return length;
144}
145
146
149 size_t len,
150 struct
152 key,
153 size_t *kb_read)
154{
155 ssize_t length;
156 if (len < sizeof (key->type))
157 return GNUNET_SYSERR;
158 GNUNET_memcpy (&key->type,
159 buffer,
160 sizeof (key->type));
162 if (len < length)
163 return GNUNET_SYSERR;
164 if (length < 0)
165 return GNUNET_SYSERR;
166 GNUNET_memcpy (&key->ecdsa_key,
167 buffer + sizeof (key->type),
168 length - sizeof (key->type));
169 *kb_read = length;
170 return GNUNET_OK;
171}
172
173
174ssize_t
177 key,
178 void *buffer,
179 size_t len)
180{
181 const ssize_t length = GNUNET_CRYPTO_blindable_sk_get_length (key);
182 if (len < length)
183 return -1;
184 if (length < 0)
185 return -2;
186 GNUNET_memcpy (buffer, &(key->type), sizeof (key->type));
187 GNUNET_memcpy (buffer + sizeof (key->type), &(key->ecdsa_key), length
188 - sizeof (key->type));
189 return length;
190}
191
192
193ssize_t
196 *sig)
197{
198 switch (ntohl (sig->type))
199 {
201 return sizeof (sig->type) + sizeof (sig->ecdsa_signature);
202 break;
204 return sizeof (sig->type) + sizeof (sig->eddsa_signature);
205 break;
206 default:
207 GNUNET_break (0);
208 }
209 return -1;
210}
211
212
213ssize_t
215{
216 switch (ntohl (type))
217 {
219 return sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
220 break;
222 return sizeof (struct GNUNET_CRYPTO_EddsaSignature);
223 break;
224 default:
225 GNUNET_break (0);
226 }
227 return -1;
228}
229
230
231ssize_t
234 *sig,
235 const void*buffer,
236 size_t len)
237{
238 ssize_t length;
239 if (len < sizeof (sig->type))
240 return -1;
241 GNUNET_memcpy (&(sig->type), buffer, sizeof (sig->type));
243 if (len < length)
244 return -1;
245 if (length < 0)
246 return -2;
247 GNUNET_memcpy (&(sig->ecdsa_signature), buffer + sizeof (sig->type), length
248 - sizeof (sig->type));
249 return length;
250}
251
252
253ssize_t
256 *sig,
257 void*buffer,
258 size_t len)
259{
260 const ssize_t length = GNUNET_CRYPTO_blinded_key_signature_get_length (sig);
261 if (len < length)
262 return -1;
263 if (length < 0)
264 return -2;
265 GNUNET_memcpy (buffer, &(sig->type), sizeof (sig->type));
266 GNUNET_memcpy (buffer + sizeof (sig->type), &(sig->ecdsa_signature), length
267 - sizeof (sig->type));
268 return length;
269}
270
271
275 const struct
277 unsigned char *sig)
278{
279 switch (ntohl (priv->type))
280 {
282 return GNUNET_CRYPTO_ecdsa_sign_ (&(priv->ecdsa_key), purpose,
283 (struct
285 break;
287 return GNUNET_CRYPTO_eddsa_sign_ (&(priv->eddsa_key), purpose,
288 (struct
290 break;
291 default:
292 GNUNET_break (0);
293 }
294
295 return GNUNET_SYSERR;
296}
297
298
302 const struct
305 )
306{
307 sig->type = priv->type;
308 switch (ntohl (priv->type))
309 {
311 return GNUNET_CRYPTO_ecdsa_sign_ (&(priv->ecdsa_key), purpose,
312 &(sig->ecdsa_signature));
313 break;
315 return GNUNET_CRYPTO_eddsa_sign_ (&(priv->eddsa_key), purpose,
316 &(sig->eddsa_signature));
317 break;
318 default:
319 GNUNET_break (0);
320 }
321
322 return GNUNET_SYSERR;
323}
324
325
328 const struct
330 validate,
331 const struct
333 *sig,
334 const struct
336 pub)
337{
338 /* check type matching of 'sig' and 'pub' */
339 GNUNET_assert (ntohl (pub->type) == ntohl (sig->type));
340 switch (ntohl (pub->type))
341 {
343 return GNUNET_CRYPTO_ecdsa_verify_ (purpose, validate,
344 &(sig->ecdsa_signature),
345 &(pub->ecdsa_key));
346 break;
348 return GNUNET_CRYPTO_eddsa_verify_ (purpose, validate,
349 &(sig->eddsa_signature),
350 &(pub->eddsa_key));
351 break;
352 default:
353 GNUNET_break (0);
354 }
355
356 return GNUNET_SYSERR;
357}
358
359
362 const struct
364 *
365 validate,
366 const unsigned char *sig,
367 const struct
369 *pub)
370{
371 switch (ntohl (pub->type))
372 {
374 return GNUNET_CRYPTO_ecdsa_verify_ (purpose, validate,
375 (struct
377 &(pub->ecdsa_key));
378 break;
380 return GNUNET_CRYPTO_eddsa_verify_ (purpose, validate,
381 (struct
383 &(pub->eddsa_key));
384 break;
385 default:
386 GNUNET_break (0);
387 }
388
389 return GNUNET_SYSERR;
390}
391
392
393char *
402
403
404char *
413
414
417 struct
419 *key)
420{
423 strlen (str),
424 key,
425 sizeof (*key));
426 if (GNUNET_OK != ret)
427 return GNUNET_SYSERR;
428 return check_key_type (ntohl (key->type));
429
430}
431
432
435 struct
437 *key)
438{
441 strlen (str),
442 key,
443 sizeof (*key));
444 if (GNUNET_OK != ret)
445 return GNUNET_SYSERR;
446 return check_key_type (ntohl (key->type));
447}
448
449
453 privkey,
455 *key)
456{
457 key->type = privkey->type;
458 switch (ntohl (privkey->type))
459 {
462 &key->ecdsa_key);
463 break;
466 &key->eddsa_key);
467 break;
468 default:
469 GNUNET_break (0);
470 return GNUNET_SYSERR;
471 }
472 return GNUNET_OK;
473}
static enum GNUNET_GenericReturnValue check_key_type(uint32_t type)
Definition crypto_pkey.c:32
static int ret
Final status code.
Definition gnunet-arm.c:93
struct GNUNET_HashCode key
The key used in the DHT.
static uint32_t type
Type string converted to DNS type value.
static struct GNUNET_CRYPTO_EddsaPublicKey pub
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_verify_(uint32_t purpose, const struct GNUNET_CRYPTO_SignaturePurpose *validate, const struct GNUNET_CRYPTO_EddsaSignature *sig, const struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Verify EdDSA signature.
Definition crypto_ecc.c:708
void GNUNET_CRYPTO_eddsa_key_get_public(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Extract the public key for the given private key.
Definition crypto_ecc.c:201
void GNUNET_CRYPTO_eddsa_key_clear(struct GNUNET_CRYPTO_EddsaPrivateKey *pk)
Clear memory that was used to store a private key.
Definition crypto_ecc.c:447
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_sign_(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, const struct GNUNET_CRYPTO_SignaturePurpose *purpose, struct GNUNET_CRYPTO_EddsaSignature *sig)
EdDSA sign a given block.
Definition crypto_ecc.c:625
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecdsa_sign_(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, const struct GNUNET_CRYPTO_SignaturePurpose *purpose, struct GNUNET_CRYPTO_EcdsaSignature *sig)
ECDSA Sign a given block.
Definition crypto_ecc.c:554
void GNUNET_CRYPTO_ecdsa_key_clear(struct GNUNET_CRYPTO_EcdsaPrivateKey *pk)
Clear memory that was used to store a private key.
Definition crypto_ecc.c:440
void GNUNET_CRYPTO_ecdsa_key_get_public(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Extract the public key for the given private key.
Definition crypto_ecc.c:190
void GNUNET_CRYPTO_private_key_clear(struct GNUNET_CRYPTO_BlindablePrivateKey *key)
Clear memory that was used to store a private key.
Definition crypto_pkey.c:47
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecdsa_verify_(uint32_t purpose, const struct GNUNET_CRYPTO_SignaturePurpose *validate, const struct GNUNET_CRYPTO_EcdsaSignature *sig, const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Verify ECDSA signature.
Definition crypto_ecc.c:649
ssize_t GNUNET_CRYPTO_public_key_get_length(const struct GNUNET_CRYPTO_BlindablePublicKey *key)
Get the compacted length of a GNUNET_CRYPTO_BlindablePublicKey.
Definition crypto_pkey.c:85
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_blindable_key_get_public(const struct GNUNET_CRYPTO_BlindablePrivateKey *privkey, struct GNUNET_CRYPTO_BlindablePublicKey *key)
Retrieves the public key representation of a private key.
#define GNUNET_log(kind,...)
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_blinded_key_sign_(const struct GNUNET_CRYPTO_BlindablePrivateKey *priv, const struct GNUNET_CRYPTO_SignaturePurpose *purpose, struct GNUNET_CRYPTO_BlindableKeySignature *sig)
Sign a given block.
char * GNUNET_CRYPTO_blindable_public_key_to_string(const struct GNUNET_CRYPTO_BlindablePublicKey *key)
Creates a (Base32) string representation of the public key.
ssize_t GNUNET_CRYPTO_blinded_key_signature_get_length_by_type(uint32_t type)
Get the compacted length of a signature by type.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_blindable_public_key_from_string(const char *str, struct GNUNET_CRYPTO_BlindablePublicKey *key)
Parses a (Base32) string representation of the public key.
ssize_t GNUNET_CRYPTO_write_blindable_sk_to_buffer(const struct GNUNET_CRYPTO_BlindablePrivateKey *key, void *buffer, size_t len)
Writes a GNUNET_CRYPTO_BlindablePrivateKey to a compact buffer.
ssize_t GNUNET_CRYPTO_write_blinded_key_signature_to_buffer(const struct GNUNET_CRYPTO_BlindableKeySignature *sig, void *buffer, size_t len)
Writes a GNUNET_CRYPTO_BlindableKeySignature to a compact buffer.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_blinded_key_signature_verify_(uint32_t purpose, const struct GNUNET_CRYPTO_SignaturePurpose *validate, const struct GNUNET_CRYPTO_BlindableKeySignature *sig, const struct GNUNET_CRYPTO_BlindablePublicKey *pub)
Verify a given signature.
char * GNUNET_CRYPTO_blindable_private_key_to_string(const struct GNUNET_CRYPTO_BlindablePrivateKey *key)
Creates a (Base32) string representation of the private key.
ssize_t GNUNET_CRYPTO_blinded_key_signature_get_length(const struct GNUNET_CRYPTO_BlindableKeySignature *sig)
Get the compacted length of a #GNUNET_CRYPTO_Signature.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_read_public_key_from_buffer(const void *buffer, size_t len, struct GNUNET_CRYPTO_BlindablePublicKey *key, size_t *kb_read)
Reads a GNUNET_CRYPTO_BlindablePublicKey from a compact buffer.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_blinded_key_signature_verify_raw_(uint32_t purpose, const struct GNUNET_CRYPTO_SignaturePurpose *validate, const unsigned char *sig, const struct GNUNET_CRYPTO_BlindablePublicKey *pub)
Verify a given signature.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_read_private_key_from_buffer(const void *buffer, size_t len, struct GNUNET_CRYPTO_BlindablePrivateKey *key, size_t *kb_read)
Reads a GNUNET_CRYPTO_BlindablePrivateKey from a compact buffer.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_blindable_private_key_from_string(const char *str, struct GNUNET_CRYPTO_BlindablePrivateKey *key)
Parses a (Base32) string representation of the private key.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_blinded_key_sign_raw_(const struct GNUNET_CRYPTO_BlindablePrivateKey *priv, const struct GNUNET_CRYPTO_SignaturePurpose *purpose, unsigned char *sig)
Sign a given block.
ssize_t GNUNET_CRYPTO_write_blindable_pk_to_buffer(const struct GNUNET_CRYPTO_BlindablePublicKey *key, void *buffer, size_t len)
Writes a GNUNET_CRYPTO_BlindablePublicKey to a compact buffer.
ssize_t GNUNET_CRYPTO_blindable_sk_get_length(const struct GNUNET_CRYPTO_BlindablePrivateKey *key)
Get the compacted length of a GNUNET_CRYPTO_BlindablePrivateKey.
Definition crypto_pkey.c:64
GNUNET_GenericReturnValue
Named constants for return values.
ssize_t GNUNET_CRYPTO_read_blinded_key_signature_from_buffer(struct GNUNET_CRYPTO_BlindableKeySignature *sig, const void *buffer, size_t len)
Reads a GNUNET_CRYPTO_BlindableKeySignature from a compact buffer.
@ GNUNET_PUBLIC_KEY_TYPE_EDDSA
EDDSA identity.
@ GNUNET_PUBLIC_KEY_TYPE_ECDSA
The identity type.
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_ERROR
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition strings.c:807
enum GNUNET_GenericReturnValue GNUNET_STRINGS_string_to_data(const char *enc, size_t enclen, void *out, size_t out_size)
Convert CrockfordBase32 encoding back to data.
Definition strings.c:832
static unsigned int size
Size of the "table".
Definition peer.c:68
An identity signature as per LSD0001.
struct GNUNET_CRYPTO_EddsaSignature eddsa_signature
AN EdDSA signature.
struct GNUNET_CRYPTO_EcdsaSignature ecdsa_signature
An ECDSA signature.
A private key for an identity as per LSD0001.
struct GNUNET_CRYPTO_EcdsaPrivateKey ecdsa_key
An ECDSA identity key.
uint32_t type
Type of public key.
struct GNUNET_CRYPTO_EddsaPrivateKey eddsa_key
AN EdDSA identtiy key.
An identity key as per LSD0001.
an ECC signature using ECDSA
an ECC signature using EdDSA.
header of what an ECC signature signs this must be followed by "size - 8" bytes of the actual signed ...
const char * str
Definition time.c:1252