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 * Utility functions for message digest functions. | 5 * Utility functions for message digest functions. |
6 */ | 6 */ |
7 | 7 |
8 #include "padding.h" | 8 #include "padding.h" |
9 #include "rsa_utility.h" | 9 #include "rsa_utility.h" |
| 10 #include "sha_utility.h" |
10 #include "utility.h" | 11 #include "utility.h" |
11 | 12 |
12 int RSAProcessedKeySize(int algorithm) { | 13 int RSAProcessedKeySize(int algorithm) { |
13 int key_len = siglen_map[algorithm] * sizeof(uint32_t); /* Key length in | 14 int key_len = siglen_map[algorithm] * sizeof(uint32_t); /* Key length in |
14 * bytes. */ | 15 * bytes. */ |
15 /* Total size needed by a RSAPublicKey structure is = | 16 /* Total size needed by a RSAPublicKey structure is = |
16 * 2 * key_len bytes for the n and rr arrays | 17 * 2 * key_len bytes for the n and rr arrays |
17 * + sizeof len + sizeof n0inv. | 18 * + sizeof len + sizeof n0inv. |
18 */ | 19 */ |
19 return (2 * key_len + sizeof(int) + sizeof(uint32_t)); | 20 return (2 * key_len + sizeof(int) + sizeof(uint32_t)); |
20 } | 21 } |
21 | 22 |
22 RSAPublicKey* RSAPublicKeyFromBuf(uint8_t* buf, int len) { | 23 void RSAPublicKeyFree(RSAPublicKey* key) { |
| 24 if (key) { |
| 25 Free(key->n); |
| 26 Free(key->rr); |
| 27 Free(key); |
| 28 } |
| 29 } |
| 30 |
| 31 RSAPublicKey* RSAPublicKeyFromBuf(const uint8_t* buf, int len) { |
23 RSAPublicKey* key = (RSAPublicKey*) Malloc(sizeof(RSAPublicKey)); | 32 RSAPublicKey* key = (RSAPublicKey*) Malloc(sizeof(RSAPublicKey)); |
24 MemcpyState st; | 33 MemcpyState st; |
25 int key_len; | 34 int key_len; |
26 | 35 |
27 st.remaining_buf = buf; | 36 st.remaining_buf = (uint8_t*) buf; |
28 st.remaining_len = len; | 37 st.remaining_len = len; |
29 | 38 |
30 StatefulMemcpy(&st, &key->len, sizeof(key->len)); | 39 StatefulMemcpy(&st, &key->len, sizeof(key->len)); |
31 key_len = key->len * sizeof(uint32_t); /* key length in bytes. */ | 40 key_len = key->len * sizeof(uint32_t); /* key length in bytes. */ |
32 key->n = (uint32_t*) Malloc(key_len); | 41 key->n = (uint32_t*) Malloc(key_len); |
33 key->rr = (uint32_t*) Malloc(key_len); | 42 key->rr = (uint32_t*) Malloc(key_len); |
34 | 43 |
35 StatefulMemcpy(&st, &key->n0inv, sizeof(key->n0inv)); | 44 StatefulMemcpy(&st, &key->n0inv, sizeof(key->n0inv)); |
36 StatefulMemcpy(&st, key->n, key_len); | 45 StatefulMemcpy(&st, key->n, key_len); |
37 StatefulMemcpy(&st, key->rr, key_len); | 46 StatefulMemcpy(&st, key->rr, key_len); |
38 if (st.remaining_len != 0) { /* Underrun or overrun. */ | 47 if (st.remaining_len != 0) { /* Underrun or overrun. */ |
39 Free(key->n); | 48 Free(key->n); |
40 Free(key->rr); | 49 Free(key->rr); |
41 Free(key); | 50 Free(key); |
42 return NULL; | 51 return NULL; |
43 } | 52 } |
44 | 53 |
45 return key; | 54 return key; |
46 } | 55 } |
| 56 |
| 57 int RSAVerifyBinary_f(const uint8_t* key_blob, |
| 58 const RSAPublicKey* key, |
| 59 const uint8_t* buf, |
| 60 int len, |
| 61 const uint8_t* sig, |
| 62 int algorithm) { |
| 63 RSAPublicKey* verification_key = NULL; |
| 64 uint8_t* digest = NULL; |
| 65 int key_size; |
| 66 int sig_size; |
| 67 int success; |
| 68 |
| 69 if (algorithm >= kNumAlgorithms) |
| 70 return 0; /* Invalid algorithm. */ |
| 71 key_size = RSAProcessedKeySize(algorithm); |
| 72 sig_size = siglen_map[algorithm] * sizeof(uint32_t); |
| 73 |
| 74 if (key_blob && !key) |
| 75 verification_key = RSAPublicKeyFromBuf(key_blob, key_size); |
| 76 else if (!key_blob && key) |
| 77 verification_key = (RSAPublicKey*) key; /* Supress const warning. */ |
| 78 else |
| 79 return 0; /* Both can't be NULL or non-NULL. */ |
| 80 |
| 81 digest = DigestBuf(buf, len, algorithm); |
| 82 success = RSA_verify(verification_key, sig, sig_size, algorithm, digest); |
| 83 |
| 84 Free(digest); |
| 85 if (!key) |
| 86 RSAPublicKeyFree(verification_key); /* Only free if we allocated it. */ |
| 87 return success; |
| 88 } |
OLD | NEW |