rsa.c 50 KB

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