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 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...
 
static gcry_sexp_t decode_private_eddsa_key (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv)
 Convert the given private key from the network format to the S-expression that can be used by libgcrypt. More...
 
static gcry_sexp_t decode_private_ecdhe_key (const struct GNUNET_CRYPTO_EcdhePrivateKey *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_eddsa_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...
 
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...
 
static void reverse_buffer (unsigned char *buffer, size_t length)
 Reverse the sequence of the bytes in buffer. More...
 
static gcry_mpi_t eddsa_d_to_a (gcry_mpi_t d)
 Convert the secret d of an EdDSA key to the value that is actually used in the EdDSA computation. More...
 
static int point_to_hash (gcry_mpi_point_t result, gcry_ctx_t ctx, struct GNUNET_HashCode *key_material)
 Take point from ECDH and convert it to key material. 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

Definition in file crypto_ecc.c.

Macro Definition Documentation

◆ EXTRA_CHECKS

#define EXTRA_CHECKS   0

Definition at line 32 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 40 of file crypto_ecc.c.

Referenced by decode_private_ecdhe_key(), decode_private_ecdsa_key(), decode_private_eddsa_key(), GNUNET_CRYPTO_ecc_ecdh(), GNUNET_CRYPTO_ecdh_eddsa(), GNUNET_CRYPTO_ecdhe_key_create2(), GNUNET_CRYPTO_ecdsa_ecdh(), GNUNET_CRYPTO_ecdsa_key_create(), GNUNET_CRYPTO_ecdsa_private_key_derive(), GNUNET_CRYPTO_ecdsa_public_key_derive(), GNUNET_CRYPTO_ecdsa_verify(), GNUNET_CRYPTO_eddsa_ecdh(), GNUNET_CRYPTO_eddsa_key_create(), and GNUNET_CRYPTO_eddsa_verify().

◆ LOG

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

◆ LOG_STRERROR

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

Definition at line 44 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 47 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:181

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 55 of file crypto_ecc.c.

Referenced by data_to_ecdsa_value(), data_to_eddsa_value(), decode_private_ecdhe_key(), decode_private_ecdsa_key(), decode_private_eddsa_key(), GNUNET_CRYPTO_ecc_ecdh(), GNUNET_CRYPTO_ecdhe_key_create2(), GNUNET_CRYPTO_ecdsa_key_create(), GNUNET_CRYPTO_ecdsa_verify(), GNUNET_CRYPTO_eddsa_key_create(), GNUNET_CRYPTO_eddsa_verify(), and point_to_hash().

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 77 of file crypto_ecc.c.

References list.

Referenced by GNUNET_CRYPTO_ecdhe_key_create2(), GNUNET_CRYPTO_ecdsa_key_create(), GNUNET_CRYPTO_ecdsa_sign(), GNUNET_CRYPTO_eddsa_key_create(), and GNUNET_CRYPTO_eddsa_sign().

81 {
82  gcry_sexp_t list;
83  gcry_sexp_t l2;
84  unsigned int idx;
85 
86  list = gcry_sexp_find_token (sexp, topname, 0);
87  if (! list)
88  return 1;
89  l2 = gcry_sexp_cadr (list);
90  gcry_sexp_release (list);
91  list = l2;
92  if (! list)
93  return 2;
94 
95  idx = 0;
96  for (const char *s = elems; *s; s++, idx++)
97  {
98  l2 = gcry_sexp_find_token (list, s, 1);
99  if (! l2)
100  {
101  for (unsigned int i = 0; i < idx; i++)
102  {
103  gcry_free (array[i]);
104  array[i] = NULL;
105  }
106  gcry_sexp_release (list);
107  return 3; /* required parameter not found */
108  }
109  array[idx] = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG);
110  gcry_sexp_release (l2);
111  if (! array[idx])
112  {
113  for (unsigned int i = 0; i < idx; i++)
114  {
115  gcry_free (array[i]);
116  array[i] = NULL;
117  }
118  gcry_sexp_release (list);
119  return 4; /* required parameter is invalid */
120  }
121  }
122  gcry_sexp_release (list);
123  return 0;
124 }
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 135 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_key_get_public(), and GNUNET_CRYPTO_ecdsa_sign().

