GNUnet 0.28.0-dev.2-5-gff43856e1
 
Loading...
Searching...
No Matches
gnsrecord_crypto.c File Reference

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

#include "platform.h"
#include "gnsrecord_crypto.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...
 
struct  EncryptionContextData
 

Macros

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

Functions

void GNR_derive_block_aes_key (unsigned char *ctr, unsigned char *key, const char *label, uint64_t exp, const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
 Derive session key and iv from label and public key.
 
void GNR_derive_block_xsalsa_key (struct GNUNET_CRYPTO_XSalsa20Nonce *nonce, struct GNUNET_CRYPTO_XSalsa20SecretKey *key, const char *label, uint64_t exp, const struct GNUNET_CRYPTO_EddsaPublicKey *pub)
 Derive session key and iv from label and public key.
 
static enum GNUNET_GenericReturnValue block_sign_ecdsa (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key, const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey, const char *label, struct GNUNET_GNSRECORD_Block *block)
 
static enum GNUNET_GenericReturnValue block_sign_eddsa (const struct GNUNET_CRYPTO_EddsaPrivateKey *key, const struct GNUNET_CRYPTO_EddsaPublicKey *pkey, const char *label, struct GNUNET_GNSRECORD_Block *block)
 
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_block_sign (const struct GNUNET_CRYPTO_BlindablePrivateKey *key, const char *label, struct GNUNET_GNSRECORD_Block *block)
 Sign a block create with GNUNET_GNSRECORD_block_create_unsigned.
 
static enum GNUNET_GenericReturnValue block_create_ecdsa (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key, const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey, struct GNUNET_TIME_Absolute expire, const char *label, const unsigned char *rdata, size_t rdata_len, struct GNUNET_GNSRECORD_Block **block, int sign)
 Sign name and records.
 
static enum GNUNET_GenericReturnValue block_create_eddsa (const struct GNUNET_CRYPTO_EddsaPrivateKey *key, const struct GNUNET_CRYPTO_EddsaPublicKey *pkey, struct GNUNET_TIME_Absolute expire, const char *label, const unsigned char *rdata, size_t rdata_len, struct GNUNET_GNSRECORD_Block **block, int sign)
 Sign name and records (EDDSA version)
 
static enum GNUNET_GenericReturnValue block_create2 (const struct GNUNET_CRYPTO_BlindablePrivateKey *pkey, struct GNUNET_TIME_Absolute expire, const char *label, const unsigned char *rdata, size_t rdata_len, struct GNUNET_GNSRECORD_Block **result, int sign)
 
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_block_verify (const struct GNUNET_GNSRECORD_Block *block)
 Check if a signature is valid.
 
static enum GNUNET_GenericReturnValue block_decrypt_ecdsa (const struct GNUNET_GNSRECORD_Block *block, const struct GNUNET_CRYPTO_EcdsaPublicKey *zone_key, const char *label, GNUNET_GNSRECORD_RecordCallback proc, void *proc_cls)
 
static enum GNUNET_GenericReturnValue block_decrypt_eddsa (const struct GNUNET_GNSRECORD_Block *block, const struct GNUNET_CRYPTO_EddsaPublicKey *zone_key, const char *label, GNUNET_GNSRECORD_RecordCallback proc, void *proc_cls)
 
void GNUNET_GNSRECORD_query_from_private_key (const struct GNUNET_CRYPTO_BlindablePrivateKey *zone, const char *label, struct GNUNET_HashCode *query)
 Calculate the DHT query for a given label in a given zone.
 
void GNUNET_GNSRECORD_query_from_public_key (const struct GNUNET_CRYPTO_BlindablePublicKey *pub, const char *label, struct GNUNET_HashCode *query)
 Calculate the DHT query for a given label in a given zone.
 
static enum GNUNET_GenericReturnValue block_open_ecdsa (void *cls, const char *label, const struct GNUNET_GNSRECORD_Block *block, GNUNET_GNSRECORD_RecordCallback proc, void *proc_cls)
 
static enum GNUNET_GenericReturnValue block_open_eddsa (void *cls, const char *label, const struct GNUNET_GNSRECORD_Block *block, GNUNET_GNSRECORD_RecordCallback proc, void *proc_cls)
 
static enum GNUNET_GenericReturnValue block_seal_not_implemented (void *cls, const char *label, struct GNUNET_TIME_Absolute expire, unsigned char *rdata, size_t rdata_len, struct GNUNET_GNSRECORD_Block **result)
 
static enum GNUNET_GenericReturnValue block_seal (void *cls, const char *label, struct GNUNET_TIME_Absolute expire, unsigned char *rdata, size_t rdata_len, struct GNUNET_GNSRECORD_Block **result)
 
struct GNUNET_GNSRECORD_EncryptionContextGNUNET_GNSRECORD_encryption_context_setup_owner (const struct GNUNET_CRYPTO_BlindablePrivateKey *sk)
 Create a new encryption context for the zone owner.
 
struct GNUNET_GNSRECORD_EncryptionContextGNUNET_GNSRECORD_encryption_context_setup_resolver (const struct GNUNET_CRYPTO_BlindablePublicKey *zkey)
 Create a new encryption context for a resolver.
 
void GNUNET_GNSRECORD_encryption_context_destroy (struct GNUNET_GNSRECORD_EncryptionContext *ec)
 Cleanup and free the encryption context.
 
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_block_decrypt (const struct GNUNET_GNSRECORD_Block *block, const struct GNUNET_CRYPTO_BlindablePublicKey *zone_key, const char *label, GNUNET_GNSRECORD_RecordCallback proc, void *proc_cls)
 Decrypt block.
 

Detailed Description

API for GNS record-related crypto.

Author
Martin Schanzenbach
Matthias Wachs
Christian Grothoff

Definition in file gnsrecord_crypto.c.

Macro Definition Documentation

◆ LOG

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

Definition at line 31 of file gnsrecord_crypto.c.

◆ CSIZE

#define CSIZE   64

Function Documentation

◆ GNR_derive_block_aes_key()

void GNR_derive_block_aes_key ( unsigned char *  ctr,
unsigned char *  key,
const char *  label,
uint64_t  exp,
const struct GNUNET_CRYPTO_EcdsaPublicKey pub 
)

Derive session key and iv from label and public key.

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

4 byte nonce

Expiration time 64 bit.

Set counter part to 1

Definition at line 34 of file gnsrecord_crypto.c.

39{
40 static const char ctx_key[] = "gns-aes-ctx-key";
41 static const char ctx_iv[] = "gns-aes-ctx-iv";
42
44 ctx_key, strlen (ctx_key),
45 pub, sizeof(struct
48 memset (ctr, 0, GNUNET_CRYPTO_AES_KEY_LENGTH / 2);
51 ctx_iv, strlen (ctx_iv),
52 pub, sizeof(struct
56 memcpy (ctr + 4, &exp, sizeof (exp));
58 ctr[15] |= 0x01;
59}
struct GNUNET_HashCode key
The key used in the DHT.
static struct GNUNET_CRYPTO_EddsaPublicKey pub
#define GNUNET_CRYPTO_hkdf_gnunet(result, out_len, xts, xts_len, skm, skm_len,...)
A peculiar HKDF instantiation that tried to mimic Truncated NMAC.
#define GNUNET_CRYPTO_kdf_arg_string(d)
#define GNUNET_CRYPTO_AES_KEY_LENGTH
length of the sessionkey in bytes
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and ECDS...

References GNUNET_CRYPTO_AES_KEY_LENGTH, GNUNET_CRYPTO_hkdf_gnunet, GNUNET_CRYPTO_kdf_arg_string, key, and pub.

Referenced by block_create_ecdsa(), block_decrypt_ecdsa(), and run_pkey().

Here is the caller graph for this function:

◆ GNR_derive_block_xsalsa_key()

void GNR_derive_block_xsalsa_key ( struct GNUNET_CRYPTO_XSalsa20Nonce nonce,
struct GNUNET_CRYPTO_XSalsa20SecretKey key,
const char *  label,
uint64_t  exp,
const struct GNUNET_CRYPTO_EddsaPublicKey pub 
)

Derive session key and iv from label and public key.

Parameters
nonceinitialization vector to initialize
skeysession key to initialize
labellabel to use for KDF
pubpublic key to use for KDF

16 byte nonce

Expiration time 64 bit.

Definition at line 63 of file gnsrecord_crypto.c.

68{
69 static const char ctx_key[] = "gns-xsalsa-ctx-key";
70 static const char ctx_iv[] = "gns-xsalsa-ctx-iv";
71
73 key, crypto_secretbox_KEYBYTES,
74 ctx_key, strlen (ctx_key),
75 pub, sizeof(struct GNUNET_CRYPTO_EddsaPublicKey),
77 memset (nonce, 0, crypto_secretbox_NONCEBYTES);
80 nonce, (crypto_secretbox_NONCEBYTES - sizeof (exp)),
81 ctx_iv, strlen (ctx_iv),
82 pub, sizeof(struct GNUNET_CRYPTO_EddsaPublicKey),
85 memcpy (&nonce->nonce[crypto_secretbox_NONCEBYTES - sizeof (exp)],
86 &exp,
87 sizeof (exp));
88}
Public ECC key (always for curve Ed25519) encoded in a format suitable for network transmission and E...
unsigned char nonce[24]
Initialization vector.

References GNUNET_CRYPTO_hkdf_gnunet, GNUNET_CRYPTO_kdf_arg_string, key, GNUNET_CRYPTO_XSalsa20Nonce::nonce, and pub.

Referenced by block_create_eddsa(), block_decrypt_eddsa(), and run_edkey().

Here is the caller graph for this function:

◆ block_sign_ecdsa()

static enum GNUNET_GenericReturnValue block_sign_ecdsa ( const struct GNUNET_CRYPTO_EcdsaPrivateKey key,
const struct GNUNET_CRYPTO_EcdsaPublicKey pkey,
const char *  label,
struct GNUNET_GNSRECORD_Block block 
)
static

Definition at line 94 of file gnsrecord_crypto.c.

100{
101 struct GNRBlockPS *gnr_block;
102 struct GNUNET_GNSRECORD_EcdsaBlock *ecblock;
103 size_t size = ntohl (block->size) - sizeof (*block) + sizeof (*gnr_block);
104
105 gnr_block = GNUNET_malloc (size);
106 ecblock = &(block)->ecdsa_block;
107 gnr_block->purpose.size = htonl (size);
108 gnr_block->purpose.purpose =
110 gnr_block->expiration_time = ecblock->expiration_time;
111 /* encrypt and sign */
112 GNUNET_memcpy (&gnr_block[1], &ecblock[1],
113 size - sizeof (*gnr_block));
115 label,
116 "gns",
117 &ecblock->derived_key);
118 if (GNUNET_OK !=
120 label,
121 "gns",
122 &gnr_block->purpose,
123 &ecblock->signature))
124 {
125 GNUNET_break (0);
126 GNUNET_free (gnr_block);
127 return GNUNET_SYSERR;
128 }
129 GNUNET_free (gnr_block);
130 return GNUNET_OK;
131}
static char * pkey
Public key of the zone to look in, in ASCII.
#define GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN
GNS record set signature (GNS)
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.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecdsa_sign_derived(const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey, const char *label, const char *context, const struct GNUNET_CRYPTO_SignaturePurpose *purpose, struct GNUNET_CRYPTO_EcdsaSignature *sig)
This is a signature function for ECDSA which takes a private key, derives/blinds it and signs the mes...
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
static unsigned int size
Size of the "table".
Definition peer.c:68
Information we have in an encrypted block with record data (i.e.
struct GNUNET_TIME_AbsoluteNBO expiration_time
Expiration time of the block.
struct GNUNET_CRYPTO_SignaturePurpose purpose
Number of bytes signed; also specifies the number of bytes of encrypted data that follow.
uint32_t purpose
What does this signature vouch for? This must contain a GNUNET_SIGNATURE_PURPOSE_XXX constant (from g...
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!...
uint32_t size
Size of the block.
Information we have in an encrypted block with record data (i.e.
struct GNUNET_CRYPTO_EcdsaSignature signature
Signature of the block.
struct GNUNET_TIME_AbsoluteNBO expiration_time
Expiration time of the block.
struct GNUNET_CRYPTO_EcdsaPublicKey derived_key
Derived key used for signing; hash of this is the query.

References GNUNET_GNSRECORD_EcdsaBlock::derived_key, GNUNET_GNSRECORD_EcdsaBlock::expiration_time, GNRBlockPS::expiration_time, GNUNET_break, GNUNET_CRYPTO_ecdsa_public_key_derive(), GNUNET_CRYPTO_ecdsa_sign_derived(), GNUNET_free, GNUNET_malloc, GNUNET_memcpy, GNUNET_OK, GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN, GNUNET_SYSERR, key, pkey, GNUNET_CRYPTO_SignaturePurpose::purpose, GNRBlockPS::purpose, GNUNET_GNSRECORD_EcdsaBlock::signature, GNUNET_CRYPTO_SignaturePurpose::size, GNUNET_GNSRECORD_Block::size, and size.

Referenced by block_create_ecdsa(), and GNUNET_GNSRECORD_block_sign().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ block_sign_eddsa()

static enum GNUNET_GenericReturnValue block_sign_eddsa ( const struct GNUNET_CRYPTO_EddsaPrivateKey key,
const struct GNUNET_CRYPTO_EddsaPublicKey pkey,
const char *  label,
struct GNUNET_GNSRECORD_Block block 
)
static

Definition at line 137 of file gnsrecord_crypto.c.

143{
144 struct GNRBlockPS *gnr_block;
145 struct GNUNET_GNSRECORD_EddsaBlock *edblock;
146 size_t size = ntohl (block->size) - sizeof (*block) + sizeof (*gnr_block);
147 gnr_block = GNUNET_malloc (size);
148 edblock = &(block)->eddsa_block;
149 gnr_block->purpose.size = htonl (size);
150 gnr_block->purpose.purpose =
152 gnr_block->expiration_time = edblock->expiration_time;
153 GNUNET_memcpy (&gnr_block[1], &edblock[1],
154 size - sizeof (*gnr_block));
155 /* encrypt and sign */
157 label,
158 "gns",
159 &edblock->derived_key);
161 label,
162 "gns",
163 &gnr_block->purpose,
164 &edblock->signature);
165 GNUNET_free (gnr_block);
166 return GNUNET_OK;
167}
void GNUNET_CRYPTO_eddsa_public_key_derive(const struct GNUNET_CRYPTO_EddsaPublicKey *pub, const char *label, const char *context, struct GNUNET_CRYPTO_EddsaPublicKey *result)
Derive a public key from a given public key and a label.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_sign_derived(const struct GNUNET_CRYPTO_EddsaPrivateKey *pkey, const char *label, const char *context, const struct GNUNET_CRYPTO_SignaturePurpose *purpose, struct GNUNET_CRYPTO_EddsaSignature *sig)
This is a signature function for EdDSA which takes a private key and derives it using the label and c...
Information we have in an encrypted block with record data (i.e.
struct GNUNET_CRYPTO_EddsaPublicKey derived_key
Derived key used for signing; hash of this is the query.
struct GNUNET_TIME_AbsoluteNBO expiration_time
Expiration time of the block.
struct GNUNET_CRYPTO_EddsaSignature signature
Signature of the block.

References GNUNET_GNSRECORD_EddsaBlock::derived_key, GNUNET_GNSRECORD_EddsaBlock::expiration_time, GNRBlockPS::expiration_time, GNUNET_CRYPTO_eddsa_public_key_derive(), GNUNET_CRYPTO_eddsa_sign_derived(), GNUNET_free, GNUNET_malloc, GNUNET_memcpy, GNUNET_OK, GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN, key, pkey, GNUNET_CRYPTO_SignaturePurpose::purpose, GNRBlockPS::purpose, GNUNET_GNSRECORD_EddsaBlock::signature, GNUNET_CRYPTO_SignaturePurpose::size, GNUNET_GNSRECORD_Block::size, and size.

Referenced by block_create_eddsa(), and GNUNET_GNSRECORD_block_sign().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ block_create_ecdsa()

static enum GNUNET_GenericReturnValue block_create_ecdsa ( const struct GNUNET_CRYPTO_EcdsaPrivateKey key,
const struct GNUNET_CRYPTO_EcdsaPublicKey pkey,
struct GNUNET_TIME_Absolute  expire,
const char *  label,
const unsigned char *  rdata,
size_t  rdata_len,
struct GNUNET_GNSRECORD_Block **  block,
int  sign 
)
static

Sign name and records.

Parameters
keythe private key
pkeyassociated public key
expireblock expiration
labelthe name for the records
rdrecord data
rd_countnumber of records
blockthe block result. Must be allocated sufficiently.
signsign the block GNUNET_NO if block will be signed later.
Returns
GNUNET_SYSERR on error (otherwise GNUNET_OK)

Definition at line 220 of file gnsrecord_crypto.c.

228{
229 struct GNUNET_GNSRECORD_EcdsaBlock *ecblock;
230 unsigned char ctr[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
231 unsigned char skey[GNUNET_CRYPTO_AES_KEY_LENGTH];
232
233 if (rdata_len > GNUNET_GNSRECORD_MAX_BLOCK_SIZE)
234 {
235 GNUNET_break (0);
236 return GNUNET_SYSERR;
237 }
238 /* serialize */
239 *block = GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Block) + rdata_len);
240 (*block)->size = htonl (sizeof (struct GNUNET_GNSRECORD_Block) + rdata_len);
241 {
242 ecblock = &(*block)->ecdsa_block;
243 (*block)->type = htonl (GNUNET_GNSRECORD_TYPE_PKEY);
246 skey,
247 label,
249 pkey);
251 rdata_len,
252 skey,
253 ctr,
254 &ecblock[1]);
255 }
256 if (GNUNET_YES != sign)
257 return GNUNET_OK;
258 if (GNUNET_OK !=
259 block_sign_ecdsa (key, pkey, label, *block))
260 {
261 GNUNET_break (0);
262 GNUNET_free (*block);
263 return GNUNET_SYSERR;
264 }
265 return GNUNET_OK;
266}
static enum GNUNET_GenericReturnValue block_sign_ecdsa(const struct GNUNET_CRYPTO_EcdsaPrivateKey *key, const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey, const char *label, struct GNUNET_GNSRECORD_Block *block)
void GNR_derive_block_aes_key(unsigned char *ctr, unsigned char *key, const char *label, uint64_t exp, const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Derive session key and iv from label and public key.
#define GNUNET_GNSRECORD_TYPE_PKEY
WARNING: This header is generated! In order to add GNS record types, you must register them in GANA,...
static char * expire
DID Document expiration Date Attribute String.
Definition gnunet-did.c:98
void GNUNET_CRYPTO_aes_ctr(const void *in_buf, size_t in_buf_len, const unsigned char key[(256/8)], const unsigned char iv[(128/8)], void *out_buf)
Decrypt or encrypt a given block using a symmetric key using AES in counter mode.
#define GNUNET_GNSRECORD_MAX_BLOCK_SIZE
Maximum size of a value that can be stored in a GNS block.
@ GNUNET_YES
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition time.c:636
uint64_t abs_value_us__
The actual value (in network byte order).

References GNUNET_TIME_AbsoluteNBO::abs_value_us__, block_sign_ecdsa(), GNUNET_GNSRECORD_EcdsaBlock::expiration_time, expire, GNR_derive_block_aes_key(), GNUNET_break, GNUNET_CRYPTO_aes_ctr(), GNUNET_CRYPTO_AES_KEY_LENGTH, GNUNET_free, GNUNET_GNSRECORD_MAX_BLOCK_SIZE, GNUNET_GNSRECORD_TYPE_PKEY, GNUNET_malloc, GNUNET_OK, GNUNET_SYSERR, GNUNET_TIME_absolute_hton(), GNUNET_YES, key, and pkey.

Referenced by block_create2().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ block_create_eddsa()

static enum GNUNET_GenericReturnValue block_create_eddsa ( const struct GNUNET_CRYPTO_EddsaPrivateKey key,
const struct GNUNET_CRYPTO_EddsaPublicKey pkey,
struct GNUNET_TIME_Absolute  expire,
const char *  label,
const unsigned char *  rdata,
size_t  rdata_len,
struct GNUNET_GNSRECORD_Block **  block,
int  sign 
)
static

Sign name and records (EDDSA version)

Parameters
keythe private key
pkeyassociated public key
expireblock expiration
labelthe name for the records
rdrecord data
rd_countnumber of records
blockwhere to store the block. Must be allocated sufficiently.
signGNUNET_YES if block shall be signed as well
Returns
GNUNET_SYSERR on error (otherwise GNUNET_OK)

Definition at line 283 of file gnsrecord_crypto.c.

291{
292 struct GNUNET_GNSRECORD_EddsaBlock *edblock;
295
296 if (rdata_len > GNUNET_GNSRECORD_MAX_BLOCK_SIZE)
297 {
298 GNUNET_break (0);
299 return GNUNET_SYSERR;
300 }
301 /* serialize */
302 *block = GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Block)
303 + rdata_len + crypto_secretbox_MACBYTES);
304 (*block)->size = htonl (sizeof (struct GNUNET_GNSRECORD_Block)
305 + rdata_len + crypto_secretbox_MACBYTES);
306 {
307 edblock = &(*block)->eddsa_block;
308 (*block)->type = htonl (GNUNET_GNSRECORD_TYPE_EDKEY);
311 &skey,
312 label,
314 pkey);
317 rdata_len,
318 (unsigned char*) rdata,
319 &skey,
320 &nonce,
321 &edblock[1]));
322 if (GNUNET_YES != sign)
323 return GNUNET_OK;
324 block_sign_eddsa (key, pkey, label, *block);
325 }
326 return GNUNET_OK;
327}
static enum GNUNET_GenericReturnValue block_sign_eddsa(const struct GNUNET_CRYPTO_EddsaPrivateKey *key, const struct GNUNET_CRYPTO_EddsaPublicKey *pkey, const char *label, struct GNUNET_GNSRECORD_Block *block)
void GNR_derive_block_xsalsa_key(struct GNUNET_CRYPTO_XSalsa20Nonce *nonce, struct GNUNET_CRYPTO_XSalsa20SecretKey *key, const char *label, uint64_t exp, const struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Derive session key and iv from label and public key.
#define GNUNET_GNSRECORD_TYPE_EDKEY
GNS zone delegation (EDKEY)
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_xsalsa20poly1305_encrypt(size_t in_buf_len, const unsigned char in_buf[in_buf_len], const struct GNUNET_CRYPTO_XSalsa20SecretKey *key, const struct GNUNET_CRYPTO_XSalsa20Nonce *nonce, void *out_buf)
Encrypt the given data using XSalsa20-Poly1305.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.

References GNUNET_TIME_AbsoluteNBO::abs_value_us__, block_sign_eddsa(), GNUNET_GNSRECORD_EddsaBlock::expiration_time, expire, GNR_derive_block_xsalsa_key(), GNUNET_assert, GNUNET_break, GNUNET_CRYPTO_xsalsa20poly1305_encrypt(), GNUNET_GNSRECORD_MAX_BLOCK_SIZE, GNUNET_GNSRECORD_TYPE_EDKEY, GNUNET_malloc, GNUNET_OK, GNUNET_SYSERR, GNUNET_TIME_absolute_hton(), GNUNET_YES, key, GNUNET_CRYPTO_XSalsa20Nonce::nonce, and pkey.

Referenced by block_create2().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ block_create2()

static enum GNUNET_GenericReturnValue block_create2 ( const struct GNUNET_CRYPTO_BlindablePrivateKey pkey,
struct GNUNET_TIME_Absolute  expire,
const char *  label,
const unsigned char *  rdata,
size_t  rdata_len,
struct GNUNET_GNSRECORD_Block **  result,
int  sign 
)
static

Definition at line 349 of file gnsrecord_crypto.c.

356{
358 struct GNUNET_CRYPTO_EddsaPublicKey edpubkey;
360 char *norm_label;
361#define CSIZE 64
362 static struct KeyCacheLine cache[CSIZE];
363 struct KeyCacheLine *line;
364
365 norm_label = GNUNET_GNSRECORD_string_normalize (label);
366
367 if (GNUNET_PUBLIC_KEY_TYPE_ECDSA == ntohl (pkey->type))
368 {
369 key = &pkey->ecdsa_key;
370
371 line = &cache[(*(unsigned int *) key) % CSIZE];
372 if (0 != memcmp (&line->key,
373 key,
374 sizeof(*key)))
375 {
376 /* cache miss, recompute */
377 line->key = *key;
379 &line->pkey);
380 }
382 &line->pkey,
383 expire,
384 norm_label,
385 rdata,
386 rdata_len,
387 result,
388 sign);
389 }
390 else if (GNUNET_PUBLIC_KEY_TYPE_EDDSA == ntohl (pkey->type))
391 {
393 &edpubkey);
394 res = block_create_eddsa (&pkey->eddsa_key,
395 &edpubkey,
396 expire,
397 norm_label,
398 rdata,
399 rdata_len,
400 result,
401 sign);
402 }
403#undef CSIZE
404 GNUNET_free (norm_label);
405 return res;
406}
static enum GNUNET_GenericReturnValue block_create_ecdsa(const struct GNUNET_CRYPTO_EcdsaPrivateKey *key, const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey, struct GNUNET_TIME_Absolute expire, const char *label, const unsigned char *rdata, size_t rdata_len, struct GNUNET_GNSRECORD_Block **block, int sign)
Sign name and records.
#define CSIZE
static enum GNUNET_GenericReturnValue block_create_eddsa(const struct GNUNET_CRYPTO_EddsaPrivateKey *key, const struct GNUNET_CRYPTO_EddsaPublicKey *pkey, struct GNUNET_TIME_Absolute expire, const char *label, const unsigned char *rdata, size_t rdata_len, struct GNUNET_GNSRECORD_Block **block, int sign)
Sign name and records (EDDSA version)
static char * line
Desired phone line (string to be converted to a hash).
static char * res
Currently read line or NULL on EOF.
static int result
Global testing status.
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_ecdsa_key_get_public(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Derive key.
Definition crypto_ecc.c:190
char * GNUNET_GNSRECORD_string_normalize(const char *src)
Normalize a UTF-8 string to a GNS name.
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_PUBLIC_KEY_TYPE_EDDSA
EDDSA identity.
@ GNUNET_PUBLIC_KEY_TYPE_ECDSA
The identity type.
Private ECC key encoded for transmission.
Line in cache mapping private keys to public keys.

References block_create_ecdsa(), block_create_eddsa(), CSIZE, expire, GNUNET_CRYPTO_ecdsa_key_get_public(), GNUNET_CRYPTO_eddsa_key_get_public(), GNUNET_free, GNUNET_GNSRECORD_string_normalize(), GNUNET_PUBLIC_KEY_TYPE_ECDSA, GNUNET_PUBLIC_KEY_TYPE_EDDSA, GNUNET_SYSERR, key, KeyCacheLine::key, line, pkey, res, and result.

Referenced by block_seal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ block_decrypt_ecdsa()

static enum GNUNET_GenericReturnValue block_decrypt_ecdsa ( const struct GNUNET_GNSRECORD_Block block,
const struct GNUNET_CRYPTO_EcdsaPublicKey zone_key,
const char *  label,
GNUNET_GNSRECORD_RecordCallback  proc,
void *  proc_cls 
)
static

Definition at line 467 of file gnsrecord_crypto.c.

473{
474 size_t payload_len = ntohl (block->size)
475 - sizeof (struct GNUNET_GNSRECORD_Block);
476 unsigned char ctr[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
477 unsigned char key[GNUNET_CRYPTO_AES_KEY_LENGTH];
478
479 if (ntohl (block->size) <
480 sizeof (struct GNUNET_GNSRECORD_Block))
481 {
482 GNUNET_break_op (0);
483 return GNUNET_SYSERR;
484 }
485 GNUNET_assert (payload_len <= UINT16_MAX);
487 key,
488 label,
490 zone_key);
491 {
492 char payload[payload_len];
493 unsigned int rd_count;
494
495 GNUNET_CRYPTO_aes_ctr (&block[1],
496 payload_len,
497 key,
498 ctr,
499 payload);
501 payload);
502 if (rd_count > 2048)
503 {
504 /* limit to sane value */
505 GNUNET_break_op (0);
506 return GNUNET_SYSERR;
507 }
508 {
510 unsigned int j;
511 struct GNUNET_TIME_Absolute now;
512
513 if (GNUNET_OK !=
515 payload,
516 rd_count,
517 rd))
518 {
519 GNUNET_break_op (0);
520 return GNUNET_SYSERR;
521 }
522 /* hide expired records */
524 j = 0;
525 for (unsigned int i = 0; i < rd_count; i++)
526 {
527 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
528 {
529 /* encrypted blocks must never have relative expiration times, skip! */
530 GNUNET_break_op (0);
531 continue;
532 }
533
534 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW))
535 {
536 int include_record = GNUNET_YES;
537 /* Shadow record, figure out if we have a not expired active record */
538 for (unsigned int k = 0; k < rd_count; k++)
539 {
540 if (k == i)
541 continue;
542 if (rd[i].expiration_time < now.abs_value_us)
543 include_record = GNUNET_NO; /* Shadow record is expired */
544 if ((rd[k].record_type == rd[i].record_type) &&
545 (rd[k].expiration_time >= now.abs_value_us) &&
546 (0 == (rd[k].flags & GNUNET_GNSRECORD_RF_SHADOW)))
547 {
548 include_record = GNUNET_NO; /* We have a non-expired, non-shadow record of the same type */
550 "Ignoring shadow record\n");
551 break;
552 }
553 }
554 if (GNUNET_YES == include_record)
555 {
556 rd[i].flags ^= GNUNET_GNSRECORD_RF_SHADOW; /* Remove Flag */
557 if (j != i)
558 rd[j] = rd[i];
559 j++;
560 }
561 }
562 else if (rd[i].expiration_time >= now.abs_value_us)
563 {
564 /* Include this record */
565 if (j != i)
566 rd[j] = rd[i];
567 j++;
568 }
569 else
570 {
571 struct GNUNET_TIME_Absolute at;
572
575 "Excluding record that expired %s (%llu ago)\n",
577 (unsigned long long) rd[i].expiration_time
578 - now.abs_value_us);
579 }
580 }
581 rd_count = j;
582 if (NULL != proc)
583 proc (proc_cls,
584 rd_count,
585 (0 != rd_count) ? rd : NULL);
586 }
587 }
588 return GNUNET_OK;
589}
static unsigned int rd_count
Number of records for currently parsed set.
static struct GNUNET_GNSRECORD_Data rd[50]
The record data under a single label.
static unsigned long long payload
How much data are we currently storing in the database?
int GNUNET_GNSRECORD_records_deserialize(size_t len, const char *src, unsigned int rd_count, struct GNUNET_GNSRECORD_Data *dest)
Deserialize the given records to the given destination.
unsigned int GNUNET_GNSRECORD_records_deserialize_get_size(size_t len, const char *src)
@ GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION
This expiration time of the record is a relative time (not an absolute time).
@ GNUNET_GNSRECORD_RF_SHADOW
This record should not be used unless all (other) records in the set with an absolute expiration time...
#define GNUNET_log(kind,...)
#define GNUNET_NZL(l)
Macro used to avoid using 0 for the length of a variable-size array (Non-Zero-Length).
@ GNUNET_NO
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
@ GNUNET_ERROR_TYPE_INFO
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition time.c:111
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition strings.c:671
struct GNUNET_GNSRECORD_EcdsaBlock ecdsa_block
enum GNUNET_GNSRECORD_Flags flags
Flags for the record.
uint64_t expiration_time
Expiration time for the DNS record.
Time for absolute times used by GNUnet, in microseconds.
uint64_t abs_value_us
The actual value.

