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

helper function for easy EdDSA key setup More...

#include "platform.h"
#include <gcrypt.h>
#include "gnunet_util_lib.h"
Include dependency graph for crypto_ecc_setup.c:

Go to the source code of this file.

Macros

#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 read_from_file (const char *filename, void *buf, size_t buf_size)
 Read file to buf. More...
 
static int atomic_write_to_file (const char *filename, const void *buf, size_t buf_size)
 Write contents of buf atomically to filename. More...
 
int GNUNET_CRYPTO_eddsa_key_from_file (const char *filename, int do_create, struct GNUNET_CRYPTO_EddsaPrivateKey *pkey)
 Create a new private key by reading it from a file. More...
 
int GNUNET_CRYPTO_ecdsa_key_from_file (const char *filename, int do_create, struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey)
 Create a new private key by reading it from a file. More...
 
struct GNUNET_CRYPTO_EddsaPrivateKeyGNUNET_CRYPTO_eddsa_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
 Create a new private key by reading our peer's key from the file specified in the configuration. More...
 
int GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_PeerIdentity *dst)
 Retrieve the identity of the host's peer. More...
 
void GNUNET_CRYPTO_eddsa_setup_key (const char *cfg_name)
 Setup a key file for a peer given the name of the configuration file (!). More...
 

Detailed Description

helper function for easy EdDSA key setup

Author
Christian Grothoff

Definition in file crypto_ecc_setup.c.

Macro Definition Documentation

◆ LOG

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

Definition at line 30 of file crypto_ecc_setup.c.

◆ LOG_STRERROR

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

Definition at line 32 of file crypto_ecc_setup.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 35 of file crypto_ecc_setup.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:184

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 43 of file crypto_ecc_setup.c.

Function Documentation

◆ read_from_file()

static int read_from_file ( const char *  filename,
void *  buf,
size_t  buf_size 
)
static

Read file to buf.

Fails if the file does not exist or does not have precisely buf_size bytes.

Parameters
filenamefile to read
[out]bufwhere to write the file contents
buf_sizenumber of bytes in buf
Returns
GNUNET_OK on success

Definition at line 65 of file crypto_ecc_setup.c.

References GNUNET_assert, GNUNET_ERROR_TYPE_WARNING, GNUNET_log, GNUNET_log_strerror_file, GNUNET_OK, and GNUNET_SYSERR.

Referenced by GNUNET_CRYPTO_ecdsa_key_from_file(), and GNUNET_CRYPTO_eddsa_key_from_file().

68 {
69  int fd;
70  struct stat sb;
71 
72  fd = open (filename,
73  O_RDONLY);
74  if (-1 == fd)
75  {
76  memset (buf,
77  0,
78  buf_size);
79  return GNUNET_SYSERR;
80  }
81  if (0 != fstat (fd,
82  &sb))
83  {
85  "stat",
86  filename);
87  GNUNET_assert (0 == close (fd));
88  memset (buf,
89  0,
90  buf_size);
91  return GNUNET_SYSERR;
92  }
93  if (sb.st_size != buf_size)
94  {
96  "File `%s' has wrong size (%llu), expected %llu bytes\n",
97  filename,
98  (unsigned long long) sb.st_size,
99  (unsigned long long) buf_size);
100  GNUNET_assert (0 == close (fd));
101  memset (buf,
102  0,
103  buf_size);
104  return GNUNET_SYSERR;
105  }
106  if (buf_size !=
107  read (fd,
108  buf,
109  buf_size))
110  {
112  "read",
113  filename);
114  GNUNET_assert (0 == close (fd));
115  memset (buf,
116  0,
117  buf_size);
118  return GNUNET_SYSERR;
119  }
120  GNUNET_assert (0 == close (fd));
121  return GNUNET_OK;
122 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
static char buf[2048]
static char * filename
#define GNUNET_log(kind,...)
Here is the caller graph for this function:

◆ atomic_write_to_file()

static int atomic_write_to_file ( const char *  filename,
const void *  buf,
size_t  buf_size 
)
static

Write contents of buf atomically to filename.

