GNUnet  0.11.x
Macros | Functions
crypto_ecc.c File Reference

public key cryptography (ECC) with libgcrypt More...

#include "platform.h"
#include <gcrypt.h>
#include "gnunet_crypto_lib.h"
#include "gnunet_strings_lib.h"
#include "benchmark.h"
#include "tweetnacl-gnunet.h"
Include dependency graph for crypto_ecc.c:

Go to the source code of this file.

Macros

#define EXTRA_CHECKS   0
 
#define CURVE   "Ed25519"
 Name of the curve we are using. More...
 
#define LOG(kind, ...)   GNUNET_log_from (kind, "util-crypto-ecc", __VA_ARGS__)
 
#define LOG_STRERROR(kind, syscall)   GNUNET_log_from_strerror (kind, "util-crypto-ecc", syscall)
 
#define LOG_STRERROR_FILE(kind, syscall, filename)   GNUNET_log_from_strerror_file (kind, "util-crypto-ecc", syscall, filename)
 
#define LOG_GCRY(level, cmd, rc)
 Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the message given by gcry_strerror(rc). More...
 

Functions

static int key_from_sexp (gcry_mpi_t *array, gcry_sexp_t sexp, const char *topname, const char *elems)
 Extract values from an S-expression. More...
 
static gcry_sexp_t decode_private_ecdsa_key (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv)
 Convert the given private key from the network format to the S-expression that can be used by libgcrypt. More...
 
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. More...
 
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. More...
 
void GNUNET_CRYPTO_ecdhe_key_get_public (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, struct GNUNET_CRYPTO_EcdhePublicKey *pub)
 Extract the public key for the given private key. More...
 
