def encrypt_blocks(datablocks, debug): blockid_to_chunk = {} for V, block in enumerate( [datablocks[N] for N in range(0, len(datablocks))]): key = "%08x%08x" % (block.fileno, block.N) blockid_to_chunk[key] = V output = [] phys_offs = 0 for P, block in enumerate(datablocks): next_chunkid = blockid_to_chunk.get( "%08x%08x" % (block[0], block[1] + 1), 0xFFFFFFFF) nonce, encrypted_data = block_encrypt(block, nextid=next_chunkid) assert len(encrypted_data) == MAX_OUTER_LEN output.append(encrypted_data) if debug: key = sha512(block.secret, encoder=RawEncoder) box = SecretBox(key[:SecretBox.KEY_SIZE]) data = box.decrypt(encrypted_data, nonce) assert len(data) == FRAME_LEN eprint("%4X %8X %s %4X %s %s %8X %s" % (P, phys_offs, hexlify(encrypted_data[:4]), block.N, sha512(block.secret)[:8], hexlify( nonce[:4]), block.offset, sha512(data)[:8])) phys_offs += len(encrypted_data) return output
def generate_blocks(filenames, debug): appendix, datablocks = files_to_blocks(filenames) if not appendix: eprint("Error: start confict") sys.exit(1) for X in range(1, 1000): shuffle(datablocks) datablocks = reposition_starts(appendix, datablocks) verify_start_blocks(appendix, datablocks) if debug: for filename in filenames: key = sha512(filename, encoder=RawEncoder) eprint("%4X %s %02X %s %s" % (calc_start_block(filename, len(datablocks)), sha512(filename)[:8], len(key) + 8, hexlify(calc_nonce(key, 0, 0)[:4]), filename)) eprint("") return datablocks
def generate_key(self, password): self.pri = PrivateKey(scrypt(sha512(password,encoder = RawEncoder), self.salt, N = 1<<17, r = 8, p = 1, buflen = 32), encoder = RawEncoder) pub = PublicID() pub.salt = self.salt pub.key = self.pri.public_key.encode() self.pub = pub
def pingRes(s, ping_req): ping_res = nstp_v3_pb2.PingResponse() if ping_req.hash_algorithm == nstp_v3_pb2.HashAlgorithm.IDENTITY: ping_res.hash = ping_req.data elif ping_req.hash_algorithm == nstp_v3_pb2.HashAlgorithm.SHA256: ping_res.hash = hash.sha256(ping_req.data) elif ping_req.hash_algorithm == nstp_v3_pb2.HashAlgorithm.SHA512: ping_res.hash = hash.sha512(ping_req.data) else: return decr_res = nstp_v3_pb2.DecryptedMessage() decr_res.ping_response.CopyFrom(ping_res) EncryptAndSend(s, decr_res)
def genera_kenviar_krecibir(self, secreto, alice): """ Genera el par de llaves kenviar y krecibir a partir de un secerto Asigna los valores correspondientes al objeto :param alice: True si eres alice, false e.o.c :param secreto: El secreto en comun entre A y B para cifrar los mensajes :return: una lista con kenviar y krecibir en este orden """ hash_val = sha512(secreto, encoder=RawEncoder) if alice: self.kenv = hash_val[:32] self.krecib = hash_val[-32:] else: self.kenv = hash_val[-32:] self.krecib = hash_val[:32] return hash_val[:32], hash_val[-32:]
def password(self, password: str): if password and password != '': self._password_hash = self._base64( hash.sha512(str.encode(password), RawEncoder))
def calc_start_block(secret, nblocks): return unpack('<I', sha512(secret, encoder=RawEncoder)[:4])[0] % nblocks
def block_encrypt(block, nextid): key = sha512(block.secret, encoder=RawEncoder) nonce = calc_nonce(key, block.N, block.offset) box = SecretBox(key[:SecretBox.KEY_SIZE]) data_padded = pack("<I", nextid) + pad_inner(block.data) return nonce, box.encrypt(data_padded, nonce).ciphertext
def calc_nonce(key, innerno, offset): raw_nonce = key + pack("<I", innerno) + pack("<I", offset) return sha512(raw_nonce, encoder=RawEncoder)[:SecretBox.NONCE_SIZE]
def sha512(self): return sha512(pickle.dumps(self))