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 "sha_utility.h" |
11 #include "utility.h" | 11 #include "utility.h" |
12 | 12 |
13 int RSAProcessedKeySize(int algorithm) { | 13 int RSAProcessedKeySize(int algorithm) { |
14 int key_len = siglen_map[algorithm]; /* Key length in | 14 int key_len = siglen_map[algorithm]; /* Key length in |
15 * bytes. */ | 15 * bytes. */ |
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 return (2 * key_len + sizeof(int) + sizeof(uint32_t)); | 20 return (2 * key_len + sizeof(int) + sizeof(uint32_t)); |
21 } | 21 } |
22 | 22 |
| 23 RSAPublicKey* RSAPublicKeyNew(void) { |
| 24 RSAPublicKey* key = (RSAPublicKey*) Malloc(sizeof(RSAPublicKey)); |
| 25 key->n = NULL; |
| 26 key->rr = NULL; |
| 27 return key; |
| 28 } |
| 29 |
23 void RSAPublicKeyFree(RSAPublicKey* key) { | 30 void RSAPublicKeyFree(RSAPublicKey* key) { |
24 if (key) { | 31 if (key) { |
25 Free(key->n); | 32 Free(key->n); |
26 Free(key->rr); | 33 Free(key->rr); |
27 Free(key); | 34 Free(key); |
28 } | 35 } |
29 } | 36 } |
30 | 37 |
31 RSAPublicKey* RSAPublicKeyFromBuf(const uint8_t* buf, int len) { | 38 RSAPublicKey* RSAPublicKeyFromBuf(const uint8_t* buf, int len) { |
32 RSAPublicKey* key = (RSAPublicKey*) Malloc(sizeof(RSAPublicKey)); | 39 RSAPublicKey* key = RSAPublicKeyNew(); |
33 MemcpyState st; | 40 MemcpyState st; |
34 int key_len; | 41 int key_len; |
35 | 42 |
36 st.remaining_buf = (uint8_t*) buf; | 43 st.remaining_buf = (uint8_t*) buf; |
37 st.remaining_len = len; | 44 st.remaining_len = len; |
38 | |
39 StatefulMemcpy(&st, &key->len, sizeof(key->len)); | 45 StatefulMemcpy(&st, &key->len, sizeof(key->len)); |
40 key_len = key->len * sizeof(uint32_t); /* key length in bytes. */ | 46 key_len = key->len * sizeof(uint32_t); /* key length in bytes. */ |
| 47 |
| 48 /* Sanity Check the key length. */ |
| 49 if (RSA1024NUMBYTES != key_len && |
| 50 RSA2048NUMBYTES != key_len && |
| 51 RSA4096NUMBYTES != key_len && |
| 52 RSA8192NUMBYTES != key_len) { |
| 53 RSAPublicKeyFree(key); |
| 54 return NULL; |
| 55 } |
| 56 |
41 key->n = (uint32_t*) Malloc(key_len); | 57 key->n = (uint32_t*) Malloc(key_len); |
42 key->rr = (uint32_t*) Malloc(key_len); | 58 key->rr = (uint32_t*) Malloc(key_len); |
43 | 59 |
44 StatefulMemcpy(&st, &key->n0inv, sizeof(key->n0inv)); | 60 StatefulMemcpy(&st, &key->n0inv, sizeof(key->n0inv)); |
45 StatefulMemcpy(&st, key->n, key_len); | 61 StatefulMemcpy(&st, key->n, key_len); |
46 StatefulMemcpy(&st, key->rr, key_len); | 62 StatefulMemcpy(&st, key->rr, key_len); |
47 if (st.remaining_len != 0) { /* Underrun or overrun. */ | 63 if (st.remaining_len != 0) { /* Underrun or overrun. */ |
48 Free(key->n); | 64 RSAPublicKeyFree(key); |
49 Free(key->rr); | |
50 Free(key); | |
51 return NULL; | 65 return NULL; |
52 } | 66 } |
53 | 67 |
54 return key; | 68 return key; |
55 } | 69 } |
56 | 70 |
57 int RSAVerifyBinary_f(const uint8_t* key_blob, | 71 int RSAVerifyBinary_f(const uint8_t* key_blob, |
58 const RSAPublicKey* key, | 72 const RSAPublicKey* key, |
59 const uint8_t* buf, | 73 const uint8_t* buf, |
60 int len, | 74 int len, |
(...skipping 18 matching lines...) Expand all Loading... |
79 return 0; /* Both can't be NULL or non-NULL. */ | 93 return 0; /* Both can't be NULL or non-NULL. */ |
80 | 94 |
81 digest = DigestBuf(buf, len, algorithm); | 95 digest = DigestBuf(buf, len, algorithm); |
82 success = RSAVerify(verification_key, sig, sig_size, algorithm, digest); | 96 success = RSAVerify(verification_key, sig, sig_size, algorithm, digest); |
83 | 97 |
84 Free(digest); | 98 Free(digest); |
85 if (!key) | 99 if (!key) |
86 RSAPublicKeyFree(verification_key); /* Only free if we allocated it. */ | 100 RSAPublicKeyFree(verification_key); /* Only free if we allocated it. */ |
87 return success; | 101 return success; |
88 } | 102 } |
OLD | NEW |