char * GNUNET_CRYPTO_ecdsa_public_key_to_string (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
 Convert a public key to a string. More...
 
char * GNUNET_CRYPTO_eddsa_public_key_to_string (const struct GNUNET_CRYPTO_EddsaPublicKey *pub)
 Convert a public key to a string. More...
 
char * GNUNET_CRYPTO_eddsa_private_key_to_string (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv)
 Convert a private key to a string. More...
 
char * GNUNET_CRYPTO_ecdsa_private_key_to_string (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv)
 Convert a private key to a string. More...
 
int GNUNET_CRYPTO_ecdsa_public_key_from_string (const char *enc, size_t enclen, struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
 Convert a string representing a public key to a public key. More...
 
int GNUNET_CRYPTO_eddsa_public_key_from_string (const char *enc, size_t enclen, struct GNUNET_CRYPTO_EddsaPublicKey *pub)
 Convert a string representing a public key to a public key. More...
 
int GNUNET_CRYPTO_eddsa_private_key_from_string (const char *enc, size_t enclen, struct GNUNET_CRYPTO_EddsaPrivateKey *priv)
 Convert a string representing a private key to a private key. More...
 
void GNUNET_CRYPTO_ecdhe_key_clear (struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
 Clear memory that was used to store a private key. More...
 
void GNUNET_CRYPTO_ecdsa_key_clear (struct GNUNET_CRYPTO_EcdsaPrivateKey *pk)
 Clear memory that was used to store a private key. More...
 
void GNUNET_CRYPTO_eddsa_key_clear (struct GNUNET_CRYPTO_EddsaPrivateKey *pk)
 Clear memory that was used to store a private key. More...
 
struct GNUNET_CRYPTO_EcdhePrivateKeyGNUNET_CRYPTO_ecdhe_key_create ()
 Create a new private key. More...
 
int GNUNET_CRYPTO_ecdhe_key_create2 (struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
 Create a new private key. More...
 
struct GNUNET_CRYPTO_EcdsaPrivateKeyGNUNET_CRYPTO_ecdsa_key_create ()
 Create a new private key. More...
 
struct GNUNET_CRYPTO_EddsaPrivateKeyGNUNET_CRYPTO_eddsa_key_create ()
 Create a new private key. More...
 
const struct GNUNET_CRYPTO_EcdsaPrivateKeyGNUNET_CRYPTO_ecdsa_key_get_anonymous ()
 Get the shared private key we use for anonymous users. More...
 
static gcry_sexp_t data_to_ecdsa_value (const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose)
 Convert the data specified in the given purpose argument to an S-expression suitable for signature operations. More...
 
int GNUNET_CRYPTO_ecdsa_sign (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, struct GNUNET_CRYPTO_EcdsaSignature *sig)
 Sign a given block. More...
 
int GNUNET_CRYPTO_eddsa_sign (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, struct GNUNET_CRYPTO_EddsaSignature *sig)
 Sign a given block. More...
 
int 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 signature. More...
 
int 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 signature. More...
 
int GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, const struct GNUNET_CRYPTO_EcdhePublicKey *pub, struct GNUNET_HashCode *key_material)
 Derive key material from a public and a private ECDHE key. More...
 
static gcry_mpi_t derive_h (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub, const char *label, const char *context)
 Derive the 'h' value for key derivation, where 'h = H(l,P)'. More...
 
struct GNUNET_CRYPTO_EcdsaPrivateKeyGNUNET_CRYPTO_ecdsa_private_key_derive (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, const char *label, const char *context)
 Derive a private key from a given private key and a label. More...
 
void GNUNET_CRYPTO_ecdsa_public_key_derive (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub, const char *label, const char *context, struct GNUNET_CRYPTO_EcdsaPublicKey *result)
 Derive a public key from a given public key and a label. More...
 
int GNUNET_CRYPTO_eddsa_ecdh (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, const struct GNUNET_CRYPTO_EcdhePublicKey *pub, struct GNUNET_HashCode *key_material)
 Derive key material from a ECDH public key and a private EdDSA key. More...
 
int GNUNET_CRYPTO_ecdsa_ecdh (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, const struct GNUNET_CRYPTO_EcdhePublicKey *pub, struct GNUNET_HashCode *key_material)
 Derive key material from a ECDH public key and a private ECDSA key. More...
 
int GNUNET_CRYPTO_ecdh_eddsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, const struct GNUNET_CRYPTO_EddsaPublicKey *pub, struct GNUNET_HashCode *key_material)
 Derive key material from a EdDSA public key and a private ECDH key. More...
 
int GNUNET_CRYPTO_ecdh_ecdsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, const struct GNUNET_CRYPTO_EcdsaPublicKey *pub, struct GNUNET_HashCode *key_material)
 Derive key material from a EcDSA public key and a private ECDH key. More...
 

Detailed Description

public key cryptography (ECC) with libgcrypt

Author
Christian Grothoff
Florian Dold

Definition in file crypto_ecc.c.

Macro Definition Documentation

◆ EXTRA_CHECKS

#define EXTRA_CHECKS   0

Definition at line 34 of file crypto_ecc.c.

◆ CURVE

#define CURVE   "Ed25519"

Name of the curve we are using.

Note that we have hard-coded structs that use 256 bits, so using a bigger curve will require changes that break stuff badly. The name of the curve given here must be agreed by all peers and be supported by libgcrypt.

Definition at line 43 of file crypto_ecc.c.

Referenced by decode_private_ecdsa_key(), GNUNET_CRYPTO_ecdsa_key_create(), GNUNET_CRYPTO_ecdsa_private_key_derive(), GNUNET_CRYPTO_ecdsa_public_key_derive(), and GNUNET_CRYPTO_ecdsa_verify().

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "util-crypto-ecc", __VA_ARGS__)

Definition at line 45 of file crypto_ecc.c.

Referenced by GNUNET_CRYPTO_ecdsa_sign(), and GNUNET_CRYPTO_ecdsa_verify().

◆ LOG_STRERROR

#define LOG_STRERROR (   kind,
  syscall 
)    GNUNET_log_from_strerror (kind, "util-crypto-ecc", syscall)

Definition at line 47 of file crypto_ecc.c.

◆ LOG_STRERROR_FILE

#define LOG_STRERROR_FILE (   kind,
  syscall,
  filename 
)    GNUNET_log_from_strerror_file (kind, "util-crypto-ecc", syscall, filename)

Definition at line 50 of file crypto_ecc.c.

◆ LOG_GCRY

#define LOG_GCRY (   level,
  cmd,
  rc 
)
Value:
do \
{ \
LOG (level, \
_ ("`%s' failed at %s:%d with error: %s\n"), \
cmd, \
__FILE__, \
__LINE__, \
gcry_strerror (rc)); \
} while (0)
#define _(String)
GNU gettext support macro.
Definition: platform.h:180

Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the message given by gcry_strerror(rc).

Definition at line 58 of file crypto_ecc.c.

Referenced by data_to_ecdsa_value(), decode_private_ecdsa_key(), GNUNET_CRYPTO_ecdsa_key_create(), and GNUNET_CRYPTO_ecdsa_verify().

Function Documentation

◆ key_from_sexp()

