GNUnet 0.22.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
103 size_t len,
105 key,
106 size_t *kb_read)
107{
108 ssize_t length;
109 if (len < sizeof (key->type))
110 return GNUNET_SYSERR;
111 GNUNET_memcpy (&key->type,
112 buffer,
113 sizeof (key->type));
115 if (len < length)
116 return GNUNET_SYSERR;
117 if (length < 0)
118 return GNUNET_SYSERR;
119 GNUNET_memcpy (&key->ecdsa_key,
120 buffer + sizeof (key->type),
121 length - sizeof (key->type));
122 *kb_read = length;
123 return GNUNET_OK;
124}
125
126
127ssize_t
130 void*buffer,
131 size_t len)
132{
133 const ssize_t length = GNUNET_CRYPTO_public_key_get_length (key);
134 if (len < length)
135 return -1;
136 if (length < 0)
137 return -2;
138 GNUNET_memcpy (buffer, &(key->type), sizeof (key->type));
139 GNUNET_memcpy (buffer + sizeof (key->type), &(key->ecdsa_key), length
140 - sizeof (key->type));
141 return length;
142}
143
144
147 size_t len,
148 struct
150 size_t *kb_read)
151{
152 ssize_t length;
153 if (len < sizeof (key->type))
154 return GNUNET_SYSERR;
155 GNUNET_memcpy (&key->type,
156 buffer,
157 sizeof (key->type));
159 if (len < length)
160 return GNUNET_SYSERR;
161 if (length < 0)
162 return GNUNET_SYSERR;
163 GNUNET_memcpy (&key->ecdsa_key,
164 buffer + sizeof (key->type),
165 length - sizeof (key->type));
166 *kb_read = length;
167 return GNUNET_OK;
168}
169
170
171ssize_t
174 void *buffer,
175 size_t len)
176{
177 const ssize_t length = GNUNET_CRYPTO_private_key_get_length (key);
178 if (len < length)
179 return -1;
180 if (length < 0)
181 return -2;
182 GNUNET_memcpy (buffer, &(key->type), sizeof (key->type));
183 GNUNET_memcpy (buffer + sizeof (key->type), &(key->ecdsa_key), length
184 - sizeof (key->type));
185 return length;
186}
187
188
189ssize_t
192{
193 switch (ntohl (sig->type))
194 {
196 return sizeof (sig->type) + sizeof (sig->ecdsa_signature);
197 break;
199 return sizeof (sig->type) + sizeof (sig->eddsa_signature);
200 break;
201 default:
202 GNUNET_break (0);
203 }
204 return -1;
205}
206
207
208ssize_t
210{
211 switch (ntohl (type))
212 {
214 return sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
215 break;
217 return sizeof (struct GNUNET_CRYPTO_EddsaSignature);
218 break;
219 default:
220 GNUNET_break (0);
221 }
222 return -1;
223}
224
225
226ssize_t
229 const void*buffer,
230 size_t len)
231{
232 ssize_t length;
233 if (len < sizeof (sig->type))
234 return -1;
235 GNUNET_memcpy (&(sig->type), buffer, sizeof (sig->type));
237 if (len < length)
238 return -1;
239 if (length < 0)
240 return -2;
241 GNUNET_memcpy (&(sig->ecdsa_signature), buffer + sizeof (sig->type), length
242 - sizeof (sig->type));
243 return length;
244}
245
246
247ssize_t
250 void*buffer,
251 size_t len)
252{
253 const ssize_t length = GNUNET_CRYPTO_signature_get_length (sig);
254 if (len < length)
255 return -1;
256 if (length < 0)
257 return -2;
258 GNUNET_memcpy (buffer, &(sig->type), sizeof (sig->type));
259 GNUNET_memcpy (buffer + sizeof (sig->type), &(sig->ecdsa_signature), length
260 - sizeof (sig->type));
261 return length;
262}
263
264
268 const struct
270 unsigned char *sig)
271{
272 switch (ntohl (priv->type))
273 {
275 return GNUNET_CRYPTO_ecdsa_sign_ (&(priv->ecdsa_key), purpose,
276 (struct
278 break;
280 return GNUNET_CRYPTO_eddsa_sign_ (&(priv->eddsa_key), purpose,
281 (struct
283 break;
284 default:
285 GNUNET_break (0);
286 }
287
288 return GNUNET_SYSERR;
289}
290
291
295 const struct
297 struct GNUNET_CRYPTO_Signature *sig)
298{
299 sig->type = priv->type;
300 switch (ntohl (priv->type))
301 {
303 return GNUNET_CRYPTO_ecdsa_sign_ (&(priv->ecdsa_key), purpose,
304 &(sig->ecdsa_signature));
305 break;
307 return GNUNET_CRYPTO_eddsa_sign_ (&(priv->eddsa_key), purpose,
308 &(sig->eddsa_signature));
309 break;
310 default:
311 GNUNET_break (0);
312 }
313
314 return GNUNET_SYSERR;
315}
316
317
320 const struct
322 const struct GNUNET_CRYPTO_Signature *sig,
323 const struct GNUNET_CRYPTO_PublicKey *pub)
324{
325 /* check type matching of 'sig' and 'pub' */
326 GNUNET_assert (ntohl (pub->type) == ntohl (sig->type));
327 switch (ntohl (pub->type))
328 {
330 return GNUNET_CRYPTO_ecdsa_verify_ (purpose, validate,
331 &(sig->ecdsa_signature),
332 &(pub->ecdsa_key));
333 break;
335 return GNUNET_CRYPTO_eddsa_verify_ (purpose, validate,
336 &(sig->eddsa_signature),
337 &(pub->eddsa_key));
338 break;
339 default:
340 GNUNET_break (0);
341 }
342
343 return GNUNET_SYSERR;
344}
345
346
349 const struct
351 validate,
352 const unsigned char *sig,
353 const struct
355{
356 switch (ntohl (pub->type))
357 {
359 return GNUNET_CRYPTO_ecdsa_verify_ (purpose, validate,
360 (struct
362 &(pub->ecdsa_key));
363 break;
365 return GNUNET_CRYPTO_eddsa_verify_ (purpose, validate,
366 (struct
368 &(pub->eddsa_key));
369 break;
370 default:
371 GNUNET_break (0);
372 }
373
374 return GNUNET_SYSERR;
375}
376
377
378char *
381{
384 size);
385}
386
387
388char *
391{
394 size);
395}
396
397
401{
404 strlen (str),
405 key,
406 sizeof (*key));
407 if (GNUNET_OK != ret)
408 return GNUNET_SYSERR;
409 return check_key_type (ntohl (key->type));
410
411}
412
413
417{
420 strlen (str),
421 key,
422 sizeof (*key));
423 if (GNUNET_OK != ret)
424 return GNUNET_SYSERR;
425 return check_key_type (ntohl (key->type));
426}
427
428
433{
434 key->type = privkey->type;
435 switch (ntohl (privkey->type))
436 {
439 &key->ecdsa_key);
440 break;
443 &key->eddsa_key);
444 break;
445 default:
446 GNUNET_break (0);
447 return GNUNET_SYSERR;
448 }
449 return GNUNET_OK;
450}
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
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:266
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:348
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:227
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,...)
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:319
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:379
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:415
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:128
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:430
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:293
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:389
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:209
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:248
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:172
#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:190
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:102
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:146
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:399
@ 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:787
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:812
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 ...
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.