References GNUNET_TIME_Absolute::abs_value_us, GNUNET_TIME_AbsoluteNBO::abs_value_us__, GNUNET_GNSRECORD_Block::ecdsa_block, GNUNET_GNSRECORD_Data::expiration_time, GNUNET_GNSRECORD_EcdsaBlock::expiration_time, GNUNET_GNSRECORD_Data::flags, GNR_derive_block_aes_key(), GNUNET_assert, GNUNET_break_op, GNUNET_CRYPTO_aes_ctr(), GNUNET_CRYPTO_AES_KEY_LENGTH, GNUNET_ERROR_TYPE_INFO, GNUNET_GNSRECORD_records_deserialize(), GNUNET_GNSRECORD_records_deserialize_get_size(), GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION, GNUNET_GNSRECORD_RF_SHADOW, GNUNET_log, GNUNET_NO, GNUNET_NZL, GNUNET_OK, GNUNET_STRINGS_absolute_time_to_string(), GNUNET_SYSERR, GNUNET_TIME_absolute_get(), GNUNET_YES, key, payload, rd, rd_count, and GNUNET_GNSRECORD_Block::size.

Referenced by block_open_ecdsa().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ block_decrypt_eddsa()

static enum GNUNET_GenericReturnValue block_decrypt_eddsa ( const struct GNUNET_GNSRECORD_Block block,
const struct GNUNET_CRYPTO_EddsaPublicKey zone_key,
const char *  label,
GNUNET_GNSRECORD_RecordCallback  proc,
void *  proc_cls 
)
static

