xref: /openbmc/u-boot/drivers/crypto/fsl/fsl_rsa.c (revision ee943655)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2014 Freescale Semiconductor, Inc.
4  * Author: Ruchika Gupta <ruchika.gupta@freescale.com>
5  */
6 
7 #include <config.h>
8 #include <common.h>
9 #include <dm.h>
10 #include <asm/types.h>
11 #include <malloc.h>
12 #include "jobdesc.h"
13 #include "desc.h"
14 #include "jr.h"
15 #include "rsa_caam.h"
16 #include <u-boot/rsa-mod-exp.h>
17 
18 int fsl_mod_exp(struct udevice *dev, const uint8_t *sig, uint32_t sig_len,
19 		struct key_prop *prop, uint8_t *out)
20 {
21 	uint32_t keylen;
22 	struct pk_in_params pkin;
23 	uint32_t desc[MAX_CAAM_DESCSIZE];
24 	int ret;
25 
26 	/* Length in bytes */
27 	keylen = prop->num_bits / 8;
28 
29 	pkin.a = sig;
30 	pkin.a_siz = sig_len;
31 	pkin.n = prop->modulus;
32 	pkin.n_siz = keylen;
33 	pkin.e = prop->public_exponent;
34 	pkin.e_siz = prop->exp_len;
35 
36 	inline_cnstr_jobdesc_pkha_rsaexp(desc, &pkin, out, sig_len);
37 
38 	ret = run_descriptor_jr(desc);
39 	if (ret) {
40 		debug("%s: RSA failed to verify: %d\n", __func__, ret);
41 		return -EFAULT;
42 	}
43 
44 	return 0;
45 }
46 
47 static const struct mod_exp_ops fsl_mod_exp_ops = {
48 	.mod_exp	= fsl_mod_exp,
49 };
50 
51 U_BOOT_DRIVER(fsl_rsa_mod_exp) = {
52 	.name	= "fsl_rsa_mod_exp",
53 	.id	= UCLASS_MOD_EXP,
54 	.ops	= &fsl_mod_exp_ops,
55 	.flags  = DM_FLAG_PRE_RELOC,
56 };
57 
58 U_BOOT_DEVICE(fsl_rsa) = {
59 	.name = "fsl_rsa_mod_exp",
60 };
61