2015-01-27 16:18:33 +01:00
|
|
|
/*
|
|
|
|
This file is part of TALER
|
2015-02-08 00:16:22 +01:00
|
|
|
Copyright (C) 2014 Christian Grothoff (and other contributing authors)
|
2015-01-27 16:18:33 +01:00
|
|
|
|
|
|
|
TALER is free software; you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 3, or (at your option) any later version.
|
|
|
|
|
|
|
|
TALER is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
/**
|
2015-01-28 20:53:21 +01:00
|
|
|
* @file util/crypto.c
|
2015-01-27 16:18:33 +01:00
|
|
|
* @brief Cryptographic utility functions
|
|
|
|
* @author Sree Harsha Totakura <sreeharsha@totakura.in>
|
|
|
|
* @author Florian Dold
|
|
|
|
* @author Benedikt Mueller
|
2015-01-28 20:53:21 +01:00
|
|
|
* @author Christian Grothoff
|
2015-01-27 16:18:33 +01:00
|
|
|
*/
|
|
|
|
#include "platform.h"
|
|
|
|
#include "taler_util.h"
|
|
|
|
#include <gnunet/gnunet_util_lib.h>
|
|
|
|
#include <gcrypt.h>
|
|
|
|
|
|
|
|
|
2015-01-27 22:01:08 +01:00
|
|
|
/**
|
|
|
|
* Function called by libgcrypt on serious errors.
|
|
|
|
* Prints an error message and aborts the process.
|
|
|
|
*
|
|
|
|
* @param cls NULL
|
|
|
|
* @param wtf unknown
|
|
|
|
* @param msg error message
|
|
|
|
*/
|
2015-01-27 16:18:33 +01:00
|
|
|
static void
|
2015-01-27 22:01:08 +01:00
|
|
|
fatal_error_handler (void *cls,
|
|
|
|
int wtf,
|
|
|
|
const char *msg)
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-01-27 22:01:08 +01:00
|
|
|
LOG_ERROR ("Fatal error in libgcrypt: %s\n",
|
|
|
|
msg);
|
2015-01-27 16:18:33 +01:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2015-01-27 22:01:08 +01:00
|
|
|
* Initialize libgcrypt.
|
2015-01-27 16:18:33 +01:00
|
|
|
*/
|
|
|
|
void
|
2015-01-27 22:01:08 +01:00
|
|
|
TALER_gcrypt_init ()
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-03-22 22:14:30 +01:00
|
|
|
gcry_set_fatalerror_handler (&fatal_error_handler,
|
|
|
|
NULL);
|
2015-01-27 22:01:08 +01:00
|
|
|
TALER_assert_as (gcry_check_version (NEED_LIBGCRYPT_VERSION),
|
|
|
|
"libgcrypt version mismatch");
|
2015-01-27 16:18:33 +01:00
|
|
|
/* Disable secure memory. */
|
|
|
|
gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
|
|
|
|
gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Derive symmetric key material for refresh operations from
|
2015-01-27 22:01:08 +01:00
|
|
|
* a given shared secret for link decryption.
|
2015-01-27 16:18:33 +01:00
|
|
|
*
|
|
|
|
* @param secret the shared secret
|
|
|
|
* @param[out] iv set to initialization vector
|
|
|
|
* @param[out] skey set to session key
|
|
|
|
*/
|
|
|
|
static void
|
2015-01-27 22:01:08 +01:00
|
|
|
derive_refresh_key (const struct TALER_LinkSecret *secret,
|
2015-01-27 16:18:33 +01:00
|
|
|
struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
|
|
|
|
struct GNUNET_CRYPTO_SymmetricSessionKey *skey)
|
|
|
|
{
|
2015-01-27 22:01:08 +01:00
|
|
|
static const char ctx_key[] = "taler-link-skey";
|
|
|
|
static const char ctx_iv[] = "taler-link-iv";
|
|
|
|
|
|
|
|
GNUNET_assert (GNUNET_YES ==
|
|
|
|
GNUNET_CRYPTO_kdf (skey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
|
|
|
|
ctx_key, strlen (ctx_key),
|
|
|
|
secret, sizeof (struct TALER_LinkSecret),
|
|
|
|
NULL, 0));
|
|
|
|
GNUNET_assert (GNUNET_YES ==
|
|
|
|
GNUNET_CRYPTO_kdf (iv, sizeof (struct GNUNET_CRYPTO_SymmetricInitializationVector),
|
|
|
|
ctx_iv, strlen (ctx_iv),
|
|
|
|
secret, sizeof (struct TALER_LinkSecret),
|
|
|
|
NULL, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Derive symmetric key material for refresh operations from
|
|
|
|
* a given shared secret for key decryption.
|
|
|
|
*
|
|
|
|
* @param secret the shared secret
|
|
|
|
* @param[out] iv set to initialization vector
|
|
|
|
* @param[out] skey set to session key
|
|
|
|
*/
|
|
|
|
static void
|
2015-01-27 22:05:31 +01:00
|
|
|
derive_transfer_key (const struct TALER_TransferSecret *secret,
|
2015-01-27 22:01:08 +01:00
|
|
|
struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
|
|
|
|
struct GNUNET_CRYPTO_SymmetricSessionKey *skey)
|
|
|
|
{
|
|
|
|
static const char ctx_key[] = "taler-transfer-skey";
|
|
|
|
static const char ctx_iv[] = "taler-transfer-iv";
|
2015-01-27 16:18:33 +01:00
|
|
|
|
|
|
|
GNUNET_assert (GNUNET_YES ==
|
|
|
|
GNUNET_CRYPTO_kdf (skey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
|
|
|
|
ctx_key, strlen (ctx_key),
|
2015-01-27 22:05:31 +01:00
|
|
|
secret, sizeof (struct TALER_TransferSecret),
|
2015-01-27 16:18:33 +01:00
|
|
|
NULL, 0));
|
|
|
|
GNUNET_assert (GNUNET_YES ==
|
|
|
|
GNUNET_CRYPTO_kdf (iv, sizeof (struct GNUNET_CRYPTO_SymmetricInitializationVector),
|
|
|
|
ctx_iv, strlen (ctx_iv),
|
2015-01-27 22:05:31 +01:00
|
|
|
secret, sizeof (struct TALER_TransferSecret),
|
2015-01-27 16:18:33 +01:00
|
|
|
NULL, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-27 18:49:02 +01:00
|
|
|
/**
|
|
|
|
* Use the @a trans_sec (from ECDHE) to decrypt the @a secret_enc
|
|
|
|
* to obtain the @a secret to decrypt the linkage data.
|
|
|
|
*
|
2015-01-27 22:01:08 +01:00
|
|
|
* @param secret_enc encrypted secret
|
2015-01-27 22:05:31 +01:00
|
|
|
* @param trans_sec transfer secret
|
2015-01-27 18:49:02 +01:00
|
|
|
* @param secret shared secret for refresh link decryption
|
|
|
|
* @return #GNUNET_OK on success
|
|
|
|
*/
|
|
|
|
int
|
2015-01-27 22:01:08 +01:00
|
|
|
TALER_transfer_decrypt (const struct TALER_EncryptedLinkSecret *secret_enc,
|
2015-01-27 22:05:31 +01:00
|
|
|
const struct TALER_TransferSecret *trans_sec,
|
2015-01-27 22:01:08 +01:00
|
|
|
struct TALER_LinkSecret *secret)
|
2015-01-27 18:49:02 +01:00
|
|
|
{
|
|
|
|
struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
|
|
|
|
struct GNUNET_CRYPTO_SymmetricSessionKey skey;
|
|
|
|
|
2015-01-27 22:01:08 +01:00
|
|
|
GNUNET_assert (sizeof (struct TALER_EncryptedLinkSecret) ==
|
|
|
|
sizeof (struct TALER_LinkSecret));
|
|
|
|
derive_transfer_key (trans_sec, &iv, &skey);
|
2015-01-27 18:49:02 +01:00
|
|
|
return GNUNET_CRYPTO_symmetric_decrypt (secret_enc,
|
2015-01-27 22:01:08 +01:00
|
|
|
sizeof (struct TALER_LinkSecret),
|
2015-01-27 18:49:02 +01:00
|
|
|
&skey,
|
|
|
|
&iv,
|
|
|
|
secret);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-27 22:01:08 +01:00
|
|
|
/**
|
|
|
|
* Use the @a trans_sec (from ECDHE) to encrypt the @a secret
|
|
|
|
* to obtain the @a secret_enc.
|
|
|
|
*
|
|
|
|
* @param secret shared secret for refresh link decryption
|
2015-01-27 22:05:31 +01:00
|
|
|
* @param trans_sec transfer secret
|
2015-01-27 22:01:08 +01:00
|
|
|
* @param secret_enc[out] encrypted secret
|
|
|
|
* @return #GNUNET_OK on success
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
TALER_transfer_encrypt (const struct TALER_LinkSecret *secret,
|
2015-01-27 22:05:31 +01:00
|
|
|
const struct TALER_TransferSecret *trans_sec,
|
2015-01-27 22:01:08 +01:00
|
|
|
struct TALER_EncryptedLinkSecret *secret_enc)
|
|
|
|
{
|
|
|
|
struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
|
|
|
|
struct GNUNET_CRYPTO_SymmetricSessionKey skey;
|
|
|
|
|
|
|
|
GNUNET_assert (sizeof (struct TALER_EncryptedLinkSecret) ==
|
|
|
|
sizeof (struct TALER_LinkSecret));
|
|
|
|
derive_transfer_key (trans_sec, &iv, &skey);
|
|
|
|
return GNUNET_CRYPTO_symmetric_encrypt (secret,
|
|
|
|
sizeof (struct TALER_LinkSecret),
|
|
|
|
&skey,
|
|
|
|
&iv,
|
|
|
|
secret_enc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-27 16:31:18 +01:00
|
|
|
/**
|
|
|
|
* Decrypt refresh link information.
|
|
|
|
*
|
|
|
|
* @param input encrypted refresh link data
|
|
|
|
* @param secret shared secret to use for decryption
|
|
|
|
* @return NULL on error
|
|
|
|
*/
|
|
|
|
struct TALER_RefreshLinkDecrypted *
|
|
|
|
TALER_refresh_decrypt (const struct TALER_RefreshLinkEncrypted *input,
|
2015-01-27 22:01:08 +01:00
|
|
|
const struct TALER_LinkSecret *secret)
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-01-27 16:31:18 +01:00
|
|
|
struct TALER_RefreshLinkDecrypted *ret;
|
2015-01-27 16:18:33 +01:00
|
|
|
struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
|
|
|
|
struct GNUNET_CRYPTO_SymmetricSessionKey skey;
|
2015-01-27 16:31:18 +01:00
|
|
|
size_t buf_size = input->blinding_key_enc_size
|
|
|
|
+ sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
|
|
|
|
char buf[buf_size];
|
2015-01-27 16:18:33 +01:00
|
|
|
|
2015-01-27 16:31:18 +01:00
|
|
|
GNUNET_assert (input->blinding_key_enc == (const char *) &input[1]);
|
2015-01-27 16:18:33 +01:00
|
|
|
derive_refresh_key (secret, &iv, &skey);
|
2015-01-27 16:31:18 +01:00
|
|
|
if (GNUNET_OK !=
|
|
|
|
GNUNET_CRYPTO_symmetric_decrypt (input->coin_priv_enc,
|
|
|
|
buf_size,
|
|
|
|
&skey,
|
|
|
|
&iv,
|
|
|
|
buf))
|
|
|
|
return NULL;
|
|
|
|
ret = GNUNET_new (struct TALER_RefreshLinkDecrypted);
|
|
|
|
memcpy (&ret->coin_priv,
|
|
|
|
buf,
|
2015-03-22 22:14:30 +01:00
|
|
|
sizeof (struct TALER_CoinSpendPrivateKey));
|
|
|
|
ret->blinding_key.rsa_blinding_key
|
2015-01-27 16:31:18 +01:00
|
|
|
= GNUNET_CRYPTO_rsa_blinding_key_decode (&buf[sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)],
|
|
|
|
input->blinding_key_enc_size);
|
2015-03-22 22:14:30 +01:00
|
|
|
if (NULL == ret->blinding_key.rsa_blinding_key)
|
2015-01-27 16:31:18 +01:00
|
|
|
{
|
|
|
|
GNUNET_free (ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ret;
|
2015-01-27 16:18:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-27 16:31:18 +01:00
|
|
|
/**
|
|
|
|
* Encrypt refresh link information.
|
|
|
|
*
|
|
|
|
* @param input plaintext refresh link data
|
|
|
|
* @param secret shared secret to use for encryption
|
|
|
|
* @return NULL on error (should never happen)
|
|
|
|
*/
|
|
|
|
struct TALER_RefreshLinkEncrypted *
|
|
|
|
TALER_refresh_encrypt (const struct TALER_RefreshLinkDecrypted *input,
|
2015-01-27 22:01:08 +01:00
|
|
|
const struct TALER_LinkSecret *secret)
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-01-27 16:31:18 +01:00
|
|
|
char *b_buf;
|
|
|
|
size_t b_buf_size;
|
2015-01-27 16:18:33 +01:00
|
|
|
struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
|
|
|
|
struct GNUNET_CRYPTO_SymmetricSessionKey skey;
|
2015-01-27 16:31:18 +01:00
|
|
|
struct TALER_RefreshLinkEncrypted *ret;
|
2015-01-27 16:18:33 +01:00
|
|
|
|
|
|
|
derive_refresh_key (secret, &iv, &skey);
|
2015-03-22 22:14:30 +01:00
|
|
|
b_buf_size = GNUNET_CRYPTO_rsa_blinding_key_encode (input->blinding_key.rsa_blinding_key,
|
2015-01-27 16:31:18 +01:00
|
|
|
&b_buf);
|
|
|
|
ret = GNUNET_malloc (sizeof (struct TALER_RefreshLinkEncrypted) +
|
|
|
|
b_buf_size);
|
|
|
|
ret->blinding_key_enc = (const char *) &ret[1];
|
|
|
|
ret->blinding_key_enc_size = b_buf_size;
|
|
|
|
{
|
|
|
|
size_t buf_size = b_buf_size + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
|
|
|
|
char buf[buf_size];
|
|
|
|
|
|
|
|
memcpy (buf,
|
|
|
|
&input->coin_priv,
|
|
|
|
sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
|
|
|
|
memcpy (&buf[sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)],
|
|
|
|
b_buf,
|
|
|
|
b_buf_size);
|
|
|
|
|
|
|
|
if (GNUNET_OK !=
|
|
|
|
GNUNET_CRYPTO_symmetric_encrypt (buf,
|
|
|
|
buf_size,
|
|
|
|
&skey,
|
|
|
|
&iv,
|
|
|
|
ret->coin_priv_enc))
|
|
|
|
{
|
|
|
|
GNUNET_free (ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
2015-01-27 16:18:33 +01:00
|
|
|
}
|
|
|
|
|
2015-01-29 17:34:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode encrypted refresh link information from buffer.
|
|
|
|
*
|
|
|
|
* @param buf buffer with refresh link data
|
|
|
|
* @param buf_len number of bytes in @a buf
|
|
|
|
* @return NULL on error (@a buf_len too small)
|
|
|
|
*/
|
|
|
|
struct TALER_RefreshLinkEncrypted *
|
|
|
|
TALER_refresh_link_encrypted_decode (const char *buf,
|
|
|
|
size_t buf_len)
|
|
|
|
{
|
|
|
|
struct TALER_RefreshLinkEncrypted *rle;
|
|
|
|
|
|
|
|
if (buf_len < sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))
|
|
|
|
return NULL;
|
|
|
|
rle = GNUNET_malloc (sizeof (struct TALER_RefreshLinkEncrypted) +
|
|
|
|
buf_len - sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
|
|
|
|
rle->blinding_key_enc = (const char *) &rle[1];
|
|
|
|
rle->blinding_key_enc_size = buf_len - sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
|
|
|
|
memcpy (rle->coin_priv_enc,
|
|
|
|
buf,
|
|
|
|
buf_len);
|
|
|
|
return rle;
|
|
|
|
}
|
|
|
|
|
2015-01-27 16:18:33 +01:00
|
|
|
|
2015-01-28 20:23:19 +01:00
|
|
|
/**
|
|
|
|
* Check if a coin is valid; that is, whether the denomination key exists,
|
|
|
|
* is not expired, and the signature is correct.
|
|
|
|
*
|
|
|
|
* @param coin_public_info the coin public info to check for validity
|
|
|
|
* @return #GNUNET_YES if the coin is valid,
|
|
|
|
* #GNUNET_NO if it is invalid
|
|
|
|
* #GNUNET_SYSERROR if an internal error occured
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
TALER_test_coin_valid (const struct TALER_CoinPublicInfo *coin_public_info)
|
|
|
|
{
|
|
|
|
struct GNUNET_HashCode c_hash;
|
|
|
|
|
|
|
|
GNUNET_CRYPTO_hash (&coin_public_info->coin_pub,
|
|
|
|
sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
|
|
|
|
&c_hash);
|
|
|
|
if (GNUNET_OK !=
|
|
|
|
GNUNET_CRYPTO_rsa_verify (&c_hash,
|
2015-03-22 22:14:30 +01:00
|
|
|
coin_public_info->denom_sig.rsa_signature,
|
|
|
|
coin_public_info->denom_pub.rsa_public_key))
|
2015-01-28 20:23:19 +01:00
|
|
|
{
|
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
|
|
|
|
"coin signature is invalid\n");
|
|
|
|
return GNUNET_NO;
|
|
|
|
}
|
|
|
|
return GNUNET_YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-27 16:18:33 +01:00
|
|
|
/* end of crypto.c */
|