rsa_e_f4.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* rsa_e_f4.c
  2. **
  3. ** Copyright 2012, The Android Open Source Project
  4. **
  5. ** Redistribution and use in source and binary forms, with or without
  6. ** modification, are permitted provided that the following conditions are met:
  7. ** * Redistributions of source code must retain the above copyright
  8. ** notice, this list of conditions and the following disclaimer.
  9. ** * Redistributions in binary form must reproduce the above copyright
  10. ** notice, this list of conditions and the following disclaimer in the
  11. ** documentation and/or other materials provided with the distribution.
  12. ** * Neither the name of Google Inc. nor the names of its contributors may
  13. ** be used to endorse or promote products derived from this software
  14. ** without specific prior written permission.
  15. **
  16. ** THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
  17. ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. ** EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "mincrypt/rsa.h"
  28. #include "mincrypt/sha.h"
  29. // a[] -= mod
  30. static void subM(const RSAPublicKey* key,
  31. uint32_t* a) {
  32. int64_t A = 0;
  33. int i;
  34. for (i = 0; i < key->len; ++i) {
  35. A += (uint64_t)a[i] - key->n[i];
  36. a[i] = (uint32_t)A;
  37. A >>= 32;
  38. }
  39. }
  40. // return a[] >= mod
  41. static int geM(const RSAPublicKey* key,
  42. const uint32_t* a) {
  43. int i;
  44. for (i = key->len; i;) {
  45. --i;
  46. if (a[i] < key->n[i]) return 0;
  47. if (a[i] > key->n[i]) return 1;
  48. }
  49. return 1; // equal
  50. }
  51. // montgomery c[] += a * b[] / R % mod
  52. static void montMulAdd(const RSAPublicKey* key,
  53. uint32_t* c,
  54. const uint32_t a,
  55. const uint32_t* b) {
  56. uint64_t A = (uint64_t)a * b[0] + c[0];
  57. uint32_t d0 = (uint32_t)A * key->n0inv;
  58. uint64_t B = (uint64_t)d0 * key->n[0] + (uint32_t)A;
  59. int i;
  60. for (i = 1; i < key->len; ++i) {
  61. A = (A >> 32) + (uint64_t)a * b[i] + c[i];
  62. B = (B >> 32) + (uint64_t)d0 * key->n[i] + (uint32_t)A;
  63. c[i - 1] = (uint32_t)B;
  64. }
  65. A = (A >> 32) + (B >> 32);
  66. c[i - 1] = (uint32_t)A;
  67. if (A >> 32) {
  68. subM(key, c);
  69. }
  70. }
  71. // montgomery c[] = a[] * b[] / R % mod
  72. static void montMul(const RSAPublicKey* key,
  73. uint32_t* c,
  74. const uint32_t* a,
  75. const uint32_t* b) {
  76. int i;
  77. for (i = 0; i < key->len; ++i) {
  78. c[i] = 0;
  79. }
  80. for (i = 0; i < key->len; ++i) {
  81. montMulAdd(key, c, a[i], b);
  82. }
  83. }
  84. // In-place public exponentiation.
  85. // Input and output big-endian byte array in inout.
  86. static void modpowF4(const RSAPublicKey* key,
  87. uint8_t* inout) {
  88. uint32_t a[RSANUMWORDS];
  89. uint32_t aR[RSANUMWORDS];
  90. uint32_t aaR[RSANUMWORDS];
  91. uint32_t* aaa = aaR; // Re-use location.
  92. int i;
  93. // Convert from big endian byte array to little endian word array.
  94. for (i = 0; i < key->len; ++i) {
  95. uint32_t tmp =
  96. (inout[((key->len - 1 - i) * 4) + 0] << 24) |
  97. (inout[((key->len - 1 - i) * 4) + 1] << 16) |
  98. (inout[((key->len - 1 - i) * 4) + 2] << 8) |
  99. (inout[((key->len - 1 - i) * 4) + 3] << 0);
  100. a[i] = tmp;
  101. }
  102. montMul(key, aR, a, key->rr); // aR = a * RR / R mod M
  103. for (i = 0; i < 16; i += 2) {
  104. montMul(key, aaR, aR, aR); // aaR = aR * aR / R mod M
  105. montMul(key, aR, aaR, aaR); // aR = aaR * aaR / R mod M
  106. }
  107. montMul(key, aaa, aR, a); // aaa = aR * a / R mod M
  108. // Make sure aaa < mod; aaa is at most 1x mod too large.
  109. if (geM(key, aaa)) {
  110. subM(key, aaa);
  111. }
  112. // Convert to bigendian byte array
  113. for (i = key->len - 1; i >= 0; --i) {
  114. uint32_t tmp = aaa[i];
  115. *inout++ = tmp >> 24;
  116. *inout++ = tmp >> 16;
  117. *inout++ = tmp >> 8;
  118. *inout++ = tmp >> 0;
  119. }
  120. }
  121. // Expected PKCS1.5 signature padding bytes, for a keytool RSA signature.
  122. // Has the 0-length optional parameter encoded in the ASN1 (as opposed to the
  123. // other flavor which omits the optional parameter entirely). This code does not
  124. // accept signatures without the optional parameter.
  125. /*
  126. static const uint8_t padding[RSANUMBYTES] = {
  127. 0x00,0x01,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x30,0x21,0x30,0x09,0x06,0x05,0x2b,0x0e,0x03,0x02,0x1a,0x05,0x00,0x04,0x14,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  128. };
  129. */
  130. // SHA-1 of PKCS1.5 signature padding for 2048 bit, as above.
  131. // At the location of the bytes of the hash all 00 are hashed.
  132. static const uint8_t kExpectedPadShaRsa2048[SHA_DIGEST_SIZE] = {
  133. 0xdc, 0xbd, 0xbe, 0x42, 0xd5, 0xf5, 0xa7, 0x2e, 0x6e, 0xfc,
  134. 0xf5, 0x5d, 0xaf, 0x9d, 0xea, 0x68, 0x7c, 0xfb, 0xf1, 0x67
  135. };
  136. // Verify a 2048 bit RSA e=65537 PKCS1.5 signature against an expected
  137. // SHA-1 hash. Returns 0 on failure, 1 on success.
  138. int RSA_e_f4_verify(const RSAPublicKey* key,
  139. const uint8_t* signature,
  140. const int len,
  141. const uint8_t* sha) {
  142. uint8_t buf[RSANUMBYTES];
  143. int i;
  144. if (key->len != RSANUMWORDS) {
  145. return 0; // Wrong key passed in.
  146. }
  147. if (len != sizeof(buf)) {
  148. return 0; // Wrong input length.
  149. }
  150. if (key->exponent != 65537) {
  151. return 0; // Wrong exponent.
  152. }
  153. for (i = 0; i < len; ++i) { // Copy input to local workspace.
  154. buf[i] = signature[i];
  155. }
  156. modpowF4(key, buf); // In-place exponentiation.
  157. // Xor sha portion, so it all becomes 00 iff equal.
  158. for (i = len - SHA_DIGEST_SIZE; i < len; ++i) {
  159. buf[i] ^= *sha++;
  160. }
  161. // Hash resulting buf, in-place.
  162. SHA(buf, len, buf);
  163. // Compare against expected hash value.
  164. for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
  165. if (buf[i] != kExpectedPadShaRsa2048[i]) {
  166. return 0;
  167. }
  168. }
  169. return 1; // All checked out OK.
  170. }