Fail if filename already exists or if not exactly buf with buf_size bytes could be written to filename.

Parameters
filenamewhere to write
bufbuffer to write
buf_sizenumber of bytes in buf to write
Returns
GNUNET_OK on success, GNUNET_NO if a file existed under filename GNUNET_SYSERR on failure

Definition at line 139 of file crypto_ecc_setup.c.

References GNUNET_asprintf(), GNUNET_assert, GNUNET_DISK_directory_create_for_file(), GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log_strerror_file, GNUNET_NO, GNUNET_OK, GNUNET_strdup, and GNUNET_SYSERR.

Referenced by GNUNET_CRYPTO_ecdsa_key_from_file(), and GNUNET_CRYPTO_eddsa_key_from_file().

142 {
143  char *tmpl;
144  int fd;
145 
146  if (GNUNET_OK !=
148  {
150  "mkstemp",
151  filename);
152  return GNUNET_SYSERR;
153  }
154  {
155  char *dname;
156 
157  dname = GNUNET_strdup (filename);
158  GNUNET_asprintf (&tmpl,
159  "%s/XXXXXX",
160  dirname (dname));
161  GNUNET_free (dname);
162  }
163  fd = mkstemp (tmpl);
164  if (-1 == fd)
165  {
167  "mkstemp",
168  tmpl);
169  GNUNET_free (tmpl);
170  return GNUNET_SYSERR;
171  }
172  if (0 != fchmod (fd,
173  S_IRUSR))
174  {
176  "chmod",
177  tmpl);
178  GNUNET_assert (0 == close (fd));
179  if (0 != unlink (tmpl))
181  "unlink",
182  tmpl);
183  GNUNET_free (tmpl);
184  return GNUNET_SYSERR;
185  }
186  if (buf_size !=
187  write (fd,
188  buf,
189  buf_size))
190  {
192  "write",
193  tmpl);
194  GNUNET_assert (0 == close (fd));
195  if (0 != unlink (tmpl))
197  "unlink",
198  tmpl);
199  GNUNET_free (tmpl);
200  return GNUNET_SYSERR;
201  }
202  GNUNET_assert (0 == close (fd));
203 
204  if (0 != link (tmpl,
205  filename))
206  {
207  if (0 != unlink (tmpl))
209  "unlink",
210  tmpl);
211  GNUNET_free (tmpl);
212  return GNUNET_NO;
213  }
214  if (0 != unlink (tmpl))
216  "unlink",
217  tmpl);
218  GNUNET_free (tmpl);
219  return GNUNET_OK;
220 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
int GNUNET_DISK_directory_create_for_file(const char *filename)
Create the directory structure for storing a file.
Definition: disk.c:684
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
static char buf[2048]
static char * filename
#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_setup_key()

void GNUNET_CRYPTO_eddsa_setup_key ( const char *  cfg_name)

Setup a key file for a peer given the name of the configuration file (!).

This function is used so that at a later point code can be certain that reading a key is fast (for example in time-dependent testcases).

Parameters
cfg_namename of the configuration file to use

Definition at line 388 of file crypto_ecc_setup.c.

References cfg, GNUNET_CONFIGURATION_create(), GNUNET_CONFIGURATION_destroy(), GNUNET_CONFIGURATION_load(), GNUNET_CRYPTO_eddsa_key_create_from_configuration(), and GNUNET_free.

389 {
391  struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
392 
394  (void) GNUNET_CONFIGURATION_load (cfg, cfg_name);
396  if (NULL != priv)
397  GNUNET_free (priv);
399 }
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(void)
Create a new configuration object.
int GNUNET_CONFIGURATION_load(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Load configuration.
void GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
Destroy configuration object.
Private ECC key encoded for transmission.
configuration data
Definition: configuration.c:84
struct GNUNET_CRYPTO_EddsaPrivateKey * GNUNET_CRYPTO_eddsa_key_create_from_configuration(const struct GNUNET_CONFIGURATION_Handle *cfg)
Create a new private key by reading our peer&#39;s key from the file specified in the configuration...
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function: