blob: a68a3ef5890e7bdac1a2538603588a937b351d85 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001#include <string.h>
2#include <sha256.h>
3#include <hmac.h>
4#include <reg.h>
5#include <lk/init.h>
6#include <debug.h>
7#include "seclib.h"
8#include "platform/mtk_serial_key.h"
9#include "platform/mmc_rpmb.h"
10
11/******************************************************************************
12 * CONSTANT DEFINITIONS
13 ******************************************************************************/
14#define MOD "RPMB"
15
16static hmac_state hmac;
17static struct sha256_context sha256_state;
18static hash_param sha256_hash = {
19 .hash_state = &sha256_state,
20 .init = (int (*)(void *))sha256_start,
21 .process = (int (*)(void *, const unsigned char *, unsigned int))sha256_process,
22 .done = (int (*)(void *, unsigned char *))sha256_end,
23 .hashsize = 32,
24 .blocksize = 64
25};
26
27/* rpmb_auth_key */
28static int rak_inited = 0;
29static unsigned char rak[32];
30
31static int rpmb_key_init(unsigned char *data, unsigned int size)
32{
33 if (size != 32)
34 return -1;
35
36 memcpy(rak, data, 32);
37 rak_inited = 1;
38 return 0;
39}
40
41int rpmb_hmac_init(unsigned char *buf, unsigned int size)
42{
43 if (rak_inited == 0) {
44 return -1;
45 }
46 hmac_init(&hmac, &sha256_hash, rak, sizeof(rak));
47 hmac_process(&hmac, buf, size);
48
49 return 0;
50}
51
52int rpmb_hmac_process(unsigned char *buf, unsigned int size)
53{
54 return hmac_process(&hmac, buf, size);
55}
56
57int rpmb_hmac_done(unsigned char *outmac, unsigned int *size)
58{
59 unsigned long out_len;
60
61 if (size == NULL || outmac == NULL) return -1;
62
63 out_len = *size;
64 hmac_done(&hmac, outmac, &out_len);
65 *size = out_len;
66
67 return 0;
68}
69
70int rpmb_set_key(int (*set_key_func)(u8 *))
71{
72 if (rak_inited == 0) {
73 return -1;
74 }
75 return set_key_func(rak);
76}
77
78void rpmb_init(void)
79{
80 u32 id[4];
81 u32 rpmb_key[8];
82
83 id[0] = readl(SERIAL_KEY_LO);
84 id[1] = readl(SERIAL_KEY_HI);
85 id[2] = readl(SERIAL_KEY_2_LO);
86 id[3] = readl(SERIAL_KEY_2_HI);
87 seclib_get_msg_auth_key((void *)id, 16, (void *)rpmb_key, 32);
88#if 0
89 dprintf(CRITICAL, "id: %08x %08x %08x %08x\n",
90 id[0], id[1], id[2], id[3]);
91 dprintf(CRITICAL, "rpmbkey: %08x %08x %08x %08x %08x %08x %08x %08x\n",
92 rpmb_key[0], rpmb_key[1], rpmb_key[2], rpmb_key[3],
93 rpmb_key[4], rpmb_key[5], rpmb_key[6], rpmb_key[7]);
94#endif
95
96 rpmb_key_init((unsigned char *)rpmb_key, sizeof(rpmb_key));
97 rpmb_set_key(mmc_rpmb_set_key);
98}