| 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 * Implementation of RSA utility functions. | 5 * Implementation of RSA utility functions. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "cryptolib.h" | 8 #include "cryptolib.h" |
| 9 #include "stateful_util.h" | 9 #include "stateful_util.h" |
| 10 #include "utility.h" | 10 #include "utility.h" |
| 11 | 11 |
| 12 int RSAProcessedKeySize(unsigned int algorithm, int* out_size) { | 12 int RSAProcessedKeySize(unsigned int algorithm, int* out_size) { |
| 13 int key_len; /* Key length in bytes. */ | 13 int key_len; /* Key length in bytes. */ |
| 14 if (algorithm < kNumAlgorithms) { | 14 if (algorithm < (unsigned int)kNumAlgorithms) { |
| 15 key_len = siglen_map[algorithm]; | 15 key_len = siglen_map[algorithm]; |
| 16 /* Total size needed by a RSAPublicKey structure is = | 16 /* Total size needed by a RSAPublicKey structure is = |
| 17 * 2 * key_len bytes for the n and rr arrays | 17 * 2 * key_len bytes for the n and rr arrays |
| 18 * + sizeof len + sizeof n0inv. | 18 * + sizeof len + sizeof n0inv. |
| 19 */ | 19 */ |
| 20 *out_size = (2 * key_len + sizeof(uint32_t) + sizeof(uint32_t)); | 20 *out_size = (2 * key_len + sizeof(uint32_t) + sizeof(uint32_t)); |
| 21 return 1; | 21 return 1; |
| 22 } | 22 } |
| 23 return 0; | 23 return 0; |
| 24 } | 24 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 const uint8_t* buf, | 78 const uint8_t* buf, |
| 79 uint64_t len, | 79 uint64_t len, |
| 80 const uint8_t* sig, | 80 const uint8_t* sig, |
| 81 unsigned int algorithm) { | 81 unsigned int algorithm) { |
| 82 RSAPublicKey* verification_key = NULL; | 82 RSAPublicKey* verification_key = NULL; |
| 83 uint8_t* digest = NULL; | 83 uint8_t* digest = NULL; |
| 84 int key_size; | 84 int key_size; |
| 85 int sig_size; | 85 int sig_size; |
| 86 int success; | 86 int success; |
| 87 | 87 |
| 88 if (algorithm >= kNumAlgorithms) | 88 if (algorithm >= (unsigned int)kNumAlgorithms) |
| 89 return 0; /* Invalid algorithm. */ | 89 return 0; /* Invalid algorithm. */ |
| 90 if (!RSAProcessedKeySize(algorithm, &key_size)) | 90 if (!RSAProcessedKeySize(algorithm, &key_size)) |
| 91 return 0; | 91 return 0; |
| 92 sig_size = siglen_map[algorithm]; | 92 sig_size = siglen_map[algorithm]; |
| 93 | 93 |
| 94 if (key_blob && !key) | 94 if (key_blob && !key) |
| 95 verification_key = RSAPublicKeyFromBuf(key_blob, key_size); | 95 verification_key = RSAPublicKeyFromBuf(key_blob, key_size); |
| 96 else if (!key_blob && key) | 96 else if (!key_blob && key) |
| 97 verification_key = (RSAPublicKey*) key; /* Supress const warning. */ | 97 verification_key = (RSAPublicKey*) key; /* Supress const warning. */ |
| 98 else | 98 else |
| (...skipping 18 matching lines...) Expand all Loading... |
| 117 int RSAVerifyBinaryWithDigest_f(const uint8_t* key_blob, | 117 int RSAVerifyBinaryWithDigest_f(const uint8_t* key_blob, |
| 118 const RSAPublicKey* key, | 118 const RSAPublicKey* key, |
| 119 const uint8_t* digest, | 119 const uint8_t* digest, |
| 120 const uint8_t* sig, | 120 const uint8_t* sig, |
| 121 unsigned int algorithm) { | 121 unsigned int algorithm) { |
| 122 RSAPublicKey* verification_key = NULL; | 122 RSAPublicKey* verification_key = NULL; |
| 123 int key_size; | 123 int key_size; |
| 124 int sig_size; | 124 int sig_size; |
| 125 int success; | 125 int success; |
| 126 | 126 |
| 127 if (algorithm >= kNumAlgorithms) | 127 if (algorithm >= (unsigned int)kNumAlgorithms) |
| 128 return 0; /* Invalid algorithm. */ | 128 return 0; /* Invalid algorithm. */ |
| 129 if (!RSAProcessedKeySize(algorithm, &key_size)) | 129 if (!RSAProcessedKeySize(algorithm, &key_size)) |
| 130 return 0; | 130 return 0; |
| 131 sig_size = siglen_map[algorithm]; | 131 sig_size = siglen_map[algorithm]; |
| 132 | 132 |
| 133 if (key_blob && !key) | 133 if (key_blob && !key) |
| 134 verification_key = RSAPublicKeyFromBuf(key_blob, key_size); | 134 verification_key = RSAPublicKeyFromBuf(key_blob, key_size); |
| 135 else if (!key_blob && key) | 135 else if (!key_blob && key) |
| 136 verification_key = (RSAPublicKey*) key; /* Supress const warning. */ | 136 verification_key = (RSAPublicKey*) key; /* Supress const warning. */ |
| 137 else | 137 else |
| 138 return 0; /* Both can't be NULL or non-NULL. */ | 138 return 0; /* Both can't be NULL or non-NULL. */ |
| 139 | 139 |
| 140 /* Ensure we have a valid key. */ | 140 /* Ensure we have a valid key. */ |
| 141 if (!verification_key) | 141 if (!verification_key) |
| 142 return 0; | 142 return 0; |
| 143 | 143 |
| 144 success = RSAVerify(verification_key, sig, (uint32_t)sig_size, | 144 success = RSAVerify(verification_key, sig, (uint32_t)sig_size, |
| 145 (uint8_t)algorithm, digest); | 145 (uint8_t)algorithm, digest); |
| 146 | 146 |
| 147 if (!key) | 147 if (!key) |
| 148 RSAPublicKeyFree(verification_key); /* Only free if we allocated it. */ | 148 RSAPublicKeyFree(verification_key); /* Only free if we allocated it. */ |
| 149 return success; | 149 return success; |
| 150 } | 150 } |
| OLD | NEW |