rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | #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 | |
| 16 | static hmac_state hmac; |
| 17 | static struct sha256_context sha256_state; |
| 18 | static 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 */ |
| 28 | static int rak_inited = 0; |
| 29 | static unsigned char rak[32]; |
| 30 | |
| 31 | static 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 | |
| 41 | int 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 | |
| 52 | int rpmb_hmac_process(unsigned char *buf, unsigned int size) |
| 53 | { |
| 54 | return hmac_process(&hmac, buf, size); |
| 55 | } |
| 56 | |
| 57 | int 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 | |
| 70 | int 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 | |
| 78 | void 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 | } |