aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_rsa.c
blob: fdacf52123e6cfb784dbefa17db0d1f42f49bec8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* NOTE: this is obsolete logic, we should migrate to the
   GNUNET_CRYPTO_rsa-API as soon as possible */


/*
  This file is part of TALER
  (C) 2014 Christian Grothoff (and other contributing authors)

  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/>
*/

/**
 * @file util/test_rsa.c
 * @brief testcase for utility functions for RSA cryptography
 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
 */
#include "platform.h"
#include "taler_rsa.h"
#include <gnunet/gnunet_util_lib.h>

#define TEST_PURPOSE UINT32_MAX


#define EXITIF(cond)                                              \
  do {                                                            \
    if (cond) { GNUNET_break (0); goto EXITIF_exit; }             \
  } while (0)

int
main (int argc, char *argv[])
{
#define RND_BLK_SIZE 16524
  unsigned char rnd_blk[RND_BLK_SIZE];
  struct TALER_RSA_PrivateKey *priv;
  struct TALER_RSA_PrivateKeyBinaryEncoded *priv_enc;
  struct TALER_RSA_PublicKeyBinaryEncoded pubkey;
  struct TALER_RSA_BlindingKey *bkey;
  struct TALER_RSA_BlindedSignaturePurpose *bsp;
  struct TALER_RSA_Signature sig;
  struct GNUNET_HashCode hash;
  int ret;

  priv = NULL;
  bsp = NULL;
  bkey = NULL;
  ret = 1;
  GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, rnd_blk,
                              RND_BLK_SIZE);
  GNUNET_CRYPTO_hash (rnd_blk, RND_BLK_SIZE, &hash);
  priv = TALER_RSA_key_create ();
  GNUNET_assert (NULL != priv);
  EXITIF (GNUNET_OK != TALER_RSA_sign (priv,
                                       &hash, sizeof (hash),
                                       &sig));
  TALER_RSA_key_get_public (priv, &pubkey);
  EXITIF (NULL == (priv_enc = TALER_RSA_encode_key (priv)));
  TALER_RSA_key_free (priv);
  priv = NULL;
  EXITIF (NULL == (priv = TALER_RSA_decode_key ((const char *) priv_enc,
                                                ntohs (priv_enc->len))));
  GNUNET_free (priv_enc);
  priv_enc = NULL;
  EXITIF (GNUNET_OK != TALER_RSA_verify (&hash, sizeof (hash),
                                         &sig,
                                         &pubkey));
  /* corrupt our hash and see if the signature is still valid */
  GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, &hash,
                              sizeof (struct GNUNET_HashCode));
  EXITIF (GNUNET_OK == TALER_RSA_verify (&hash, sizeof (hash),
                                         &sig,
                                         &pubkey));

  /* test blind signing */
  GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, rnd_blk,
                              RND_BLK_SIZE);
  GNUNET_CRYPTO_hash (rnd_blk, RND_BLK_SIZE, &hash);
  (void) memset (&sig, 0, sizeof (struct TALER_RSA_Signature));
  EXITIF (NULL == (bkey = TALER_RSA_blinding_key_create ()));
  EXITIF (NULL == (bsp =
                   TALER_RSA_message_blind (&hash, sizeof (hash),
                                              bkey, &pubkey)));
  EXITIF (GNUNET_OK != TALER_RSA_sign (priv,
                                       bsp,
                                       sizeof (struct TALER_RSA_BlindedSignaturePurpose),
                                       &sig));
  EXITIF (GNUNET_OK != TALER_RSA_unblind (&sig,
                                          bkey,
                                          &pubkey));
  EXITIF (GNUNET_OK != TALER_RSA_verify (&hash, sizeof (hash),
                                         &sig,
                                         &pubkey));
  /* corrupt our hash and see if the signature is still valid */
  GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, &hash,
                              sizeof (struct GNUNET_HashCode));
  EXITIF (GNUNET_OK == TALER_RSA_verify (&hash, sizeof (hash),
                                         &sig,
                                         &pubkey));
  ret = 0;                      /* all OK */

 EXITIF_exit:
  if (NULL != priv)
  {
    TALER_RSA_key_free (priv);
    priv = NULL;
  }
  if (NULL != priv_enc)
  {
    GNUNET_free (priv_enc);
    priv_enc = NULL;
  }
  if (NULL != bkey)
    TALER_RSA_blinding_key_destroy (bkey);
  GNUNET_free_non_null (bsp);
  return ret;
}