decrypt.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. babeld-lor
  3. Copyright (C) 2017 Rodrigo Garcia
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "decrypt.h"
  16. /**
  17. * \brief Initializes rsa module by reading a public key file from
  18. * a file. Return 0 if success, -1 otherwise.
  19. */
  20. int rsa_module_init(char public_key_file[],
  21. mbedtls_rsa_context *rsa_context,
  22. mbedtls_entropy_context *rsa_entropy,
  23. mbedtls_ctr_drbg_context *rsa_ctr_drbg)
  24. {
  25. FILE *f;
  26. int return_val;//, exit_val;
  27. const char *pers = "rsa_decrypt";
  28. memset(rsa_result, 0, sizeof( rsa_result ) );
  29. //exit_val = MBEDTLS_EXIT_SUCCESS;
  30. mbedtls_rsa_init( rsa_context, MBEDTLS_RSA_PKCS_V15, 0 );
  31. mbedtls_ctr_drbg_init( rsa_ctr_drbg );
  32. mbedtls_entropy_init( rsa_entropy );
  33. return_val = mbedtls_ctr_drbg_seed( rsa_ctr_drbg, mbedtls_entropy_func,
  34. rsa_entropy, (const unsigned char *) pers,
  35. strlen( pers ) );
  36. if( return_val != 0 )
  37. {
  38. //exit_val = MBEDTLS_EXIT_FAILURE;
  39. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
  40. return_val ); // TODO: change by perror
  41. goto exit;
  42. }
  43. if( ( f = fopen( public_key_file, "rb" ) ) == NULL )
  44. {
  45. //exit_val = MBEDTLS_EXIT_FAILURE;
  46. // TODO: change by perror
  47. mbedtls_printf( " failed\n ! Could not open rsa_pub.txt\n" );
  48. goto exit;
  49. }
  50. if( ( return_val = mbedtls_mpi_read_file( &rsa_context->N , 16, f ) ) != 0 ||
  51. ( return_val = mbedtls_mpi_read_file( &rsa_context->E , 16, f ) ) != 0 )
  52. {
  53. //exit_val = MBEDTLS_EXIT_FAILURE;
  54. mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n",
  55. return_val ); // TODO: change by perror
  56. fclose( f );
  57. goto exit;
  58. }
  59. rsa_context->len = ( mbedtls_mpi_bitlen( &rsa_context->N ) + 7 ) >> 3;
  60. fclose( f );
  61. exit:
  62. //mbedtls_ctr_drbg_free( &ctr_drbg );
  63. //mbedtls_entropy_free( &entropy );
  64. //mbedtls_rsa_free( &rsa );
  65. return return_val;
  66. #if defined(_WIN32)
  67. //mbedtls_printf( " + Press Enter to exit this program.\n" );
  68. fflush( stdout ); getchar();
  69. #endif
  70. }
  71. /**
  72. * \brief
  73. * Decrypts the given string using initialized rsa_context, rsa_entropy,
  74. * rsa_ctr_drbg structures, stores the decrypted message in rsa_result string.
  75. *
  76. * Note: context structures must have been initialized using rsa_module_init()
  77. *
  78. * \return 0 if success
  79. *
  80. * Note: The expected string to dechiper is like:
  81. * 85E986F19D9678A03C23435B7A27B455AB...
  82. * There is no blank spaces nor special characters within the string, this is
  83. * done to reduce the message being sent as a TLV.
  84. */
  85. int rsa_decrypt(mbedtls_rsa_context *rsa_context,
  86. mbedtls_entropy_context *rsa_entropy,
  87. mbedtls_ctr_drbg_context *rsa_ctr_drbg,
  88. char *encrypted_message,
  89. unsigned char *rsa_result)
  90. {
  91. size_t i;
  92. int c;
  93. i = 0;
  94. while ( sscanf(encrypted_message, "%02X", &c) > 0 &&
  95. i < (int) sizeof(rsa_buff))
  96. {
  97. //printf(" %x=%d",c, (unsigned char)c);
  98. rsa_buff[i++] = (unsigned char) c;
  99. // jumps to next integer pair
  100. encrypted_message+=2;
  101. }
  102. if (i != rsa_context->len)
  103. {
  104. // TODO: change by perror
  105. printf("\nInvalid RSA signature format\n");
  106. return 1;
  107. }
  108. /*
  109. * Decrypt the encrypted RSA data.
  110. */
  111. fflush( stdout );
  112. int return_val = mbedtls_rsa_pkcs1_decrypt( rsa_context,
  113. mbedtls_ctr_drbg_random,
  114. rsa_ctr_drbg, MBEDTLS_RSA_PUBLIC, &i,
  115. rsa_buff, rsa_result, 1024 );
  116. if( return_val != 0 )
  117. {
  118. //exit_val = MBEDTLS_EXIT_FAILURE;
  119. //TODO: change by perror
  120. printf ("\tfailed\n ! mbedtls_rsa_pkcs1_decrypt (using public key) returned %d\n\n",
  121. return_val );
  122. return 1;
  123. }
  124. //mbedtls_printf( "\n . OK\n\n" );
  125. //mbedtls_printf( "The decrypted result is: '%s'\n\n", result );
  126. return 0;
  127. }