RSAéµã®çææã«ç¢ºççç´ æ°å¤å®æ³ã使ã£ã¦åé¡ãªãã®ã
ååè¨äºãRSA公開鍵から素数の積を取り出す方法ãã§ãç´¹ä»ãã¾ããããRSAéµã®çæã«ã¯å·¨å¤§ãª2ã¤ã®ç´ æ°p,qãå¿ è¦ã§ããè¿å¹´ä¸è¬çã«ä½¿ããã¦ãã2048bit RSAéµã®å ´åãp,qã®å¤§ããã¯1024bitã10é²ã§ç´308æ¡ã®æ°ã«ãªãã¾ãã
ãã®RSAã®ã¢ã«ã´ãªãºã ä¸ã§ã¯pã¨qãæ³ã¨ãããã§ã«ãã¼ã®å°å®çï¼æ£ç¢ºã«ã¯ãã®æ¡å¼µã§ãããªã¤ã©ã¼ã®å®çï¼ãå©ç¨ãã¦ãã¾ããã¤ã¾ããpã¨qãåææ°ã ã¨RSAæå·ã®å¤§åæãçã£ã¦ãã¾ãã¾ãã®ã§ãpã¨qã«ã¯ç¢ºå®ã«ç´ æ°ãé¸ã¶å¿ è¦ãããã¾ãã
ã¨ããã§ãOpenSSLã®RSAéµçæã®å®è£ ã§ã¯ãpã¨qã®ç´ æ°å¤å®ã«Miller-Rabinç´ æ°å¤å®æ³ãç¨ãããã¦ãã¾ããMiller-Rabinç´ æ°å¤å®æ³ã¯çå´èª¤ãã®ç¢ºççã¢ã«ã´ãªãºã ã§ãããã¶ãç´ æ°ãã確å®ã«åææ°ãã®å¤å®ãã§ãããããªãã®ã§ããpã¨qã®ç´ æ°æ§ãéè¦ãªã®ã«ããã®å¤å®ã«ç¢ºççã¢ã«ã´ãªãºã ã使ã£ã¦ãåé¡ãªãã®ã§ããããï¼
åææ°ã®p,qã§RSAéµãä½ã£ã¦ã¿ã
ãã®ã¯è©¦ãã§ãåææ°ã®p,qã§RSAéµãä½ã£ã¦ã¿ã¾ããããOpenSSLã®RSAéµçæã®å®ä½ã¯bn_prime.cã®BN_generate_prime_exé¢æ°ã«ããã¾ãã
*** openssl-1.0.1h-orig/crypto/bn/bn_prime.c 2014-06-05 16:22:48.000000000 +0900 --- openssl-1.0.1h/crypto/bn/bn_prime.c 2014-06-09 09:08:35.000000000 +0900 *************** *** 196,201 **** --- 196,202 ---- if (!safe) { + checks=-1; i=BN_is_prime_fasttest_ex(ret,checks,ctx,0,cb); if (i == -1) goto err; if (i == 0) goto loop;
ä¸è¨ã®ããã«1è¡è¿½å ãããã¨ã§ãp,qãç´ æ°ãã©ãã確èªããéã®Miller-Rabinç´ æ°å¤å®æ³ãã¹ãããããã¦ãp,qãåææ°ã«ãªã確çãé«ãããã¨ãã§ãã¾ãããã®ä¿®æ£ãå ããOpenSSLã¨ssh-keygenãç¨ãã¦ãã¹ãã¬ã¼ãºãªãã®SSHéµãçæãã¦ã¿ã¾ããã
$ ./ssh-keygen -f /tmp/broken_id_rsa Generating public/private rsa key pair. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /tmp/broken_id_rsa. Your public key has been saved in /tmp/broken_id_rsa.pub. The key fingerprint is: 88:91:2d:63:fd:b9:5f:41:a5:74:39:4a:97:ee:e7:26 [email protected] The key's randomart image is: +--[ RSA 2048]----+ | . oo | | + ..+= | | * o .o+ . | | . = o . .. . | | . . S .. | | . .. .| | . . o | | . . E o| | . o | +-----------------+
çæããç§å¯éµã®ä¸ã®p,qã確èªãã¦ã¿ã¾ãããã
$ openssl asn1parse -in /tmp/broken_id_rsa 0:d=0 hl=4 l=1188 cons: SEQUENCE 4:d=1 hl=2 l= 1 prim: INTEGER :00 7:d=1 hl=4 l= 257 prim: INTEGER :BD7235019F70FD2FA112B89F7A9B049615169C03AE01C69F6D6A49451A65C010D916AEE8F6BED3EF47B507234A9572F545657C86F2E927F5C1DAD7956D1E897663FA35A4D582C721C58E586E8C0FCE8A6788CDBDB5915C0C7D8455F3D2DA5921C388A3C6C6180FE0358977D8D3AC8C0F41E0ED08BB72E1BADEE887E1CF5611B286BD8C199DD093FECEB0C5B8B02EDCD3077FE3267D66FACAB40C65A8C76A08F6D28B617C8BF0F226E49265C07728A8CA2BB17C86AD1813B898B6D2878B2FC37543CDFA23A8C15179234440D14216F3D43E9578A3500ADE27086FF733F31B56FC7DC0263176DCC0B2F2414CCE23D54EF41736AC3D8E052CF4B2972574E12639B3 268:d=1 hl=2 l= 3 prim: INTEGER :010001 273:d=1 hl=4 l= 257 prim: INTEGER :9A1369A2E13EEEDC2EDF60026C9FE931FB02C16E88B5EF09B8DE49AB07161C0857D707F876BDAAF69FD64E70D87705E10F48C3E7A9661156E20C0F6BFB2C6BD63AE7C37B451F30BF79C214900C1FCAF66BD02AAC912020C213CF6E6C785F97404B9C34BF345B5B861964AD714E6EB616AE98B58F758CB0A3E029346265D3755DDB9B0FEBE1DECD027D718734B24E8D3F0568A3AA7B599A76824DB7A5DB47D54E643A81FEC0B4F23FCF20C9DFA1259533692D3524E9B52DF2C37626A6EDDE477B432A76CEEDF82E63A8A6DB7D71B8335338650107F17A657E8675DE06C001C49A13A68E3BFE591481CE0EB3CB53430206A79CA9A5DA5C6ECB44D0DB8AE2FBC281 534:d=1 hl=3 l= 129 prim: INTEGER :E489D4B72A4EEE27550F6FA1ECFE04504EBC830E4DB689A807CD1CD1399F3018BA5A28EC54842BEFC01477AB822A31842EEAD02ABD2211E61C433DD927712F62BE3E392D3EEFB78BD1C0A6CFCE901EEC82E1B1718CA247B7909D3D6CBF9F7AC85E9F195245415FB6043C2EC0E756BBF6AD85D232215F7BDDBD03E41A500DD641 666:d=1 hl=3 l= 129 prim: INTEGER :D435D76CD11F1B67EE1C3FD1158B8E413ECB7CB470787218719A41B8A971B8B38D7377687F73684588F9BE87F1840395EF532F8B97B14ED3DB98FB02FC514A90E4907F92E5E93AD96F254319AF8E2087A34D695EA892B31FA39AC76F44ECEEA4898EFFC451959A05EC43B5AB7DF268CFF5B7DBA1C518A61184FADDE574715AF3 798:d=1 hl=3 l= 129 prim: INTEGER :AE5579FFB3757C74628DA0E18BD085E7E0F82A9D19A91A3F249C51D444B96B4E21B1AF300094C6936019FCE3C72A7A9553D8E9AD0093E1C5805FC6E9450E315088C11C8AA84CE2DDF4C69A3941606D468BDCB0A866D8500EF6710C2F4DC9D136D1FF59A8898E01FDEE231EA32695E2529D31CD1352A3ECF04C44909785E3D841 930:d=1 hl=3 l= 128 prim: INTEGER :2FC7BFAD7C98686F4A298A24E598FA7AAE4CDAD335CFA8C0E0333F40F8C5E6346750EC3DD7148111C6F99470BF6C5DF25064867C03B7A46C0731B6B2C164FC490B6D8D8BE1C055C3C746B888FC022048E9F7F015A41703C5C7EC7DA30BFDDCEDA71B4F73407B52A6AABFC413CCD3DBCD9721C28DF5F6CACD7F078D16B0D52509 1061:d=1 hl=3 l= 128 prim: INTEGER :5FD2EFBAF7CAC160391E92557D6102D3C26607B75BBF72D5F8A20CD30772FE90915DF1AF18389A227C1C48D17D76A1A9881EE74657838FCF83263D49C3927BA1E6C1D7AAD6532DF860C9912E6B5DFEF2F7826B0210BE3E120F39EE1EC4BE2FA4BF5396B191F51D982673B6CBDCEDB9668CC6F887D37F0574185B2571C3F397B3
ãã®ãã¡ã5çªç®ã¨6çªç®ãp,qã§ãï¼åç §ï¼RFC3447 Appendix A.1.2ï¼ãããã¯ä¸è¨ã®éãç´ æ°ã§ã¯ããã¾ããã
$ openssl prime -hex E489D4B72A4EEE27550F6FA1ECFE04504EBC830E4DB689A807CD1CD1399F3018BA5A28EC54842BEFC01477AB822A31842EEAD02ABD2211E61C433DD927712F62BE3E392D3EEFB78BD1C0A6CFCE901EEC82E1B1718CA247B7909D3D6CBF9F7AC85E9F195245415FB6043C2EC0E756BBF6AD85D232215F7BDDBD03E41A500DD641 E489D4B72A4EEE27550F6FA1ECFE04504EBC830E4DB689A807CD1CD1399F3018BA5A28EC54842BEFC01477AB822A31842EEAD02ABD2211E61C433DD927712F62BE3E392D3EEFB78BD1C0A6CFCE901EEC82E1B1718CA247B7909D3D6CBF9F7AC85E9F195245415FB6043C2EC0E756BBF6AD85D232215F7BDDBD03E41A500DD641 is not prime $ openssl prime -hex D435D76CD11F1B67EE1C3FD1158B8E413ECB7CB470787218719A41B8A971B8B38D7377687F73684588F9BE87F1840395EF532F8B97B14ED3DB98FB02FC514A90E4907F92E5E93AD96F254319AF8E2087A34D695EA892B31FA39AC76F44ECEEA4898EFFC451959A05EC43B5AB7DF268CFF5B7DBA1C518A61184FADDE574715AF3 D435D76CD11F1B67EE1C3FD1158B8E413ECB7CB470787218719A41B8A971B8B38D7377687F73684588F9BE87F1840395EF532F8B97B14ED3DB98FB02FC514A90E4907F92E5E93AD96F254319AF8E2087A34D695EA892B31FA39AC76F44ECEEA4898EFFC451959A05EC43B5AB7DF268CFF5B7DBA1C518A61184FADDE574715AF3 is not prime
ãã®RSAéµãã¢ã§SSHéä¿¡ã§ããã®ã§ããããï¼ä½ã£ãå ¬ééµãå¥ã®ãã·ã³ã«ç»é²ããä¸ã§ãã°ã¤ã³ãããã¨ããã¨ãä¸è¨ã®ããã«ãã°ã¤ã³ã«å¤±æãã¦ãã¾ãã¾ãã
$ ssh -v -i /tmp/broken_id_rsa 192.168.56.64 OpenSSH_6.6, OpenSSL 1.0.1h 5 Jun 2014 debug1: Reading configuration data /Users/hnw/.ssh/config debug1: /Users/hnw/.ssh/config line 86: Applying options for * debug1: Reading configuration data /usr/local/etc/ssh/ssh_config debug1: Connecting to 192.168.56.64 [192.168.56.64] port 22. debug1: Connection established. debug1: identity file /tmp/broken_id_rsa type 1 debug1: identity file /tmp/broken_id_rsa-cert type -1 debug1: Enabling compatibility mode for protocol 2.0 debug1: Local version string SSH-2.0-OpenSSH_6.6 debug1: Remote protocol version 2.0, remote software version OpenSSH_6.6.1p1 Ubuntu-2ubuntu2 debug1: match: OpenSSH_6.6.1p1 Ubuntu-2ubuntu2 pat OpenSSH* compat 0x04000000 debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug1: kex: server->client aes128-ctr [email protected] none debug1: kex: client->server aes128-ctr [email protected] none debug1: sending SSH2_MSG_KEX_ECDH_INIT debug1: expecting SSH2_MSG_KEX_ECDH_REPLY debug1: Server host key: RSA 78:b6:ï¼ä»¥ä¸ç¥ï¼ debug1: Host '192.168.56.64' is known and matches the RSA host key. debug1: Found key in /Users/hnw/.ssh/known_hosts:79 debug1: ssh_rsa_verify: signature correct debug1: SSH2_MSG_NEWKEYS sent debug1: expecting SSH2_MSG_NEWKEYS debug1: SSH2_MSG_NEWKEYS received debug1: Roaming not allowed by server debug1: SSH2_MSG_SERVICE_REQUEST sent debug1: SSH2_MSG_SERVICE_ACCEPT received debug1: Authentications that can continue: publickey debug1: Next authentication method: publickey debug1: Offering RSA public key: /Users/hnw/.ssh/id_rsa debug1: Authentications that can continue: publickey debug1: Offering RSA public key: /tmp/broken_id_rsa debug1: Server accepts key: pkalg ssh-rsa blen 279 debug1: key_parse_private2: missing begin marker debug1: read PEM private key done: type RSA debug1: Authentications that can continue: publickey debug1: No more authentication methods to try. Permission denied (publickey).
RSAéµã®p,qãåææ°ã ã¨ä½ãèµ·ããã
ä¸ã®å®é¨ã§ä½ãèµ·ããã®ã§ããããããããç¥ãããã«ãã¾ãã¯RSAæå·ã®ä»çµã¿ã復ç¿ãã¦ã¿ã¾ããããRSAéµçæã®ã¿ã¤ãã³ã°ã§ãç´ æ°p,qã®ä»ã«eã¨dã¨ãããã©ã¡ã¼ã¿ãè¨ç®ããã¾ãããã®e,dã¯ä¸è¨(1)å¼ãæãç«ã¤ããã«è¨ç®ãã¾ãã
- n=p*qï¼pâ q, p,qã¯ç´ æ°ï¼ã¨ãã
- ä»»æã®xã«å¯¾ã㦠x^(e*d)=x (mod n) ⦠(1)
ãã®e,d,nã使ãã¨ãRSAã®æå·åå¦çã»å¾©å·å¦çã¯æ¬¡ã®ããã«è¡¨ãã¾ãã
- æå·å
- å¹³æãiã¨ããããã ãi<n
- æå·æ c = i^e (mod n)
- 復å·
- æå·æ c ã«å¯¾ã㦠c^d (mod n)ã§å¹³æãå¾ããã
ãã®ããã«ãç§å¯éµã«å«ã¾ããdããã¬ãªããã°ãæå·åã«å¿ è¦ãªeã¯å ¬éãããã¨ãã§ããã¨ããä»çµã¿ã«ãªã£ã¦ãã¾ãã
ã¨ããã§ããªã¤ã©ã¼ã®å®çãå©ç¨ããã°ã次ã®(2)å¼ãæºããdã(1)å¼ãæºãããã¨ããããã¾ãã
- Ï(n) = (p-1)*(q-1)
- e*d = 1 (mod Ï(n)) ⦠(2)
ã¤ã¾ããeã®ã¢ã¸ã¥ã©éæ°ãåãã°dãè¨ç®ã§ããã¨ããããã§ãã
ãã ããä¸è¨ã®åæã¯pã¨qãç´ æ°ã§ãããã¨ã§ããp,qãããããåææ°ã®å ´åããã®ããã«æ±ããdãå¼(1)ãæºãããªããªãã¯ãã§ã*1ã
ä¸ã®å®é¨ã§ãp,qãåææ°ã ã£ãããã«e,dã®çµã(1)å¼ãæºãããã復å·å¦çã«å¤±æããã¨èãããã¾ãã
RSAéµçææã«p,qãããããåææ°ã«ãªã確ç
RSAéµçææã«p,qãããããåææ°ã ã¨RSAã®å¾©å·å¦çã«å¤±æãã¦ãã¾ããéµãã¢ã¨ãã¦æ©è½ããªããã¨ããããã¾ãããä¸æ¹ã§ãp,qãç´ æ°ãã©ããã¯Miller-Rabinç´ æ°å¤å®æ³ã¨ãã確ççã¢ã«ã´ãªãºã ã§è¡ããã¾ããããä¸å®ã®ç¢ºçã§å£ããéµãã¢ãçæãã¦ãã¾ããã¨ã«ãªãã¾ãã
ã§ã¯ãp,qã®ç´ æ°å¤å®ã«å¤±æãã¦å£ããéµãã¢ãçæãã確çã¯ã©ãããããªã®ã§ããããããã®çããOpenSSLã®crypto/bn/bn.hã«æ¸ãã¦ããã¾ãã
/* number of Miller-Rabin iterations for an error rate of less than 2^-80 * for random 'b'-bit input, b >= 100 (taken from table 4.4 in the Handbook * of Applied Cryptography [Menezes, van Oorschot, Vanstone; CRC Press 1996]; * original paper: Damgaard, Landrock, Pomerance: Average case error estimates * for the strong probable prime test. -- Math. Comp. 61 (1993) 177-194) */ #define BN_prime_checks_for_size(b) ((b) >= 1300 ? 2 : \ (b) >= 850 ? 3 : \ (b) >= 650 ? 4 : \ (b) >= 550 ? 5 : \ (b) >= 450 ? 6 : \ (b) >= 400 ? 7 : \ (b) >= 350 ? 8 : \ (b) >= 300 ? 9 : \ (b) >= 250 ? 12 : \ (b) >= 200 ? 15 : \ (b) >= 150 ? 18 : \ /* b >= 100 */ 27)
ãã®ãã¯ãã¯ãç´ æ°å¤å®ããæ°ã®ãããæ°ããMiller-Rabinã®ã¤ãã¬ã¼ã·ã§ã³æ°ãè¿ããã¯ãã§ããããã¯ä¸è¨ã³ã¡ã³ãé¨ã«ãã1993å¹´ã®è«æãå ãã¿ã§ãç´ æ°ã誤å¤å®ãã確çã1/2^80以ä¸ã«ãªãããã«ã¤ãã¬ã¼ã·ã§ã³æ°ã決å®ãã¦ãã¾ãã
ã¤ã¾ããå ã»ã©ã®p,qã®ã©ã¡ããä¸æ¹ã§ãç´ æ°å¤å®ãééã確çã¯1/2^79以ä¸ã«ãªãã¾ããããã¯æ¯ç§1000åã®éµãã¢ãçæã§ãããã·ã³100ä¸å°ã§æåããã¦éµçæãç¶ããã¨ãã¦ã2000ä¸å¹´ã®éã«å£ããéµãã¢ã1ã¤ã§ãããã©ããã§ããããä»ã®ãã°ãè¸ã確çã®æ¹ããã»ã©å¤§ããããªæ°ããã¾ãã
決å®çãªç´ æ°å¤å®ã¢ã«ã´ãªãºã ã使ãããªãçç±
ããã確çãä½ãã¦ãã確å®æ§ãå¿ è¦ãªã®ã«ç¢ºççã¢ã«ã´ãªãºã ã使ãã®ã¯æ°æã¡ãæªãã¨æãã人ãããã§ãããã決å®çãªç´ æ°å¤å®ã¢ã«ã´ãªãºã ã使ãããã«ã¯ãããªãã®ã§ããããï¼
決å®çã¢ã«ã´ãªãºã ã使ããã¦ããªãçç±ã¯æ§è½ã®åé¡ã§ããããIPAã«ããè³æã素数生成アルゴリズムの調査・開発 調査報告書ã(PDF)ã®5.2ç« ã«ããã°ã1024bitç´ æ°ã®å¤å®ã«æ±ºå®çã¢ã«ã´ãªãºã ã使ãã¨Miller-Rabinã®1000åç¨åº¦é ãããã§ãã
å¾ãããçµæã®ç²¾åº¦ã¨é度ã®ãã©ã³ã¹ãèããã¨Miller-Rabinã®æ¡ç¨ã妥å½ã¨ãããã¨ãªã®ã§ãããã
ã¾ã¨ã
- OpenSSLã§ã¯RSAéµçæã®éä¸ã§ç¢ºççç´ æ°å¤å®ã¢ã«ã´ãªãºã ã使ã£ã¦ãããããå£ããéµãã¢ãä½ãããå¯è½æ§ããã
ãã®è¨äºãæ¸ãåã¯ãRSAéµçæã®ã¿ã¤ãã³ã°ã§x^(e*d)=x (mod n)ã®æ¤ç®ããã¦ãããããå£ããéµãåºæ¥ãããã¨ã©ã¼ãåºãã¦ãããã¨äºæ³ãã¦ãã¾ãããããããªå¦çã¯ç¡ãã¿ããã§ãã
*1:p,qãåææ°ã«ããããããæ±ããdãå¼(1)ãæºãããã¨ãããããã§ãããã©ãããæ¡ä»¶ã§ãããªããçè ã«ã¯ãããã¾ãããçè«çèæ¯ãªã©ãåãã®æ¹ã¯æãã¦ãã ãããå°ãªãã¨ãä»åå®é¨ããç¯å²ã§ã¯å¿ ãRSAéä¿¡ã«å¤±æãã¦ãã¾ãã