rsa.c 69 KB

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