static int key_from_sexp ( gcry_mpi_t *  array,
gcry_sexp_t  sexp,
const char *  topname,
const char *  elems 
)
static

Extract values from an S-expression.

Parameters
arraywhere to store the result(s)
sexpS-expression to parse
topnametop-level name in the S-expression that is of interest
elemsnames of the elements to extract
Returns
0 on success

Definition at line 80 of file crypto_ecc.c.

References list.

Referenced by GNUNET_CRYPTO_ecdsa_key_create(), and GNUNET_CRYPTO_ecdsa_sign().

84 {
85  gcry_sexp_t list;
86  gcry_sexp_t l2;
87  unsigned int idx;
88 
89  list = gcry_sexp_find_token (sexp, topname, 0);
90  if (! list)
91  return 1;
92  l2 = gcry_sexp_cadr (list);
93  gcry_sexp_release (list);
94  list = l2;
95  if (! list)
96  return 2;
97 
98  idx = 0;
99  for (const char *s = elems; *s; s++, idx++)
100  {
101  l2 = gcry_sexp_find_token (list, s, 1);
102  if (! l2)
103  {
104  for (unsigned int i = 0; i < idx; i++)
105  {
106  gcry_free (array[i]);
107  array[i] = NULL;
108  }
109  gcry_sexp_release (list);
110  return 3; /* required parameter not found */
111  }
112  array[idx] = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG);
113  gcry_sexp_release (l2);
114  if (! array[idx])
115  {
116  for (unsigned int i = 0; i < idx; i++)
117  {
118  gcry_free (array[i]);
119  array[i] = NULL;
120  }
121  gcry_sexp_release (list);
122  return 4; /* required parameter is invalid */
123  }
124  }
125  gcry_sexp_release (list);
126  return 0;
127 }
static int list
Set if we should print a list of currently running services.
Definition: gnunet-arm.c:69
Here is the caller graph for this function:

◆ decode_private_ecdsa_key()

static gcry_sexp_t decode_private_ecdsa_key ( const struct GNUNET_CRYPTO_EcdsaPrivateKey priv)
static

Convert the given private key from the network format to the S-expression that can be used by libgcrypt.

Parameters
privprivate key to decode
Returns
NULL on error

Definition at line 138 of file crypto_ecc.c.

References CURVE, GNUNET_CRYPTO_EcdsaPrivateKey::d, GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, LOG_GCRY, and result.

Referenced by GNUNET_CRYPTO_ecdsa_sign().

139 {
140  gcry_sexp_t result;
141  int rc;
142 
143  rc = gcry_sexp_build (&result,
144  NULL,
145  "(private-key(ecc(curve \"" CURVE "\")"
146  "(d %b)))",
147  (int) sizeof(priv->d),
148  priv->d);
149  if (0 != rc)
150  {
151  LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
152  GNUNET_assert (0);
153  }
154 #if EXTRA_CHECKS
155  if (0 != (rc = gcry_pk_testkey (result)))
156  {
157  LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
158  GNUNET_assert (0);
159  }
160 #endif
161  return result;
162 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static int result
Global testing status.
#define LOG_GCRY(level, cmd, rc)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
Definition: crypto_ecc.c:58
#define CURVE
Name of the curve we are using.
Definition: crypto_ecc.c:43
unsigned char d[256/8]
d is a value mod n, where n has at most 256 bits.
Here is the caller graph for this function:

◆ GNUNET_CRYPTO_ecdsa_public_key_to_string()

char* GNUNET_CRYPTO_ecdsa_public_key_to_string ( const struct GNUNET_CRYPTO_EcdsaPublicKey pub)

Convert a public key to a string.

Parameters
pubkey to convert
Returns
string representing pub

Definition at line 223 of file crypto_ecc.c.

References end, GNUNET_free, GNUNET_malloc, and GNUNET_STRINGS_data_to_string().

Referenced by abd_value_to_string(), create_finished(), delegation_chain_fw_resolution_start(), ego_get_for_subsystem(), forward_resolution(), get_ego(), gns_value_to_string(), GNUNET_ABD_delegate_to_string(), GNUNET_GNSRECORD_pkey_to_zkey(), handle_intermediate_result(), handle_verify_result(), id_connect_cb(), init_egos(), list_ego(), print_deleset(), print_ego(), and store_cb().

225 {
226  char *pubkeybuf;
227  size_t keylen = (sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)) * 8;
228  char *end;
229 
230  if (keylen % 5 > 0)
231  keylen += 5 - keylen % 5;
232  keylen /= 5;
233  pubkeybuf = GNUNET_malloc (keylen + 1);
234  end =
235  GNUNET_STRINGS_data_to_string ((unsigned char *) pub,
236  sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
237  pubkeybuf,
238  keylen);
239  if (NULL == end)
240  {
241  GNUNET_free (pubkeybuf);
242  return NULL;
243  }
244  *end = '\0';
245  return pubkeybuf;
246 }
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and ECDS...
#define GNUNET_malloc(size)
Wrapper around malloc.
char * GNUNET_STRINGS_data_to_string(const void *data, size_t size, char *out, size_t out_size)
Convert binary data to ASCII encoding using CrockfordBase32.
Definition: strings.c:859
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CRYPTO_eddsa_public_key_to_string()

