1 | // =================================================================== |
---|
2 | // |
---|
3 | // Copyright (c) 2005, Intel Corp. |
---|
4 | // All rights reserved. |
---|
5 | // |
---|
6 | // Redistribution and use in source and binary forms, with or without |
---|
7 | // modification, are permitted provided that the following conditions |
---|
8 | // are met: |
---|
9 | // |
---|
10 | // * Redistributions of source code must retain the above copyright |
---|
11 | // notice, this list of conditions and the following disclaimer. |
---|
12 | // * Redistributions in binary form must reproduce the above |
---|
13 | // copyright notice, this list of conditions and the following |
---|
14 | // disclaimer in the documentation and/or other materials provided |
---|
15 | // with the distribution. |
---|
16 | // * Neither the name of Intel Corporation nor the names of its |
---|
17 | // contributors may be used to endorse or promote products derived |
---|
18 | // from this software without specific prior written permission. |
---|
19 | // |
---|
20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
---|
23 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
---|
24 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
---|
25 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
26 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
---|
27 | // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
28 | // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
---|
29 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
30 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
---|
31 | // OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
32 | // =================================================================== |
---|
33 | // |
---|
34 | // crypto.c |
---|
35 | // |
---|
36 | // This file will handle all the TPM Crypto functionality |
---|
37 | // |
---|
38 | // ================================================================== |
---|
39 | |
---|
40 | #include <string.h> |
---|
41 | #include <openssl/crypto.h> |
---|
42 | #include <openssl/err.h> |
---|
43 | #include <openssl/evp.h> |
---|
44 | #include <openssl/rand.h> |
---|
45 | #include "crypto.h" |
---|
46 | #include "log.h" |
---|
47 | |
---|
48 | /** |
---|
49 | * Initialize cryptography library |
---|
50 | * @rand: random seed |
---|
51 | * @size: size of @rand |
---|
52 | */ |
---|
53 | void Crypto_Init(const BYTE* rand, int size) { |
---|
54 | ERR_load_crypto_strings(); |
---|
55 | CRYPTO_malloc_init(); |
---|
56 | OpenSSL_add_all_algorithms(); |
---|
57 | SYM_CIPHER = EVP_aes_128_cbc(); |
---|
58 | RAND_poll(); |
---|
59 | if (rand == NULL) |
---|
60 | return; |
---|
61 | |
---|
62 | RAND_add(rand, size, size); |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * Shutdown cryptography library |
---|
67 | */ |
---|
68 | void Crypto_Exit() { |
---|
69 | ERR_free_strings(); |
---|
70 | ERR_remove_state(0); |
---|
71 | EVP_cleanup(); |
---|
72 | } |
---|
73 | |
---|
74 | |
---|
75 | /** |
---|
76 | * Get random data |
---|
77 | * @data: (OUT) Random data |
---|
78 | * @size: Size of @data |
---|
79 | */ |
---|
80 | void Crypto_GetRandom(void* data, int size) { |
---|
81 | int result; |
---|
82 | |
---|
83 | result = RAND_pseudo_bytes((BYTE*) data, size); |
---|
84 | |
---|
85 | if (result <= 0) |
---|
86 | vtpmlogerror (VTPM_LOG_CRYPTO, "RAND_pseudo_bytes failed: %s\n", |
---|
87 | ERR_error_string (ERR_get_error(), NULL)); |
---|
88 | } |
---|