rsa.c 50 KB

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