/*
This file is part of GNUnet.
- (C) 2001-2013 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2001-2013 Christian Grothoff (and other contributing authors)
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
};
-/**
- * Paillier plaintext.
- */
-struct GNUNET_CRYPTO_PaillierPlaintext
-{
- /**
- * The bits of the plaintext.
- */
- unsigned char bits[GNUNET_CRYPTO_PAILLIER_BITS / 8];
-};
-
-
/**
* Paillier ciphertext.
*/
struct GNUNET_CRYPTO_PaillierCiphertext
{
/**
- * guaranteed minimum number of homomorphic operations with this ciphertext
+ * Guaranteed minimum number of homomorphic operations with this ciphertext,
+ * in network byte order (NBO).
*/
int32_t remaining_ops GNUNET_PACKED;
-
+
/**
* The bits of the ciphertext.
*/
* @return updated crc sum (must be subjected to #GNUNET_CRYPTO_crc16_finish to get actual crc16)
*/
uint32_t
-GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len);
+GNUNET_CRYPTO_crc16_step (uint32_t sum,
+ const void *buf,
+ size_t len);
/**
* @return crc16 value
*/
uint16_t
-GNUNET_CRYPTO_crc16_n (const void *buf, size_t len);
+GNUNET_CRYPTO_crc16_n (const void *buf,
+ size_t len);
/**
* @return the resulting CRC32 checksum
*/
int32_t
-GNUNET_CRYPTO_crc32_n (const void *buf, size_t len);
+GNUNET_CRYPTO_crc32_n (const void *buf,
+ size_t len);
/**
* @param length buffer length
*/
void
-GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode, void *buffer, size_t length);
+GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode,
+ void *buffer,
+ size_t length);
/**
* @ingroup crypto
* @return a random value in the interval [0,@a i) (exclusive).
*/
uint32_t
-GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i);
+GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode,
+ uint32_t i);
/**
* @return random 64-bit number
*/
uint64_t
-GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max);
+GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode,
+ uint64_t max);
/**
* @return the permutation array (allocated from heap)
*/
unsigned int *
-GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n);
+GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode,
+ unsigned int n);
/**
* @return the size of the encrypted block, -1 for errors
*/
ssize_t
-GNUNET_CRYPTO_symmetric_encrypt (const void *block, size_t size,
+GNUNET_CRYPTO_symmetric_encrypt (const void *block,
+ size_t size,
const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
void *result);
* @return -1 on failure, size of decrypted block on success
*/
ssize_t
-GNUNET_CRYPTO_symmetric_decrypt (const void *block, size_t size,
+GNUNET_CRYPTO_symmetric_decrypt (const void *block,
+ size_t size,
const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
void *result);
* safely cast to char*, a '\\0' termination is set).
*/
void
-GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode * block,
+GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode *block,
struct GNUNET_CRYPTO_HashAsciiEncoded *result);
* @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
*/
int
-GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen,
+GNUNET_CRYPTO_hash_from_string2 (const char *enc,
+ size_t enclen,
struct GNUNET_HashCode *result);
* @param ret pointer to where to write the hashcode
*/
void
-GNUNET_CRYPTO_hash (const void *block, size_t size, struct GNUNET_HashCode * ret);
+GNUNET_CRYPTO_hash (const void *block,
+ size_t size,
+ struct GNUNET_HashCode *ret);
+
+
+/**
+ * Context for cummulative hashing.
+ */
+struct GNUNET_HashContext;
+
+
+/**
+ * Start incremental hashing operation.
+ *
+ * @return context for incremental hash computation
+ */
+struct GNUNET_HashContext *
+GNUNET_CRYPTO_hash_context_start (void);
+
+
+/**
+ * Add data to be hashed.
+ *
+ * @param hc cummulative hash context
+ * @param buf data to add
+ * @param size number of bytes in @a buf
+ */
+void
+GNUNET_CRYPTO_hash_context_read (struct GNUNET_HashContext *hc,
+ const void *buf,
+ size_t size);
+
+
+/**
+ * Finish the hash computation.
+ *
+ * @param hc hash context to use, is freed in the process
+ * @param r_hash where to write the latest / final hash code
+ */
+void
+GNUNET_CRYPTO_hash_context_finish (struct GNUNET_HashContext *hc,
+ struct GNUNET_HashCode *r_hash);
+
+
+/**
+ * Abort hashing, do not bother calculating final result.
+ *
+ * @param hc hash context to destroy
+ */
+void
+GNUNET_CRYPTO_hash_context_abort (struct GNUNET_HashContext *hc);
/**
*/
void
GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
- const void *plaintext, size_t plaintext_len,
- struct GNUNET_HashCode * hmac);
+ const void *plaintext,
+ size_t plaintext_len,
+ struct GNUNET_HashCode *hmac);
/**
* @param cls closure
* @param res resulting hash, NULL on error
*/
-typedef void (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
- const struct GNUNET_HashCode *res);
+typedef void
+(*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
+ const struct GNUNET_HashCode *res);
/**
*/
struct GNUNET_CRYPTO_FileHashContext *
GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
- const char *filename, size_t blocksize,
+ const char *filename,
+ size_t blocksize,
GNUNET_CRYPTO_HashCompletedCallback callback,
void *callback_cls);
* @param result set to @a a ^ @a b
*/
void
-GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode * a, const struct GNUNET_HashCode * b,
- struct GNUNET_HashCode * result);
+GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a,
+ const struct GNUNET_HashCode *b,
+ struct GNUNET_HashCode *result);
/**
void
GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
- const void *salt, size_t salt_len, ...);
+ const void *salt, size_t salt_len,
+ ...);
/**
* @return #GNUNET_YES on success
*/
int
-GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
- const void *xts, size_t xts_len, const void *skm,
- size_t skm_len, ...);
+GNUNET_CRYPTO_hkdf (void *result,
+ size_t out_len,
+ int xtr_algo,
+ int prf_algo,
+ const void *xts,
+ size_t xts_len,
+ const void *skm,
+ size_t skm_len,
+ ...);
/**
* @return #GNUNET_YES on success
*/
int
-GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
- const void *xts, size_t xts_len, const void *skm,
- size_t skm_len, va_list argp);
+GNUNET_CRYPTO_hkdf_v (void *result,
+ size_t out_len,
+ int xtr_algo,
+ int prf_algo,
+ const void *xts,
+ size_t xts_len,
+ const void *skm,
+ size_t skm_len,
+ va_list argp);
/**
* @return #GNUNET_YES on success
*/
int
-GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts,
- size_t xts_len, const void *skm, size_t skm_len,
+GNUNET_CRYPTO_kdf_v (void *result,
+ size_t out_len,
+ const void *xts,
+ size_t xts_len,
+ const void *skm,
+ size_t skm_len,
va_list argp);
* @return #GNUNET_YES on success
*/
int
-GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts,
- size_t xts_len, const void *skm, size_t skm_len, ...);
+GNUNET_CRYPTO_kdf (void *result,
+ size_t out_len,
+ const void *xts,
+ size_t xts_len,
+ const void *skm,
+ size_t skm_len,
+ ...);
/**
struct GNUNET_CRYPTO_EddsaPublicKey *pub);
+
+/**
+ * @ingroup crypto
+ * Convert ECDSA public key to ECDHE public key.
+ * Please be very careful when using this function, as mixing
+ * cryptographic primitives is not always healthy.
+ *
+ * @param ecdsa ecdsa public key
+ * @param ecdhe[OUT] ecdhe public key
+ */
+void
+GNUNET_CRYPTO_ecdsa_public_to_ecdhe (const struct GNUNET_CRYPTO_EcdsaPublicKey *ecdsa,
+ struct GNUNET_CRYPTO_EcdhePublicKey *ecdhe);
+
+
+/**
+ * @ingroup crypto
+ * Convert ECDSA private key to ECDHE private key.
+ * Please be very careful when using this function, as mixing
+ * cryptographic primitives is not always healthy.
+ *
+ * @param ecdsa ecdsa private key
+ * @param ecdhe[OUT] ecdhe private key
+ */
+void
+GNUNET_CRYPTO_ecdsa_private_to_ecdhe (const struct GNUNET_CRYPTO_EcdsaPrivateKey *ecdsa,
+ struct GNUNET_CRYPTO_EcdhePrivateKey *ecdhe);
+
+
/**
* @ingroup crypto
* Extract the public key for the given private key.
struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
+/**
+ * Convert a string representing a private key to a private key.
+ *
+ * @param enc encoded public key
+ * @param enclen number of bytes in @a enc (without 0-terminator)
+ * @param priv where to store the private key
+ * @return #GNUNET_OK on success
+ */
+int
+GNUNET_CRYPTO_eddsa_private_key_from_string (const char *enc,
+ size_t enclen,
+ struct GNUNET_CRYPTO_EddsaPrivateKey *pub);
+
+
/**
* Convert a string representing a public key to a public key.
*
GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename);
+/**
+ * Forward declaration to simplify #include-structure.
+ */
+struct GNUNET_CONFIGURATION_Handle;
+
+
/**
* @ingroup crypto
* Create a new private key by reading our peer's key from
GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
struct GNUNET_PeerIdentity *dst);
+
/**
* Compare two Peer Identities.
*
*
* @param public_key Public key to use.
* @param m Plaintext to encrypt.
+ * @param desired_ops How many homomorphic ops the caller intends to use
* @param[out] ciphertext Encrytion of @a plaintext with @a public_key.
- * @return guaranteed number of supported homomorphic operations
+ * @return guaranteed number of supported homomorphic operations >= 1,
+ * or desired_ops, in case that is lower,
+ * or -1 if less than one homomorphic operation is possible
*/
int
GNUNET_CRYPTO_paillier_encrypt (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
const gcry_mpi_t m,
+ int desired_ops,
struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext);
/**
- * Get the number of remaining supported homomorphic operations.
+ * Get the number of remaining supported homomorphic operations.
*
* @param c Paillier cipher text.
* @return the number of remaining homomorphic operations
int
GNUNET_CRYPTO_paillier_hom_get_remaining (const struct GNUNET_CRYPTO_PaillierCiphertext *c);
+
+/* ********* Chaum-style RSA-based blind signatures ******************* */
+
+
+
+
+/**
+ * The private information of an RSA key pair.
+ */
+struct GNUNET_CRYPTO_rsa_PrivateKey;
+
+/**
+ * The public information of an RSA key pair.
+ */
+struct GNUNET_CRYPTO_rsa_PublicKey;
+
+/**
+ * Key used to blind a message
+ */
+struct GNUNET_CRYPTO_rsa_BlindingKey;
+
+/**
+ * @brief an RSA signature
+ */
+struct GNUNET_CRYPTO_rsa_Signature;
+
+
+/**
+ * Create a new private key. Caller must free return value.
+ *
+ * @param len length of the key in bits (i.e. 2048)
+ * @return fresh private key
+ */
+struct GNUNET_CRYPTO_rsa_PrivateKey *
+GNUNET_CRYPTO_rsa_private_key_create (unsigned int len);
+
+
+/**
+ * Free memory occupied by the private key.
+ *
+ * @param key pointer to the memory to free
+ */
+void
+GNUNET_CRYPTO_rsa_private_key_free (struct GNUNET_CRYPTO_rsa_PrivateKey *key);
+
+
+/**
+ * Encode the private key in a format suitable for
+ * storing it into a file.
+ *
+ * @param key the private key
+ * @param[out] buffer set to a buffer with the encoded key
+ * @return size of memory allocatedin @a buffer
+ */
+size_t
+GNUNET_CRYPTO_rsa_private_key_encode (const struct GNUNET_CRYPTO_rsa_PrivateKey *key,
+ char **buffer);
+
+
+/**
+ * Decode the private key from the data-format back
+ * to the "normal", internal format.
+ *
+ * @param buf the buffer where the private key data is stored
+ * @param len the length of the data in @a buf
+ * @return NULL on error
+ */
+struct GNUNET_CRYPTO_rsa_PrivateKey *
+GNUNET_CRYPTO_rsa_private_key_decode (const char *buf,
+ size_t len);
+
+
+/**
+ * Extract the public key of the given private key.
+ *
+ * @param priv the private key
+ * @retur NULL on error, otherwise the public key
+ */
+struct GNUNET_CRYPTO_rsa_PublicKey *
+GNUNET_CRYPTO_rsa_private_key_get_public (const struct GNUNET_CRYPTO_rsa_PrivateKey *priv);
+
+
+/**
+ * Compute hash over the public key.
+ *
+ * @param key public key to hash
+ * @param hc where to store the hash code
+ */
+void
+GNUNET_CRYPTO_rsa_public_key_hash (const struct GNUNET_CRYPTO_rsa_PublicKey *key,
+ struct GNUNET_HashCode *hc);
+
+
+/**
+ * Free memory occupied by the public key.
+ *
+ * @param key pointer to the memory to free
+ */
+void
+GNUNET_CRYPTO_rsa_public_key_free (struct GNUNET_CRYPTO_rsa_PublicKey *key);
+
+
+/**
+ * Encode the public key in a format suitable for
+ * storing it into a file.
+ *
+ * @param key the private key
+ * @param[out] buffer set to a buffer with the encoded key
+ * @return size of memory allocated in @a buffer
+ */
+size_t
+GNUNET_CRYPTO_rsa_public_key_encode (const struct GNUNET_CRYPTO_rsa_PublicKey *key,
+ char **buffer);
+
+
+/**
+ * Decode the public key from the data-format back
+ * to the "normal", internal format.
+ *
+ * @param buf the buffer where the public key data is stored
+ * @param len the length of the data in @a buf
+ * @return NULL on error
+ */
+struct GNUNET_CRYPTO_rsa_PublicKey *
+GNUNET_CRYPTO_rsa_public_key_decode (const char *buf,
+ size_t len);
+
+
+/**
+ * Create a blinding key
+ *
+ * @param len length of the key in bits (i.e. 2048)
+ * @return the newly created blinding key
+ */
+struct GNUNET_CRYPTO_rsa_BlindingKey *
+GNUNET_CRYPTO_rsa_blinding_key_create (unsigned int len);
+
+
+/**
+ * Destroy a blinding key
+ *
+ * @param bkey the blinding key to destroy
+ */
+void
+GNUNET_CRYPTO_rsa_blinding_key_free (struct GNUNET_CRYPTO_rsa_BlindingKey *bkey);
+
+
+/**
+ * Encode the blinding key in a format suitable for
+ * storing it into a file.
+ *
+ * @param bkey the blinding key
+ * @param[out] buffer set to a buffer with the encoded key
+ * @return size of memory allocated in @a buffer
+ */
+size_t
+GNUNET_CRYPTO_rsa_blinding_key_encode (const struct GNUNET_CRYPTO_rsa_BlindingKey *bkey,
+ char **buffer);
+
+
+/**
+ * Decode the blinding key from the data-format back
+ * to the "normal", internal format.
+ *
+ * @param buf the buffer where the public key data is stored
+ * @param len the length of the data in @a buf
+ * @return NULL on error
+ */
+struct GNUNET_CRYPTO_rsa_BlindingKey *
+GNUNET_CRYPTO_rsa_blinding_key_decode (const char *buf,
+ size_t len);
+
+
+/**
+ * Blinds the given message with the given blinding key
+ *
+ * @param hash hash of the message to sign
+ * @param bkey the blinding key
+ * @param pkey the public key of the signer
+ * @param[out] buffer set to a buffer with the blinded message to be signed
+ * @return number of bytes stored in @a buffer
+ */
+size_t
+GNUNET_CRYPTO_rsa_blind (const struct GNUNET_HashCode *hash,
+ struct GNUNET_CRYPTO_rsa_BlindingKey *bkey,
+ struct GNUNET_CRYPTO_rsa_PublicKey *pkey,
+ char **buffer);
+
+
+/**
+ * Sign the given message.
+ *
+ * @param key private key to use for the signing
+ * @param msg the (blinded) message to sign
+ * @param msg_len number of bytes in @a msg to sign
+ * @return NULL on error, signature on success
+ */
+struct GNUNET_CRYPTO_rsa_Signature *
+GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_rsa_PrivateKey *key,
+ const void *msg,
+ size_t msg_len);
+
+
+/**
+ * Free memory occupied by signature.
+ *
+ * @param sig memory to freee
+ */
+void
+GNUNET_CRYPTO_rsa_signature_free (struct GNUNET_CRYPTO_rsa_Signature *sig);
+
+
+/**
+ * Encode the given signature in a format suitable for storing it into a file.
+ *
+ * @param sig the signature
+ * @param[out] buffer set to a buffer with the encoded key
+ * @return size of memory allocated in @a buffer
+ */
+size_t
+GNUNET_CRYPTO_rsa_signature_encode (const struct GNUNET_CRYPTO_rsa_Signature *sig,
+ char **buffer);
+
+
+/**
+ * Decode the signature from the data-format back to the "normal", internal
+ * format.
+ *
+ * @param buf the buffer where the public key data is stored
+ * @param len the length of the data in @a buf
+ * @return NULL on error
+ */
+struct GNUNET_CRYPTO_rsa_Signature *
+GNUNET_CRYPTO_rsa_signature_decode (const char *buf,
+ size_t len);
+
+
+/**
+ * Unblind a blind-signed signature. The signature should have been generated
+ * with #GNUNET_CRYPTO_rsa_sign() using a hash that was blinded with
+ * #GNUNET_CRYPTO_rsa_blind().
+ *
+ * @param sig the signature made on the blinded signature purpose
+ * @param bkey the blinding key used to blind the signature purpose
+ * @param pkey the public key of the signer
+ * @return unblinded signature on success, NULL on error
+ */
+struct GNUNET_CRYPTO_rsa_Signature *
+GNUNET_CRYPTO_rsa_unblind (struct GNUNET_CRYPTO_rsa_Signature *sig,
+ struct GNUNET_CRYPTO_rsa_BlindingKey *bkey,
+ struct GNUNET_CRYPTO_rsa_PublicKey *pkey);
+
+
+/**
+ * Verify whether the given hash corresponds to the given signature and the
+ * signature is valid with respect to the given public key.
+ *
+ * @param hash the message to verify to match the @a sig
+ * @param sig signature that is being validated
+ * @param public_key public key of the signer
+ * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
+ */
+int
+GNUNET_CRYPTO_rsa_verify (const struct GNUNET_HashCode *hash,
+ const struct GNUNET_CRYPTO_rsa_Signature *sig,
+ const struct GNUNET_CRYPTO_rsa_PublicKey *public_key);
+
+
#if 0 /* keep Emacsens' auto-indent happy */
{
#endif