char* GNUNET_CRYPTO_eddsa_public_key_to_string ( const struct GNUNET_CRYPTO_EddsaPublicKey pub)

Convert a public key to a string.

Parameters
pubkey to convert
Returns
string representing pub

Definition at line 256 of file crypto_ecc.c.

References end, GNUNET_free, GNUNET_malloc, and GNUNET_STRINGS_data_to_string().

Referenced by conversation_value_to_string(), create_keys(), GCP_2s(), GNUNET_FRIENDS_write(), GNUNET_HELLO_compose_uri(), GNUNET_i2s(), GNUNET_i2s2(), GNUNET_i2s_full(), main(), print_key(), run(), and uri_loc_to_string().

258 {
259  char *pubkeybuf;
260  size_t keylen = (sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)) * 8;
261  char *end;
262 
263  if (keylen % 5 > 0)
264  keylen += 5 - keylen % 5;
265  keylen /= 5;
266  pubkeybuf = GNUNET_malloc (keylen + 1);
267  end =
268  GNUNET_STRINGS_data_to_string ((unsigned char *) pub,
269  sizeof(struct GNUNET_CRYPTO_EddsaPublicKey),
270  pubkeybuf,
271  keylen);
272  if (NULL == end)
273  {
274  GNUNET_free (pubkeybuf);
275  return NULL;
276  }
277  *end = '\0';
278  return pubkeybuf;
279 }
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
#define GNUNET_malloc(size)
Wrapper around malloc.
char * GNUNET_STRINGS_data_to_string(const void *data, size_t size, char *out, size_t out_size)
Convert binary data to ASCII encoding using CrockfordBase32.
Definition: strings.c:859
#define GNUNET_free(ptr)
Wrapper around free.
Public ECC key (always for curve Ed25519) encoded in a format suitable for network transmission and E...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CRYPTO_eddsa_private_key_to_string()

char* GNUNET_CRYPTO_eddsa_private_key_to_string ( const struct GNUNET_CRYPTO_EddsaPrivateKey priv)

Convert a private key to a string.

Parameters
privkey to convert
Returns
string representing pub

Definition at line 289 of file crypto_ecc.c.

References end, GNUNET_free, GNUNET_malloc, and GNUNET_STRINGS_data_to_string().

Referenced by run().

291 {
292  char *privkeybuf;
293  size_t keylen = (sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey)) * 8;
294  char *end;
295 
296  if (keylen % 5 > 0)
297  keylen += 5 - keylen % 5;
298  keylen /= 5;
299  privkeybuf = GNUNET_malloc (keylen + 1);
300  end = GNUNET_STRINGS_data_to_string ((unsigned char *) priv,
301  sizeof(
303  privkeybuf,
304  keylen);
305  if (NULL == end)
306  {
307  GNUNET_free (privkeybuf);
308  return NULL;
309  }
310  *end = '\0';
311  return privkeybuf;
312 }
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
Private ECC key encoded for transmission.
#define GNUNET_malloc(size)
Wrapper around malloc.
char * GNUNET_STRINGS_data_to_string(const void *data, size_t size, char *out, size_t out_size)
Convert binary data to ASCII encoding using CrockfordBase32.
Definition: strings.c:859
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CRYPTO_ecdsa_private_key_to_string()

char* GNUNET_CRYPTO_ecdsa_private_key_to_string ( const struct GNUNET_CRYPTO_EcdsaPrivateKey priv)

Convert a private key to a string.

Parameters
privkey to convert
Returns
string representing priv

Definition at line 322 of file crypto_ecc.c.

References end, GNUNET_free, GNUNET_malloc, and GNUNET_STRINGS_data_to_string().

Referenced by create_finished(), and print_ego().

