Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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