You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rsa.c 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <stdarg.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <ipxe/asn1.h>
  26. #include <ipxe/crypto.h>
  27. #include <ipxe/bigint.h>
  28. #include <ipxe/random_nz.h>
  29. #include <ipxe/md5.h>
  30. #include <ipxe/sha1.h>
  31. #include <ipxe/sha256.h>
  32. #include <ipxe/rsa.h>
  33. /** @file
  34. *
  35. * RSA public-key cryptography
  36. *
  37. * RSA is documented in RFC 3447.
  38. */
  39. /* Disambiguate the various error causes */
  40. #define EACCES_VERIFY \
  41. __einfo_error ( EINFO_EACCES_VERIFY )
  42. #define EINFO_EACCES_VERIFY \
  43. __einfo_uniqify ( EINFO_EACCES, 0x01, "RSA signature incorrect" )
  44. /** "rsaEncryption" object identifier */
  45. static uint8_t oid_rsa_encryption[] = { ASN1_OID_RSAENCRYPTION };
  46. /** "md5WithRSAEncryption" object identifier */
  47. static uint8_t oid_md5_with_rsa_encryption[] =
  48. { ASN1_OID_MD5WITHRSAENCRYPTION };
  49. /** "sha1WithRSAEncryption" object identifier */
  50. static uint8_t oid_sha1_with_rsa_encryption[] =
  51. { ASN1_OID_SHA1WITHRSAENCRYPTION };
  52. /** "sha256WithRSAEncryption" object identifier */
  53. static uint8_t oid_sha256_with_rsa_encryption[] =
  54. { ASN1_OID_SHA256WITHRSAENCRYPTION };
  55. /** "rsaEncryption" OID-identified algorithm */
  56. struct asn1_algorithm rsa_encryption_algorithm __asn1_algorithm = {
  57. .name = "rsaEncryption",
  58. .pubkey = &rsa_algorithm,
  59. .digest = NULL,
  60. .oid = ASN1_OID_CURSOR ( oid_rsa_encryption ),
  61. };
  62. /** "md5WithRSAEncryption" OID-identified algorithm */
  63. struct asn1_algorithm md5_with_rsa_encryption_algorithm __asn1_algorithm = {
  64. .name = "md5WithRSAEncryption",
  65. .pubkey = &rsa_algorithm,
  66. .digest = &md5_algorithm,
  67. .oid = ASN1_OID_CURSOR ( oid_md5_with_rsa_encryption ),
  68. };
  69. /** "sha1WithRSAEncryption" OID-identified algorithm */
  70. struct asn1_algorithm sha1_with_rsa_encryption_algorithm __asn1_algorithm = {
  71. .name = "sha1WithRSAEncryption",
  72. .pubkey = &rsa_algorithm,
  73. .digest = &sha1_algorithm,
  74. .oid = ASN1_OID_CURSOR ( oid_sha1_with_rsa_encryption ),
  75. };
  76. /** "sha256WithRSAEncryption" OID-identified algorithm */
  77. struct asn1_algorithm sha256_with_rsa_encryption_algorithm __asn1_algorithm = {
  78. .name = "sha256WithRSAEncryption",
  79. .pubkey = &rsa_algorithm,
  80. .digest = &sha256_algorithm,
  81. .oid = ASN1_OID_CURSOR ( oid_sha256_with_rsa_encryption ),
  82. };
  83. /** MD5 digestInfo prefix */
  84. static const uint8_t rsa_md5_prefix_data[] =
  85. { RSA_DIGESTINFO_PREFIX ( MD5_DIGEST_SIZE, ASN1_OID_MD5 ) };
  86. /** SHA-1 digestInfo prefix */
  87. static const uint8_t rsa_sha1_prefix_data[] =
  88. { RSA_DIGESTINFO_PREFIX ( SHA1_DIGEST_SIZE, ASN1_OID_SHA1 ) };
  89. /** SHA-256 digestInfo prefix */
  90. static const uint8_t rsa_sha256_prefix_data[] =
  91. { RSA_DIGESTINFO_PREFIX ( SHA256_DIGEST_SIZE, ASN1_OID_SHA256 ) };
  92. /** MD5 digestInfo prefix */
  93. struct rsa_digestinfo_prefix rsa_md5_prefix __rsa_digestinfo_prefix = {
  94. .digest = &md5_algorithm,
  95. .data = rsa_md5_prefix_data,
  96. .len = sizeof ( rsa_md5_prefix_data ),
  97. };
  98. /** SHA-1 digestInfo prefix */
  99. struct rsa_digestinfo_prefix rsa_sha1_prefix __rsa_digestinfo_prefix = {
  100. .digest = &sha1_algorithm,
  101. .data = rsa_sha1_prefix_data,
  102. .len = sizeof ( rsa_sha1_prefix_data ),
  103. };
  104. /** SHA-256 digestInfo prefix */
  105. struct rsa_digestinfo_prefix rsa_sha256_prefix __rsa_digestinfo_prefix = {
  106. .digest = &sha256_algorithm,
  107. .data = rsa_sha256_prefix_data,
  108. .len = sizeof ( rsa_sha256_prefix_data ),
  109. };
  110. /**
  111. * Identify RSA prefix
  112. *
  113. * @v digest Digest algorithm
  114. * @ret prefix RSA prefix, or NULL
  115. */
  116. static struct rsa_digestinfo_prefix *
  117. rsa_find_prefix ( struct digest_algorithm *digest ) {
  118. struct rsa_digestinfo_prefix *prefix;
  119. for_each_table_entry ( prefix, RSA_DIGESTINFO_PREFIXES ) {
  120. if ( prefix->digest == digest )
  121. return prefix;
  122. }
  123. return NULL;
  124. }
  125. /**
  126. * Free RSA dynamic storage
  127. *
  128. * @v context RSA context
  129. */
  130. static void rsa_free ( struct rsa_context *context ) {
  131. free ( context->dynamic );
  132. context->dynamic = NULL;
  133. }
  134. /**
  135. * Allocate RSA dynamic storage
  136. *
  137. * @v context RSA context
  138. * @v modulus_len Modulus length
  139. * @v exponent_len Exponent length
  140. * @ret rc Return status code
  141. */
  142. static int rsa_alloc ( struct rsa_context *context, size_t modulus_len,
  143. size_t exponent_len ) {
  144. unsigned int size = bigint_required_size ( modulus_len );
  145. unsigned int exponent_size = bigint_required_size ( exponent_len );
  146. bigint_t ( size ) *modulus;
  147. bigint_t ( exponent_size ) *exponent;
  148. size_t tmp_len = bigint_mod_exp_tmp_len ( modulus, exponent );
  149. struct {
  150. bigint_t ( size ) modulus;
  151. bigint_t ( exponent_size ) exponent;
  152. bigint_t ( size ) input;
  153. bigint_t ( size ) output;
  154. uint8_t tmp[tmp_len];
  155. } __attribute__ (( packed )) *dynamic;
  156. /* Free any existing dynamic storage */
  157. rsa_free ( context );
  158. /* Allocate dynamic storage */
  159. dynamic = malloc ( sizeof ( *dynamic ) );
  160. if ( ! dynamic )
  161. return -ENOMEM;
  162. /* Assign dynamic storage */
  163. context->dynamic = dynamic;
  164. context->modulus0 = &dynamic->modulus.element[0];
  165. context->size = size;
  166. context->max_len = modulus_len;
  167. context->exponent0 = &dynamic->exponent.element[0];
  168. context->exponent_size = exponent_size;
  169. context->input0 = &dynamic->input.element[0];
  170. context->output0 = &dynamic->output.element[0];
  171. context->tmp = &dynamic->tmp;
  172. return 0;
  173. }
  174. /**
  175. * Parse RSA integer
  176. *
  177. * @v integer Integer to fill in
  178. * @v raw ASN.1 cursor
  179. * @ret rc Return status code
  180. */
  181. static int rsa_parse_integer ( struct asn1_cursor *integer,
  182. const struct asn1_cursor *raw ) {
  183. /* Enter integer */
  184. memcpy ( integer, raw, sizeof ( *integer ) );
  185. asn1_enter ( integer, ASN1_INTEGER );
  186. /* Skip initial sign byte if applicable */
  187. if ( ( integer->len > 1 ) &&
  188. ( *( ( uint8_t * ) integer->data ) == 0x00 ) ) {
  189. integer->data++;
  190. integer->len--;
  191. }
  192. /* Fail if cursor or integer are invalid */
  193. if ( ! integer->len )
  194. return -EINVAL;
  195. return 0;
  196. }
  197. /**
  198. * Parse RSA modulus and exponent
  199. *
  200. * @v modulus Modulus to fill in
  201. * @v exponent Exponent to fill in
  202. * @v raw ASN.1 cursor
  203. * @ret rc Return status code
  204. */
  205. static int rsa_parse_mod_exp ( struct asn1_cursor *modulus,
  206. struct asn1_cursor *exponent,
  207. const struct asn1_cursor *raw ) {
  208. struct asn1_bit_string bits;
  209. struct asn1_cursor cursor;
  210. int is_private;
  211. int rc;
  212. /* Enter subjectPublicKeyInfo/RSAPrivateKey */
  213. memcpy ( &cursor, raw, sizeof ( cursor ) );
  214. asn1_enter ( &cursor, ASN1_SEQUENCE );
  215. /* Determine key format */
  216. if ( asn1_type ( &cursor ) == ASN1_INTEGER ) {
  217. /* Private key */
  218. is_private = 1;
  219. /* Skip version */
  220. asn1_skip_any ( &cursor );
  221. } else {
  222. /* Public key */
  223. is_private = 0;
  224. /* Skip algorithm */
  225. asn1_skip ( &cursor, ASN1_SEQUENCE );
  226. /* Enter subjectPublicKey */
  227. if ( ( rc = asn1_integral_bit_string ( &cursor, &bits ) ) != 0 )
  228. return rc;
  229. cursor.data = bits.data;
  230. cursor.len = bits.len;
  231. /* Enter RSAPublicKey */
  232. asn1_enter ( &cursor, ASN1_SEQUENCE );
  233. }
  234. /* Extract modulus */
  235. if ( ( rc = rsa_parse_integer ( modulus, &cursor ) ) != 0 )
  236. return rc;
  237. asn1_skip_any ( &cursor );
  238. /* Skip public exponent, if applicable */
  239. if ( is_private )
  240. asn1_skip ( &cursor, ASN1_INTEGER );
  241. /* Extract publicExponent/privateExponent */
  242. if ( ( rc = rsa_parse_integer ( exponent, &cursor ) ) != 0 )
  243. return rc;
  244. return 0;
  245. }
  246. /**
  247. * Initialise RSA cipher
  248. *
  249. * @v ctx RSA context
  250. * @v key Key
  251. * @v key_len Length of key
  252. * @ret rc Return status code
  253. */
  254. static int rsa_init ( void *ctx, const void *key, size_t key_len ) {
  255. struct rsa_context *context = ctx;
  256. struct asn1_cursor modulus;
  257. struct asn1_cursor exponent;
  258. struct asn1_cursor cursor;
  259. int rc;
  260. /* Initialise context */
  261. memset ( context, 0, sizeof ( *context ) );
  262. /* Initialise cursor */
  263. cursor.data = key;
  264. cursor.len = key_len;
  265. /* Parse modulus and exponent */
  266. if ( ( rc = rsa_parse_mod_exp ( &modulus, &exponent, &cursor ) ) != 0 ){
  267. DBGC ( context, "RSA %p invalid modulus/exponent:\n", context );
  268. DBGC_HDA ( context, 0, cursor.data, cursor.len );
  269. goto err_parse;
  270. }
  271. DBGC ( context, "RSA %p modulus:\n", context );
  272. DBGC_HDA ( context, 0, modulus.data, modulus.len );
  273. DBGC ( context, "RSA %p exponent:\n", context );
  274. DBGC_HDA ( context, 0, exponent.data, exponent.len );
  275. /* Allocate dynamic storage */
  276. if ( ( rc = rsa_alloc ( context, modulus.len, exponent.len ) ) != 0 )
  277. goto err_alloc;
  278. /* Construct big integers */
  279. bigint_init ( ( ( bigint_t ( context->size ) * ) context->modulus0 ),
  280. modulus.data, modulus.len );
  281. bigint_init ( ( ( bigint_t ( context->exponent_size ) * )
  282. context->exponent0 ), exponent.data, exponent.len );
  283. return 0;
  284. rsa_free ( context );
  285. err_alloc:
  286. err_parse:
  287. return rc;
  288. }
  289. /**
  290. * Calculate RSA maximum output length
  291. *
  292. * @v ctx RSA context
  293. * @ret max_len Maximum output length
  294. */
  295. static size_t rsa_max_len ( void *ctx ) {
  296. struct rsa_context *context = ctx;
  297. return context->max_len;
  298. }
  299. /**
  300. * Perform RSA cipher operation
  301. *
  302. * @v context RSA context
  303. * @v in Input buffer
  304. * @v out Output buffer
  305. */
  306. static void rsa_cipher ( struct rsa_context *context,
  307. const void *in, void *out ) {
  308. bigint_t ( context->size ) *input = ( ( void * ) context->input0 );
  309. bigint_t ( context->size ) *output = ( ( void * ) context->output0 );
  310. bigint_t ( context->size ) *modulus = ( ( void * ) context->modulus0 );
  311. bigint_t ( context->exponent_size ) *exponent =
  312. ( ( void * ) context->exponent0 );
  313. /* Initialise big integer */
  314. bigint_init ( input, in, context->max_len );
  315. /* Perform modular exponentiation */
  316. bigint_mod_exp ( input, modulus, exponent, output, context->tmp );
  317. /* Copy out result */
  318. bigint_done ( output, out, context->max_len );
  319. }
  320. /**
  321. * Encrypt using RSA
  322. *
  323. * @v ctx RSA context
  324. * @v plaintext Plaintext
  325. * @v plaintext_len Length of plaintext
  326. * @v ciphertext Ciphertext
  327. * @ret ciphertext_len Length of ciphertext, or negative error
  328. */
  329. static int rsa_encrypt ( void *ctx, const void *plaintext,
  330. size_t plaintext_len, void *ciphertext ) {
  331. struct rsa_context *context = ctx;
  332. void *temp;
  333. uint8_t *encoded;
  334. size_t max_len = ( context->max_len - 11 );
  335. size_t random_nz_len = ( max_len - plaintext_len + 8 );
  336. int rc;
  337. /* Sanity check */
  338. if ( plaintext_len > max_len ) {
  339. DBGC ( context, "RSA %p plaintext too long (%zd bytes, max "
  340. "%zd)\n", context, plaintext_len, max_len );
  341. return -ERANGE;
  342. }
  343. DBGC ( context, "RSA %p encrypting:\n", context );
  344. DBGC_HDA ( context, 0, plaintext, plaintext_len );
  345. /* Construct encoded message (using the big integer output
  346. * buffer as temporary storage)
  347. */
  348. temp = context->output0;
  349. encoded = temp;
  350. encoded[0] = 0x00;
  351. encoded[1] = 0x02;
  352. if ( ( rc = get_random_nz ( &encoded[2], random_nz_len ) ) != 0 ) {
  353. DBGC ( context, "RSA %p could not generate random data: %s\n",
  354. context, strerror ( rc ) );
  355. return rc;
  356. }
  357. encoded[ 2 + random_nz_len ] = 0x00;
  358. memcpy ( &encoded[ context->max_len - plaintext_len ],
  359. plaintext, plaintext_len );
  360. /* Encipher the encoded message */
  361. rsa_cipher ( context, encoded, ciphertext );
  362. DBGC ( context, "RSA %p encrypted:\n", context );
  363. DBGC_HDA ( context, 0, ciphertext, context->max_len );
  364. return context->max_len;
  365. }
  366. /**
  367. * Decrypt using RSA
  368. *
  369. * @v ctx RSA context
  370. * @v ciphertext Ciphertext
  371. * @v ciphertext_len Ciphertext length
  372. * @v plaintext Plaintext
  373. * @ret plaintext_len Plaintext length, or negative error
  374. */
  375. static int rsa_decrypt ( void *ctx, const void *ciphertext,
  376. size_t ciphertext_len, void *plaintext ) {
  377. struct rsa_context *context = ctx;
  378. void *temp;
  379. uint8_t *encoded;
  380. uint8_t *end;
  381. uint8_t *zero;
  382. uint8_t *start;
  383. size_t plaintext_len;
  384. /* Sanity check */
  385. if ( ciphertext_len != context->max_len ) {
  386. DBGC ( context, "RSA %p ciphertext incorrect length (%zd "
  387. "bytes, should be %zd)\n",
  388. context, ciphertext_len, context->max_len );
  389. return -ERANGE;
  390. }
  391. DBGC ( context, "RSA %p decrypting:\n", context );
  392. DBGC_HDA ( context, 0, ciphertext, ciphertext_len );
  393. /* Decipher the message (using the big integer input buffer as
  394. * temporary storage)
  395. */
  396. temp = context->input0;
  397. encoded = temp;
  398. rsa_cipher ( context, ciphertext, encoded );
  399. /* Parse the message */
  400. end = ( encoded + context->max_len );
  401. if ( ( encoded[0] != 0x00 ) || ( encoded[1] != 0x02 ) )
  402. goto invalid;
  403. zero = memchr ( &encoded[2], 0, ( end - &encoded[2] ) );
  404. if ( ! zero )
  405. goto invalid;
  406. start = ( zero + 1 );
  407. plaintext_len = ( end - start );
  408. /* Copy out message */
  409. memcpy ( plaintext, start, plaintext_len );
  410. DBGC ( context, "RSA %p decrypted:\n", context );
  411. DBGC_HDA ( context, 0, plaintext, plaintext_len );
  412. return plaintext_len;
  413. invalid:
  414. DBGC ( context, "RSA %p invalid decrypted message:\n", context );
  415. DBGC_HDA ( context, 0, encoded, context->max_len );
  416. return -EINVAL;
  417. }
  418. /**
  419. * Encode RSA digest
  420. *
  421. * @v context RSA context
  422. * @v digest Digest algorithm
  423. * @v value Digest value
  424. * @v encoded Encoded digest
  425. * @ret rc Return status code
  426. */
  427. static int rsa_encode_digest ( struct rsa_context *context,
  428. struct digest_algorithm *digest,
  429. const void *value, void *encoded ) {
  430. struct rsa_digestinfo_prefix *prefix;
  431. size_t digest_len = digest->digestsize;
  432. uint8_t *temp = encoded;
  433. size_t digestinfo_len;
  434. size_t max_len;
  435. size_t pad_len;
  436. /* Identify prefix */
  437. prefix = rsa_find_prefix ( digest );
  438. if ( ! prefix ) {
  439. DBGC ( context, "RSA %p has no prefix for %s\n",
  440. context, digest->name );
  441. return -ENOTSUP;
  442. }
  443. digestinfo_len = ( prefix->len + digest_len );
  444. /* Sanity check */
  445. max_len = ( context->max_len - 11 );
  446. if ( digestinfo_len > max_len ) {
  447. DBGC ( context, "RSA %p %s digestInfo too long (%zd bytes, max"
  448. "%zd)\n",
  449. context, digest->name, digestinfo_len, max_len );
  450. return -ERANGE;
  451. }
  452. DBGC ( context, "RSA %p encoding %s digest:\n",
  453. context, digest->name );
  454. DBGC_HDA ( context, 0, value, digest_len );
  455. /* Construct encoded message */
  456. *(temp++) = 0x00;
  457. *(temp++) = 0x01;
  458. pad_len = ( max_len - digestinfo_len + 8 );
  459. memset ( temp, 0xff, pad_len );
  460. temp += pad_len;
  461. *(temp++) = 0x00;
  462. memcpy ( temp, prefix->data, prefix->len );
  463. temp += prefix->len;
  464. memcpy ( temp, value, digest_len );
  465. temp += digest_len;
  466. assert ( temp == ( encoded + context->max_len ) );
  467. DBGC ( context, "RSA %p encoded %s digest:\n", context, digest->name );
  468. DBGC_HDA ( context, 0, encoded, context->max_len );
  469. return 0;
  470. }
  471. /**
  472. * Sign digest value using RSA
  473. *
  474. * @v ctx RSA context
  475. * @v digest Digest algorithm
  476. * @v value Digest value
  477. * @v signature Signature
  478. * @ret signature_len Signature length, or negative error
  479. */
  480. static int rsa_sign ( void *ctx, struct digest_algorithm *digest,
  481. const void *value, void *signature ) {
  482. struct rsa_context *context = ctx;
  483. void *temp;
  484. int rc;
  485. DBGC ( context, "RSA %p signing %s digest:\n", context, digest->name );
  486. DBGC_HDA ( context, 0, value, digest->digestsize );
  487. /* Encode digest (using the big integer output buffer as
  488. * temporary storage)
  489. */
  490. temp = context->output0;
  491. if ( ( rc = rsa_encode_digest ( context, digest, value, temp ) ) != 0 )
  492. return rc;
  493. /* Encipher the encoded digest */
  494. rsa_cipher ( context, temp, signature );
  495. DBGC ( context, "RSA %p signed %s digest:\n", context, digest->name );
  496. DBGC_HDA ( context, 0, signature, context->max_len );
  497. return context->max_len;
  498. }
  499. /**
  500. * Verify signed digest value using RSA
  501. *
  502. * @v ctx RSA context
  503. * @v digest Digest algorithm
  504. * @v value Digest value
  505. * @v signature Signature
  506. * @v signature_len Signature length
  507. * @ret rc Return status code
  508. */
  509. static int rsa_verify ( void *ctx, struct digest_algorithm *digest,
  510. const void *value, const void *signature,
  511. size_t signature_len ) {
  512. struct rsa_context *context = ctx;
  513. void *temp;
  514. void *expected;
  515. void *actual;
  516. int rc;
  517. /* Sanity check */
  518. if ( signature_len != context->max_len ) {
  519. DBGC ( context, "RSA %p signature incorrect length (%zd "
  520. "bytes, should be %zd)\n",
  521. context, signature_len, context->max_len );
  522. return -ERANGE;
  523. }
  524. DBGC ( context, "RSA %p verifying %s digest:\n",
  525. context, digest->name );
  526. DBGC_HDA ( context, 0, value, digest->digestsize );
  527. DBGC_HDA ( context, 0, signature, signature_len );
  528. /* Decipher the signature (using the big integer input buffer
  529. * as temporary storage)
  530. */
  531. temp = context->input0;
  532. expected = temp;
  533. rsa_cipher ( context, signature, expected );
  534. DBGC ( context, "RSA %p deciphered signature:\n", context );
  535. DBGC_HDA ( context, 0, expected, context->max_len );
  536. /* Encode digest (using the big integer output buffer as
  537. * temporary storage)
  538. */
  539. temp = context->output0;
  540. actual = temp;
  541. if ( ( rc = rsa_encode_digest ( context, digest, value, actual ) ) !=0 )
  542. return rc;
  543. /* Verify the signature */
  544. if ( memcmp ( actual, expected, context->max_len ) != 0 ) {
  545. DBGC ( context, "RSA %p signature verification failed\n",
  546. context );
  547. return -EACCES_VERIFY;
  548. }
  549. DBGC ( context, "RSA %p signature verified successfully\n", context );
  550. return 0;
  551. }
  552. /**
  553. * Finalise RSA cipher
  554. *
  555. * @v ctx RSA context
  556. */
  557. static void rsa_final ( void *ctx ) {
  558. struct rsa_context *context = ctx;
  559. rsa_free ( context );
  560. }
  561. /**
  562. * Check for matching RSA public/private key pair
  563. *
  564. * @v private_key Private key
  565. * @v private_key_len Private key length
  566. * @v public_key Public key
  567. * @v public_key_len Public key length
  568. * @ret rc Return status code
  569. */
  570. static int rsa_match ( const void *private_key, size_t private_key_len,
  571. const void *public_key, size_t public_key_len ) {
  572. struct asn1_cursor private_modulus;
  573. struct asn1_cursor private_exponent;
  574. struct asn1_cursor private_cursor;
  575. struct asn1_cursor public_modulus;
  576. struct asn1_cursor public_exponent;
  577. struct asn1_cursor public_cursor;
  578. int rc;
  579. /* Initialise cursors */
  580. private_cursor.data = private_key;
  581. private_cursor.len = private_key_len;
  582. public_cursor.data = public_key;
  583. public_cursor.len = public_key_len;
  584. /* Parse moduli and exponents */
  585. if ( ( rc = rsa_parse_mod_exp ( &private_modulus, &private_exponent,
  586. &private_cursor ) ) != 0 )
  587. return rc;
  588. if ( ( rc = rsa_parse_mod_exp ( &public_modulus, &public_exponent,
  589. &public_cursor ) ) != 0 )
  590. return rc;
  591. /* Compare moduli */
  592. if ( asn1_compare ( &private_modulus, &public_modulus ) != 0 )
  593. return -ENOTTY;
  594. return 0;
  595. }
  596. /** RSA public-key algorithm */
  597. struct pubkey_algorithm rsa_algorithm = {
  598. .name = "rsa",
  599. .ctxsize = sizeof ( struct rsa_context ),
  600. .init = rsa_init,
  601. .max_len = rsa_max_len,
  602. .encrypt = rsa_encrypt,
  603. .decrypt = rsa_decrypt,
  604. .sign = rsa_sign,
  605. .verify = rsa_verify,
  606. .final = rsa_final,
  607. .match = rsa_match,
  608. };