| OLD | NEW |
| 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
| 4 */ | 4 */ |
| 5 | 5 |
| 6 /* Implementation of RSA signature verification which uses a pre-processed | 6 /* Implementation of RSA signature verification which uses a pre-processed |
| 7 * key for computation. The code extends Android's RSA verification code to | 7 * key for computation. The code extends Android's RSA verification code to |
| 8 * support multiple RSA key lengths and hash digest algorithms. | 8 * support multiple RSA key lengths and hash digest algorithms. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <stdio.h> | 11 #include "cryptolib.h" |
| 12 | |
| 13 #include "padding.h" | |
| 14 #include "rsa.h" | |
| 15 #include "utility.h" | 12 #include "utility.h" |
| 16 | 13 |
| 17 /* a[] -= mod */ | 14 /* a[] -= mod */ |
| 18 static void subM(const RSAPublicKey *key, uint32_t *a) { | 15 static void subM(const RSAPublicKey *key, uint32_t *a) { |
| 19 int64_t A = 0; | 16 int64_t A = 0; |
| 20 int i; | 17 int i; |
| 21 for (i = 0; i < key->len; ++i) { | 18 for (i = 0; i < key->len; ++i) { |
| 22 A += (uint64_t)a[i] - key->n[i]; | 19 A += (uint64_t)a[i] - key->n[i]; |
| 23 a[i] = (uint32_t)A; | 20 a[i] = (uint32_t)A; |
| 24 A >>= 32; | 21 A >>= 32; |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 const uint8_t *sig, | 128 const uint8_t *sig, |
| 132 const int sig_len, | 129 const int sig_len, |
| 133 const uint8_t sig_type, | 130 const uint8_t sig_type, |
| 134 const uint8_t *hash) { | 131 const uint8_t *hash) { |
| 135 int i; | 132 int i; |
| 136 uint8_t* buf; | 133 uint8_t* buf; |
| 137 const uint8_t* padding; | 134 const uint8_t* padding; |
| 138 int success = 1; | 135 int success = 1; |
| 139 | 136 |
| 140 if (sig_len != (key->len * sizeof(uint32_t))) { | 137 if (sig_len != (key->len * sizeof(uint32_t))) { |
| 141 fprintf(stderr, "Signature is of incorrect length!\n"); | 138 debug("Signature is of incorrect length!\n"); |
| 142 return 0; | 139 return 0; |
| 143 } | 140 } |
| 144 | 141 |
| 145 if (sig_type >= kNumAlgorithms) { | 142 if (sig_type >= kNumAlgorithms) { |
| 146 fprintf(stderr, "Invalid signature type!\n"); | 143 debug("Invalid signature type!\n"); |
| 147 return 0; | 144 return 0; |
| 148 } | 145 } |
| 149 | 146 |
| 150 if (key->len != siglen_map[sig_type] / sizeof(uint32_t)) { | 147 if (key->len != siglen_map[sig_type] / sizeof(uint32_t)) { |
| 151 fprintf(stderr, "Wrong key passed in!\n"); | 148 debug("Wrong key passed in!\n"); |
| 152 return 0; | 149 return 0; |
| 153 } | 150 } |
| 154 | 151 |
| 155 buf = (uint8_t*) Malloc(sig_len); | 152 buf = (uint8_t*) Malloc(sig_len); |
| 156 Memcpy(buf, sig, sig_len); | 153 Memcpy(buf, sig, sig_len); |
| 157 | 154 |
| 158 modpowF4(key, buf); | 155 modpowF4(key, buf); |
| 159 | 156 |
| 160 /* Determine padding to use depending on the signature type. */ | 157 /* Determine padding to use depending on the signature type. */ |
| 161 padding = padding_map[sig_type]; | 158 padding = padding_map[sig_type]; |
| 162 | 159 |
| 163 /* Check pkcs1.5 padding bytes. */ | 160 /* Check pkcs1.5 padding bytes. */ |
| 164 for (i = 0; i < padding_size_map[sig_type]; ++i) { | 161 for (i = 0; i < padding_size_map[sig_type]; ++i) { |
| 165 if (buf[i] != padding[i]) { | 162 if (buf[i] != padding[i]) { |
| 166 #ifndef NDEBUG | 163 #ifndef NDEBUG |
| 167 /* TODO(gauravsh): Replace with a macro call for logging. */ | 164 /* TODO(gauravsh): Replace with a macro call for logging. */ |
| 168 fprintf(stderr, "Padding: Expecting = %02x Got = %02x\n", padding[i], | 165 debug("Padding: Expecting = %02x Got = %02x\n", padding[i], |
| 169 buf[i]); | 166 buf[i]); |
| 170 #endif | 167 #endif |
| 171 success = 0; | 168 success = 0; |
| 172 } | 169 } |
| 173 } | 170 } |
| 174 | 171 |
| 175 /* Check if digest matches. */ | 172 /* Check if digest matches. */ |
| 176 for (; i < sig_len; ++i) { | 173 for (; i < sig_len; ++i) { |
| 177 if (buf[i] != *hash++) { | 174 if (buf[i] != *hash++) { |
| 178 #ifndef NDEBUG | 175 #ifndef NDEBUG |
| 179 /* TODO(gauravsh): Replace with a macro call for logging. */ | 176 /* TODO(gauravsh): Replace with a macro call for logging. */ |
| 180 fprintf(stderr, "Digest: Expecting = %02x Got = %02x\n", padding[i], | 177 debug("Digest: Expecting = %02x Got = %02x\n", padding[i], |
| 181 buf[i]); | 178 buf[i]); |
| 182 #endif | 179 #endif |
| 183 success = 0; | 180 success = 0; |
| 184 } | 181 } |
| 185 } | 182 } |
| 186 | 183 |
| 187 Free(buf); | 184 Free(buf); |
| 188 | 185 |
| 189 return success; | 186 return success; |
| 190 } | 187 } |
| OLD | NEW |