GNUnet debian-0.24.3-29-g453fda2cf
 
Loading...
Searching...
No Matches
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).
 

Functions

static enum GNUNET_GenericReturnValue read_from_file (const char *filename, void *buf, size_t buf_size)
 Read file to buf.
 
enum GNUNET_GenericReturnValue 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.
 
enum GNUNET_GenericReturnValue 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.
 
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.
 
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_PeerIdentity *dst)
 Retrieve the identity of the host's peer.
 
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_sign_by_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg, const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, struct GNUNET_CRYPTO_EddsaSignature *sig)
 Sign a given block with a specific purpose using the host's peer identity.
 
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_verify_peer_identity (uint32_t purpose, const struct GNUNET_CRYPTO_EccSignaturePurpose *validate, const struct GNUNET_CRYPTO_EddsaSignature *sig, const struct GNUNET_PeerIdentity *identity)
 Verify a given signature with a peer's identity.
 

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

45 { \
46 LOG (level, \
47 _ ("`%s' failed at %s:%d with error: %s\n"), \
48 cmd, \
49 __FILE__, \
50 __LINE__, \
51 gcry_strerror (rc)); \
52 } while (0)
53
54
65read_from_file (const char *filename,
66 void *buf,
67 size_t buf_size)
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",
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}
123
124
144 int do_create,
146{
148
149 if (GNUNET_OK ==
151 pkey,
152 sizeof (*pkey)))
153 {
154 /* file existed, report that we didn't create it... */
155 return (do_create) ? GNUNET_NO : GNUNET_OK;
156 }
157 else if (! do_create)
158 {
159 return GNUNET_SYSERR;
160 }
161
164 pkey,
165 sizeof (*pkey),
167 if ( (GNUNET_OK == ret) ||
168 (GNUNET_SYSERR == ret) )
169 return ret;
170 /* maybe another process succeeded in the meantime, try reading one more time */
171 if (GNUNET_OK ==
173 pkey,
174 sizeof (*pkey)))
175 {
176 /* file existed, report that *we* didn't create it... */
177 return GNUNET_NO;
178 }
179 /* give up */
180 return GNUNET_SYSERR;
181}
182
183
201 int do_create,
203{
204 if (GNUNET_OK ==
206 pkey,
207 sizeof (*pkey)))
208 {
209 /* file existed, report that we didn't create it... */
210 return (do_create) ? GNUNET_NO : GNUNET_OK;
211 }
212 else if (! do_create)
213 {
214 return GNUNET_SYSERR;
215 }
217 if (GNUNET_OK ==
219 pkey,
220 sizeof (*pkey),
222 return GNUNET_OK;
223 /* maybe another process succeeded in the meantime, try reading one more time */
224 if (GNUNET_OK ==
226 pkey,
227 sizeof (*pkey)))
228 {
229 /* file existed, report that *we* didn't create it... */
230 return GNUNET_NO;
231 }
232 /* give up */
233 return GNUNET_SYSERR;
234}
235
236
247 const struct GNUNET_CONFIGURATION_Handle *cfg)
248{
250 char *fn;
251
252 if (GNUNET_OK !=
254 "PEER",
255 "PRIVATE_KEY",
256 &fn))
257 return NULL;
261 priv))
262 {
263 GNUNET_free (fn);
264 GNUNET_free (priv);
265 return NULL;
266 }
267 GNUNET_free (fn);
268 return priv;
269}
270
271
274 struct GNUNET_PeerIdentity *dst)
275{
277
279 {
281 _ ("Could not load peer's private key\n"));
282 return GNUNET_SYSERR;
283 }
285 &dst->public_key);
286 GNUNET_free (priv);
287 return GNUNET_OK;
288}
289
290
294 const struct
297{
300
302 {
304 _ ("Could not load peer's private key\n"));
305 return GNUNET_SYSERR;
306 }
307
308 result = GNUNET_CRYPTO_eddsa_sign_ (priv, purpose, sig);
310 return result;
311}
312
313
315GNUNET_CRYPTO_verify_peer_identity (uint32_t purpose,
316 const struct
318 const struct
320 const struct GNUNET_PeerIdentity *identity)
321{
322 return GNUNET_CRYPTO_eddsa_verify_ (purpose, validate, sig,
323 &identity->public_key);
324}
325
326
350/* end of crypto_ecc_setup.c */
static enum GNUNET_GenericReturnValue read_from_file(const char *filename, void *buf, size_t buf_size)
Read file to buf.
static int ret
Final status code.
Definition gnunet-arm.c:93
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition gnunet-arm.c:108
static char * filename
static char * pkey
Public key of the zone to look in, in ASCII.
static struct GNUNET_IDENTITY_Handle * identity
Which namespace do we publish to? NULL if we do not publish to a namespace.
static int result
Global testing status.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_sign_by_peer_identity(const struct GNUNET_CONFIGURATION_Handle *cfg, const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, struct GNUNET_CRYPTO_EddsaSignature *sig)
Sign a given block with a specific purpose using the host's peer identity.
enum GNUNET_GenericReturnValue 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.
enum GNUNET_GenericReturnValue 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.
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
void GNUNET_CRYPTO_eddsa_key_create(struct GNUNET_CRYPTO_EddsaPrivateKey *pk)
Create a new private key.
Definition crypto_ecc.c:480
void GNUNET_CRYPTO_ecdsa_key_create(struct GNUNET_CRYPTO_EcdsaPrivateKey *pk)
Create a new private key.
Definition crypto_ecc.c:465
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
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
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_get_peer_identity(const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_PeerIdentity *dst)
Retrieve the identity of the host's peer.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_verify_peer_identity(uint32_t purpose, const struct GNUNET_CRYPTO_EccSignaturePurpose *validate, const struct GNUNET_CRYPTO_EddsaSignature *sig, const struct GNUNET_PeerIdentity *identity)
Verify a given signature with a peer's identity.
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's key from the file specified in the configuration.
enum GNUNET_GenericReturnValue GNUNET_DISK_fn_write(const char *fn, const void *buf, size_t buf_size, enum GNUNET_DISK_AccessPermissions mode)
Write a buffer to a file atomically.
Definition disk.c:750
@ GNUNET_DISK_PERM_USER_READ
Owner can read.
#define GNUNET_log(kind,...)
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#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 'level' that indicates a failure of the command 'cmd' with the mess...
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
#define _(String)
GNU gettext support macro.
Definition platform.h:179
header of what an ECC signature signs this must be followed by "size - 8" bytes of the actual signed ...
Private ECC key encoded for transmission.
Private ECC key encoded for transmission.
an ECC signature using EdDSA.
The identity of the host (wraps the signing key of the peer).
struct GNUNET_CRYPTO_EddsaPublicKey public_key