Definition at line 593 of file gnsrecord_crypto.c.

599{
600 size_t payload_len = ntohl (block->size)
601 - sizeof (struct GNUNET_GNSRECORD_Block);
604
605
606 if (ntohl (block->size) <
607 sizeof(struct GNUNET_GNSRECORD_Block))
608 {
609 GNUNET_break_op (0);
610 return GNUNET_SYSERR;
611 }
613 &skey,
614 label,
616 ,
617 zone_key);
618 {
619 char payload[payload_len];
620 unsigned int rd_count;
621
624 payload_len,
625 (unsigned char*) &block[1],
626 &skey,
627 &nonce,
628 payload));
629 payload_len -= crypto_secretbox_MACBYTES;
631 payload);
632 if (rd_count > 2048)
633 {
634 /* limit to sane value */
635 GNUNET_break_op (0);
636 return GNUNET_SYSERR;
637 }
638 {
640 unsigned int j;
641 struct GNUNET_TIME_Absolute now;
642
643 if (GNUNET_OK !=
645 payload,
646 rd_count,
647 rd))
648 {
649 GNUNET_break_op (0);
650 return GNUNET_SYSERR;
651 }
652 /* hide expired records */
654 j = 0;
655 for (unsigned int i = 0; i < rd_count; i++)
656 {
657 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
658 {
659 /* encrypted blocks must never have relative expiration times, skip! */
660 GNUNET_break_op (0);
661 continue;
662 }
663
664 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW))
665 {
666 int include_record = GNUNET_YES;
667 /* Shadow record, figure out if we have a not expired active record */
668 for (unsigned int k = 0; k < rd_count; k++)
669 {
670 if (k == i)
671 continue;
672 if (rd[i].expiration_time < now.abs_value_us)
673 include_record = GNUNET_NO; /* Shadow record is expired */
674 if ((rd[k].record_type == rd[i].record_type) &&
675 (rd[k].expiration_time >= now.abs_value_us) &&
676 (0 == (rd[k].flags & GNUNET_GNSRECORD_RF_SHADOW)))
677 {
678 include_record = GNUNET_NO; /* We have a non-expired, non-shadow record of the same type */
680 "Ignoring shadow record\n");
681 break;
682 }
683 }
684 if (GNUNET_YES == include_record)
685 {
686 rd[i].flags ^= GNUNET_GNSRECORD_RF_SHADOW; /* Remove Flag */
687 if (j != i)
688 rd[j] = rd[i];
689 j++;
690 }
691 }
692 else if (rd[i].expiration_time >= now.abs_value_us)
693 {
694 /* Include this record */
695 if (j != i)
696 rd[j] = rd[i];
697 j++;
698 }
699 else
700 {
701 struct GNUNET_TIME_Absolute at;
702
705 "Excluding record that expired %s (%llu ago)\n",
707 (unsigned long long) rd[i].expiration_time
708 - now.abs_value_us);
709 }
710 }
711 rd_count = j;
712 if (NULL != proc)
713 proc (proc_cls,
714 rd_count,
715 (0 != rd_count) ? rd : NULL);
716 }
717 }
718 return GNUNET_OK;
719}
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_xsalsa20poly1305_decrypt(size_t in_buf_len, const unsigned char in_buf[in_buf_len], const struct GNUNET_CRYPTO_XSalsa20SecretKey *key, const struct GNUNET_CRYPTO_XSalsa20Nonce *nonce, void *out_buf)
Encrypt the given data using XSalsa20-Poly1305.
struct GNUNET_GNSRECORD_EddsaBlock eddsa_block

