Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame^] | 1 | /* rsa.c |
| 2 | ** |
| 3 | ** Copyright 2008, 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 | |
| 28 | #include "twrsa.h" |
| 29 | #include "twsha.h" |
| 30 | #include "common.h" |
| 31 | |
| 32 | /* a[] -= mod */ |
| 33 | static void subM(const RSAPublicKey *key, uint32_t *a) { |
| 34 | int64_t A = 0; |
| 35 | int i; |
| 36 | for (i = 0; i < key->len; ++i) { |
| 37 | A += (uint64_t)a[i] - key->n[i]; |
| 38 | a[i] = (uint32_t)A; |
| 39 | A >>= 32; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /* return a[] >= mod */ |
| 44 | static int geM(const RSAPublicKey *key, const uint32_t *a) { |
| 45 | int i; |
| 46 | for (i = key->len; i;) { |
| 47 | --i; |
| 48 | if (a[i] < key->n[i]) return 0; |
| 49 | if (a[i] > key->n[i]) return 1; |
| 50 | } |
| 51 | return 1; /* equal */ |
| 52 | } |
| 53 | |
| 54 | /* montgomery c[] += a * b[] / R % mod */ |
| 55 | static void montMulAdd(const RSAPublicKey *key, |
| 56 | uint32_t* c, |
| 57 | const uint32_t a, |
| 58 | const uint32_t* b) { |
| 59 | uint64_t A = (uint64_t)a * b[0] + c[0]; |
| 60 | uint32_t d0 = (uint32_t)A * key->n0inv; |
| 61 | uint64_t B = (uint64_t)d0 * key->n[0] + (uint32_t)A; |
| 62 | int i; |
| 63 | |
| 64 | for (i = 1; i < key->len; ++i) { |
| 65 | A = (A >> 32) + (uint64_t)a * b[i] + c[i]; |
| 66 | B = (B >> 32) + (uint64_t)d0 * key->n[i] + (uint32_t)A; |
| 67 | c[i - 1] = (uint32_t)B; |
| 68 | } |
| 69 | |
| 70 | A = (A >> 32) + (B >> 32); |
| 71 | |
| 72 | c[i - 1] = (uint32_t)A; |
| 73 | |
| 74 | if (A >> 32) { |
| 75 | subM(key, c); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /* montgomery c[] = a[] * b[] / R % mod */ |
| 80 | static void montMul(const RSAPublicKey *key, |
| 81 | uint32_t* c, |
| 82 | const uint32_t* a, |
| 83 | const uint32_t* b) { |
| 84 | int i; |
| 85 | for (i = 0; i < key->len; ++i) { |
| 86 | c[i] = 0; |
| 87 | } |
| 88 | for (i = 0; i < key->len; ++i) { |
| 89 | montMulAdd(key, c, a[i], b); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /* In-place public exponentiation. |
| 94 | ** Input and output big-endian byte array in inout. |
| 95 | */ |
| 96 | static void modpow3(const RSAPublicKey *key, |
| 97 | uint8_t* inout) { |
| 98 | uint32_t a[RSANUMWORDS]; |
| 99 | uint32_t aR[RSANUMWORDS]; |
| 100 | uint32_t aaR[RSANUMWORDS]; |
| 101 | uint32_t *aaa = aR; /* Re-use location. */ |
| 102 | int i; |
| 103 | |
| 104 | /* Convert from big endian byte array to little endian word array. */ |
| 105 | for (i = 0; i < key->len; ++i) { |
| 106 | uint32_t tmp = |
| 107 | (inout[((key->len - 1 - i) * 4) + 0] << 24) | |
| 108 | (inout[((key->len - 1 - i) * 4) + 1] << 16) | |
| 109 | (inout[((key->len - 1 - i) * 4) + 2] << 8) | |
| 110 | (inout[((key->len - 1 - i) * 4) + 3] << 0); |
| 111 | a[i] = tmp; |
| 112 | } |
| 113 | |
| 114 | montMul(key, aR, a, key->rr); /* aR = a * RR / R mod M */ |
| 115 | montMul(key, aaR, aR, aR); /* aaR = aR * aR / R mod M */ |
| 116 | montMul(key, aaa, aaR, a); /* aaa = aaR * a / R mod M */ |
| 117 | |
| 118 | /* Make sure aaa < mod; aaa is at most 1x mod too large. */ |
| 119 | if (geM(key, aaa)) { |
| 120 | subM(key, aaa); |
| 121 | } |
| 122 | |
| 123 | /* Convert to bigendian byte array */ |
| 124 | for (i = key->len - 1; i >= 0; --i) { |
| 125 | uint32_t tmp = aaa[i]; |
| 126 | *inout++ = tmp >> 24; |
| 127 | *inout++ = tmp >> 16; |
| 128 | *inout++ = tmp >> 8; |
| 129 | *inout++ = tmp >> 0; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /* Expected PKCS1.5 signature padding bytes, for a keytool RSA signature. |
| 134 | ** Has the 0-length optional parameter encoded in the ASN1 (as opposed to the |
| 135 | ** other flavor which omits the optional parameter entirely). This code does not |
| 136 | ** accept signatures without the optional parameter. |
| 137 | */ |
| 138 | static const uint8_t padding[RSANUMBYTES - SHA_DIGEST_SIZE] = { |
| 139 | 0x00,0x01,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, |
| 140 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, |
| 141 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, |
| 142 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, |
| 143 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, |
| 144 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, |
| 145 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, |
| 146 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, |
| 147 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, |
| 148 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, |
| 149 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, |
| 150 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, |
| 151 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, |
| 152 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, |
| 153 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, |
| 154 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, |
| 155 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00, |
| 156 | 0x30,0x21,0x30,0x09,0x06,0x05,0x2b,0x0e,0x03,0x02,0x1a,0x05,0x00, |
| 157 | 0x04,0x14 |
| 158 | }; |
| 159 | |
| 160 | /* Verify a 2048 bit RSA PKCS1.5 signature against an expected SHA-1 hash. |
| 161 | ** Returns 0 on failure, 1 on success. |
| 162 | */ |
| 163 | int RSA_verify(const RSAPublicKey *key, |
| 164 | const uint8_t *signature, |
| 165 | const int len, |
| 166 | const uint8_t *sha) { |
| 167 | uint8_t buf[RSANUMBYTES]; |
| 168 | int i; |
| 169 | |
| 170 | if (key->len != RSANUMWORDS) { |
| 171 | return 0; /* Wrong key passed in. */ |
| 172 | } |
| 173 | |
| 174 | if (len != sizeof(buf)) { |
| 175 | return 0; /* Wrong input length. */ |
| 176 | } |
| 177 | |
| 178 | for (i = 0; i < len; ++i) { |
| 179 | buf[i] = signature[i]; |
| 180 | } |
| 181 | |
| 182 | modpow3(key, buf); |
| 183 | |
| 184 | /* Check pkcs1.5 padding bytes. */ |
| 185 | for (i = 0; i < (int) sizeof(padding); ++i) { |
| 186 | if (buf[i] != padding[i]) { |
| 187 | return 0; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /* Check sha digest matches. */ |
| 192 | for (; i < len; ++i) { |
| 193 | if (buf[i] != *sha++) { |
| 194 | return 0; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | return 1; |
| 199 | } |