def MsgTestAESCBCSeperate(self,msg): groupObj = PairingGroup('SS512') ran = groupObj.random(GT) a = AuthenticatedCryptoAbstraction(sha1(ran)) ct = a.encrypt(msg) b = AuthenticatedCryptoAbstraction(sha1(ran)) dmsg = b.decrypt(ct); assert msg == dmsg , 'o: =>%s\nm: =>%s' % (msg, dmsg)
def MsgTestAESCBCSeperate(self, msg): groupObj = PairingGroup('SS512') ran = groupObj.random(GT) a = AuthenticatedCryptoAbstraction(sha1(ran)) ct = a.encrypt(msg) b = AuthenticatedCryptoAbstraction(sha1(ran)) dmsg = b.decrypt(ct) assert msg == dmsg, 'o: =>%s\nm: =>%s' % (msg, dmsg)
def encrypt(self, pk, M, object): key = self.group.random(GT) c1 = abenc.encrypt(pk, key, object) # instantiate a symmetric enc scheme from this key cipher = AuthenticatedCryptoAbstraction(sha1(key)) c2 = cipher.encrypt(M) return { 'c1':c1, 'c2':c2 }
def decrypt(self, pk, sk, ct): c1, c2 = ct['c1'], ct['c2'] key = abenc.decrypt(pk, sk, c1) print type(key), key cipher = AuthenticatedCryptoAbstraction(sha1(key)) print cipher return cipher.decrypt(c2)
def testTamperMac(self): key = sha1(PairingGroup('SS512').random(GT)) m = MessageAuthenticator(key) a = m.mac('hello world') m1 = MessageAuthenticator(key) a["digest"]= "tampered" assert not m1.verify(a), "expected message to verify";
def testTamperMac(self): key = sha1(PairingGroup('SS512').random(GT)) m = MessageAuthenticator(key) a = m.mac('hello world') m1 = MessageAuthenticator(key) a["digest"] = "tampered" assert not m1.verify(a), "expected message to verify"
def decrypt(self, sKey, serializedCiphertext): ''' Decrypt the provided ciphertext sing the secret key. Decryption is only successful if the policy embedded in the secret key matches the ciphertext access policy. NOTE: there is a bug in Charm (?) where a policy of a single value causes an error to be thrown: unsupported right operand types: int, bytes, str Sample code that throws the error: enc2 = EncryptionModule() policy = '(three)' attrs = ['ONE', 'TWO', 'THREE'] msg = "Hello world!" ct2 = enc2.encrypt(msg, policy) sk2 = enc2.generateUserKey(attrs) enc2.decrypt(sk2, ct2) ''' ciphertext = bytesToObject(serializedCiphertext, self.groupObj) c1, c2 = ciphertext['c1'], ciphertext['c2'] success = True try: key = self.cpabe.decrypt(self.public, sKey, c1) if (key == False): success = False except: success = False # Try to perform the decryption if we were able to recover the key plaintext = None if (success == True): cipher = AuthenticatedCryptoAbstraction(sha1(key)) plaintext = cipher.decrypt(c2) return (success, plaintext)
def callKeyGen(scheme, pp, mk, N, tp): if (tp=='lw08'): return scheme.keygen(pp,mk,p_complex(N)) elif tp == 'AESCBC': return sha1(mk) else: return scheme.keygen(pp,mk,p_attr(N))
def encrypt(self, pk, M, object): key = self.group.random(GT) c1 = abenc.encrypt(pk, key, object) # instantiate a symmetric enc scheme from this key cipher = AuthenticatedCryptoAbstraction(sha1(key)) c2 = cipher.encrypt(M) return {'c1': c1, 'c2': c2}
def testTamperAlg(self): key = sha1(PairingGroup('SS512').random(GT)) m = MessageAuthenticator(key) a = m.mac('hello world') m1 = MessageAuthenticator(key) m1._algorithm = "alg" # bypassing the algorithm check to verify the mac is over the alg + data a["alg"]= "alg" assert not m1.verify(a), "expected message to verify";
def testTamperAlg(self): key = sha1(PairingGroup('SS512').random(GT)) m = MessageAuthenticator(key) a = m.mac('hello world') m1 = MessageAuthenticator(key) m1._algorithm = "alg" # bypassing the algorithm check to verify the mac is over the alg + data a["alg"] = "alg" assert not m1.verify(a), "expected message to verify"
def encrypt(self, pk, gp, M, policy_str): if type(M) != bytes and type(policy_str) != str: raise "message and policy not right type!" key = group.random(GT) c1 = abencma.encrypt(pk, gp, key, policy_str) # instantiate a symmetric enc scheme from this key cipher = AuthenticatedCryptoAbstraction(sha1(key)) c2 = cipher.encrypt(M) return { 'c1':c1, 'c2':c2 }
def encrypt_jet(self, params, ID, M): if type(M) != bytes: raise "Type ERROR: Message should be bytes." key = group.random(GT) c1 = self.encrypt(params, ID, key) # instantiate a symmetric enc scheme from this key cipher = AuthenticatedCryptoAbstraction(sha1(key)) c2 = cipher.encrypt(M) return {'c1': c1, 'c2': c2}
def keyenc(self, params, ID, msg): s = group.random() A = sha1(params['v']**s) # session key B = params['Y']**s C = (params['X']**s) * (params['g']**(s * ID)) # use prf here? ciph = {'B': B, 'C': C} return (A, ciph) # user must destroy A since it protects the msg
def encrypt(self, pk, ID, M): if type(M) != bytes: raise "message not right type!" key = group.random(GT) c1 = ibenc.encrypt(pk, ID, key) # instantiate a symmetric enc scheme from this key cipher = AuthenticatedCryptoAbstraction(sha1(key)) c2 = cipher.encrypt(M) return { 'c1':c1, 'c2':c2 }
def encrypt(self, pk, ID, M): if type(M) != bytes: raise "message not right type!" key = group.random(GT) c1 = ibenc.encrypt(pk, ID, key) # instantiate a symmetric enc scheme from this key cipher = AuthenticatedCryptoAbstraction(sha1(key)) c2 = cipher.encrypt(M) return {'c1': c1, 'c2': c2}
def keyenc(self, params, ID, msg): s = group.random() A = sha1(params['v'] ** s) # session key B = params['Y'] ** s C = (params['X'] ** s) * (params['g'] ** (s * ID)) # use prf here? ciph = { 'B': B, 'C': C } return (A, ciph) # user must destroy A since it protects the msg
def encrypt(self, pk, gp, M, policy_str): if type(M) != bytes and type(policy_str) != str: raise "message and policy not right type!" key = group.random(GT) c1 = abencma.encrypt(pk, gp, key, policy_str) # instantiate a symmetric enc scheme from this key cipher = AuthenticatedCryptoAbstraction(sha1(key)) c2 = cipher.encrypt(M) return {'c1': c1, 'c2': c2}
def encrypt(self, plaintext, policy): #return self.cpabe.encrypt(self.public, plaintext, policy) key = self.groupObj.random(GT) c1 = self.cpabe.encrypt(self.public, key, policy) # instantiate a symmetric enc scheme from this key cipher = AuthenticatedCryptoAbstraction(sha1(key)) c2 = cipher.encrypt(plaintext) return { 'c1':c1, 'c2':c2 }
def encrypt(self, plaintext, policy): ''' Encrypt a block of plaintext using the provided polcy structure. The ciphertext is stored as a dictionary, for now. ''' key = self.groupObj.random(GT) c1 = self.cpabe.encrypt(self.public, key, policy) # Instantiate a symmetric enc scheme from this key cipher = AuthenticatedCryptoAbstraction(sha1(key)) c2 = cipher.encrypt(plaintext) return objectToBytes({ 'c1':c1, 'c2':c2 }, self.groupObj)
def generate_param(n): storage = {} global kac kac = KAC() storage['n'] = n storage['param'] = kac.setup(n) storage['plain'] = [kac.group.random(GT)] storage['cipher'] = [kac.group.random(GT)] storage['e_g1_g2'] = kac.e_g1_g2 storage['key'] = kac.keygen(storage['param']) global AESkey AESkey=[] for i in range(1,n+1): plain = kac.group.random(GT) AESkey.append(SymmetricCryptoAbstraction(sha1(plain))) storage['plain'].append(plain) storage['cipher'].append(kac.encrypt(storage['key']['pk'], i, plain, storage['param'])) print "I have done all parameters!" # outFile = open('GTparam.txt', 'w') # for i in range(1,n+1): # storage['plain'] = [kac.group.random(GT)] # storage['cipher'] = [kac.group.random(GT)] # # with open('GTparam.txt', 'w') as outfile: # # json.dump(data,outfile) # # outfile.write(str(i)+':\n') # outFile.write('\nMy output GT element!\n') # plain = kac.group.random(GT) # storage['plain'].append(plain) # storage['cipher'].append(kac.encrypt(storage['key']['pk'], i, plain, storage['param'])) # data=objectToBytes(storage['plain'], kac.group) # # with open('GTparam.txt', 'w') as outfile: # outFile.write(data) # # outfile.write(storage['plain']) # outFile.close() # p=subprocess.Popen("/home/grace/Desktop/KAC code/abc",stdin=subprocess.PIPE,stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = False) # p.stdin.write(str(n)+'\n') # a = p.stdout.read() # print a # print "sent" # print storage return storage
def decrypt(self, sKey, ciphertext): #return self.cpabe.decrypt(self.public, sKey, ciphertext) c1, c2 = ciphertext['c1'], ciphertext['c2'] success = True # TODO: we need to supress the print statement that comes out of this guy, to avoid unnecessary events try: key = self.cpabe.decrypt(self.public, sKey, c1) if (key == False): success = False except: success = False # Try to perform the encryption if we were able to recover the key plaintext = None if (success == True): cipher = AuthenticatedCryptoAbstraction(sha1(key)) plaintext = cipher.decrypt(c2) return (success, plaintext)
def decrypt(self, sKey, serializedCiphertext): ''' Decrypt the provided ciphertext sing the secret key. Decryption is only successful if the policy embedded in the secret key matches the ciphertext access policy. ''' ciphertext = bytesToObject(serializedCiphertext, PairingGroup('SS512')) c1, c2 = ciphertext['c1'], ciphertext['c2'] success = True try: key = self.cpabe.decrypt(self.public, sKey, c1) if (key == False): success = False except: success = False # Try to perform the decryption if we were able to recover the key plaintext = None if (success == True): cipher = AuthenticatedCryptoAbstraction(sha1(key)) plaintext = cipher.decrypt(c2) return (success, plaintext)
def testSeperateVerify(self): key = sha1(PairingGroup('SS512').random(GT)) m = MessageAuthenticator(key) a = m.mac('hello world') m1 = MessageAuthenticator(key) assert m1.verify(a), "expected message to verify";
# print pk+"\n" # print cipher+"\n" mk = bytesToObject(mk, groupObj) pk = bytesToObject(pk, groupObj) cipher = bytesToObject(cipher, groupObj) sk = cpabe.keygen(pk, mk, asl) print sk try: plaintext = cpabe.decrypt(pk, sk, cipher) except: print "Error" if plaintext != False: a = SymmetricCryptoAbstraction(sha1(plaintext)) with open(fileUrl, "rb") as f: cloudfile = f.read() file = a.decrypt(cloudfile) with open(fileUrl, "wb") as re: re.write(file) else: print "Dcrypto Fail!" del groupObj #os.remove(configUrl)
def keydec(self, pk, dID, CT): A, B, C = CT['A'], CT['B'], CT['C'] v_s = pair(((B**dID['r']) * C), dID['K']) return sha1(v_s)
from charm.toolbox.symcrypto import SymmetricCryptoAbstraction, AuthenticatedCryptoAbstraction, MessageAuthenticator from charm.toolbox.pairinggroup import PairingGroup, GT from charm.core.math.pairing import hashPair as sha1 debug = 0 groupObj = PairingGroup('SS512') pk = groupObj.random(GT) if debug: print pk a = SymmetricCryptoAbstraction(sha1(pk)) try: f = open('/Users/cirnotxm/down/charm.dmg', 'rb') ff = f.read() ct = a.encrypt(ff) if debug: print ct ffe = open('/Users/cirnotxm/down/jiami', 'wb') ffe.write(ct) fpk = open('/Users/cirnotxm/down/pk', 'wb') fpk.write(groupObj.serialize(pk)) finally:
from charm.toolbox.pairinggroup import PairingGroup,GT from charm.core.math.pairing import hashPair as sha1 from charm.schemes.abenc.abenc_waters09 import CPabe09 from charm.toolbox.symcrypto import SymmetricCryptoAbstraction,AuthenticatedCryptoAbstraction, MessageAuthenticator groupObj = PairingGroup('SS512') symKey = groupObj.random(GT) a1 = SymmetricCryptoAbstraction(sha1(symKey)) with open('/Users/cirnotxm/down/pk','rb') as f: msg1 = f.read() ct = a1.encrypt(msg1) cpabe = CPabe09(groupObj) (msk, pk) = cpabe.setup() pol = '((ONE or THREE) and (TWO or FOUR))' with open('/Users/cirnotxm/down/msk','wb') as fm: fm.write(str(msk)) with open('/Users/cirnotxm/down/mpk','wb') as fp: fp.write(str(pk)) with open('/Users/cirnotxm/down/info','wb') as fi: fi.write(ct) print('Acces Policy: %s' % pol) cipher = cpabe.encrypt(pk, symKey, pol) print("\nCiphertext...") print cipher
def testSeperateVerify(self): key = sha1(PairingGroup('SS512').random(GT)) m = MessageAuthenticator(key) a = m.mac('hello world') m1 = MessageAuthenticator(key) assert m1.verify(a), "expected message to verify"
# print pk+"\n" # print cipher+"\n" mk = bytesToObject(mk,groupObj) pk = bytesToObject(pk,groupObj) cipher = bytesToObject(cipher,groupObj) sk = cpabe.keygen(pk,mk,asl) print sk try: plaintext = cpabe.decrypt(pk,sk,cipher) except: print "Error" if plaintext!=False : a = SymmetricCryptoAbstraction(sha1(plaintext)) with open(fileUrl,"rb") as f: cloudfile = f.read() file = a.decrypt(cloudfile) with open(fileUrl,"wb") as re: re.write(file) else: print "Dcrypto Fail!" del groupObj #os.remove(configUrl)
def keydec(self, pk, dID, CT): A, B, C = CT['A'], CT['B'], CT['C'] v_s = pair(((B ** dID['r']) * C), dID['K']) return sha1(v_s)
def decrypt_jet(self, params, skid, cid): c1, c2 = cid['c1'], cid['c2'] key = self.decrypt(params, skid, c1) cipher = AuthenticatedCryptoAbstraction(sha1(key)) return cipher.decrypt(c2)
def decrypt(self, pk, ID, ct): c1, c2 = ct['c1'], ct['c2'] key = ibenc.decrypt(pk, ID, c1) cipher = AuthenticatedCryptoAbstraction(sha1(key)) return cipher.decrypt(c2)
from charm.toolbox.symcrypto import SymmetricCryptoAbstraction, AuthenticatedCryptoAbstraction, MessageAuthenticator from charm.toolbox.pairinggroup import PairingGroup, GT from charm.core.math.pairing import hashPair as sha1 debug = 0 groupObj = PairingGroup('SS512') try: f = open('/Users/cirnotxm/down/pk', 'rb') pk = f.read() a = SymmetricCryptoAbstraction(sha1(groupObj.deserialize(pk))) ffe = open('/Users/cirnotxm/down/jiami', 'rb') ct = ffe.read() de = a.decrypt(ct) fpk = open('/Users/cirnotxm/down/jiemi.dmg', 'wb') fpk.write(de) finally: ffe.close() f.close() fpk.close()
from charm.core.math.pairing import hashPair as sha1 from charm.schemes.abenc.abenc_waters09 import CPabe09 from charm.toolbox.symcrypto import SymmetricCryptoAbstraction,AuthenticatedCryptoAbstraction, MessageAuthenticator asl= ['ONE','TWO'] groupObj = PairingGroup('SS512') cpabe = CPabe09(groupObj) with open('/Users/cirnotxm/down/info','rb') as f: info = f.read() cpkey = cpabe.keygen(pk,msk,asl) with open('/Users/cirnotxm/down/cipk','r') as f2: ciphertext = f2.read() ciphertext = eval(ciphertext) print ciphertext prig_msg = cpabe.decrypt(pk,cpkey,ciphertext) a2 = SymmetricCryptoAbstraction(sha1(orig_symKey)) plaintext = a2.decrypt(info) print plaintext
def generateSessionKey(encaps): key=sha1(cpabe.decrypt(pk, cpkey, encaps.pop())) for encap in encaps: key = xorString(key,sha1(cpabe.decrypt(pk, cpkey, encap))) return key
def MsgtestAESCBC(self, msg): groupObj = PairingGroup('SS512') a = AuthenticatedCryptoAbstraction(sha1(groupObj.random(GT))) ct = a.encrypt(msg) dmsg = a.decrypt(ct) assert msg == dmsg, 'o: =>%s\nm: =>%s' % (msg, dmsg)
def decrypt(self, ct, sk): c1, c2 = ct['c1'], ct['c2'] key = abenc.decrypt(c1, sk) cipher = AuthenticatedCryptoAbstraction(sha1(key)) return cipher.decrypt(c2)
def MsgtestAESCBC(self,msg): groupObj = PairingGroup('SS512') a = AuthenticatedCryptoAbstraction(sha1(groupObj.random(GT))) ct = a.encrypt(msg) dmsg = a.decrypt(ct); assert msg == dmsg , 'o: =>%s\nm: =>%s' % (msg, dmsg)
def decrypt(self, gp, sk, ct): c1, c2 = ct['c1'], ct['c2'] key = abencma.decrypt(gp, sk, c1) cipher = AuthenticatedCryptoAbstraction(sha1(key)) return cipher.decrypt(c2)
from charm.toolbox.pairinggroup import PairingGroup, GT from charm.core.math.pairing import hashPair as sha1 from charm.schemes.abenc.abenc_waters09 import CPabe09 from charm.toolbox.symcrypto import SymmetricCryptoAbstraction, AuthenticatedCryptoAbstraction, MessageAuthenticator asl = ['ONE', 'TWO'] groupObj = PairingGroup('SS512') cpabe = CPabe09(groupObj) with open('/Users/cirnotxm/down/info', 'rb') as f: info = f.read() cpkey = cpabe.keygen(pk, msk, asl) with open('/Users/cirnotxm/down/cipk', 'r') as f2: ciphertext = f2.read() ciphertext = eval(ciphertext) print ciphertext prig_msg = cpabe.decrypt(pk, cpkey, ciphertext) a2 = SymmetricCryptoAbstraction(sha1(orig_symKey)) plaintext = a2.decrypt(info) print plaintext
def getAESCBC(groupObj): pp = sha1(groupObj.random(G1)) mk = groupObj.random(G1) scheme = AuthenticatedCryptoAbstraction(pp) return (scheme, pp, mk)
def getDEK(self): """selects a new DEK from G1""" return sha1(self.groupObj.random(G1))
from charm.toolbox.symcrypto import SymmetricCryptoAbstraction,AuthenticatedCryptoAbstraction, MessageAuthenticator from charm.toolbox.pairinggroup import PairingGroup,GT from charm.core.math.pairing import hashPair as sha1 debug = 0 groupObj = PairingGroup('SS512') pk = groupObj.random(GT) if debug :print pk a = SymmetricCryptoAbstraction(sha1(pk)) try: f = open('/Users/cirnotxm/down/charm.dmg','rb') ff = f.read() ct = a.encrypt(ff) if debug :print ct ffe = open('/Users/cirnotxm/down/jiami','wb') ffe.write(ct) fpk = open('/Users/cirnotxm/down/pk','wb') fpk.write(groupObj.serialize(pk))