blob: 335b7fe25acbcf65fe6994c54ca6bdd82c51d190 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ruchika Gupta29c1a6b2015-01-23 16:01:55 +05302/*
3 * (C) Copyright 2014 Freescale Semiconductor, Inc.
4 * Author: Ruchika Gupta <ruchika.gupta@freescale.com>
Ruchika Gupta29c1a6b2015-01-23 16:01:55 +05305 */
6
7#include <config.h>
8#include <common.h>
Ye Li3183fdd2022-09-28 15:49:02 +05309#include <cpu_func.h>
Ruchika Gupta29c1a6b2015-01-23 16:01:55 +053010#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Ruchika Gupta29c1a6b2015-01-23 16:01:55 +053012#include <asm/types.h>
13#include <malloc.h>
14#include "jobdesc.h"
15#include "desc.h"
16#include "jr.h"
17#include "rsa_caam.h"
18#include <u-boot/rsa-mod-exp.h>
19
20int fsl_mod_exp(struct udevice *dev, const uint8_t *sig, uint32_t sig_len,
21 struct key_prop *prop, uint8_t *out)
22{
23 uint32_t keylen;
24 struct pk_in_params pkin;
25 uint32_t desc[MAX_CAAM_DESCSIZE];
26 int ret;
27
28 /* Length in bytes */
29 keylen = prop->num_bits / 8;
30
31 pkin.a = sig;
32 pkin.a_siz = sig_len;
33 pkin.n = prop->modulus;
34 pkin.n_siz = keylen;
35 pkin.e = prop->public_exponent;
36 pkin.e_siz = prop->exp_len;
37
38 inline_cnstr_jobdesc_pkha_rsaexp(desc, &pkin, out, sig_len);
39
Ye Li3183fdd2022-09-28 15:49:02 +053040 flush_dcache_range((ulong)sig, (ulong)sig + sig_len);
41 flush_dcache_range((ulong)prop->modulus, (ulong)(prop->modulus) + keylen);
42 flush_dcache_range((ulong)prop->public_exponent,
43 (ulong)(prop->public_exponent) + prop->exp_len);
44 flush_dcache_range((ulong)desc, (ulong)desc + (sizeof(uint32_t) * MAX_CAAM_DESCSIZE));
45 flush_dcache_range((ulong)out, (ulong)out + sig_len);
46
Ruchika Gupta29c1a6b2015-01-23 16:01:55 +053047 ret = run_descriptor_jr(desc);
48 if (ret) {
49 debug("%s: RSA failed to verify: %d\n", __func__, ret);
50 return -EFAULT;
51 }
52
Ye Li3183fdd2022-09-28 15:49:02 +053053 invalidate_dcache_range((ulong)out, (ulong)out + sig_len);
54
Ruchika Gupta29c1a6b2015-01-23 16:01:55 +053055 return 0;
56}
57
58static const struct mod_exp_ops fsl_mod_exp_ops = {
59 .mod_exp = fsl_mod_exp,
60};
61
62U_BOOT_DRIVER(fsl_rsa_mod_exp) = {
63 .name = "fsl_rsa_mod_exp",
64 .id = UCLASS_MOD_EXP,
65 .ops = &fsl_mod_exp_ops,
Ruchika Gupta29c1a6b2015-01-23 16:01:55 +053066};
67
Simon Glass1d8364a2020-12-28 20:34:54 -070068U_BOOT_DRVINFO(fsl_rsa) = {
Ruchika Gupta29c1a6b2015-01-23 16:01:55 +053069 .name = "fsl_rsa_mod_exp",
70};