References GNUNET_TIME_Absolute::abs_value_us, GNUNET_TIME_AbsoluteNBO::abs_value_us__, GNUNET_GNSRECORD_Block::eddsa_block, GNUNET_GNSRECORD_Data::expiration_time, GNUNET_GNSRECORD_EddsaBlock::expiration_time, GNUNET_GNSRECORD_Data::flags, GNR_derive_block_xsalsa_key(), GNUNET_assert, GNUNET_break_op, GNUNET_CRYPTO_xsalsa20poly1305_decrypt(), GNUNET_ERROR_TYPE_INFO, GNUNET_GNSRECORD_records_deserialize(), GNUNET_GNSRECORD_records_deserialize_get_size(), GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION, GNUNET_GNSRECORD_RF_SHADOW, GNUNET_log, GNUNET_NO, GNUNET_NZL, GNUNET_OK, GNUNET_STRINGS_absolute_time_to_string(), GNUNET_SYSERR, GNUNET_TIME_absolute_get(), GNUNET_YES, GNUNET_CRYPTO_XSalsa20Nonce::nonce, payload, rd, rd_count, and GNUNET_GNSRECORD_Block::size.

Referenced by block_open_eddsa().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ block_open_ecdsa()

static enum GNUNET_GenericReturnValue block_open_ecdsa ( void *  cls,
const char *  label,
const struct GNUNET_GNSRECORD_Block block,
GNUNET_GNSRECORD_RecordCallback  proc,
void *  proc_cls 
)
static

