2 This file is part of GNUnet.
3 (C) 2009-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file gnsrecord/gnsrecord_crypto.c
23 * @brief API for GNS record-related crypto
24 * @author Martin Schanzenbach
25 * @author Matthias Wachs
26 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_signatures.h"
32 #include "gnunet_arm_service.h"
33 #include "gnunet_gnsrecord_lib.h"
34 #include "gnunet_dnsparser_lib.h"
35 #include "gnunet_tun_lib.h"
38 #define LOG(kind,...) GNUNET_log_from (kind, "gnsrecord",__VA_ARGS__)
42 * Derive session key and iv from label and public key.
44 * @param iv initialization vector to initialize
45 * @param skey session key to initialize
46 * @param label label to use for KDF
47 * @param pub public key to use for KDF
50 derive_block_aes_key (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
51 struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
53 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
55 static const char ctx_key[] = "gns-aes-ctx-key";
56 static const char ctx_iv[] = "gns-aes-ctx-iv";
58 GNUNET_CRYPTO_kdf (skey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
59 pub, sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
60 label, strlen (label),
61 ctx_key, strlen (ctx_key),
63 GNUNET_CRYPTO_kdf (iv, sizeof (struct GNUNET_CRYPTO_SymmetricInitializationVector),
64 pub, sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
65 label, strlen (label),
66 ctx_iv, strlen (ctx_iv),
72 * Sign name and records
74 * @param key the private key
75 * @param expire block expiration
76 * @param label the name for the records
77 * @param rd record data
78 * @param rd_count number of records
79 * @return NULL on error (block too large)
81 struct GNUNET_NAMESTORE_Block *
82 GNUNET_NAMESTORE_block_create (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
83 struct GNUNET_TIME_Absolute expire,
85 const struct GNUNET_NAMESTORE_RecordData *rd,
86 unsigned int rd_count)
88 size_t payload_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
89 char payload[sizeof (uint32_t) + payload_len];
90 struct GNUNET_NAMESTORE_Block *block;
91 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
92 struct GNUNET_CRYPTO_EcdsaPrivateKey *dkey;
93 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
94 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
95 uint32_t rd_count_nbo;
97 if (payload_len > GNUNET_NAMESTORE_MAX_VALUE_SIZE)
99 rd_count_nbo = htonl (rd_count);
100 memcpy (payload, &rd_count_nbo, sizeof (uint32_t));
101 GNUNET_assert (payload_len ==
102 GNUNET_NAMESTORE_records_serialize (rd_count, rd,
103 payload_len, &payload[sizeof (uint32_t)]));
104 block = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Block) +
105 sizeof (uint32_t) + payload_len);
106 block->purpose.size = htonl (sizeof (uint32_t) + payload_len +
107 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
108 sizeof (struct GNUNET_TIME_AbsoluteNBO));
109 block->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
110 block->expiration_time = GNUNET_TIME_absolute_hton (expire);
111 dkey = GNUNET_CRYPTO_ecdsa_private_key_derive (key,
114 GNUNET_CRYPTO_ecdsa_key_get_public (dkey,
115 &block->derived_key);
116 GNUNET_CRYPTO_ecdsa_key_get_public (key,
118 derive_block_aes_key (&iv, &skey, label, &pkey);
119 GNUNET_break (payload_len + sizeof (uint32_t) ==
120 GNUNET_CRYPTO_symmetric_encrypt (payload, payload_len + sizeof (uint32_t),
124 GNUNET_CRYPTO_ecdsa_sign (dkey,
139 * Check if a signature is valid. This API is used by the GNS Block
140 * to validate signatures received from the network.
142 * @param block block to verify
143 * @return #GNUNET_OK if the signature is valid
146 GNUNET_NAMESTORE_block_verify (const struct GNUNET_NAMESTORE_Block *block)
148 return GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN,
151 &block->derived_key);
158 * @param block block to decrypt
159 * @param zone_key public key of the zone
160 * @param label the name for the records
161 * @param proc function to call with the result
162 * @param proc_cls closure for proc
163 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block was
167 GNUNET_NAMESTORE_block_decrypt (const struct GNUNET_NAMESTORE_Block *block,
168 const struct GNUNET_CRYPTO_EcdsaPublicKey *zone_key,
170 GNUNET_NAMESTORE_RecordCallback proc,
173 size_t payload_len = ntohl (block->purpose.size) -
174 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) -
175 sizeof (struct GNUNET_TIME_AbsoluteNBO);
176 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
177 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
179 if (ntohl (block->purpose.size) <
180 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
181 sizeof (struct GNUNET_TIME_AbsoluteNBO))
184 return GNUNET_SYSERR;
186 derive_block_aes_key (&iv, &skey, label, zone_key);
188 char payload[payload_len];
191 GNUNET_break (payload_len ==
192 GNUNET_CRYPTO_symmetric_decrypt (&block[1], payload_len,
198 rd_count = ntohl (rd_count);
201 /* limit to sane value */
203 return GNUNET_SYSERR;
206 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
209 GNUNET_NAMESTORE_records_deserialize (payload_len - sizeof (uint32_t),
210 &payload[sizeof (uint32_t)],
215 return GNUNET_SYSERR;
218 proc (proc_cls, rd_count, (0 != rd_count) ? rd : NULL);
226 * Calculate the DHT query for a given @a label in a given @a zone.
228 * @param zone private key of the zone
229 * @param label label of the record
230 * @param query hash to use for the query
233 GNUNET_NAMESTORE_query_from_private_key (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
235 struct GNUNET_HashCode *query)
237 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
239 GNUNET_CRYPTO_ecdsa_key_get_public (zone, &pub);
240 GNUNET_NAMESTORE_query_from_public_key (&pub, label, query);
245 * Calculate the DHT query for a given @a label in a given @a zone.
247 * @param pub public key of the zone
248 * @param label label of the record
249 * @param query hash to use for the query
252 GNUNET_NAMESTORE_query_from_public_key (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
254 struct GNUNET_HashCode *query)
256 struct GNUNET_CRYPTO_EcdsaPublicKey pd;
258 GNUNET_CRYPTO_ecdsa_public_key_derive (pub, label, "gns", &pd);
259 GNUNET_CRYPTO_hash (&pd, sizeof (pd), query);
263 /* end of gnsrecord_crypto.c */