◆ LOG_STRERROR_FILE

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

Definition at line 36 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)

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

46 { \
47 LOG (level, \
48 _ ("`%s' failed at %s:%d with error: %s\n"), \
49 cmd, \
50 __FILE__, \
51 __LINE__, \
52 gcry_strerror (rc)); \
53 } while (0)

Function Documentation

◆ read_from_file()

static enum GNUNET_GenericReturnValue 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 66 of file crypto_ecc_setup.c.

69{
70 int fd;
71 struct stat sb;
72
73 fd = open (filename,
74 O_RDONLY);
75 if (-1 == fd)
76 {
77 memset (buf,
78 0,
79 buf_size);
80 return GNUNET_SYSERR;
81 }
82 if (0 != fstat (fd,
83 &sb))
84 {
86 "stat",
87 filename);
88 GNUNET_assert (0 == close (fd));
89 memset (buf,
90 0,
91 buf_size);
92 return GNUNET_SYSERR;
93 }
94 if (sb.st_size != buf_size)
95 {
97 "File `%s' has wrong size (%llu), expected %llu bytes\n",
99 (unsigned long long) sb.st_size,
100 (unsigned long long) buf_size);
101 GNUNET_assert (0 == close (fd));
102 memset (buf,
103 0,
104 buf_size);
105 return GNUNET_SYSERR;
106 }
107 if (buf_size !=
108 read (fd,
109 buf,
110 buf_size))
111 {
113 "read",
114 filename);
115 GNUNET_assert (0 == close (fd));
116 memset (buf,
117 0,
118 buf_size);
119 return GNUNET_SYSERR;
120 }
121 GNUNET_assert (0 == close (fd));
122 return GNUNET_OK;
123}

References filename, 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().

Here is the caller graph for this function: