rsa.c 55 KB

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