rsa_sign_pss.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * RSASSA-PSS/SHA-1 signature creation program
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. #ifndef _CRT_SECURE_NO_DEPRECATE
  26. #define _CRT_SECURE_NO_DEPRECATE 1
  27. #endif
  28. #include <string.h>
  29. #include <stdio.h>
  30. #include "polarssl/config.h"
  31. #include "polarssl/havege.h"
  32. #include "polarssl/md.h"
  33. #include "polarssl/rsa.h"
  34. #include "polarssl/sha1.h"
  35. #include "polarssl/x509.h"
  36. #if defined _MSC_VER && !defined snprintf
  37. #define snprintf _snprintf
  38. #endif
  39. #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_HAVEGE_C) || \
  40. !defined(POLARSSL_RSA_C) || !defined(POLARSSL_SHA1_C) || \
  41. !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO)
  42. int main( void )
  43. {
  44. printf("POLARSSL_BIGNUM_C and/or POLARSSL_HAVEGE_C and/or "
  45. "POLARSSL_RSA_C and/or POLARSSL_SHA1_C and/or "
  46. "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
  47. return( 0 );
  48. }
  49. #else
  50. int main( int argc, char *argv[] )
  51. {
  52. FILE *f;
  53. int ret;
  54. rsa_context rsa;
  55. havege_state hs;
  56. unsigned char hash[20];
  57. unsigned char buf[512];
  58. char filename[512];
  59. ret = 1;
  60. if( argc != 3 )
  61. {
  62. printf( "usage: rsa_sign_pss <key_file> <filename>\n" );
  63. #ifdef WIN32
  64. printf( "\n" );
  65. #endif
  66. goto exit;
  67. }
  68. printf( "\n . Reading private key from '%s'", argv[1] );
  69. fflush( stdout );
  70. havege_init( &hs );
  71. rsa_init( &rsa, RSA_PKCS_V21, POLARSSL_MD_SHA1 );
  72. if( ( ret = x509parse_keyfile( &rsa, argv[1], "" ) ) != 0 )
  73. {
  74. ret = 1;
  75. printf( " failed\n ! Could not open '%s'\n", argv[1] );
  76. goto exit;
  77. }
  78. /*
  79. * Compute the SHA-1 hash of the input file,
  80. * then calculate the RSA signature of the hash.
  81. */
  82. printf( "\n . Generating the RSA/SHA-1 signature" );
  83. fflush( stdout );
  84. if( ( ret = sha1_file( argv[2], hash ) ) != 0 )
  85. {
  86. printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
  87. goto exit;
  88. }
  89. if( ( ret = rsa_pkcs1_sign( &rsa, havege_rand, &hs, RSA_PRIVATE, SIG_RSA_SHA1,
  90. 20, hash, buf ) ) != 0 )
  91. {
  92. printf( " failed\n ! rsa_pkcs1_sign returned %d\n\n", ret );
  93. goto exit;
  94. }
  95. /*
  96. * Write the signature into <filename>-sig.txt
  97. */
  98. snprintf( filename, 512, "%s.sig", argv[2] );
  99. if( ( f = fopen( filename, "wb+" ) ) == NULL )
  100. {
  101. ret = 1;
  102. printf( " failed\n ! Could not create %s\n\n", filename );
  103. goto exit;
  104. }
  105. if( fwrite( buf, 1, rsa.len, f ) != (size_t) rsa.len )
  106. {
  107. printf( "failed\n ! fwrite failed\n\n" );
  108. goto exit;
  109. }
  110. fclose( f );
  111. printf( "\n . Done (created \"%s\")\n\n", filename );
  112. exit:
  113. #ifdef WIN32
  114. printf( " + Press Enter to exit this program.\n" );
  115. fflush( stdout ); getchar();
  116. #endif
  117. return( ret );
  118. }
  119. #endif /* POLARSSL_BIGNUM_C && POLARSSL_HAVEGE_C && POLARSSL_RSA_C &&
  120. POLARSSL_SHA1_C && POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */