def generate_ciphertext_keys(n): storage = {} storage['n'] = n kpabe = KPabe(group) more_than = 1 storage['plain'] = [group.random(GT)] storage['cipher'] = [group.random(GT)] no_of_bits = len(list(bin(n)[2:]))+1 print "no_of_bits" print no_of_bits storage['master_public_key'], storage['master_key'] = kpabe.setup() frames_attribute = (num_to_attribute('A', i, no_of_bits) for i in xrange(1, n+1)) print "frames_attribute" print frames_attribute for i in xrange(n): plain = group.random(GT) storage['plain'].append(plain) storage['cipher'].append(kpabe.encrypt(storage['master_public_key'], plain, frames_attribute.next())) # ct = storage['cipher'][1:n+1] # result = [kpabe.decrypt(cipher_text, secret_key) for cipher_text in ct] # print 'lol', result # print storage['plain'][1:n+1] return storage
def generate_ciphertext_keys(n): storage = {} storage['n'] = n kpabe = KPabe(group) more_than = 1 storage['plain'] = [group.random(GT)] storage['cipher'] = [group.random(GT)] no_of_bits = len(list(bin(n)[2:])) + 1 storage['master_public_key'], storage['master_key'] = kpabe.setup() frames_attribute = (num_to_attribute('A', i, no_of_bits) for i in xrange(1, n + 1)) for i in xrange(n): plain = group.random(GT) storage['plain'].append(plain) storage['cipher'].append( kpabe.encrypt(storage['master_public_key'], plain, frames_attribute.next())) # ct = storage['cipher'][1:n+1] # result = [kpabe.decrypt(cipher_text, secret_key) for cipher_text in ct] # print 'lol', result # print storage['plain'][1:n+1] return storage
def kpabe_encrypt(group, mpk, ptxt, attributes): """Encrypts a plaintext using the Lewmko2008rws KP-ABE Scheme. @param group The `PairingGroup` used within the underlying crypto. @param mpk The master public key of type `mk_t`. @param ptxt The `bytearray` resulting from io.open or `io.IOBytes` containing the plaintext. @param attributes The set of `str` attributes used to encrypt the plaintext. @return The encrypted data returned as a `bytearray`. """ kpabe = KPabe(group) session_key = group.random(GT) session_key_ctxt = kpabe.encrypt(mpk, session_key, [a.upper() for a in attributes]) ctxt = io.BytesIO() iv = Random.new().read(AES.block_size) symcipher = AES.new(sha(session_key)[0:32], AES.MODE_CFB, iv) ctxt.write(bytes(iv)) session_key_ctxt_b = objectToBytes(session_key_ctxt, group) ctxt.write(struct.pack('Q', len(session_key_ctxt_b))) ctxt.write(session_key_ctxt_b) for b in read_data(bin_data=ptxt, chunksize=AES.block_size): ctxt.write(symcipher.encrypt(b)) ctxt.flush() return ctxt.getvalue()
def kpabe_encrypt(group, mpk, ptxt, attributes): """Encrypts a plaintext using the Lewmko2008rws KP-ABE Scheme. @param group The `PairingGroup` used within the underlying crypto. @param mpk The master public key of type `mk_t`. @param ptxt The `bytearray` resulting from io.open or `io.IOBytes` containing the plaintext. @param attributes The set of `str` attributes used to encrypt the plaintext. @return The encrypted data returned as a `bytearray`. """ kpabe = KPabe(group) session_key = group.random(GT) session_key_ctxt = kpabe.encrypt(mpk, session_key, [a.upper() for a in attributes]) ctxt = io.BytesIO() iv = Random.new().read(AES.block_size) symcipher = AES.new(sha(session_key)[0:32], AES.MODE_CFB, iv) ctxt.write(bytes(iv)) session_key_ctxt_b = objectToBytes(session_key_ctxt, group) ctxt.write(struct.pack('Q' ,len(session_key_ctxt_b))) ctxt.write(session_key_ctxt_b) for b in read_data(bin_data=ptxt, chunksize=AES.block_size): ctxt.write(symcipher.encrypt(b)) ctxt.flush() return ctxt.getvalue()
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))
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!")
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!")