def decrypt(ik, sk, ct, pk=None): group = PairingGroup('SS512') if pk is None: pk = getPK(stsConfig['TS_ip'],stsConfig['api_getPK']) if type(pk['g']) == type(''): pk = dictToObject(pk_dict=pk, group=group) if type(ct['C0']) == type(''): ct = dictToObject(ct_dict=ct) if type(ik['K0']) == type(''): ik = dictToObject(ik_dict=ik) if type(sk) == type(''): sk = dictToObject(pairingElement_dict=sk) group = PairingGroup('SS512') util = SecretUtil(group, False) w = pk['w'] u = pk['u'] S = ik['S'] #['THREE', 'ONE', 'TWO'] K0 = ik['K0'] K1 = ik['K1'] Kj2 = ik['Ki2'] Kj3 = ik['Ki3'] C0 = ct['C0'] Ci1 = ct['C_j_1'] Ci2 = ct['C_j_2'] Ci3 = ct['C_j_3'] Ci4 = ct['C_j_4'] Ci5 = ct['C_j_5'] policy = util.createPolicy(ct['policy']) #((ONE or THREE) and (TWO or FOUR)) print('access policy of this file :', policy) try: pruned = util.prune(policy, S) #[ONE, TWO] except Exception,e: raise Exception('your attributes do not satisfy the policy!')
def predecrypt(pk, ik, ct): group = PairingGroup('SS512') util = SecretUtil(group, debug) w = pk['w'] u = pk['u'] S = ik['S'] #['THREE', 'ONE', 'TWO'] K0 = ik['K0'] K1 = ik['K1'] Kj2 = ik['Ki2'] Kj3 = ik['Ki3'] C0 = ct['C0'] Ci1 = ct['C_j_1'] Ci2 = ct['C_j_2'] Ci3 = ct['C_j_3'] Ci4 = ct['C_j_4'] Ci5 = ct['C_j_5'] policy = util.createPolicy(ct['policy']) #((ONE or THREE) and (TWO or FOUR)) print('policy in predecrypt===', policy) print('S in predecrypt===', S) pruned = util.prune(policy, S) #[ONE, TWO] if pruned is False: raise Exception('Prune error in pre-decrypt') wi = util.getCoefficients(policy) #{u'TWO': <pairing.Element>, u'FOUR': <pairing.Element>, u'THREE': <pairing.Element>, u'ONE': <pairing.Element>} eC0K0 = pair(C0,K0) ECi4wi = 0 for each in pruned: i = each.getAttribute() ECi4wi += Ci4[i] * wi[i] ewECi4wiK1 = pair(w ** ECi4wi,K1) PIeCi1K1eCi2uCi5Kj2eCi3Kj3wi = 1 for each in pruned: j = each.getAttributeAndIndex() i = each.getAttribute() eCi1K1 = pair(Ci1[i],K1) eCi2uCi5Kj2 = pair(Ci2[i] * (u ** -Ci5[i]), Kj2[j]) eCi3Kj3 = pair(Ci3[i],Kj3[j]) PIeCi1K1eCi2uCi5Kj2eCi3Kj3wi *= (eCi1K1 * eCi2uCi5Kj2 * eCi3Kj3) ** wi[i] return objectToBytes(eC0K0/(ewECi4wiK1 * PIeCi1K1eCi2uCi5Kj2eCi3Kj3wi), group)
class MAABE(object): def __init__(self, groupObj): self.util = SecretUtil(groupObj, verbose=False) #Create Secret Sharing Scheme self.group = groupObj #:Prime order group def setup(self): '''Global Setup (executed by CA)''' #:In global setup, a bilinear group G of prime order p is chosen #:The global public parameters, GP and p, and a generator g of G. A random oracle H maps global identities GID to elements of G #:group contains #:the prime order p is contained somewhere within the group object g = self.group.random(G1) #: The oracle that maps global identities GID onto elements of G #:H = lambda str: g** group.hash(str) H = lambda x: self.group.hash(x, G1) a = self.group.random() b = self.group.random() g_a = g**a g_b = g**b GPP = {'g': g, 'g_a': g_a, 'g_b': g_b, 'H': H} GMK = {'a': a, 'b': b} return (GPP, GMK) def registerUser(self, GPP): '''Generate user keys (executed by the user).''' g = GPP['g'] ugsk1 = self.group.random() ugsk2 = self.group.random() ugpk1 = g**ugsk1 ugpk2 = g**ugsk2 return ((ugpk1, ugsk2), { 'pk': ugpk2, 'sk': ugsk1 }) # (private, public) def setupAuthority(self, GPP, authorityid, attributes, authorities): '''Generate attribute authority keys (executed by attribute authority)''' if authorityid not in authorities: alpha = self.group.random() beta = self.group.random() gamma = self.group.random() SK = {'alpha': alpha, 'beta': beta, 'gamma': gamma} PK = { 'e_alpha': pair(GPP['g'], GPP['g'])**alpha, 'g_beta': GPP['g']**beta, 'g_beta_inv': GPP['g']**~beta } authAttrs = {} authorities[authorityid] = (SK, PK, authAttrs) else: SK, PK, authAttrs = authorities[authorityid] for attrib in attributes: if attrib in authAttrs: continue versionKey = self.group.random() # random or really 'choose' ? h = GPP['H'](attrib) pk = h**versionKey authAttrs[attrib] = { 'VK': versionKey, #secret 'PK1': pk, #public 'PK2': pk**SK['gamma'] #public } return (SK, PK, authAttrs) def keygen(self, GPP, authority, attribute, userObj, USK=None): '''Generate user keys for a specific attribute (executed on attribute authority)''' if 't' not in userObj: userObj['t'] = self.group.random() #private to AA t = userObj['t'] ASK, APK, authAttrs = authority u = userObj if USK is None: USK = {} if 'K' not in USK or 'KS' not in USK or 'AK' not in USK: USK['K'] = \ (GPP['g'] ** ASK['alpha']) * \ (GPP['g_a'] ** u['sk']) * \ (GPP['g_b'] ** t) USK['KS'] = GPP['g']**t USK['AK'] = {} AK = (u['pk'] ** (t * ASK['beta'])) * \ ((authAttrs[attribute]['PK1'] ** ASK['beta']) ** (u['sk'] + ASK['gamma'])) USK['AK'][attribute] = AK return USK def encrypt(self, GPP, policy_str, k, authority): '''Generate the cipher-text from the content(-key) and a policy (executed by the content owner)''' #GPP are global parameters #k is the content key (group element based on AES key) #policy_str is the policy string #authority is the authority tuple _, APK, authAttrs = authority policy = self.util.createPolicy(policy_str) secret = self.group.random() shares = self.util.calculateSharesList(secret, policy) shares = dict([(x[0].getAttributeAndIndex(), x[1]) for x in shares]) C1 = k * (APK['e_alpha']**secret) C2 = GPP['g']**secret C3 = GPP['g_b']**secret C = {} CS = {} D = {} DS = {} for attr, s_share in shares.items(): k_attr = self.util.strip_index(attr) r_i = self.group.random() attrPK = authAttrs[attr] C[attr] = (GPP['g_a']**s_share) * ~(attrPK['PK1']**r_i) CS[attr] = GPP['g']**r_i D[attr] = APK['g_beta_inv']**r_i DS[attr] = attrPK['PK2']**r_i return { 'C1': C1, 'C2': C2, 'C3': C3, 'C': C, 'CS': CS, 'D': D, 'DS': DS, 'policy': policy_str } def decrypt(self, GPP, CT, user): '''Decrypts the content(-key) from the cipher-text (executed by user/content consumer)''' UASK = user['authoritySecretKeys'] USK = user['keys'] usr_attribs = list(UASK['AK'].keys()) policy = self.util.createPolicy(CT['policy']) pruned = self.util.prune(policy, usr_attribs) if pruned == False: return False coeffs = self.util.getCoefficients(policy) first = pair(CT['C2'], UASK['K']) * ~pair(CT['C3'], UASK['KS']) n_a = 1 ugpk1, ugsk2 = USK e_gg_auns = 1 for attr in pruned: x = attr.getAttributeAndIndex() y = attr.getAttribute() temp = \ pair(CT['C'][y], ugpk1) * \ pair(CT['D'][y], UASK['AK'][y]) * \ pair(CT['CS'][y], ~(UASK['KS'] ** ugsk2)) * \ ~pair(GPP['g'], CT['DS'][y]) e_gg_auns *= temp**(coeffs[x] * n_a) return CT['C1'] / (first / e_gg_auns) def ukeygen(self, GPP, authority, attribute, userObj): '''Generate update keys for users and cloud provider (executed by attribute authority?)''' ASK, _, authAttrs = authority oldVersionKey = authAttrs[attribute]['VK'] newVersionKey = oldVersionKey while oldVersionKey == newVersionKey: newVersionKey = self.group.random() authAttrs[attribute]['VK'] = newVersionKey u_uid = userObj['sk'] UKs = GPP['H'](attribute)**(ASK['beta'] * (newVersionKey - oldVersionKey) * (u_uid + ASK['gamma'])) UKc = (newVersionKey / oldVersionKey, (oldVersionKey - newVersionKey) / (oldVersionKey * ASK['gamma'])) authAttrs[attribute]['PK1'] = authAttrs[attribute]['PK1']**UKc[0] authAttrs[attribute]['PK2'] = authAttrs[attribute]['PK2']**UKc[0] return {'UKs': UKs, 'UKc': UKc} def skupdate(self, USK, attribute, UKs): '''Updates the user attribute secret key for the specified attribute (executed by non-revoked user)''' USK['AK'][attribute] = USK['AK'][attribute] * UKs def ctupdate(self, GPP, CT, attribute, UKc): '''Updates the cipher-text using the update key, because of the revoked attribute (executed by cloud provider)''' CT['C'][attribute] = CT['C'][attribute] * (CT['DS'][attribute]**UKc[1]) CT['DS'][attribute] = CT['DS'][attribute]**UKc[0]
class MaabeKPA(ABEncMultiAuth): """ A Multiauthority Attribute Based Cryptosystem with Expressive Policies >>> group = PairingGroup('SS512') >>> n_users = 8 >>> height = 3 >>> tree = Tree(n_users, height) >>> maabe = MaabeKPA(group, tree) >>> public_parameters = maabe.setup() Setup the attribute authorities >>> attributes1 = ['ONE', 'TWO', 'THREE'] >>> attributes2 = ['FOUR', 'FIVE', 'SIX'] >>> attributes3 = ['SEVEN', 'EIGHT', 'NINE'] >>> attributes4 = ['TEN', 'ELEVEN', 'TWELVE'] >>> (public_key1, secret_key1) = maabe.authsetup(public_parameters, 'AB') >>> (public_key2, secret_key2) = maabe.authsetup(public_parameters, 'CD') >>> (public_key3, secret_key3) = maabe.authsetup(public_parameters, 'EF') >>> (public_key4, secret_key4) = maabe.authsetup(public_parameters, 'GH') >>> public_keys = {'AB': public_key1, 'CD': public_key2, 'EF': public_key3, 'GH': public_key4} >>> secret_keys = {'AB': secret_key1, 'CD': secret_key2, 'EF': secret_key3, 'GH': secret_key4} Setup a user and give him some keys >>> gid = 2 >>> user_attributes1 = ['A1@AB', 'B1@AB', 'C1@AB'] >>> user_attributes2 = ['D1@CD', 'E1@CD', 'F1@CD'] >>> user_attributes3 = ['A2@EF', 'B2@EF', 'C2@EF'] >>> user_attributes4 = ['D2@GH', 'E2@GH', 'F2@GH'] >>> path = tree.Path(2) >>> user_keys1 = maabe.multiple_attributes_keygen(public_parameters, secret_key1, gid, path, user_attributes1) >>> user_keys2 = maabe.multiple_attributes_keygen(public_parameters, secret_key2, gid, path, user_attributes2) >>> user_keys3 = maabe.multiple_attributes_keygen(public_parameters, secret_key3, gid, path, user_attributes3) >>> user_keys4 = maabe.multiple_attributes_keygen(public_parameters, secret_key4, gid, path, user_attributes4) >>> user_keys = {'GID': gid, 'keys': merge_dicts(user_keys1, user_keys2, user_keys3, user_keys4)} >>> update_key1 = maabe.update_keygen(public_parameters, gid, public_key1, secret_key1, user_keys1, user_attributes1, 20) >>> update_key2 = maabe.update_keygen(public_parameters, gid, public_key2, secret_key2, user_keys2, user_attributes2, 20) >>> update_key3 = maabe.update_keygen(public_parameters, gid, public_key3, secret_key3, user_keys3, user_attributes3, 20) >>> update_key4 = maabe.update_keygen(public_parameters, gid, public_key4, secret_key4, user_keys4, user_attributes4, 20) >>> decrypt_key1 = maabe.decrypt_keygen(public_parameters, public_key1, user_keys1, gid, update_key1, user_attributes1, True) >>> decrypt_key2 = maabe.decrypt_keygen(public_parameters, public_key2, user_keys2, gid, update_key2, user_attributes2, True) >>> decrypt_key3 = maabe.decrypt_keygen(public_parameters, public_key3, user_keys3, gid, update_key3, user_attributes3, True) >>> decrypt_key4 = maabe.decrypt_keygen(public_parameters, public_key4, user_keys4, gid, update_key4, user_attributes4, True) >>> decrypt_keys = {'GID': gid, 'keys': merge_dicts(decrypt_key1, decrypt_key2, decrypt_key3, decrypt_key4)} Create a random message >>> message = group.random(GT) Encrypt the message >>> access_policy = '(A1@AB or E1@CD) and (C2@EF or D2@GH)' >>> cipher_text = maabe.encrypt(public_parameters, public_keys, message, access_policy, 20) print("Decryption key") print(decrypt_keys['keys'].keys()) print("User key") print(user_keys['keys'].keys()) Decrypt the message >>> decrypted_message = maabe.decrypt(gid, public_parameters, user_keys, decrypt_keys, cipher_text) >>> decrypted_message == message True Check for key sanity >>> sanityCheck = maabe.sanitycheck(public_parameters, secret_keys, public_keys, user_keys) Sanity Check SanityCheck1: True SanityCheck2: True >>> sanityCheck == True True """ def __init__(self, group, tree, verbose=False): ABEncMultiAuth.__init__(self) self.group = group self.tree = tree self.util = SecretUtil(group, verbose) def setup(self): g1 = self.group.random(G1) g2 = self.group.random(G2) egg = pair(g1, g2) H = lambda x: self.group.hash(x, G2) F = lambda x: self.group.hash(x, G2) gp = {'g1': g1, 'g2': g2, 'egg': egg, 'H': H, 'F': F} if debug: print("Setup") print(gp) return gp def unpack_attribute(self, attribute): """ Unpacks an attribute in attribute name, authority name and index :param attribute: The attribute to unpack :return: The attribute name, authority name and the attribute index, if present. >>> group = PairingGroup('SS512') >>> n_users = 8 >>> height = 3 >>> tree = Tree(n_users, height) >>> maabe = MaabeKPA(group, tree) >>> maabe.unpack_attribute('STUDENT@UT') ('STUDENT', 'UT', None) >>> maabe.unpack_attribute('STUDENT@UT_2') ('STUDENT', 'UT', '2') """ parts = re.split(r"[@_]", attribute) assert len(parts) > 1, "No @ char in [attribute@authority] name" return parts[0], parts[1], None if len(parts) < 3 else parts[2] def authsetup(self, gp, name): N, d = 8, 5 alpha, y = self.group.random(), self.group.random() a, b = self.group.random(), self.group.random() egga = gp['egg']**alpha gy = gp['g1']**y ga = gp['g1']**a gb = gp['g1']**b g2a = gp['g2']**a g2b = gp['g2']**b f_list = list() r_list = dict() for i in range(0, d + 1): f_list.append(self.group.random(G2)) for i in range(1, 2 * N): r_list[i] = self.group.random() pk = { 'name': name, 'egga': egga, 'gy': gy, 'ga': ga, 'gb': gb, 'f_list': f_list, 'g2a': g2a, 'g2b': g2b } sk = { 'name': name, 'alpha': alpha, 'y': y, 'a': a, 'b': b, 'r_list': r_list } if debug: print("Authsetup: %s" % name) print(pk) print(sk) return pk, sk def J(self, f_list, d, t): t_binary = bin(t)[2:] prod = self.group.init(G2, 1) prod *= f_list[0] for i in range(1, d + 1): if t_binary[i - 1] == '1': prod *= f_list[i] return prod def keygen(self, gp, sk, gid, attribute, w): """ Generate a user secret key for the attribute. :param gp: The global parameters. :param sk: The secret key of the attribute authority. :param gid: The global user identifier. :param attribute: The attribute. :param w: The node in the path :return: The secret key for the attribute for the user with identifier gid. """ _, auth, _ = self.unpack_attribute(attribute) assert sk[ 'name'] == auth, "Attribute %s does not belong to authority %s" % ( attribute, sk['name']) tt = self.group.random() r = self.group.random() j = (sk['a'] + sk['b'] * r) K1 = gp['g2']**((sk['alpha'] - sk['r_list'][w]) / (j + gid)) K1 *= gp['H'](str(gid))**(sk['y'] / (j + gid)) K1 *= gp['F'](attribute)**tt K2 = gid K3 = r K4 = gp['g1']**tt K5 = gp['g1']**(j * tt) if debug: print("Keygen") print("User, Attribute, Node") print(gid) print(attribute) print(w) print({'K1': K1, 'K2': K2, 'K3': K3, 'K4': K4, 'K5': K5}) return {'K1': K1, 'K2': K2, 'K3': K3, 'K4': K4, 'K5': K5} def multiple_attributes_keygen(self, gp, sk, gid, path, attributes): """ Generate a dictionary of secret keys for a user for a list of attributes. :param gp: The global parameters. :param sk: The secret key of the attribute authority. :param gid: The global user identifier. :param attributes: The list of attributes. :return: A dictionary with attribute names as keys, and secret keys for the attributes as values. """ usk = {} for attribute in attributes: usk[attribute] = {} for w in path: usk[attribute][w] = self.keygen(gp, sk, gid, attribute, w) return usk def update_keygen(self, gp, gid, pks, sk, user_key, attributes, t): X, Y = self.tree.get_sets() gid_hash = gid uk = {} for attribute in attributes: uk[attribute] = {} for w in Y: m = self.group.random() r = user_key[attribute][w]['K3'] j = (sk['a'] + sk['b'] * r) uk1 = (gp['g2']**((sk['r_list'][w]) / (j + gid_hash))) * (self.J( pks['f_list'], 5, t)**(m / (j + gid_hash))) uk2 = gp['g1']**m uk3 = self.J(pks['f_list'], 5, t)**(1 / (j + gid_hash)) uk[attribute][w] = {'U1': uk1, 'U2': uk2, 'U3': uk3} if debug: print("Update keygen") print(uk) return uk def decrypt_keygen(self, gp, pks, usk, gid, uk, attributes, first_iter): X, Y = self.tree.get_sets() path = set(self.tree.Path(gid)) common = path.intersection(set(Y)) dsk = {} for w in common: if first_iter: m = 0 else: m = self.group.random() for attribute in attributes: D1 = usk[attribute][w]['K1'] * uk[attribute][w]['U1'] * ( uk[attribute][w]['U3']**m) D2 = usk[attribute][w]['K4'] Dt = uk[attribute][w]['U2'] * gp['g1']**m dsk[attribute] = {'D1': D1, 'D2': D2, 'Dt': Dt} if debug: print("Decrypt keygen") print(dsk) return dsk def encrypt(self, gp, pks, message, policy_str, t): """ Encrypt a message under an access policy :param gp: The global parameters. :param pks: The public keys of the relevant attribute authorities, as dict from authority name to public key. :param message: The message to encrypt. :param policy_str: The access policy to use. :return: The encrypted message. """ s = self.group.random() # secret to be shared w = self.group.init(ZR, 0) # 0 to be shared policy = self.util.createPolicy(policy_str) attribute_list = self.util.getAttributeList(policy) secret_shares = self.util.calculateSharesDict( s, policy) # These are correctly set to be exponents in Z_p zero_shares = self.util.calculateSharesDict(w, policy) C0 = message * (gp['egg']**s) C1, C2, C3, C4, C5, C6, Ct = {}, {}, {}, {}, {}, {}, {} b = bin(t)[2:] testing = list() rx_list = {} for i in attribute_list: attribute_name, auth, _ = self.unpack_attribute(i) attr = "%s@%s" % (attribute_name, auth) rx = self.group.random() rx_list[i] = rx C1[i] = gp['egg']**secret_shares[i] * pks[auth]['egga']**rx C2[i] = gp['g1']**(-rx) C3[i] = pks[auth]['gy']**rx * gp['g1']**zero_shares[i] C4[i] = gp['F'](attr)**rx C5[i] = pks[auth]['ga']**(-rx) C6[i] = pks[auth]['gb']**(-rx) Ct[i] = self.group.init(G2, 1) Ct[i] *= pks[auth]['f_list'][0] for k in range(0, len(b)): if b[k] == '1': Ct[i] *= pks[auth]['f_list'][k + 1] Ct[i] = Ct[i]**rx if debug: print("Encrypt") print(message) print({ 'policy': policy_str, 'C0': C0, 'C1': C1, 'C2': C2, 'C3': C3, 'C4': C4, 'C5': C5, 'C6': C6, 'Ct': Ct }) print("Testing value") print(testing) return { 'policy': policy_str, 'C0': C0, 'C1': C1, 'C2': C2, 'C3': C3, 'C4': C4, 'C5': C5, 'C6': C6, 'Ct': Ct, 'secret_shares': secret_shares, 'zero_shares': zero_shares, 'rx': rx_list } # def update_encrypt(self, gp, pks, ct): # """ # Encrypt a message under an access policy # :param gp: The global parameters. # :param pks: The public keys of the relevant attribute authorities, as dict from authority name to public key. # :param message: The message to encrypt. # :param policy_str: The access policy to use. # :return: The encrypted message. # """ # s_new = self.group.random() # secret to be shared # w_new = self.group.init(ZR, 0) # 0 to be shared # policy = self.util.createPolicy(ct['policy']) # attribute_list = self.util.getAttributeList(policy) # secret_shares = self.util.calculateSharesDict(s, policy) # These are correctly set to be exponents in Z_p # zero_shares = self.util.calculateSharesDict(w, policy) # C0_new = ct['C0'] * (gp['egg'] ** s_new) # C1_new, C2_new, C3_new, C4_new, C5_new, C6_new = {}, {}, {}, {}, {}, {} # for i in attribute_list: # attribute_name, auth, _ = self.unpack_attribute(i) # attr = "%s@%s" % (attribute_name, auth) # rx_new = self.group.random() # C1_new[i] = ct['C1'][i] * gp['egg'] ** secret_shares[i] * pks[auth]['egga'] ** rx_new # C2_new[i] = ct['C2'][i] * gp['g1'] ** (-rx_new) # C3_new[i] = ct['C3'][i] * pks[auth]['gy'] ** rx_new * gp['g1'] ** zero_shares[i] # C4_new[i] = ct['C4'][i] * gp['F'](attr) ** rx_new # C5_new[i] = ct['C5'][i] * pks[auth]['ga'] ** (-rx_new) # C6_new[i] = ct['C6'][i] * pks[auth]['gb'] ** (-rx_new) # if debug: # print("Update Encrypt") # print(message) # print({'policy': policy_str, 'C0_new': C0_new, 'C1_new': C1_new, 'C2_new': C2_new, 'C3_new': C3_new, 'C4_new': C4_new, 'C5_new': C5_new, 'C6_new': C6_new}) # return {'policy': policy_str, 'C0_new': C0_new, 'C1_new': C1_new, 'C2_new': C2_new, 'C3_new': C3_new, 'C4_new': C4_new, 'C5_new': C5_new, 'C6_new': C6_new} def decrypt(self, gi, gp, usk, dsk, ct): """ Decrypt the ciphertext using the secret keys of the user. :param gp: The global parameters. :param sk: The secret keys of the user. :param ct: The ciphertext to decrypt. :return: The decrypted message. :raise Exception: When the access policy can not be satisfied with the user's attributes. """ X, Y = self.tree.get_sets() path = set(self.tree.Path(gi)) common = path.intersection(set(Y)) gid = list(common)[0] policy = self.util.createPolicy(ct['policy']) coefficients = self.util.getCoefficients(policy) pruned_list = self.util.prune(policy, usk['keys'].keys()) if not pruned_list: raise Exception( "You don't have the required attributes for decryption!") B = self.group.init(GT, 1) for i in range(len(pruned_list)): x = pruned_list[i].getAttribute() # without the underscore # y = pruned_list[i].getAttributeAndIndex() # with the underscore t1 = ct['C2'][x]**usk['keys'][x][gid]['K2'] t1 *= ct['C5'][x] t1 *= ct['C6'][x]**usk['keys'][x][gid]['K3'] result1 = pair(t1, dsk['keys'][x]['D1']) result2 = ct['C1'][x] result3 = pair(dsk['keys'][x]['Dt'], ct['Ct'][x]) result4 = pair((dsk['keys'][x]['D2']**usk['keys'][x][gid]['K2']) * usk['keys'][x][gid]['K5'], ct['C4'][x]) result5 = pair(ct['C3'][x], gp['H'](str(usk['keys'][x][gid]['K2']))) B *= (result1 * result2 * result3 * result4 * result5)**coefficients[x] decrypted_message = ct['C0'] / B if debug: print("Decrypt") print("DSK:") print(dsk) print("Decrypted Message:") print(decrypted_message) return decrypted_message def sanitycheck(self, gp, sks, pks, usk): # self.group.StartBenchmark(["RealTime", "Mul", "Exp", "Pair"]) # policy = self.util.createPolicy(policy_ct) # pruned_list = self.util.prune(policy, sk['keys'].keys()) # if not pruned_list: # raise Exception("You don't have the required attributes for decryption!") attributes = usk['keys'].keys() keySanityCheck = False for x in attributes: # x = pruned_list[i].getAttribute() # keySanityCheck = True keySanityCheck1 = False keySanityCheck2 = False attribute_name, auth, _ = self.unpack_attribute(x) for w in usk['keys'][x].keys(): keySanityCheck1 = keySanityCheck1 or (pair( usk['keys'][x][w]['K5'], gp['g2']) == pair( usk['keys'][x][w]['K4'], pks[auth]['g2a'] * (pks[auth]['g2b']**usk['keys'][x][w]['K3']))) keySanityCheck2 = keySanityCheck2 or ( pair( pks[auth]['ga'] * (pks[auth]['gb']**usk['keys'][x][w]['K3']) * (gp['g1']**usk['keys'][x][w]['K2']), usk['keys'][x][w]['K1']) == (gp['egg']** (sks[auth]['alpha'] - sks[auth]['r_list'][w])) * pair(pks[auth]['gy'], gp['H'](str( usk['keys'][x][w]['K2']))) * pair( usk['keys'][x][w]['K5'] * (usk['keys'][x][w]['K4']**usk['keys'][x][w]['K2']), gp['F'](x))) keySanityCheck = (keySanityCheck1 and keySanityCheck2) if keySanityCheck: break if debug: print("Sanity Check") print("SanityCheck1:", keySanityCheck1) print("SanityCheck2:", keySanityCheck2) return keySanityCheck
class MaabeKPA(ABEncMultiAuth): """ A Multiauthority Attribute Based Cryptosystem with Expressive Policies >>> group = PairingGroup('SS512') >>> maabe = MaabeKPA(group) >>> public_parameters = maabe.setup() Setup the attribute authorities attributes1 = ['ONE', 'TWO', 'THREE', 'FOUR'] attributes2 = ['THREE', 'FOUR'] >>> (public_key1, secret_key1) = maabe.authsetup(public_parameters, 'AB') >>> (public_key2, secret_key2) = maabe.authsetup(public_parameters, 'CD') >>> (public_key3, secret_key3) = maabe.authsetup(public_parameters, 'EF') >>> (public_key4, secret_key4) = maabe.authsetup(public_parameters, 'GH') >>> public_keys = {'AB': public_key1, 'CD': public_key2, 'EF': public_key3, 'GH': public_key4 } Setup a user and give him some keys >>> gid = "bob" >>> user_attributes1 = ['A1@AB', 'B1@AB', 'C1@AB', 'D1@AB'] >>> user_attributes2 = ['A2@CD', 'B2@CD', 'C2@CD', 'D2@CD'] >>> user_attributes3 = ['A3@EF', 'B3@EF', 'C3@EF', 'D3@EF'] >>> user_attributes4 = ['A4@GH', 'B4@GH', 'C4@GH', 'D4@GH'] >>> user_keys1 = maabe.multiple_attributes_keygen(public_parameters, secret_key1, gid, user_attributes1) >>> user_keys2 = maabe.multiple_attributes_keygen(public_parameters, secret_key2, gid, user_attributes2) >>> user_keys3 = maabe.multiple_attributes_keygen(public_parameters, secret_key3, gid, user_attributes3) >>> user_keys4 = maabe.multiple_attributes_keygen(public_parameters, secret_key4, gid, user_attributes4) >>> user_keys = {'GID': gid, 'keys': merge_dicts(user_keys1, user_keys2, user_keys3, user_keys4)} Create a random message >>> message = group.random(GT) Encrypt the message >>> access_policy = '(A1@AB or D2@CD) and (B3@EF or C4@GH)' >>> cipher_text = maabe.encrypt(public_parameters, public_keys, message, access_policy) Decrypt the message >>> decrypted_message = maabe.decrypt(public_parameters, user_keys, cipher_text) >>> decrypted_message == message True Check for key sanity >>> sanityCheck = maabe.sanitycheck(public_parameters, public_keys, user_keys, cipher_text['policy']) Sanity Check SanityCheck1: True SanityCheck2: True >>> sanityCheck == True True """ def __init__(self, group, verbose=False): ABEncMultiAuth.__init__(self) self.group = group self.util = SecretUtil(group, verbose) f = open('Benchmarks.txt', 'a+') f.write("\n") f.close() def setup(self): g1 = self.group.random(G1) g2 = self.group.random(G2) egg = pair(g1, g2) H = lambda x: self.group.hash(x, G2) F = lambda x: self.group.hash(x, G2) gp = {'g1': g1, 'g2': g2, 'egg': egg, 'H': H, 'F': F} if debug: print("Setup") print(gp) return gp def unpack_attribute(self, attribute): """ Unpacks an attribute in attribute name, authority name and index :param attribute: The attribute to unpack :return: The attribute name, authority name and the attribute index, if present. >>> group = PairingGroup('SS512') >>> maabe = MaabeKPA(group) >>> maabe.unpack_attribute('STUDENT@UT') ('STUDENT', 'UT', None) >>> maabe.unpack_attribute('STUDENT@UT_2') ('STUDENT', 'UT', '2') """ parts = re.split(r"[@_]", attribute) assert len(parts) > 1, "No @ char in [attribute@authority] name" return parts[0], parts[1], None if len(parts) < 3 else parts[2] def authsetup(self, gp, name): """ Setup an attribute authority. :param gp: The global parameters :param name: The name of the authority :return: The public and private key of the authority """ f = open('Benchmarks.txt', 'a+') assert self.group.InitBenchmark(), "failed to initialize benchmark" self.group.StartBenchmark(["RealTime", "Mul", "Exp", "Pair"]) alpha, y = self.group.random(), self.group.random() a, b = self.group.random(), self.group.random() egga = gp['egg']**alpha gy = gp['g1']**y ga = gp['g1']**a gb = gp['g1']**b pk = {'name': name, 'egga': egga, 'gy': gy, 'ga': ga, 'gb': gb} sk = {'name': name, 'alpha': alpha, 'y': y, 'a': a, 'b': b} self.group.EndBenchmark() msmtDict = self.group.GetGeneralBenchmarks() f.write("\nAuth Setup Benchmarks for %s:" % name) json.dump(msmtDict, f) f.close() if debug: print("Authsetup: %s" % name) print(pk) print(sk) return pk, sk def keygen(self, gp, sk, gid, attribute): """ Generate a user secret key for the attribute. :param gp: The global parameters. :param sk: The secret key of the attribute authority. :param gid: The global user identifier. :param attribute: The attribute. :return: The secret key for the attribute for the user with identifier gid. """ _, auth, _ = self.unpack_attribute(attribute) assert sk[ 'name'] == auth, "Attribute %s does not belong to authority %s" % ( attribute, sk['name']) t = self.group.random() i = self.group.random() gid_hash = self.group.hash(gid, ZR) j = sk['a'] + sk['a'] * sk['b'] * gid_hash K4 = gp['g2']**i K1 = gp['g2']**sk['alpha'] * K4**j * gp['H'](gid)**sk['y'] * gp['F']( attribute)**t K2 = gid K3 = K4**sk['b'] K5 = gp['g1']**t if debug: print("Keygen") print("User: %s, Attribute: %s" % (gid, attribute)) print({'K1': K1, 'K2': K2, 'K3': K3, 'K4': K4, 'K5': K5}) return {'K1': K1, 'K2': K2, 'K3': K3, 'K4': K4, 'K5': K5} def multiple_attributes_keygen(self, gp, sk, gid, attributes): """ Generate a dictionary of secret keys for a user for a list of attributes. :param gp: The global parameters. :param sk: The secret key of the attribute authority. :param gid: The global user identifier. :param attributes: The list of attributes. :return: A dictionary with attribute names as keys, and secret keys for the attributes as values. """ uk = {} f = open('Benchmarks.txt', 'a+') # assert self.group.InitBenchmark(), "failed to initialize benchmark" self.group.StartBenchmark(["RealTime", "Mul", "Exp", "Pair"]) for attribute in attributes: uk[attribute] = self.keygen(gp, sk, gid, attribute) self.group.EndBenchmark() msmtDict = self.group.GetGeneralBenchmarks() f.write("\nTotal Keygen Benchmarks:") json.dump(msmtDict, f) f.close() return uk def encrypt(self, gp, pks, message, policy_str): """ Encrypt a message under an access policy :param gp: The global parameters. :param pks: The public keys of the relevant attribute authorities, as dict from authority name to public key. :param message: The message to encrypt. :param policy_str: The access policy to use. :return: The encrypted message. """ f = open('Benchmarks.txt', 'a+') s = self.group.random() # secret to be shared w = self.group.init(ZR, 0) # 0 to be shared policy = self.util.createPolicy(policy_str) attribute_list = self.util.getAttributeList(policy) # assert self.group.InitBenchmark(), "failed to initialize benchmark" self.group.StartBenchmark(["RealTime", "Mul", "Exp", "Pair"]) secret_shares = self.util.calculateSharesDict( s, policy) # These are correctly set to be exponents in Z_p zero_shares = self.util.calculateSharesDict(w, policy) C0 = message * (gp['egg']**s) C1, C2, C3, C4, C5 = {}, {}, {}, {}, {} for i in attribute_list: attribute_name, auth, _ = self.unpack_attribute(i) attr = "%s@%s" % (attribute_name, auth) tx = self.group.random() C1[i] = gp['egg']**secret_shares[i] * pks[auth]['egga']**tx C2[i] = gp['g1']**(-tx) C3[i] = pks[auth]['gy']**tx * gp['g1']**zero_shares[i] C4[i] = gp['F'](attr)**tx C5[i] = pks[auth]['ga']**tx self.group.EndBenchmark() msmtDict = self.group.GetGeneralBenchmarks() f.write("\nEncryption Benchmarks:") json.dump(msmtDict, f) # granDict = self.group.GetGranularBenchmarks() # print("<=== General Benchmarks ===>") # print("Results := ", msmtDict) # print("<=== Granular Benchmarks ===>") # print("G1 mul := ", granDict["Mul"][G1]) f.close() if debug: print("Encrypt") print(message) print({ 'policy': policy_str, 'C0': C0, 'C1': C1, 'C2': C2, 'C3': C3, 'C4': C4, 'C5': C5 }) return { 'policy': policy_str, 'C0': C0, 'C1': C1, 'C2': C2, 'C3': C3, 'C4': C4, 'C5': C5 } def decrypt(self, gp, sk, ct): """ Decrypt the ciphertext using the secret keys of the user. :param gp: The global parameters. :param sk: The secret keys of the user. :param ct: The ciphertext to decrypt. :return: The decrypted message. :raise Exception: When the access policy can not be satisfied with the user's attributes. """ f = open('Benchmarks.txt', 'a+') # assert self.group.InitBenchmark(), "failed to initialize benchmark" self.group.StartBenchmark(["RealTime", "Mul", "Exp", "Pair"]) policy = self.util.createPolicy(ct['policy']) coefficients = self.util.getCoefficients(policy) pruned_list = self.util.prune(policy, sk['keys'].keys()) print(pruned_list) if not pruned_list: raise Exception( "You don't have the required attributes for decryption!") B = self.group.init(GT, 1) for i in range(len(pruned_list)): x = pruned_list[i].getAttribute() # without the underscore y = pruned_list[i].getAttributeAndIndex() # with the underscore K2_hash = self.group.hash(sk['keys'][x]['K2'], ZR) B *= (ct['C1'][y] * pair(ct['C2'][y], sk['keys'][x]['K1']) * pair(ct['C3'][y], gp['H'](sk['keys'][x]['K2'])) * pair(sk['keys'][x]['K5'], ct['C4'][y]) * pair(ct['C5'][y], sk['keys'][x]['K4'] * sk['keys'][x]['K3']**K2_hash))**coefficients[y] decrypted_message = ct['C0'] / B self.group.EndBenchmark() msmtDict = self.group.GetGeneralBenchmarks() f.write("\nDecryption Benchmarks:") json.dump(msmtDict, f) f.close() if debug: print("Decrypt") print("SK:") print(sk['keys'].keys()) print(x) print(y) print("Decrypted Message:") print(decrypted_message) return decrypted_message def sanitycheck(self, gp, pks, sk, policy_ct): f = open('Benchmarks.txt', 'a+') # assert self.group.InitBenchmark(), "failed to initialize benchmark" self.group.StartBenchmark(["RealTime", "Mul", "Exp", "Pair"]) policy = self.util.createPolicy(policy_ct) pruned_list = self.util.prune(policy, sk['keys'].keys()) if not pruned_list: raise Exception( "You don't have the required attributes for decryption!") keySanityCheck = False for i in range(len(pruned_list)): x = pruned_list[i].getAttribute() attribute_name, auth, _ = self.unpack_attribute(x) K2_hash = self.group.hash(sk['keys'][x]['K2'], ZR) keySanityCheck1 = (pair(gp['g1'], sk['keys'][x]['K3']) == pair( pks[auth]['gb'], sk['keys'][x]['K4'])) keySanityCheck2 = ( pair(gp['g1'], sk['keys'][x]['K1']) == pks[auth]['egga'] * pair(pks[auth]['gy'], gp['H'](sk['keys'][x]['K2'])) * pair(sk['keys'][x]['K5'], gp['F'](x)) * pair(pks[auth]['ga'], sk['keys'][x]['K4'] * sk['keys'][x]['K3']**K2_hash)) keySanityCheck = keySanityCheck1 and keySanityCheck2 if keySanityCheck: break self.group.EndBenchmark() msmtDict = self.group.GetGeneralBenchmarks() f.write("\nSanity Check Benchmarks:") json.dump(msmtDict, f) f.close() if debug: print("Sanity Check") print("SanityCheck1:", keySanityCheck1) print("SanityCheck2:", keySanityCheck2) return keySanityCheck
class MaabeRW15(ABEncMultiAuth): """ Efficient Statically-Secure Large-Universe Multi-Authority Attribute-Based Encryption Rouselakis - Waters """ def __init__(self, group, verbose=False): ABEncMultiAuth.__init__(self) self.group = group self.util = SecretUtil(group, verbose) def setup(self): g1 = self.group.random(G1) g2 = self.group.random(G2) egg = pair(g1, g2) H = lambda x: self.group.hash(x, G2) F = lambda x: self.group.hash(x, G2) gp = {'g1': g1, 'g2': g2, 'egg': egg, 'H': H, 'F': F} if debug: print("Setup") print(gp) return gp def unpack_attribute(self, attribute): """ Unpacks an attribute in attribute name, authority name and index :param attribute: The attribute to unpack :return: The attribute name, authority name and the attribute index, if present. group = PairingGroup('SS512') maabe = MaabeRW15(group) maabe.unpack_attribute('STUDENT@UT') ('STUDENT', 'UT', None) maabe.unpack_attribute('STUDENT@UT_2') ('STUDENT', 'UT', '2') """ parts = re.split(r"[@_]", attribute) assert len(parts) > 1, "No @ char in [attribute@authority] name" return parts[0], parts[1], None if len(parts) < 3 else parts[2] def authsetup(self, gp, name): """ Setup an attribute authority. :param gp: The global parameters :param name: The name of the authority :return: The public and private key of the authority """ alpha, y = self.group.random(), self.group.random() egga = gp['egg']**alpha gy = gp['g1']**y pk = {'name': name, 'egga': egga, 'gy': gy} sk = {'name': name, 'alpha': alpha, 'y': y} if debug: print("Authsetup: %s" % name) print(pk) print(sk) return pk, sk def keygen(self, gp, sk, gid, attribute): """ Generate a user secret key for the attribute. :param gp: The global parameters. :param sk: The secret key of the attribute authority. :param gid: The global user identifier. :param attribute: The attribute. :return: The secret key for the attribute for the user with identifier gid. """ _, auth, _ = self.unpack_attribute(attribute) assert sk[ 'name'] == auth, "Attribute %s does not belong to authority %s" % ( attribute, sk['name']) t = self.group.random() K = gp['g2']**sk['alpha'] * gp['H'](gid)**sk['y'] * gp['F']( attribute)**t KP = gp['g1']**t if debug: print("Keygen") print("User: %s, Attribute: %s" % (gid, attribute)) print({'K': K, 'KP': KP}) return {'K': K, 'KP': KP} def multiple_attributes_keygen(self, gp, sk, gid, attributes): """ Generate a dictionary of secret keys for a user for a list of attributes. :param gp: The global parameters. :param sk: The secret key of the attribute authority. :param gid: The global user identifier. :param attributes: The list of attributes. :return: A dictionary with attribute names as keys, and secret keys for the attributes as values. """ uk = {} for attribute in attributes: uk[attribute] = self.keygen(gp, sk, gid, attribute) return uk def encrypt(self, gp, pks, message, policy_str): """ Encrypt a message under an access policy :param gp: The global parameters. :param pks: The public keys of the relevant attribute authorities, as dict from authority name to public key. :param message: The message to encrypt. :param policy_str: The access policy to use. :return: The encrypted message. """ s = self.group.random() # secret to be shared w = self.group.init(ZR, 0) # 0 to be shared policy = self.util.createPolicy(policy_str) attribute_list = self.util.getAttributeList(policy) secret_shares = self.util.calculateSharesDict( s, policy) # These are correctly set to be exponents in Z_p zero_shares = self.util.calculateSharesDict(w, policy) C0 = message * (gp['egg']**s) C1, C2, C3, C4 = {}, {}, {}, {} for i in attribute_list: attribute_name, auth, _ = self.unpack_attribute(i) attr = "%s@%s" % (attribute_name, auth) tx = self.group.random() C1[i] = gp['egg']**secret_shares[i] * pks[auth]['egga']**tx C2[i] = gp['g1']**(-tx) C3[i] = pks[auth]['gy']**tx * gp['g1']**zero_shares[i] C4[i] = gp['F'](attr)**tx if debug: print("Encrypt") print(message) print({ 'policy': policy_str, 'C0': C0, 'C1': C1, 'C2': C2, 'C3': C3, 'C4': C4 }) return { 'policy': policy_str, 'C0': C0, 'C1': C1, 'C2': C2, 'C3': C3, 'C4': C4 } def decrypt(self, gp, sk, ct): """ Decrypt the ciphertext using the secret keys of the user. :param gp: The global parameters. :param sk: The secret keys of the user. :param ct: The ciphertext to decrypt. :return: The decrypted message. :raise Exception: When the access policy can not be satisfied with the user's attributes. """ policy = self.util.createPolicy(ct['policy']) coefficients = self.util.getCoefficients(policy) pruned_list = self.util.prune(policy, sk['keys'].keys()) if not pruned_list: raise Exception( "You don't have the required attributes for decryption!") B = self.group.init(GT, 1) for i in range(len(pruned_list)): x = pruned_list[i].getAttribute() # without the underscore y = pruned_list[i].getAttributeAndIndex() # with the underscore B *= (ct['C1'][y] * pair(ct['C2'][y], sk['keys'][x]['K']) * pair(ct['C3'][y], gp['H'](sk['GID'])) * pair(sk['keys'][x]['KP'], ct['C4'][y]))**coefficients[y] if debug: print("Decrypt") print("SK:") print(sk) print("Decrypted Message:") print(ct['C0'] / B) return ct['C0'] / B
class DACMACS(object): def __init__(self, groupObj): self.util = SecretUtil(groupObj, verbose=False) #Create Secret Sharing Scheme self.group = groupObj #:Prime order group def setup(self): '''Global Setup (executed by CA)''' #:In global setup, a bilinear group G of prime order p is chosen #:The global public parameters, GP and p, and a generator g of G. A random oracle H maps global identities GID to elements of G #:group contains #:the prime order p is contained somewhere within the group object g = self.group.random(G1) #: The oracle that maps global identities GID onto elements of G #:H = lambda str: g** group.hash(str) H = lambda x: self.group.hash(x, G1) a = self.group.random() g_a = g**a GPP = {'g': g, 'g_a': g_a, 'H': H} GMK = {'a': a} return (GPP, GMK) def registerUser(self, GPP): '''Generate user keys (executed by the user).''' g = GPP['g'] u = self.group.random() z = self.group.random() g_u = g**u g_z = g**(1 / z) return ((g_u, z), {'g_z': g_z, 'u': u}) # (private, public) def setupAuthority(self, GPP, authorityid, attributes, authorities): '''Generate attribute authority keys (executed by attribute authority)''' if authorityid not in authorities: alpha = self.group.random() beta = self.group.random() gamma = self.group.random() SK = {'alpha': alpha, 'beta': beta, 'gamma': gamma} PK = { 'e_alpha': pair(GPP['g'], GPP['g'])**alpha, 'g_beta_inv': GPP['g']**(1 / beta), 'g_beta_gamma': GPP['g']**(gamma / beta) } authAttrs = {} authorities[authorityid] = (SK, PK, authAttrs) else: SK, PK, authAttrs = authorities[authorityid] for attrib in attributes: if attrib in authAttrs: continue versionKey = self.group.random() # random or really 'choose' ? h = GPP['H'](attrib) pk = ((GPP['g']**versionKey) * h)**SK['gamma'] authAttrs[attrib] = { 'VK': versionKey, #secret 'PK': pk, #public } return (SK, PK, authAttrs) def keygen(self, GPP, authority, attribute, userObj, USK=None): '''Generate user keys for a specific attribute (executed on attribute authority)''' if 't' not in userObj: userObj['t'] = self.group.random() #private to AA t = userObj['t'] ASK, APK, authAttrs = authority u = userObj if USK is None: USK = {} if 'K' not in USK or 'L' not in USK or 'R' not in USK or 'AK' not in USK: USK['K'] = \ (u['g_z'] ** ASK['alpha']) * \ (GPP['g_a'] ** u['u']) * \ (GPP['g_a'] ** (t / ASK['beta'])) USK['L'] = u['g_z']**(ASK['beta'] * t) USK['R'] = GPP['g_a']**t USK['AK'] = {} AK = (u['g_z'] ** (ASK['beta'] * ASK['gamma'] * t)) * \ (authAttrs[attribute]['PK'] ** (ASK['beta'] * u['u'])) USK['AK'][attribute] = AK return USK def encrypt(self, GPP, policy_str, k, authority): '''Generate the cipher-text from the content(-key) and a policy (executed by the content owner)''' #GPP are global parameters #k is the content key (group element based on AES key) #policy_str is the policy string #authority is the authority tuple _, APK, authAttrs = authority policy = self.util.createPolicy(policy_str) secret = self.group.random() shares = self.util.calculateSharesList(secret, policy) shares = dict([(x[0].getAttributeAndIndex(), x[1]) for x in shares]) C1 = k * (APK['e_alpha']**secret) C2 = GPP['g']**secret C3 = APK['g_beta_inv']**secret C = {} D = {} DS = {} for attr, s_share in shares.items(): k_attr = self.util.strip_index(attr) r_i = self.group.random() attrPK = authAttrs[attr] C[attr] = (GPP['g_a']**s_share) * ~(attrPK['PK']**r_i) D[attr] = APK['g_beta_inv']**r_i DS[attr] = ~(APK['g_beta_gamma']**r_i) return { 'C1': C1, 'C2': C2, 'C3': C3, 'C': C, 'D': D, 'DS': DS, 'policy': policy_str } def generateTK(self, GPP, CT, UASK, g_u): '''Generates a token using the user's attribute secret keys to offload the decryption process (executed by cloud provider)''' usr_attribs = list(UASK['AK'].keys()) policy = self.util.createPolicy(CT['policy']) pruned = self.util.prune(policy, usr_attribs) if pruned == False: return False coeffs = self.util.getCoefficients(policy) dividend = pair(CT['C2'], UASK['K']) * ~pair(UASK['R'], CT['C3']) n_a = 1 divisor = 1 for attr in pruned: x = attr.getAttributeAndIndex() y = attr.getAttribute() temp = \ pair(CT['C'][y], g_u) * \ pair(CT['D'][y], UASK['AK'][y]) * \ pair(CT['DS'][y], UASK['L']) divisor *= temp**(coeffs[x] * n_a) return dividend / divisor def decrypt(self, CT, TK, z): '''Decrypts the content(-key) from the cipher-text using the token and the user secret key (executed by user/content consumer)''' return CT['C1'] / (TK**z) def ukeygen(self, GPP, authority, attribute, userObj): '''Generate update keys for users and cloud provider (executed by attribute authority?)''' ASK, _, authAttrs = authority oldVersionKey = authAttrs[attribute]['VK'] newVersionKey = oldVersionKey while oldVersionKey == newVersionKey: newVersionKey = self.group.random() authAttrs[attribute]['VK'] = newVersionKey u = userObj['u'] AUK = ASK['gamma'] * (newVersionKey - oldVersionKey) KUK = GPP['g']**(u * ASK['beta'] * AUK) CUK = ASK['beta'] * AUK / ASK['gamma'] authAttrs[attribute]['PK'] = authAttrs[attribute]['PK'] * (GPP['g']** AUK) return {'KUK': KUK, 'CUK': CUK} def skupdate(self, USK, attribute, KUK): '''Updates the user attribute secret key for the specified attribute (executed by non-revoked user)''' USK['AK'][attribute] = USK['AK'][attribute] * KUK def ctupdate(self, GPP, CT, attribute, CUK): '''Updates the cipher-text using the update key, because of the revoked attribute (executed by cloud provider)''' CT['C'][attribute] = CT['C'][attribute] * (CT['DS'][attribute]**CUK)
class YLLC15(ABEnc): """ Possibly a subclass of BSW07? """ def __init__(self, group): ABEnc.__init__(self) self.group = group self.util = SecretUtil(self.group) @Output(params_t, msk_t) def setup(self): g, gp = self.group.random(G1), self.group.random(G2) alpha, beta = self.group.random(ZR), self.group.random(ZR) # initialize pre-processing for generators g.initPP() gp.initPP() h = g**beta e_gg_alpha = pair(g, gp**alpha) params = {'g': g, 'g2': gp, 'h': h, 'e_gg_alpha': e_gg_alpha} msk = {'beta': beta, 'alpha': alpha} return params, msk @Input(params_t) @Output(pku_t, sku_t) def ukgen(self, params): g2 = params['g2'] x = self.group.random(ZR) pku = g2**x sku = x return pku, sku @Input(params_t, msk_t, pku_t, pku_t, [str]) # @Output(pxku_t) def proxy_keygen(self, params, msk, pkcs, pku, attribute_list): """ attributes specified in the `attribute_list` are converted to uppercase """ r1 = self.group.random(ZR) r2 = self.group.random(ZR) g = params['g'] g2 = params['g2'] k = ((pkcs**r1) * (pku**msk['alpha']) * (g2**r2))**~msk['beta'] k_prime = g2**r1 k_attrs = {} for attr in attribute_list: attr_caps = attr.upper() r_attr = self.group.random(ZR) k_attr1 = (g2**r2) * (self.group.hash(str(attr_caps), G2)**r_attr) k_attr2 = g**r_attr k_attrs[attr_caps] = (k_attr1, k_attr2) proxy_key_user = {'k': k, 'k_prime': k_prime, 'k_attrs': k_attrs} return proxy_key_user @Input(params_t, GT, str) # @Output(ct_t) def encrypt(self, params, msg, policy_str): """ Encrypt a message M under a policy string. attributes specified in policy_str are converted to uppercase policy_str must use parentheses e.g. (A) and (B) """ policy = self.util.createPolicy(policy_str) s = self.group.random(ZR) shares = self.util.calculateSharesDict(s, policy) C = (params['e_gg_alpha']**s) * msg c_prime = params['h']**s c_prime_prime = params['g']**s c_attrs = {} for attr in shares.keys(): attr_stripped = self.util.strip_index(attr) c_i1 = params['g']**shares[attr] c_i2 = self.group.hash(attr_stripped, G1)**shares[attr] c_attrs[attr] = (c_i1, c_i2) ciphertext = { 'policy_str': policy_str, 'C': C, 'C_prime': c_prime, 'C_prime_prime': c_prime_prime, 'c_attrs': c_attrs } return ciphertext # @Input(sku_t, pxku_t, ct_t) @Output(v_t) def proxy_decrypt(self, skcs, proxy_key_user, ciphertext): policy_root_node = ciphertext['policy_str'] k = proxy_key_user['k'] k_prime = proxy_key_user['k_prime'] c_prime = ciphertext['C_prime'] c_prime_prime = ciphertext['C_prime_prime'] c_attrs = ciphertext['c_attrs'] k_attrs = proxy_key_user['k_attrs'] policy = self.util.createPolicy(policy_root_node) attributes = proxy_key_user['k_attrs'].keys() pruned_list = self.util.prune(policy, attributes) if not pruned_list: return None z = self.util.getCoefficients(policy) # reconstitute the policy random secret (A) which was used to encrypt the message A = 1 for i in pruned_list: attr_idx = i.getAttributeAndIndex() attr = i.getAttribute() A *= (pair(c_attrs[attr_idx][0], k_attrs[attr][0]) / pair(k_attrs[attr][1], c_attrs[attr_idx][1]))**z[attr_idx] e_k_c_prime = pair(k, c_prime) denominator = (pair(k_prime, c_prime_prime)**skcs) * A encrypted_element_for_user_pkenc_scheme = e_k_c_prime / denominator intermediate_value = { 'C': ciphertext['C'], 'e_term': encrypted_element_for_user_pkenc_scheme } return intermediate_value @Input(type(None), sku_t, v_t) @Output(GT) def decrypt(self, params, sku, intermediate_value): """ :param params: Not required - pass None instead. For interface compatibility only. :param sku: the secret key of the user as generated by `ukgen()`. :param intermediate_value: the partially decrypted ciphertext returned by `proxy_decrypt()`. :return: the plaintext message """ ciphertext = intermediate_value['C'] e_term = intermediate_value['e_term'] denominator = e_term**(sku**-1) msg = ciphertext / denominator return msg
class OMACPABE(object): def __init__(self, group_object): # initialize class object with secret sharing utility # and appropriate group object self.util = SecretUtil(group_object, verbose=False) self.group = group_object # certificate authority (CA) setup function def abenc_casetup(self): """ Global setup function run by the CA to generate the Global Master Key (GMK) and the Global Public Parameters (GPP) :return: GMK, GPP """ # initialize bilinear group G of prime p with generator g g = self.group.random(G1) # initialize hash function that maps to an element of G H = lambda x: self.group.hash(x, G1) # select random elements from Z_p a = self.group.random(ZR) b = self.group.random(ZR) g_a = g**a g_b = g**b # Global Public Parameters (GPP) = g, g_a, g_b, H GPP = { 'g': g, 'g_a': g_a, 'g_b': g_b, 'H': H, } # Global Master Key (GMK) = a, b GMK = { 'a': a, 'b': b, } # return (GPP, GMK) def abenc_userreg(self, GPP, entity='user'): """ User registration by Certificate Authority (CA) to generate corresponding key pairs (i.e. Public and Private keys) :param GPP: Global Public Parameters (GPP) :param entity: the entity executing algorithm # :param registered_users: Dictionary of already registered users :return: User Global Secret and Public Keys (GSK_uid, GSK_uid_prime, GPK_uid, GPK_uid_prime) """ # group generator from GPP g = GPP['g'] # random numbers as user global secret keys u_uid = self.group.random(ZR) u_uid_prime = self.group.random(ZR) # user global public keys g_u_uid = g**u_uid g_u_uid_prime = g**(1 / u_uid_prime) # secret public key pair sent to user GSK_uid_prime = u_uid_prime GPK_uid = g_u_uid # secret public key pair to be sent to registered Attribute Authorities (AAs) GSK_uid = u_uid GPK_uid_prime = g_u_uid_prime return (GPK_uid, GSK_uid_prime), { 'GSK_uid': GSK_uid, 'GPK_uid_prime': GPK_uid_prime, 'u_uid': u_uid, } def abenc_aareg(self, GPP, authority_id, attributes, registered_authorities): """ Registration of Attribute Authorities (AA) by the Certificate Authority (CA) :param GPP: Global Public Parameters (GPP) :param authority_id: Unique ID for Attribute Authority :param attributes: Attributes managed by the authority :param registered_authorities: Dictionary of already registered authorities :return: Attribute Authority Secret and Public Key pairs with Version and Public keys for the attributes """ # check if authority has already been registered if authority_id not in registered_authorities: # generate random values to serves as attribute authority secret key alpha_aid = self.group.random(ZR) beta_aid = self.group.random(ZR) gamma_aid = self.group.random(ZR) # attribute authority secret key values SK_aid = { 'alpha_aid': alpha_aid, 'beta_aid': beta_aid, 'gamma_aid': gamma_aid } # attribute authority public key values PK_aid = { 'e_alpha': pair(GPP['g'], GPP['g'])**alpha_aid, 'g_beta_aid': GPP['g']**beta_aid, 'g_beta_aid_inv': GPP['g']**(1 / beta_aid), } authority_attributes = {} registered_authorities[authority_id] = (SK_aid, PK_aid, authority_attributes) else: SK_aid, PK_aid, authority_attributes = registered_authorities[ authority_id] # generate version and public keys for attributes for attribute in attributes: # check if attributes already exist with public and version keys # if they do, skip generation process if attribute in authority_attributes: continue # generate random element as version key version_key = self.group.random(ZR) h = GPP['H'](attribute) PK_1_attribute = h**version_key PK_2_attribute = h**(version_key * SK_aid['gamma_aid']) PK_attribute_aid = [PK_1_attribute, PK_2_attribute] authority_attributes[attribute] = { 'VK': version_key, 'PK': PK_attribute_aid, } return (SK_aid, PK_aid, authority_attributes) def abenc_keygen(self, GPP, authority, attribute, user_object, USK=None): """ Generate attribute authority related secret keys for users (executed by the corresponding attribute authority) :param GPP: Global Public Parameters :param authority: Attribute Authority Parameters :param attribute: Attribute for which secret key is being generated :param user_object: User :param USK: Generated attribute authority related user secret key :return: User Secret Key (USK) """ # generate random integer to tie attribute secret key to user if 't' not in user_object: user_object['t'] = self.group.random(ZR) t = user_object['t'] # assign corresponding attribute authority parameters ASK, APK, authority_attrs = authority u = user_object # create USK data set if none exists already if USK is None: USK = {} if 'K_uid_aid' not in USK or 'K_uid_aid_prime' not in USK or 'AK_uid_aid' not in USK: USK['K_uid_aid'] = (u['GPK_uid_prime']**ASK['alpha_aid']) * ( GPP['g_a']**u['u_uid']) * (GPP['g_b']**t) USK['K_uid_aid_prime'] = GPP['g']**t USK['AK_uid_aid'] = {} # generate attribute specific secret key parameters AK_uid_aid = (GPP['g'] ** (t * ASK['beta_aid'])) * authority_attrs[attribute]['PK'][0] \ ** (ASK['beta_aid'] * (u['u_uid'] + ASK['gamma_aid'])) USK['AK_uid_aid'][attribute] = AK_uid_aid return USK def abenc_encrypt(self, GPP, policy_string, k, authority): """ Encryption algorithm which encrypts the message given, based on the policy :param GPP: Global Public Parameters :param policy_string: Policy :param k: Content Key (i.e group element based on AES key) :param authority: Attribute Authority Parameters :return: Ciphertext """ APK = {} authority_attributes = {} authority_g_beta_inv = {} # extract the APK for the different authorities for authority_temp in authority.keys(): APK[authority_temp] = authority[authority_temp][1] # extract the PK values of the attributes of the attribute authorities # extract the corresponding g_beta_inverse values for the attribute authorities for item in authority[authority_temp][2].keys(): authority_attributes[item] = authority[authority_temp][2][item] authority_g_beta_inv[item] = APK[authority_temp][ 'g_beta_aid_inv'] # extract policy and use policy elements to slit the secret # into their corresponding shares for encryption policy = self.util.createPolicy(policy_string) # generate secret through random element secret = self.group.random(ZR) # split secret into shares (this returns a list) shares = self.util.calculateSharesList(secret, policy) # process shares list to create a dict with attribute as key # and corresponding shares as value shares = dict([(x[0].getAttributeAndIndex(), x[1]) for x in shares]) # initialize blinding factor to hide key blinding_factor = 1 for authority_temp in authority.keys(): blinding_factor *= APK[authority_temp]['e_alpha'] # create C elements of encrypted file C = k * (blinding_factor**secret) C_prime = GPP['g']**secret C_prime_prime = GPP['g_b']**secret # create structure (dict) to hold the C_i and D_i elements of the encrypted file # these are the components related to the attributes C_i = {} C_i_prime = {} D_i = {} D_i_prime = {} # generate C_i and D_i elements for attribute, secret_share in shares.items(): # attribute_temp = self.util.strip_index(attribute) # generate random r_i element k_attr = self.util.strip_index(attribute) r_i = self.group.random(ZR) attribute_PK = authority_attributes[attribute] C_i[attribute] = (GPP['g_a']** secret_share) * ~(attribute_PK['PK'][0]**r_i) C_i_prime[attribute] = GPP['g']**r_i D_i[attribute] = authority_g_beta_inv[attribute]**r_i D_i_prime[attribute] = attribute_PK['PK'][1]**r_i return { 'C': C, 'C_prime': C_prime, 'C_prime_prime': C_prime_prime, 'C_i': C_i, 'C_i_prime': C_i_prime, 'D_i': D_i, 'D_i_prime': D_i_prime, 'policy': policy_string, } def abenc_generatetoken(self, GPP, CT, UASK, user_keys): """ Partial decryption of the ciphertext :param GPP: Global Public Parameters :param CT: Ciphertext elements :param UASK: Secret Keys for user gotten from Attribute Authorities :param user_keys: User global keys :return: Partially decrypted ciphertext """ # list to hold corresponding attributes possessed by the user user_attributes = [] for authority in UASK.keys(): user_attributes.extend(UASK[authority]['AK_uid_aid'].keys()) # access ciphertext policy encryption_policy = self.util.createPolicy(CT['policy']) # generate list of minimum policy elements needed for encryption # returns False if user fails policy assessment minimal_policy_list = self.util.prune(encryption_policy, user_attributes) # print(minimal_policy_list) # this is an error handling implementation that should be fixed later if not minimal_policy_list: return False # get attribute coefficients to be able to access their share of the secret coefficients = self.util.getCoefficients(encryption_policy) # initialize the dividend value for the token generation computation dividend = 1 for authority in UASK.keys(): dividend *= ( pair(CT['C_prime'], UASK[authority]['K_uid_aid']) * ~pair(CT['C_prime_prime'], UASK[authority]['K_uid_aid_prime'])) # attribute authority index? n_a = 1 # initialize divisor value for token generation computation divisor = 1 # create dict to hold attributes for the authorities and their corresponding secret keys attribute_keys = {} # create dict to hold attributes contained in the pruned list and their corresponding secret keys pruned_attribute_keys = {} # populate attribute with with corresponding key value pairs for authority in UASK.keys(): attribute_keys.update(UASK[authority]['AK_uid_aid']) # populate pruned attribute with corresponding key value pairs # from attribute list for attribute in minimal_policy_list: pruned_attribute_keys[str(attribute)] = attribute_keys[str( attribute)] # compute divisor for authority in UASK.keys(): temp_divisor = 1 for attribute in minimal_policy_list: x = attribute.getAttributeAndIndex() y = attribute.getAttribute() temp_divisor *= ( (pair(CT['C_i'][y], user_keys) * pair(CT['D_i'][y], pruned_attribute_keys[y]) * ~pair(CT['C_i_prime'][y], UASK[authority]['K_uid_aid_prime']) * ~pair(GPP['g'], CT['D_i_prime'][y]))**(coefficients[x] * n_a)) divisor *= temp_divisor Token = dividend / divisor return (Token, CT['C']) def abenc_decrypt(self, CT, TK, user_keys): """ Final decryption algorithm to reveal original message. To be run by the user :param CT: Original component of ciphertext that contains the encrypted message :param TK: Token generated during partial decryption of ciphertext :param user_keys: User global keys :return: Decrypted message """ message = CT / (TK**user_keys[1]) return message def abenc_ukeygen(self, GPP, authority, attribute, user_object): """ Generate update keys used in the revocation process for users and the cloud service provider. This will be run by the Attribute Authority. :param GPP: Global Public Parameters :param authority: Attribute Authority :param attribute: Attribute to be updated :param user_object: User :return: User attribute update keys and ciphertext update keys """ ASK, _, authAttrs = authority # attribute version key to be updated old_version_key = authAttrs[attribute]['VK'] # set new version key to old value new_version_key = old_version_key # ensure that new version key is different from original version key while old_version_key == new_version_key: new_version_key = self.group.random() # update version key of the attribute in the dictionary authAttrs[attribute]['VK'] = new_version_key u_uid = user_object['u_uid'] # create update key for users i.e to update the attribute involved KUK = GPP['H'](attribute)**(ASK['beta_aid'] * (new_version_key - old_version_key) * (u_uid + ASK['gamma_aid'])) # create update key for ciphertexts encrypted with attribute involved CUK = (new_version_key / old_version_key, (old_version_key - new_version_key) / (old_version_key * ASK['gamma_aid'])) # update the public parameters of the attribute involvedauthAttrs[attribute]['PK'][0] = authAttrs[attribute]['PK'][0] ** CUK[0] authAttrs[attribute]['PK'][1] = authAttrs[attribute]['PK'][1]**CUK[0] return { 'KUK': KUK, 'CUK': CUK, } def abenc_skupdate(self, USK, attribute, KUK): """ Updates the attribute secret key for the specific attribute. This is executed by a non-revoked user. :param USK: User secret key :param attribute: Attribute whose secret key is to be updated :param KUK: Update key for users :return: NA """ # update the secret key component of the affected attribute # print(USK) USK['AK_uid_aid'][attribute] = USK['AK_uid_aid'][attribute] * KUK def abenc_ctupdate(self, GPP, CT, attribute, CUK): """ Updates the ciphertexts that contain the specific attribute (revoked attribute). This is executed by the cloud service provider. :param GPP: Global Public Parameters :param CT: The affected ciphertext :param attribute: Attribute that is affected by the revocation process :param CUK: The Ciphertext Update Key :return: NA """ # update the corresponding components of the ciphertext that are related to the affected attribute CT['C_i'][attribute] = CT['C_i'][attribute] * ( CT['D_i_prime'][attribute]**CUK[1]) CT['D_i_prime'][attribute] = CT['D_i_prime'][attribute]**CUK[0]