GNUnet 0.26.2-20-ga2d76f2e4
 
Loading...
Searching...
No Matches
crypto_ecc_gnsrecord.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013, 2015 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
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 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
29#include "platform.h"
30#include <gcrypt.h>
31#include <sodium.h>
32#include "gnunet_util_lib.h"
33
34#define CURVE "Ed25519"
35
47static void
48derive_h (const void *pub,
49 size_t pubsize,
50 const char *label,
51 const char *context,
52 struct GNUNET_HashCode *hc)
53{
60 static const char *const salt = "key-derivation";
61
63 hc,
64 sizeof(*hc),
65 salt,
66 strlen (salt),
67 pub,
68 pubsize,
71}
72
73
77 const char *label,
78 const char *context,
79 const struct GNUNET_CRYPTO_SignaturePurpose *purpose,
81{
83 crypto_hash_sha512_state hs;
84 unsigned char sk[64];
85 unsigned char r[64];
86 unsigned char hram[64];
87 unsigned char R[32];
88 unsigned char zk[32];
89 unsigned char tmp[32];
90 unsigned char r_mod[64];
91 unsigned char hram_mod[64];
92
97 label,
98 context,
99 &priv);
100
101 crypto_hash_sha512_init (&hs);
102
111 memcpy (sk, priv.s, 64);
112
117 crypto_scalarmult_ed25519_base_noclamp (zk,
118 sk);
119
128 crypto_hash_sha512_update (&hs, sk + 32, 32);
129 crypto_hash_sha512_update (&hs, (uint8_t*) purpose, ntohl (purpose->size));
130 crypto_hash_sha512_final (&hs, r);
131
135 memcpy (sig->s, zk, 32);
136
140 crypto_core_ed25519_scalar_reduce (r_mod, r);
141
145 crypto_scalarmult_ed25519_base_noclamp (R, r_mod);
146 memcpy (sig->r, R, sizeof (R));
147
152 crypto_hash_sha512_init (&hs);
153 crypto_hash_sha512_update (&hs, (uint8_t*) sig, 64);
154 crypto_hash_sha512_update (&hs, (uint8_t*) purpose,
155 ntohl (purpose->size));
156 crypto_hash_sha512_final (&hs, hram);
157
161 crypto_core_ed25519_scalar_reduce (hram_mod, hram);
162
167 crypto_core_ed25519_scalar_mul (tmp, hram_mod, sk);
168 crypto_core_ed25519_scalar_add (sig->s, tmp, r_mod);
169
170 sodium_memzero (sk, sizeof (sk));
171 sodium_memzero (r, sizeof (r));
172 sodium_memzero (r_mod, sizeof (r_mod));
173 return GNUNET_OK;
174}
175
176
179 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
180 const char *label,
181 const char *context,
182 const struct GNUNET_CRYPTO_SignaturePurpose *purpose,
184{
188 label,
189 context);
191 purpose,
192 sig);
194 return res;
195}
196
197
200 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
201 const char *label,
202 const char *context)
203{
206 struct GNUNET_HashCode hc;
207 uint8_t dc[32];
208 gcry_mpi_t h;
209 gcry_mpi_t x;
210 gcry_mpi_t d;
211 gcry_mpi_t n;
212 gcry_ctx_t ctx;
213
214 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, NULL, CURVE));
215
216 n = gcry_mpi_ec_get_mpi ("n", ctx, 1);
218
219 derive_h (&pub, sizeof (pub), label, context, &hc);
220 GNUNET_CRYPTO_mpi_scan_unsigned (&h, (unsigned char *) &hc, sizeof(hc));
221
222 /* Convert to big endian for libgcrypt */
223 for (size_t i = 0; i < 32; i++)
224 dc[i] = priv->d[31 - i];
226 d = gcry_mpi_new (256);
227 gcry_mpi_mulm (d, h, x, n);
228 gcry_mpi_release (h);
229 gcry_mpi_release (x);
230 gcry_mpi_release (n);
231 gcry_ctx_release (ctx);
234 /* Convert to big endian for libgcrypt */
235 for (size_t i = 0; i < 32; i++)
236 ret->d[i] = dc[31 - i];
237 sodium_memzero (dc, sizeof(dc));
238 gcry_mpi_release (d);
239 return ret;
240}
241
242
243void
245 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
246 const char *label,
247 const char *context,
249{
250 struct GNUNET_HashCode hc;
251 gcry_ctx_t ctx;
252 gcry_mpi_t q_y;
253 gcry_mpi_t h;
254 gcry_mpi_t n;
255 gcry_mpi_t h_mod_n;
256 gcry_mpi_point_t q;
257 gcry_mpi_point_t v;
258
259 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, NULL, CURVE));
260
261 /* obtain point 'q' from original public key. The provided 'q' is
262 compressed thus we first store it in the context and then get it
263 back as a (decompresssed) point. */
264 q_y = gcry_mpi_set_opaque_copy (NULL, pub->q_y, 8 * sizeof(pub->q_y));
265 GNUNET_assert (NULL != q_y);
266 GNUNET_assert (0 == gcry_mpi_ec_set_mpi ("q", q_y, ctx));
267 gcry_mpi_release (q_y);
268 q = gcry_mpi_ec_get_point ("q", ctx, 0);
270
271 /* calculate h_mod_n = h % n */
272 derive_h (pub, sizeof (*pub), label, context, &hc);
273 GNUNET_CRYPTO_mpi_scan_unsigned (&h, (unsigned char *) &hc, sizeof(hc));
274 n = gcry_mpi_ec_get_mpi ("n", ctx, 1);
275 h_mod_n = gcry_mpi_new (256);
276 gcry_mpi_mod (h_mod_n, h, n);
277 /* calculate v = h_mod_n * q */
278 v = gcry_mpi_point_new (0);
279 gcry_mpi_ec_mul (v, h_mod_n, q, ctx);
280 gcry_mpi_release (h_mod_n);
281 gcry_mpi_release (h);
282 gcry_mpi_release (n);
283 gcry_mpi_point_release (q);
284
285 /* convert point 'v' to public key that we return */
286 GNUNET_assert (0 == gcry_mpi_ec_set_point ("q", v, ctx));
287 gcry_mpi_point_release (v);
288 q_y = gcry_mpi_ec_get_mpi ("q@eddsa", ctx, 0);
289 GNUNET_assert (q_y);
290 GNUNET_CRYPTO_mpi_print_unsigned (result->q_y, sizeof(result->q_y), q_y);
291 gcry_mpi_release (q_y);
292 gcry_ctx_release (ctx);
293}
294
295
296void
298 const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
299 const char *label,
300 const char *context,
302{
304 struct GNUNET_HashCode hc;
305 uint8_t dc[32];
306 unsigned char sk[64];
307 gcry_mpi_t h;
308 gcry_mpi_t h_mod_L;
309 gcry_mpi_t a;
310 gcry_mpi_t d;
311 gcry_mpi_t L;
312 gcry_ctx_t ctx;
313
318 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, NULL, "Ed25519"));
319
323 L = gcry_mpi_ec_get_mpi ("n", ctx, 1);
325
332 crypto_hash_sha512 (sk, priv->d, 32);
333 sk[0] &= 248;
334 sk[31] &= 127;
335 sk[31] |= 64;
336
340 derive_h (&pub, sizeof (pub), label, context, &hc);
341 GNUNET_CRYPTO_mpi_scan_unsigned (&h, (unsigned char *) &hc, sizeof(hc));
342 h_mod_L = gcry_mpi_new (256);
343 gcry_mpi_mod (h_mod_L, h, L);
344 /* Convert scalar to big endian for libgcrypt */
345 for (size_t i = 0; i < 32; i++)
346 dc[i] = sk[31 - i];
347
353 GNUNET_CRYPTO_mpi_scan_unsigned (&a, dc, sizeof(dc)); // a
354 d = gcry_mpi_new (256);
355 gcry_mpi_mulm (d, h_mod_L, a, L); // d := h * a mod L
356 gcry_mpi_release (h);
357 gcry_mpi_release (a);
358 gcry_mpi_release (L);
359 gcry_mpi_release (h_mod_L);
360 gcry_ctx_release (ctx);
362 {
369 crypto_hash_sha256_state hs;
370 crypto_hash_sha256_init (&hs);
371 crypto_hash_sha256_update (&hs, sk + 32, 32);
372 crypto_hash_sha256_update (&hs, (unsigned char*) &hc, sizeof (hc));
373 crypto_hash_sha256_final (&hs, result->s + 32);
374 }
375 /* Convert to little endian for libsodium */
376 for (size_t i = 0; i < 32; i++)
377 result->s[i] = dc[31 - i];
378
379 sodium_memzero (dc, sizeof(dc));
380 gcry_mpi_release (d);
381}
382
383
384void
386 const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
387 const char *label,
388 const char *context,
390{
391 struct GNUNET_HashCode hc;
392 gcry_ctx_t ctx;
393 gcry_mpi_t q_y;
394 gcry_mpi_t h;
395 gcry_mpi_t n;
396 gcry_mpi_t h_mod_n;
397 gcry_mpi_point_t q;
398 gcry_mpi_point_t v;
399
400 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, NULL, "Ed25519"));
401
402 /* obtain point 'q' from original public key. The provided 'q' is
403 compressed thus we first store it in the context and then get it
404 back as a (decompresssed) point. */
405 q_y = gcry_mpi_set_opaque_copy (NULL, pub->q_y, 8 * sizeof(pub->q_y));
406 GNUNET_assert (NULL != q_y);
407 GNUNET_assert (0 == gcry_mpi_ec_set_mpi ("q", q_y, ctx));
408 gcry_mpi_release (q_y);
409 q = gcry_mpi_ec_get_point ("q", ctx, 0);
411
412 /* calculate h_mod_n = h % n */
413 derive_h (pub, sizeof (*pub), label, context, &hc);
414 GNUNET_CRYPTO_mpi_scan_unsigned (&h, (unsigned char *) &hc, sizeof(hc));
415
416 n = gcry_mpi_ec_get_mpi ("n", ctx, 1);
417 h_mod_n = gcry_mpi_new (256);
418 gcry_mpi_mod (h_mod_n, h, n);
419
420 /* calculate v = h_mod_n * q */
421 v = gcry_mpi_point_new (0);
422 gcry_mpi_ec_mul (v, h_mod_n, q, ctx);
423 gcry_mpi_release (h_mod_n);
424 gcry_mpi_release (h);
425 gcry_mpi_release (n);
426 gcry_mpi_point_release (q);
427
428 /* convert point 'v' to public key that we return */
429 GNUNET_assert (0 == gcry_mpi_ec_set_point ("q", v, ctx));
430 gcry_mpi_point_release (v);
431 q_y = gcry_mpi_ec_get_mpi ("q@eddsa", ctx, 0);
432 GNUNET_assert (q_y);
433 GNUNET_CRYPTO_mpi_print_unsigned (result->q_y, sizeof(result->q_y), q_y);
434 gcry_mpi_release (q_y);
435 gcry_ctx_release (ctx);
436
437}
438
439
440void
442 const struct GNUNET_CRYPTO_EddsaPrivateScalar *priv,
444{
445 unsigned char sk[32];
446
447 memcpy (sk, priv->s, 32);
448
453 crypto_scalarmult_ed25519_base_noclamp (pkey->q_y,
454 sk);
455}
static void derive_h(const void *pub, size_t pubsize, const char *label, const char *context, struct GNUNET_HashCode *hc)
Derive the 'h' value for key derivation, where 'h = H(l,P)'.
#define CURVE
static mp_limb_t d[(((256)+GMP_NUMB_BITS - 1)/GMP_NUMB_BITS)]
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition gnunet-arm.c:98
static int ret
Final status code.
Definition gnunet-arm.c:93
struct GNUNET_HashCode key
The key used in the DHT.
static struct GNUNET_FS_Handle * ctx
static struct GNUNET_FS_DownloadContext * dc
static pa_context * context
Pulseaudio context.
static char * pkey
Public key of the zone to look in, in ASCII.
static char * res
Currently read line or NULL on EOF.
static int result
Global testing status.
static struct GNUNET_REVOCATION_Query * q
Handle for revocation query.
static struct GNUNET_CRYPTO_EddsaPublicKey pub
static struct GNUNET_CRYPTO_PowSalt salt
Salt for PoW calculations.
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.
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.
void GNUNET_CRYPTO_eddsa_private_key_derive(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, const char *label, const char *context, struct GNUNET_CRYPTO_EddsaPrivateScalar *result)
Derive a private scalar from a given private key and a label.
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
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecdsa_sign_(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, const struct GNUNET_CRYPTO_SignaturePurpose *purpose, struct GNUNET_CRYPTO_EcdsaSignature *sig)
ECDSA Sign a given block.
Definition crypto_ecc.c:554
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.
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
#define GNUNET_CRYPTO_hkdf_gnunet(result, out_len, xts, xts_len, skm, skm_len,...)
A peculiar HKDF instantiation that tried to mimic Truncated NMAC.
void GNUNET_CRYPTO_mpi_scan_unsigned(gcry_mpi_t *result, const void *data, size_t size)
Convert data buffer into MPI value.
Definition crypto_mpi.c:132
#define GNUNET_CRYPTO_kdf_arg_string(d)
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecdsa_sign_derived(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, 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...
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...
void GNUNET_CRYPTO_mpi_print_unsigned(void *buf, size_t size, gcry_mpi_t val)
Output the given MPI value to the given buffer in network byte order.
Definition crypto_mpi.c:79
void GNUNET_CRYPTO_eddsa_key_get_public_from_scalar(const struct GNUNET_CRYPTO_EddsaPrivateScalar *priv, struct GNUNET_CRYPTO_EddsaPublicKey *pkey)
Extract the public key of the given private scalar.
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
Private ECC key encoded for transmission.
unsigned char d[256/8]
d is a value mod n, where n has at most 256 bits.
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and ECDS...
an ECC signature using ECDSA
Private ECC key encoded for transmission.
unsigned char d[256/8]
d is a value mod n, where n has at most 256 bits.
Private ECC scalar encoded for transmission.
unsigned char s[512/8]
s is the expandedprivate 512-bit scalar of a private key.
Public ECC key (always for curve Ed25519) encoded in a format suitable for network transmission and E...
unsigned char q_y[256/8]
Point Q consists of a y-value mod p (256 bits); the x-value is always positive.
an ECC signature using EdDSA.
unsigned char s[256/8]
S value.
unsigned char r[256/8]
R value.
header of what an ECC signature signs this must be followed by "size - 8" bytes of the actual signed ...
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!...
A 512-bit hashcode.