Skip to content

alan-mushi/libressl-RSA-backdoor

Repository files navigation

Implementation of a 'backdoor' into RSA key generation in LibreSSL, original paper here (french).

The modified code used to generate the flawed keys: crypto/rsa/rsa_gen.diff.

Start by compiling LibreSSL:

git clone --recursive https://github.com/alan-mushi/libressl-RSA-backdoor.git && cd libressl-RSA-backdoor
mkdir build && cd build
cmake ..
make -j 4

Then compile the attack code:

cd rsa_backdoor_attack
make

Generate RSA keys (works for 1024, 2048 and 4096 key length):

~/libressl-RSA-backdoor/build $ ./apps/openssl genrsa -out /tmp/private_key.pem 4096
Generating RSA private key, 4096 bit long modulus

[RSA backdoor keygen] STEP 1

[RSA backdoor keygen] M = 261097220353288126672938177679156095644991062261845945096058370820988476992889364212206702983749389585222526678609815709496893359546448401815809021962841319486489244135927499585090397533595929578607017502963986556594079709849428186418083671086146654374201977176450926017821012185029652278616994445899507251671734744220446946486726407547565235149894863358205867325756674110764756253993099966928553885423458889971322871579559478608624183521952968159874118775047260337252104265418773417083462637758243022067387692495904092352983253803449206459297022958414187805329623211592031387556499575103086196215648918623048654255951626478311402706432958845021902155525708567549424550578292254419501668798871269980409104842571343781196003726789783864995697628349902887948567776707783522646068221069947888712445738580883629266305817265348726496923250530740848921945719737110154001853236418729955762642910594288704080345157761475729034231677085714110182611974992975445366440868305962566813264974948999022699867300443656204429612466863914812544582267618279858291387701892055461642936593324221228204880079364250610231654227718537096269602982451130745464334744412025781521475750325603366797431668304122877782900730195434508359022560951177085100788547584

[RSA backdoor keygen] d1 = 253266331108459042877954581524118722595974501479640924072000569439126758509088631982403994686712878069348015540240526683495797795130113239006767262824338603946605334680267915265
.................................................................................................................................++
...........................++

[RSA backdoor keygen] p = 28541862452204735570100022594825671452154551895693405988841464116035713030148722030627254702451277333987103112162689476439566264560562399785716513185012645494342865001987622327294704459553499547638044527517695284412305293446737815485942204917707165600657944282140981102582059107803483048080152864209575248664588754877580839526306058879520124877251590146031908107105075062512410138893857965885302280622448530702485649955328729782595803461845782089835193442074498354492264649092505646951306790694350286914558364965864444937444870317985776542065677049382240673489009802644783616539931251366240092760581560328305130733037

[RSA backdoor keygen] q = 26616028872944985358889484569824024398960479673938097255199045225655436627341357045370829598910496098329168624740597485658247446970714419434102123604697537063717529416352456119082073104078500446885809684176547504107806056597109060272530653153162441441622306041494087501840970432060174782967798327356073642227009327257421213670368501282921014311831499566749372140382315051135969078276983920962985062982717177023013889179624542291392645875750753490367288360048196212135146898511249858463949186421138411736693860169927379320593156855145372310991789138934539974577103124650470039213337157722205506639327161847246248420223

[RSA backdoor keygen] n = 759671035115505604121678577489014536495631223574031932647849539075307602858761167465249360498975792165261792336930434091481971363122406096655293095521183260323865510181960783556513088285750880803664358166371884322306292053383065594735870001609541877005237808854573461723053233499282119917149174712924948565179905349055781707537047522343911381866136053616771274057224114740736204384873852515194392887786661995659363368701079580457317855851674425997729849245348970716043958975709270647411401079911562915324472627911905486037638872708026125842727715116280517326182084944689786833405941759097975242282408065824703179323002489453254579710018322995279742736978923396248539156530122420971586067105733789248157007735450816399718224759103292985006081236576544082922174910738560716366979643789475280457947071961317215296517629938719009160053320616638543801854706867946657899965531406784826652668710217323807686198358731544934303498086180279222298183532243292539065142882765264430807371822803526610748591786466304794539462966806873592364855141439270873663208524895476863800028498684429430336851300733473784838286555136360668608698747037924571090496902570242442726537118815850254552235375409396072168966156994353884461280429356513454847205007251

[RSA backdoor keygen] STEP 2

