rsa_basic.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* rsa_basic.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/bigint_io.h>
  21. #include <crypto/rsa_basic.h>
  22. #define DEBUG 0
  23. #if DEBUG
  24. #include "cli.h"
  25. #include "uart_lowlevel.h"
  26. #include "string-extras.h"
  27. #endif
  28. void rsa_enc(bigint_t* data, const rsa_publickey_t* key){
  29. /*
  30. cli_putstr("\r\n -->rsa_enc()\r\n m = ");
  31. bigint_print_hex(data);
  32. cli_putstr("\r\n e = ");
  33. bigint_print_hex(key->exponent);
  34. cli_putstr("\r\n n = ");
  35. bigint_print_hex(key->modulus);
  36. */
  37. bigint_expmod_u(data, data, &key->exponent, &key->modulus);
  38. }
  39. /*
  40. (p,q,dp,dq,qinv)
  41. m1 = c**dp % p
  42. m2 = c**dq % q
  43. h = (m1 - m2) * qinv % p
  44. m = m2 + q * h
  45. */
  46. uint8_t rsa_dec_crt_mono(bigint_t* data, const rsa_privatekey_t* key){
  47. bigint_t m1, m2;
  48. m1.wordv = malloc(key->components[0].length_W * sizeof(bigint_word_t));
  49. m2.wordv = malloc(key->components[1].length_W * sizeof(bigint_word_t));
  50. if(!m1.wordv || !m2.wordv){
  51. #if DEBUG
  52. cli_putstr("\r\nERROR: OOM!");
  53. #endif
  54. free(m2.wordv);
  55. free(m1.wordv);
  56. return 1;
  57. }
  58. #if DEBUG
  59. cli_putstr("\r\nDBG: expmod m1 ...");
  60. #endif
  61. bigint_expmod_u(&m1, data, &key->components[2], &key->components[0]);
  62. #if DEBUG
  63. cli_putstr("expmod m2 ...");
  64. #endif
  65. bigint_expmod_u(&m2, data, &key->components[3], &key->components[1]);
  66. bigint_sub_s(&m1, &m1, &m2);
  67. while(BIGINT_NEG_MASK & m1.info){
  68. bigint_add_s(&m1, &m1, &key->components[0]);
  69. }
  70. #if DEBUG
  71. cli_putstr("\r\nDBG: reduce-mul ...");
  72. #endif
  73. bigint_reduce(&m1, &key->components[0]);
  74. bigint_mul_u(data, &m1, &key->components[4]);
  75. bigint_reduce(data, &key->components[0]);
  76. bigint_mul_u(data, data, &key->components[1]);
  77. bigint_add_u(data, data, &m2);
  78. free(m2.wordv);
  79. free(m1.wordv);
  80. return 0;
  81. }
  82. uint8_t rsa_dec(bigint_t* data, const rsa_privatekey_t* key){
  83. if(key->n == 1){
  84. bigint_expmod_u(data, data, &key->components[0], &key->modulus);
  85. return 0;
  86. }
  87. if(key->n == 5){
  88. if (rsa_dec_crt_mono(data, key)){
  89. return 3;
  90. }
  91. return 0;
  92. }
  93. if(key->n<8 || (key->n-5)%3 != 0){
  94. return 1;
  95. }
  96. //rsa_dec_crt_multi(data, key, (key->n-5)/3);
  97. return 2;
  98. }
  99. void rsa_os2ip(bigint_t* dest, const void* data, uint32_t length_B){
  100. #if BIGINT_WORD_SIZE == 8
  101. if(data){
  102. memcpy(dest->wordv, data, length_B)
  103. }
  104. dest->length_W = length_B;
  105. #else
  106. uint8_t off;
  107. off = (sizeof(bigint_word_t) - length_B % sizeof(bigint_word_t)) % sizeof(bigint_word_t);
  108. #if DEBUG
  109. cli_putstr("\r\nDBG: off = 0x");
  110. cli_hexdump_byte(off);
  111. #endif
  112. if(!data){
  113. if(off){
  114. dest->wordv = realloc(dest->wordv, length_B + sizeof(bigint_word_t) - off);
  115. memmove((uint8_t*)dest->wordv+off, dest->wordv, length_B);
  116. memset(dest->wordv, 0, off);
  117. }
  118. }else{
  119. memcpy((uint8_t*)dest->wordv + off, data, length_B);
  120. if(off){
  121. memset(dest->wordv, 0, off);
  122. }
  123. }
  124. dest->length_W = (length_B + off) / sizeof(bigint_word_t);
  125. #if DEBUG
  126. cli_putstr("\r\nDBG: dest->length_W = 0x");
  127. cli_hexdump_rev(&(dest->length_W), 2);
  128. #endif
  129. #endif
  130. dest->info = 0;
  131. bigint_changeendianess(dest);
  132. bigint_adjust(dest);
  133. }
  134. void rsa_i2osp(void* dest, bigint_t* src, uint16_t* out_length_B){
  135. #if BIGINT_WORD_SIZE == 8
  136. if(dest){
  137. uint8_t *e = src->wordv + src->length_W;
  138. uint16_t i;
  139. for(i=src->length_W; i>0; --i){
  140. *((uint8_t*)dest) = *--e;
  141. dest = (uint8_t*)dest + 1;
  142. }
  143. }else{
  144. bigint_changeendianess(src);
  145. }
  146. *out_length_B = src->length_W;
  147. #else
  148. *out_length_B = bigint_get_first_set_bit(src) / 8 + 1;
  149. if(dest){
  150. uint16_t i;
  151. for(i=*out_length_B; i>0; --i){
  152. *((uint8_t*)dest) = ((uint8_t*)src->wordv)[i-1];
  153. dest = (uint8_t*)dest + 1;
  154. }
  155. }else{
  156. uint8_t off;
  157. bigint_changeendianess(src);
  158. bigint_adjust(src);
  159. off = bigint_get_last_set_bit(src)/8;
  160. if(off){
  161. memmove(src->wordv, (uint8_t*)src->wordv+off, *out_length_B);
  162. }
  163. }
  164. #endif
  165. }