34 #define EXTRA_CHECKS 0
59 #define CURVE "Ed25519"
61 #define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-ecc", __VA_ARGS__)
63 #define LOG_STRERROR(kind, syscall) \
64 GNUNET_log_from_strerror (kind, "util-crypto-ecc", syscall)
66 #define LOG_STRERROR_FILE(kind, syscall, filename) \
67 GNUNET_log_from_strerror_file (kind, "util-crypto-ecc", syscall, \
75 #define LOG_GCRY(level, cmd, rc) \
79 _ ("`%s' failed at %s:%d with error: %s\n"), \
83 gcry_strerror (rc)); \
106 list = gcry_sexp_find_token (sexp, topname, 0);
109 l2 = gcry_sexp_cadr (
list);
110 gcry_sexp_release (
list);
116 for (
const char *s = elems; *s; s++, idx++)
118 l2 = gcry_sexp_find_token (
list, s, 1);
121 for (
unsigned int i = 0; i < idx; i++)
123 gcry_free (array[i]);
126 gcry_sexp_release (
list);
129 array[idx] = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG);
130 gcry_sexp_release (l2);
133 for (
unsigned int i = 0; i < idx; i++)
135 gcry_free (array[i]);
138 gcry_sexp_release (
list);
142 gcry_sexp_release (
list);
161 for (
size_t i = 0; i<32; i++)
162 d[i] = priv->
d[31 - i];
164 rc = gcry_sexp_build (&
result,
166 "(private-key(ecc(curve \"" CURVE "\")"
176 if (0 != (rc = gcry_pk_testkey (
result)))
192 crypto_scalarmult_ed25519_base_noclamp (
pub->
q_y, priv->
d);
202 unsigned char pk[crypto_sign_PUBLICKEYBYTES];
203 unsigned char sk[crypto_sign_SECRETKEYBYTES];
208 sodium_memzero (sk, crypto_sign_SECRETKEYBYTES);
233 keylen += 5 - keylen % 5;
260 keylen += 5 - keylen % 5;
287 keylen += 5 - keylen % 5;
314 keylen += 5 - keylen % 5;
341 keylen += 5 - keylen % 5;
343 if (enclen != keylen)
366 keylen += 5 - keylen % 5;
368 if (enclen != keylen)
391 keylen += 5 - keylen % 5;
393 if (enclen != keylen)
496 anonymous.
d[0] &= 248;
497 anonymous.
d[31] &= 127;
498 anonymous.
d[31] |= 64;
521 if (0 != (rc = gcry_sexp_build (&
data,
523 "(data(flags rfc6979)(hash %s %b))",
541 gcry_sexp_t priv_sexp;
542 gcry_sexp_t sig_sexp;
551 if (0 != (rc = gcry_pk_sign (&sig_sexp,
data, priv_sexp)))
554 _ (
"ECC signing failed at %s:%d: %s\n"),
558 gcry_sexp_release (
data);
559 gcry_sexp_release (priv_sexp);
562 gcry_sexp_release (priv_sexp);
563 gcry_sexp_release (
data);
567 if (0 != (rc =
key_from_sexp (rs, sig_sexp,
"sig-val",
"rs")))
570 gcry_sexp_release (sig_sexp);
573 gcry_sexp_release (sig_sexp);
576 gcry_mpi_release (rs[0]);
577 gcry_mpi_release (rs[1]);
592 unsigned char sk[crypto_sign_SECRETKEYBYTES];
593 unsigned char pk[crypto_sign_PUBLICKEYBYTES];
597 res = crypto_sign_detached ((uint8_t *) sig,
613 size_t mlen = ntohl (purpose->
size);
614 unsigned char sk[crypto_sign_SECRETKEYBYTES];
615 unsigned char pk[crypto_sign_PUBLICKEYBYTES];
620 res = crypto_sign_detached ((uint8_t *) sig,
638 gcry_sexp_t sig_sexpr;
639 gcry_sexp_t pub_sexpr;
644 if (purpose != ntohl (validate->
purpose))
648 if (0 != (rc = gcry_sexp_build (&sig_sexpr,
650 "(sig-val(ecdsa(r %b)(s %b)))",
651 (
int)
sizeof(sig->
r),
653 (
int)
sizeof(sig->
s),
660 if (0 != (rc = gcry_sexp_build (&pub_sexpr,
662 "(public-key(ecc(curve " CURVE ")(q %b)))",
666 gcry_sexp_release (
data);
667 gcry_sexp_release (sig_sexpr);
670 rc = gcry_pk_verify (sig_sexpr,
data, pub_sexpr);
671 gcry_sexp_release (pub_sexpr);
672 gcry_sexp_release (
data);
673 gcry_sexp_release (sig_sexpr);
677 _ (
"ECDSA signature verification failed at %s:%d: %s\n"),
696 const unsigned char *
m = (
const void *) validate;
697 size_t mlen = ntohl (validate->
size);
698 const unsigned char *s = (
const void *) sig;
702 if (purpose != ntohl (validate->
purpose))
707 res = crypto_sign_verify_detached (s,
m, mlen,
pub->
q_y);
718 uint8_t
p[crypto_scalarmult_BYTES];
719 if (0 != crypto_scalarmult (
p, priv->
d,
pub->
q_y))
732 uint8_t a[crypto_scalarmult_SCALARBYTES];
733 uint8_t
p[crypto_scalarmult_BYTES];
739 if (0 != crypto_scalarmult (
p, a,
pub->
q_y))
742 crypto_scalarmult_BYTES,
763 uint8_t
p[crypto_scalarmult_BYTES];
766 if (0 != crypto_scalarmult (
p, priv->
d,
pub->
q_y))
769 crypto_scalarmult_BYTES,
781 uint8_t
p[crypto_scalarmult_BYTES];
782 uint8_t curve25510_pk[crypto_scalarmult_BYTES];
784 if (0 != crypto_sign_ed25519_pk_to_curve25519 (curve25510_pk,
pub->
q_y))
786 if (0 != crypto_scalarmult (
p, priv->
d, curve25510_pk))
895 if (0 != memcmp (&pub_test, &c->
pub, sizeof (c->
pub)))
944 uint8_t
p[crypto_scalarmult_BYTES];
945 uint8_t curve25510_pk[crypto_scalarmult_BYTES];
947 if (0 != crypto_sign_ed25519_pk_to_curve25519 (curve25510_pk,
pub->
q_y))
949 if (0 != crypto_scalarmult (
p, priv->
d, curve25510_pk))
benchmarking for various operations
#define BENCHMARK_START(opname)
#define BENCHMARK_END(opname)
static int key_from_sexp(gcry_mpi_t *array, gcry_sexp_t sexp, const char *topname, const char *elems)
Extract values from an S-expression.
static gcry_sexp_t data_to_ecdsa_value(const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose)
Convert the data specified in the given purpose argument to an S-expression suitable for signature op...
#define CURVE
IMPLEMENTATION NOTICE:
#define LOG_GCRY(level, cmd, rc)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
static enum GNUNET_GenericReturnValue fo_kem_decaps(const struct GNUNET_HashCode *w, const struct GNUNET_CRYPTO_FoKemC *c, struct GNUNET_HashCode *key_material)
static gcry_sexp_t decode_private_ecdsa_key(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv)
Convert the given private key from the network format to the S-expression that can be used by libgcry...
static int once
Global to mark if we've run the initialization.
static int ret
Return value of the commandline.
static struct GNUNET_ARM_MonitorHandle * m
Monitor connection with ARM.
static int list
Set if we should print a list of currently running services.
static int end
Set if we are to shutdown all services (including ARM).
static OpusEncoder * enc
OPUS encoder.
uint32_t data
The data value.
struct GNUNET_IDENTITY_PrivateKey pk
Private key from command line option, or NULL.
static int result
Global testing status.
static struct GNUNET_CRYPTO_EddsaPublicKey pub
static struct GNUNET_OS_Process * p
Helper process we started.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecdsa_fo_kem_decaps(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, struct GNUNET_CRYPTO_FoKemC *c, struct GNUNET_HashCode *key_material)
Decapsulate key material using a CCA-secure KEM.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecc_ecdh(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, const struct GNUNET_CRYPTO_EcdhePublicKey *pub, struct GNUNET_HashCode *key_material)
Derive key material from a public and a private ECC key.
void GNUNET_CRYPTO_ecdhe_key_create(struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
Create a new private key.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_kem_decaps(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, const struct GNUNET_CRYPTO_EcdhePublicKey *c, struct GNUNET_HashCode *key_material)
Decapsulate a key for a private EdDSA key.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_fo_kem_encaps(const struct GNUNET_CRYPTO_EddsaPublicKey *pub, struct GNUNET_CRYPTO_FoKemC *c, struct GNUNET_HashCode *key_material)
Encapsulate key material using a CCA-secure KEM.
void GNUNET_CRYPTO_random_block(enum GNUNET_CRYPTO_Quality mode, void *buffer, size_t length)
Fill block with a random values.
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.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecdsa_ecdh(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, const struct GNUNET_CRYPTO_EcdhePublicKey *pub, struct GNUNET_HashCode *key_material)
Derive key material from a ECDH public key and a private ECDSA key.
void GNUNET_CRYPTO_eddsa_key_clear(struct GNUNET_CRYPTO_EddsaPrivateKey *pk)
Clear memory that was used to store a private key.
enum GNUNET_GenericReturnValue 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.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecdsa_fo_kem_encaps(const struct GNUNET_CRYPTO_EcdsaPublicKey *pub, struct GNUNET_CRYPTO_FoKemC *c, struct GNUNET_HashCode *key_material)
Encapsulate key material using a CCA-secure KEM.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_kem_encaps(const struct GNUNET_CRYPTO_EddsaPublicKey *pub, struct GNUNET_CRYPTO_EcdhePublicKey *c, struct GNUNET_HashCode *key_material)
Encapsulate key material for a EdDSA public key.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_ecdh(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, const struct GNUNET_CRYPTO_EcdhePublicKey *pub, struct GNUNET_HashCode *key_material)
Derive key material from a ECDH public key and a private EdDSA key.
const struct GNUNET_CRYPTO_EcdsaPrivateKey * GNUNET_CRYPTO_ecdsa_key_get_anonymous()
Get the shared private key we use for anonymous users.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecdh_ecdsa(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, const struct GNUNET_CRYPTO_EcdsaPublicKey *pub, struct GNUNET_HashCode *key_material)
Derive key material from a EcDSA public key and a private ECDH key.
void GNUNET_CRYPTO_eddsa_key_create(struct GNUNET_CRYPTO_EddsaPrivateKey *pk)
Create a new private key.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecdh_eddsa(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, const struct GNUNET_CRYPTO_EddsaPublicKey *pub, struct GNUNET_HashCode *key_material)
Derive key material from a EdDSA public key and a private ECDH key.
void GNUNET_CRYPTO_ecdsa_key_create(struct GNUNET_CRYPTO_EcdsaPrivateKey *pk)
Create a new private key.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_sign_(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, struct GNUNET_CRYPTO_EddsaSignature *sig)
EdDSA sign a given block.
void GNUNET_CRYPTO_ecdsa_key_clear(struct GNUNET_CRYPTO_EcdsaPrivateKey *pk)
Clear memory that was used to store a private key.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecdsa_verify_(uint32_t purpose, const struct GNUNET_CRYPTO_EccSignaturePurpose *validate, const struct GNUNET_CRYPTO_EcdsaSignature *sig, const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Verify ECDSA signature.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_fo_kem_decaps(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, const struct GNUNET_CRYPTO_FoKemC *c, struct GNUNET_HashCode *key_material)
This implementation is not testes/publicly exposed yet.
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.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_verify_(uint32_t purpose, const struct GNUNET_CRYPTO_EccSignaturePurpose *validate, const struct GNUNET_CRYPTO_EddsaSignature *sig, const struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Verify EdDSA signature.
void GNUNET_CRYPTO_ecdhe_key_clear(struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
Clear memory that was used to store a private key.
void GNUNET_CRYPTO_ecdhe_key_get_public(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, struct GNUNET_CRYPTO_EcdhePublicKey *pub)
Extract the public key for the given private key.
@ GNUNET_CRYPTO_QUALITY_NONCE
Randomness for IVs etc.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
void GNUNET_CRYPTO_hash_xor(const struct GNUNET_HashCode *a, const struct GNUNET_HashCode *b, struct GNUNET_HashCode *result)
compute result = a ^ b
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_sign_raw(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, void *data, size_t size, struct GNUNET_CRYPTO_EddsaSignature *sig)
char * GNUNET_CRYPTO_ecdsa_public_key_to_string(const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Convert a public key to a string.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecdsa_public_key_from_string(const char *enc, size_t enclen, struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Convert a string representing a public key to a public key.
char * GNUNET_CRYPTO_eddsa_public_key_to_string(const struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Convert a public key to a string.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_public_key_from_string(const char *enc, size_t enclen, struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Convert a string representing a public key to a public key.
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.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_private_key_from_string(const char *enc, size_t enclen, struct GNUNET_CRYPTO_EddsaPrivateKey *priv)
Convert a string representing a private key to a private key.
GNUNET_GenericReturnValue
Named constants for return values.
char * GNUNET_CRYPTO_ecdsa_private_key_to_string(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv)
Convert a private key to a string.
char * GNUNET_CRYPTO_eddsa_private_key_to_string(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv)
Convert a private key to a string.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
char * GNUNET_STRINGS_data_to_string(const void *data, size_t size, char *out, size_t out_size)
Convert binary data to ASCII encoding using CrockfordBase32.
enum GNUNET_GenericReturnValue GNUNET_STRINGS_string_to_data(const char *enc, size_t enclen, void *out, size_t out_size)
Convert CrockfordBase32 encoding back to data.
static unsigned int size
Size of the "table".
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 (!...
uint32_t purpose
What does this signature vouch for? This must contain a GNUNET_SIGNATURE_PURPOSE_XXX constant (from g...
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 encr...
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
unsigned char s[256/8]
S value.
unsigned char r[256/8]
R value.
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 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.
This is the encapsulated key of our FO-KEM.
struct GNUNET_CRYPTO_EcdhePublicKey pub