[RSA backdoor keygen] STEP 3

[RSA backdoor keygen] e1 = 279758910393302105002652789505026982308325759838552528530341026328920302451106844356597021568511458747638563298457476632090963856831626900854155278322664116198054090535698473979073397970653487054686100129359258852676728033825186805026047324365763284472284142273509949141263056621635085494883807815380608345563498199026182428000074811875606669383391228290079495297788982934359996542400100624976442170230287345956435875548041231753423667284307175951965054475062405201031500524632620093546435947051129689009797349792403421507354764415646335452019694713132236648006109792605006230244682987576225515801637343870799049279798235354323036142602552498054187919443585855955866681988520932601483588294049420553439787701288370299882262379552958392565036219386433192607035318594037113552132896298974894292187611763422033543579489214364031942628960142021698308342221405423561956936618388263402053269871713958629787254725537547494347791983687533243135214143692256325903389838983790668102105655728037884848113788740492964536233850096550677376620821257453906918769705128669353725470894113568473759845789149064857097188915414522617928087327584447675342787527029456476257376922531592520496514520678781186564357951461424419742609152567929942297104921329

[RSA backdoor keygen] d1 = 253266331108459042877954581524118722595974501479640924072000569439126758509088631982403994686712878069348015540240526683495797795130113239006767262824338603946605334680267915265

[RSA backdoor keygen] phi = 759671035115505604121678577489014536495631223574031932647849539075307602858761167465249360498975792165261792336930434091481971363122406096655293095521183260323865510181960783556513088285750880803664358166371884322306292053383065594735870001609541877005237808854573461723053233499282119917149174712924948565179905349055781707537047522343911381866136053616771274057224114740736204384873852515194392887786661995659363368701079580457317855851674425997729849245348970716043958975709270647411401079911562915324472627911905486037638872708026125842727715116280517326182084944689786833405941759097975242282408065824703179322947331561929429989089333488115093041127808364678907653286081911629894917448243710172158923434089042967401953022200006022908267525045267263702356273948850533808919249371135202011570294397685215301993775727024766371533209266594696926096234009875788292923251156461191584064287187783944028367310780353368654607194582197087296130335568732376624003693682174718026091575316136497100212569295462907691175623201707884639355602304317601589220075557880328219826016882306735770223889185870029332871299159245179910047494812788779266238864543069311577684061349661937771587309296468776915310403725944796015681029447791279295825853992

[RSA backdoor keygen] e1M = 540856130746590231675590967184183077953316822100398473626399397149908779443996208568803724552260848332861089977067292341587857216378075302669964300285505435684543334671625973564163795504249416633293117632323245409270807743674614991444130995451909938846486119449960875159084068806664737773500802261280115597235232943246629374486801219423171904533286091648285362623545657045124752796393200591904996055653746235927758747127600710362047850806260144111839173250109665538283604790051393510629898584809372711077185042288307513860338018219095541911316717671546424453335733004197037617801182562679311712017286262493847703535749861832634438849035511343076090074969294423505291232566813187020985257092920690533848892543859714081078266106342742257560733847736336080555603095301820636198201117368922783004633350344305662809885306479712758439552210672762547230287941142533715958789854806993357815912782308247333867599883299023223382023660773247353317826118685231771269830707289753234915370630677036907547981089184149168965846316960465489921203088875733765210157407020724815368407487437789701964725868513315467328843143133059714197690310035578420807122271441482257778852672857195887293946188982904064347258681656858928101631713519107027397893468913

[RSA backdoor keygen] STEP 4

[RSA backdoor keygen] e = 540856130746590231675590967184183077953316822100398473626399397149908779443996208568803724552260848332861089977067292341587857216378075302669964300285505435684543334671625973564163795504249416633293117632323245409270807743674614991444130995451909938846486119449960875159084068806664737773500802261280115597235232943246629374486801219423171904533286091648285362623545657045124752796393200591904996055653746235927758747127600710362047850806260144111839173250109665538283604790051393510629898584809372711077185042288307513860338018219095541911316717671546424453335733004197037617801182562679311712017286262493847703535749861832634438849035511343076090074969294423505291232566813187020985257092920690533848892543859714081078266106342742257560733847736336080555603095301820636198201117368922783004633350344305662809885306479712758439552210672762547230287941142533715958789854806993357815912782308247333867599883299023223382023660773247353317826118685231771269830707289753234915370630677036907547981089184149168965846316960465489921203088875733765210157407020724815368407487437789701964725868513315467328843143133059714197690310035578420807122271441482257778852672857195887293946188982904064347258681656858928101631713519107027397893468913

