rsa_sign.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * RSA/SHA-256 signature creation program
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "mbedtls/build_info.h"
  20. #include "mbedtls/platform.h"
  21. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
  22. !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
  23. !defined(MBEDTLS_FS_IO)
  24. int main(void)
  25. {
  26. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
  27. "MBEDTLS_MD_C and/or "
  28. "MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO not defined.\n");
  29. mbedtls_exit(0);
  30. }
  31. #else
  32. #include "mbedtls/rsa.h"
  33. #include "mbedtls/md.h"
  34. #include <stdio.h>
  35. #include <string.h>
  36. int main(int argc, char *argv[])
  37. {
  38. FILE *f;
  39. int ret = 1;
  40. int exit_code = MBEDTLS_EXIT_FAILURE;
  41. size_t i;
  42. mbedtls_rsa_context rsa;
  43. unsigned char hash[32];
  44. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  45. char filename[512];
  46. mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
  47. mbedtls_rsa_init(&rsa);
  48. mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
  49. mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
  50. mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
  51. if (argc != 2) {
  52. mbedtls_printf("usage: rsa_sign <filename>\n");
  53. #if defined(_WIN32)
  54. mbedtls_printf("\n");
  55. #endif
  56. goto exit;
  57. }
  58. mbedtls_printf("\n . Reading private key from rsa_priv.txt");
  59. fflush(stdout);
  60. if ((f = fopen("rsa_priv.txt", "rb")) == NULL) {
  61. mbedtls_printf(" failed\n ! Could not open rsa_priv.txt\n" \
  62. " ! Please run rsa_genkey first\n\n");
  63. goto exit;
  64. }
  65. if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 ||
  66. (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0 ||
  67. (ret = mbedtls_mpi_read_file(&D, 16, f)) != 0 ||
  68. (ret = mbedtls_mpi_read_file(&P, 16, f)) != 0 ||
  69. (ret = mbedtls_mpi_read_file(&Q, 16, f)) != 0 ||
  70. (ret = mbedtls_mpi_read_file(&DP, 16, f)) != 0 ||
  71. (ret = mbedtls_mpi_read_file(&DQ, 16, f)) != 0 ||
  72. (ret = mbedtls_mpi_read_file(&QP, 16, f)) != 0) {
  73. mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret);
  74. fclose(f);
  75. goto exit;
  76. }
  77. fclose(f);
  78. if ((ret = mbedtls_rsa_import(&rsa, &N, &P, &Q, &D, &E)) != 0) {
  79. mbedtls_printf(" failed\n ! mbedtls_rsa_import returned %d\n\n",
  80. ret);
  81. goto exit;
  82. }
  83. if ((ret = mbedtls_rsa_complete(&rsa)) != 0) {
  84. mbedtls_printf(" failed\n ! mbedtls_rsa_complete returned %d\n\n",
  85. ret);
  86. goto exit;
  87. }
  88. mbedtls_printf("\n . Checking the private key");
  89. fflush(stdout);
  90. if ((ret = mbedtls_rsa_check_privkey(&rsa)) != 0) {
  91. mbedtls_printf(" failed\n ! mbedtls_rsa_check_privkey failed with -0x%0x\n",
  92. (unsigned int) -ret);
  93. goto exit;
  94. }
  95. /*
  96. * Compute the SHA-256 hash of the input file,
  97. * then calculate the RSA signature of the hash.
  98. */
  99. mbedtls_printf("\n . Generating the RSA/SHA-256 signature");
  100. fflush(stdout);
  101. if ((ret = mbedtls_md_file(
  102. mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
  103. argv[1], hash)) != 0) {
  104. mbedtls_printf(" failed\n ! Could not open or read %s\n\n", argv[1]);
  105. goto exit;
  106. }
  107. if ((ret = mbedtls_rsa_pkcs1_sign(&rsa, NULL, NULL, MBEDTLS_MD_SHA256,
  108. 32, hash, buf)) != 0) {
  109. mbedtls_printf(" failed\n ! mbedtls_rsa_pkcs1_sign returned -0x%0x\n\n",
  110. (unsigned int) -ret);
  111. goto exit;
  112. }
  113. /*
  114. * Write the signature into <filename>.sig
  115. */
  116. mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[1]);
  117. if ((f = fopen(filename, "wb+")) == NULL) {
  118. mbedtls_printf(" failed\n ! Could not create %s\n\n", argv[1]);
  119. goto exit;
  120. }
  121. for (i = 0; i < rsa.MBEDTLS_PRIVATE(len); i++) {
  122. mbedtls_fprintf(f, "%02X%s", buf[i],
  123. (i + 1) % 16 == 0 ? "\r\n" : " ");
  124. }
  125. fclose(f);
  126. mbedtls_printf("\n . Done (created \"%s\")\n\n", filename);
  127. exit_code = MBEDTLS_EXIT_SUCCESS;
  128. exit:
  129. mbedtls_rsa_free(&rsa);
  130. mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
  131. mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
  132. mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
  133. mbedtls_exit(exit_code);
  134. }
  135. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
  136. MBEDTLS_FS_IO */