test/jdk/sun/security/tools/keytool/fakegen/java.base/sun/security/rsa/RSAKeyPairGenerator.java
changeset 55640 3081f39a3d30
parent 55468 076f34b82b98
child 58679 9c3209ff7550
equal deleted inserted replaced
55639:4722e5e28449 55640:3081f39a3d30
       
     1 /*
       
     2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 package sun.security.rsa;
       
    25 
       
    26 import java.math.BigInteger;
       
    27 
       
    28 import java.security.*;
       
    29 import java.security.spec.AlgorithmParameterSpec;
       
    30 import java.security.spec.RSAKeyGenParameterSpec;
       
    31 
       
    32 import sun.security.x509.AlgorithmId;
       
    33 import static sun.security.rsa.RSAUtil.KeyType;
       
    34 
       
    35 /**
       
    36  * A fake RSA keypair generation.
       
    37  */
       
    38 public abstract class RSAKeyPairGenerator extends KeyPairGeneratorSpi {
       
    39 
       
    40     // public exponent to use
       
    41     private BigInteger publicExponent;
       
    42 
       
    43     // size of the key to generate, >= RSAKeyFactory.MIN_MODLEN
       
    44     private int keySize;
       
    45 
       
    46     private final KeyType type;
       
    47     private AlgorithmId rsaId;
       
    48 
       
    49     RSAKeyPairGenerator(KeyType type, int defKeySize) {
       
    50         this.type = type;
       
    51         // initialize to default in case the app does not call initialize()
       
    52         initialize(defKeySize, null);
       
    53     }
       
    54 
       
    55     // initialize the generator. See JCA doc
       
    56     public void initialize(int keySize, SecureRandom random) {
       
    57         try {
       
    58             initialize(new RSAKeyGenParameterSpec(keySize,
       
    59                     RSAKeyGenParameterSpec.F4), random);
       
    60         } catch (InvalidAlgorithmParameterException iape) {
       
    61             throw new InvalidParameterException(iape.getMessage());
       
    62         }
       
    63     }
       
    64 
       
    65     // second initialize method. See JCA doc.
       
    66     public void initialize(AlgorithmParameterSpec params, SecureRandom random)
       
    67             throws InvalidAlgorithmParameterException {
       
    68         if (params instanceof RSAKeyGenParameterSpec == false) {
       
    69             throw new InvalidAlgorithmParameterException
       
    70                 ("Params must be instance of RSAKeyGenParameterSpec");
       
    71         }
       
    72 
       
    73         RSAKeyGenParameterSpec rsaSpec = (RSAKeyGenParameterSpec)params;
       
    74         int tmpKeySize = rsaSpec.getKeysize();
       
    75         BigInteger tmpPublicExponent = rsaSpec.getPublicExponent();
       
    76         AlgorithmParameterSpec tmpParams = rsaSpec.getKeyParams();
       
    77 
       
    78         if (tmpPublicExponent == null) {
       
    79             tmpPublicExponent = RSAKeyGenParameterSpec.F4;
       
    80         } else {
       
    81             if (tmpPublicExponent.compareTo(RSAKeyGenParameterSpec.F0) < 0) {
       
    82                 throw new InvalidAlgorithmParameterException
       
    83                         ("Public exponent must be 3 or larger");
       
    84             }
       
    85             if (tmpPublicExponent.bitLength() > tmpKeySize) {
       
    86                 throw new InvalidAlgorithmParameterException
       
    87                         ("Public exponent must be smaller than key size");
       
    88             }
       
    89         }
       
    90 
       
    91         // do not allow unreasonably large key sizes, probably user error
       
    92         try {
       
    93             RSAKeyFactory.checkKeyLengths(tmpKeySize, tmpPublicExponent,
       
    94                 512, 64 * 1024);
       
    95         } catch (InvalidKeyException e) {
       
    96             throw new InvalidAlgorithmParameterException(
       
    97                 "Invalid key sizes", e);
       
    98         }
       
    99 
       
   100         try {
       
   101             this.rsaId = RSAUtil.createAlgorithmId(type, tmpParams);
       
   102         } catch (ProviderException e) {
       
   103             throw new InvalidAlgorithmParameterException(
       
   104                 "Invalid key parameters", e);
       
   105         }
       
   106 
       
   107         this.keySize = tmpKeySize;
       
   108         this.publicExponent = tmpPublicExponent;
       
   109     }
       
   110 
       
   111     // generate the keypair. See JCA doc
       
   112     public KeyPair generateKeyPair() {
       
   113 
       
   114         // accommodate odd key sizes in case anybody wants to use them
       
   115         BigInteger e = publicExponent;
       
   116         if (!e.equals(RSAKeyGenParameterSpec.F4)) {
       
   117             throw new AssertionError("Only support F4 now");
       
   118         }
       
   119         BigInteger p, q, n;
       
   120 
       
   121         // Pre-calculated p and q for e == RSAKeyGenParameterSpec.F4
       
   122         switch (keySize) {
       
   123             case 1024:
       
   124                 p = new BigInteger("1220491537800192366196661816910427"
       
   125                         + "2375185130493819649338056226264568132442590"
       
   126                         + "2306195110391300298681932797393339089272174"
       
   127                         + "24490645345596103420476757417659909");
       
   128 
       
   129                 q = new BigInteger("8480533592205316739308384508307319"
       
   130                         + "3310632635895778398980504245932789486455154"
       
   131                         + "4265220956952343855842030266079089174423047"
       
   132                         + "7382175514060777025691485728713063");
       
   133                 break;
       
   134             case 2048:
       
   135                 p = new BigInteger("1600840041787354447543653385760927"
       
   136                         + "2642568308955833364523274045522752644800599"
       
   137                         + "8669541532595690224703734511692014533312515"
       
   138                         + "1867029838883431415692353449578487671384896"
       
   139                         + "6611685764860941767986520897595108597563035"
       
   140                         + "4023785639802607792535812062420427283857665"
       
   141                         + "9883578590844700707106157871508280052743363"
       
   142                         + "65749456332400771");
       
   143                 q = new BigInteger("1303880717101677622201474394769850"
       
   144                         + "7257196073324816341282215626935164930077468"
       
   145                         + "5999131251387556761167658937349436378464220"
       
   146                         + "4831804147777472146628148336776639855791417"
       
   147                         + "3849903041999943901924899580268176393595653"
       
   148                         + "7357080543898614581363167420619163047562600"
       
   149                         + "6155574020606891195960345238780709194499010"
       
   150                         + "43652862954645301");
       
   151                 break;
       
   152             case 3072:
       
   153                 p = new BigInteger("2403380417344527161525447148950543"
       
   154                         + "9379802436047502603774623852967909282895900"
       
   155                         + "7474251859703715384817150107392592636129366"
       
   156                         + "5680725148417789414447073716354703692160825"
       
   157                         + "9910929198523923851672957013786423746474670"
       
   158                         + "5285365870313877239114930859096040034848729"
       
   159                         + "0251735848703378313724697081522892607625120"
       
   160                         + "0765531003751468146539682665307123591367928"
       
   161                         + "1883284784295069877414057796300484015307336"
       
   162                         + "5167008365209621810307777651197826810474895"
       
   163                         + "858836606799546054179898997210174885200767");
       
   164                 q = new BigInteger("2046511893459993309772203642844931"
       
   165                         + "1853027469856467052942666265372903000702193"
       
   166                         + "7794121141028918655144044411085405243565188"
       
   167                         + "8318027937964509940322691121105328104450287"
       
   168                         + "0400405975394764523022670537180050051646095"
       
   169                         + "3320242109876620452477757629185501504625999"
       
   170                         + "8487187901577781067325277853462587849063313"
       
   171                         + "5364789330253471096755661110557598411108366"
       
   172                         + "1566266965711522070909440716658568303529454"
       
   173                         + "8075296935907088988560548322049196321207173"
       
   174                         + "136436923455192617031129191723631954025427");
       
   175                 break;
       
   176             case 3073:
       
   177                 p = new BigInteger("358024230418365680745725628188289"
       
   178                         + "996969857993064124357766607077169315932503"
       
   179                         + "907030633492082868150575329278637502167157"
       
   180                         + "345572819552678004302525872656665704012432"
       
   181                         + "644868036964802792468216554458209893873320"
       
   182                         + "297608885231998895441396822219845215463819"
       
   183                         + "216163684222596923153702349718019538798623"
       
   184                         + "793830598445085650304217449539016339429747"
       
   185                         + "385490484982419227549804698120223647408926"
       
   186                         + "021549912791440569127641517442362359166673"
       
   187                         + "430151753277549861070074076802118983202554"
       
   188                         + "7683523973");
       
   189                 q = new BigInteger("169752169194244078720180277210205"
       
   190                         + "255207796420396114596943944148158798629671"
       
   191                         + "789863988428383183103705641694331073747120"
       
   192                         + "522505020908219489265190380712941311249155"
       
   193                         + "156834674079165492105570443486536740756291"
       
   194                         + "314279877766807786397193023304245520017295"
       
   195                         + "969248707516367324340822062886043618371887"
       
   196                         + "320492710434958863952407959935025922006108"
       
   197                         + "282752473898685457767312430894004833959025"
       
   198                         + "415996648289919841900071076314309778495245"
       
   199                         + "505026665971431792707247787031811654193145"
       
   200                         + "1044952887");
       
   201                 break;
       
   202             case 4096:
       
   203                 p = new BigInteger("2985635754414679487171962796211911"
       
   204                         + "1563710734938215274736352092606404045130913"
       
   205                         + "2477365484439939846705721840432140066578525"
       
   206                         + "0762327458086280430118434094733412377416194"
       
   207                         + "8736124795243564050755767519346747209606612"
       
   208                         + "5835460937739428885308798309679495432910469"
       
   209                         + "0294757621321446003970767164933974474924664"
       
   210                         + "1513767092845098947552598109657871041666676"
       
   211                         + "2945573325433283821164032766425479703026349"
       
   212                         + "9433641551427112483593214628620450175257586"
       
   213                         + "4350119143877183562692754400346175237007314"
       
   214                         + "7121580349193179272551363894896336921717843"
       
   215                         + "3734726842184251708799134654802475890197293"
       
   216                         + "9094908310578403843742664173424031260840446"
       
   217                         + "591633359364559754200663");
       
   218                 q = new BigInteger("2279248439141087793789384816271625"
       
   219                         + "1304008816573950275844533962181244003563987"
       
   220                         + "6638461665174020058827698592331066726709304"
       
   221                         + "9231319346136709972639455506783245161859951"
       
   222                         + "6191872757335765533547033659834427437142631"
       
   223                         + "3801232751161907082392011429712327250253948"
       
   224                         + "6012497852063361866175243227579880020724881"
       
   225                         + "9393797645220239009219998518884396282407710"
       
   226                         + "7199202450846395844337846503427790307364624"
       
   227                         + "5124871273035872938616425951596065309519651"
       
   228                         + "1519189356431513094684173807318945903212527"
       
   229                         + "7712469749366620048658571121822171067675915"
       
   230                         + "5479178304648399924549334007222294762969503"
       
   231                         + "5341584429803583589276956979963609078497238"
       
   232                         + "760757619468018224491053");
       
   233                 break;
       
   234             case 7680:
       
   235                 p = new BigInteger("7034022146817764608206409206476311"
       
   236                         + "1371065855827199565170055133179419153145313"
       
   237                         + "9446295819321510144417300286482767498463255"
       
   238                         + "3370362723164324606829434617977090251035572"
       
   239                         + "4237546099249090966627372485629853471350368"
       
   240                         + "1497807044971675189990783927066958945388379"
       
   241                         + "4004871857862380913954692362042250112646330"
       
   242                         + "0515873693830575810241740671573357342073942"
       
   243                         + "4924331206670599086552994426505996651481869"
       
   244                         + "2750320309695596383830444337180596058381417"
       
   245                         + "7804256675894755482917598033151085120879826"
       
   246                         + "5373459707672000040090469058320463160804122"
       
   247                         + "8041026671361647347262771363754358441620179"
       
   248                         + "3861076191970047581401830740749715862725492"
       
   249                         + "5750077182986169592435996668282677192000592"
       
   250                         + "8019204141383012670399208092972174321639234"
       
   251                         + "6398779487064860458178135981700487728919191"
       
   252                         + "2418625085287791733119321378648688730352488"
       
   253                         + "6446800847442687530322927871063574241918202"
       
   254                         + "1883228799435533794167861499482410970370569"
       
   255                         + "6964781912514810444018313637829915454156777"
       
   256                         + "4591735979781496237582756842195362757823524"
       
   257                         + "2054367976655738752756266147542536910268553"
       
   258                         + "7448833810249238249431673425245843356607647"
       
   259                         + "4372260685985882252621076435844355190011754"
       
   260                         + "0656637395317040751098009821385816878380790"
       
   261                         + "9017022563392158510700243843871343995665725"
       
   262                         + "9447");
       
   263                 q = new BigInteger("7006097449483280289139842436346899"
       
   264                         + "9530483972793167261845287040200424598484519"
       
   265                         + "0516644904497480849788319004016685820778949"
       
   266                         + "8514891188272714344395538114013074460410497"
       
   267                         + "8359325044727263936454825245761418873141623"
       
   268                         + "2797767712619624879346520836103128457266811"
       
   269                         + "0175147598232556414901292143389437071453369"
       
   270                         + "4867355570712858957689566849950544938172174"
       
   271                         + "9136345403774445991224311311204082279382451"
       
   272                         + "1977673518543939329498617563049052032259503"
       
   273                         + "1057378839061411322975567618416093845144604"
       
   274                         + "4563500271329350225709899627798126176885843"
       
   275                         + "9953695524756659384424971651654225913916902"
       
   276                         + "7087953078966988666226779422822854729725410"
       
   277                         + "1118666318953891716194309185187152294057704"
       
   278                         + "2509582485250984938518224243061447485603856"
       
   279                         + "0197517312286511585545481984194518805766300"
       
   280                         + "7351141154720653351520788558277866920369211"
       
   281                         + "9269193267018615410395030712426882576851775"
       
   282                         + "9240726416572698143674385203349496226355950"
       
   283                         + "0327319442530888807531774734409411628435155"
       
   284                         + "6449625936156829175453919098709371623972260"
       
   285                         + "4090598351878256110255744263233419699486705"
       
   286                         + "3658219936031243191809689036438631617494147"
       
   287                         + "8183898954195399872571907981872765548994005"
       
   288                         + "0754996819683869837696561316479950526914796"
       
   289                         + "3613549619563212818369365543951734053135086"
       
   290                         + "8593");
       
   291                 break;
       
   292             case 7681:
       
   293                 p = new BigInteger("1051422012172516922972337796421710"
       
   294                         + "1479918158427421377774284533124281583092508"
       
   295                         + "4961153980854679659383249048667658521370629"
       
   296                         + "6404078819943386006431233653506112030196761"
       
   297                         + "7596078830067404622117784094070983115834842"
       
   298                         + "0487926993058767148294211654032287322495894"
       
   299                         + "1733666341539644761909820700670130074920230"
       
   300                         + "0423405253327351685101039824822997870642295"
       
   301                         + "8176417666723292317903368285439828460328413"
       
   302                         + "9578515412811253729224087502906626189200448"
       
   303                         + "4062549581079444644685394008569377879899377"
       
   304                         + "0578136407363616816108795867392262638913436"
       
   305                         + "2041457188733948013731661456602199370852579"
       
   306                         + "2394583215214266552642082043674873685065171"
       
   307                         + "9053703728689139119006448644148961706358756"
       
   308                         + "0498696516029596534138345596800696824976461"
       
   309                         + "8461070371773003241093580622731426623849732"
       
   310                         + "8737707046931934781383331589291927520571138"
       
   311                         + "2759511573966362005294438252298268405782746"
       
   312                         + "9642105075721912120520774388679358659061825"
       
   313                         + "8867415333830751976884165265610029632416351"
       
   314                         + "6666642762305875140340531095190000199307531"
       
   315                         + "2186952558457690950270489966695323839026041"
       
   316                         + "0797018634946454573060304991245539422899112"
       
   317                         + "9312288231966690989900334936215870198735213"
       
   318                         + "6745631923445509394027128331099748294658904"
       
   319                         + "6303809606943116678969915369410846798143779"
       
   320                         + "89187");
       
   321                 q = new BigInteger("8160183444544784072886152354850963"
       
   322                         + "2507221530634202707531181684048014518183560"
       
   323                         + "4586261698204521358487995639975786964316103"
       
   324                         + "5861955444259138806900123878887948650444640"
       
   325                         + "7062365744302695074634248387572850855758081"
       
   326                         + "9074631976865727985289995811413472254074432"
       
   327                         + "4751574514329862821405069035713821908598155"
       
   328                         + "5436571566703295992717666421591959121774983"
       
   329                         + "5913798632992394695663301960139778059658979"
       
   330                         + "4944466598070019481108421780314115410201334"
       
   331                         + "2925668957166258484312039121420100795544341"
       
   332                         + "5372918969907063685116069321551182817324799"
       
   333                         + "3347159610063964281388144113123539915925090"
       
   334                         + "2692309096312451442405059120315891913678403"
       
   335                         + "4977738880336169371406947347468157606390086"
       
   336                         + "3381587148478097717352225924672548657441771"
       
   337                         + "3887371520215341151934095945869956390140929"
       
   338                         + "3098286281540595154062683214111934217839063"
       
   339                         + "3309526631019699109621050440794920159910038"
       
   340                         + "3248965999877529393614116991972153758910967"
       
   341                         + "1712258745878268303349611893651932564447696"
       
   342                         + "9601760120187828039923387985032881949408596"
       
   343                         + "0689784023540256749586591441103043888423326"
       
   344                         + "3078541569639917739590630101701573133092711"
       
   345                         + "6157430583592378472242178997833136144409256"
       
   346                         + "3558220277370179546822023437190315852529873"
       
   347                         + "0831890147323301322699740037060134979740963"
       
   348                         + "4289");
       
   349                 break;
       
   350             case 8192:
       
   351                 p = new BigInteger("9821669838446774374944535804569858"
       
   352                         + "0553278885576950130485823829973470553571905"
       
   353                         + "3014418421996241500307589880457361653957913"
       
   354                         + "9176499436767288125182942994089196450118944"
       
   355                         + "8701794862752733776161684616570463744619126"
       
   356                         + "4981622564763630694110472008409561205704867"
       
   357                         + "0221819623405201369630462487520858670679048"
       
   358                         + "5854008441429858453634949980424333056803703"
       
   359                         + "1205609490778445762604050796894221725977551"
       
   360                         + "1428887194691696420765173256600200430067305"
       
   361                         + "4364524177041858044598166859757042904625691"
       
   362                         + "4292728453597609683799189454690202563236931"
       
   363                         + "8171122071288244573793276051041975005528757"
       
   364                         + "0228306442708182141334279133965507583927772"
       
   365                         + "9244311696220253059281524393613278272067808"
       
   366                         + "7017494446447670799055720358621918361716353"
       
   367                         + "5018317015764698318012095108914870478138809"
       
   368                         + "8204738169777192718869484177321870413838036"
       
   369                         + "8149216482968887382371881239714335470844573"
       
   370                         + "1862934371951394070111726593305334971041399"
       
   371                         + "5517260339034138718517336990212463882142363"
       
   372                         + "9154412320743552301967162100734381046548816"
       
   373                         + "3883737645359595416600487444018399886391071"
       
   374                         + "3777667222706059170707223589163679915863781"
       
   375                         + "4662302526078720977228426750718207481384357"
       
   376                         + "7918717041190413457052439016978578217755022"
       
   377                         + "7370720979516554707297685239584071755267452"
       
   378                         + "6021894842754355160100506065457679069228273"
       
   379                         + "95209345267367982516553449135291473361");
       
   380                 q = new BigInteger("7902448465953646210110784092684896"
       
   381                         + "0265474424590294110174550047938700740921014"
       
   382                         + "1981650823416127449143596912363210790070524"
       
   383                         + "2903784112701128957948996730263815210531364"
       
   384                         + "0489145287401377007608600217628773627723381"
       
   385                         + "1194123533939872283952535576847014977682278"
       
   386                         + "9332064706645169741712060131540562788886577"
       
   387                         + "3762235020990267901959745687867018811088495"
       
   388                         + "3716021011509120447248882358515954471433808"
       
   389                         + "2782236662758287959413069553620728137831579"
       
   390                         + "2321174813204514354999978428741310035945405"
       
   391                         + "0226661395731921098764192439072425262100813"
       
   392                         + "9732949866553839713092238096261034339815187"
       
   393                         + "2832617055364163276140160068136296115910569"
       
   394                         + "9466440903693740716929166334256441926903849"
       
   395                         + "1082968246155177124035336609654226388424434"
       
   396                         + "5775783323612758615407928446164631651292743"
       
   397                         + "8428509642959278732826297890909454571009075"
       
   398                         + "7836191622138731918099379467912681177757761"
       
   399                         + "6141378131042432093843778753846726589215845"
       
   400                         + "7402160146427434508515156204064224022904659"
       
   401                         + "8645441448874409852211668374267341177082462"
       
   402                         + "7341410218867175406105046487057429530801973"
       
   403                         + "0931082058719258230993681115780999537424968"
       
   404                         + "2385515792331573549935317407789344892257264"
       
   405                         + "7464569110078675090194686816764429827739815"
       
   406                         + "0566036514181547634372488184242167294602000"
       
   407                         + "8232780963578241583529875079397308150506597"
       
   408                         + "37190564909892937290776929541076192569");
       
   409                 break;
       
   410             default:
       
   411                 throw new AssertionError("Unknown keySize " + keySize);
       
   412         }
       
   413 
       
   414         n = p.multiply(q);
       
   415 
       
   416         // phi = (p - 1) * (q - 1) must be relative prime to e
       
   417         // otherwise RSA just won't work ;-)
       
   418         BigInteger p1 = p.subtract(BigInteger.ONE);
       
   419         BigInteger q1 = q.subtract(BigInteger.ONE);
       
   420         BigInteger phi = p1.multiply(q1);
       
   421         // generate new p and q until they work. typically
       
   422         // the first try will succeed when using F4
       
   423         if (e.gcd(phi).equals(BigInteger.ONE) == false) {
       
   424             throw new AssertionError("Should not happen");
       
   425         }
       
   426 
       
   427         // private exponent d is the inverse of e mod phi
       
   428         BigInteger d = e.modInverse(phi);
       
   429 
       
   430         // 1st prime exponent pe = d mod (p - 1)
       
   431         BigInteger pe = d.mod(p1);
       
   432         // 2nd prime exponent qe = d mod (q - 1)
       
   433         BigInteger qe = d.mod(q1);
       
   434 
       
   435         // crt coefficient coeff is the inverse of q mod p
       
   436         BigInteger coeff = q.modInverse(p);
       
   437 
       
   438         try {
       
   439             PublicKey publicKey = new RSAPublicKeyImpl(rsaId, n, e);
       
   440             PrivateKey privateKey = new RSAPrivateCrtKeyImpl(
       
   441                     rsaId, n, e, d, p, q, pe, qe, coeff);
       
   442             return new KeyPair(publicKey, privateKey);
       
   443         } catch (InvalidKeyException exc) {
       
   444             // invalid key exception only thrown for keys < 512 bit,
       
   445             // will not happen here
       
   446             throw new RuntimeException(exc);
       
   447         }
       
   448     }
       
   449 }