[RSA backdoor keygen] d = 737163731518495508182702069841062333909657262519933787789214459654125387231099074473877111259431062983418516246873404682636751069234759583835213020442751952289222088894070630529784252956408720996469360513043457986628341885076516181016342860278516138890418808548635613567360005478064226547850809059263727151506532636480862148374065542165017640981698218982395398640608166632061207609112749270498384173350310645861246135713139257030281022639110125410276554457583525807036325953169973683279403857545170938127687607793099593672224929941362159473270782467962773306369589797624388510537920358123634724253494478977332803691187110398608467965849687660072995407614878110029219467189124786586333152390406157534082618511216255577574242287730071698151796859839586630816269867631760748007075157939743931629867217886344949855567419777979230264025435790333605863738615584621523516368958956174387758503718863891544686063749482110828737445625406497924099165820767932327127995867220945222516446750390694160893100186034812248744851556679792716726571009983302868500746653685903598538541193782473376674335850918134428688293009292972578666746904793492579746703686985892113894047253791591015646902766291552807820663582733557115532030856172620185406241662081
e is 4006604100520984733 (0x379A5133972D409D)

~/libressl-RSA-backdoor/build $ openssl rsa -check -in /tmp/private_key.pem -noout	# Note that this is OpenSSL installed on my system
RSA key ok

Extract the public key to a file:

~/libressl-RSA-backdoor/build $ ./apps/openssl rsa -in /tmp/private_key.pem -outform PEM -pubout -out /tmp/public_key.pem
writing RSA key

Launch the attack code to recover the private key from the public key mod and exp (sometimes you need to run it multiple times):

~/libressl-RSA-backdoor/rsa_backdoor_attack $ ./formattor.sh /tmp/public_key.pem 
mod="759671035115505604121678577489014536495631223574031932647849539075307602858761167465249360498975792165261792336930434091481971363122406096655293095521183260323865510181960783556513088285750880803664358166371884322306292053383065594735870001609541877005237808854573461723053233499282119917149174712924948565179905349055781707537047522343911381866136053616771274057224114740736204384873852515194392887786661995659363368701079580457317855851674425997729849245348970716043958975709270647411401079911562915324472627911905486037638872708026125842727715116280517326182084944689786833405941759097975242282408065824703179323002489453254579710018322995279742736978923396248539156530122420971586067105733789248157007735450816399718224759103292985006081236576544082922174910738560716366979643789475280457947071961317215296517629938719009160053320616638543801854706867946657899965531406784826652668710217323807686198358731544934303498086180279222298183532243292539065142882765264430807371822803526610748591786466304794539462966806873592364855141439270873663208524895476863800028498684429430336851300733473784838286555136360668608698747037924571090496902570242442726537118815850254552235375409396072168966156994353884461280429356513454847205007251"
exp="540856130746590231675590967184183077953316822100398473626399397149908779443996208568803724552260848332861089977067292341587857216378075302669964300285505435684543334671625973564163795504249416633293117632323245409270807743674614991444130995451909938846486119449960875159084068806664737773500802261280115597235232943246629374486801219423171904533286091648285362623545657045124752796393200591904996055653746235927758747127600710362047850806260144111839173250109665538283604790051393510629898584809372711077185042288307513860338018219095541911316717671546424453335733004197037617801182562679311712017286262493847703535749861832634438849035511343076090074969294423505291232566813187020985257092920690533848892543859714081078266106342742257560733847736336080555603095301820636198201117368922783004633350344305662809885306479712758439552210672762547230287941142533715958789854806993357815912782308247333867599883299023223382023660773247353317826118685231771269830707289753234915370630677036907547981089184149168965846316960465489921203088875733765210157407020724815368407487437789701964725868513315467328843143133059714197690310035578420807122271441482257778852672857195887293946188982904064347258681656858928101631713519107027397893468913"

