rsa_sign_pss.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * RSASSA-PSS/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_MD_C) || !defined(MBEDTLS_ENTROPY_C) || \
  22. !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
  23. !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
  24. !defined(MBEDTLS_CTR_DRBG_C)
  25. int main(void)
  26. {
  27. mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_ENTROPY_C and/or "
  28. "MBEDTLS_RSA_C and/or MBEDTLS_SHA256_C and/or "
  29. "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
  30. "MBEDTLS_CTR_DRBG_C not defined.\n");
  31. mbedtls_exit(0);
  32. }
  33. #else
  34. #include "mbedtls/entropy.h"
  35. #include "mbedtls/ctr_drbg.h"
  36. #include "mbedtls/md.h"
  37. #include "mbedtls/rsa.h"
  38. #include "mbedtls/pk.h"
  39. #include <stdio.h>
  40. #include <string.h>
  41. int main(int argc, char *argv[])
  42. {
  43. FILE *f;
  44. int ret = 1;
  45. int exit_code = MBEDTLS_EXIT_FAILURE;
  46. mbedtls_pk_context pk;
  47. mbedtls_entropy_context entropy;
  48. mbedtls_ctr_drbg_context ctr_drbg;
  49. unsigned char hash[32];
  50. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  51. char filename[512];
  52. const char *pers = "rsa_sign_pss";
  53. size_t olen = 0;
  54. mbedtls_entropy_init(&entropy);
  55. mbedtls_pk_init(&pk);
  56. mbedtls_ctr_drbg_init(&ctr_drbg);
  57. if (argc != 3) {
  58. mbedtls_printf("usage: rsa_sign_pss <key_file> <filename>\n");
  59. #if defined(_WIN32)
  60. mbedtls_printf("\n");
  61. #endif
  62. goto exit;
  63. }
  64. mbedtls_printf("\n . Seeding the random number generator...");
  65. fflush(stdout);
  66. if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
  67. (const unsigned char *) pers,
  68. strlen(pers))) != 0) {
  69. mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
  70. goto exit;
  71. }
  72. mbedtls_printf("\n . Reading private key from '%s'", argv[1]);
  73. fflush(stdout);
  74. if ((ret = mbedtls_pk_parse_keyfile(&pk, argv[1], "",
  75. mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
  76. mbedtls_printf(" failed\n ! Could not read key from '%s'\n", argv[1]);
  77. mbedtls_printf(" ! mbedtls_pk_parse_public_keyfile returned %d\n\n", ret);
  78. goto exit;
  79. }
  80. if (!mbedtls_pk_can_do(&pk, MBEDTLS_PK_RSA)) {
  81. mbedtls_printf(" failed\n ! Key is not an RSA key\n");
  82. goto exit;
  83. }
  84. if ((ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(pk),
  85. MBEDTLS_RSA_PKCS_V21,
  86. MBEDTLS_MD_SHA256)) != 0) {
  87. mbedtls_printf(" failed\n ! Padding not supported\n");
  88. goto exit;
  89. }
  90. /*
  91. * Compute the SHA-256 hash of the input file,
  92. * then calculate the RSA signature of the hash.
  93. */
  94. mbedtls_printf("\n . Generating the RSA/SHA-256 signature");
  95. fflush(stdout);
  96. if ((ret = mbedtls_md_file(
  97. mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
  98. argv[2], hash)) != 0) {
  99. mbedtls_printf(" failed\n ! Could not open or read %s\n\n", argv[2]);
  100. goto exit;
  101. }
  102. if ((ret = mbedtls_pk_sign(&pk, MBEDTLS_MD_SHA256, hash, 0,
  103. buf, sizeof(buf), &olen,
  104. mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
  105. mbedtls_printf(" failed\n ! mbedtls_pk_sign returned %d\n\n", ret);
  106. goto exit;
  107. }
  108. /*
  109. * Write the signature into <filename>.sig
  110. */
  111. mbedtls_snprintf(filename, 512, "%s.sig", argv[2]);
  112. if ((f = fopen(filename, "wb+")) == NULL) {
  113. mbedtls_printf(" failed\n ! Could not create %s\n\n", filename);
  114. goto exit;
  115. }
  116. if (fwrite(buf, 1, olen, f) != olen) {
  117. mbedtls_printf("failed\n ! fwrite failed\n\n");
  118. fclose(f);
  119. goto exit;
  120. }
  121. fclose(f);
  122. mbedtls_printf("\n . Done (created \"%s\")\n\n", filename);
  123. exit_code = MBEDTLS_EXIT_SUCCESS;
  124. exit:
  125. mbedtls_pk_free(&pk);
  126. mbedtls_ctr_drbg_free(&ctr_drbg);
  127. mbedtls_entropy_free(&entropy);
  128. mbedtls_exit(exit_code);
  129. }
  130. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
  131. MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
  132. MBEDTLS_CTR_DRBG_C */