1 /*
2 * Copyright (c) 2018-2022 Intel Corporation.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 *
25 */
26
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <openssl/rand.h>
31
32 #include "types.h"
33 #include "vrpmb.h"
34 #include "log.h"
35
36 #define DRNG_MAX_RETRIES 5U
37
38 struct key_material {
39 uint8_t key[RPMB_KEY_LEN];
40 bool initialized;
41 };
42
43 static struct key_material vrkey = {
44 .key = {0},
45 .initialized = false
46 };
47
get_vrpmb_key(uint8_t * out,size_t size)48 int get_vrpmb_key(uint8_t *out, size_t size)
49 {
50 int ret;
51 int i;
52
53 if (!out) {
54 pr_err("%s: Invalid output pointer\n", __func__);
55 return 0;
56 }
57
58 if (size != RPMB_KEY_LEN) {
59 pr_err("%s: Invalid input key size\n", __func__);
60 return 0;
61 }
62
63 if ( vrkey.initialized == false ) {
64 for (i = 0; i < DRNG_MAX_RETRIES; i++) {
65 ret = RAND_bytes(vrkey.key, RPMB_KEY_LEN);
66 if (ret == 1) {
67 vrkey.initialized = true;
68 break;
69 }
70 }
71
72 if (vrkey.initialized != true) {
73 pr_err("%s: unable to generate random key!\n", __func__);
74 return 0;
75 }
76 }
77
78 memcpy(out, vrkey.key, size);
79 return 1;
80 }
81