~/libressl-RSA-backdoor/rsa_backdoor_attack $ mod="759671035115505604121678577489014536495631223574031932647849539075307602858761167465249360498975792165261792336930434091481971363122406096655293095521183260323865510181960783556513088285750880803664358166371884322306292053383065594735870001609541877005237808854573461723053233499282119917149174712924948565179905349055781707537047522343911381866136053616771274057224114740736204384873852515194392887786661995659363368701079580457317855851674425997729849245348970716043958975709270647411401079911562915324472627911905486037638872708026125842727715116280517326182084944689786833405941759097975242282408065824703179323002489453254579710018322995279742736978923396248539156530122420971586067105733789248157007735450816399718224759103292985006081236576544082922174910738560716366979643789475280457947071961317215296517629938719009160053320616638543801854706867946657899965531406784826652668710217323807686198358731544934303498086180279222298183532243292539065142882765264430807371822803526610748591786466304794539462966806873592364855141439270873663208524895476863800028498684429430336851300733473784838286555136360668608698747037924571090496902570242442726537118815850254552235375409396072168966156994353884461280429356513454847205007251"

~/libressl-RSA-backdoor/rsa_backdoor_attack $ exp=540856130746590231675590967184183077953316822100398473626399397149908779443996208568803724552260848332861089977067292341587857216378075302669964300285505435684543334671625973564163795504249416633293117632323245409270807743674614991444130995451909938846486119449960875159084068806664737773500802261280115597235232943246629374486801219423171904533286091648285362623545657045124752796393200591904996055653746235927758747127600710362047850806260144111839173250109665538283604790051393510629898584809372711077185042288307513860338018219095541911316717671546424453335733004197037617801182562679311712017286262493847703535749861832634438849035511343076090074969294423505291232566813187020985257092920690533848892543859714081078266106342742257560733847736336080555603095301820636198201117368922783004633350344305662809885306479712758439552210672762547230287941142533715958789854806993357815912782308247333867599883299023223382023660773247353317826118685231771269830707289753234915370630677036907547981089184149168965846316960465489921203088875733765210157407020724815368407487437789701964725868513315467328843143133059714197690310035578420807122271441482257778852672857195887293946188982904064347258681656858928101631713519107027397893468913

~/libressl-RSA-backdoor/rsa_backdoor_attack $ M="261097220353288126672938177679156095644991062261845945096058370820988476992889364212206702983749389585222526678609815709496893359546448401815809021962841319486489244135927499585090397533595929578607017502963986556594079709849428186418083671086146654374201977176450926017821012185029652278616994445899507251671734744220446946486726407547565235149894863358205867325756674110764756253993099966928553885423458889971322871579559478608624183521952968159874118775047260337252104265418773417083462637758243022067387692495904092352983253803449206459297022958414187805329623211592031387556499575103086196215648918623048654255951626478311402706432958845021902155525708567549424550578292254419501668798871269980409104842571343781196003726789783864995697628349902887948567776707783522646068221069947888712445738580883629266305817265348726496923250530740848921945719737110154001853236418729955762642910594288704080345157761475729034231677085714110182611974992975445366440868305962566813264974948999022699867300443656204429612466863914812544582267618279858291387701892055461642936593324221228204880079364250610231654227718537096269602982451130745464334744412025781521475750325603366797431668304122877782900730195434508359022560951177085100788547584"

~/libressl-RSA-backdoor/rsa_backdoor_attack $ ./main $M $mod $exp

[RSA backdoor attack] M = 261097220353288126672938177679156095644991062261845945096058370820988476992889364212206702983749389585222526678609815709496893359546448401815809021962841319486489244135927499585090397533595929578607017502963986556594079709849428186418083671086146654374201977176450926017821012185029652278616994445899507251671734744220446946486726407547565235149894863358205867325756674110764756253993099966928553885423458889971322871579559478608624183521952968159874118775047260337252104265418773417083462637758243022067387692495904092352983253803449206459297022958414187805329623211592031387556499575103086196215648918623048654255951626478311402706432958845021902155525708567549424550578292254419501668798871269980409104842571343781196003726789783864995697628349902887948567776707783522646068221069947888712445738580883629266305817265348726496923250530740848921945719737110154001853236418729955762642910594288704080345157761475729034231677085714110182611974992975445366440868305962566813264974948999022699867300443656204429612466863914812544582267618279858291387701892055461642936593324221228204880079364250610231654227718537096269602982451130745464334744412025781521475750325603366797431668304122877782900730195434508359022560951177085100788547584

