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 <errno.h> 9*91f16700Schasinglulu #include <stdbool.h> 10*91f16700Schasinglulu #include <stdint.h> 11*91f16700Schasinglulu #include <stdio.h> 12*91f16700Schasinglulu #include <stdlib.h> 13*91f16700Schasinglulu #include <string.h> 14*91f16700Schasinglulu 15*91f16700Schasinglulu #include <arch_helpers.h> 16*91f16700Schasinglulu #include "caam.h" 17*91f16700Schasinglulu #include <common/debug.h> 18*91f16700Schasinglulu #include <drivers/auth/crypto_mod.h> 19*91f16700Schasinglulu 20*91f16700Schasinglulu #include "jobdesc.h" 21*91f16700Schasinglulu #include "rsa.h" 22*91f16700Schasinglulu #include "sec_hw_specific.h" 23*91f16700Schasinglulu 24*91f16700Schasinglulu /* This array contains DER value for SHA-256 */ 25*91f16700Schasinglulu static const uint8_t hash_identifier[] = { 26*91f16700Schasinglulu 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 27*91f16700Schasinglulu 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 28*91f16700Schasinglulu 0x04, 0x20 29*91f16700Schasinglulu }; 30*91f16700Schasinglulu 31*91f16700Schasinglulu static void rsa_done(uint32_t *desc, uint32_t status, void *arg, 32*91f16700Schasinglulu void *job_ring) 33*91f16700Schasinglulu { 34*91f16700Schasinglulu INFO("RSA Desc SUCCESS with status %x\n", status); 35*91f16700Schasinglulu } 36*91f16700Schasinglulu 37*91f16700Schasinglulu static int rsa_public_verif_sec(uint8_t *sign, uint8_t *to, 38*91f16700Schasinglulu uint8_t *rsa_pub_key, uint32_t klen) 39*91f16700Schasinglulu { 40*91f16700Schasinglulu int ret = 0; 41*91f16700Schasinglulu struct rsa_context ctx __aligned(CACHE_WRITEBACK_GRANULE); 42*91f16700Schasinglulu struct job_descriptor jobdesc __aligned(CACHE_WRITEBACK_GRANULE); 43*91f16700Schasinglulu 44*91f16700Schasinglulu jobdesc.arg = NULL; 45*91f16700Schasinglulu jobdesc.callback = rsa_done; 46*91f16700Schasinglulu 47*91f16700Schasinglulu memset(&ctx, 0, sizeof(struct rsa_context)); 48*91f16700Schasinglulu 49*91f16700Schasinglulu ctx.pkin.a = sign; 50*91f16700Schasinglulu ctx.pkin.a_siz = klen; 51*91f16700Schasinglulu ctx.pkin.n = rsa_pub_key; 52*91f16700Schasinglulu ctx.pkin.n_siz = klen; 53*91f16700Schasinglulu ctx.pkin.e = rsa_pub_key + klen; 54*91f16700Schasinglulu ctx.pkin.e_siz = klen; 55*91f16700Schasinglulu 56*91f16700Schasinglulu cnstr_jobdesc_pkha_rsaexp(jobdesc.desc, &ctx.pkin, to, klen); 57*91f16700Schasinglulu 58*91f16700Schasinglulu #if defined(SEC_MEM_NON_COHERENT) && defined(IMAGE_BL2) 59*91f16700Schasinglulu flush_dcache_range((uintptr_t)sign, klen); 60*91f16700Schasinglulu flush_dcache_range((uintptr_t)rsa_pub_key, 2 * klen); 61*91f16700Schasinglulu flush_dcache_range((uintptr_t)&ctx.pkin, sizeof(ctx.pkin)); 62*91f16700Schasinglulu inv_dcache_range((uintptr_t)to, klen); 63*91f16700Schasinglulu 64*91f16700Schasinglulu dmbsy(); 65*91f16700Schasinglulu dsbsy(); 66*91f16700Schasinglulu isb(); 67*91f16700Schasinglulu #endif 68*91f16700Schasinglulu 69*91f16700Schasinglulu /* Finally, generate the requested random data bytes */ 70*91f16700Schasinglulu ret = run_descriptor_jr(&jobdesc); 71*91f16700Schasinglulu if (ret != 0) { 72*91f16700Schasinglulu ERROR("Error in running descriptor\n"); 73*91f16700Schasinglulu ret = -1; 74*91f16700Schasinglulu } 75*91f16700Schasinglulu #if defined(SEC_MEM_NON_COHERENT) && defined(IMAGE_BL2) 76*91f16700Schasinglulu inv_dcache_range((uintptr_t)to, klen); 77*91f16700Schasinglulu dmbsy(); 78*91f16700Schasinglulu dsbsy(); 79*91f16700Schasinglulu isb(); 80*91f16700Schasinglulu #endif 81*91f16700Schasinglulu return ret; 82*91f16700Schasinglulu } 83*91f16700Schasinglulu 84*91f16700Schasinglulu /* 85*91f16700Schasinglulu * Construct encoded hash EM' wrt PKCSv1.5. This function calculates the 86*91f16700Schasinglulu * pointers for padding, DER value and hash. And finally, constructs EM' 87*91f16700Schasinglulu * which includes hash of complete CSF header and ESBC image. If SG flag 88*91f16700Schasinglulu * is on, hash of SG table and entries is also included. 89*91f16700Schasinglulu */ 90*91f16700Schasinglulu static int construct_img_encoded_hash_second(uint8_t *hash, uint8_t hash_len, 91*91f16700Schasinglulu uint8_t *encoded_hash_second, 92*91f16700Schasinglulu unsigned int key_len) 93*91f16700Schasinglulu { 94*91f16700Schasinglulu /* 95*91f16700Schasinglulu * RSA PKCSv1.5 encoding format for encoded message is below 96*91f16700Schasinglulu * EM = 0x0 || 0x1 || PS || 0x0 || DER || Hash 97*91f16700Schasinglulu * PS is Padding String 98*91f16700Schasinglulu * DER is DER value for SHA-256 99*91f16700Schasinglulu * Hash is SHA-256 hash 100*91f16700Schasinglulu * ********************************************************* 101*91f16700Schasinglulu * representative points to first byte of EM initially and is 102*91f16700Schasinglulu * filled with 0x0 103*91f16700Schasinglulu * representative is incremented by 1 and second byte is filled 104*91f16700Schasinglulu * with 0x1 105*91f16700Schasinglulu * padding points to third byte of EM 106*91f16700Schasinglulu * digest points to full length of EM - 32 bytes 107*91f16700Schasinglulu * hash_id (DER value) points to 19 bytes before pDigest 108*91f16700Schasinglulu * separator is one byte which separates padding and DER 109*91f16700Schasinglulu */ 110*91f16700Schasinglulu 111*91f16700Schasinglulu unsigned int len; 112*91f16700Schasinglulu uint8_t *representative; 113*91f16700Schasinglulu uint8_t *padding, *digest; 114*91f16700Schasinglulu uint8_t *hash_id, *separator; 115*91f16700Schasinglulu int i; 116*91f16700Schasinglulu int ret = 0; 117*91f16700Schasinglulu 118*91f16700Schasinglulu if (hash_len != SHA256_BYTES) { 119*91f16700Schasinglulu return -1; 120*91f16700Schasinglulu } 121*91f16700Schasinglulu 122*91f16700Schasinglulu /* Key length = Modulus length */ 123*91f16700Schasinglulu len = (key_len / 2U) - 1U; 124*91f16700Schasinglulu representative = encoded_hash_second; 125*91f16700Schasinglulu representative[0] = 0U; 126*91f16700Schasinglulu representative[1] = 1U; /* block type 1 */ 127*91f16700Schasinglulu 128*91f16700Schasinglulu padding = &representative[2]; 129*91f16700Schasinglulu digest = &representative[1] + len - 32; 130*91f16700Schasinglulu hash_id = digest - sizeof(hash_identifier); 131*91f16700Schasinglulu separator = hash_id - 1; 132*91f16700Schasinglulu 133*91f16700Schasinglulu /* fill padding area pointed by padding with 0xff */ 134*91f16700Schasinglulu memset(padding, 0xff, separator - padding); 135*91f16700Schasinglulu 136*91f16700Schasinglulu /* fill byte pointed by separator */ 137*91f16700Schasinglulu *separator = 0U; 138*91f16700Schasinglulu 139*91f16700Schasinglulu /* fill SHA-256 DER value pointed by HashId */ 140*91f16700Schasinglulu memcpy(hash_id, hash_identifier, sizeof(hash_identifier)); 141*91f16700Schasinglulu 142*91f16700Schasinglulu /* fill hash pointed by Digest */ 143*91f16700Schasinglulu for (i = 0; i < SHA256_BYTES; i++) { 144*91f16700Schasinglulu digest[i] = hash[i]; 145*91f16700Schasinglulu } 146*91f16700Schasinglulu 147*91f16700Schasinglulu return ret; 148*91f16700Schasinglulu } 149*91f16700Schasinglulu 150*91f16700Schasinglulu int rsa_verify_signature(void *hash_ptr, unsigned int hash_len, 151*91f16700Schasinglulu void *sig_ptr, unsigned int sig_len, 152*91f16700Schasinglulu void *pk_ptr, unsigned int pk_len) 153*91f16700Schasinglulu { 154*91f16700Schasinglulu uint8_t img_encoded_hash_second[RSA_4K_KEY_SZ_BYTES]; 155*91f16700Schasinglulu uint8_t encoded_hash[RSA_4K_KEY_SZ_BYTES] __aligned(CACHE_WRITEBACK_GRANULE); 156*91f16700Schasinglulu int ret = 0; 157*91f16700Schasinglulu 158*91f16700Schasinglulu ret = construct_img_encoded_hash_second(hash_ptr, hash_len, 159*91f16700Schasinglulu img_encoded_hash_second, 160*91f16700Schasinglulu pk_len); 161*91f16700Schasinglulu if (ret != 0) { 162*91f16700Schasinglulu ERROR("Encoded Hash Failure\n"); 163*91f16700Schasinglulu return CRYPTO_ERR_SIGNATURE; 164*91f16700Schasinglulu } 165*91f16700Schasinglulu 166*91f16700Schasinglulu ret = rsa_public_verif_sec(sig_ptr, encoded_hash, pk_ptr, pk_len / 2); 167*91f16700Schasinglulu if (ret != 0) { 168*91f16700Schasinglulu ERROR("RSA signature Failure\n"); 169*91f16700Schasinglulu return CRYPTO_ERR_SIGNATURE; 170*91f16700Schasinglulu } 171*91f16700Schasinglulu 172*91f16700Schasinglulu ret = memcmp(img_encoded_hash_second, encoded_hash, sig_len); 173*91f16700Schasinglulu if (ret != 0) { 174*91f16700Schasinglulu ERROR("Comparison Failure\n"); 175*91f16700Schasinglulu return CRYPTO_ERR_SIGNATURE; 176*91f16700Schasinglulu } 177*91f16700Schasinglulu 178*91f16700Schasinglulu return CRYPTO_SUCCESS; 179*91f16700Schasinglulu } 180