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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 context RSA context
  178. * @v integer Integer to fill in
  179. * @v raw ASN.1 cursor
  180. * @ret rc Return status code
  181. */
  182. static int rsa_parse_integer ( struct rsa_context *context,
  183. struct asn1_cursor *integer,
  184. const struct asn1_cursor *raw ) {
  185. /* Enter integer */
  186. memcpy ( integer, raw, sizeof ( *integer ) );
  187. asn1_enter ( integer, ASN1_INTEGER );
  188. /* Skip initial sign byte if applicable */
  189. if ( ( integer->len > 1 ) &&
  190. ( *( ( uint8_t * ) integer->data ) == 0x00 ) ) {
  191. integer->data++;
  192. integer->len--;
  193. }
  194. /* Fail if cursor or integer are invalid */
  195. if ( ! integer->len ) {
  196. DBGC ( context, "RSA %p invalid integer:\n", context );
  197. DBGC_HDA ( context, 0, raw->data, raw->len );
  198. return -EINVAL;
  199. }
  200. return 0;
  201. }
  202. /**
  203. * Initialise RSA cipher
  204. *
  205. * @v ctx RSA context
  206. * @v key Key
  207. * @v key_len Length of key
  208. * @ret rc Return status code
  209. */
  210. static int rsa_init ( void *ctx, const void *key, size_t key_len ) {
  211. struct rsa_context *context = ctx;
  212. struct asn1_bit_string bits;
  213. struct asn1_cursor modulus;
  214. struct asn1_cursor exponent;
  215. struct asn1_cursor cursor;
  216. int is_private;
  217. int rc;
  218. /* Initialise context */
  219. memset ( context, 0, sizeof ( *context ) );
  220. /* Initialise cursor */
  221. cursor.data = key;
  222. cursor.len = key_len;
  223. /* Enter subjectPublicKeyInfo/RSAPrivateKey */
  224. asn1_enter ( &cursor, ASN1_SEQUENCE );
  225. /* Determine key format */
  226. if ( asn1_type ( &cursor ) == ASN1_INTEGER ) {
  227. /* Private key */
  228. is_private = 1;
  229. /* Skip version */
  230. asn1_skip_any ( &cursor );
  231. } else {
  232. /* Public key */
  233. is_private = 0;
  234. /* Skip algorithm */
  235. asn1_skip ( &cursor, ASN1_SEQUENCE );
  236. /* Enter subjectPublicKey */
  237. if ( ( rc = asn1_integral_bit_string ( &cursor, &bits ) ) != 0 )
  238. goto err_parse;
  239. cursor.data = bits.data;
  240. cursor.len = bits.len;
  241. /* Enter RSAPublicKey */
  242. asn1_enter ( &cursor, ASN1_SEQUENCE );
  243. }
  244. /* Extract modulus */
  245. if ( ( rc = rsa_parse_integer ( context, &modulus, &cursor ) ) != 0 )
  246. goto err_parse;
  247. asn1_skip_any ( &cursor );
  248. /* Skip public exponent, if applicable */
  249. if ( is_private )
  250. asn1_skip ( &cursor, ASN1_INTEGER );
  251. /* Extract publicExponent/privateExponent */
  252. if ( ( rc = rsa_parse_integer ( context, &exponent, &cursor ) ) != 0 )
  253. goto err_parse;
  254. DBGC ( context, "RSA %p modulus:\n", context );
  255. DBGC_HDA ( context, 0, modulus.data, modulus.len );
  256. DBGC ( context, "RSA %p exponent:\n", context );
  257. DBGC_HDA ( context, 0, exponent.data, exponent.len );
  258. /* Allocate dynamic storage */
  259. if ( ( rc = rsa_alloc ( context, modulus.len, exponent.len ) ) != 0 )
  260. goto err_alloc;
  261. /* Construct big integers */
  262. bigint_init ( ( ( bigint_t ( context->size ) * ) context->modulus0 ),
  263. modulus.data, modulus.len );
  264. bigint_init ( ( ( bigint_t ( context->exponent_size ) * )
  265. context->exponent0 ), exponent.data, exponent.len );
  266. return 0;
  267. rsa_free ( context );
  268. err_alloc:
  269. err_parse:
  270. return rc;
  271. }
  272. /**
  273. * Calculate RSA maximum output length
  274. *
  275. * @v ctx RSA context
  276. * @ret max_len Maximum output length
  277. */
  278. static size_t rsa_max_len ( void *ctx ) {
  279. struct rsa_context *context = ctx;
  280. return context->max_len;
  281. }
  282. /**
  283. * Perform RSA cipher operation
  284. *
  285. * @v context RSA context
  286. * @v in Input buffer
  287. * @v out Output buffer
  288. */
  289. static void rsa_cipher ( struct rsa_context *context,
  290. const void *in, void *out ) {
  291. bigint_t ( context->size ) *input = ( ( void * ) context->input0 );
  292. bigint_t ( context->size ) *output = ( ( void * ) context->output0 );
  293. bigint_t ( context->size ) *modulus = ( ( void * ) context->modulus0 );
  294. bigint_t ( context->exponent_size ) *exponent =
  295. ( ( void * ) context->exponent0 );
  296. /* Initialise big integer */
  297. bigint_init ( input, in, context->max_len );
  298. /* Perform modular exponentiation */
  299. bigint_mod_exp ( input, modulus, exponent, output, context->tmp );
  300. /* Copy out result */
  301. bigint_done ( output, out, context->max_len );
  302. }
  303. /**
  304. * Encrypt using RSA
  305. *
  306. * @v ctx RSA context
  307. * @v plaintext Plaintext
  308. * @v plaintext_len Length of plaintext
  309. * @v ciphertext Ciphertext
  310. * @ret ciphertext_len Length of ciphertext, or negative error
  311. */
  312. static int rsa_encrypt ( void *ctx, const void *plaintext,
  313. size_t plaintext_len, void *ciphertext ) {
  314. struct rsa_context *context = ctx;
  315. void *temp;
  316. uint8_t *encoded;
  317. size_t max_len = ( context->max_len - 11 );
  318. size_t random_nz_len = ( max_len - plaintext_len + 8 );
  319. int rc;
  320. /* Sanity check */
  321. if ( plaintext_len > max_len ) {
  322. DBGC ( context, "RSA %p plaintext too long (%zd bytes, max "
  323. "%zd)\n", context, plaintext_len, max_len );
  324. return -ERANGE;
  325. }
  326. DBGC ( context, "RSA %p encrypting:\n", context );
  327. DBGC_HDA ( context, 0, plaintext, plaintext_len );
  328. /* Construct encoded message (using the big integer output
  329. * buffer as temporary storage)
  330. */
  331. temp = context->output0;
  332. encoded = temp;
  333. encoded[0] = 0x00;
  334. encoded[1] = 0x02;
  335. if ( ( rc = get_random_nz ( &encoded[2], random_nz_len ) ) != 0 ) {
  336. DBGC ( context, "RSA %p could not generate random data: %s\n",
  337. context, strerror ( rc ) );
  338. return rc;
  339. }
  340. encoded[ 2 + random_nz_len ] = 0x00;
  341. memcpy ( &encoded[ context->max_len - plaintext_len ],
  342. plaintext, plaintext_len );
  343. /* Encipher the encoded message */
  344. rsa_cipher ( context, encoded, ciphertext );
  345. DBGC ( context, "RSA %p encrypted:\n", context );
  346. DBGC_HDA ( context, 0, ciphertext, context->max_len );
  347. return context->max_len;
  348. }
  349. /**
  350. * Decrypt using RSA
  351. *
  352. * @v ctx RSA context
  353. * @v ciphertext Ciphertext
  354. * @v ciphertext_len Ciphertext length
  355. * @v plaintext Plaintext
  356. * @ret plaintext_len Plaintext length, or negative error
  357. */
  358. static int rsa_decrypt ( void *ctx, const void *ciphertext,
  359. size_t ciphertext_len, void *plaintext ) {
  360. struct rsa_context *context = ctx;
  361. void *temp;
  362. uint8_t *encoded;
  363. uint8_t *end;
  364. uint8_t *zero;
  365. uint8_t *start;
  366. size_t plaintext_len;
  367. /* Sanity check */
  368. if ( ciphertext_len != context->max_len ) {
  369. DBGC ( context, "RSA %p ciphertext incorrect length (%zd "
  370. "bytes, should be %zd)\n",
  371. context, ciphertext_len, context->max_len );
  372. return -ERANGE;
  373. }
  374. DBGC ( context, "RSA %p decrypting:\n", context );
  375. DBGC_HDA ( context, 0, ciphertext, ciphertext_len );
  376. /* Decipher the message (using the big integer input buffer as
  377. * temporary storage)
  378. */
  379. temp = context->input0;
  380. encoded = temp;
  381. rsa_cipher ( context, ciphertext, encoded );
  382. /* Parse the message */
  383. end = ( encoded + context->max_len );
  384. if ( ( encoded[0] != 0x00 ) || ( encoded[1] != 0x02 ) )
  385. goto invalid;
  386. zero = memchr ( &encoded[2], 0, ( end - &encoded[2] ) );
  387. if ( ! zero )
  388. goto invalid;
  389. start = ( zero + 1 );
  390. plaintext_len = ( end - start );
  391. /* Copy out message */
  392. memcpy ( plaintext, start, plaintext_len );
  393. DBGC ( context, "RSA %p decrypted:\n", context );
  394. DBGC_HDA ( context, 0, plaintext, plaintext_len );
  395. return plaintext_len;
  396. invalid:
  397. DBGC ( context, "RSA %p invalid decrypted message:\n", context );
  398. DBGC_HDA ( context, 0, encoded, context->max_len );
  399. return -EINVAL;
  400. }
  401. /**
  402. * Encode RSA digest
  403. *
  404. * @v context RSA context
  405. * @v digest Digest algorithm
  406. * @v value Digest value
  407. * @v encoded Encoded digest
  408. * @ret rc Return status code
  409. */
  410. static int rsa_encode_digest ( struct rsa_context *context,
  411. struct digest_algorithm *digest,
  412. const void *value, void *encoded ) {
  413. struct rsa_digestinfo_prefix *prefix;
  414. size_t digest_len = digest->digestsize;
  415. uint8_t *temp = encoded;
  416. size_t digestinfo_len;
  417. size_t max_len;
  418. size_t pad_len;
  419. /* Identify prefix */
  420. prefix = rsa_find_prefix ( digest );
  421. if ( ! prefix ) {
  422. DBGC ( context, "RSA %p has no prefix for %s\n",
  423. context, digest->name );
  424. return -ENOTSUP;
  425. }
  426. digestinfo_len = ( prefix->len + digest_len );
  427. /* Sanity check */
  428. max_len = ( context->max_len - 11 );
  429. if ( digestinfo_len > max_len ) {
  430. DBGC ( context, "RSA %p %s digestInfo too long (%zd bytes, max"
  431. "%zd)\n",
  432. context, digest->name, digestinfo_len, max_len );
  433. return -ERANGE;
  434. }
  435. DBGC ( context, "RSA %p encoding %s digest:\n",
  436. context, digest->name );
  437. DBGC_HDA ( context, 0, value, digest_len );
  438. /* Construct encoded message */
  439. *(temp++) = 0x00;
  440. *(temp++) = 0x01;
  441. pad_len = ( max_len - digestinfo_len + 8 );
  442. memset ( temp, 0xff, pad_len );
  443. temp += pad_len;
  444. *(temp++) = 0x00;
  445. memcpy ( temp, prefix->data, prefix->len );
  446. temp += prefix->len;
  447. memcpy ( temp, value, digest_len );
  448. temp += digest_len;
  449. assert ( temp == ( encoded + context->max_len ) );
  450. DBGC ( context, "RSA %p encoded %s digest:\n", context, digest->name );
  451. DBGC_HDA ( context, 0, encoded, context->max_len );
  452. return 0;
  453. }
  454. /**
  455. * Sign digest value using RSA
  456. *
  457. * @v ctx RSA context
  458. * @v digest Digest algorithm
  459. * @v value Digest value
  460. * @v signature Signature
  461. * @ret signature_len Signature length, or negative error
  462. */
  463. static int rsa_sign ( void *ctx, struct digest_algorithm *digest,
  464. const void *value, void *signature ) {
  465. struct rsa_context *context = ctx;
  466. void *temp;
  467. int rc;
  468. DBGC ( context, "RSA %p signing %s digest:\n", context, digest->name );
  469. DBGC_HDA ( context, 0, value, digest->digestsize );
  470. /* Encode digest (using the big integer output buffer as
  471. * temporary storage)
  472. */
  473. temp = context->output0;
  474. if ( ( rc = rsa_encode_digest ( context, digest, value, temp ) ) != 0 )
  475. return rc;
  476. /* Encipher the encoded digest */
  477. rsa_cipher ( context, temp, signature );
  478. DBGC ( context, "RSA %p signed %s digest:\n", context, digest->name );
  479. DBGC_HDA ( context, 0, signature, context->max_len );
  480. return context->max_len;
  481. }
  482. /**
  483. * Verify signed digest value using RSA
  484. *
  485. * @v ctx RSA context
  486. * @v digest Digest algorithm
  487. * @v value Digest value
  488. * @v signature Signature
  489. * @v signature_len Signature length
  490. * @ret rc Return status code
  491. */
  492. static int rsa_verify ( void *ctx, struct digest_algorithm *digest,
  493. const void *value, const void *signature,
  494. size_t signature_len ) {
  495. struct rsa_context *context = ctx;
  496. void *temp;
  497. void *expected;
  498. void *actual;
  499. int rc;
  500. /* Sanity check */
  501. if ( signature_len != context->max_len ) {
  502. DBGC ( context, "RSA %p signature incorrect length (%zd "
  503. "bytes, should be %zd)\n",
  504. context, signature_len, context->max_len );
  505. return -ERANGE;
  506. }
  507. DBGC ( context, "RSA %p verifying %s digest:\n",
  508. context, digest->name );
  509. DBGC_HDA ( context, 0, value, digest->digestsize );
  510. DBGC_HDA ( context, 0, signature, signature_len );
  511. /* Decipher the signature (using the big integer input buffer
  512. * as temporary storage)
  513. */
  514. temp = context->input0;
  515. expected = temp;
  516. rsa_cipher ( context, signature, expected );
  517. DBGC ( context, "RSA %p deciphered signature:\n", context );
  518. DBGC_HDA ( context, 0, expected, context->max_len );
  519. /* Encode digest (using the big integer output buffer as
  520. * temporary storage)
  521. */
  522. temp = context->output0;
  523. actual = temp;
  524. if ( ( rc = rsa_encode_digest ( context, digest, value, actual ) ) !=0 )
  525. return rc;
  526. /* Verify the signature */
  527. if ( memcmp ( actual, expected, context->max_len ) != 0 ) {
  528. DBGC ( context, "RSA %p signature verification failed\n",
  529. context );
  530. return -EACCES_VERIFY;
  531. }
  532. DBGC ( context, "RSA %p signature verified successfully\n", context );
  533. return 0;
  534. }
  535. /**
  536. * Finalise RSA cipher
  537. *
  538. * @v ctx RSA context
  539. */
  540. static void rsa_final ( void *ctx ) {
  541. struct rsa_context *context = ctx;
  542. rsa_free ( context );
  543. }
  544. /** RSA public-key algorithm */
  545. struct pubkey_algorithm rsa_algorithm = {
  546. .name = "rsa",
  547. .ctxsize = sizeof ( struct rsa_context ),
  548. .init = rsa_init,
  549. .max_len = rsa_max_len,
  550. .encrypt = rsa_encrypt,
  551. .decrypt = rsa_decrypt,
  552. .sign = rsa_sign,
  553. .verify = rsa_verify,
  554. .final = rsa_final,
  555. };