rsa.c 55 KB

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