soc_rsa.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /* rsa.c
  2. **
  3. ** Copyright 2012, The Android Open Source Project
  4. **
  5. ** Redistribution and use in source and binary forms, with or without
  6. ** modification, are permitted provided that the following conditions are met:
  7. ** * Redistributions of source code must retain the above copyright
  8. ** notice, this list of conditions and the following disclaimer.
  9. ** * Redistributions in binary form must reproduce the above copyright
  10. ** notice, this list of conditions and the following disclaimer in the
  11. ** documentation and/or other materials provided with the distribution.
  12. ** * Neither the name of Google Inc. nor the names of its contributors may
  13. ** be used to endorse or promote products derived from this software
  14. ** without specific prior written permission.
  15. **
  16. ** THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
  17. ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. ** EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <device.h>
  28. #include <soc.h>
  29. #ifndef __ORDER_BIG_ENDIAN__
  30. #define __ORDER_BIG_ENDIAN__ 4321
  31. #endif
  32. #ifndef __ORDER_LITTLE_ENDIAN__
  33. #define __ORDER_LITTLE_ENDIAN__ 1234
  34. #endif
  35. #define KEY_EXPONENT_65537
  36. #define RSANUMBYTES 256 /* 2048 bit key length */
  37. #define RSANUMWORDS (RSANUMBYTES / sizeof(uint32_t))
  38. typedef struct RSAPublicKey {
  39. uint32_t n0inv; /* -1 / n[0] mod 2^32 */
  40. uint32_t n[RSANUMWORDS]; /* modulus as little endian array */
  41. uint32_t rr[RSANUMWORDS]; /* R^2 as little endian array */
  42. } RSAPublicKey;
  43. #define KEY_LEN (4 + 256 + 256)
  44. #define SIG_LEN 256
  45. #define act_writel sys_write32
  46. #define act_readl sys_read32
  47. #define SHA256_DIGEST_SIZE 32
  48. #define CONFIG_SHA256_HARDWARE
  49. #ifdef CONFIG_SHA256_HARDWARE
  50. static __ramfunc unsigned int wait_reg_status(unsigned int reg, unsigned int dat, unsigned int check, unsigned int ms)
  51. {
  52. unsigned int res, t, t_cycle;
  53. unsigned int wait_start = sys_read32(T2_CNT);
  54. res = -1;
  55. t_cycle = ms*(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC/1000);
  56. while (1) {
  57. if((sys_read32(reg) & dat) == check) {
  58. res = 0;
  59. break;
  60. }
  61. t = sys_read32(T2_CNT)-wait_start;
  62. if (t > t_cycle) {
  63. break;
  64. }
  65. }
  66. return res;
  67. }
  68. #define SHA_MODE_SHA_FIRST 5
  69. #define SHA_MODE_SHA_PADDING 4
  70. #define SHA_MODE_SHA_MODE_e 1
  71. #define SHA_MODE_SHA_MODE_SHIFT 0
  72. #define SHA_MODE_SHA_MODE_MASK (0x3<<0)
  73. #define SHA_INFIFOCTL_INFIFOIP 8
  74. #define SHA_INFIFOCTL_INFIFO_ERR 3
  75. #define SHA_INFIFOCTL_INFIFO_FULL 2
  76. #define SHA_INFIFOCTL_INFIFODE 1
  77. #define SHA_INFIFOCTL_INFIFOIE 0
  78. #define SHA_CTRL_SHA_END 31
  79. #define SHA_CTRL_SHA_LEN_ERROR 30
  80. #define SHA_CTRL_SHA_GATE_EN 4
  81. #define SHA_CTRL_SHA_CLK_EN 3
  82. #define SHA_CTRL_SHA_INT_EN 2
  83. #define SHA_CTRL_SHA_RESET 1
  84. #define SHA_CTRL_SHA_EN 0
  85. #define CMU_DEVCLKEN0_SECLKEN 16
  86. #define MRCR0_SEC_RESET 16
  87. #define SHA_MODE_ALG(x) ((x) << SHA_MODE_SHA_MODE_SHIFT)
  88. #define SHA_MODE_ALG_SHA1 SHA_MODE_ALG(0)
  89. #define SHA_MODE_ALG_SHA256 SHA_MODE_ALG(1)
  90. #define SHA_MODE_ALG_SHA224 SHA_MODE_ALG(2)
  91. #define SHA_MODE_ALG_MASK SHA_MODE_ALG(3)
  92. #define RMU_BASE 0x40000000
  93. #define MRCR0 (RMU_BASE+0x00000000)
  94. #define SHA_CTRL (SE_REG_BASE+0x0200)
  95. #define SHA_MODE (SE_REG_BASE+0x0204)
  96. #define SHA_LEN (SE_REG_BASE+0x0208)
  97. #define SHA_INFIFOCTL (SE_REG_BASE+0x020c)
  98. #define SHA_INFIFO (SE_REG_BASE+0x0210)
  99. #define SHA_DATAOUT (SE_REG_BASE+0x0214)
  100. #define SHA_TOTAL_LEN (SE_REG_BASE+0x0218)
  101. static __ramfunc void se_sha_clk_init(void)
  102. {
  103. /* enable SE clock & clear SE reset */
  104. /* enable SE clock & clear SE reset */
  105. act_writel(act_readl(CMU_DEVCLKEN0) | (1 << CMU_DEVCLKEN0_SECLKEN), CMU_DEVCLKEN0);
  106. act_writel(act_readl(MRCR0) | (1 << MRCR0_SEC_RESET), MRCR0);
  107. /* clock source: 24M HOSC */
  108. act_writel(0, CMU_SECCLK);
  109. //act_writel(0x102, CMU_SECCLK); // 0x100 = corepll/1, 0x101 = corepll/2 0x102 = corepll/4 0x103 = corepll/8
  110. }
  111. static __ramfunc int se_sha256_init(void)
  112. {
  113. int err;
  114. se_sha_clk_init();
  115. /* enable SHA clock */
  116. act_writel(1 << SHA_CTRL_SHA_CLK_EN , SHA_CTRL);
  117. /* Se fifo select */
  118. act_writel(2, SE_FIFOCTRL);
  119. /* clear DRQ */
  120. act_writel(0, SHA_INFIFOCTL);
  121. /* reset SHA controller */
  122. act_writel(act_readl(SHA_CTRL) | (1 << SHA_CTRL_SHA_RESET), SHA_CTRL);
  123. /* wait reset finished, timeout: 10ms */
  124. err = wait_reg_status(SHA_CTRL, 1 << SHA_CTRL_SHA_RESET, 0, 10);
  125. if (err) {
  126. return -1;
  127. }
  128. /* must set to 0 before clear SHA_MODE_PADDING_HW */
  129. act_writel(0, SHA_LEN);
  130. /* start data process */
  131. act_writel((1 << SHA_MODE_SHA_FIRST) | SHA_MODE_ALG_SHA256, SHA_MODE);
  132. return 0;
  133. }
  134. #define DMA1CTL (DMA_REG_BASE+0x00000200)
  135. #define DMA1START (DMA_REG_BASE+0x00000204)
  136. #define DMA1SADDR0 (DMA_REG_BASE+0x00000208)
  137. #define DMA1SADDR1 (DMA_REG_BASE+0x0000020c)
  138. #define DMA1DADDR0 (DMA_REG_BASE+0x00000210)
  139. #define DMA1DADDR1 (DMA_REG_BASE+0x00000214)
  140. #define DMA1BC (DMA_REG_BASE+0x00000218)
  141. #define DMA1RC (DMA_REG_BASE+0x0000021c)
  142. #define DMA_MAX_LEN 0x80000
  143. static __ramfunc int se_sha256_update(const void* data, int len, int is_last)
  144. {
  145. int err;
  146. uint32_t tlen, buf_addr;
  147. if (!data || !len)
  148. return -1;
  149. if (is_last) {
  150. act_writel(act_readl(SHA_MODE) | (1 << SHA_MODE_SHA_PADDING), SHA_MODE);
  151. }
  152. act_writel(len, SHA_LEN);
  153. /* enable DRQ */
  154. act_writel(act_readl(SHA_INFIFOCTL) | (1 << SHA_INFIFOCTL_INFIFODE) , SHA_INFIFOCTL);
  155. /* start data process */
  156. act_writel(act_readl(SHA_CTRL) | (1 << SHA_CTRL_SHA_EN) , SHA_CTRL);
  157. /* config dma */
  158. act_writel(0x9100, DMA1CTL);
  159. act_writel(SHA_INFIFO, DMA1DADDR0);
  160. buf_addr = (unsigned int)data;
  161. tlen = len;
  162. while(tlen){
  163. act_writel(buf_addr, DMA1SADDR0);
  164. if(tlen < DMA_MAX_LEN) {
  165. act_writel(tlen, DMA1BC);
  166. act_writel(1, DMA1START);
  167. break;
  168. }else{
  169. act_writel(DMA_MAX_LEN, DMA1BC);
  170. act_writel(1, DMA1START);
  171. buf_addr += DMA_MAX_LEN;
  172. tlen -= DMA_MAX_LEN;
  173. while(act_readl(DMA1START)&0x01){
  174. }
  175. }
  176. }
  177. /* wait sha caculate end */
  178. err = wait_reg_status(SHA_CTRL, 1 << SHA_CTRL_SHA_END, 1 << SHA_CTRL_SHA_END, 100);
  179. if (err) {
  180. act_writel(0, DMA1START);
  181. goto exit;
  182. }
  183. exit:
  184. /* clear SHA pending, ensure controller is disable */
  185. act_writel(act_readl(SHA_CTRL) & ~(1 << SHA_CTRL_SHA_EN), SHA_CTRL);
  186. /* clear first flag */
  187. if (act_readl(SHA_MODE) & (1 << SHA_MODE_SHA_FIRST)) {
  188. act_writel(act_readl(SHA_MODE) & ~(1 << SHA_MODE_SHA_FIRST), SHA_MODE);
  189. }
  190. return err;
  191. }
  192. static __ramfunc void sys_put_le16(uint16_t val, uint8_t dst[2])
  193. {
  194. dst[0] = val;
  195. dst[1] = val >> 8;
  196. }
  197. /**
  198. * @brief Put a 32-bit integer as little-endian to arbitrary location.
  199. *
  200. * Put a 32-bit integer, originally in host endianness, to a
  201. * potentially unaligned memory location in little-endian format.
  202. *
  203. * @param val 32-bit integer in host endianness.
  204. * @param dst Destination memory address to store the result.
  205. */
  206. static __ramfunc void sys_put_le32(uint32_t val, uint8_t dst[4])
  207. {
  208. sys_put_le16(val, dst);
  209. sys_put_le16(val >> 16, &dst[2]);
  210. }
  211. static __ramfunc int se_sha256_final(unsigned char output[32])
  212. {
  213. unsigned int w;
  214. int i;
  215. if (!output)
  216. return -1;
  217. /* read sha values */
  218. for (i = 0; i < SHA256_DIGEST_SIZE / 4; i++) {
  219. w = act_readl(SHA_DATAOUT);
  220. sys_put_le32(w, &output[i * 4]);
  221. }
  222. return 0;
  223. }
  224. __ramfunc const uint8_t* SHA256_hash(const void *input, int len,
  225. uint8_t* digest)
  226. {
  227. if (!input || ((unsigned int)input & 0x3)){
  228. return NULL;
  229. }
  230. se_sha256_init();
  231. se_sha256_update(input, len, 1);
  232. se_sha256_final(digest);
  233. return digest;
  234. }
  235. #else
  236. struct HASH_CTX; // forward decl
  237. typedef struct HASH_VTAB {
  238. void (* const init)(struct HASH_CTX*);
  239. void (* const update)(struct HASH_CTX*, const void*, int);
  240. const uint8_t* (* const final)(struct HASH_CTX*);
  241. const uint8_t* (* const hash)(const void*, int, uint8_t*);
  242. int size;
  243. } HASH_VTAB;
  244. typedef struct HASH_CTX {
  245. HASH_VTAB * f;
  246. uint64_t count;
  247. union {
  248. uint8_t buf[64];
  249. uint32_t buf32[64 / 4];
  250. };
  251. uint32_t state[8]; // upto SHA2
  252. } HASH_CTX;
  253. #define HASH_init(ctx) (ctx)->f->init(ctx)
  254. #define HASH_update(ctx, data, len) (ctx)->f->update(ctx, data, len)
  255. #define HASH_final(ctx) (ctx)->f->final(ctx)
  256. #define HASH_hash(data, len, digest) (ctx)->f->hash(data, len, digest)
  257. #define HASH_size(ctx) (ctx)->f->size
  258. typedef HASH_CTX SHA256_CTX;
  259. void SHA256_init(SHA256_CTX* ctx);
  260. void SHA256_update(SHA256_CTX* ctx, const void* data, int len);
  261. const uint8_t* SHA256_final(SHA256_CTX* ctx);
  262. // Convenience method. Returns digest address.
  263. const uint8_t* SHA256_hash(const void* data, int len, uint8_t* digest);
  264. uint32_t __REV(uint32_t value);
  265. void modpow(void *key_param, void *buffer);
  266. #define ror(value, bits) (((value) >> (bits)) | ((value) << (32 - (bits))))
  267. #define shr(value, bits) ((value) >> (bits))
  268. uint32_t sha_K[64] = {
  269. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  270. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  271. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  272. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  273. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  274. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  275. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  276. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  277. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  278. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  279. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  280. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  281. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  282. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  283. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  284. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  285. };
  286. static __ramfunc void SHA256_Transform(SHA256_CTX* ctx) {
  287. uint32_t W[64];
  288. uint32_t A, B, C, D, E, F, G, H;
  289. #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  290. uint32_t* p32 = ctx->buf32;
  291. #else
  292. uint8_t* p = ctx->buf;
  293. #endif
  294. int t;
  295. for(t = 0; t < 16; ++t) {
  296. #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  297. W[t] = __REV(*p32++);
  298. #elif (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  299. W[t] = *p32++;
  300. #else
  301. uint32_t tmp = *p++ << 24;
  302. tmp |= *p++ << 16;
  303. tmp |= *p++ << 8;
  304. tmp |= *p++;
  305. W[t] = tmp;
  306. #endif
  307. }
  308. for(; t < 64; t++) {
  309. uint32_t s0 = ror(W[t-15], 7) ^ ror(W[t-15], 18) ^ shr(W[t-15], 3);
  310. uint32_t s1 = ror(W[t-2], 17) ^ ror(W[t-2], 19) ^ shr(W[t-2], 10);
  311. W[t] = W[t-16] + s0 + W[t-7] + s1;
  312. }
  313. A = ctx->state[0];
  314. B = ctx->state[1];
  315. C = ctx->state[2];
  316. D = ctx->state[3];
  317. E = ctx->state[4];
  318. F = ctx->state[5];
  319. G = ctx->state[6];
  320. H = ctx->state[7];
  321. for(t = 0; t < 64; t++) {
  322. uint32_t s0 = ror(A, 2) ^ ror(A, 13) ^ ror(A, 22);
  323. uint32_t maj = (A & B) ^ (A & C) ^ (B & C);
  324. uint32_t t2 = s0 + maj;
  325. uint32_t s1 = ror(E, 6) ^ ror(E, 11) ^ ror(E, 25);
  326. uint32_t ch = (E & F) ^ ((~E) & G);
  327. uint32_t t1 = H + s1 + ch + sha_K[t] + W[t];
  328. H = G;
  329. G = F;
  330. F = E;
  331. E = D + t1;
  332. D = C;
  333. C = B;
  334. B = A;
  335. A = t1 + t2;
  336. }
  337. ctx->state[0] += A;
  338. ctx->state[1] += B;
  339. ctx->state[2] += C;
  340. ctx->state[3] += D;
  341. ctx->state[4] += E;
  342. ctx->state[5] += F;
  343. ctx->state[6] += G;
  344. ctx->state[7] += H;
  345. }
  346. static HASH_VTAB SHA256_VTAB = {
  347. SHA256_init,
  348. SHA256_update,
  349. SHA256_final,
  350. SHA256_hash,
  351. SHA256_DIGEST_SIZE
  352. };
  353. __ramfunc void SHA256_init(SHA256_CTX* ctx) {
  354. ctx->f = &SHA256_VTAB;
  355. ctx->state[0] = 0x6a09e667;
  356. ctx->state[1] = 0xbb67ae85;
  357. ctx->state[2] = 0x3c6ef372;
  358. ctx->state[3] = 0xa54ff53a;
  359. ctx->state[4] = 0x510e527f;
  360. ctx->state[5] = 0x9b05688c;
  361. ctx->state[6] = 0x1f83d9ab;
  362. ctx->state[7] = 0x5be0cd19;
  363. ctx->count = 0;
  364. }
  365. __ramfunc void SHA256_update(SHA256_CTX* ctx, const void* data, int len) {
  366. int i = (int) (ctx->count & 63);
  367. const uint8_t* p = (const uint8_t*)data;
  368. ctx->count += len;
  369. #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  370. if (len >= 4 && (i & 0x3) == 0 && ((uint32_t)p & 0x3) == 0) {
  371. const uint32_t *p32 = (const uint32_t *)p;
  372. int k = i / 4;
  373. while (len >= 4) {
  374. len -= 4;
  375. #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  376. ctx->buf32[k++] = *p32++;
  377. #else
  378. ctx->buf32[k++] = __REV(*p32++);
  379. #endif
  380. if (k == 64 / 4) {
  381. SHA256_Transform(ctx);
  382. k = 0;
  383. }
  384. }
  385. i = k * 4;
  386. p = (const uint8_t *)p32;
  387. }
  388. #endif
  389. while (len--) {
  390. ctx->buf[i++] = *p++;
  391. if (i == 64) {
  392. SHA256_Transform(ctx);
  393. i = 0;
  394. }
  395. }
  396. }
  397. __ramfunc const uint8_t* SHA256_final(SHA256_CTX* ctx) {
  398. uint8_t *p = ctx->buf;
  399. uint64_t cnt = ctx->count * 8;
  400. int i;
  401. uint8_t buf[2];
  402. buf[0] = '\x80';
  403. buf[1] = 0;
  404. SHA256_update(ctx, buf, 1);
  405. while ((ctx->count & 63) != 56) {
  406. buf[0] = '\0';
  407. SHA256_update(ctx, buf, 1);
  408. }
  409. for (i = 0; i < 8; ++i) {
  410. uint8_t tmp = (uint8_t) (cnt >> ((7 - i) * 8));//64bit ³ý·¨Óõ½ÁËlibÖеÄ__aeabi_llsr
  411. SHA256_update(ctx, &tmp, 1);
  412. }
  413. for (i = 0; i < 8; i++) {
  414. uint32_t tmp = ctx->state[i];
  415. *p++ = tmp >> 24;
  416. *p++ = tmp >> 16;
  417. *p++ = tmp >> 8;
  418. *p++ = tmp >> 0;
  419. }
  420. return ctx->buf;
  421. }
  422. /* Convenience function */
  423. __ramfunc const uint8_t* SHA256_hash(const void* data, int len, uint8_t* digest) {
  424. SHA256_CTX ctx;
  425. int i;
  426. SHA256_init(&ctx);
  427. SHA256_update(&ctx, data, len);
  428. //memcpy(digest, SHA256_final(&ctx), SHA256_DIGEST_SIZE);
  429. SHA256_final(&ctx);
  430. for(i = 0; i < SHA256_DIGEST_SIZE; i++)
  431. digest[i] = ctx.buf[i];
  432. return digest;
  433. }
  434. __ramfunc uint32_t __REV(uint32_t value)
  435. {
  436. //return __builtin_bswap32(value);
  437. return ((uint32_t) ((((value) >> 24) & 0xff) | \
  438. (((value) >> 8) & 0xff00) | \
  439. (((value) & 0xff00) << 8) | \
  440. (((value) & 0xff) << 24)));
  441. }
  442. #endif // sha256 config
  443. // SHA-256 of PKCS1.5 signature sha256_padding for 2048 bit, as above.
  444. // At the location of the bytes of the hash all 00 are hashed.
  445. static uint8_t kExpectedPadSha256Rsa2048[SHA256_DIGEST_SIZE] = {
  446. 0xab, 0x28, 0x8d, 0x8a, 0xd7, 0xd9, 0x59, 0x92,
  447. 0xba, 0xcc, 0xf8, 0x67, 0x20, 0xe1, 0x15, 0x2e,
  448. 0x39, 0x8d, 0x80, 0x36, 0xd6, 0x6f, 0xf0, 0xfd,
  449. 0x90, 0xe8, 0x7d, 0x8b, 0xe1, 0x7c, 0x87, 0x59,
  450. };
  451. void modpow(void *key_param, void *buffer);
  452. // Verify a 2048-bit RSA PKCS1.5 signature against an expected hash.
  453. //
  454. // Returns 1 on successful verification, 0 on failure.
  455. __ramfunc int RSA_verify(const RSAPublicKey *key,
  456. const uint8_t *signature,
  457. const uint32_t len,
  458. const uint8_t *msg,
  459. const uint32_t msg_len) {
  460. uint8_t buf[RSANUMBYTES];
  461. int i, j;
  462. const uint8_t* padding_hash;
  463. unsigned char hash[SHA256_DIGEST_SIZE];
  464. unsigned int hash_len = sizeof(hash);
  465. if (key == NULL || signature == NULL || msg == NULL) {
  466. return 0;
  467. }
  468. if (msg_len == 0) {
  469. return 0;
  470. }
  471. if (len != sizeof(buf)) {
  472. return 0; // Wrong input length.
  473. }
  474. for (i = 0; i < len; ++i) { // Copy input to local workspace.
  475. buf[i] = signature[i];
  476. }
  477. // In-place exponentiation.
  478. modpow((void *)key, buf);
  479. SHA256_hash(msg, msg_len, hash);//caclulate_image_hash
  480. // Xor sha portion, so it all becomes 00 iff equal.
  481. for (i = len - hash_len, j = 0; i < len; ++i, ++j) {
  482. buf[i] ^= hash[j];
  483. }
  484. // Hash resulting buf, in-place.
  485. switch (hash_len) {
  486. case SHA256_DIGEST_SIZE:
  487. padding_hash = kExpectedPadSha256Rsa2048;
  488. SHA256_hash(buf, len, buf);
  489. break;
  490. default:
  491. return 0;
  492. }
  493. // Compare against expected hash value.
  494. for (i = 0; i < hash_len; ++i) {
  495. if (buf[i] != padding_hash[i]) {
  496. return 0;
  497. }
  498. }
  499. return 1; // All checked out OK.
  500. }
  501. // a[] -= mod
  502. static __ramfunc void subM(const RSAPublicKey* key,
  503. uint32_t* a) {
  504. int64_t A = 0;
  505. int i;
  506. for (i = 0; i < RSANUMWORDS; ++i) {
  507. A += (uint64_t)a[i] - key->n[i];
  508. a[i] = (uint32_t)A;
  509. A >>= 32;
  510. }
  511. }
  512. // return a[] >= mod
  513. static __ramfunc int geM(const RSAPublicKey* key,
  514. const uint32_t* a) {
  515. int i;
  516. for (i = RSANUMWORDS; i;) {
  517. --i;
  518. if (a[i] < key->n[i]) return 0;
  519. if (a[i] > key->n[i]) return 1;
  520. }
  521. return 1; // equal
  522. }
  523. // montgomery c[] += a * b[] / R % mod
  524. static __ramfunc void montMulAdd(const RSAPublicKey* key,
  525. uint32_t* c,
  526. const uint32_t a,
  527. const uint32_t* b) {
  528. uint64_t A = (uint64_t)a * b[0] + c[0];
  529. uint32_t d0 = (uint32_t)A * key->n0inv;
  530. uint64_t B = (uint64_t)d0 * key->n[0] + (uint32_t)A;
  531. int i;
  532. for (i = 1; i < RSANUMWORDS; ++i) {
  533. A = (A >> 32) + (uint64_t)a * b[i] + c[i];
  534. B = (B >> 32) + (uint64_t)d0 * key->n[i] + (uint32_t)A;
  535. c[i - 1] = (uint32_t)B;
  536. }
  537. A = (A >> 32) + (B >> 32);
  538. c[i - 1] = (uint32_t)A;
  539. if (A >> 32) {
  540. subM(key, c);
  541. }
  542. }
  543. // montgomery c[] = a[] * b[] / R % mod
  544. static __ramfunc void montMul(const RSAPublicKey* key,
  545. uint32_t* c,
  546. const uint32_t* a,
  547. const uint32_t* b) {
  548. int i;
  549. for (i = 0; i < RSANUMWORDS; ++i) {
  550. c[i] = 0;
  551. }
  552. for (i = 0; i < RSANUMWORDS; ++i) {
  553. montMulAdd(key, c, a[i], b);
  554. }
  555. }
  556. // In-place public exponentiation.
  557. // Input and output big-endian byte array in inout.
  558. __ramfunc void modpow(void *key_param, void *buffer) {
  559. uint32_t a[RSANUMWORDS];
  560. uint32_t aR[RSANUMWORDS];
  561. uint32_t aaR[RSANUMWORDS];
  562. uint32_t* aaa = 0;
  563. int i;
  564. RSAPublicKey* key = (RSAPublicKey*)key_param;
  565. uint8_t* inout =(uint8_t *)buffer;
  566. // Convert from big endian byte array to little endian word array.
  567. for (i = 0; i < RSANUMWORDS; ++i) {
  568. uint32_t tmp =
  569. (inout[((RSANUMWORDS - 1 - i) * 4) + 0] << 24) |
  570. (inout[((RSANUMWORDS - 1 - i) * 4) + 1] << 16) |
  571. (inout[((RSANUMWORDS - 1 - i) * 4) + 2] << 8) |
  572. (inout[((RSANUMWORDS - 1 - i) * 4) + 3] << 0);
  573. a[i] = tmp;
  574. }
  575. #ifdef KEY_EXPONENT_65537
  576. aaa = aaR; // Re-use location.
  577. montMul(key, aR, a, key->rr); // aR = a * RR / R mod M
  578. for (i = 0; i < 16; i += 2) {
  579. montMul(key, aaR, aR, aR); // aaR = aR * aR / R mod M
  580. montMul(key, aR, aaR, aaR); // aR = aaR * aaR / R mod M
  581. }
  582. montMul(key, aaa, aR, a); // aaa = aR * a / R mod M
  583. #elif defined(KEY_EXPONENT_3)
  584. aaa = aR; // Re-use location.
  585. montMul(key, aR, a, key->rr); /* aR = a * RR / R mod M */
  586. montMul(key, aaR, aR, aR); /* aaR = aR * aR / R mod M */
  587. montMul(key, aaa, aaR, a); /* aaa = aaR * a / R mod M */
  588. #else
  589. #error "No valid KEY_EXPONENT_XXX defined"
  590. #endif
  591. // Make sure aaa < mod; aaa is at most 1x mod too large.
  592. if (geM(key, aaa)) {
  593. subM(key, aaa);
  594. }
  595. // Convert to bigendian byte array
  596. for (i = RSANUMWORDS - 1; i >= 0; --i) {
  597. uint32_t tmp = aaa[i];
  598. *inout++ = tmp >> 24;
  599. *inout++ = tmp >> 16;
  600. *inout++ = tmp >> 8;
  601. *inout++ = tmp >> 0;
  602. }
  603. }
  604. __ramfunc int verify_signature(const unsigned char *key, const unsigned char *sig, const unsigned char *data, unsigned int len)
  605. {
  606. int ret;
  607. ret = RSA_verify((const RSAPublicKey *)key, sig, SIG_LEN, data, len);
  608. if (ret == 0) {
  609. return 1;
  610. }
  611. return 0;
  612. }