Example #1
0
 def testRSAEnc(self):
     rsa = RSA_Enc()
     (pk, sk) = rsa.keygen(1024)
     
     #m = integer(34567890981234556498) % pk['N']
     m = b'This is a test'
     c = rsa.encrypt(pk, m)
     
     orig_m = rsa.decrypt(pk, sk, c)
 
     assert m == orig_m, 'o: =>%s\nm: =>%s' % (orig_m, m)
Example #2
0
    def testRSAVector(self):
        # ==================================
        # Example 1: A 1024-bit RSA Key Pair
        # ==================================
        
        # ------------------------------
        # Components of the RSA Key Pair
        # ------------------------------
        
        # RSA modulus n:
        n = a2b_hex(bytes('\
        bb f8 2f 09 06 82 ce 9c 23 38 ac 2b 9d a8 71 f7 \
        36 8d 07 ee d4 10 43 a4 40 d6 b6 f0 74 54 f5 1f \
        b8 df ba af 03 5c 02 ab 61 ea 48 ce eb 6f cd 48 \
        76 ed 52 0d 60 e1 ec 46 19 71 9d 8a 5b 8b 80 7f \
        af b8 e0 a3 df c7 37 72 3e e6 b4 b7 d9 3a 25 84 \
        ee 6a 64 9d 06 09 53 74 88 34 b2 45 45 98 39 4e \
        e0 aa b1 2d 7b 61 a5 1f 52 7a 9a 41 f6 c1 68 7f \
        e2 53 72 98 ca 2a 8f 59 46 f8 e5 fd 09 1d bd cb '.replace(' ',''),'utf-8'))
        n = Conversion.OS2IP(n, True)
        
        # RSA public exponent e:
        e = a2b_hex(b'11')
        e = Conversion.OS2IP(e, True)
        
        # Prime p: 
        p = a2b_hex(bytes('\
        ee cf ae 81 b1 b9 b3 c9 08 81 0b 10 a1 b5 60 01 \
        99 eb 9f 44 ae f4 fd a4 93 b8 1a 9e 3d 84 f6 32 \
        12 4e f0 23 6e 5d 1e 3b 7e 28 fa e7 aa 04 0a 2d \
        5b 25 21 76 45 9d 1f 39 75 41 ba 2a 58 fb 65 99 '.replace(' ',''),'utf-8'))
        p = Conversion.OS2IP(p, True)
        
        # Prime q: 
        q = a2b_hex(bytes('\
        c9 7f b1 f0 27 f4 53 f6 34 12 33 ea aa d1 d9 35 \
        3f 6c 42 d0 88 66 b1 d0 5a 0f 20 35 02 8b 9d 86 \
        98 40 b4 16 66 b4 2e 92 ea 0d a3 b4 32 04 b5 cf \
        ce 33 52 52 4d 04 16 a5 a4 41 e7 00 af 46 15 03'.replace(' ',''),'utf-8'))
        q = Conversion.OS2IP(q, True)
        
        phi_N = (p - 1) * (q - 1)
        e = e % phi_N

        d = e ** -1
        
        # ----------------------------------
        # Step-by-step RSAES-OAEP Encryption
        # ----------------------------------
        
        # Message to be encrypted:
        M = a2b_hex(bytes('\
        d4 36 e9 95 69 fd 32 a7 c8 a0 5b bc 90 d3 2c 49'.replace(' ',''),'utf-8'))
        
        lhash = a2b_hex(bytes('\
        da 39 a3 ee 5e 6b 4b 0d 32 55 bf ef 95 60 18 90 \
        af d8 07 09'.replace(' ', ''),'utf-8'))
        
        # DB:
        db = a2b_hex(bytes('\
        da 39 a3 ee 5e 6b 4b 0d 32 55 bf ef 95 60 18 90 \
        af d8 07 09 00 00 00 00 00 00 00 00 00 00 00 00 \
        00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 \
        00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 \
        00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 \
        00 00 00 00 00 00 00 00 00 00 01 d4 36 e9 95 69 \
        fd 32 a7 c8 a0 5b bc 90 d3 2c 49'.replace(' ', ''),'utf-8')) 
        
        # Seed:
        seed = a2b_hex(bytes('\
        aa fd 12 f6 59 ca e6 34 89 b4 79 e5 07 6d de c2 \
        f0 6c b5 8f '.replace(' ',''),'utf-8'))
        
        # dbMask:
        dbmask = a2b_hex(bytes('\
        06 e1 de b2 36 9a a5 a5 c7 07 d8 2c 8e 4e 93 24 \
        8a c7 83 de e0 b2 c0 46 26 f5 af f9 3e dc fb 25 \
        c9 c2 b3 ff 8a e1 0e 83 9a 2d db 4c dc fe 4f f4 \
        77 28 b4 a1 b7 c1 36 2b aa d2 9a b4 8d 28 69 d5 \
        02 41 21 43 58 11 59 1b e3 92 f9 82 fb 3e 87 d0 \
        95 ae b4 04 48 db 97 2f 3a c1 4e af f4 9c 8c 3b \
        7c fc 95 1a 51 ec d1 dd e6 12 64'.replace(' ',''),'utf-8')) 
        
        # maskedDB:
        maskeddb = a2b_hex(bytes('\
        dc d8 7d 5c 68 f1 ee a8 f5 52 67 c3 1b 2e 8b b4 \
        25 1f 84 d7 e0 b2 c0 46 26 f5 af f9 3e dc fb 25 \
        c9 c2 b3 ff 8a e1 0e 83 9a 2d db 4c dc fe 4f f4 \
        77 28 b4 a1 b7 c1 36 2b aa d2 9a b4 8d 28 69 d5 \
        02 41 21 43 58 11 59 1b e3 92 f9 82 fb 3e 87 d0 \
        95 ae b4 04 48 db 97 2f 3a c1 4f 7b c2 75 19 52 \
        81 ce 32 d2 f1 b7 6d 4d 35 3e 2d '.replace(' ',''),'utf-8'))
        
        # seedMask:
        seedmask = a2b_hex(bytes('\
        41 87 0b 5a b0 29 e6 57 d9 57 50 b5 4c 28 3c 08 \
        72 5d be a9 '.replace(' ',''),'utf-8'))
        
        # maskedSeed:
        maskedseed = a2b_hex(bytes('\
        eb 7a 19 ac e9 e3 00 63 50 e3 29 50 4b 45 e2 ca \
        82 31 0b 26 '.replace(' ',''),'utf-8'))
        
        # EM = 00 || maskedSeed || maskedDB:
        em = a2b_hex(bytes('\
        00 eb 7a 19 ac e9 e3 00 63 50 e3 29 50 4b 45 e2 \
        ca 82 31 0b 26 dc d8 7d 5c 68 f1 ee a8 f5 52 67 \
        c3 1b 2e 8b b4 25 1f 84 d7 e0 b2 c0 46 26 f5 af \
        f9 3e dc fb 25 c9 c2 b3 ff 8a e1 0e 83 9a 2d db \
        4c dc fe 4f f4 77 28 b4 a1 b7 c1 36 2b aa d2 9a \
        b4 8d 28 69 d5 02 41 21 43 58 11 59 1b e3 92 f9 \
        82 fb 3e 87 d0 95 ae b4 04 48 db 97 2f 3a c1 4f \
        7b c2 75 19 52 81 ce 32 d2 f1 b7 6d 4d 35 3e 2d '.replace(' ',''),'utf-8'))
            
        # Encryption:
        enc = a2b_hex(bytes('\
        12 53 e0 4d c0 a5 39 7b b4 4a 7a b8 7e 9b f2 a0 \
        39 a3 3d 1e 99 6f c8 2a 94 cc d3 00 74 c9 5d f7 \
        63 72 20 17 06 9e 52 68 da 5d 1c 0b 4f 87 2c f6 \
        53 c1 1d f8 23 14 a6 79 68 df ea e2 8d ef 04 bb \
        6d 84 b1 c3 1d 65 4a 19 70 e5 78 3b d6 eb 96 a0 \
        24 c2 ca 2f 4a 90 fe 9f 2e f5 c9 c1 40 e5 bb 48 \
        da 95 36 ad 87 00 c8 4f c9 13 0a de a7 4e 55 8d \
        51 a7 4d df 85 d8 b5 0d e9 68 38 d6 06 3e 09 55 '.replace(' ',''),'utf-8'))
        
        rsa = RSA_Enc()
        pk = { 'N':n, 'e':e }
        sk = { 'phi_N':phi_N, 'd':d , 'N': n}
        
        c = rsa.encrypt(pk, M, seed)
        C = Conversion.IP2OS(c)
        
        if debug: 
            print("RSA OEAP step by step")
            print("Label L  = empty string")
            print("lHash      = ", lhash)
            print("DB         = ", db)
            print("seed       = ", seed)
            print("dbMask     = ", dbmask)
            print("maskedDB   = ", maskeddb)
            print("seedMask   = ", seedmask)
            print("maskedSeed = ", maskedseed)
            print("EM         = ", em)
        
        assert C == enc