GNUnet  0.11.x
Data Structures | Macros | Functions
gnsrecord_crypto.c File Reference

API for GNS record-related crypto. More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_constants.h"
#include "gnunet_signatures.h"
#include "gnunet_arm_service.h"
#include "gnunet_gnsrecord_lib.h"
#include "gnunet_dnsparser_lib.h"
#include "gnunet_tun_lib.h"
Include dependency graph for gnsrecord_crypto.c:

Go to the source code of this file.

Data Structures

struct  KeyCacheLine
 Line in cache mapping private keys to public keys. More...


#define LOG(kind, ...)   GNUNET_log_from (kind, "gnsrecord", __VA_ARGS__)
#define CSIZE   64


static void derive_block_aes_key (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, struct GNUNET_CRYPTO_SymmetricSessionKey *skey, const char *label, const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
 Derive session key and iv from label and public key. More...
static struct GNUNET_GNSRECORD_Blockblock_create (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key, const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey, struct GNUNET_TIME_Absolute expire, const char *label, const struct GNUNET_GNSRECORD_Data *rd, unsigned int rd_count)
 Sign name and records. More...
struct GNUNET_GNSRECORD_BlockGNUNET_GNSRECORD_block_create (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key, struct GNUNET_TIME_Absolute expire, const char *label, const struct GNUNET_GNSRECORD_Data *rd, unsigned int rd_count)
 Sign name and records. More...
struct GNUNET_GNSRECORD_BlockGNUNET_GNSRECORD_block_create2 (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key, struct GNUNET_TIME_Absolute expire, const char *label, const struct GNUNET_GNSRECORD_Data *rd, unsigned int rd_count)
 Sign name and records, cache derived public key (also keeps the private key in static memory, so do not use this function if keeping the private key in the process'es RAM is a major issue). More...
int GNUNET_GNSRECORD_block_verify (const struct GNUNET_GNSRECORD_Block *block)
 Check if a signature is valid. More...
int GNUNET_GNSRECORD_block_decrypt (const struct GNUNET_GNSRECORD_Block *block, const struct GNUNET_CRYPTO_EcdsaPublicKey *zone_key, const char *label, GNUNET_GNSRECORD_RecordCallback proc, void *proc_cls)
 Decrypt block. More...
void GNUNET_GNSRECORD_query_from_private_key (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, const char *label, struct GNUNET_HashCode *query)
 Calculate the DHT query for a given label in a given zone. More...
void GNUNET_GNSRECORD_query_from_public_key (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub, const char *label, struct GNUNET_HashCode *query)
 Calculate the DHT query for a given label in a given zone. More...

Detailed Description

API for GNS record-related crypto.

Martin Schanzenbach
Matthias Wachs
Christian Grothoff

Definition in file gnsrecord_crypto.c.

Macro Definition Documentation


#define LOG (   kind,
)    GNUNET_log_from (kind, "gnsrecord", __VA_ARGS__)

Definition at line 38 of file gnsrecord_crypto.c.


#define CSIZE   64

Function Documentation

◆ derive_block_aes_key()

static void derive_block_aes_key ( struct GNUNET_CRYPTO_SymmetricInitializationVector iv,
struct GNUNET_CRYPTO_SymmetricSessionKey skey,
const char *  label,
const struct GNUNET_CRYPTO_EcdsaPublicKey pub 

Derive session key and iv from label and public key.

ivinitialization vector to initialize
skeysession key to initialize
labellabel to use for KDF
pubpublic key to use for KDF

Definition at line 50 of file gnsrecord_crypto.c.

References GNUNET_CRYPTO_kdf().

Referenced by block_create(), and GNUNET_GNSRECORD_block_decrypt().

54 {
55  static const char ctx_key[] = "gns-aes-ctx-key";
56  static const char ctx_iv[] = "gns-aes-ctx-iv";
59  ctx_key, strlen (ctx_key),
60  pub, sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
61  label, strlen (label),
62  NULL, 0);
63  GNUNET_CRYPTO_kdf (iv, sizeof(struct
65  ctx_iv, strlen (ctx_iv),
66  pub, sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
67  label, strlen (label),
68  NULL, 0);
69 }
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and ECDS...
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:

◆ block_create()

static struct GNUNET_GNSRECORD_Block* block_create ( const struct GNUNET_CRYPTO_EcdsaPrivateKey key,
const struct GNUNET_CRYPTO_EcdsaPublicKey pkey,
struct GNUNET_TIME_Absolute  expire,
const char *  label,
const struct GNUNET_GNSRECORD_Data rd,
unsigned int  rd_count 

Sign name and records.

keythe private key
pkeyassociated public key
expireblock expiration
labelthe name for the records
rdrecord data
rd_countnumber of records
NULL on error (block too large)

Definition at line 84 of file gnsrecord_crypto.c.

References GNUNET_TIME_Absolute::abs_value_us, derive_block_aes_key(), GNUNET_GNSRECORD_Block::derived_key, GNUNET_GNSRECORD_Data::expiration_time, GNUNET_GNSRECORD_Block::expiration_time, GNUNET_GNSRECORD_Data::flags, GNUNET_assert, GNUNET_break, GNUNET_CRYPTO_ecdsa_key_get_public(), GNUNET_CRYPTO_ecdsa_private_key_derive(), GNUNET_CRYPTO_ecdsa_sign_(), GNUNET_CRYPTO_symmetric_encrypt(), GNUNET_free, GNUNET_GNSRECORD_MAX_BLOCK_SIZE, GNUNET_GNSRECORD_records_get_size(), GNUNET_GNSRECORD_records_serialize(), GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION, GNUNET_malloc, GNUNET_memcpy, GNUNET_OK, GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN, GNUNET_TIME_absolute_add(), GNUNET_TIME_absolute_get(), GNUNET_TIME_absolute_hton(), payload, GNUNET_CRYPTO_EccSignaturePurpose::purpose, GNUNET_GNSRECORD_Block::purpose, GNUNET_TIME_Relative::rel_value_us, GNUNET_GNSRECORD_Block::signature, and GNUNET_CRYPTO_EccSignaturePurpose::size.

Referenced by GNUNET_GNSRECORD_block_create(), and GNUNET_GNSRECORD_block_create2().

90 {
91  ssize_t payload_len = GNUNET_GNSRECORD_records_get_size (rd_count,
92  rd);
93  struct GNUNET_GNSRECORD_Block *block;
94  struct GNUNET_CRYPTO_EcdsaPrivateKey *dkey;
97  struct GNUNET_GNSRECORD_Data rdc[GNUNET_NZL (rd_count)];
98  uint32_t rd_count_nbo;
99  struct GNUNET_TIME_Absolute now;
101  if (payload_len < 0)
102  {
103  GNUNET_break (0);
104  return NULL;
105  }
106  if (payload_len > GNUNET_GNSRECORD_MAX_BLOCK_SIZE)
107  {
108  GNUNET_break (0);
109  return NULL;
110  }
111  /* convert relative to absolute times */
112  now = GNUNET_TIME_absolute_get ();
113  for (unsigned int i = 0; i < rd_count; i++)
114  {
115  rdc[i] = rd[i];
116  if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
117  {
118  struct GNUNET_TIME_Relative t;
120  /* encrypted blocks must never have relative expiration times, convert! */
122  t.rel_value_us = rdc[i].expiration_time;
123  rdc[i].expiration_time = GNUNET_TIME_absolute_add (now, t).abs_value_us;
124  }
125  }
126  /* serialize */
127  rd_count_nbo = htonl (rd_count);
128  {
129  char payload[sizeof(uint32_t) + payload_len];
132  &rd_count_nbo,
133  sizeof(uint32_t));
134  GNUNET_assert (payload_len ==
136  rdc,
137  payload_len,
138  &payload[sizeof(uint32_t)
139  ]));
140  block = GNUNET_malloc (sizeof(struct GNUNET_GNSRECORD_Block)
141  + sizeof(uint32_t)
142  + payload_len);
143  block->purpose.size = htonl (sizeof(uint32_t)
144  + payload_len
145  + sizeof(struct
147  + sizeof(struct GNUNET_TIME_AbsoluteNBO));
149  block->expiration_time = GNUNET_TIME_absolute_hton (expire);
150  /* encrypt and sign */
152  label,
153  "gns");
155  &block->derived_key);
157  &skey,
158  label,
159  pkey);
160  GNUNET_break (payload_len + sizeof(uint32_t) ==
162  payload_len
163  + sizeof(uint32_t),
164  &skey,
165  &iv,
166  &block[1]));
167  }
168  if (GNUNET_OK !=
170  &block->purpose,
171  &block->signature))
172  {
173  GNUNET_break (0);
174  GNUNET_free (dkey);
175  GNUNET_free (block);
176  return NULL;
177  }
178  GNUNET_free (dkey);
179  return block;
180 }
struct GNUNET_TIME_AbsoluteNBO expiration_time
Expiration time of the block.
GNUNET_NETWORK_STRUCT_END ssize_t GNUNET_GNSRECORD_records_get_size(unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd)
Calculate how many bytes we will need to serialize the given records.
uint32_t purpose
What does this signature vouch for? This must contain a GNUNET_SIGNATURE_PURPOSE_XXX constant (from g...
ssize_t GNUNET_GNSRECORD_records_serialize(unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd, size_t dest_size, char *dest)
Serialize the given records to the given destination buffer.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Maximum size of a value that can be stored in a GNS block.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_add(struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Relative duration)
Add a given relative duration to the given start time.
Definition: time.c:395
Private ECC key encoded for transmission.
static struct GNUNET_SCHEDULER_Task * t
Main task.
Information we have in an encrypted block with record data (i.e.
Time for absolute time used by GNUnet, in microseconds and in network byte order. ...
uint64_t abs_value_us
The actual value.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
header of what an ECC signature signs this must be followed by "size - 8" bytes of the actual signed ...
ssize_t GNUNET_CRYPTO_symmetric_encrypt(const void *block, size_t size, const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey, const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, void *result)
Encrypt a block using a symmetric sessionkey.
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!)...
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
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.
Definition: crypto_ecc.c:176
int GNUNET_CRYPTO_ecdsa_sign_(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, struct GNUNET_CRYPTO_EcdsaSignature *sig)
ECDSA Sign a given block.
Definition: crypto_ecc.c:645
static unsigned long long payload
How much data are we currently storing in the database?
static void derive_block_aes_key(struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, struct GNUNET_CRYPTO_SymmetricSessionKey *skey, const char *label, const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Derive session key and iv from label and public key.
#define GNUNET_NZL(l)
Macro used to avoid using 0 for the length of a variable-size array (Non-Zero-Length).
struct GNUNET_CRYPTO_EcdsaSignature signature
Signature of the block.
Signature of a gnunet naming system record block.
Time for absolute times used by GNUnet, in microseconds.
This expiration time of the record is a relative time (not an absolute time).
struct GNUNET_CRYPTO_EcdsaPrivateKey * GNUNET_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.
Definition: crypto_ecc.c:911
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
Number of bytes signed; also specifies the number of bytes of encrypted data that follow...
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:657
struct GNUNET_CRYPTO_EcdsaPublicKey derived_key
Derived key used for signing; hash of this is the query.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.
Here is the call graph for this function:
Here is the caller graph for this function: