GNUnet 0.21.2
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
101ssize_t
103{
104 switch (kt)
105 {
107 return sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
108 break;
110 return sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
111 break;
112 default:
113 GNUNET_break (0);
114 }
115 return -1;
116}
117
118
121 size_t len,
123 key,
124 size_t *kb_read)
125{
126 if (len < sizeof (key->type))
127 return GNUNET_SYSERR;
128 GNUNET_memcpy (&key->type,
129 buffer,
130 sizeof (key->type));
131 ssize_t length = GNUNET_CRYPTO_public_key_get_length (key);
132 if (len < length)
133 return GNUNET_SYSERR;
134 if (length < 0)
135 return GNUNET_SYSERR;
136 GNUNET_memcpy (&key->ecdsa_key,
137 buffer + sizeof (key->type),
138 length - sizeof (key->type));
139 *kb_read = length;
140 return GNUNET_OK;
141}
142
143
144ssize_t
147 void*buffer,
148 size_t len)
149{
150 const ssize_t length = GNUNET_CRYPTO_public_key_get_length (key);
151 if (len < length)
152 return -1;
153 if (length < 0)
154 return -2;
155 GNUNET_memcpy (buffer, &(key->type), sizeof (key->type));
156 GNUNET_memcpy (buffer + sizeof (key->type), &(key->ecdsa_key), length
157 - sizeof (key->type));
158 return length;
159}
160
161
164 size_t len,
165 struct
167 size_t *kb_read)
168{
169 if (len < sizeof (key->type))
170 return GNUNET_SYSERR;
171 GNUNET_memcpy (&key->type,
172 buffer,
173 sizeof (key->type));
174 ssize_t length = GNUNET_CRYPTO_private_key_get_length (key);
175 if (len < length)
176 return GNUNET_SYSERR;
177 if (length < 0)
178 return GNUNET_SYSERR;
179 GNUNET_memcpy (&key->ecdsa_key,
180 buffer + sizeof (key->type),
181 length - sizeof (key->type));
182 *kb_read = length;
183 return GNUNET_OK;
184}
185
186
187ssize_t
190 void *buffer,
191 size_t len)
192{
193 const ssize_t length = GNUNET_CRYPTO_private_key_get_length (key);
194 if (len < length)
195 return -1;
196 if (length < 0)
197 return -2;
198 GNUNET_memcpy (buffer, &(key->type), sizeof (key->type));
199 GNUNET_memcpy (buffer + sizeof (key->type), &(key->ecdsa_key), length
200 - sizeof (key->type));
201 return length;
202}
203
204
205ssize_t
208{
209 switch (ntohl (sig->type))
210 {
212 return sizeof (sig->type) + sizeof (sig->ecdsa_signature);
213 break;
215 return sizeof (sig->type) + sizeof (sig->eddsa_signature);
216 break;
217 default:
218 GNUNET_break (0);
219 }
220 return -1;
221}
222
223
224ssize_t
226{
227 switch (ntohl (type))
228 {
230 return sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
231 break;
233 return sizeof (struct GNUNET_CRYPTO_EddsaSignature);
234 break;
235 default:
236 GNUNET_break (0);
237 }
238 return -1;
239}
240
241
242ssize_t
245 const void*buffer,
246 size_t len)
247{
248 if (len < sizeof (sig->type))
249 return -1;
250 GNUNET_memcpy (&(sig->type), buffer, sizeof (sig->type));
251 const ssize_t length = GNUNET_CRYPTO_signature_get_length (sig);
252 if (len < length)
253 return -1;
254 if (length < 0)
255 return -2;
256 GNUNET_memcpy (&(sig->ecdsa_signature), buffer + sizeof (sig->type), length
257 - sizeof (sig->type));
258 return length;
259}
260
261
262ssize_t
265 void*buffer,
266 size_t len)
267{
268 const ssize_t length = GNUNET_CRYPTO_signature_get_length (sig);
269 if (len < length)
270 return -1;
271 if (length < 0)
272 return -2;
273 GNUNET_memcpy (buffer, &(sig->type), sizeof (sig->type));
274 GNUNET_memcpy (buffer + sizeof (sig->type), &(sig->ecdsa_signature), length
275 - sizeof (sig->type));
276 return length;
277}
278
279
283 const struct
285 unsigned char *sig)
286{
287 switch (ntohl (priv->type))
288 {
290 return GNUNET_CRYPTO_ecdsa_sign_ (&(priv->ecdsa_key), purpose,
291 (struct
293 break;
295 return GNUNET_CRYPTO_eddsa_sign_ (&(priv->eddsa_key), purpose,
296 (struct
298 break;
299 default:
300 GNUNET_break (0);
301 }
302
303 return GNUNET_SYSERR;
304}
305
306
310 const struct
312 struct GNUNET_CRYPTO_Signature *sig)
313{
314 sig->type = priv->type;
315 switch (ntohl (priv->type))
316 {
318 return GNUNET_CRYPTO_ecdsa_sign_ (&(priv->ecdsa_key), purpose,
319 &(sig->ecdsa_signature));
320 break;
322 return GNUNET_CRYPTO_eddsa_sign_ (&(priv->eddsa_key), purpose,
323 &(sig->eddsa_signature));
324 break;
325 default:
326 GNUNET_break (0);
327 }
328
329 return GNUNET_SYSERR;
330}
331
332
335 const struct
337 const struct GNUNET_CRYPTO_Signature *sig,
338 const struct GNUNET_CRYPTO_PublicKey *pub)
339{
340 /* check type matching of 'sig' and 'pub' */
341 GNUNET_assert (ntohl (pub->type) == ntohl (sig->type));
342 switch (ntohl (pub->type))
343 {
345 return GNUNET_CRYPTO_ecdsa_verify_ (purpose, validate,
346 &(sig->ecdsa_signature),
347 &(pub->ecdsa_key));
348 break;
350 return GNUNET_CRYPTO_eddsa_verify_ (purpose, validate,
351 &(sig->eddsa_signature),
352 &(pub->eddsa_key));
353 break;
354 default:
355 GNUNET_break (0);
356 }
357
358 return GNUNET_SYSERR;
359}
360
361
364 const struct
366 validate,
367 const unsigned char *sig,
368 const struct
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 *
396{
399 size);
400}
401
402
403char *
406{
409 size);
410}
411
412
416{
419 strlen (str),
420 key,
421 sizeof (*key));
422 if (GNUNET_OK != ret)
423 return GNUNET_SYSERR;
424 return check_key_type (ntohl (key->type));
425
426}
427
428
432{
435 strlen (str),
436 key,
437 sizeof (*key));
438 if (GNUNET_OK != ret)
439 return GNUNET_SYSERR;
440 return check_key_type (ntohl (key->type));
441}
442
443
448{
449 key->type = privkey->type;
450 switch (ntohl (privkey->type))
451 {
454 &key->ecdsa_key);
455 break;
458 &key->eddsa_key);
459 break;
460 default:
461 GNUNET_break (0);
462 return GNUNET_SYSERR;
463 }
464 return GNUNET_OK;
465}
ssize_t GNUNET_CRYPTO_private_key_length_by_type(enum GNUNET_CRYPTO_KeyType kt)
Definition: crypto_pkey.c:102
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:94
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
Definition: gnunet-scrypt.c:47
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_ecdsa_sign_(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, struct GNUNET_CRYPTO_EcdsaSignature *sig)
ECDSA Sign a given block.
Definition: crypto_ecc.c:554
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_sign_(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, struct GNUNET_CRYPTO_EddsaSignature *sig)
EdDSA sign a given block.
Definition: crypto_ecc.c:625
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
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecdsa_verify_(uint32_t purpose, const struct GNUNET_CRYPTO_EccSignaturePurpose *validate, const struct GNUNET_CRYPTO_EcdsaSignature *sig, const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Verify ECDSA signature.
Definition: crypto_ecc.c:649
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
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_verify_(uint32_t purpose, const struct GNUNET_CRYPTO_EccSignaturePurpose *validate, const struct GNUNET_CRYPTO_EddsaSignature *sig, const struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Verify EdDSA signature.
Definition: crypto_ecc.c:708
void GNUNET_CRYPTO_private_key_clear(struct GNUNET_CRYPTO_PrivateKey *key)
Clear memory that was used to store a private key.
Definition: crypto_pkey.c:47
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_sign_raw_(const struct GNUNET_CRYPTO_PrivateKey *priv, const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, unsigned char *sig)
Sign a given block.
Definition: crypto_pkey.c:281
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_signature_verify_raw_(uint32_t purpose, const struct GNUNET_CRYPTO_EccSignaturePurpose *validate, const unsigned char *sig, const struct GNUNET_CRYPTO_PublicKey *pub)
Verify a given signature.
Definition: crypto_pkey.c:363
ssize_t GNUNET_CRYPTO_read_signature_from_buffer(struct GNUNET_CRYPTO_Signature *sig, const void *buffer, size_t len)
Reads a GNUNET_CRYPTO_Signature from a compact buffer.
Definition: crypto_pkey.c:243
ssize_t GNUNET_CRYPTO_public_key_get_length(const struct GNUNET_CRYPTO_PublicKey *key)
Get the compacted length of a GNUNET_CRYPTO_PublicKey.
Definition: crypto_pkey.c:85
#define GNUNET_log(kind,...)
GNUNET_CRYPTO_KeyType
Key type for the generic public key union.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_signature_verify_(uint32_t purpose, const struct GNUNET_CRYPTO_EccSignaturePurpose *validate, const struct GNUNET_CRYPTO_Signature *sig, const struct GNUNET_CRYPTO_PublicKey *pub)
Verify a given signature.
Definition: crypto_pkey.c:334
char * GNUNET_CRYPTO_public_key_to_string(const struct GNUNET_CRYPTO_PublicKey *key)
Creates a (Base32) string representation of the public key.
Definition: crypto_pkey.c:394
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_private_key_from_string(const char *str, struct GNUNET_CRYPTO_PrivateKey *key)
Parses a (Base32) string representation of the private key.
Definition: crypto_pkey.c:430
ssize_t GNUNET_CRYPTO_write_public_key_to_buffer(const struct GNUNET_CRYPTO_PublicKey *key, void *buffer, size_t len)
Writes a GNUNET_CRYPTO_PublicKey to a compact buffer.
Definition: crypto_pkey.c:145
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_key_get_public(const struct GNUNET_CRYPTO_PrivateKey *privkey, struct GNUNET_CRYPTO_PublicKey *key)
Retrieves the public key representation of a private key.
Definition: crypto_pkey.c:445
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_sign_(const struct GNUNET_CRYPTO_PrivateKey *priv, const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, struct GNUNET_CRYPTO_Signature *sig)
Sign a given block.
Definition: crypto_pkey.c:308
char * GNUNET_CRYPTO_private_key_to_string(const struct GNUNET_CRYPTO_PrivateKey *key)
Creates a (Base32) string representation of the private key.
Definition: crypto_pkey.c:404
ssize_t GNUNET_CRYPTO_signature_get_raw_length_by_type(uint32_t type)
Get the compacted length of a signature by type.
Definition: crypto_pkey.c:225
ssize_t GNUNET_CRYPTO_write_signature_to_buffer(const struct GNUNET_CRYPTO_Signature *sig, void *buffer, size_t len)
Writes a GNUNET_CRYPTO_Signature to a compact buffer.
Definition: crypto_pkey.c:263
ssize_t GNUNET_CRYPTO_private_key_get_length(const struct GNUNET_CRYPTO_PrivateKey *key)
Get the compacted length of a GNUNET_CRYPTO_PrivateKey.
Definition: crypto_pkey.c:64
ssize_t GNUNET_CRYPTO_write_private_key_to_buffer(const struct GNUNET_CRYPTO_PrivateKey *key, void *buffer, size_t len)
Writes a GNUNET_CRYPTO_PrivateKey to a compact buffer.
Definition: crypto_pkey.c:188
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
ssize_t GNUNET_CRYPTO_signature_get_length(const struct GNUNET_CRYPTO_Signature *sig)
Get the compacted length of a GNUNET_CRYPTO_Signature.
Definition: crypto_pkey.c:206
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_read_public_key_from_buffer(const void *buffer, size_t len, struct GNUNET_CRYPTO_PublicKey *key, size_t *kb_read)
Reads a GNUNET_CRYPTO_PublicKey from a compact buffer.
Definition: crypto_pkey.c:120
GNUNET_GenericReturnValue
Named constants for return values.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_read_private_key_from_buffer(const void *buffer, size_t len, struct GNUNET_CRYPTO_PrivateKey *key, size_t *kb_read)
Reads a GNUNET_CRYPTO_PrivateKey from a compact buffer.
Definition: crypto_pkey.c:163
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_public_key_from_string(const char *str, struct GNUNET_CRYPTO_PublicKey *key)
Parses a (Base32) string representation of the public key.
Definition: crypto_pkey.c:414
@ 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:764
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:789
static unsigned int size
Size of the "table".
Definition: peer.c:68
header of what an ECC signature signs this must be followed by "size - 8" bytes of the actual signed ...
Private ECC key encoded for transmission.
an ECC signature using ECDSA
an ECC signature using EdDSA.
A private key for an identity as per LSD0001.
uint32_t type
Type of public key.
struct GNUNET_CRYPTO_EddsaPrivateKey eddsa_key
AN EdDSA identtiy key.
struct GNUNET_CRYPTO_EcdsaPrivateKey ecdsa_key
An ECDSA identity key.
An identity key as per LSD0001.
An identity signature as per LSD0001.
struct GNUNET_CRYPTO_EddsaSignature eddsa_signature
AN EdDSA signature.
struct GNUNET_CRYPTO_EcdsaSignature ecdsa_signature
An ECDSA signature.
uint32_t type
Type of signature.