From 32b1c1d6fff2e138c668810e0fd154e8ab0e9e56 Mon Sep 17 00:00:00 2001 From: Siva Durga Prasad Paladugu Date: Fri, 29 Nov 2013 19:01:24 +0530 Subject: [PATCH] image: Added RSA support for zynq Added RSA support for zynq. This uses the montogomery multiplications while verifying the authenticated image. Modified Makefile to compile rsa-verify.c if CONFIG_CMD_ZYNQ_RSA was enabled to get rid of unrequired FDT functionality incase of zynq. Signed-off-by: Siva Durga Prasad Paladugu Signed-off-by: Michal Simek --- include/rsa.h | 2 ++ lib/rsa/Makefile | 2 ++ lib/rsa/rsa-verify.c | 47 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/include/rsa.h b/include/rsa.h index add4c789f33..6f07ae368a0 100644 --- a/include/rsa.h +++ b/include/rsa.h @@ -89,4 +89,6 @@ static inline int rsa_verify(struct image_sign_info *info, } #endif +int zynq_pow_mod(uint32_t *keyptr, uint32_t *inout); + #endif diff --git a/lib/rsa/Makefile b/lib/rsa/Makefile index decd6e509df..27c3bc3f9cd 100644 --- a/lib/rsa/Makefile +++ b/lib/rsa/Makefile @@ -15,6 +15,8 @@ ifdef CONFIG_FIT_SIGNATURE COBJS-$(CONFIG_RSA) += rsa-verify.o endif +COBJS-$(CONFIG_CMD_ZYNQ_RSA) += rsa-verify.o + COBJS := $(sort $(COBJS-y)) SRCS := $(COBJS:.o=.c) OBJS := $(addprefix $(obj),$(COBJS)) diff --git a/lib/rsa/rsa-verify.c b/lib/rsa/rsa-verify.c index 02cc4e33530..ccebf27d143 100644 --- a/lib/rsa/rsa-verify.c +++ b/lib/rsa/rsa-verify.c @@ -164,7 +164,7 @@ static void montgomery_mul(const struct rsa_public_key *key, for (i = 0; i < key->len; ++i) montgomery_mul_add_step(key, result, a[i], b); } - +#if IMAGE_ENABLE_VERIFY /** * pow_mod() - in-place public exponentiation * @@ -370,3 +370,48 @@ int rsa_verify(struct image_sign_info *info, return ret; } +#endif + +/** + * zynq_pow_mod() - in-place public exponentiation + * + * @keyptr: RSA key + * @inout: Big-endian word array containing value and result + */ +int zynq_pow_mod(uint32_t *keyptr, uint32_t *inout) +{ + uint32_t *result, *ptr; + uint i; + struct rsa_public_key *key; + + key = (struct rsa_public_key *)keyptr; + + /* Sanity check for stack size - key->len is in 32-bit words */ + if (key->len > RSA_MAX_KEY_BITS / 32) { + debug("RSA key words %u exceeds maximum %d\n", key->len, + RSA_MAX_KEY_BITS / 32); + return -EINVAL; + } + + uint32_t val[key->len], acc[key->len], tmp[key->len]; + result = tmp; /* Re-use location. */ + + for (i = 0, ptr = inout; i < key->len; i++, ptr++) + val[i] = *(ptr); + + montgomery_mul(key, acc, val, key->rr); /* axx = a * RR / R mod M */ + for (i = 0; i < 16; i += 2) { + montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod M */ + montgomery_mul(key, acc, tmp, tmp); /* acc = tmp^2 / R mod M */ + } + montgomery_mul(key, result, acc, val); /* result = XX * a / R mod M */ + + /* Make sure result < mod; result is at most 1x mod too large. */ + if (greater_equal_modulus(key, result)) + subtract_modulus(key, result); + + for (i = 0, ptr = inout; i < key->len; i++, ptr++) + *ptr = result[i]; + + return 0; +} -- 2.47.3