136 {
137  gcry_sexp_t result;
138  int rc;
139 
140  rc = gcry_sexp_build (&result,
141  NULL,
142  "(private-key(ecc(curve \"" CURVE "\")"
143  "(d %b)))",
144  (int) sizeof(priv->d),
145  priv->d);
146  if (0 != rc)
147  {
148  LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
149  GNUNET_assert (0);
150  }
151 #if EXTRA_CHECKS
152  if (0 != (rc = gcry_pk_testkey (result)))
153  {
154  LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
155  GNUNET_assert (0);
156  }
157 #endif
158  return result;
159 }
#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:55
#define CURVE
Name of the curve we are using.
Definition: crypto_ecc.c:40
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:

◆ decode_private_eddsa_key()

static gcry_sexp_t decode_private_eddsa_key ( const struct GNUNET_CRYPTO_EddsaPrivateKey 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 170 of file crypto_ecc.c.

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

Referenced by GNUNET_CRYPTO_eddsa_key_get_public(), and GNUNET_CRYPTO_eddsa_sign().

171 {
172  gcry_sexp_t result;
173  int rc;
174 
175  rc = gcry_sexp_build (&result,
176  NULL,
177  "(private-key(ecc(curve \"" CURVE "\")"
178  "(flags eddsa)(d %b)))",
179  (int) sizeof(priv->d),
180  priv->d);
181  if (0 != rc)
182  {
183  LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
184  GNUNET_assert (0);
185  }
186 #if EXTRA_CHECKS
187  if (0 != (rc = gcry_pk_testkey (result)))
188  {
189  LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
190  GNUNET_assert (0);
191  }
192 #endif
193  return result;
194 }
unsigned char d[256/8]
d is a value mod n, where n has at most 256 bits.
#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:55
#define CURVE
Name of the curve we are using.
Definition: crypto_ecc.c:40
Here is the caller graph for this function:

◆ decode_private_ecdhe_key()

static gcry_sexp_t decode_private_ecdhe_key ( const struct GNUNET_CRYPTO_EcdhePrivateKey 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 205 of file crypto_ecc.c.

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

Referenced by GNUNET_CRYPTO_ecdhe_key_get_public().

206 {
207  gcry_sexp_t result;
208  int rc;
209 
210  rc = gcry_sexp_build (&result,
211  NULL,
212  "(private-key(ecc(curve \"" CURVE "\")"
213  "(d %b)))",
214  (int) sizeof(priv->d),
215  priv->d);
216  if (0 != rc)
217  {
218  LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
219  GNUNET_assert (0);
220  }
221 #if EXTRA_CHECKS
222  if (0 != (rc = gcry_pk_testkey (result)))
223  {
224  LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
225  GNUNET_assert (0);
226  }
227 #endif
228  return result;
229 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
unsigned char d[256/8]
d is a value mod n, where n has at most 256 bits.
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:55
#define CURVE
Name of the curve we are using.
Definition: crypto_ecc.c:40
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 332 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().

334 {
335  char *pubkeybuf;
336  size_t keylen = (sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)) * 8;
337  char *end;
338 
339  if (keylen % 5 > 0)
340  keylen += 5 - keylen % 5;
341  keylen /= 5;
342  pubkeybuf = GNUNET_malloc (keylen + 1);
343  end =
344  GNUNET_STRINGS_data_to_string ((unsigned char *) pub,
345  sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
346  pubkeybuf,
347  keylen);
348  if (NULL == end)
349  {
350  GNUNET_free (pubkeybuf);
351  return NULL;
352  }
353  *end = '\0';
354  return pubkeybuf;
355 }
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:856
#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 365 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().

367 {
368  char *pubkeybuf;
369  size_t keylen = (sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)) * 8;
370  char *end;
371 
372  if (keylen % 5 > 0)
373  keylen += 5 - keylen % 5;
374  keylen /= 5;
375  pubkeybuf = GNUNET_malloc (keylen + 1);
376  end =
377  GNUNET_STRINGS_data_to_string ((unsigned char *) pub,
378  sizeof(struct GNUNET_CRYPTO_EddsaPublicKey),
379  pubkeybuf,
380  keylen);
381  if (NULL == end)
382  {
383  GNUNET_free (pubkeybuf);
384  return NULL;
385  }
386  *end = '\0';
387  return pubkeybuf;
388 }
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:856
#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 398 of file crypto_ecc.c.

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

Referenced by run().

400 {
401  char *privkeybuf;
402  size_t keylen = (sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey)) * 8;
403  char *end;
404 
405  if (keylen % 5 > 0)
406  keylen += 5 - keylen % 5;
407  keylen /= 5;
408  privkeybuf = GNUNET_malloc (keylen + 1);
409  end = GNUNET_STRINGS_data_to_string ((unsigned char *) priv,
410  sizeof(
412  privkeybuf,
413  keylen);
414  if (NULL == end)
415  {
416  GNUNET_free (privkeybuf);
417  return NULL;
418  }
419  *end = '\0';
420  return privkeybuf;
421 }
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:856
#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 431 of file crypto_ecc.c.

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

Referenced by create_finished(), and print_ego().

433 {
434  char *privkeybuf;
435  size_t keylen = (sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey)) * 8;
436  char *end;
437 
438  if (keylen % 5 > 0)
439  keylen += 5 - keylen % 5;
440  keylen /= 5;
441  privkeybuf = GNUNET_malloc (keylen + 1);
442  end = GNUNET_STRINGS_data_to_string ((unsigned char *) priv,
443  sizeof(
445  privkeybuf,
446  keylen);
447  if (NULL == end)
448  {
449  GNUNET_free (privkeybuf);
450  return NULL;
451  }
452  *end = '\0';
453  return privkeybuf;
454 }
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:856
#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 466 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().

470 {
471  size_t keylen = (sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)) * 8;
472 
473  if (keylen % 5 > 0)
474  keylen += 5 - keylen % 5;
475  keylen /= 5;
476  if (enclen != keylen)
477  return GNUNET_SYSERR;
478 
479  if (GNUNET_OK !=
481  enclen,
482  pub,
483  sizeof(
485  return GNUNET_SYSERR;
486  return GNUNET_OK;
487 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
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:952
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 499 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().

503 {
504  size_t keylen = (sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)) * 8;
505 
506  if (keylen % 5 > 0)
507  keylen += 5 - keylen % 5;
508  keylen /= 5;
509  if (enclen != keylen)
510  return GNUNET_SYSERR;
511 
512  if (GNUNET_OK !=
514  enclen,
515  pub,
516  sizeof(
518  return GNUNET_SYSERR;
519  return GNUNET_OK;
520 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
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:952
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 532 of file crypto_ecc.c.

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

536 {
537  size_t keylen = (sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey)) * 8;
538 
539  if (keylen % 5 > 0)
540  keylen += 5 - keylen % 5;
541  keylen /= 5;
542  if (enclen != keylen)
543  return GNUNET_SYSERR;
544 
545  if (GNUNET_OK !=
547  enclen,
548  priv,
549  sizeof(
551  return GNUNET_SYSERR;
552 #if CRYPTO_BUG
553  if (GNUNET_OK != check_eddsa_key (priv))
554  {
555  GNUNET_break (0);
556  return GNUNET_OK;
557  }
558 #endif
559  return GNUNET_OK;
560 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#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:76
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:952
Here is the call graph for this function:

◆ data_to_eddsa_value()

static gcry_sexp_t data_to_eddsa_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 839 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_eddsa_sign(), and GNUNET_CRYPTO_eddsa_verify().

840 {
841  gcry_sexp_t data;
842  int rc;
843 
844 /* SEE #5398 */
845 #if 1
846  struct GNUNET_HashCode hc;
847 
848  GNUNET_CRYPTO_hash (purpose, ntohl (purpose->size), &hc);
849  if (0 != (rc = gcry_sexp_build (&data,
850  NULL,
851  "(data(flags eddsa)(hash-algo %s)(value %b))",
852  "sha512",
853  (int) sizeof(hc),
854  &hc)))
855  {
856  LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
857  return NULL;
858  }
859 #else
860  GNUNET_CRYPTO_hash (purpose, ntohl (purpose->size), &hc);
861  if (0 != (rc = gcry_sexp_build (&data,
862  NULL,
863  "(data(flags eddsa)(hash-algo %s)(value %b))",
864  "sha512",
865  ntohl (purpose->size),
866  purpose)))
867  {
868  LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
869  return NULL;
870  }
871 #endif
872  return data;
873 }
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:55
uint32_t data
The data value.
Here is the call graph for this function:
Here is the caller 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 884 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().

885 {
886  gcry_sexp_t data;
887  int rc;
888 
889 /* See #5398 */
890 #if 1
891  struct GNUNET_HashCode hc;
892 
893  GNUNET_CRYPTO_hash (purpose, ntohl (purpose->size), &hc);
894  if (0 != (rc = gcry_sexp_build (&data,
895  NULL,
896  "(data(flags rfc6979)(hash %s %b))",
897  "sha512",
898  (int) sizeof(hc),
899  &hc)))
900  {
901  LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
902  return NULL;
903  }
904 #else
905  if (0 != (rc = gcry_sexp_build (&data,
906  NULL,
907  "(data(flags rfc6979)(hash %s %b))",
908  "sha512",
909  ntohl (purpose->size),
910  purpose)))
911  {
912  LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
913  return NULL;
914  }
915 #endif
916  return data;
917 }
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:55
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 1256 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().

1259 {
1260  gcry_mpi_t h;
1261  struct GNUNET_HashCode hc;
1262  static const char *const salt = "key-derivation";
1263 
1264  GNUNET_CRYPTO_kdf (&hc,
1265  sizeof(hc),
1266  salt,
1267  strlen (salt),
1268  pub,
1269  sizeof(*pub),
1270  label,
1271  strlen (label),
1272  context,
1273  strlen (context),
1274  NULL,
1275  0);
1276  GNUNET_CRYPTO_mpi_scan_unsigned (&h, (unsigned char *) &hc, sizeof(hc));
1277  return h;
1278 }
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:

◆ reverse_buffer()

static void reverse_buffer ( unsigned char *  buffer,
size_t  length 
)
static

Reverse the sequence of the bytes in buffer.

Parameters

Definition at line 1395 of file crypto_ecc.c.

Referenced by eddsa_d_to_a().

1396 {
1397  unsigned char tmp;
1398  size_t i;
1399 
1400  for (i = 0; i < length / 2; i++)
1401  {
1402  tmp = buffer[i];
1403  buffer[i] = buffer[length - 1 - i];
1404  buffer[length - 1 - i] = tmp;
1405  }
1406 }
Here is the caller graph for this function:

◆ eddsa_d_to_a()

static gcry_mpi_t eddsa_d_to_a ( gcry_mpi_t  d)
static

Convert the secret d of an EdDSA key to the value that is actually used in the EdDSA computation.

Parameters
dsecret input
Returns
value used for the calculation in EdDSA

Definition at line 1417 of file crypto_ecc.c.

References GNUNET_assert, GNUNET_CRYPTO_mpi_scan_unsigned(), and reverse_buffer().

Referenced by GNUNET_CRYPTO_eddsa_ecdh().

1418 {
1419  unsigned char rawmpi[32]; /* 256-bit value */
1420  size_t rawmpilen;
1421  unsigned char digest[64]; /* 512-bit hash value */
1422  gcry_buffer_t hvec[2];
1423  unsigned int b;
1424  gcry_mpi_t a;
1425 
1426  b = 256 / 8; /* number of bytes in `d` */
1427 
1428  /* Note that we clear DIGEST so we can use it as input to left pad
1429  the key with zeroes for hashing. */
1430  memset (digest, 0, sizeof digest);
1431  memset (hvec, 0, sizeof hvec);
1432  rawmpilen = sizeof(rawmpi);
1433  GNUNET_assert (
1434  0 == gcry_mpi_print (GCRYMPI_FMT_USG, rawmpi, rawmpilen, &rawmpilen, d));
1435  hvec[0].data = digest;
1436  hvec[0].off = 0;
1437  hvec[0].len = b > rawmpilen ? (b - rawmpilen) : 0;
1438  hvec[1].data = rawmpi;
1439  hvec[1].off = 0;
1440  hvec[1].len = rawmpilen;
1441  GNUNET_assert (
1442  0 == gcry_md_hash_buffers (GCRY_MD_SHA512, 0 /* flags */, digest, hvec, 2));
1443  /* Compute the A value. */
1444  reverse_buffer (digest, 32); /* Only the first half of the hash. */
1445  digest[0] = (digest[0] & 0x7f) | 0x40;
1446  digest[31] &= 0xf8;
1447 
1448  GNUNET_CRYPTO_mpi_scan_unsigned (&a, digest, 32);
1449  return a;
1450 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static void reverse_buffer(unsigned char *buffer, size_t length)
Reverse the sequence of the bytes in buffer.
Definition: crypto_ecc.c:1395
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ point_to_hash()

static int point_to_hash ( gcry_mpi_point_t  result,
gcry_ctx_t  ctx,
struct GNUNET_HashCode key_material 
)
static

Take point from ECDH and convert it to key material.

Parameters
resultpoint from ECDH
ctxECC context
key_material[out]set to derived key material
Returns
GNUNET_OK on success

Definition at line 1462 of file crypto_ecc.c.

References GNUNET_assert, GNUNET_CRYPTO_hash(), GNUNET_ERROR_TYPE_ERROR, GNUNET_OK, GNUNET_SYSERR, and LOG_GCRY.

Referenced by GNUNET_CRYPTO_ecdh_eddsa(), GNUNET_CRYPTO_ecdsa_ecdh(), and GNUNET_CRYPTO_eddsa_ecdh().

1465 {
1466  gcry_mpi_t result_x;
1467  unsigned char xbuf[256 / 8];
1468  size_t rsize;
1469 
1470  /* finally, convert point to string for hashing */
1471  result_x = gcry_mpi_new (256);
1472  if (gcry_mpi_ec_get_affine (result_x, NULL, result, ctx))
1473  {
1474  LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "get_affine failed", 0);
1475  return GNUNET_SYSERR;
1476  }
1477 
1478  rsize = sizeof(xbuf);
1479  GNUNET_assert (! gcry_mpi_get_flag (result_x, GCRYMPI_FLAG_OPAQUE));
1480  /* result_x can be negative here, so we do not use 'GNUNET_CRYPTO_mpi_print_unsigned'
1481  as that does not include the sign bit; x should be a 255-bit
1482  value, so with the sign it should fit snugly into the 256-bit
1483  xbuf */
1484  GNUNET_assert (
1485  0 == gcry_mpi_print (GCRYMPI_FMT_STD, xbuf, rsize, &rsize, result_x));
1486  GNUNET_CRYPTO_hash (xbuf, rsize, key_material);
1487  gcry_mpi_release (result_x);
1488  return GNUNET_OK;
1489 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
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
static int result
Global testing status.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
#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:55
Here is the call graph for this function:
Here is the caller graph for this function: