GNUnet 0.28.0-dev.3-20-gf1136b0b8
 
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#pragma GCC diagnostic push
31#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
32
35{
36 switch (type)
37 {
40 return GNUNET_OK;
41 default:
42 return GNUNET_SYSERR;
43 }
44 return GNUNET_SYSERR;
45}
46
47
48void
50{
51 switch (ntohl (key->type))
52 {
55 break;
58 break;
59 default:
60 GNUNET_break (0);
61 }
62}
63
64
65ssize_t
68{
69 switch (ntohl (key->type))
70 {
72 return sizeof (key->type) + sizeof (key->ecdsa_key);
73 break;
75 return sizeof (key->type) + sizeof (key->eddsa_key);
76 break;
77 default:
79 "Got key type %u\n", ntohl (key->type));
80 GNUNET_break (0);
81 }
82 return -1;
83}
84
85
86ssize_t
89{
90 switch (ntohl (key->type))
91 {
93 return sizeof (key->type) + sizeof (key->ecdsa_key);
95 return sizeof (key->type) + sizeof (key->eddsa_key);
96 default:
97 GNUNET_break (0);
98 }
99 return -1;
100}
101
102
105 const void *buffer,
106 size_t len,
108 size_t *read)
109{
110 ssize_t length;
111
112 if (len < sizeof (key->type))
113 return GNUNET_SYSERR;
114 GNUNET_memcpy (&key->type,
115 buffer,
116 sizeof (key->type));
118 if (len < length)
119 return GNUNET_SYSERR;
120 if (length < 0)
121 return GNUNET_SYSERR;
122 GNUNET_memcpy (&key->ecdsa_key,
123 buffer + sizeof (key->type),
124 length - sizeof (key->type));
125 *read = length;
126 return GNUNET_OK;
127}
128
129
130ssize_t
133 key,
134 void*buffer,
135 size_t len)
136{
137 const ssize_t length = GNUNET_CRYPTO_blindable_pk_get_length (key);
138 if (len < length)
139 return -1;
140 if (length < 0)
141 return -2;
142 GNUNET_memcpy (buffer, &(key->type), sizeof (key->type));
143 GNUNET_memcpy (buffer + sizeof (key->type), &(key->ecdsa_key), length
144 - sizeof (key->type));
145 return length;
146}
147
148
151 size_t len,
152 struct
154 key,
155 size_t *kb_read)
156{
157 ssize_t length;
158 if (len < sizeof (key->type))
159 return GNUNET_SYSERR;
160 GNUNET_memcpy (&key->type,
161 buffer,
162 sizeof (key->type));
164 if (len < length)
165 return GNUNET_SYSERR;
166 if (length < 0)
167 return GNUNET_SYSERR;
168 GNUNET_memcpy (&key->ecdsa_key,
169 buffer + sizeof (key->type),
170 length - sizeof (key->type));
171 *kb_read = length;
172 return GNUNET_OK;
173}
174
175
176ssize_t
179 key,
180 void *buffer,
181 size_t len)
182{
183 const ssize_t length = GNUNET_CRYPTO_blindable_sk_get_length (key);
184 if (len < length)
185 return -1;
186 if (length < 0)
187 return -2;
188 GNUNET_memcpy (buffer, &(key->type), sizeof (key->type));
189 GNUNET_memcpy (buffer + sizeof (key->type), &(key->ecdsa_key), length
190 - sizeof (key->type));
191 return length;
192}
193
194
195ssize_t
198 *sig)
199{
200 switch (ntohl (sig->type))
201 {
203 return sizeof (sig->type) + sizeof (sig->ecdsa_signature);
204 break;
206 return sizeof (sig->type) + sizeof (sig->eddsa_signature);
207 break;
208 default:
209 GNUNET_break (0);
210 }
211 return -1;
212}
213
214
215ssize_t
217{
218 switch (ntohl (type))
219 {
221 return sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
222 break;
224 return sizeof (struct GNUNET_CRYPTO_EddsaSignature);
225 break;
226 default:
227 GNUNET_break (0);
228 }
229 return -1;
230}
231
232
233ssize_t
236 *sig,
237 const void*buffer,
238 size_t len)
239{
240 ssize_t length;
241 if (len < sizeof (sig->type))
242 return -1;
243 GNUNET_memcpy (&(sig->type), buffer, sizeof (sig->type));
245 if (len < length)
246 return -1;
247 if (length < 0)
248 return -2;
249 GNUNET_memcpy (&(sig->ecdsa_signature), buffer + sizeof (sig->type), length
250 - sizeof (sig->type));
251 return length;
252}
253
254
255ssize_t
258 *sig,
259 void*buffer,
260 size_t len)
261{
262 const ssize_t length = GNUNET_CRYPTO_blinded_key_signature_get_length (sig);
263 if (len < length)
264 return -1;
265 if (length < 0)
266 return -2;
267 GNUNET_memcpy (buffer, &(sig->type), sizeof (sig->type));
268 GNUNET_memcpy (buffer + sizeof (sig->type), &(sig->ecdsa_signature), length
269 - sizeof (sig->type));
270 return length;
271}
272
273
277 const struct
279 unsigned char *sig)
280{
281 switch (ntohl (priv->type))
282 {
284 return GNUNET_CRYPTO_ecdsa_sign_ (&(priv->ecdsa_key), purpose,
285 (struct
287 break;
289 return GNUNET_CRYPTO_eddsa_sign_ (&(priv->eddsa_key), purpose,
290 (struct
292 break;
293 default:
294 GNUNET_break (0);
295 }
296
297 return GNUNET_SYSERR;
298}
299
300
304 const struct
307 )
308{
309 sig->type = priv->type;
310 switch (ntohl (priv->type))
311 {
313 return GNUNET_CRYPTO_ecdsa_sign_ (&(priv->ecdsa_key), purpose,
314 &(sig->ecdsa_signature));
315 break;
317 return GNUNET_CRYPTO_eddsa_sign_ (&(priv->eddsa_key), purpose,
318 &(sig->eddsa_signature));
319 break;
320 default:
321 GNUNET_break (0);
322 }
323
324 return GNUNET_SYSERR;
325}
326
327
330 const struct
332 validate,
333 const struct
335 *sig,
336 const struct
338 pub)
339{
340 /* check type matching of 'sig' and 'pub' */
341 if (ntohl (pub->type) != ntohl (sig->type))
342 {
343 GNUNET_break_op (0);
344 return GNUNET_SYSERR;
345 }
346 switch (ntohl (pub->type))
347 {
349 return GNUNET_CRYPTO_ecdsa_verify_ (purpose, validate,
350 &(sig->ecdsa_signature),
351 &(pub->ecdsa_key));
352 break;
354 return GNUNET_CRYPTO_eddsa_verify_ (purpose, validate,
355 &(sig->eddsa_signature),
356 &(pub->eddsa_key));
357 break;
358 default:
359 GNUNET_break (0);
360 }
361
362 return GNUNET_SYSERR;
363}
364
365
368 const struct
370 *
371 validate,
372 const unsigned char *sig,
373 const struct
375 *pub)
376{
377 switch (ntohl (pub->type))
378 {
380 return GNUNET_CRYPTO_ecdsa_verify_ (purpose, validate,
381 (struct
383 &(pub->ecdsa_key));
384 break;
386 return GNUNET_CRYPTO_eddsa_verify_ (purpose, validate,
387 (struct
389 &(pub->eddsa_key));
390 break;
391 default:
392 GNUNET_break (0);
393 }
394
395 return GNUNET_SYSERR;
396}
397
398
399char *
408
409
410char *
419
420
423 struct
425 *key)
426{
429 strlen (str),
430 key,
431 sizeof (*key));
432 if (GNUNET_OK != ret)
433 return GNUNET_SYSERR;
434 return check_key_type (ntohl (key->type));
435
436}
437
438
441 struct
443 *key)
444{
447 strlen (str),
448 key,
449 sizeof (*key));
450 if (GNUNET_OK != ret)
451 return GNUNET_SYSERR;
452 return check_key_type (ntohl (key->type));
453}
454
455
459 privkey,
461 *key)
462{
463 key->type = privkey->type;
464 switch (ntohl (privkey->type))
465 {
468 &key->ecdsa_key);
469 break;
472 &key->eddsa_key);
473 break;
474 default:
475 GNUNET_break (0);
476 return GNUNET_SYSERR;
477 }
478 return GNUNET_OK;
479}
480
481
482#pragma GCC diagnostic pop
static enum GNUNET_GenericReturnValue check_key_type(uint32_t type)
Definition crypto_pkey.c:34
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:728
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:645
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:574
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)
Derive 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:49
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:669
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.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_read_blindable_pk_from_buffer(const void *buffer, size_t len, struct GNUNET_CRYPTO_BlindablePublicKey *key, size_t *read)
Reads a GNUNET_CRYPTO_BlindablePublicKey from a compact buffer.
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_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:66
GNUNET_GenericReturnValue
Named constants for return values.
ssize_t GNUNET_CRYPTO_blindable_pk_get_length(const struct GNUNET_CRYPTO_BlindablePublicKey *key)
Get the compacted length of a GNUNET_CRYPTO_BlindablePublicKey.
Definition crypto_pkey.c:87
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_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
#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:818
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:843
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