test_crypto_rsa.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2014,2015 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/test_crypto_rsa.c
  18. * @brief testcase for utility functions for RSA cryptography
  19. * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  20. * @author Jeffrey Burdges <burdges@gnunet.org>
  21. */
  22. #include "platform.h"
  23. #include <gcrypt.h>
  24. #include "gnunet_util_lib.h"
  25. #define KEY_SIZE 1024
  26. int
  27. main (int argc,
  28. char *argv[])
  29. {
  30. #define RND_BLK_SIZE 4096
  31. unsigned char rnd_blk[RND_BLK_SIZE];
  32. struct GNUNET_CRYPTO_RsaPrivateKey *priv;
  33. struct GNUNET_CRYPTO_RsaPrivateKey *priv_copy;
  34. struct GNUNET_CRYPTO_RsaPublicKey *pub;
  35. struct GNUNET_CRYPTO_RsaPublicKey *pub_copy;
  36. struct GNUNET_CRYPTO_RsaSignature *sig;
  37. struct GNUNET_CRYPTO_RsaSignature *sig_copy;
  38. struct GNUNET_CRYPTO_RsaSignature *bsig;
  39. struct GNUNET_CRYPTO_RsaBlindingKeySecret bsec;
  40. struct GNUNET_HashCode hash;
  41. void *blind_buf;
  42. size_t bsize;
  43. GNUNET_log_setup ("test-rsa", "WARNING", NULL);
  44. GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
  45. rnd_blk,
  46. RND_BLK_SIZE);
  47. GNUNET_CRYPTO_hash (rnd_blk,
  48. RND_BLK_SIZE,
  49. &hash);
  50. priv = GNUNET_CRYPTO_rsa_private_key_create (KEY_SIZE);
  51. priv_copy = GNUNET_CRYPTO_rsa_private_key_dup (priv);
  52. GNUNET_assert (NULL != priv_copy);
  53. GNUNET_assert (0 == GNUNET_CRYPTO_rsa_private_key_cmp (priv, priv_copy));
  54. pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
  55. /* Encoding */
  56. size_t size;
  57. void *enc;
  58. enc = NULL;
  59. size = GNUNET_CRYPTO_rsa_private_key_encode (priv, &enc);
  60. /* Decoding */
  61. GNUNET_CRYPTO_rsa_private_key_free (priv);
  62. priv = NULL;
  63. priv = GNUNET_CRYPTO_rsa_private_key_decode (enc, size);
  64. GNUNET_assert (NULL != priv);
  65. GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
  66. enc, size);
  67. GNUNET_assert (NULL == GNUNET_CRYPTO_rsa_private_key_decode (enc, size));
  68. (void) fprintf (stderr, "The above warning is expected.\n");
  69. GNUNET_free (enc);
  70. /* try ordinary sig first */
  71. sig = GNUNET_CRYPTO_rsa_sign_fdh (priv,
  72. &hash);
  73. sig_copy = GNUNET_CRYPTO_rsa_signature_dup (sig);
  74. GNUNET_assert (NULL != sig);
  75. GNUNET_assert (0 == GNUNET_CRYPTO_rsa_signature_cmp (sig, sig_copy));
  76. pub_copy = GNUNET_CRYPTO_rsa_public_key_dup (pub);
  77. GNUNET_assert (NULL != pub_copy);
  78. GNUNET_assert (GNUNET_OK ==
  79. GNUNET_CRYPTO_rsa_verify (&hash, sig, pub_copy));
  80. {
  81. void *buf;
  82. size_t buf_size;
  83. struct GNUNET_CRYPTO_RsaPublicKey *pub2;
  84. struct GNUNET_CRYPTO_RsaSignature *sig2;
  85. buf_size = GNUNET_CRYPTO_rsa_public_key_encode (pub,
  86. &buf);
  87. pub2 = GNUNET_CRYPTO_rsa_public_key_decode (buf,
  88. buf_size);
  89. GNUNET_free (buf);
  90. buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
  91. &buf);
  92. sig2 = GNUNET_CRYPTO_rsa_signature_decode (buf,
  93. buf_size);
  94. GNUNET_free (buf);
  95. GNUNET_assert (GNUNET_OK ==
  96. GNUNET_CRYPTO_rsa_verify (&hash, sig2, pub2));
  97. GNUNET_CRYPTO_rsa_public_key_free (pub2);
  98. GNUNET_CRYPTO_rsa_signature_free (sig2);
  99. }
  100. /* corrupt our hash and see if the signature is still valid */
  101. GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, &hash,
  102. sizeof(struct GNUNET_HashCode));
  103. GNUNET_assert (GNUNET_OK != GNUNET_CRYPTO_rsa_verify (&hash,
  104. sig,
  105. pub));
  106. (void) fprintf (stderr, "The above warning is expected.\n");
  107. GNUNET_CRYPTO_rsa_signature_free (sig);
  108. /* test blind signing */
  109. GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
  110. &bsec,
  111. sizeof(bsec));
  112. GNUNET_CRYPTO_rsa_blind (&hash,
  113. &bsec,
  114. pub,
  115. &blind_buf, &bsize);
  116. GNUNET_assert (0 != bsize);
  117. bsig = GNUNET_CRYPTO_rsa_sign_blinded (priv,
  118. blind_buf,
  119. bsize);
  120. GNUNET_free (blind_buf);
  121. sig = GNUNET_CRYPTO_rsa_unblind (bsig,
  122. &bsec,
  123. pub);
  124. GNUNET_CRYPTO_rsa_signature_free (bsig);
  125. GNUNET_assert (GNUNET_OK ==
  126. GNUNET_CRYPTO_rsa_verify (&hash, sig, pub));
  127. GNUNET_CRYPTO_rsa_signature_free (sig);
  128. GNUNET_CRYPTO_rsa_signature_free (sig_copy);
  129. GNUNET_CRYPTO_rsa_private_key_free (priv);
  130. GNUNET_CRYPTO_rsa_private_key_free (priv_copy);
  131. GNUNET_CRYPTO_rsa_public_key_free (pub);
  132. GNUNET_CRYPTO_rsa_public_key_free (pub_copy);
  133. return 0;
  134. }