324 {
325  char *privkeybuf;
326  size_t keylen = (sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey)) * 8;
327  char *end;
328 
329  if (keylen % 5 > 0)
330  keylen += 5 - keylen % 5;
331  keylen /= 5;
332  privkeybuf = GNUNET_malloc (keylen + 1);
333  end = GNUNET_STRINGS_data_to_string ((unsigned char *) priv,
334  sizeof(
336  privkeybuf,
337  keylen);
338  if (NULL == end)
339  {
340  GNUNET_free (privkeybuf);
341  return NULL;
342  }
343  *end = '\0';
344  return privkeybuf;
345 }
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
Private ECC key encoded for transmission.
#define GNUNET_malloc(size)
Wrapper around malloc.
char * GNUNET_STRINGS_data_to_string(const void *data, size_t size, char *out, size_t out_size)
Convert binary data to ASCII encoding using CrockfordBase32.
Definition: strings.c:859
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CRYPTO_ecdsa_public_key_from_string()

int GNUNET_CRYPTO_ecdsa_public_key_from_string ( const char *  enc,
size_t  enclen,
struct GNUNET_CRYPTO_EcdsaPublicKey pub 
)

Convert a string representing a public key to a public key.

Parameters
encencoded public key
enclennumber of bytes in enc (without 0-terminator)
pubwhere to store the public key
Returns
GNUNET_OK on success

Definition at line 357 of file crypto_ecc.c.

References GNUNET_OK, GNUNET_STRINGS_string_to_data(), and GNUNET_SYSERR.

Referenced by abd_string_to_value(), access_handler_callback(), authorize_endpoint(), code_redirect(), create_response(), gns_string_to_value(), GNUNET_ABD_delegate_from_string(), GNUNET_GNS_lookup_with_tld(), GNUNET_GNSRECORD_zkey_to_pkey(), identity_cb(), lookup_it_finished(), run(), run_with_zone_pkey(), sign_cb(), start_process(), and tld_iter().

