xref: /arm-trusted-firmware/drivers/nxp/crypto/caam/src/auth/nxp_crypto.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright 2021 NXP
3*91f16700Schasinglulu  *
4*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
5*91f16700Schasinglulu  *
6*91f16700Schasinglulu  */
7*91f16700Schasinglulu 
8*91f16700Schasinglulu #include <stddef.h>
9*91f16700Schasinglulu #include <string.h>
10*91f16700Schasinglulu 
11*91f16700Schasinglulu #include "caam.h"
12*91f16700Schasinglulu #include <common/debug.h>
13*91f16700Schasinglulu #include <drivers/auth/crypto_mod.h>
14*91f16700Schasinglulu 
15*91f16700Schasinglulu #include "hash.h"
16*91f16700Schasinglulu #include "rsa.h"
17*91f16700Schasinglulu 
18*91f16700Schasinglulu #define LIB_NAME		"NXP crypto"
19*91f16700Schasinglulu 
20*91f16700Schasinglulu /*
21*91f16700Schasinglulu  * Initialize the library and export the descriptor
22*91f16700Schasinglulu  */
23*91f16700Schasinglulu static void init(void)
24*91f16700Schasinglulu {
25*91f16700Schasinglulu 	/* Initialize NXP crypto library`:*/
26*91f16700Schasinglulu 	NOTICE("Initializing & configuring SEC block.\n");
27*91f16700Schasinglulu 
28*91f16700Schasinglulu 	if (config_sec_block() < 0) {
29*91f16700Schasinglulu 		ERROR("Init & config failure for caam.\n");
30*91f16700Schasinglulu 	}
31*91f16700Schasinglulu }
32*91f16700Schasinglulu 
33*91f16700Schasinglulu /*
34*91f16700Schasinglulu  * Verify a signature.
35*91f16700Schasinglulu  *
36*91f16700Schasinglulu  * For IMG_PLAT - data points to a PKCS#1.5 encoded HASH
37*91f16700Schasinglulu  * sig_alg will be RSA or ECC
38*91f16700Schasinglulu  * Parameters are passed using the DER encoding format following the ASN.1
39*91f16700Schasinglulu  * structures detailed above.
40*91f16700Schasinglulu  */
41*91f16700Schasinglulu static int verify_signature(void *data_ptr, unsigned int data_len,
42*91f16700Schasinglulu 			    void *sig_ptr, unsigned int sig_len,
43*91f16700Schasinglulu 			    void *sign_alg, unsigned int sig_alg_len,
44*91f16700Schasinglulu 			    void *pk_ptr, unsigned int pk_len)
45*91f16700Schasinglulu {
46*91f16700Schasinglulu 	int ret = CRYPTO_SUCCESS;
47*91f16700Schasinglulu 
48*91f16700Schasinglulu 	enum sig_alg alg = *(enum sig_alg *)sign_alg;
49*91f16700Schasinglulu 
50*91f16700Schasinglulu 	switch (alg) {
51*91f16700Schasinglulu 	case RSA:
52*91f16700Schasinglulu 		NOTICE("Verifying RSA\n");
53*91f16700Schasinglulu 		ret = rsa_verify_signature(data_ptr, data_len, sig_ptr, sig_len,
54*91f16700Schasinglulu 					   pk_ptr, pk_len);
55*91f16700Schasinglulu 		break;
56*91f16700Schasinglulu 	case ECC:
57*91f16700Schasinglulu 	default:
58*91f16700Schasinglulu 		ret = CRYPTO_ERR_SIGNATURE;
59*91f16700Schasinglulu 		break;
60*91f16700Schasinglulu 	}
61*91f16700Schasinglulu 
62*91f16700Schasinglulu 	if (ret != 0) {
63*91f16700Schasinglulu 		ERROR("RSA verification Failed\n");
64*91f16700Schasinglulu 	}
65*91f16700Schasinglulu 	return ret;
66*91f16700Schasinglulu 
67*91f16700Schasinglulu }
68*91f16700Schasinglulu 
69*91f16700Schasinglulu /*
70*91f16700Schasinglulu  * Match a hash
71*91f16700Schasinglulu  *
72*91f16700Schasinglulu  * Digest info is passed as a table of SHA-26 hashes and digest_info_len
73*91f16700Schasinglulu  * is number of entries in the table
74*91f16700Schasinglulu  * This implementation is very specific to the CSF header parser ROTPK
75*91f16700Schasinglulu  * comparison.
76*91f16700Schasinglulu  */
77*91f16700Schasinglulu static int verify_hash(void *data_ptr, unsigned int data_len,
78*91f16700Schasinglulu 		       void *digest_info_ptr, unsigned int digest_info_len)
79*91f16700Schasinglulu {
80*91f16700Schasinglulu 	void *ctx = NULL;
81*91f16700Schasinglulu 	int i = 0, ret = 0;
82*91f16700Schasinglulu 	enum hash_algo algo = SHA256;
83*91f16700Schasinglulu 	uint8_t hash[SHA256_BYTES] __aligned(CACHE_WRITEBACK_GRANULE) = {0};
84*91f16700Schasinglulu 	uint32_t digest_size = SHA256_BYTES;
85*91f16700Schasinglulu 	uint8_t *hash_tbl = digest_info_ptr;
86*91f16700Schasinglulu 
87*91f16700Schasinglulu 	NOTICE("Verifying hash\n");
88*91f16700Schasinglulu 	ret = hash_init(algo, &ctx);
89*91f16700Schasinglulu 	if (ret != 0) {
90*91f16700Schasinglulu 		return CRYPTO_ERR_HASH;
91*91f16700Schasinglulu 	}
92*91f16700Schasinglulu 
93*91f16700Schasinglulu 	/* Update hash with that of SRK table */
94*91f16700Schasinglulu 	ret = hash_update(algo, ctx, data_ptr, data_len);
95*91f16700Schasinglulu 	if (ret != 0) {
96*91f16700Schasinglulu 		return CRYPTO_ERR_HASH;
97*91f16700Schasinglulu 	}
98*91f16700Schasinglulu 
99*91f16700Schasinglulu 	/* Copy hash at destination buffer */
100*91f16700Schasinglulu 	ret = hash_final(algo, ctx, hash, digest_size);
101*91f16700Schasinglulu 	if (ret != 0) {
102*91f16700Schasinglulu 		return CRYPTO_ERR_HASH;
103*91f16700Schasinglulu 	}
104*91f16700Schasinglulu 
105*91f16700Schasinglulu 	VERBOSE("%s Calculated hash\n", __func__);
106*91f16700Schasinglulu 	for (i = 0; i < SHA256_BYTES/4; i++) {
107*91f16700Schasinglulu 		VERBOSE("%x\n", *((uint32_t *)hash + i));
108*91f16700Schasinglulu 	}
109*91f16700Schasinglulu 
110*91f16700Schasinglulu 	for (i = 0; i < digest_info_len; i++) {
111*91f16700Schasinglulu 		if (memcmp(hash, (hash_tbl + (i * digest_size)),
112*91f16700Schasinglulu 			   digest_size) == 0) {
113*91f16700Schasinglulu 			return CRYPTO_SUCCESS;
114*91f16700Schasinglulu 		}
115*91f16700Schasinglulu 	}
116*91f16700Schasinglulu 
117*91f16700Schasinglulu 	return CRYPTO_ERR_HASH;
118*91f16700Schasinglulu }
119*91f16700Schasinglulu 
120*91f16700Schasinglulu /*
121*91f16700Schasinglulu  * Register crypto library descriptor
122*91f16700Schasinglulu  */
123*91f16700Schasinglulu REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, NULL, NULL, NULL);
124