rsa.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320
  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/rsa_internal.h"
  46. #include "mbedtls/oid.h"
  47. #include <string.h>
  48. #if defined(MBEDTLS_PKCS1_V21)
  49. #include "mbedtls/md.h"
  50. #endif
  51. #if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
  52. #include <stdlib.h>
  53. #endif
  54. #if defined(MBEDTLS_PLATFORM_C)
  55. #include "mbedtls/platform.h"
  56. #else
  57. #include <stdio.h>
  58. #define mbedtls_printf printf
  59. #define mbedtls_calloc calloc
  60. #define mbedtls_free free
  61. #endif
  62. #if !defined(MBEDTLS_RSA_ALT)
  63. /* Implementation that should never be optimized out by the compiler */
  64. static void mbedtls_zeroize( void *v, size_t n ) {
  65. volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
  66. }
  67. /* constant-time buffer comparison */
  68. static inline int mbedtls_safer_memcmp( const void *a, const void *b, size_t n )
  69. {
  70. size_t i;
  71. const unsigned char *A = (const unsigned char *) a;
  72. const unsigned char *B = (const unsigned char *) b;
  73. unsigned char diff = 0;
  74. for( i = 0; i < n; i++ )
  75. diff |= A[i] ^ B[i];
  76. return( diff );
  77. }
  78. int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
  79. const mbedtls_mpi *N,
  80. const mbedtls_mpi *P, const mbedtls_mpi *Q,
  81. const mbedtls_mpi *D, const mbedtls_mpi *E )
  82. {
  83. int ret;
  84. if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
  85. ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
  86. ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
  87. ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
  88. ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
  89. {
  90. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
  91. }
  92. if( N != NULL )
  93. ctx->len = mbedtls_mpi_size( &ctx->N );
  94. return( 0 );
  95. }
  96. int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
  97. unsigned char const *N, size_t N_len,
  98. unsigned char const *P, size_t P_len,
  99. unsigned char const *Q, size_t Q_len,
  100. unsigned char const *D, size_t D_len,
  101. unsigned char const *E, size_t E_len )
  102. {
  103. int ret = 0;
  104. if( N != NULL )
  105. {
  106. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
  107. ctx->len = mbedtls_mpi_size( &ctx->N );
  108. }
  109. if( P != NULL )
  110. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
  111. if( Q != NULL )
  112. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
  113. if( D != NULL )
  114. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
  115. if( E != NULL )
  116. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
  117. cleanup:
  118. if( ret != 0 )
  119. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
  120. return( 0 );
  121. }
  122. /*
  123. * Checks whether the context fields are set in such a way
  124. * that the RSA primitives will be able to execute without error.
  125. * It does *not* make guarantees for consistency of the parameters.
  126. */
  127. static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
  128. int blinding_needed )
  129. {
  130. #if !defined(MBEDTLS_RSA_NO_CRT)
  131. /* blinding_needed is only used for NO_CRT to decide whether
  132. * P,Q need to be present or not. */
  133. ((void) blinding_needed);
  134. #endif
  135. if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
  136. ctx->len > MBEDTLS_MPI_MAX_SIZE )
  137. {
  138. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  139. }
  140. /*
  141. * 1. Modular exponentiation needs positive, odd moduli.
  142. */
  143. /* Modular exponentiation wrt. N is always used for
  144. * RSA public key operations. */
  145. if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
  146. mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
  147. {
  148. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  149. }
  150. #if !defined(MBEDTLS_RSA_NO_CRT)
  151. /* Modular exponentiation for P and Q is only
  152. * used for private key operations and if CRT
  153. * is used. */
  154. if( is_priv &&
  155. ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
  156. mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
  157. mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
  158. mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
  159. {
  160. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  161. }
  162. #endif /* !MBEDTLS_RSA_NO_CRT */
  163. /*
  164. * 2. Exponents must be positive
  165. */
  166. /* Always need E for public key operations */
  167. if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
  168. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  169. #if defined(MBEDTLS_RSA_NO_CRT)
  170. /* For private key operations, use D or DP & DQ
  171. * as (unblinded) exponents. */
  172. if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
  173. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  174. #else
  175. if( is_priv &&
  176. ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
  177. mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
  178. {
  179. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  180. }
  181. #endif /* MBEDTLS_RSA_NO_CRT */
  182. /* Blinding shouldn't make exponents negative either,
  183. * so check that P, Q >= 1 if that hasn't yet been
  184. * done as part of 1. */
  185. #if defined(MBEDTLS_RSA_NO_CRT)
  186. if( is_priv && blinding_needed &&
  187. ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
  188. mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
  189. {
  190. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  191. }
  192. #endif
  193. /* It wouldn't lead to an error if it wasn't satisfied,
  194. * but check for QP >= 1 nonetheless. */
  195. #if !defined(MBEDTLS_RSA_NO_CRT)
  196. if( is_priv &&
  197. mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
  198. {
  199. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  200. }
  201. #endif
  202. return( 0 );
  203. }
  204. int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
  205. {
  206. int ret = 0;
  207. const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
  208. const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
  209. const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
  210. const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
  211. const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
  212. /*
  213. * Check whether provided parameters are enough
  214. * to deduce all others. The following incomplete
  215. * parameter sets for private keys are supported:
  216. *
  217. * (1) P, Q missing.
  218. * (2) D and potentially N missing.
  219. *
  220. */
  221. const int n_missing = have_P && have_Q && have_D && have_E;
  222. const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
  223. const int d_missing = have_P && have_Q && !have_D && have_E;
  224. const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
  225. /* These three alternatives are mutually exclusive */
  226. const int is_priv = n_missing || pq_missing || d_missing;
  227. if( !is_priv && !is_pub )
  228. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  229. /*
  230. * Step 1: Deduce N if P, Q are provided.
  231. */
  232. if( !have_N && have_P && have_Q )
  233. {
  234. if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
  235. &ctx->Q ) ) != 0 )
  236. {
  237. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
  238. }
  239. ctx->len = mbedtls_mpi_size( &ctx->N );
  240. }
  241. /*
  242. * Step 2: Deduce and verify all remaining core parameters.
  243. */
  244. if( pq_missing )
  245. {
  246. ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
  247. &ctx->P, &ctx->Q );
  248. if( ret != 0 )
  249. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
  250. }
  251. else if( d_missing )
  252. {
  253. if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
  254. &ctx->Q,
  255. &ctx->E,
  256. &ctx->D ) ) != 0 )
  257. {
  258. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
  259. }
  260. }
  261. /*
  262. * Step 3: Deduce all additional parameters specific
  263. * to our current RSA implementation.
  264. */
  265. #if !defined(MBEDTLS_RSA_NO_CRT)
  266. if( is_priv )
  267. {
  268. ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
  269. &ctx->DP, &ctx->DQ, &ctx->QP );
  270. if( ret != 0 )
  271. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
  272. }
  273. #endif /* MBEDTLS_RSA_NO_CRT */
  274. /*
  275. * Step 3: Basic sanity checks
  276. */
  277. return( rsa_check_context( ctx, is_priv, 1 ) );
  278. }
  279. int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
  280. unsigned char *N, size_t N_len,
  281. unsigned char *P, size_t P_len,
  282. unsigned char *Q, size_t Q_len,
  283. unsigned char *D, size_t D_len,
  284. unsigned char *E, size_t E_len )
  285. {
  286. int ret = 0;
  287. /* Check if key is private or public */
  288. const int is_priv =
  289. mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
  290. mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
  291. mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
  292. mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
  293. mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
  294. if( !is_priv )
  295. {
  296. /* If we're trying to export private parameters for a public key,
  297. * something must be wrong. */
  298. if( P != NULL || Q != NULL || D != NULL )
  299. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  300. }
  301. if( N != NULL )
  302. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
  303. if( P != NULL )
  304. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
  305. if( Q != NULL )
  306. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
  307. if( D != NULL )
  308. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
  309. if( E != NULL )
  310. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
  311. cleanup:
  312. return( ret );
  313. }
  314. int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
  315. mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
  316. mbedtls_mpi *D, mbedtls_mpi *E )
  317. {
  318. int ret;
  319. /* Check if key is private or public */
  320. int is_priv =
  321. mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
  322. mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
  323. mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
  324. mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
  325. mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
  326. if( !is_priv )
  327. {
  328. /* If we're trying to export private parameters for a public key,
  329. * something must be wrong. */
  330. if( P != NULL || Q != NULL || D != NULL )
  331. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  332. }
  333. /* Export all requested core parameters. */
  334. if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
  335. ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
  336. ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
  337. ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
  338. ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
  339. {
  340. return( ret );
  341. }
  342. return( 0 );
  343. }
  344. /*
  345. * Export CRT parameters
  346. * This must also be implemented if CRT is not used, for being able to
  347. * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
  348. * can be used in this case.
  349. */
  350. int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
  351. mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
  352. {
  353. int ret;
  354. /* Check if key is private or public */
  355. int is_priv =
  356. mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
  357. mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
  358. mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
  359. mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
  360. mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
  361. if( !is_priv )
  362. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  363. #if !defined(MBEDTLS_RSA_NO_CRT)
  364. /* Export all requested blinding parameters. */
  365. if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
  366. ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
  367. ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
  368. {
  369. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
  370. }
  371. #else
  372. if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
  373. DP, DQ, QP ) ) != 0 )
  374. {
  375. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
  376. }
  377. #endif
  378. return( 0 );
  379. }
  380. /*
  381. * Initialize an RSA context
  382. */
  383. void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
  384. int padding,
  385. int hash_id )
  386. {
  387. memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
  388. mbedtls_rsa_set_padding( ctx, padding, hash_id );
  389. #if defined(MBEDTLS_THREADING_C)
  390. mbedtls_mutex_init( &ctx->mutex );
  391. #endif
  392. }
  393. /*
  394. * Set padding for an existing RSA context
  395. */
  396. void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
  397. {
  398. ctx->padding = padding;
  399. ctx->hash_id = hash_id;
  400. }
  401. /*
  402. * Get length in bytes of RSA modulus
  403. */
  404. size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
  405. {
  406. return( ctx->len );
  407. }
  408. #if defined(MBEDTLS_GENPRIME)
  409. /*
  410. * Generate an RSA keypair
  411. */
  412. int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
  413. int (*f_rng)(void *, unsigned char *, size_t),
  414. void *p_rng,
  415. unsigned int nbits, int exponent )
  416. {
  417. int ret;
  418. mbedtls_mpi H, G;
  419. if( f_rng == NULL || nbits < 128 || exponent < 3 )
  420. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  421. if( nbits % 2 )
  422. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  423. mbedtls_mpi_init( &H );
  424. mbedtls_mpi_init( &G );
  425. /*
  426. * find primes P and Q with Q < P so that:
  427. * GCD( E, (P-1)*(Q-1) ) == 1
  428. */
  429. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
  430. do
  431. {
  432. MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
  433. f_rng, p_rng ) );
  434. MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
  435. f_rng, p_rng ) );
  436. if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
  437. continue;
  438. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
  439. if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
  440. continue;
  441. if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
  442. mbedtls_mpi_swap( &ctx->P, &ctx->Q );
  443. /* Temporarily replace P,Q by P-1, Q-1 */
  444. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
  445. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
  446. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
  447. MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
  448. }
  449. while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
  450. /* Restore P,Q */
  451. MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
  452. MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
  453. ctx->len = mbedtls_mpi_size( &ctx->N );
  454. /*
  455. * D = E^-1 mod ((P-1)*(Q-1))
  456. * DP = D mod (P - 1)
  457. * DQ = D mod (Q - 1)
  458. * QP = Q^-1 mod P
  459. */
  460. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) );
  461. #if !defined(MBEDTLS_RSA_NO_CRT)
  462. MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
  463. &ctx->DP, &ctx->DQ, &ctx->QP ) );
  464. #endif /* MBEDTLS_RSA_NO_CRT */
  465. /* Double-check */
  466. MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
  467. cleanup:
  468. mbedtls_mpi_free( &H );
  469. mbedtls_mpi_free( &G );
  470. if( ret != 0 )
  471. {
  472. mbedtls_rsa_free( ctx );
  473. return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
  474. }
  475. return( 0 );
  476. }
  477. #endif /* MBEDTLS_GENPRIME */
  478. /*
  479. * Check a public RSA key
  480. */
  481. int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
  482. {
  483. if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
  484. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  485. if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
  486. {
  487. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  488. }
  489. if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
  490. mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
  491. mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
  492. {
  493. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  494. }
  495. return( 0 );
  496. }
  497. /*
  498. * Check for the consistency of all fields in an RSA private key context
  499. */
  500. int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
  501. {
  502. if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
  503. rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
  504. {
  505. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  506. }
  507. if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
  508. &ctx->D, &ctx->E, NULL, NULL ) != 0 )
  509. {
  510. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  511. }
  512. #if !defined(MBEDTLS_RSA_NO_CRT)
  513. else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
  514. &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
  515. {
  516. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  517. }
  518. #endif
  519. return( 0 );
  520. }
  521. /*
  522. * Check if contexts holding a public and private key match
  523. */
  524. int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
  525. const mbedtls_rsa_context *prv )
  526. {
  527. if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
  528. mbedtls_rsa_check_privkey( prv ) != 0 )
  529. {
  530. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  531. }
  532. if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
  533. mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
  534. {
  535. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  536. }
  537. return( 0 );
  538. }
  539. /*
  540. * Do an RSA public key operation
  541. */
  542. int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
  543. const unsigned char *input,
  544. unsigned char *output )
  545. {
  546. int ret;
  547. size_t olen;
  548. mbedtls_mpi T;
  549. if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
  550. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  551. mbedtls_mpi_init( &T );
  552. #if defined(MBEDTLS_THREADING_C)
  553. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  554. return( ret );
  555. #endif
  556. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
  557. if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
  558. {
  559. ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  560. goto cleanup;
  561. }
  562. olen = ctx->len;
  563. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
  564. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
  565. cleanup:
  566. #if defined(MBEDTLS_THREADING_C)
  567. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  568. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  569. #endif
  570. mbedtls_mpi_free( &T );
  571. if( ret != 0 )
  572. return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
  573. return( 0 );
  574. }
  575. /*
  576. * Generate or update blinding values, see section 10 of:
  577. * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
  578. * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
  579. * Berlin Heidelberg, 1996. p. 104-113.
  580. */
  581. static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
  582. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  583. {
  584. int ret, count = 0;
  585. if( ctx->Vf.p != NULL )
  586. {
  587. /* We already have blinding values, just update them by squaring */
  588. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
  589. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
  590. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
  591. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
  592. goto cleanup;
  593. }
  594. /* Unblinding value: Vf = random number, invertible mod N */
  595. do {
  596. if( count++ > 10 )
  597. return( MBEDTLS_ERR_RSA_RNG_FAILED );
  598. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
  599. MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
  600. } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
  601. /* Blinding value: Vi = Vf^(-e) mod N */
  602. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
  603. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->Vi, &ctx->Vi, &ctx->E, &ctx->N, &ctx->RN ) );
  604. cleanup:
  605. return( ret );
  606. }
  607. /*
  608. * Exponent blinding supposed to prevent side-channel attacks using multiple
  609. * traces of measurements to recover the RSA key. The more collisions are there,
  610. * the more bits of the key can be recovered. See [3].
  611. *
  612. * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
  613. * observations on avarage.
  614. *
  615. * For example with 28 byte blinding to achieve 2 collisions the adversary has
  616. * to make 2^112 observations on avarage.
  617. *
  618. * (With the currently (as of 2017 April) known best algorithms breaking 2048
  619. * bit RSA requires approximately as much time as trying out 2^112 random keys.
  620. * Thus in this sense with 28 byte blinding the security is not reduced by
  621. * side-channel attacks like the one in [3])
  622. *
  623. * This countermeasure does not help if the key recovery is possible with a
  624. * single trace.
  625. */
  626. #define RSA_EXPONENT_BLINDING 28
  627. /*
  628. * Do an RSA private key operation
  629. */
  630. int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
  631. int (*f_rng)(void *, unsigned char *, size_t),
  632. void *p_rng,
  633. const unsigned char *input,
  634. unsigned char *output )
  635. {
  636. int ret;
  637. size_t olen;
  638. mbedtls_mpi T, T1, T2;
  639. mbedtls_mpi P1, Q1, R;
  640. #if defined(MBEDTLS_RSA_NO_CRT)
  641. mbedtls_mpi D_blind;
  642. mbedtls_mpi *D = &ctx->D;
  643. #else
  644. mbedtls_mpi DP_blind, DQ_blind;
  645. mbedtls_mpi *DP = &ctx->DP;
  646. mbedtls_mpi *DQ = &ctx->DQ;
  647. #endif
  648. if( rsa_check_context( ctx, 1 /* private key checks */,
  649. f_rng != NULL /* blinding y/n */ ) != 0 )
  650. {
  651. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  652. }
  653. mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
  654. mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
  655. if( f_rng != NULL )
  656. {
  657. #if defined(MBEDTLS_RSA_NO_CRT)
  658. mbedtls_mpi_init( &D_blind );
  659. #else
  660. mbedtls_mpi_init( &DP_blind );
  661. mbedtls_mpi_init( &DQ_blind );
  662. #endif
  663. }
  664. #if defined(MBEDTLS_THREADING_C)
  665. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  666. return( ret );
  667. #endif
  668. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
  669. if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
  670. {
  671. ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  672. goto cleanup;
  673. }
  674. if( f_rng != NULL )
  675. {
  676. /*
  677. * Blinding
  678. * T = T * Vi mod N
  679. */
  680. MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
  681. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
  682. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
  683. /*
  684. * Exponent blinding
  685. */
  686. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
  687. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
  688. #if defined(MBEDTLS_RSA_NO_CRT)
  689. /*
  690. * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
  691. */
  692. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
  693. f_rng, p_rng ) );
  694. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
  695. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
  696. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
  697. D = &D_blind;
  698. #else
  699. /*
  700. * DP_blind = ( P - 1 ) * R + DP
  701. */
  702. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
  703. f_rng, p_rng ) );
  704. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
  705. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
  706. &ctx->DP ) );
  707. DP = &DP_blind;
  708. /*
  709. * DQ_blind = ( Q - 1 ) * R + DQ
  710. */
  711. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
  712. f_rng, p_rng ) );
  713. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
  714. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
  715. &ctx->DQ ) );
  716. DQ = &DQ_blind;
  717. #endif /* MBEDTLS_RSA_NO_CRT */
  718. }
  719. #if defined(MBEDTLS_RSA_NO_CRT)
  720. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
  721. #else
  722. /*
  723. * Faster decryption using the CRT
  724. *
  725. * T1 = input ^ dP mod P
  726. * T2 = input ^ dQ mod Q
  727. */
  728. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
  729. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
  730. /*
  731. * T = (T1 - T2) * (Q^-1 mod P) mod P
  732. */
  733. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
  734. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
  735. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
  736. /*
  737. * T = T2 + T * Q
  738. */
  739. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
  740. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
  741. #endif /* MBEDTLS_RSA_NO_CRT */
  742. if( f_rng != NULL )
  743. {
  744. /*
  745. * Unblind
  746. * T = T * Vf mod N
  747. */
  748. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
  749. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
  750. }
  751. olen = ctx->len;
  752. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
  753. cleanup:
  754. #if defined(MBEDTLS_THREADING_C)
  755. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  756. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  757. #endif
  758. mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
  759. mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
  760. if( f_rng != NULL )
  761. {
  762. #if defined(MBEDTLS_RSA_NO_CRT)
  763. mbedtls_mpi_free( &D_blind );
  764. #else
  765. mbedtls_mpi_free( &DP_blind );
  766. mbedtls_mpi_free( &DQ_blind );
  767. #endif
  768. }
  769. if( ret != 0 )
  770. return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
  771. return( 0 );
  772. }
  773. #if defined(MBEDTLS_PKCS1_V21)
  774. /**
  775. * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
  776. *
  777. * \param dst buffer to mask
  778. * \param dlen length of destination buffer
  779. * \param src source of the mask generation
  780. * \param slen length of the source buffer
  781. * \param md_ctx message digest context to use
  782. */
  783. static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
  784. size_t slen, mbedtls_md_context_t *md_ctx )
  785. {
  786. unsigned char mask[MBEDTLS_MD_MAX_SIZE];
  787. unsigned char counter[4];
  788. unsigned char *p;
  789. unsigned int hlen;
  790. size_t i, use_len;
  791. int ret = 0;
  792. memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
  793. memset( counter, 0, 4 );
  794. hlen = mbedtls_md_get_size( md_ctx->md_info );
  795. /* Generate and apply dbMask */
  796. p = dst;
  797. while( dlen > 0 )
  798. {
  799. use_len = hlen;
  800. if( dlen < hlen )
  801. use_len = dlen;
  802. if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
  803. goto exit;
  804. if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
  805. goto exit;
  806. if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
  807. goto exit;
  808. if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
  809. goto exit;
  810. for( i = 0; i < use_len; ++i )
  811. *p++ ^= mask[i];
  812. counter[3]++;
  813. dlen -= use_len;
  814. }
  815. exit:
  816. mbedtls_zeroize( mask, sizeof( mask ) );
  817. return( ret );
  818. }
  819. #endif /* MBEDTLS_PKCS1_V21 */
  820. #if defined(MBEDTLS_PKCS1_V21)
  821. /*
  822. * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
  823. */
  824. int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
  825. int (*f_rng)(void *, unsigned char *, size_t),
  826. void *p_rng,
  827. int mode,
  828. const unsigned char *label, size_t label_len,
  829. size_t ilen,
  830. const unsigned char *input,
  831. unsigned char *output )
  832. {
  833. size_t olen;
  834. int ret;
  835. unsigned char *p = output;
  836. unsigned int hlen;
  837. const mbedtls_md_info_t *md_info;
  838. mbedtls_md_context_t md_ctx;
  839. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
  840. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  841. if( f_rng == NULL )
  842. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  843. md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
  844. if( md_info == NULL )
  845. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  846. olen = ctx->len;
  847. hlen = mbedtls_md_get_size( md_info );
  848. /* first comparison checks for overflow */
  849. if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
  850. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  851. memset( output, 0, olen );
  852. *p++ = 0;
  853. /* Generate a random octet string seed */
  854. if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
  855. return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
  856. p += hlen;
  857. /* Construct DB */
  858. if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
  859. return( ret );
  860. p += hlen;
  861. p += olen - 2 * hlen - 2 - ilen;
  862. *p++ = 1;
  863. memcpy( p, input, ilen );
  864. mbedtls_md_init( &md_ctx );
  865. if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
  866. goto exit;
  867. /* maskedDB: Apply dbMask to DB */
  868. if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
  869. &md_ctx ) ) != 0 )
  870. goto exit;
  871. /* maskedSeed: Apply seedMask to seed */
  872. if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
  873. &md_ctx ) ) != 0 )
  874. goto exit;
  875. exit:
  876. mbedtls_md_free( &md_ctx );
  877. if( ret != 0 )
  878. return( ret );
  879. return( ( mode == MBEDTLS_RSA_PUBLIC )
  880. ? mbedtls_rsa_public( ctx, output, output )
  881. : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
  882. }
  883. #endif /* MBEDTLS_PKCS1_V21 */
  884. #if defined(MBEDTLS_PKCS1_V15)
  885. /*
  886. * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
  887. */
  888. int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
  889. int (*f_rng)(void *, unsigned char *, size_t),
  890. void *p_rng,
  891. int mode, size_t ilen,
  892. const unsigned char *input,
  893. unsigned char *output )
  894. {
  895. size_t nb_pad, olen;
  896. int ret;
  897. unsigned char *p = output;
  898. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
  899. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  900. // We don't check p_rng because it won't be dereferenced here
  901. if( f_rng == NULL || input == NULL || output == NULL )
  902. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  903. olen = ctx->len;
  904. /* first comparison checks for overflow */
  905. if( ilen + 11 < ilen || olen < ilen + 11 )
  906. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  907. nb_pad = olen - 3 - ilen;
  908. *p++ = 0;
  909. if( mode == MBEDTLS_RSA_PUBLIC )
  910. {
  911. *p++ = MBEDTLS_RSA_CRYPT;
  912. while( nb_pad-- > 0 )
  913. {
  914. int rng_dl = 100;
  915. do {
  916. ret = f_rng( p_rng, p, 1 );
  917. } while( *p == 0 && --rng_dl && ret == 0 );
  918. /* Check if RNG failed to generate data */
  919. if( rng_dl == 0 || ret != 0 )
  920. return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
  921. p++;
  922. }
  923. }
  924. else
  925. {
  926. *p++ = MBEDTLS_RSA_SIGN;
  927. while( nb_pad-- > 0 )
  928. *p++ = 0xFF;
  929. }
  930. *p++ = 0;
  931. memcpy( p, input, ilen );
  932. return( ( mode == MBEDTLS_RSA_PUBLIC )
  933. ? mbedtls_rsa_public( ctx, output, output )
  934. : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
  935. }
  936. #endif /* MBEDTLS_PKCS1_V15 */
  937. /*
  938. * Add the message padding, then do an RSA operation
  939. */
  940. int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
  941. int (*f_rng)(void *, unsigned char *, size_t),
  942. void *p_rng,
  943. int mode, size_t ilen,
  944. const unsigned char *input,
  945. unsigned char *output )
  946. {
  947. switch( ctx->padding )
  948. {
  949. #if defined(MBEDTLS_PKCS1_V15)
  950. case MBEDTLS_RSA_PKCS_V15:
  951. return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
  952. input, output );
  953. #endif
  954. #if defined(MBEDTLS_PKCS1_V21)
  955. case MBEDTLS_RSA_PKCS_V21:
  956. return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
  957. ilen, input, output );
  958. #endif
  959. default:
  960. return( MBEDTLS_ERR_RSA_INVALID_PADDING );
  961. }
  962. }
  963. #if defined(MBEDTLS_PKCS1_V21)
  964. /*
  965. * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
  966. */
  967. int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
  968. int (*f_rng)(void *, unsigned char *, size_t),
  969. void *p_rng,
  970. int mode,
  971. const unsigned char *label, size_t label_len,
  972. size_t *olen,
  973. const unsigned char *input,
  974. unsigned char *output,
  975. size_t output_max_len )
  976. {
  977. int ret;
  978. size_t ilen, i, pad_len;
  979. unsigned char *p, bad, pad_done;
  980. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  981. unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
  982. unsigned int hlen;
  983. const mbedtls_md_info_t *md_info;
  984. mbedtls_md_context_t md_ctx;
  985. /*
  986. * Parameters sanity checks
  987. */
  988. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
  989. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  990. ilen = ctx->len;
  991. if( ilen < 16 || ilen > sizeof( buf ) )
  992. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  993. md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
  994. if( md_info == NULL )
  995. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  996. hlen = mbedtls_md_get_size( md_info );
  997. // checking for integer underflow
  998. if( 2 * hlen + 2 > ilen )
  999. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1000. /*
  1001. * RSA operation
  1002. */
  1003. ret = ( mode == MBEDTLS_RSA_PUBLIC )
  1004. ? mbedtls_rsa_public( ctx, input, buf )
  1005. : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
  1006. if( ret != 0 )
  1007. goto cleanup;
  1008. /*
  1009. * Unmask data and generate lHash
  1010. */
  1011. mbedtls_md_init( &md_ctx );
  1012. if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
  1013. {
  1014. mbedtls_md_free( &md_ctx );
  1015. goto cleanup;
  1016. }
  1017. /* seed: Apply seedMask to maskedSeed */
  1018. if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
  1019. &md_ctx ) ) != 0 ||
  1020. /* DB: Apply dbMask to maskedDB */
  1021. ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
  1022. &md_ctx ) ) != 0 )
  1023. {
  1024. mbedtls_md_free( &md_ctx );
  1025. goto cleanup;
  1026. }
  1027. mbedtls_md_free( &md_ctx );
  1028. /* Generate lHash */
  1029. if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
  1030. goto cleanup;
  1031. /*
  1032. * Check contents, in "constant-time"
  1033. */
  1034. p = buf;
  1035. bad = 0;
  1036. bad |= *p++; /* First byte must be 0 */
  1037. p += hlen; /* Skip seed */
  1038. /* Check lHash */
  1039. for( i = 0; i < hlen; i++ )
  1040. bad |= lhash[i] ^ *p++;
  1041. /* Get zero-padding len, but always read till end of buffer
  1042. * (minus one, for the 01 byte) */
  1043. pad_len = 0;
  1044. pad_done = 0;
  1045. for( i = 0; i < ilen - 2 * hlen - 2; i++ )
  1046. {
  1047. pad_done |= p[i];
  1048. pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
  1049. }
  1050. p += pad_len;
  1051. bad |= *p++ ^ 0x01;
  1052. /*
  1053. * The only information "leaked" is whether the padding was correct or not
  1054. * (eg, no data is copied if it was not correct). This meets the
  1055. * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
  1056. * the different error conditions.
  1057. */
  1058. if( bad != 0 )
  1059. {
  1060. ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
  1061. goto cleanup;
  1062. }
  1063. if( ilen - ( p - buf ) > output_max_len )
  1064. {
  1065. ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
  1066. goto cleanup;
  1067. }
  1068. *olen = ilen - (p - buf);
  1069. memcpy( output, p, *olen );
  1070. ret = 0;
  1071. cleanup:
  1072. mbedtls_zeroize( buf, sizeof( buf ) );
  1073. mbedtls_zeroize( lhash, sizeof( lhash ) );
  1074. return( ret );
  1075. }
  1076. #endif /* MBEDTLS_PKCS1_V21 */
  1077. #if defined(MBEDTLS_PKCS1_V15)
  1078. /*
  1079. * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
  1080. */
  1081. int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
  1082. int (*f_rng)(void *, unsigned char *, size_t),
  1083. void *p_rng,
  1084. int mode, size_t *olen,
  1085. const unsigned char *input,
  1086. unsigned char *output,
  1087. size_t output_max_len)
  1088. {
  1089. int ret;
  1090. size_t ilen, pad_count = 0, i;
  1091. unsigned char *p, bad, pad_done = 0;
  1092. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  1093. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
  1094. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1095. ilen = ctx->len;
  1096. if( ilen < 16 || ilen > sizeof( buf ) )
  1097. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1098. ret = ( mode == MBEDTLS_RSA_PUBLIC )
  1099. ? mbedtls_rsa_public( ctx, input, buf )
  1100. : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
  1101. if( ret != 0 )
  1102. goto cleanup;
  1103. p = buf;
  1104. bad = 0;
  1105. /*
  1106. * Check and get padding len in "constant-time"
  1107. */
  1108. bad |= *p++; /* First byte must be 0 */
  1109. /* This test does not depend on secret data */
  1110. if( mode == MBEDTLS_RSA_PRIVATE )
  1111. {
  1112. bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
  1113. /* Get padding len, but always read till end of buffer
  1114. * (minus one, for the 00 byte) */
  1115. for( i = 0; i < ilen - 3; i++ )
  1116. {
  1117. pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
  1118. pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
  1119. }
  1120. p += pad_count;
  1121. bad |= *p++; /* Must be zero */
  1122. }
  1123. else
  1124. {
  1125. bad |= *p++ ^ MBEDTLS_RSA_SIGN;
  1126. /* Get padding len, but always read till end of buffer
  1127. * (minus one, for the 00 byte) */
  1128. for( i = 0; i < ilen - 3; i++ )
  1129. {
  1130. pad_done |= ( p[i] != 0xFF );
  1131. pad_count += ( pad_done == 0 );
  1132. }
  1133. p += pad_count;
  1134. bad |= *p++; /* Must be zero */
  1135. }
  1136. bad |= ( pad_count < 8 );
  1137. if( bad )
  1138. {
  1139. ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
  1140. goto cleanup;
  1141. }
  1142. if( ilen - ( p - buf ) > output_max_len )
  1143. {
  1144. ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
  1145. goto cleanup;
  1146. }
  1147. *olen = ilen - (p - buf);
  1148. memcpy( output, p, *olen );
  1149. ret = 0;
  1150. cleanup:
  1151. mbedtls_zeroize( buf, sizeof( buf ) );
  1152. return( ret );
  1153. }
  1154. #endif /* MBEDTLS_PKCS1_V15 */
  1155. /*
  1156. * Do an RSA operation, then remove the message padding
  1157. */
  1158. int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
  1159. int (*f_rng)(void *, unsigned char *, size_t),
  1160. void *p_rng,
  1161. int mode, size_t *olen,
  1162. const unsigned char *input,
  1163. unsigned char *output,
  1164. size_t output_max_len)
  1165. {
  1166. switch( ctx->padding )
  1167. {
  1168. #if defined(MBEDTLS_PKCS1_V15)
  1169. case MBEDTLS_RSA_PKCS_V15:
  1170. return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
  1171. input, output, output_max_len );
  1172. #endif
  1173. #if defined(MBEDTLS_PKCS1_V21)
  1174. case MBEDTLS_RSA_PKCS_V21:
  1175. return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
  1176. olen, input, output,
  1177. output_max_len );
  1178. #endif
  1179. default:
  1180. return( MBEDTLS_ERR_RSA_INVALID_PADDING );
  1181. }
  1182. }
  1183. #if defined(MBEDTLS_PKCS1_V21)
  1184. /*
  1185. * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
  1186. */
  1187. int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
  1188. int (*f_rng)(void *, unsigned char *, size_t),
  1189. void *p_rng,
  1190. int mode,
  1191. mbedtls_md_type_t md_alg,
  1192. unsigned int hashlen,
  1193. const unsigned char *hash,
  1194. unsigned char *sig )
  1195. {
  1196. size_t olen;
  1197. unsigned char *p = sig;
  1198. unsigned char salt[MBEDTLS_MD_MAX_SIZE];
  1199. unsigned int slen, hlen, offset = 0;
  1200. int ret;
  1201. size_t msb;
  1202. const mbedtls_md_info_t *md_info;
  1203. mbedtls_md_context_t md_ctx;
  1204. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
  1205. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1206. if( f_rng == NULL )
  1207. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1208. olen = ctx->len;
  1209. if( md_alg != MBEDTLS_MD_NONE )
  1210. {
  1211. /* Gather length of hash to sign */
  1212. md_info = mbedtls_md_info_from_type( md_alg );
  1213. if( md_info == NULL )
  1214. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1215. hashlen = mbedtls_md_get_size( md_info );
  1216. }
  1217. md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
  1218. if( md_info == NULL )
  1219. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1220. hlen = mbedtls_md_get_size( md_info );
  1221. slen = hlen;
  1222. if( olen < hlen + slen + 2 )
  1223. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1224. memset( sig, 0, olen );
  1225. /* Generate salt of length slen */
  1226. if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
  1227. return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
  1228. /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
  1229. msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
  1230. p += olen - hlen * 2 - 2;
  1231. *p++ = 0x01;
  1232. memcpy( p, salt, slen );
  1233. p += slen;
  1234. mbedtls_md_init( &md_ctx );
  1235. if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
  1236. goto exit;
  1237. /* Generate H = Hash( M' ) */
  1238. if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
  1239. goto exit;
  1240. if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
  1241. goto exit;
  1242. if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
  1243. goto exit;
  1244. if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
  1245. goto exit;
  1246. if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
  1247. goto exit;
  1248. /* Compensate for boundary condition when applying mask */
  1249. if( msb % 8 == 0 )
  1250. offset = 1;
  1251. /* maskedDB: Apply dbMask to DB */
  1252. if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
  1253. &md_ctx ) ) != 0 )
  1254. goto exit;
  1255. msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
  1256. sig[0] &= 0xFF >> ( olen * 8 - msb );
  1257. p += hlen;
  1258. *p++ = 0xBC;
  1259. mbedtls_zeroize( salt, sizeof( salt ) );
  1260. exit:
  1261. mbedtls_md_free( &md_ctx );
  1262. if( ret != 0 )
  1263. return( ret );
  1264. return( ( mode == MBEDTLS_RSA_PUBLIC )
  1265. ? mbedtls_rsa_public( ctx, sig, sig )
  1266. : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
  1267. }
  1268. #endif /* MBEDTLS_PKCS1_V21 */
  1269. #if defined(MBEDTLS_PKCS1_V15)
  1270. /*
  1271. * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
  1272. */
  1273. /* Construct a PKCS v1.5 encoding of a hashed message
  1274. *
  1275. * This is used both for signature generation and verification.
  1276. *
  1277. * Parameters:
  1278. * - md_alg: Identifies the hash algorithm used to generate the given hash;
  1279. * MBEDTLS_MD_NONE if raw data is signed.
  1280. * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
  1281. * - hash: Buffer containing the hashed message or the raw data.
  1282. * - dst_len: Length of the encoded message.
  1283. * - dst: Buffer to hold the encoded message.
  1284. *
  1285. * Assumptions:
  1286. * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
  1287. * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
  1288. * - dst points to a buffer of size at least dst_len.
  1289. *
  1290. */
  1291. static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
  1292. unsigned int hashlen,
  1293. const unsigned char *hash,
  1294. size_t dst_len,
  1295. unsigned char *dst )
  1296. {
  1297. size_t oid_size = 0;
  1298. size_t nb_pad = dst_len;
  1299. unsigned char *p = dst;
  1300. const char *oid = NULL;
  1301. /* Are we signing hashed or raw data? */
  1302. if( md_alg != MBEDTLS_MD_NONE )
  1303. {
  1304. const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
  1305. if( md_info == NULL )
  1306. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1307. if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
  1308. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1309. hashlen = mbedtls_md_get_size( md_info );
  1310. /* Double-check that 8 + hashlen + oid_size can be used as a
  1311. * 1-byte ASN.1 length encoding and that there's no overflow. */
  1312. if( 8 + hashlen + oid_size >= 0x80 ||
  1313. 10 + hashlen < hashlen ||
  1314. 10 + hashlen + oid_size < 10 + hashlen )
  1315. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1316. /*
  1317. * Static bounds check:
  1318. * - Need 10 bytes for five tag-length pairs.
  1319. * (Insist on 1-byte length encodings to protect against variants of
  1320. * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
  1321. * - Need hashlen bytes for hash
  1322. * - Need oid_size bytes for hash alg OID.
  1323. */
  1324. if( nb_pad < 10 + hashlen + oid_size )
  1325. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1326. nb_pad -= 10 + hashlen + oid_size;
  1327. }
  1328. else
  1329. {
  1330. if( nb_pad < hashlen )
  1331. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1332. nb_pad -= hashlen;
  1333. }
  1334. /* Need space for signature header and padding delimiter (3 bytes),
  1335. * and 8 bytes for the minimal padding */
  1336. if( nb_pad < 3 + 8 )
  1337. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1338. nb_pad -= 3;
  1339. /* Now nb_pad is the amount of memory to be filled
  1340. * with padding, and at least 8 bytes long. */
  1341. /* Write signature header and padding */
  1342. *p++ = 0;
  1343. *p++ = MBEDTLS_RSA_SIGN;
  1344. memset( p, 0xFF, nb_pad );
  1345. p += nb_pad;
  1346. *p++ = 0;
  1347. /* Are we signing raw data? */
  1348. if( md_alg == MBEDTLS_MD_NONE )
  1349. {
  1350. memcpy( p, hash, hashlen );
  1351. return( 0 );
  1352. }
  1353. /* Signing hashed data, add corresponding ASN.1 structure
  1354. *
  1355. * DigestInfo ::= SEQUENCE {
  1356. * digestAlgorithm DigestAlgorithmIdentifier,
  1357. * digest Digest }
  1358. * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
  1359. * Digest ::= OCTET STRING
  1360. *
  1361. * Schematic:
  1362. * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
  1363. * TAG-NULL + LEN [ NULL ] ]
  1364. * TAG-OCTET + LEN [ HASH ] ]
  1365. */
  1366. *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
  1367. *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
  1368. *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
  1369. *p++ = (unsigned char)( 0x04 + oid_size );
  1370. *p++ = MBEDTLS_ASN1_OID;
  1371. *p++ = (unsigned char) oid_size;
  1372. memcpy( p, oid, oid_size );
  1373. p += oid_size;
  1374. *p++ = MBEDTLS_ASN1_NULL;
  1375. *p++ = 0x00;
  1376. *p++ = MBEDTLS_ASN1_OCTET_STRING;
  1377. *p++ = (unsigned char) hashlen;
  1378. memcpy( p, hash, hashlen );
  1379. p += hashlen;
  1380. /* Just a sanity-check, should be automatic
  1381. * after the initial bounds check. */
  1382. if( p != dst + dst_len )
  1383. {
  1384. mbedtls_zeroize( dst, dst_len );
  1385. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1386. }
  1387. return( 0 );
  1388. }
  1389. /*
  1390. * Do an RSA operation to sign the message digest
  1391. */
  1392. int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
  1393. int (*f_rng)(void *, unsigned char *, size_t),
  1394. void *p_rng,
  1395. int mode,
  1396. mbedtls_md_type_t md_alg,
  1397. unsigned int hashlen,
  1398. const unsigned char *hash,
  1399. unsigned char *sig )
  1400. {
  1401. int ret;
  1402. unsigned char *sig_try = NULL, *verif = NULL;
  1403. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
  1404. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1405. /*
  1406. * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
  1407. */
  1408. if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
  1409. ctx->len, sig ) ) != 0 )
  1410. return( ret );
  1411. /*
  1412. * Call respective RSA primitive
  1413. */
  1414. if( mode == MBEDTLS_RSA_PUBLIC )
  1415. {
  1416. /* Skip verification on a public key operation */
  1417. return( mbedtls_rsa_public( ctx, sig, sig ) );
  1418. }
  1419. /* Private key operation
  1420. *
  1421. * In order to prevent Lenstra's attack, make the signature in a
  1422. * temporary buffer and check it before returning it.
  1423. */
  1424. sig_try = mbedtls_calloc( 1, ctx->len );
  1425. if( sig_try == NULL )
  1426. return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
  1427. verif = mbedtls_calloc( 1, ctx->len );
  1428. if( verif == NULL )
  1429. {
  1430. mbedtls_free( sig_try );
  1431. return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
  1432. }
  1433. MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
  1434. MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
  1435. if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
  1436. {
  1437. ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
  1438. goto cleanup;
  1439. }
  1440. memcpy( sig, sig_try, ctx->len );
  1441. cleanup:
  1442. mbedtls_free( sig_try );
  1443. mbedtls_free( verif );
  1444. return( ret );
  1445. }
  1446. #endif /* MBEDTLS_PKCS1_V15 */
  1447. /*
  1448. * Do an RSA operation to sign the message digest
  1449. */
  1450. int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
  1451. int (*f_rng)(void *, unsigned char *, size_t),
  1452. void *p_rng,
  1453. int mode,
  1454. mbedtls_md_type_t md_alg,
  1455. unsigned int hashlen,
  1456. const unsigned char *hash,
  1457. unsigned char *sig )
  1458. {
  1459. switch( ctx->padding )
  1460. {
  1461. #if defined(MBEDTLS_PKCS1_V15)
  1462. case MBEDTLS_RSA_PKCS_V15:
  1463. return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
  1464. hashlen, hash, sig );
  1465. #endif
  1466. #if defined(MBEDTLS_PKCS1_V21)
  1467. case MBEDTLS_RSA_PKCS_V21:
  1468. return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
  1469. hashlen, hash, sig );
  1470. #endif
  1471. default:
  1472. return( MBEDTLS_ERR_RSA_INVALID_PADDING );
  1473. }
  1474. }
  1475. #if defined(MBEDTLS_PKCS1_V21)
  1476. /*
  1477. * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
  1478. */
  1479. int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
  1480. int (*f_rng)(void *, unsigned char *, size_t),
  1481. void *p_rng,
  1482. int mode,
  1483. mbedtls_md_type_t md_alg,
  1484. unsigned int hashlen,
  1485. const unsigned char *hash,
  1486. mbedtls_md_type_t mgf1_hash_id,
  1487. int expected_salt_len,
  1488. const unsigned char *sig )
  1489. {
  1490. int ret;
  1491. size_t siglen;
  1492. unsigned char *p;
  1493. unsigned char *hash_start;
  1494. unsigned char result[MBEDTLS_MD_MAX_SIZE];
  1495. unsigned char zeros[8];
  1496. unsigned int hlen;
  1497. size_t observed_salt_len, msb;
  1498. const mbedtls_md_info_t *md_info;
  1499. mbedtls_md_context_t md_ctx;
  1500. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  1501. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
  1502. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1503. siglen = ctx->len;
  1504. if( siglen < 16 || siglen > sizeof( buf ) )
  1505. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1506. ret = ( mode == MBEDTLS_RSA_PUBLIC )
  1507. ? mbedtls_rsa_public( ctx, sig, buf )
  1508. : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
  1509. if( ret != 0 )
  1510. return( ret );
  1511. p = buf;
  1512. if( buf[siglen - 1] != 0xBC )
  1513. return( MBEDTLS_ERR_RSA_INVALID_PADDING );
  1514. if( md_alg != MBEDTLS_MD_NONE )
  1515. {
  1516. /* Gather length of hash to sign */
  1517. md_info = mbedtls_md_info_from_type( md_alg );
  1518. if( md_info == NULL )
  1519. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1520. hashlen = mbedtls_md_get_size( md_info );
  1521. }
  1522. md_info = mbedtls_md_info_from_type( mgf1_hash_id );
  1523. if( md_info == NULL )
  1524. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1525. hlen = mbedtls_md_get_size( md_info );
  1526. memset( zeros, 0, 8 );
  1527. /*
  1528. * Note: EMSA-PSS verification is over the length of N - 1 bits
  1529. */
  1530. msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
  1531. if( buf[0] >> ( 8 - siglen * 8 + msb ) )
  1532. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1533. /* Compensate for boundary condition when applying mask */
  1534. if( msb % 8 == 0 )
  1535. {
  1536. p++;
  1537. siglen -= 1;
  1538. }
  1539. if( siglen < hlen + 2 )
  1540. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1541. hash_start = p + siglen - hlen - 1;
  1542. mbedtls_md_init( &md_ctx );
  1543. if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
  1544. goto exit;
  1545. ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
  1546. if( ret != 0 )
  1547. goto exit;
  1548. buf[0] &= 0xFF >> ( siglen * 8 - msb );
  1549. while( p < hash_start - 1 && *p == 0 )
  1550. p++;
  1551. if( *p++ != 0x01 )
  1552. {
  1553. ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
  1554. goto exit;
  1555. }
  1556. observed_salt_len = hash_start - p;
  1557. if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
  1558. observed_salt_len != (size_t) expected_salt_len )
  1559. {
  1560. ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
  1561. goto exit;
  1562. }
  1563. /*
  1564. * Generate H = Hash( M' )
  1565. */
  1566. ret = mbedtls_md_starts( &md_ctx );
  1567. if ( ret != 0 )
  1568. goto exit;
  1569. ret = mbedtls_md_update( &md_ctx, zeros, 8 );
  1570. if ( ret != 0 )
  1571. goto exit;
  1572. ret = mbedtls_md_update( &md_ctx, hash, hashlen );
  1573. if ( ret != 0 )
  1574. goto exit;
  1575. ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
  1576. if ( ret != 0 )
  1577. goto exit;
  1578. ret = mbedtls_md_finish( &md_ctx, result );
  1579. if ( ret != 0 )
  1580. goto exit;
  1581. if( memcmp( hash_start, result, hlen ) != 0 )
  1582. {
  1583. ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
  1584. goto exit;
  1585. }
  1586. exit:
  1587. mbedtls_md_free( &md_ctx );
  1588. return( ret );
  1589. }
  1590. /*
  1591. * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
  1592. */
  1593. int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
  1594. int (*f_rng)(void *, unsigned char *, size_t),
  1595. void *p_rng,
  1596. int mode,
  1597. mbedtls_md_type_t md_alg,
  1598. unsigned int hashlen,
  1599. const unsigned char *hash,
  1600. const unsigned char *sig )
  1601. {
  1602. mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
  1603. ? (mbedtls_md_type_t) ctx->hash_id
  1604. : md_alg;
  1605. return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
  1606. md_alg, hashlen, hash,
  1607. mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
  1608. sig ) );
  1609. }
  1610. #endif /* MBEDTLS_PKCS1_V21 */
  1611. #if defined(MBEDTLS_PKCS1_V15)
  1612. /*
  1613. * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
  1614. */
  1615. int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
  1616. int (*f_rng)(void *, unsigned char *, size_t),
  1617. void *p_rng,
  1618. int mode,
  1619. mbedtls_md_type_t md_alg,
  1620. unsigned int hashlen,
  1621. const unsigned char *hash,
  1622. const unsigned char *sig )
  1623. {
  1624. int ret = 0;
  1625. const size_t sig_len = ctx->len;
  1626. unsigned char *encoded = NULL, *encoded_expected = NULL;
  1627. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
  1628. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  1629. /*
  1630. * Prepare expected PKCS1 v1.5 encoding of hash.
  1631. */
  1632. if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
  1633. ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
  1634. {
  1635. ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
  1636. goto cleanup;
  1637. }
  1638. if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
  1639. encoded_expected ) ) != 0 )
  1640. goto cleanup;
  1641. /*
  1642. * Apply RSA primitive to get what should be PKCS1 encoded hash.
  1643. */
  1644. ret = ( mode == MBEDTLS_RSA_PUBLIC )
  1645. ? mbedtls_rsa_public( ctx, sig, encoded )
  1646. : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
  1647. if( ret != 0 )
  1648. goto cleanup;
  1649. /*
  1650. * Compare
  1651. */
  1652. if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
  1653. sig_len ) ) != 0 )
  1654. {
  1655. ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
  1656. goto cleanup;
  1657. }
  1658. cleanup:
  1659. if( encoded != NULL )
  1660. {
  1661. mbedtls_zeroize( encoded, sig_len );
  1662. mbedtls_free( encoded );
  1663. }
  1664. if( encoded_expected != NULL )
  1665. {
  1666. mbedtls_zeroize( encoded_expected, sig_len );
  1667. mbedtls_free( encoded_expected );
  1668. }
  1669. return( ret );
  1670. }
  1671. #endif /* MBEDTLS_PKCS1_V15 */
  1672. /*
  1673. * Do an RSA operation and check the message digest
  1674. */
  1675. int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
  1676. int (*f_rng)(void *, unsigned char *, size_t),
  1677. void *p_rng,
  1678. int mode,
  1679. mbedtls_md_type_t md_alg,
  1680. unsigned int hashlen,
  1681. const unsigned char *hash,
  1682. const unsigned char *sig )
  1683. {
  1684. switch( ctx->padding )
  1685. {
  1686. #if defined(MBEDTLS_PKCS1_V15)
  1687. case MBEDTLS_RSA_PKCS_V15:
  1688. return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
  1689. hashlen, hash, sig );
  1690. #endif
  1691. #if defined(MBEDTLS_PKCS1_V21)
  1692. case MBEDTLS_RSA_PKCS_V21:
  1693. return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
  1694. hashlen, hash, sig );
  1695. #endif
  1696. default:
  1697. return( MBEDTLS_ERR_RSA_INVALID_PADDING );
  1698. }
  1699. }
  1700. /*
  1701. * Copy the components of an RSA key
  1702. */
  1703. int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
  1704. {
  1705. int ret;
  1706. dst->ver = src->ver;
  1707. dst->len = src->len;
  1708. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
  1709. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
  1710. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
  1711. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
  1712. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
  1713. #if !defined(MBEDTLS_RSA_NO_CRT)
  1714. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
  1715. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
  1716. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
  1717. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
  1718. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
  1719. #endif
  1720. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
  1721. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
  1722. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
  1723. dst->padding = src->padding;
  1724. dst->hash_id = src->hash_id;
  1725. cleanup:
  1726. if( ret != 0 )
  1727. mbedtls_rsa_free( dst );
  1728. return( ret );
  1729. }
  1730. /*
  1731. * Free the components of an RSA key
  1732. */
  1733. void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
  1734. {
  1735. mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
  1736. mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
  1737. mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
  1738. mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
  1739. #if !defined(MBEDTLS_RSA_NO_CRT)
  1740. mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
  1741. mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
  1742. mbedtls_mpi_free( &ctx->DP );
  1743. #endif /* MBEDTLS_RSA_NO_CRT */
  1744. #if defined(MBEDTLS_THREADING_C)
  1745. mbedtls_mutex_free( &ctx->mutex );
  1746. #endif
  1747. }
  1748. #endif /* !MBEDTLS_RSA_ALT */
  1749. #if defined(MBEDTLS_SELF_TEST)
  1750. #include "mbedtls/sha1.h"
  1751. /*
  1752. * Example RSA-1024 keypair, for test purposes
  1753. */
  1754. #define KEY_LEN 128
  1755. #define RSA_N "9292758453063D803DD603D5E777D788" \
  1756. "8ED1D5BF35786190FA2F23EBC0848AEA" \
  1757. "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
  1758. "7130B9CED7ACDF54CFC7555AC14EEBAB" \
  1759. "93A89813FBF3C4F8066D2D800F7C38A8" \
  1760. "1AE31942917403FF4946B0A83D3D3E05" \
  1761. "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
  1762. "5E94BB77B07507233A0BC7BAC8F90F79"
  1763. #define RSA_E "10001"
  1764. #define RSA_D "24BF6185468786FDD303083D25E64EFC" \
  1765. "66CA472BC44D253102F8B4A9D3BFA750" \
  1766. "91386C0077937FE33FA3252D28855837" \
  1767. "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
  1768. "DF79C5CE07EE72C7F123142198164234" \
  1769. "CABB724CF78B8173B9F880FC86322407" \
  1770. "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
  1771. "071513A1E85B5DFA031F21ECAE91A34D"
  1772. #define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
  1773. "2C01CAD19EA484A87EA4377637E75500" \
  1774. "FCB2005C5C7DD6EC4AC023CDA285D796" \
  1775. "C3D9E75E1EFC42488BB4F1D13AC30A57"
  1776. #define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
  1777. "E211C2B9E5DB1ED0BF61D0D9899620F4" \
  1778. "910E4168387E3C30AA1E00C339A79508" \
  1779. "8452DD96A9A5EA5D9DCA68DA636032AF"
  1780. #define PT_LEN 24
  1781. #define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
  1782. "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
  1783. #if defined(MBEDTLS_PKCS1_V15)
  1784. static int myrand( void *rng_state, unsigned char *output, size_t len )
  1785. {
  1786. #if !defined(__OpenBSD__)
  1787. size_t i;
  1788. if( rng_state != NULL )
  1789. rng_state = NULL;
  1790. for( i = 0; i < len; ++i )
  1791. output[i] = rand();
  1792. #else
  1793. if( rng_state != NULL )
  1794. rng_state = NULL;
  1795. arc4random_buf( output, len );
  1796. #endif /* !OpenBSD */
  1797. return( 0 );
  1798. }
  1799. #endif /* MBEDTLS_PKCS1_V15 */
  1800. /*
  1801. * Checkup routine
  1802. */
  1803. int mbedtls_rsa_self_test( int verbose )
  1804. {
  1805. int ret = 0;
  1806. #if defined(MBEDTLS_PKCS1_V15)
  1807. size_t len;
  1808. mbedtls_rsa_context rsa;
  1809. unsigned char rsa_plaintext[PT_LEN];
  1810. unsigned char rsa_decrypted[PT_LEN];
  1811. unsigned char rsa_ciphertext[KEY_LEN];
  1812. #if defined(MBEDTLS_SHA1_C)
  1813. unsigned char sha1sum[20];
  1814. #endif
  1815. mbedtls_mpi K;
  1816. mbedtls_mpi_init( &K );
  1817. mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
  1818. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
  1819. MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
  1820. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
  1821. MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
  1822. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
  1823. MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
  1824. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
  1825. MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
  1826. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
  1827. MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
  1828. MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
  1829. if( verbose != 0 )
  1830. mbedtls_printf( " RSA key validation: " );
  1831. if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
  1832. mbedtls_rsa_check_privkey( &rsa ) != 0 )
  1833. {
  1834. if( verbose != 0 )
  1835. mbedtls_printf( "failed\n" );
  1836. return( 1 );
  1837. }
  1838. if( verbose != 0 )
  1839. mbedtls_printf( "passed\n PKCS#1 encryption : " );
  1840. memcpy( rsa_plaintext, RSA_PT, PT_LEN );
  1841. if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
  1842. PT_LEN, rsa_plaintext,
  1843. rsa_ciphertext ) != 0 )
  1844. {
  1845. if( verbose != 0 )
  1846. mbedtls_printf( "failed\n" );
  1847. return( 1 );
  1848. }
  1849. if( verbose != 0 )
  1850. mbedtls_printf( "passed\n PKCS#1 decryption : " );
  1851. if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
  1852. &len, rsa_ciphertext, rsa_decrypted,
  1853. sizeof(rsa_decrypted) ) != 0 )
  1854. {
  1855. if( verbose != 0 )
  1856. mbedtls_printf( "failed\n" );
  1857. return( 1 );
  1858. }
  1859. if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
  1860. {
  1861. if( verbose != 0 )
  1862. mbedtls_printf( "failed\n" );
  1863. return( 1 );
  1864. }
  1865. if( verbose != 0 )
  1866. mbedtls_printf( "passed\n" );
  1867. #if defined(MBEDTLS_SHA1_C)
  1868. if( verbose != 0 )
  1869. mbedtls_printf( " PKCS#1 data sign : " );
  1870. if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
  1871. {
  1872. if( verbose != 0 )
  1873. mbedtls_printf( "failed\n" );
  1874. return( 1 );
  1875. }
  1876. if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
  1877. MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
  1878. sha1sum, rsa_ciphertext ) != 0 )
  1879. {
  1880. if( verbose != 0 )
  1881. mbedtls_printf( "failed\n" );
  1882. return( 1 );
  1883. }
  1884. if( verbose != 0 )
  1885. mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
  1886. if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
  1887. MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
  1888. sha1sum, rsa_ciphertext ) != 0 )
  1889. {
  1890. if( verbose != 0 )
  1891. mbedtls_printf( "failed\n" );
  1892. return( 1 );
  1893. }
  1894. if( verbose != 0 )
  1895. mbedtls_printf( "passed\n" );
  1896. #endif /* MBEDTLS_SHA1_C */
  1897. if( verbose != 0 )
  1898. mbedtls_printf( "\n" );
  1899. cleanup:
  1900. mbedtls_mpi_free( &K );
  1901. mbedtls_rsa_free( &rsa );
  1902. #else /* MBEDTLS_PKCS1_V15 */
  1903. ((void) verbose);
  1904. #endif /* MBEDTLS_PKCS1_V15 */
  1905. return( ret );
  1906. }
  1907. #endif /* MBEDTLS_SELF_TEST */
  1908. #endif /* MBEDTLS_RSA_C */