Definition at line 810 of file gnsrecord_crypto.c.

815{
816 struct EncryptionContextData *ecd = cls;
818 char *norm_label;
819
820 norm_label = GNUNET_GNSRECORD_string_normalize (label);
821 return block_decrypt_ecdsa (block,
822 &ecd->zkey.ecdsa_key,
823 norm_label, proc,
824 proc_cls);
825 GNUNET_free (norm_label);
826 return res;
827
828}
static enum GNUNET_GenericReturnValue block_decrypt_ecdsa(const struct GNUNET_GNSRECORD_Block *block, const struct GNUNET_CRYPTO_EcdsaPublicKey *zone_key, const char *label, GNUNET_GNSRECORD_RecordCallback proc, void *proc_cls)
struct GNUNET_CRYPTO_BlindablePublicKey zkey
struct GNUNET_CRYPTO_EcdsaPublicKey ecdsa_key
An ECDSA identity key.

References block_decrypt_ecdsa(), GNUNET_CRYPTO_BlindablePublicKey::ecdsa_key, GNUNET_free, GNUNET_GNSRECORD_string_normalize(), GNUNET_SYSERR, res, and EncryptionContextData::zkey.

Referenced by GNUNET_GNSRECORD_encryption_context_setup_owner(), and GNUNET_GNSRECORD_encryption_context_setup_resolver().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ block_open_eddsa()

