예제 #1
0
파일: ABE.py 프로젝트: dkhungme/FastKAC
def decryption_time(storage, q_list):
	iterations = 1
	timings = []
	aggregate_time = []
	aggregate_size = []
	no_of_bits = len(list(bin(storage['n'])[2:]))+1  
	kpabe = KPabe(group)
	for q in q_list:
		check = True
		timing = 0.0
		gen_timing = 0.0
		for i in xrange(iterations):
			more_than_equal, less_than = generate_range(storage, q)	 
			start = time.clock()
			policy = policy_less_than('A', less_than, no_of_bits) + " and " + policy_more_than_equal('A', more_than_equal, no_of_bits)
			secret_key = kpabe.keygen(storage['master_public_key'], storage['master_key'], policy)
			end = time.clock()
			gen_timing += end-start

			cipher_text = storage['cipher'][more_than_equal:less_than]
			plain_text = storage['plain'][more_than_equal:less_than]
			start = time.clock()
			result = [kpabe.decrypt(ct, secret_key) for ct in cipher_text]
			end = time.clock()
			timing+=end-start
			if (plain_text!=result):
				check = False
		aggregate_time.append((q, gen_timing/iterations))
		aggregate_size.append((q, getsizeof(pickle.dumps(objectToBytes(secret_key, group)))))
		if (check==True):
			timings.append((q, timing/iterations))
		else:
			timings.append((q, 'error'))
	return timings
예제 #2
0
def kpabe_decrypt(group, mpk, deckey, ctxt):
    """Decrypts a ciphertext using the Lewmko2008rws KP-ABE Scheme.

    The plaintext will be returned iff the set of attributes used to
    generate the cipher-text can be satisfied by the policy within the
    decryption key.

    @param group  The `PairingGroup` used within the underlying crypto.
    @param mpk    The Master Public Key of type `mk_t`.
    @param deckey The decryption key of type `sk_t`.
    @param ctxt   The `bytearray` resulting from `io.open` or `io.IOBytes`
                 containing the ciphertext.

    @return A `bytearray` containing the plaintext.

    @throw PebelDecryptionException if deckey cannot satisfy the
            policy within the ciphertext.
    """
    kpabe = KPabe(group)

    ptxt = io.BytesIO()

    iv = ctxt.read(AES.block_size)
    session_key_size = struct.unpack('<Q', ctxt.read(struct.calcsize('Q')))[0]
    session_key_ctxt = bytesToObject(ctxt.read(session_key_size), group)
    session_key = kpabe.decrypt(session_key_ctxt, deckey)

    if session_key:
        symcipher = AES.new(sha(session_key)[0:32], AES.MODE_CFB, iv)
        for b in read_data(bin_data=ctxt, chunksize=AES.block_size):
            ptxt.write(symcipher.decrypt(b))
            ptxt.flush()
        return ptxt.getvalue()
    else:
        raise PebelDecryptionException("Unable to decrypt given ciphertext")
예제 #3
0
def decryption_time(storage, q_list):
	iterations = 5
	timings = []
	aggregate_time = []
	aggregate_size = []
	no_of_bits = len(list(bin(storage['n'])[2:]))+1  
	kpabe = KPabe(group)
	for q in q_list:
		print "q"
		print q

		check = True
		timing = 0.0
		gen_timing = 0.0
		for i in xrange(iterations):
			# print "iterations"
			# print i

			more_than_equal, less_than = generate_range(storage, q)	 
			print "more_than_equal"
			print more_than_equal
			print "less_than"
			print less_than

			start = time.clock()
			policy = policy_less_than('A', less_than, no_of_bits) + " and " + policy_more_than_equal('A', more_than_equal, no_of_bits)
			print "policy"
			print policy

			secret_key = kpabe.keygen(storage['master_public_key'], storage['master_key'], policy)
			end = time.clock()
			gen_timing += end-start

			cipher_text = storage['cipher'][more_than_equal:less_than]
			plain_text = storage['plain'][more_than_equal:less_than]
			start = time.clock()

			assert group.InitBenchmark(), "failed to initialize benchmark"
			group.StartBenchmark(["Mul", "Exp", "Pair", "Div", "Granular"])

			result = [kpabe.decrypt(ct, secret_key) for ct in cipher_text]
			
			group.EndBenchmark()

			end = time.clock()
			msmtDict = group.GetGeneralBenchmarks()
			print("<=== General Benchmarks ===>")
			print("Results  := ", msmtDict)

			timing+=end-start
			if (plain_text!=result):
				check = False
		aggregate_time.append((q, gen_timing/iterations))
		aggregate_size.append((q, getsizeof(pickle.dumps(objectToBytes(secret_key, group)))))
		if (check==True):
			timings.append((q, timing/iterations))
		else:
			timings.append((q, 'error'))
	return timings