[RSA backdoor attack] n = 759671035115505604121678577489014536495631223574031932647849539075307602858761167465249360498975792165261792336930434091481971363122406096655293095521183260323865510181960783556513088285750880803664358166371884322306292053383065594735870001609541877005237808854573461723053233499282119917149174712924948565179905349055781707537047522343911381866136053616771274057224114740736204384873852515194392887786661995659363368701079580457317855851674425997729849245348970716043958975709270647411401079911562915324472627911905486037638872708026125842727715116280517326182084944689786833405941759097975242282408065824703179323002489453254579710018322995279742736978923396248539156530122420971586067105733789248157007735450816399718224759103292985006081236576544082922174910738560716366979643789475280457947071961317215296517629938719009160053320616638543801854706867946657899965531406784826652668710217323807686198358731544934303498086180279222298183532243292539065142882765264430807371822803526610748591786466304794539462966806873592364855141439270873663208524895476863800028498684429430336851300733473784838286555136360668608698747037924571090496902570242442726537118815850254552235375409396072168966156994353884461280429356513454847205007251

[RSA backdoor attack] e = 540856130746590231675590967184183077953316822100398473626399397149908779443996208568803724552260848332861089977067292341587857216378075302669964300285505435684543334671625973564163795504249416633293117632323245409270807743674614991444130995451909938846486119449960875159084068806664737773500802261280115597235232943246629374486801219423171904533286091648285362623545657045124752796393200591904996055653746235927758747127600710362047850806260144111839173250109665538283604790051393510629898584809372711077185042288307513860338018219095541911316717671546424453335733004197037617801182562679311712017286262493847703535749861832634438849035511343076090074969294423505291232566813187020985257092920690533848892543859714081078266106342742257560733847736336080555603095301820636198201117368922783004633350344305662809885306479712758439552210672762547230287941142533715958789854806993357815912782308247333867599883299023223382023660773247353317826118685231771269830707289753234915370630677036907547981089184149168965846316960465489921203088875733765210157407020724815368407487437789701964725868513315467328843143133059714197690310035578420807122271441482257778852672857195887293946188982904064347258681656858928101631713519107027397893468913

[RSA backdoor attack] p1 = 26616028872944985358889484569824024398960479673938097255199045225655436627341357045370829598910496098329168624740597485658247446970714419434102123604697537063717529416352456119082073104078500446885809684176547504107806056597109060272530653153162441441622306041494087501840970432060174782967798327356073642227009327257421213670368501282921014311831499566749372140382315051135969078276983920962985062982717177023013889179624542291392645875750753490367288360048196212135146898511249858463949186421138411736693860169927379320593156855145372310991789138934539974577103124650470039213337157722205506639327161847246248420223

[RSA backdoor attack] q1 = 28541862452204735570100022594825671452154551895693405988841464116035713030148722030627254702451277333987103112162689476439566264560562399785716513185012645494342865001987622327294704459553499547638044527517695284412305293446737815485942204917707165600657944282140981102582059107803483048080152864209575248664588754877580839526306058879520124877251590146031908107105075062512410138893857965885302280622448530702485649955328729782595803461845782089835193442074498354492264649092505646951306790694350286914558364965864444937444870317985776542065677049382240673489009802644783616539931251366240092760581560328305130733037

[RSA backdoor attack] d2 = 737163731518495508182702069841062333909657262519933787789214459654125387231099074473877111259431062983418516246873404682636751069234759583835213020442751952289222088894070630529784252956408720996469360513043457986628341885076516181016342860278516138890418808548635613567360005478064226547850809059263727151506532636480862148374065542165017640981698218982395398640608166632061207609112749270498384173350310645861246135713139257030281022639110125410276554457583525807036325953169973683279403857545170938127687607793099593672224929941362159473270782467962773306369589797624388510537920358123634724253494478977332803691187110398608467965849687660072995407614878110029219467189124786586333152390406157534082618511216255577574242287730071698151796859839586630816269867631760748007075157939743931629867217886344949855567419777979230264025435790333605863738615584621523516368958956174387758503718863891544686063749482110828737445625406497924099165820767932327127995867220945222516446750390694160893100186034812248744851556679792716726571009983302868500746653685903598538541193782473376674335850918134428688293009292972578666746904793492579746703686985892113894047253791591015646902766291552807820663582733557115532030856172620185406241662081

d2 and d are a match, we also have an equality of the factors (p and q), we found the private exponent from the public key (e, n) and our special value M!

About

LibreSSL with a backdoor in RSA key generation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published