static enum GNUNET_GenericReturnValue block_open_eddsa ( void *  cls,
const char *  label,
const struct GNUNET_GNSRECORD_Block block,
GNUNET_GNSRECORD_RecordCallback  proc,
void *  proc_cls 
)
static

Definition at line 832 of file gnsrecord_crypto.c.

837{
838 struct EncryptionContextData *ecd = cls;
840 char *norm_label;
841
842 norm_label = GNUNET_GNSRECORD_string_normalize (label);
843 res = block_decrypt_eddsa (block,
844 &ecd->zkey.eddsa_key,
845 norm_label, proc,
846 proc_cls);
847 GNUNET_free (norm_label);
848 return res;
849}
static enum GNUNET_GenericReturnValue block_decrypt_eddsa(const struct GNUNET_GNSRECORD_Block *block, const struct GNUNET_CRYPTO_EddsaPublicKey *zone_key, const char *label, GNUNET_GNSRECORD_RecordCallback proc, void *proc_cls)
struct GNUNET_CRYPTO_EddsaPublicKey eddsa_key
AN EdDSA identtiy key.

References block_decrypt_eddsa(), GNUNET_CRYPTO_BlindablePublicKey::eddsa_key, GNUNET_free, GNUNET_GNSRECORD_string_normalize(), res, and EncryptionContextData::zkey.

