Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ruchika Gupta | 29c1a6b | 2015-01-23 16:01:55 +0530 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2014 Freescale Semiconductor, Inc. |
| 4 | * Author: Ruchika Gupta <ruchika.gupta@freescale.com> |
Ruchika Gupta | 29c1a6b | 2015-01-23 16:01:55 +0530 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <config.h> |
Ye Li | 3183fdd | 2022-09-28 15:49:02 +0530 | [diff] [blame] | 8 | #include <cpu_func.h> |
Ruchika Gupta | 29c1a6b | 2015-01-23 16:01:55 +0530 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Ruchika Gupta | 29c1a6b | 2015-01-23 16:01:55 +0530 | [diff] [blame] | 11 | #include <asm/types.h> |
| 12 | #include <malloc.h> |
| 13 | #include "jobdesc.h" |
| 14 | #include "desc.h" |
| 15 | #include "jr.h" |
| 16 | #include "rsa_caam.h" |
| 17 | #include <u-boot/rsa-mod-exp.h> |
| 18 | |
| 19 | int fsl_mod_exp(struct udevice *dev, const uint8_t *sig, uint32_t sig_len, |
| 20 | struct key_prop *prop, uint8_t *out) |
| 21 | { |
| 22 | uint32_t keylen; |
| 23 | struct pk_in_params pkin; |
| 24 | uint32_t desc[MAX_CAAM_DESCSIZE]; |
| 25 | int ret; |
| 26 | |
| 27 | /* Length in bytes */ |
| 28 | keylen = prop->num_bits / 8; |
| 29 | |
| 30 | pkin.a = sig; |
| 31 | pkin.a_siz = sig_len; |
| 32 | pkin.n = prop->modulus; |
| 33 | pkin.n_siz = keylen; |
| 34 | pkin.e = prop->public_exponent; |
| 35 | pkin.e_siz = prop->exp_len; |
| 36 | |
| 37 | inline_cnstr_jobdesc_pkha_rsaexp(desc, &pkin, out, sig_len); |
| 38 | |
Ye Li | 3183fdd | 2022-09-28 15:49:02 +0530 | [diff] [blame] | 39 | flush_dcache_range((ulong)sig, (ulong)sig + sig_len); |
| 40 | flush_dcache_range((ulong)prop->modulus, (ulong)(prop->modulus) + keylen); |
| 41 | flush_dcache_range((ulong)prop->public_exponent, |
| 42 | (ulong)(prop->public_exponent) + prop->exp_len); |
| 43 | flush_dcache_range((ulong)desc, (ulong)desc + (sizeof(uint32_t) * MAX_CAAM_DESCSIZE)); |
| 44 | flush_dcache_range((ulong)out, (ulong)out + sig_len); |
| 45 | |
Ruchika Gupta | 29c1a6b | 2015-01-23 16:01:55 +0530 | [diff] [blame] | 46 | ret = run_descriptor_jr(desc); |
| 47 | if (ret) { |
| 48 | debug("%s: RSA failed to verify: %d\n", __func__, ret); |
| 49 | return -EFAULT; |
| 50 | } |
| 51 | |
Ye Li | 3183fdd | 2022-09-28 15:49:02 +0530 | [diff] [blame] | 52 | invalidate_dcache_range((ulong)out, (ulong)out + sig_len); |
| 53 | |
Ruchika Gupta | 29c1a6b | 2015-01-23 16:01:55 +0530 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static const struct mod_exp_ops fsl_mod_exp_ops = { |
| 58 | .mod_exp = fsl_mod_exp, |
| 59 | }; |
| 60 | |
| 61 | U_BOOT_DRIVER(fsl_rsa_mod_exp) = { |
| 62 | .name = "fsl_rsa_mod_exp", |
| 63 | .id = UCLASS_MOD_EXP, |
| 64 | .ops = &fsl_mod_exp_ops, |
Ruchika Gupta | 29c1a6b | 2015-01-23 16:01:55 +0530 | [diff] [blame] | 65 | }; |
| 66 | |
Simon Glass | 1d8364a | 2020-12-28 20:34:54 -0700 | [diff] [blame] | 67 | U_BOOT_DRVINFO(fsl_rsa) = { |
Ruchika Gupta | 29c1a6b | 2015-01-23 16:01:55 +0530 | [diff] [blame] | 68 | .name = "fsl_rsa_mod_exp", |
| 69 | }; |