| 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 "cryptolib.h" | 11 #include <stdio.h> |
| 12 |
| 13 #include "padding.h" |
| 14 #include "rsa.h" |
| 12 #include "utility.h" | 15 #include "utility.h" |
| 13 | 16 |
| 14 /* a[] -= mod */ | 17 /* a[] -= mod */ |
| 15 static void subM(const RSAPublicKey *key, uint32_t *a) { | 18 static void subM(const RSAPublicKey *key, uint32_t *a) { |
| 16 int64_t A = 0; | 19 int64_t A = 0; |
| 17 int i; | 20 int i; |
| 18 for (i = 0; i < key->len; ++i) { | 21 for (i = 0; i < key->len; ++i) { |
| 19 A += (uint64_t)a[i] - key->n[i]; | 22 A += (uint64_t)a[i] - key->n[i]; |
| 20 a[i] = (uint32_t)A; | 23 a[i] = (uint32_t)A; |
| 21 A >>= 32; | 24 A >>= 32; |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 const uint8_t *sig, | 131 const uint8_t *sig, |
| 129 const int sig_len, | 132 const int sig_len, |
| 130 const uint8_t sig_type, | 133 const uint8_t sig_type, |
| 131 const uint8_t *hash) { | 134 const uint8_t *hash) { |
| 132 int i; | 135 int i; |
| 133 uint8_t* buf; | 136 uint8_t* buf; |
| 134 const uint8_t* padding; | 137 const uint8_t* padding; |
| 135 int success = 1; | 138 int success = 1; |
| 136 | 139 |
| 137 if (sig_len != (key->len * sizeof(uint32_t))) { | 140 if (sig_len != (key->len * sizeof(uint32_t))) { |
| 138 debug("Signature is of incorrect length!\n"); | 141 fprintf(stderr, "Signature is of incorrect length!\n"); |
| 139 return 0; | 142 return 0; |
| 140 } | 143 } |
| 141 | 144 |
| 142 if (sig_type >= kNumAlgorithms) { | 145 if (sig_type >= kNumAlgorithms) { |
| 143 debug("Invalid signature type!\n"); | 146 fprintf(stderr, "Invalid signature type!\n"); |
| 144 return 0; | 147 return 0; |
| 145 } | 148 } |
| 146 | 149 |
| 147 if (key->len != siglen_map[sig_type] / sizeof(uint32_t)) { | 150 if (key->len != siglen_map[sig_type] / sizeof(uint32_t)) { |
| 148 debug("Wrong key passed in!\n"); | 151 fprintf(stderr, "Wrong key passed in!\n"); |
| 149 return 0; | 152 return 0; |
| 150 } | 153 } |
| 151 | 154 |
| 152 buf = (uint8_t*) Malloc(sig_len); | 155 buf = (uint8_t*) Malloc(sig_len); |
| 153 Memcpy(buf, sig, sig_len); | 156 Memcpy(buf, sig, sig_len); |
| 154 | 157 |
| 155 modpowF4(key, buf); | 158 modpowF4(key, buf); |
| 156 | 159 |
| 157 /* Determine padding to use depending on the signature type. */ | 160 /* Determine padding to use depending on the signature type. */ |
| 158 padding = padding_map[sig_type]; | 161 padding = padding_map[sig_type]; |
| 159 | 162 |
| 160 /* Check pkcs1.5 padding bytes. */ | 163 /* Check pkcs1.5 padding bytes. */ |
| 161 for (i = 0; i < padding_size_map[sig_type]; ++i) { | 164 for (i = 0; i < padding_size_map[sig_type]; ++i) { |
| 162 if (buf[i] != padding[i]) { | 165 if (buf[i] != padding[i]) { |
| 163 #ifndef NDEBUG | 166 #ifndef NDEBUG |
| 164 /* TODO(gauravsh): Replace with a macro call for logging. */ | 167 /* TODO(gauravsh): Replace with a macro call for logging. */ |
| 165 debug("Padding: Expecting = %02x Got = %02x\n", padding[i], | 168 fprintf(stderr, "Padding: Expecting = %02x Got = %02x\n", padding[i], |
| 166 buf[i]); | 169 buf[i]); |
| 167 #endif | 170 #endif |
| 168 success = 0; | 171 success = 0; |
| 169 } | 172 } |
| 170 } | 173 } |
| 171 | 174 |
| 172 /* Check if digest matches. */ | 175 /* Check if digest matches. */ |
| 173 for (; i < sig_len; ++i) { | 176 for (; i < sig_len; ++i) { |
| 174 if (buf[i] != *hash++) { | 177 if (buf[i] != *hash++) { |
| 175 #ifndef NDEBUG | 178 #ifndef NDEBUG |
| 176 /* TODO(gauravsh): Replace with a macro call for logging. */ | 179 /* TODO(gauravsh): Replace with a macro call for logging. */ |
| 177 debug("Digest: Expecting = %02x Got = %02x\n", padding[i], | 180 fprintf(stderr, "Digest: Expecting = %02x Got = %02x\n", padding[i], |
| 178 buf[i]); | 181 buf[i]); |
| 179 #endif | 182 #endif |
| 180 success = 0; | 183 success = 0; |
| 181 } | 184 } |
| 182 } | 185 } |
| 183 | 186 |
| 184 Free(buf); | 187 Free(buf); |
| 185 | 188 |
| 186 return success; | 189 return success; |
| 187 } | 190 } |
| OLD | NEW |