blob: 0bb9066f9caf73065fb18e04e9d86872ef7adad3 [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}
Kangheui Won07de9082020-08-14 14:37:53 +1000105
106vb2_error_t vb2ex_hwcrypto_rsa_verify_digest(const struct vb2_public_key *key,
107 const uint8_t *sig, const uint8_t *digest)
108{
109 RSAPKCS_VERIFY_PARAMS RSAParams;
110 uint32_t retval;
111 uint32_t exp = 65537;
112 uint32_t sig_size;
113 size_t digest_size;
114
115 /* PSP only supports 2K and 4K RSA */
116 if (key->sig_alg != VB2_SIG_RSA2048 &&
117 key->sig_alg != VB2_SIG_RSA2048_EXP3 &&
118 key->sig_alg != VB2_SIG_RSA4096) {
119 return VB2_ERROR_EX_HWCRYPTO_UNSUPPORTED;
120 }
121
122 /* PSP only supports SHA256, SHA384 and SHA512*/
123 if (key->hash_alg != VB2_HASH_SHA256 &&
124 key->hash_alg != VB2_HASH_SHA384 &&
125 key->hash_alg != VB2_HASH_SHA512) {
126 return VB2_ERROR_EX_HWCRYPTO_UNSUPPORTED;
127 }
128
129 if (key->sig_alg == VB2_SIG_RSA2048_EXP3)
130 exp = 3;
131 sig_size = vb2_rsa_sig_size(key->sig_alg);
132 digest_size = vb2_digest_size(key->hash_alg);
133
134 RSAParams.pHash = (char *)digest;
135 RSAParams.HashLen = digest_size;
136 RSAParams.pModulus = (char *)key->n;
137 RSAParams.ModulusSize = sig_size;
138 RSAParams.pExponent = (char *)&exp;
139 RSAParams.ExpSize = sizeof(exp);
140 RSAParams.pSig = (char *)sig;
141
142 retval = svc_rsa_pkcs_verify(&RSAParams);
143 if (retval) {
144 printk(BIOS_ERR, "ERROR: HW crypto failed - errorcode: %#x\n",
145 retval);
146 return VB2_ERROR_RSA_VERIFY_DIGEST;
147 }
148
149 return VB2_SUCCESS;
150}