GNUnet 0.28.0-dev.2-18-g144d0c5be
 
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 <sodium.h>
31#include "gnunet_util_lib.h"
32
33#define CURVE "Ed25519"
34
46static void
47derive_h (const void *pub,
48 size_t pubsize,
49 const char *label,
50 const char *context,
51 struct GNUNET_HashCode *hc)
52{
59 static const char *const salt = "key-derivation";
60
62 hc,
63 sizeof(*hc),
64 salt,
65 strlen (salt),
66 pub,
67 pubsize,
70}
71
72
76 const char *label,
77 const char *context,
78 const struct GNUNET_CRYPTO_SignaturePurpose *purpose,
80{
82 crypto_hash_sha512_state hs;
83 unsigned char sk[64];
84 unsigned char r[64];
85 unsigned char hram[64];
86 unsigned char R[32];
87 unsigned char zk[32];
88 unsigned char tmp[32];
89 unsigned char r_mod[64];
90 unsigned char hram_mod[64];
91
96 label,
97 context,
98 &priv);
99
100 crypto_hash_sha512_init (&hs);
101
110 memcpy (sk, priv.s, 64);
111
116 crypto_scalarmult_ed25519_base_noclamp (zk,
117 sk);
118
127 crypto_hash_sha512_update (&hs, sk + 32, 32);
128 crypto_hash_sha512_update (&hs, (uint8_t*) purpose, ntohl (purpose->size));
129 crypto_hash_sha512_final (&hs, r);
130
134 memcpy (sig->s, zk, 32);
135
139 crypto_core_ed25519_scalar_reduce (r_mod, r);
140
144 crypto_scalarmult_ed25519_base_noclamp (R, r_mod);
145 memcpy (sig->r, R, sizeof (R));
146
151 crypto_hash_sha512_init (&hs);
152 crypto_hash_sha512_update (&hs, (uint8_t*) sig, 64);
153 crypto_hash_sha512_update (&hs, (uint8_t*) purpose,
154 ntohl (purpose->size));
155 crypto_hash_sha512_final (&hs, hram);
156
160 crypto_core_ed25519_scalar_reduce (hram_mod, hram);
161
166 crypto_core_ed25519_scalar_mul (tmp, hram_mod, sk);
167 crypto_core_ed25519_scalar_add (sig->s, tmp, r_mod);
168
169 sodium_memzero (sk, sizeof (sk));
170 sodium_memzero (r, sizeof (r));
171 sodium_memzero (r_mod, sizeof (r_mod));
172 return GNUNET_OK;
173}
174
175
176#pragma GCC diagnostic push
177#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
180 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
181 const char *label,
182 const char *context,
183 const struct GNUNET_CRYPTO_SignaturePurpose *purpose,
185{
189 label,
190 context);
192 purpose,
193 sig);
195 return res;
196}
197
198
201 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
202 const char *label,
203 const char *context)
204{
207 struct GNUNET_HashCode h;
208 unsigned char h_mod_L[crypto_core_ed25519_SCALARBYTES];
209 unsigned char h_le[64];
210
211
214
215 derive_h (&pub, sizeof (pub), label, context, &h);
216
221 for (size_t i = 0; i < 64; i++)
222 h_le[i] = ((unsigned char*) &h)[63 - i];
223
230 crypto_core_ed25519_scalar_reduce (h_mod_L,
231 (unsigned char*) &h_le);
232 crypto_core_ed25519_scalar_mul (ret->d, h_mod_L, priv->d);
233 return ret;
234}
235
236
237void
239 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
240 const char *label,
241 const char *context,
243{
244 struct GNUNET_HashCode hc;
245 unsigned char h_mod_L[crypto_core_ed25519_SCALARBYTES];
246 unsigned char h_le[64];
247
248 derive_h (pub, sizeof (*pub), label, context, &hc);
253 for (size_t i = 0; i < 64; i++)
254 h_le[i] = ((unsigned char*) &hc)[63 - i];
255
256
261 crypto_core_ed25519_scalar_reduce (h_mod_L,
262 (unsigned char*) &h_le);
263 GNUNET_assert (0 == crypto_scalarmult_ed25519_noclamp (result->q_y,
264 h_mod_L,
265 pub->q_y));
266}
267
268
269#pragma GCC diagnostic pop
270
271
272void
274 const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
275 const char *label,
276 const char *context,
278{
280 struct GNUNET_HashCode h;
281 unsigned char h_le[64];
282 unsigned char sk[64];
283 unsigned char *d;
284 unsigned char *nonce;
285 unsigned char h_mod_L[crypto_core_ed25519_SCALARBYTES];
286
287 d = result->s;
288 nonce = result->s + 32;
289
296 crypto_hash_sha512 (sk, priv->d, 32);
297 sk[0] &= 248;
298 sk[31] &= 127;
299 sk[31] |= 64;
300
305 derive_h (&pub, sizeof (pub), label, context, &h);
306
311 for (size_t i = 0; i < 64; i++)
312 h_le[i] = ((unsigned char*) &h)[63 - i];
313
314
321 crypto_core_ed25519_scalar_reduce (h_mod_L,
322 (unsigned char*) &h_le);
323 crypto_core_ed25519_scalar_mul (d, h_mod_L, sk);
324
325 {
332 crypto_hash_sha256_state hs;
333 crypto_hash_sha256_init (&hs);
334 crypto_hash_sha256_update (&hs, sk + 32, 32);
335 crypto_hash_sha256_update (&hs, (unsigned char*) &h, sizeof (h));
336 crypto_hash_sha256_final (&hs, nonce);
337 }
338
339}
340
341
342void
344 const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
345 const char *label,
346 const char *context,
348{
349 struct GNUNET_HashCode h;
350 unsigned char h_le[64];
351 unsigned char h_mod_L[crypto_core_ed25519_SCALARBYTES];
352
353 /* calculate h_mod_n = h % n */
354 derive_h (pub, sizeof (*pub), label, context, &h);
355
360 for (size_t i = 0; i < 64; i++)
361 h_le[i] = ((unsigned char*) &h)[63 - i];
362
368 crypto_core_ed25519_scalar_reduce (h_mod_L,
369 (unsigned char*) &h_le);
370
371 GNUNET_assert (0 == crypto_scalarmult_ed25519_noclamp (result->q_y,
372 h_mod_L,
373 pub->q_y));
374}
375
376
377void
379 const struct GNUNET_CRYPTO_EddsaPrivateScalar *priv,
381{
382 unsigned char sk[32];
383
384 memcpy (sk, priv->s, 32);
385
390 crypto_scalarmult_ed25519_base_noclamp (pkey->q_y,
391 sk);
392}
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)'.
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 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_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:574
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.
#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_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.