blob: c010eb626b2e4a55f1a8bd88cb0895eda7aec404 [file] [log] [blame]
Martin Rothc7acf162020-05-28 00:44:50 -06001/* SPDX-License-Identifier: GPL-2.0-only */
2
3#include <2crypto.h>
4#include <2return_codes.h>
5#include <bl_uapp/bl_syscall_public.h>
6#include <commonlib/bsd/helpers.h>
7#include <console/console.h>
8#include "psp_verstage.h"
9#include <stddef.h>
10#include <string.h>
11#include <vb2_api.h>
12
13static struct SHA_GENERIC_DATA_T sha_op;
14static uint32_t sha_op_size_remaining;
15static uint8_t __attribute__((aligned(32))) sha_hash[64];
16
17vb2_error_t vb2ex_hwcrypto_digest_init(enum vb2_hash_algorithm hash_alg, uint32_t data_size)
18{
19 printk(BIOS_DEBUG, "Calculating hash of %d bytes\n", data_size);
20
21 sha_op_size_remaining = data_size;
22
23 if (hash_alg == VB2_HASH_SHA256) {
24 sha_op.SHAType = SHA_TYPE_256;
25 sha_op.DigestLen = 32;
26 } else if (hash_alg == VB2_HASH_SHA512) {
27 sha_op.SHAType = SHA_TYPE_512;
28 sha_op.DigestLen = 64;
29 } else {
30 printk(BIOS_INFO, "Unsupported hash_alg %d!\n", hash_alg);
31 return VB2_ERROR_EX_HWCRYPTO_UNSUPPORTED;
32 }
33
34 /* Set init flag for first operation */
35 sha_op.Init = 1;
36
37 /* Clear eom flag until last operation */
38 sha_op.Eom = 0;
39
40 /* Need documentation on this b:157610147 */
41 sha_op.DataMemType = 2;
42
43 sha_op.Digest = sha_hash;
44
45 sha_op.IntermediateDigest = NULL;
46
47 sha_op.IntermediateMsgLen = 0;
48
49 return VB2_SUCCESS;
50}
51
52vb2_error_t vb2ex_hwcrypto_digest_extend(const uint8_t *buf, uint32_t size)
53{
54 uint32_t retval;
55 sha_op.Data = (uint8_t *) buf;
56
57 if (!sha_op_size_remaining) {
58 printk(BIOS_ERR, "ERROR: got more data than expected.\n");
59 return VB2_ERROR_UNKNOWN;
60 }
61
62 while (size) {
63 sha_op.DataLen = size;
64
65 sha_op_size_remaining -= sha_op.DataLen;
66
67 /* Set eom flag for final operation */
68 if (sha_op_size_remaining == 0)
69 sha_op.Eom = 1;
70
71 retval = svc_crypto_sha(&sha_op, SHA_GENERIC);
72 if (retval) {
73 printk(BIOS_ERR, "ERROR: HW crypto failed - errorcode: %#x\n",
74 retval);
75 return VB2_ERROR_UNKNOWN;
76 }
77
78 /* Clear init flag after first operation */
79 if (sha_op.Init == 1)
80 sha_op.Init = 0;
81
82 size -= sha_op.DataLen;
83 }
84
85 return VB2_SUCCESS;
86}
87
88/* Copy the hash back to verstage */
89vb2_error_t vb2ex_hwcrypto_digest_finalize(uint8_t *digest, uint32_t digest_size)
90{
91 if (sha_op.Eom == 0) {
92 printk(BIOS_ERR, "ERROR: Got less data than expected.\n");
93 return VB2_ERROR_UNKNOWN;
94 }
95
96 if (digest_size != sha_op.DigestLen) {
97 printk(BIOS_ERR, "ERROR: Digest size does not match expected length.\n");
98 return VB2_ERROR_UNKNOWN;
99 }
100
101 memcpy(digest, sha_hash, digest_size);
102
103 return VB2_SUCCESS;
104}