예제 #4
0
def benchmark():
    groupObj1 = PairingGroup('MNT224')
    groupObj2 = PairingGroup('MNT224')
    ekpabe = EKPabe(groupObj1)
    kpabe = KPabe(groupObj2)

    t1_s = 0
    t1_k = 0
    t1_e = 0
    t1_d = 0
    t2_s = 0
    t2_k = 0
    t2_e = 0
    t2_d = 0

    attributes = ['ONE', 'TWO', 'THREE', 'FOUR']
    policy = 'THREE and (ONE or TWO)'
    msg1 = b"Some Random Message"
    msg2 = groupObj2.random(GT)

    for b in range(4):
        start = clock()
        (epk, emk) = ekpabe.setup(attributes)
        t1_s += clock() - start

        start = clock()
        (pk, mk) = kpabe.setup()
        t2_s += clock() - start

        start = clock()
        emykey = ekpabe.keygen(epk, emk, policy)
        t1_k += clock() - start

        start = clock()
        mykey = kpabe.keygen(pk, mk, policy)
        t2_k += clock() - start

        for i in range(50):
            start = clock()
            eciphertext = ekpabe.encrypt(epk, msg1, attributes)
            t1_e += clock() - start

            start = clock()
            ciphertext = kpabe.encrypt(pk, msg2, attributes)
            t2_e += clock() - start

            start = clock()
            erec_msg = ekpabe.decrypt(eciphertext, emykey)
            t1_d += clock() - start

            start = clock()
            rec_msg = kpabe.decrypt(ciphertext, mykey)
            t2_d += clock() - start

            assert msg1 == erec_msg
            assert msg2 == rec_msg

    print("yct14 s=%s k=%s e=%s d=%s" % (t1_s, t1_k, t1_e, t1_d))
    print("lsw08 s=%s k=%s e=%s d=%s" % (t2_s, t2_k, t2_e, t2_d))
예제 #5
0
파일: abenc_test.py 프로젝트: FinalF/charm
 def testKPabe(self):    
     groupObj = PairingGroup('MNT224')
     kpabe = KPabe(groupObj)
     
     (pk, mk) = kpabe.setup()
     
     policy = '(ONE or THREE) and (THREE or TWO)'
     attributes = [ 'ONE', 'TWO', 'THREE', 'FOUR' ]
     msg = groupObj.random(GT) 
  
     mykey = kpabe.keygen(pk, mk, policy)
     
     if debug: print("Encrypt under these attributes: ", attributes)
     ciphertext = kpabe.encrypt(pk, msg, attributes)
     if debug: print(ciphertext)
     
     rec_msg = kpabe.decrypt(ciphertext, mykey)
    
     assert msg == rec_msg 
     if debug: print("Successful Decryption!")
예제 #6
0
    def testKPabe(self):
        groupObj = PairingGroup('MNT224')
        kpabe = KPabe(groupObj)

        (pk, mk) = kpabe.setup()

        policy = '(ONE or THREE) and (THREE or TWO)'
        attributes = ['ONE', 'TWO', 'THREE', 'FOUR']
        msg = groupObj.random(GT)

        mykey = kpabe.keygen(pk, mk, policy)

        if debug: print("Encrypt under these attributes: ", attributes)
        ciphertext = kpabe.encrypt(pk, msg, attributes)
        if debug: print(ciphertext)

        rec_msg = kpabe.decrypt(ciphertext, mykey)

        assert msg == rec_msg
        if debug: print("Successful Decryption!")
예제 #7
0
def decryption_time(storage, q_list):
    iterations = 1
    timings = []
    aggregate_time = []
    aggregate_size = []
    no_of_bits = len(list(bin(storage['n'])[2:])) + 1
    kpabe = KPabe(group)
    for q in q_list:
        check = True
        timing = 0.0
        gen_timing = 0.0
        for i in xrange(iterations):
            more_than_equal, less_than = generate_range(storage, q)
            start = time.clock()
            policy = policy_less_than(
                'A', less_than, no_of_bits) + " and " + policy_more_than_equal(
                    'A', more_than_equal, no_of_bits)
            secret_key = kpabe.keygen(storage['master_public_key'],
                                      storage['master_key'], policy)
            end = time.clock()
            gen_timing += end - start

            cipher_text = storage['cipher'][more_than_equal:less_than]
            plain_text = storage['plain'][more_than_equal:less_than]
            start = time.clock()
            result = [kpabe.decrypt(ct, secret_key) for ct in cipher_text]
            end = time.clock()
            timing += end - start
            if (plain_text != result):
                check = False
        aggregate_time.append((q, gen_timing / iterations))
        aggregate_size.append(
            (q, getsizeof(pickle.dumps(objectToBytes(secret_key, group)))))
        if (check == True):
            timings.append((q, timing / iterations))
        else:
            timings.append((q, 'error'))
    return timings
예제 #8
0
파일: kpabe.py 프로젝트: dusty141/pyPEBEL
def kpabe_decrypt(group, mpk, deckey, ctxt):
    """Decrypts a ciphertext using the Lewmko2008rws KP-ABE Scheme.

    The plaintext will be returned iff the set of attributes used to
    generate the cipher-text can be satisfied by the policy within the
    decryption key.

    @param group  The `PairingGroup` used within the underlying crypto.
    @param mpk    The Master Public Key of type `mk_t`.
    @param deckey The decryption key of type `sk_t`.
    @param ctxt   The `bytearray` resulting from `io.open` or `io.IOBytes`
                 containing the ciphertext.

    @return A `bytearray` containing the plaintext.

    @throw PebelDecryptionException if deckey cannot satisfy the
            policy within the ciphertext.
    """
    kpabe = KPabe(group)

    ptxt = io.BytesIO()

    iv = ctxt.read(AES.block_size)
    session_key_size = struct.unpack('<Q',
                                     ctxt.read(struct.calcsize('Q')))[0]
    session_key_ctxt = bytesToObject(ctxt.read(session_key_size), group)
    session_key = kpabe.decrypt(session_key_ctxt, deckey)

    if session_key:
        symcipher = AES.new(sha(session_key)[0:32], AES.MODE_CFB, iv)
        for b in read_data(bin_data=ctxt, chunksize=AES.block_size):
            ptxt.write(symcipher.decrypt(b))
            ptxt.flush()
        return ptxt.getvalue()
    else:
        raise PebelDecryptionException("Unable to decrypt given ciphertext")