Gaurav Shah | e178fd9 | 2010-02-05 11:44:58 -0800 | [diff] [blame] | 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 |
| 3 | * found in the LICENSE file. |
| 4 | * |
| 5 | * Utility functions for message digest functions. |
| 6 | */ |
| 7 | |
| 8 | #include "padding.h" |
| 9 | #include "rsa_utility.h" |
| 10 | #include "utility.h" |
| 11 | |
| 12 | int RSAProcessedKeySize(int algorithm) { |
| 13 | int key_len = siglen_map[algorithm] * sizeof(uint32_t); /* Key length in |
| 14 | * bytes. */ |
| 15 | /* Total size needed by a RSAPublicKey structure is = |
| 16 | * 2 * key_len bytes for the n and rr arrays |
| 17 | * + sizeof len + sizeof n0inv. |
| 18 | */ |
| 19 | return (2 * key_len + sizeof(int) + sizeof(uint32_t)); |
| 20 | } |
| 21 | |
| 22 | RSAPublicKey* RSAPublicKeyFromBuf(uint8_t* buf, int len) { |
| 23 | RSAPublicKey* key = (RSAPublicKey*) Malloc(sizeof(RSAPublicKey)); |
| 24 | MemcpyState st; |
| 25 | int key_len; |
| 26 | |
| 27 | st.remaining_buf = buf; |
| 28 | st.remaining_len = len; |
| 29 | |
| 30 | StatefulMemcpy(&st, &key->len, sizeof(key->len)); |
| 31 | key_len = key->len * sizeof(uint32_t); /* key length in bytes. */ |
| 32 | key->n = (uint32_t*) Malloc(key_len); |
| 33 | key->rr = (uint32_t*) Malloc(key_len); |
| 34 | |
| 35 | StatefulMemcpy(&st, &key->n0inv, sizeof(key->n0inv)); |
| 36 | StatefulMemcpy(&st, key->n, key_len); |
| 37 | StatefulMemcpy(&st, key->rr, key_len); |
| 38 | if (st.remaining_len != 0) { /* Underrun or overrun. */ |
| 39 | Free(key->n); |
| 40 | Free(key->rr); |
| 41 | Free(key); |
| 42 | return NULL; |
| 43 | } |
| 44 | |
| 45 | return key; |
| 46 | } |