Referenced by GNUNET_GNSRECORD_encryption_context_setup_owner(), and GNUNET_GNSRECORD_encryption_context_setup_resolver().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ block_seal_not_implemented()

static enum GNUNET_GenericReturnValue block_seal_not_implemented ( void *  cls,
const char *  label,
struct GNUNET_TIME_Absolute  expire,
unsigned char *  rdata,
size_t  rdata_len,
struct GNUNET_GNSRECORD_Block **  result 
)
static

Definition at line 853 of file gnsrecord_crypto.c.

859{
860 GNUNET_break (0);
861 return GNUNET_SYSERR;
862}

References GNUNET_break, and GNUNET_SYSERR.

Referenced by GNUNET_GNSRECORD_encryption_context_setup_resolver().

Here is the caller graph for this function:

◆ block_seal()

static enum GNUNET_GenericReturnValue block_seal ( void *  cls,
const char *  label,
struct GNUNET_TIME_Absolute  expire,
unsigned char *  rdata,
size_t  rdata_len,
struct GNUNET_GNSRECORD_Block **  result 
)
static

Definition at line 866 of file gnsrecord_crypto.c.

872{
873 struct EncryptionContextData *ecd = cls;
874
875 return block_create2 (ecd->sk,
876 expire,
877 label,
878 rdata,
879 rdata_len,
880 result,
881 GNUNET_YES);
882}
static enum GNUNET_GenericReturnValue block_create2(const struct GNUNET_CRYPTO_BlindablePrivateKey *pkey, struct GNUNET_TIME_Absolute expire, const char *label, const unsigned char *rdata, size_t rdata_len, struct GNUNET_GNSRECORD_Block **result, int sign)
struct GNUNET_CRYPTO_BlindablePrivateKey * sk

References block_create2(), expire, GNUNET_YES, result, and EncryptionContextData::sk.

Referenced by GNUNET_GNSRECORD_encryption_context_setup_owner().

Here is the call graph for this function:
Here is the caller graph for this function: