rsaes_pkcs1v15.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* rsa_pkcs1v15.c */
  2. /*
  3. This file is part of the ARM-Crypto-Lib.
  4. Copyright (C) 2006-2011 Daniel Otte (daniel.otte@rub.de)
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stdint.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <crypto/bigint.h>
  20. #include <crypto/rsa_basic.h>
  21. #define DEBUG 0
  22. #if DEBUG
  23. #include <crypto/bigint_io.h>
  24. #include <crypto/cli.h>
  25. #endif
  26. #include <crypto/random_dummy.h>
  27. uint16_t rsa_pkcs1v15_compute_padlength_B(const bigint_t* modulus, uint16_t msg_length_B){
  28. return bigint_get_first_set_bit(modulus) / 8 + 1 - msg_length_B - 3;
  29. }
  30. uint8_t rsa_encrypt_pkcs1v15(void* dest, uint16_t* out_length, const void* src,
  31. uint16_t length_B, const rsa_publickey_t* key, const void* pad){
  32. int16_t pad_length;
  33. bigint_t x;
  34. pad_length = rsa_pkcs1v15_compute_padlength_B(&key->modulus, length_B);
  35. if(pad_length<8){
  36. #if DEBUG
  37. cli_putstr("\r\nERROR: pad_length<8; pad_length: ");
  38. cli_hexdump_rev(&pad_length, 2);
  39. #endif
  40. return 2; /* message to long */
  41. }
  42. if(!pad){
  43. #if DEBUG
  44. cli_putstr("\r\nauto-generating pad ...");
  45. #endif
  46. uint16_t i;
  47. uint8_t c;
  48. for(i=0; i<pad_length; ++i){
  49. do{
  50. c = prng_get_byte();
  51. }while(c==0);
  52. ((uint8_t*)dest)[i+2] = c;
  53. }
  54. }else{
  55. #if DEBUG
  56. cli_putstr("\r\nsupplied pad: ");
  57. cli_hexdump_block(pad, pad_length, 4, 16);
  58. #endif
  59. memcpy((uint8_t*)dest + 2, pad, pad_length);
  60. }
  61. ((uint8_t*)dest)[0] = 0x00;
  62. ((uint8_t*)dest)[1] = 0x02;
  63. ((uint8_t*)dest)[2+pad_length] = 0x00;
  64. memcpy((uint8_t*)dest+3+pad_length, src, length_B);
  65. x.wordv = dest;
  66. x.length_W = (length_B+pad_length+3+sizeof(bigint_word_t)-1)/sizeof(bigint_word_t);
  67. #if DEBUG
  68. cli_putstr("\r\nx-data: ");
  69. cli_hexdump_block(x.wordv, x.length_W * sizeof(bigint_word_t), 4, 16);
  70. #endif
  71. bigint_adjust(&x);
  72. rsa_os2ip(&x, NULL, length_B+pad_length+3);
  73. rsa_enc(&x, key);
  74. rsa_i2osp(NULL, &x, out_length);
  75. return 0;
  76. }
  77. uint8_t rsa_decrypt_pkcs1v15(void* dest, uint16_t* out_length, const void* src,
  78. uint16_t length_B, const rsa_privatekey_t* key, void* pad){
  79. bigint_t x;
  80. uint16_t m_length, pad_length=0, idx=0;
  81. x.wordv = dest;
  82. rsa_os2ip(&x, src, length_B);
  83. #if DEBUG
  84. cli_putstr("\r\ncalling rsa_dec() ...");
  85. cli_putstr("\r\nencoded block (src.len = 0x");
  86. cli_hexdump_rev(&length_B, 2);
  87. cli_putstr("):");
  88. cli_hexdump_block(x.wordv, x.length_W * sizeof(bigint_word_t), 4, 16);
  89. #endif
  90. rsa_dec(&x, key);
  91. #if DEBUG
  92. cli_putstr("\r\nfinished rsa_dec() ...");
  93. #endif
  94. rsa_i2osp(NULL, &x, &m_length);
  95. #if DEBUG
  96. cli_putstr("\r\ndecoded block:");
  97. cli_hexdump_block(x.wordv, m_length, 4, 16);
  98. #endif
  99. while(((uint8_t*)x.wordv)[idx]==0 && idx<m_length){
  100. ++idx;
  101. }
  102. if(idx>=m_length){
  103. return 1;
  104. }
  105. if(((uint8_t*)x.wordv)[idx]!=2){
  106. return 3;
  107. }
  108. ++idx;
  109. while(((uint8_t*)x.wordv)[idx+pad_length]!=0 && (idx+pad_length)<m_length){
  110. ++pad_length;
  111. }
  112. if(pad_length<8 || (idx+pad_length)>=m_length){
  113. return 2;
  114. }
  115. *out_length = m_length - idx - pad_length - 1;
  116. if(pad){
  117. #if DEBUG
  118. cli_putstr("\r\npadding block:");
  119. cli_hexdump_block(((uint8_t*)x.wordv)+idx, pad_length, 4, 16);
  120. cli_putstr("\r\npad @ 0x");
  121. cli_hexdump_rev(&pad, 2);
  122. cli_putstr("\r\ndst @ 0x");
  123. cli_hexdump_rev(&dest, 2);
  124. #endif
  125. memcpy(pad, ((uint8_t*)x.wordv)+idx, pad_length);
  126. }
  127. memmove(dest, ((uint8_t*)x.wordv) + idx + pad_length + 1, m_length - idx - pad_length - 1);
  128. return 0;
  129. }