rsa_encrypt.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * RSA simple data encryption 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. #ifdef PRINTF_STDLIB
  30. #include <stdio.h>
  31. #endif
  32. #ifdef PRINTF_CUSTOM
  33. #include "tinystdio.h"
  34. #endif
  35. #include "polarssl/config.h"
  36. #include "polarssl/rsa.h"
  37. #include "polarssl/havege.h"
  38. #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
  39. !defined(POLARSSL_HAVEGE_C) || !defined(POLARSSL_FS_IO)
  40. int main( void )
  41. {
  42. printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
  43. "POLARSSL_HAVEGE_C and/or POLARSSL_FS_IO not defined.\n");
  44. return( 0 );
  45. }
  46. #else
  47. int main( int argc, char *argv[] )
  48. {
  49. FILE *f;
  50. int ret;
  51. size_t i;
  52. rsa_context rsa;
  53. havege_state hs;
  54. unsigned char input[1024];
  55. unsigned char buf[512];
  56. havege_init( &hs );
  57. ret = 1;
  58. if( argc != 2 )
  59. {
  60. printf( "usage: rsa_encrypt <string of max 100 characters>\n" );
  61. #ifdef WIN32
  62. printf( "\n" );
  63. #endif
  64. goto exit;
  65. }
  66. printf( "\n . Reading private key from rsa_priv.txt" );
  67. fflush( stdout );
  68. if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
  69. {
  70. ret = 1;
  71. printf( " failed\n ! Could not open rsa_priv.txt\n" \
  72. " ! Please run rsa_genkey first\n\n" );
  73. goto exit;
  74. }
  75. rsa_init( &rsa, RSA_PKCS_V15, 0 );
  76. if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
  77. ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
  78. ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
  79. ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
  80. ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
  81. ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
  82. ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
  83. ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
  84. {
  85. printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
  86. goto exit;
  87. }
  88. rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
  89. fclose( f );
  90. if( strlen( argv[1] ) > 100 )
  91. {
  92. printf( " Input data larger than 100 characters.\n\n" );
  93. goto exit;
  94. }
  95. memcpy( input, argv[1], strlen( argv[1] ) );
  96. /*
  97. * Calculate the RSA encryption of the hash.
  98. */
  99. printf( "\n . Generating the RSA encrypted value" );
  100. fflush( stdout );
  101. if( ( ret = rsa_pkcs1_encrypt( &rsa, havege_rand, &hs, RSA_PRIVATE, strlen( argv[1] ), input, buf ) ) != 0 )
  102. {
  103. printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
  104. goto exit;
  105. }
  106. /*
  107. * Write the signature into result-enc.txt
  108. */
  109. if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
  110. {
  111. ret = 1;
  112. printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
  113. goto exit;
  114. }
  115. for( i = 0; i < rsa.len; i++ )
  116. fprintf( f, "%02X%s", buf[i],
  117. ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
  118. fclose( f );
  119. printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
  120. exit:
  121. #ifdef WIN32
  122. printf( " + Press Enter to exit this program.\n" );
  123. fflush( stdout ); getchar();
  124. #endif
  125. return( ret );
  126. }
  127. #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_HAVEGE_C &&
  128. POLARSSL_FS_IO */