361 {
362  size_t keylen = (sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)) * 8;
363 
364  if (keylen % 5 > 0)
365  keylen += 5 - keylen % 5;
366  keylen /= 5;
367  if (enclen != keylen)
368  return GNUNET_SYSERR;
369 
370  if (GNUNET_OK !=
372  enclen,
373  pub,
374  sizeof(
376  return GNUNET_SYSERR;
377  return GNUNET_OK;
378 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and ECDS...
static OpusEncoder * enc
OPUS encoder.
int 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:955
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CRYPTO_eddsa_public_key_from_string()

int GNUNET_CRYPTO_eddsa_public_key_from_string ( const char *  enc,
size_t  enclen,
struct GNUNET_CRYPTO_EddsaPublicKey pub 
)

Convert a string representing a public key to a public key.

Parameters
encencoded public key
enclennumber of bytes in enc (without 0-terminator)
pubwhere to store the public key
Returns
GNUNET_OK on success

Definition at line 390 of file crypto_ecc.c.

References GNUNET_OK, GNUNET_STRINGS_string_to_data(), and GNUNET_SYSERR.

Referenced by blacklist_cfg_iter(), conversation_string_to_value(), create_keys(), gns_string_to_value(), GNUNET_FRIENDS_parse(), hosts_directory_scan_callback(), run(), s2i_full(), server_parse_url(), show_peer(), and uri_loc_parse().

394 {
395  size_t keylen = (sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)) * 8;
396 
397  if (keylen % 5 > 0)
398  keylen += 5 - keylen % 5;
399  keylen /= 5;
400  if (enclen != keylen)
401  return GNUNET_SYSERR;
402 
403  if (GNUNET_OK !=
405  enclen,
406  pub,
407  sizeof(
409  return GNUNET_SYSERR;
410  return GNUNET_OK;
411 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
static OpusEncoder * enc
OPUS encoder.
int 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:955
Public ECC key (always for curve Ed25519) encoded in a format suitable for network transmission and E...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CRYPTO_eddsa_private_key_from_string()

int GNUNET_CRYPTO_eddsa_private_key_from_string ( const char *  enc,
size_t  enclen,
struct GNUNET_CRYPTO_EddsaPrivateKey priv 
)

Convert a string representing a private key to a private key.

Parameters
encencoded public key
enclennumber of bytes in enc (without 0-terminator)
privwhere to store the private key
Returns
GNUNET_OK on success

Definition at line 423 of file crypto_ecc.c.

References GNUNET_break, GNUNET_OK, GNUNET_STRINGS_string_to_data(), and GNUNET_SYSERR.

427 {
428  size_t keylen = (sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey)) * 8;
429 
430  if (keylen % 5 > 0)
431  keylen += 5 - keylen % 5;
432  keylen /= 5;
433  if (enclen != keylen)
434  return GNUNET_SYSERR;
435 
436  if (GNUNET_OK !=
438  enclen,
439  priv,
440  sizeof(
442  return GNUNET_SYSERR;
443 #if CRYPTO_BUG
444  if (GNUNET_OK != check_eddsa_key (priv))
445  {
446  GNUNET_break (0);
447  return GNUNET_OK;
448  }
449 #endif
450  return GNUNET_OK;
451 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
Private ECC key encoded for transmission.
static OpusEncoder * enc
OPUS encoder.
int 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:955
Here is the call graph for this function:

◆ data_to_ecdsa_value()

static gcry_sexp_t data_to_ecdsa_value ( const struct GNUNET_CRYPTO_EccSignaturePurpose purpose)
static

Convert the data specified in the given purpose argument to an S-expression suitable for signature operations.

Parameters
purposedata to convert
Returns
converted s-expression

Definition at line 642 of file crypto_ecc.c.

References data, GNUNET_CRYPTO_hash(), GNUNET_ERROR_TYPE_ERROR, LOG_GCRY, and GNUNET_CRYPTO_EccSignaturePurpose::size.

Referenced by GNUNET_CRYPTO_ecdsa_sign(), and GNUNET_CRYPTO_ecdsa_verify().

643 {
644  gcry_sexp_t data;
645  int rc;
646 
647 /* See #5398 */
648 #if 1
649  struct GNUNET_HashCode hc;
650 
651  GNUNET_CRYPTO_hash (purpose, ntohl (purpose->size), &hc);
652  if (0 != (rc = gcry_sexp_build (&data,
653  NULL,
654  "(data(flags rfc6979)(hash %s %b))",
655  "sha512",
656  (int) sizeof(hc),
657  &hc)))
658  {
659  LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
660  return NULL;
661  }
662 #else
663  if (0 != (rc = gcry_sexp_build (&data,
664  NULL,
665  "(data(flags rfc6979)(hash %s %b))",
666  "sha512",
667  ntohl (purpose->size),
668  purpose)))
669  {
670  LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
671  return NULL;
672  }
673 #endif
674  return data;
675 }
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:48
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!)...
A 512-bit hashcode.
#define LOG_GCRY(level, cmd, rc)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
Definition: crypto_ecc.c:58
uint32_t data
The data value.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ derive_h()

static gcry_mpi_t derive_h ( const struct GNUNET_CRYPTO_EcdsaPublicKey pub,
const char *  label,
const char *  context 
)
static

Derive the 'h' value for key derivation, where 'h = H(l,P)'.

Parameters
pubpublic key for deriviation
labellabel for deriviation
contextadditional context to use for HKDF of 'h'; typically the name of the subsystem/application
Returns
h value

Definition at line 897 of file crypto_ecc.c.

References GNUNET_CRYPTO_kdf(), GNUNET_CRYPTO_mpi_scan_unsigned(), and h.

Referenced by GNUNET_CRYPTO_ecdsa_private_key_derive(), and GNUNET_CRYPTO_ecdsa_public_key_derive().

900 {
901  gcry_mpi_t h;
902  struct GNUNET_HashCode hc;
903  static const char *const salt = "key-derivation";
904 
905  GNUNET_CRYPTO_kdf (&hc,
906  sizeof(hc),
907  salt,
908  strlen (salt),
909  pub,
910  sizeof(*pub),
911  label,
912  strlen (label),
913  context,
914  strlen (context),
915  NULL,
916  0);
917  GNUNET_CRYPTO_mpi_scan_unsigned (&h, (unsigned char *) &hc, sizeof(hc));
918  return h;
919 }
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
static pa_context * context
Pulseaudio context.
A 512-bit hashcode.
void GNUNET_CRYPTO_mpi_scan_unsigned(gcry_mpi_t *result, const void *data, size_t size)
Convert data buffer into MPI value.
Definition: crypto_mpi.c:131
int GNUNET_CRYPTO_kdf(void *result, size_t out_len, const void *xts, size_t xts_len, const void *skm, size_t skm_len,...)
Derive key.
Definition: crypto_kdf.c:89
Here is the call graph for this function:
Here is the caller graph for this function: