rsa.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846
  1. /*
  2. * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  3. */
  4. /*
  5. * The following sources were referenced in the design of this implementation
  6. * of the RSA algorithm:
  7. *
  8. * [1] A method for obtaining digital signatures and public-key cryptosystems
  9. * R Rivest, A Shamir, and L Adleman
  10. * http://people.csail.mit.edu/rivest/pubs.html#RSA78
  11. *
  12. * [2] Handbook of Applied Cryptography - 1997, Chapter 8
  13. * Menezes, van Oorschot and Vanstone
  14. *
  15. * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
  16. * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
  17. * Stefan Mangard
  18. * https://arxiv.org/abs/1702.08719v2
  19. *
  20. */
  21. #if !defined(MBEDTLS_CONFIG_FILE)
  22. #include "mbedtls/config.h"
  23. #else
  24. #include MBEDTLS_CONFIG_FILE
  25. #endif
  26. #if defined(MBEDTLS_RSA_C)
  27. #include "mbedtls/rsa.h"
  28. #include "mbedtls/oid.h"
  29. #include <string.h>
  30. #if defined(MBEDTLS_PKCS1_V21)
  31. #include "mbedtls/md.h"
  32. #endif
  33. #if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
  34. #include <stdlib.h>
  35. #endif
  36. #if defined(MBEDTLS_PLATFORM_C)
  37. #include "mbedtls/platform.h"
  38. #else
  39. #include <stdio.h>
  40. #define mbedtls_printf printf
  41. #define mbedtls_calloc calloc
  42. #define mbedtls_free free
  43. #endif
  44. /* Implementation that should never be optimized out by the compiler */
  45. static void mbedtls_zeroize( void *v, size_t n ) {
  46. volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
  47. }
  48. /*
  49. * Initialize an RSA context
  50. */
  51. void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
  52. int padding,
  53. int hash_id )
  54. {
  55. memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
  56. mbedtls_rsa_set_padding( ctx, padding, hash_id );
  57. #if defined(MBEDTLS_THREADING_C)
  58. mbedtls_mutex_init( &ctx->mutex );
  59. #endif
  60. }
  61. /*
  62. * Set padding for an existing RSA context
  63. */
  64. void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
  65. {
  66. ctx->padding = padding;
  67. ctx->hash_id = hash_id;
  68. }
  69. #if defined(MBEDTLS_GENPRIME)
  70. /*
  71. * Generate an RSA keypair
  72. */
  73. int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
  74. int (*f_rng)(void *, unsigned char *, size_t),
  75. void *p_rng,
  76. unsigned int nbits, int exponent )
  77. {
  78. int ret;
  79. mbedtls_mpi P1, Q1, H, G;
  80. if( f_rng == NULL || nbits < 128 || exponent < 3 )
  81. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  82. if( nbits % 2 )
  83. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  84. mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
  85. mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
  86. /*
  87. * find primes P and Q with Q < P so that:
  88. * GCD( E, (P-1)*(Q-1) ) == 1
  89. */
  90. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
  91. do
  92. {
  93. MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
  94. f_rng, p_rng ) );
  95. MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
  96. f_rng, p_rng ) );
  97. if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
  98. continue;
  99. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
  100. if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
  101. continue;
  102. if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
  103. mbedtls_mpi_swap( &ctx->P, &ctx->Q );
  104. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
  105. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
  106. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
  107. MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
  108. }
  109. while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
  110. /*
  111. * D = E^-1 mod ((P-1)*(Q-1))
  112. * DP = D mod (P - 1)
  113. * DQ = D mod (Q - 1)
  114. * QP = Q^-1 mod P
  115. */
  116. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
  117. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
  118. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
  119. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
  120. ctx->len = ( mbedtls_mpi_bitlen( &ctx->N ) + 7 ) >> 3;
  121. cleanup:
  122. mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
  123. if( ret != 0 )
  124. {
  125. mbedtls_rsa_free( ctx );
  126. return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
  127. }
  128. return( 0 );
  129. }
  130. #endif /* MBEDTLS_GENPRIME */
  131. /*
  132. * Check a public RSA key
  133. */
  134. int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
  135. {
  136. if( !ctx->N.p || !ctx->E.p )
  137. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  138. if( ( ctx->N.p[0] & 1 ) == 0 ||
  139. ( ctx->E.p[0] & 1 ) == 0 )
  140. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  141. if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
  142. mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
  143. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  144. if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
  145. mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
  146. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  147. return( 0 );
  148. }
  149. /*
  150. * Check a private RSA key
  151. */
  152. int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
  153. {
  154. int ret;
  155. mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
  156. if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
  157. return( ret );
  158. if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
  159. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  160. mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
  161. mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 );
  162. mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
  163. mbedtls_mpi_init( &QP );
  164. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
  165. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
  166. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
  167. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
  168. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
  169. MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
  170. MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
  171. MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
  172. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
  173. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
  174. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
  175. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
  176. /*
  177. * Check for a valid PKCS1v2 private key
  178. */
  179. if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
  180. mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
  181. mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
  182. mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
  183. mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
  184. mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
  185. mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
  186. {
  187. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  188. }
  189. cleanup:
  190. mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
  191. mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 );
  192. mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
  193. mbedtls_mpi_free( &QP );
  194. if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
  195. return( ret );
  196. if( ret != 0 )
  197. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
  198. return( 0 );
  199. }
  200. /*
  201. * Check if contexts holding a public and private key match
  202. */
  203. int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
  204. {
  205. if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
  206. mbedtls_rsa_check_privkey( prv ) != 0 )
  207. {
  208. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  209. }
  210. if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
  211. mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
  212. {
  213. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  214. }
  215. return( 0 );
  216. }
  217. /*
  218. * Do an RSA public key operation
  219. */
  220. int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
  221. const unsigned char *input,
  222. unsigned char *output )
  223. {
  224. int ret;
  225. size_t olen;
  226. mbedtls_mpi T;
  227. mbedtls_mpi_init( &T );
  228. #if defined(MBEDTLS_THREADING_C)
  229. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  230. return( ret );
  231. #endif
  232. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
  233. if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
  234. {
  235. ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  236. goto cleanup;
  237. }
  238. olen = ctx->len;
  239. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
  240. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
  241. cleanup:
  242. #if defined(MBEDTLS_THREADING_C)
  243. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  244. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  245. #endif
  246. mbedtls_mpi_free( &T );
  247. if( ret != 0 )
  248. return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
  249. return( 0 );
  250. }
  251. /*
  252. * Generate or update blinding values, see section 10 of:
  253. * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
  254. * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
  255. * Berlin Heidelberg, 1996. p. 104-113.
  256. */
  257. static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
  258. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  259. {
  260. int ret, count = 0;
  261. if( ctx->Vf.p != NULL )
  262. {
  263. /* We already have blinding values, just update them by squaring */
  264. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
  265. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
  266. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
  267. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
  268. goto cleanup;
  269. }
  270. /* Unblinding value: Vf = random number, invertible mod N */
  271. do {
  272. if( count++ > 10 )
  273. return( MBEDTLS_ERR_RSA_RNG_FAILED );
  274. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
  275. MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
  276. } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
  277. /* Blinding value: Vi = Vf^(-e) mod N */
  278. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
  279. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->Vi, &ctx->Vi, &ctx->E, &ctx->N, &ctx->RN ) );
  280. cleanup:
  281. return( ret );
  282. }
  283. /*
  284. * Exponent blinding supposed to prevent side-channel attacks using multiple
  285. * traces of measurements to recover the RSA key. The more collisions are there,
  286. * the more bits of the key can be recovered. See [3].
  287. *
  288. * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
  289. * observations on avarage.
  290. *
  291. * For example with 28 byte blinding to achieve 2 collisions the adversary has
  292. * to make 2^112 observations on avarage.
  293. *
  294. * (With the currently (as of 2017 April) known best algorithms breaking 2048
  295. * bit RSA requires approximately as much time as trying out 2^112 random keys.
  296. * Thus in this sense with 28 byte blinding the security is not reduced by
  297. * side-channel attacks like the one in [3])
  298. *
  299. * This countermeasure does not help if the key recovery is possible with a
  300. * single trace.
  301. */
  302. #define RSA_EXPONENT_BLINDING 28
  303. /*
  304. * Do an RSA private key operation
  305. */
  306. int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
  307. int (*f_rng)(void *, unsigned char *, size_t),
  308. void *p_rng,
  309. const unsigned char *input,
  310. unsigned char *output )
  311. {
  312. int ret;
  313. size_t olen;
  314. mbedtls_mpi T, T1, T2;
  315. mbedtls_mpi P1, Q1, R;
  316. #if defined(MBEDTLS_RSA_NO_CRT)
  317. mbedtls_mpi D_blind;
  318. mbedtls_mpi *D = &ctx->D;
  319. #else
  320. mbedtls_mpi DP_blind, DQ_blind;
  321. mbedtls_mpi *DP = &ctx->DP;
  322. mbedtls_mpi *DQ = &ctx->DQ;
  323. #endif
  324. /* Make sure we have private key info, prevent possible misuse */
  325. if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL )
  326. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  327. mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
  328. mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
  329. if( f_rng != NULL )
  330. {
  331. #if defined(MBEDTLS_RSA_NO_CRT)
  332. mbedtls_mpi_init( &D_blind );
  333. #else
  334. mbedtls_mpi_init( &DP_blind );
  335. mbedtls_mpi_init( &DQ_blind );
  336. #endif
  337. }
  338. #if defined(MBEDTLS_THREADING_C)
  339. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  340. return( ret );
  341. #endif
  342. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
  343. if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
  344. {
  345. ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  346. goto cleanup;
  347. }
  348. if( f_rng != NULL )
  349. {
  350. /*
  351. * Blinding
  352. * T = T * Vi mod N
  353. */
  354. MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
  355. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
  356. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
  357. /*
  358. * Exponent blinding
  359. */
  360. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
  361. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
  362. #if defined(MBEDTLS_RSA_NO_CRT)
  363. /*
  364. * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
  365. */
  366. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
  367. f_rng, p_rng ) );
  368. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
  369. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
  370. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
  371. D = &D_blind;
  372. #else
  373. /*
  374. * DP_blind = ( P - 1 ) * R + DP
  375. */
  376. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
  377. f_rng, p_rng ) );
  378. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
  379. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
  380. &ctx->DP ) );
  381. DP = &DP_blind;
  382. /*
  383. * DQ_blind = ( Q - 1 ) * R + DQ
  384. */
  385. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
  386. f_rng, p_rng ) );
  387. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
  388. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
  389. &ctx->DQ ) );
  390. DQ = &DQ_blind;
  391. #endif /* MBEDTLS_RSA_NO_CRT */
  392. }
  393. #if defined(MBEDTLS_RSA_NO_CRT)
  394. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
  395. #else
  396. /*
  397. * Faster decryption using the CRT
  398. *
  399. * T1 = input ^ dP mod P
  400. * T2 = input ^ dQ mod Q
  401. */
  402. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
  403. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
  404. /*
  405. * T = (T1 - T2) * (Q^-1 mod P) mod P
  406. */
  407. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
  408. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
  409. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
  410. /*
  411. * T = T2 + T * Q
  412. */
  413. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
  414. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
  415. #endif /* MBEDTLS_RSA_NO_CRT */
  416. if( f_rng != NULL )
  417. {
  418. /*
  419. * Unblind
  420. * T = T * Vf mod N
  421. */
  422. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
  423. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
  424. }
  425. olen = ctx->len;
  426. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
  427. cleanup:
  428. #if defined(MBEDTLS_THREADING_C)
  429. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  430. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  431. #endif
  432. mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
  433. mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
  434. if( f_rng != NULL )
  435. {
  436. #if defined(MBEDTLS_RSA_NO_CRT)
  437. mbedtls_mpi_free( &D_blind );
  438. #else
  439. mbedtls_mpi_free( &DP_blind );
  440. mbedtls_mpi_free( &DQ_blind );
  441. #endif
  442. }
  443. if( ret != 0 )
  444. return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
  445. return( 0 );
  446. }
  447. #if defined(MBEDTLS_PKCS1_V21)
  448. /**
  449. * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
  450. *
  451. * \param dst buffer to mask
  452. * \param dlen length of destination buffer
  453. * \param src source of the mask generation
  454. * \param slen length of the source buffer
  455. * \param md_ctx message digest context to use
  456. */
  457. static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
  458. size_t slen, mbedtls_md_context_t *md_ctx )
  459. {
  460. unsigned char mask[MBEDTLS_MD_MAX_SIZE];
  461. unsigned char counter[4];
  462. unsigned char *p;
  463. unsigned int hlen;
  464. size_t i, use_len;
  465. memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
  466. memset( counter, 0, 4 );
  467. hlen = mbedtls_md_get_size( md_ctx->md_info );
  468. /* Generate and apply dbMask */
  469. p = dst;
  470. while( dlen > 0 )
  471. {
  472. use_len = hlen;
  473. if( dlen < hlen )
  474. use_len = dlen;
  475. mbedtls_md_starts( md_ctx );
  476. mbedtls_md_update( md_ctx, src, slen );
  477. mbedtls_md_update( md_ctx, counter, 4 );
  478. mbedtls_md_finish( md_ctx, mask );
  479. for( i = 0; i < use_len; ++i )
  480. *p++ ^= mask[i];
  481. counter[3]++;
  482. dlen -= use_len;
  483. }
  484. mbedtls_zeroize( mask, sizeof( mask ) );
  485. }
  486. #endif /* MBEDTLS_PKCS1_V21 */
  487. #if defined(MBEDTLS_PKCS1_V21)
  488. /*
  489. * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
  490. */
  491. int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
  492. int (*f_rng)(void *, unsigned char *, size_t),
  493. void *p_rng,
  494. int mode,
  495. const unsigned char *label, size_t label_len,
  496. size_t ilen,
  497. const unsigned char *input,
  498. unsigned char *output )
  499. {
  500. size_t olen;
  501. int ret;
  502. unsigned char *p = output;
  503. unsigned int hlen;
  504. const mbedtls_md_info_t *md_info;
  505. mbedtls_md_context_t md_ctx;
  506. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
  507. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  508. if( f_rng == NULL )
  509. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  510. md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
  511. if( md_info == NULL )
  512. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  513. olen = ctx->len;
  514. hlen = mbedtls_md_get_size( md_info );
  515. /* first comparison checks for overflow */
  516. if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
  517. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  518. memset( output, 0, olen );
  519. *p++ = 0;
  520. /* Generate a random octet string seed */
  521. if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
  522. return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
  523. p += hlen;
  524. /* Construct DB */
  525. mbedtls_md( md_info, label, label_len, p );
  526. p += hlen;
  527. p += olen - 2 * hlen - 2 - ilen;
  528. *p++ = 1;
  529. memcpy( p, input, ilen );
  530. mbedtls_md_init( &md_ctx );
  531. if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
  532. {
  533. mbedtls_md_free( &md_ctx );
  534. return( ret );
  535. }
  536. /* maskedDB: Apply dbMask to DB */
  537. mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
  538. &md_ctx );
  539. /* maskedSeed: Apply seedMask to seed */
  540. mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
  541. &md_ctx );
  542. mbedtls_md_free( &md_ctx );
  543. return( ( mode == MBEDTLS_RSA_PUBLIC )
  544. ? mbedtls_rsa_public( ctx, output, output )
  545. : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
  546. }
  547. #endif /* MBEDTLS_PKCS1_V21 */
  548. #if defined(MBEDTLS_PKCS1_V15)
  549. /*
  550. * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
  551. */
  552. int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
  553. int (*f_rng)(void *, unsigned char *, size_t),
  554. void *p_rng,
  555. int mode, size_t ilen,
  556. const unsigned char *input,
  557. unsigned char *output )
  558. {
  559. size_t nb_pad, olen;
  560. int ret;
  561. unsigned char *p = output;
  562. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
  563. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  564. // We don't check p_rng because it won't be dereferenced here
  565. if( f_rng == NULL || input == NULL || output == NULL )
  566. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  567. olen = ctx->len;
  568. /* first comparison checks for overflow */
  569. if( ilen + 11 < ilen || olen < ilen + 11 )
  570. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  571. nb_pad = olen - 3 - ilen;
  572. *p++ = 0;
  573. if( mode == MBEDTLS_RSA_PUBLIC )
  574. {
  575. *p++ = MBEDTLS_RSA_CRYPT;
  576. while( nb_pad-- > 0 )
  577. {
  578. int rng_dl = 100;
  579. do {
  580. ret = f_rng( p_rng, p, 1 );
  581. } while( *p == 0 && --rng_dl && ret == 0 );
  582. /* Check if RNG failed to generate data */
  583. if( rng_dl == 0 || ret != 0 )
  584. return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
  585. p++;
  586. }
  587. }
  588. else
  589. {
  590. *p++ = MBEDTLS_RSA_SIGN;
  591. while( nb_pad-- > 0 )
  592. *p++ = 0xFF;
  593. }
  594. *p++ = 0;
  595. memcpy( p, input, ilen );
  596. return( ( mode == MBEDTLS_RSA_PUBLIC )
  597. ? mbedtls_rsa_public( ctx, output, output )
  598. : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
  599. }
  600. #endif /* MBEDTLS_PKCS1_V15 */
  601. /*
  602. * Add the message padding, then do an RSA operation
  603. */
  604. int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
  605. int (*f_rng)(void *, unsigned char *, size_t),
  606. void *p_rng,
  607. int mode, size_t ilen,
  608. const unsigned char *input,
  609. unsigned char *output )
  610. {
  611. switch( ctx->padding )
  612. {
  613. #if defined(MBEDTLS_PKCS1_V15)
  614. case MBEDTLS_RSA_PKCS_V15:
  615. return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
  616. input, output );
  617. #endif
  618. #if defined(MBEDTLS_PKCS1_V21)
  619. case MBEDTLS_RSA_PKCS_V21:
  620. return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
  621. ilen, input, output );
  622. #endif
  623. default:
  624. return( MBEDTLS_ERR_RSA_INVALID_PADDING );
  625. }
  626. }
  627. #if defined(MBEDTLS_PKCS1_V21)
  628. /*
  629. * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
  630. */
  631. int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
  632. int (*f_rng)(void *, unsigned char *, size_t),
  633. void *p_rng,
  634. int mode,
  635. const unsigned char *label, size_t label_len,
  636. size_t *olen,
  637. const unsigned char *input,
  638. unsigned char *output,
  639. size_t output_max_len )
  640. {
  641. int ret;
  642. size_t ilen, i, pad_len;
  643. unsigned char *p, bad, pad_done;
  644. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  645. unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
  646. unsigned int hlen;
  647. const mbedtls_md_info_t *md_info;
  648. mbedtls_md_context_t md_ctx;
  649. /*
  650. * Parameters sanity checks
  651. */
  652. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
  653. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  654. ilen = ctx->len;
  655. if( ilen < 16 || ilen > sizeof( buf ) )
  656. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  657. md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
  658. if( md_info == NULL )
  659. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  660. hlen = mbedtls_md_get_size( md_info );
  661. // checking for integer underflow
  662. if( 2 * hlen + 2 > ilen )
  663. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  664. /*
  665. * RSA operation
  666. */
  667. ret = ( mode == MBEDTLS_RSA_PUBLIC )
  668. ? mbedtls_rsa_public( ctx, input, buf )
  669. : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
  670. if( ret != 0 )
  671. goto cleanup;
  672. /*
  673. * Unmask data and generate lHash
  674. */
  675. mbedtls_md_init( &md_ctx );
  676. if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
  677. {
  678. mbedtls_md_free( &md_ctx );
  679. goto cleanup;
  680. }
  681. /* Generate lHash */
  682. mbedtls_md( md_info, label, label_len, lhash );
  683. /* seed: Apply seedMask to maskedSeed */
  684. mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
  685. &md_ctx );
  686. /* DB: Apply dbMask to maskedDB */
  687. mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
  688. &md_ctx );
  689. mbedtls_md_free( &md_ctx );
  690. /*
  691. * Check contents, in "constant-time"
  692. */
  693. p = buf;
  694. bad = 0;
  695. bad |= *p++; /* First byte must be 0 */
  696. p += hlen; /* Skip seed */
  697. /* Check lHash */
  698. for( i = 0; i < hlen; i++ )
  699. bad |= lhash[i] ^ *p++;
  700. /* Get zero-padding len, but always read till end of buffer
  701. * (minus one, for the 01 byte) */
  702. pad_len = 0;
  703. pad_done = 0;
  704. for( i = 0; i < ilen - 2 * hlen - 2; i++ )
  705. {
  706. pad_done |= p[i];
  707. pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
  708. }
  709. p += pad_len;
  710. bad |= *p++ ^ 0x01;
  711. /*
  712. * The only information "leaked" is whether the padding was correct or not
  713. * (eg, no data is copied if it was not correct). This meets the
  714. * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
  715. * the different error conditions.
  716. */
  717. if( bad != 0 )
  718. {
  719. ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
  720. goto cleanup;
  721. }
  722. if( ilen - ( p - buf ) > output_max_len )
  723. {
  724. ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
  725. goto cleanup;
  726. }
  727. *olen = ilen - (p - buf);
  728. memcpy( output, p, *olen );
  729. ret = 0;
  730. cleanup:
  731. mbedtls_zeroize( buf, sizeof( buf ) );
  732. mbedtls_zeroize( lhash, sizeof( lhash ) );
  733. return( ret );
  734. }
  735. #endif /* MBEDTLS_PKCS1_V21 */
  736. #if defined(MBEDTLS_PKCS1_V15)
  737. /*
  738. * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
  739. */
  740. int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
  741. int (*f_rng)(void *, unsigned char *, size_t),
  742. void *p_rng,
  743. int mode, size_t *olen,
  744. const unsigned char *input,
  745. unsigned char *output,
  746. size_t output_max_len)
  747. {
  748. int ret;
  749. size_t ilen, pad_count = 0, i;
  750. unsigned char *p, bad, pad_done = 0;
  751. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  752. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
  753. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  754. ilen = ctx->len;
  755. if( ilen < 16 || ilen > sizeof( buf ) )
  756. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  757. ret = ( mode == MBEDTLS_RSA_PUBLIC )
  758. ? mbedtls_rsa_public( ctx, input, buf )
  759. : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
  760. if( ret != 0 )
  761. goto cleanup;
  762. p = buf;
  763. bad = 0;
  764. /*
  765. * Check and get padding len in "constant-time"
  766. */
  767. bad |= *p++; /* First byte must be 0 */
  768. /* This test does not depend on secret data */
  769. if( mode == MBEDTLS_RSA_PRIVATE )
  770. {
  771. bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
  772. /* Get padding len, but always read till end of buffer
  773. * (minus one, for the 00 byte) */
  774. for( i = 0; i < ilen - 3; i++ )
  775. {
  776. pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
  777. pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
  778. }
  779. p += pad_count;
  780. bad |= *p++; /* Must be zero */
  781. }
  782. else
  783. {
  784. bad |= *p++ ^ MBEDTLS_RSA_SIGN;
  785. /* Get padding len, but always read till end of buffer
  786. * (minus one, for the 00 byte) */
  787. for( i = 0; i < ilen - 3; i++ )
  788. {
  789. pad_done |= ( p[i] != 0xFF );
  790. pad_count += ( pad_done == 0 );
  791. }
  792. p += pad_count;
  793. bad |= *p++; /* Must be zero */
  794. }
  795. bad |= ( pad_count < 8 );
  796. if( bad )
  797. {
  798. ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
  799. goto cleanup;
  800. }
  801. if( ilen - ( p - buf ) > output_max_len )
  802. {
  803. ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
  804. goto cleanup;
  805. }
  806. *olen = ilen - (p - buf);
  807. memcpy( output, p, *olen );
  808. ret = 0;
  809. cleanup:
  810. mbedtls_zeroize( buf, sizeof( buf ) );
  811. return( ret );
  812. }
  813. #endif /* MBEDTLS_PKCS1_V15 */
  814. /*
  815. * Do an RSA operation, then remove the message padding
  816. */
  817. int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
  818. int (*f_rng)(void *, unsigned char *, size_t),
  819. void *p_rng,
  820. int mode, size_t *olen,
  821. const unsigned char *input,
  822. unsigned char *output,
  823. size_t output_max_len)
  824. {
  825. switch( ctx->padding )
  826. {
  827. #if defined(MBEDTLS_PKCS1_V15)
  828. case MBEDTLS_RSA_PKCS_V15:
  829. return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
  830. input, output, output_max_len );
  831. #endif
  832. #if defined(MBEDTLS_PKCS1_V21)
  833. case MBEDTLS_RSA_PKCS_V21:
  834. return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
  835. olen, input, output,
  836. output_max_len );
  837. #endif
  838. default:
  839. return( MBEDTLS_ERR_RSA_INVALID_PADDING );
  840. }
  841. }
  842. #if defined(MBEDTLS_PKCS1_V21)
  843. /*
  844. * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
  845. */
  846. int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
  847. int (*f_rng)(void *, unsigned char *, size_t),
  848. void *p_rng,
  849. int mode,
  850. mbedtls_md_type_t md_alg,
  851. unsigned int hashlen,
  852. const unsigned char *hash,
  853. unsigned char *sig )
  854. {
  855. size_t olen;
  856. unsigned char *p = sig;
  857. unsigned char salt[MBEDTLS_MD_MAX_SIZE];
  858. unsigned int slen, hlen, offset = 0;
  859. int ret;
  860. size_t msb;
  861. const mbedtls_md_info_t *md_info;
  862. mbedtls_md_context_t md_ctx;
  863. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
  864. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  865. if( f_rng == NULL )
  866. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  867. olen = ctx->len;
  868. if( md_alg != MBEDTLS_MD_NONE )
  869. {
  870. /* Gather length of hash to sign */
  871. md_info = mbedtls_md_info_from_type( md_alg );
  872. if( md_info == NULL )
  873. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  874. hashlen = mbedtls_md_get_size( md_info );
  875. }
  876. md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
  877. if( md_info == NULL )
  878. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  879. hlen = mbedtls_md_get_size( md_info );
  880. slen = hlen;
  881. if( olen < hlen + slen + 2 )
  882. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  883. memset( sig, 0, olen );
  884. /* Generate salt of length slen */
  885. if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
  886. return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
  887. /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
  888. msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
  889. p += olen - hlen * 2 - 2;
  890. *p++ = 0x01;
  891. memcpy( p, salt, slen );
  892. p += slen;
  893. mbedtls_md_init( &md_ctx );
  894. if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
  895. {
  896. mbedtls_md_free( &md_ctx );
  897. /* No need to zeroize salt: we didn't use it. */
  898. return( ret );
  899. }
  900. /* Generate H = Hash( M' ) */
  901. mbedtls_md_starts( &md_ctx );
  902. mbedtls_md_update( &md_ctx, p, 8 );
  903. mbedtls_md_update( &md_ctx, hash, hashlen );
  904. mbedtls_md_update( &md_ctx, salt, slen );
  905. mbedtls_md_finish( &md_ctx, p );
  906. mbedtls_zeroize( salt, sizeof( salt ) );
  907. /* Compensate for boundary condition when applying mask */
  908. if( msb % 8 == 0 )
  909. offset = 1;
  910. /* maskedDB: Apply dbMask to DB */
  911. mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
  912. mbedtls_md_free( &md_ctx );
  913. msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
  914. sig[0] &= 0xFF >> ( olen * 8 - msb );
  915. p += hlen;
  916. *p++ = 0xBC;
  917. return( ( mode == MBEDTLS_RSA_PUBLIC )
  918. ? mbedtls_rsa_public( ctx, sig, sig )
  919. : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
  920. }
  921. #endif /* MBEDTLS_PKCS1_V21 */
  922. #if defined(MBEDTLS_PKCS1_V15)
  923. /*
  924. * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
  925. */
  926. /*
  927. * Do an RSA operation to sign the message digest
  928. */
  929. int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
  930. int (*f_rng)(void *, unsigned char *, size_t),
  931. void *p_rng,
  932. int mode,
  933. mbedtls_md_type_t md_alg,
  934. unsigned int hashlen,
  935. const unsigned char *hash,
  936. unsigned char *sig )
  937. {
  938. size_t nb_pad, olen, oid_size = 0;
  939. unsigned char *p = sig;
  940. const char *oid = NULL;
  941. unsigned char *sig_try = NULL, *verif = NULL;
  942. size_t i;
  943. unsigned char diff;
  944. volatile unsigned char diff_no_optimize;
  945. int ret;
  946. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
  947. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  948. olen = ctx->len;
  949. nb_pad = olen - 3;
  950. if( md_alg != MBEDTLS_MD_NONE )
  951. {
  952. const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
  953. if( md_info == NULL )
  954. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  955. if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
  956. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  957. nb_pad -= 10 + oid_size;
  958. hashlen = mbedtls_md_get_size( md_info );
  959. }
  960. nb_pad -= hashlen;
  961. if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
  962. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  963. *p++ = 0;
  964. *p++ = MBEDTLS_RSA_SIGN;
  965. memset( p, 0xFF, nb_pad );
  966. p += nb_pad;
  967. *p++ = 0;
  968. if( md_alg == MBEDTLS_MD_NONE )
  969. {
  970. memcpy( p, hash, hashlen );
  971. }
  972. else
  973. {
  974. /*
  975. * DigestInfo ::= SEQUENCE {
  976. * digestAlgorithm DigestAlgorithmIdentifier,
  977. * digest Digest }
  978. *
  979. * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
  980. *
  981. * Digest ::= OCTET STRING
  982. */
  983. *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
  984. *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
  985. *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
  986. *p++ = (unsigned char) ( 0x04 + oid_size );
  987. *p++ = MBEDTLS_ASN1_OID;
  988. *p++ = oid_size & 0xFF;
  989. memcpy( p, oid, oid_size );
  990. p += oid_size;
  991. *p++ = MBEDTLS_ASN1_NULL;
  992. *p++ = 0x00;
  993. *p++ = MBEDTLS_ASN1_OCTET_STRING;
  994. *p++ = hashlen;
  995. memcpy( p, hash, hashlen );
  996. }
  997. if( mode == MBEDTLS_RSA_PUBLIC )
  998. return( mbedtls_rsa_public( ctx, sig, sig ) );
  999. /*
  1000. * In order to prevent Lenstra's attack, make the signature in a
  1001. * temporary buffer and check it before returning it.
  1002. */
  1003. sig_try = mbedtls_calloc( 1, ctx->len );
  1004. if( sig_try == NULL )
  1005. return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
  1006. verif = mbedtls_calloc( 1, ctx->len );
  1007. if( verif == NULL )
  1008. {
  1009. mbedtls_free( sig_try );
  1010. return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
  1011. }
  1012. MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
  1013. MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
  1014. /* Compare in constant time just in case */
  1015. for( diff = 0, i = 0; i < ctx->len; i++ )
  1016. diff |= verif[i] ^ sig[i];
  1017. diff_no_optimize = diff;
  1018. if( diff_no_optimize != 0 )
  1019. {
  1020. ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
  1021. goto cleanup;
  1022. }
  1023. memcpy( sig, sig_try, ctx->len );
  1024. cleanup:
  1025. mbedtls_free( sig_try );
  1026. mbedtls_free( verif );
  1027. return( ret );
  1028. }
  1029. #endif /* MBEDTLS_PKCS1_V15 */
  1030. /*
  1031. * Do an RSA operation to sign the message digest
  1032. */
  1033. int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
  1034. int (*f_rng)(void *, unsigned char *, size_t),
  1035. void *p_rng,
  1036. int mode,
  1037. mbedtls_md_type_t md_alg,
  1038. unsigned int hashlen,
  1039. const unsigned char *hash,
  1040. unsigned char *sig )
  1041. {
  1042. switch( ctx->padding )
  1043. {
  1044. #if defined(MBEDTLS_PKCS1_V15)
  1045. case MBEDTLS_RSA_PKCS_V15:
  1046. return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
  1047. hashlen, hash, sig );
  1048. #endif
  1049. #if defined(MBEDTLS_PKCS1_V21)
  1050. case MBEDTLS_RSA_PKCS_V21:
  1051. return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
  1052. hashlen, hash, sig );
  1053. #endif
  1054. default:
  1055. return( MBEDTLS_ERR_RSA_INVALID_PADDING );
  1056. }
  1057. }
  1058. #if defined(MBEDTLS_PKCS1_V21)
  1059. /*
  1060. * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
  1061. */
  1062. int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
  1063. int (*f_rng)(void *, unsigned char *, size_t),
  1064. void *p_rng,
  1065. int mode,
  1066. mbedtls_md_type_t md_alg,
  1067. unsigned int hashlen,
  1068. const unsigned char *hash,
  1069. mbedtls_md_type_t mgf1_hash_id,
  1070. int expected_salt_len,
  1071. const unsigned char *sig )
  1072. {
  1073. int ret;
  1074. size_t siglen;
  1075. unsigned char *p;
  1076. unsigned char result[MBEDTLS_MD_MAX_SIZE];
  1077. unsigned char zeros[8];
  1078. unsigned int hlen;
  1079. size_t slen, msb;
  1080. const mbedtls_md_info_t *md_info;
  1081. mbedtls_md_context_t md_ctx;
  1082. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  1083. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
  1084. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1085. siglen = ctx->len;
  1086. if( siglen < 16 || siglen > sizeof( buf ) )
  1087. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1088. ret = ( mode == MBEDTLS_RSA_PUBLIC )
  1089. ? mbedtls_rsa_public( ctx, sig, buf )
  1090. : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
  1091. if( ret != 0 )
  1092. return( ret );
  1093. p = buf;
  1094. if( buf[siglen - 1] != 0xBC )
  1095. return( MBEDTLS_ERR_RSA_INVALID_PADDING );
  1096. if( md_alg != MBEDTLS_MD_NONE )
  1097. {
  1098. /* Gather length of hash to sign */
  1099. md_info = mbedtls_md_info_from_type( md_alg );
  1100. if( md_info == NULL )
  1101. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1102. hashlen = mbedtls_md_get_size( md_info );
  1103. }
  1104. md_info = mbedtls_md_info_from_type( mgf1_hash_id );
  1105. if( md_info == NULL )
  1106. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1107. hlen = mbedtls_md_get_size( md_info );
  1108. slen = siglen - hlen - 1; /* Currently length of salt + padding */
  1109. memset( zeros, 0, 8 );
  1110. /*
  1111. * Note: EMSA-PSS verification is over the length of N - 1 bits
  1112. */
  1113. msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
  1114. /* Compensate for boundary condition when applying mask */
  1115. if( msb % 8 == 0 )
  1116. {
  1117. p++;
  1118. siglen -= 1;
  1119. }
  1120. if( buf[0] >> ( 8 - siglen * 8 + msb ) )
  1121. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1122. mbedtls_md_init( &md_ctx );
  1123. if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
  1124. {
  1125. mbedtls_md_free( &md_ctx );
  1126. return( ret );
  1127. }
  1128. mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
  1129. buf[0] &= 0xFF >> ( siglen * 8 - msb );
  1130. while( p < buf + siglen && *p == 0 )
  1131. p++;
  1132. if( p == buf + siglen ||
  1133. *p++ != 0x01 )
  1134. {
  1135. mbedtls_md_free( &md_ctx );
  1136. return( MBEDTLS_ERR_RSA_INVALID_PADDING );
  1137. }
  1138. /* Actual salt len */
  1139. slen -= p - buf;
  1140. if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
  1141. slen != (size_t) expected_salt_len )
  1142. {
  1143. mbedtls_md_free( &md_ctx );
  1144. return( MBEDTLS_ERR_RSA_INVALID_PADDING );
  1145. }
  1146. /*
  1147. * Generate H = Hash( M' )
  1148. */
  1149. mbedtls_md_starts( &md_ctx );
  1150. mbedtls_md_update( &md_ctx, zeros, 8 );
  1151. mbedtls_md_update( &md_ctx, hash, hashlen );
  1152. mbedtls_md_update( &md_ctx, p, slen );
  1153. mbedtls_md_finish( &md_ctx, result );
  1154. mbedtls_md_free( &md_ctx );
  1155. if( memcmp( p + slen, result, hlen ) == 0 )
  1156. return( 0 );
  1157. else
  1158. return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
  1159. }
  1160. /*
  1161. * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
  1162. */
  1163. int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
  1164. int (*f_rng)(void *, unsigned char *, size_t),
  1165. void *p_rng,
  1166. int mode,
  1167. mbedtls_md_type_t md_alg,
  1168. unsigned int hashlen,
  1169. const unsigned char *hash,
  1170. const unsigned char *sig )
  1171. {
  1172. mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
  1173. ? (mbedtls_md_type_t) ctx->hash_id
  1174. : md_alg;
  1175. return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
  1176. md_alg, hashlen, hash,
  1177. mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
  1178. sig ) );
  1179. }
  1180. #endif /* MBEDTLS_PKCS1_V21 */
  1181. #if defined(MBEDTLS_PKCS1_V15)
  1182. /*
  1183. * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
  1184. */
  1185. int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
  1186. int (*f_rng)(void *, unsigned char *, size_t),
  1187. void *p_rng,
  1188. int mode,
  1189. mbedtls_md_type_t md_alg,
  1190. unsigned int hashlen,
  1191. const unsigned char *hash,
  1192. const unsigned char *sig )
  1193. {
  1194. int ret;
  1195. size_t len, siglen, asn1_len;
  1196. unsigned char *p, *end;
  1197. mbedtls_md_type_t msg_md_alg;
  1198. const mbedtls_md_info_t *md_info;
  1199. mbedtls_asn1_buf oid;
  1200. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  1201. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
  1202. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1203. siglen = ctx->len;
  1204. if( siglen < 16 || siglen > sizeof( buf ) )
  1205. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1206. ret = ( mode == MBEDTLS_RSA_PUBLIC )
  1207. ? mbedtls_rsa_public( ctx, sig, buf )
  1208. : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
  1209. if( ret != 0 )
  1210. return( ret );
  1211. p = buf;
  1212. if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
  1213. return( MBEDTLS_ERR_RSA_INVALID_PADDING );
  1214. while( *p != 0 )
  1215. {
  1216. if( p >= buf + siglen - 1 || *p != 0xFF )
  1217. return( MBEDTLS_ERR_RSA_INVALID_PADDING );
  1218. p++;
  1219. }
  1220. p++;
  1221. len = siglen - ( p - buf );
  1222. if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
  1223. {
  1224. if( memcmp( p, hash, hashlen ) == 0 )
  1225. return( 0 );
  1226. else
  1227. return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
  1228. }
  1229. md_info = mbedtls_md_info_from_type( md_alg );
  1230. if( md_info == NULL )
  1231. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1232. hashlen = mbedtls_md_get_size( md_info );
  1233. end = p + len;
  1234. /*
  1235. * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure
  1236. */
  1237. if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
  1238. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  1239. return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
  1240. if( asn1_len + 2 != len )
  1241. return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
  1242. if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
  1243. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  1244. return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
  1245. if( asn1_len + 6 + hashlen != len )
  1246. return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
  1247. if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
  1248. return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
  1249. oid.p = p;
  1250. p += oid.len;
  1251. if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
  1252. return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
  1253. if( md_alg != msg_md_alg )
  1254. return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
  1255. /*
  1256. * assume the algorithm parameters must be NULL
  1257. */
  1258. if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
  1259. return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
  1260. if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
  1261. return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
  1262. if( asn1_len != hashlen )
  1263. return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
  1264. if( memcmp( p, hash, hashlen ) != 0 )
  1265. return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
  1266. p += hashlen;
  1267. if( p != end )
  1268. return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
  1269. return( 0 );
  1270. }
  1271. #endif /* MBEDTLS_PKCS1_V15 */
  1272. /*
  1273. * Do an RSA operation and check the message digest
  1274. */
  1275. int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
  1276. int (*f_rng)(void *, unsigned char *, size_t),
  1277. void *p_rng,
  1278. int mode,
  1279. mbedtls_md_type_t md_alg,
  1280. unsigned int hashlen,
  1281. const unsigned char *hash,
  1282. const unsigned char *sig )
  1283. {
  1284. switch( ctx->padding )
  1285. {
  1286. #if defined(MBEDTLS_PKCS1_V15)
  1287. case MBEDTLS_RSA_PKCS_V15:
  1288. return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
  1289. hashlen, hash, sig );
  1290. #endif
  1291. #if defined(MBEDTLS_PKCS1_V21)
  1292. case MBEDTLS_RSA_PKCS_V21:
  1293. return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
  1294. hashlen, hash, sig );
  1295. #endif
  1296. default:
  1297. return( MBEDTLS_ERR_RSA_INVALID_PADDING );
  1298. }
  1299. }
  1300. /*
  1301. * Copy the components of an RSA key
  1302. */
  1303. int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
  1304. {
  1305. int ret;
  1306. dst->ver = src->ver;
  1307. dst->len = src->len;
  1308. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
  1309. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
  1310. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
  1311. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
  1312. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
  1313. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
  1314. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
  1315. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
  1316. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
  1317. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
  1318. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
  1319. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
  1320. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
  1321. dst->padding = src->padding;
  1322. dst->hash_id = src->hash_id;
  1323. cleanup:
  1324. if( ret != 0 )
  1325. mbedtls_rsa_free( dst );
  1326. return( ret );
  1327. }
  1328. /*
  1329. * Free the components of an RSA key
  1330. */
  1331. void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
  1332. {
  1333. mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
  1334. mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
  1335. mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
  1336. mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
  1337. mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
  1338. #if defined(MBEDTLS_THREADING_C)
  1339. mbedtls_mutex_free( &ctx->mutex );
  1340. #endif
  1341. }
  1342. #if defined(MBEDTLS_SELF_TEST)
  1343. #include "mbedtls/sha1.h"
  1344. /*
  1345. * Example RSA-1024 keypair, for test purposes
  1346. */
  1347. #define KEY_LEN 128
  1348. #define RSA_N "9292758453063D803DD603D5E777D788" \
  1349. "8ED1D5BF35786190FA2F23EBC0848AEA" \
  1350. "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
  1351. "7130B9CED7ACDF54CFC7555AC14EEBAB" \
  1352. "93A89813FBF3C4F8066D2D800F7C38A8" \
  1353. "1AE31942917403FF4946B0A83D3D3E05" \
  1354. "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
  1355. "5E94BB77B07507233A0BC7BAC8F90F79"
  1356. #define RSA_E "10001"
  1357. #define RSA_D "24BF6185468786FDD303083D25E64EFC" \
  1358. "66CA472BC44D253102F8B4A9D3BFA750" \
  1359. "91386C0077937FE33FA3252D28855837" \
  1360. "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
  1361. "DF79C5CE07EE72C7F123142198164234" \
  1362. "CABB724CF78B8173B9F880FC86322407" \
  1363. "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
  1364. "071513A1E85B5DFA031F21ECAE91A34D"
  1365. #define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
  1366. "2C01CAD19EA484A87EA4377637E75500" \
  1367. "FCB2005C5C7DD6EC4AC023CDA285D796" \
  1368. "C3D9E75E1EFC42488BB4F1D13AC30A57"
  1369. #define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
  1370. "E211C2B9E5DB1ED0BF61D0D9899620F4" \
  1371. "910E4168387E3C30AA1E00C339A79508" \
  1372. "8452DD96A9A5EA5D9DCA68DA636032AF"
  1373. #define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
  1374. "3C94D22288ACD763FD8E5600ED4A702D" \
  1375. "F84198A5F06C2E72236AE490C93F07F8" \
  1376. "3CC559CD27BC2D1CA488811730BB5725"
  1377. #define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
  1378. "D8AAEA56749EA28623272E4F7D0592AF" \
  1379. "7C1F1313CAC9471B5C523BFE592F517B" \
  1380. "407A1BD76C164B93DA2D32A383E58357"
  1381. #define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
  1382. "F38D18D2B2F0E2DD275AA977E2BF4411" \
  1383. "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
  1384. "A74206CEC169D74BF5A8C50D6F48EA08"
  1385. #define PT_LEN 24
  1386. #define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
  1387. "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
  1388. #if defined(MBEDTLS_PKCS1_V15)
  1389. static int myrand( void *rng_state, unsigned char *output, size_t len )
  1390. {
  1391. #if !defined(__OpenBSD__)
  1392. size_t i;
  1393. if( rng_state != NULL )
  1394. rng_state = NULL;
  1395. for( i = 0; i < len; ++i )
  1396. output[i] = rand();
  1397. #else
  1398. if( rng_state != NULL )
  1399. rng_state = NULL;
  1400. arc4random_buf( output, len );
  1401. #endif /* !OpenBSD */
  1402. return( 0 );
  1403. }
  1404. #endif /* MBEDTLS_PKCS1_V15 */
  1405. /*
  1406. * Checkup routine
  1407. */
  1408. int mbedtls_rsa_self_test( int verbose )
  1409. {
  1410. int ret = 0;
  1411. #if defined(MBEDTLS_PKCS1_V15)
  1412. size_t len;
  1413. mbedtls_rsa_context rsa;
  1414. unsigned char rsa_plaintext[PT_LEN];
  1415. unsigned char rsa_decrypted[PT_LEN];
  1416. unsigned char rsa_ciphertext[KEY_LEN];
  1417. #if defined(MBEDTLS_SHA1_C)
  1418. unsigned char sha1sum[20];
  1419. #endif
  1420. mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
  1421. rsa.len = KEY_LEN;
  1422. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
  1423. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
  1424. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
  1425. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
  1426. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
  1427. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
  1428. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
  1429. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
  1430. if( verbose != 0 )
  1431. mbedtls_printf( " RSA key validation: " );
  1432. if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
  1433. mbedtls_rsa_check_privkey( &rsa ) != 0 )
  1434. {
  1435. if( verbose != 0 )
  1436. mbedtls_printf( "failed\n" );
  1437. return( 1 );
  1438. }
  1439. if( verbose != 0 )
  1440. mbedtls_printf( "passed\n PKCS#1 encryption : " );
  1441. memcpy( rsa_plaintext, RSA_PT, PT_LEN );
  1442. if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
  1443. rsa_plaintext, rsa_ciphertext ) != 0 )
  1444. {
  1445. if( verbose != 0 )
  1446. mbedtls_printf( "failed\n" );
  1447. return( 1 );
  1448. }
  1449. if( verbose != 0 )
  1450. mbedtls_printf( "passed\n PKCS#1 decryption : " );
  1451. if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
  1452. rsa_ciphertext, rsa_decrypted,
  1453. sizeof(rsa_decrypted) ) != 0 )
  1454. {
  1455. if( verbose != 0 )
  1456. mbedtls_printf( "failed\n" );
  1457. return( 1 );
  1458. }
  1459. if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
  1460. {
  1461. if( verbose != 0 )
  1462. mbedtls_printf( "failed\n" );
  1463. return( 1 );
  1464. }
  1465. if( verbose != 0 )
  1466. mbedtls_printf( "passed\n" );
  1467. #if defined(MBEDTLS_SHA1_C)
  1468. if( verbose != 0 )
  1469. mbedtls_printf( " PKCS#1 data sign : " );
  1470. mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
  1471. if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
  1472. sha1sum, rsa_ciphertext ) != 0 )
  1473. {
  1474. if( verbose != 0 )
  1475. mbedtls_printf( "failed\n" );
  1476. return( 1 );
  1477. }
  1478. if( verbose != 0 )
  1479. mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
  1480. if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
  1481. sha1sum, rsa_ciphertext ) != 0 )
  1482. {
  1483. if( verbose != 0 )
  1484. mbedtls_printf( "failed\n" );
  1485. return( 1 );
  1486. }
  1487. if( verbose != 0 )
  1488. mbedtls_printf( "passed\n" );
  1489. #endif /* MBEDTLS_SHA1_C */
  1490. if( verbose != 0 )
  1491. mbedtls_printf( "\n" );
  1492. cleanup:
  1493. mbedtls_rsa_free( &rsa );
  1494. #else /* MBEDTLS_PKCS1_V15 */
  1495. ((void) verbose);
  1496. #endif /* MBEDTLS_PKCS1_V15 */
  1497. return( ret );
  1498. }
  1499. #endif /* MBEDTLS_SELF_TEST */
  1500. #endif /* MBEDTLS_RSA_C */