def test_crypto_secretstream_xchacha20poly1305_rekey(self): if not pysodium.sodium_version_check(1, 0, 15): return key = pysodium.crypto_secretstream_xchacha20poly1305_keygen() state, header = pysodium.crypto_secretstream_xchacha20poly1305_init_push( key) # Encrypt two messages with intermediate re-key ciphertext = pysodium.crypto_secretstream_xchacha20poly1305_push( state, b"Correct Horse Battery Staple", None, 0) pysodium.crypto_secretstream_xchacha20poly1305_rekey(state) ciphertext2 = pysodium.crypto_secretstream_xchacha20poly1305_push( state, b"howdy", None, pysodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL) # Verify by decrypting them state2 = pysodium.crypto_secretstream_xchacha20poly1305_init_pull( header, key) msg, tag = pysodium.crypto_secretstream_xchacha20poly1305_pull( state2, ciphertext, None) pysodium.crypto_secretstream_xchacha20poly1305_rekey(state2) msg2, tag2 = pysodium.crypto_secretstream_xchacha20poly1305_pull( state2, ciphertext2, None) self.assertEqual(msg, b"Correct Horse Battery Staple") self.assertEqual(tag, 0) self.assertEqual(msg2, b"howdy") self.assertEqual( tag2, pysodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL)
def test_crypto_secretstream_xchacha20poly1305_pull_multiple(self): if not pysodium.sodium_version_check(1, 0, 15): return key = pysodium.crypto_secretstream_xchacha20poly1305_keygen() state, header = pysodium.crypto_secretstream_xchacha20poly1305_init_push( key) ciphertext = pysodium.crypto_secretstream_xchacha20poly1305_push( state, b"Correct Horse Battery Staple", None, 0) ciphertext2 = pysodium.crypto_secretstream_xchacha20poly1305_push( state, b"howdy", None, pysodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL) # Verify decryption state2 = pysodium.crypto_secretstream_xchacha20poly1305_init_pull( header, key) msg, tag = pysodium.crypto_secretstream_xchacha20poly1305_pull( state2, ciphertext, None) msg2, tag2 = pysodium.crypto_secretstream_xchacha20poly1305_pull( state2, ciphertext2, None) self.assertEqual(msg, b"Correct Horse Battery Staple") self.assertEqual(tag, 0) self.assertEqual(msg2, b"howdy") self.assertEqual( tag2, pysodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL)
def next_chunk(self): if self.enable: if self.chunk_id == 0: chunk = self.child.next_chunk( pysodium.crypto_secretstream_xchacha20poly1305_ABYTES + pysodium.crypto_secretstream_xchacha20poly1305_HEADERBYTES) if not isinstance(chunk, bytes): raise TypeError('Data must be a byte string') header = chunk[:pysodium. crypto_secretstream_xchacha20poly1305_HEADERBYTES] chunk = chunk[ pysodium. crypto_secretstream_xchacha20poly1305_HEADERBYTES:] self.state = pysodium.crypto_secretstream_xchacha20poly1305_init_pull( header, self.crypt_key) else: chunk = self.child.next_chunk( pysodium.crypto_secretstream_xchacha20poly1305_ABYTES) if chunk is not None and not isinstance(chunk, bytes): raise TypeError('Data must be a byte string') if chunk is None: return None msg, self.tag = pysodium.crypto_secretstream_xchacha20poly1305_pull( self.state, chunk, self.pipeline_header) self.chunk_id += 1 return msg else: return self.child.next_chunk()
def test_crypto_secretstream_xchacha20poly1305_pull_multiple(self): if not pysodium.sodium_version_check(1, 0, 15): return key = pysodium.crypto_secretstream_xchacha20poly1305_keygen() state, header = pysodium.crypto_secretstream_xchacha20poly1305_init_push(key) ciphertext = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"Correct Horse Battery Staple", None, 0) ciphertext2 = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"howdy", None, pysodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL) # Verify decryption state2 = pysodium.crypto_secretstream_xchacha20poly1305_init_pull(header, key) msg, tag = pysodium.crypto_secretstream_xchacha20poly1305_pull(state2, ciphertext, None) msg2, tag2 = pysodium.crypto_secretstream_xchacha20poly1305_pull(state2, ciphertext2, None) self.assertEqual(msg, b"Correct Horse Battery Staple") self.assertEqual(tag, 0) self.assertEqual(msg2, b"howdy") self.assertEqual(tag2, pysodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL)
def test_crypto_secretstream_xchacha20poly1305_pull(self): if not pysodium.sodium_version_check(1, 0, 15): return key = pysodium.crypto_secretstream_xchacha20poly1305_keygen() state, header = pysodium.crypto_secretstream_xchacha20poly1305_init_push(key) ciphertext = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"howdy", None, pysodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL) state2 = pysodium.crypto_secretstream_xchacha20poly1305_init_pull(header, key) msg, tag = pysodium.crypto_secretstream_xchacha20poly1305_pull(state2, ciphertext, None) self.assertEqual(msg, b"howdy") self.assertEqual(tag, pysodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL)
def test_crypto_secretstream_xchacha20poly1305_rekey(self): if not pysodium.sodium_version_check(1, 0, 15): return key = pysodium.crypto_secretstream_xchacha20poly1305_keygen() state, header = pysodium.crypto_secretstream_xchacha20poly1305_init_push(key) # Encrypt two messages with intermediate re-key ciphertext = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"Correct Horse Battery Staple", None, 0) pysodium.crypto_secretstream_xchacha20poly1305_rekey(state) ciphertext2 = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"howdy", None, pysodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL) # Verify by decrypting them state2 = pysodium.crypto_secretstream_xchacha20poly1305_init_pull(header, key) msg, tag = pysodium.crypto_secretstream_xchacha20poly1305_pull(state2, ciphertext, None) pysodium.crypto_secretstream_xchacha20poly1305_rekey(state2) msg2, tag2 = pysodium.crypto_secretstream_xchacha20poly1305_pull(state2, ciphertext2, None) self.assertEqual(msg, b"Correct Horse Battery Staple") self.assertEqual(tag, 0) self.assertEqual(msg2, b"howdy") self.assertEqual(tag2, pysodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL)
def test_crypto_secretstream_xchacha20poly1305_missing_rekey(self): if not pysodium.sodium_version_check(1, 0, 15): return key = pysodium.crypto_secretstream_xchacha20poly1305_keygen() state, header = pysodium.crypto_secretstream_xchacha20poly1305_init_push(key) # Encrypt two messages with intermediate re-key ciphertext = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"Correct Horse Battery Staple", None, 0) pysodium.crypto_secretstream_xchacha20poly1305_rekey(state) ciphertext2 = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"howdy", None, pysodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL) state2 = pysodium.crypto_secretstream_xchacha20poly1305_init_pull(header, key) msg, tag = pysodium.crypto_secretstream_xchacha20poly1305_pull(state2, ciphertext, None) # re-key should be here, so following call should fail self.assertRaises(ValueError, pysodium.crypto_secretstream_xchacha20poly1305_pull, state2, ciphertext2, None)
def decrypt(child, meta, config): data, meta2 = child(meta, config) if not isinstance(data, bytes): raise TypeError('Data must be a byte string') pl_format = rrbackup.pipeline.parse_pipeline_format(meta2['header']) if 'encrypt' in pl_format['format']: crypt_key = config['crypto']['stream_crypt_key'] ad_data = meta2['header'] header = data[:pysodium. crypto_secretstream_xchacha20poly1305_HEADERBYTES] chunk = data[pysodium. crypto_secretstream_xchacha20poly1305_HEADERBYTES:] state = pysodium.crypto_secretstream_xchacha20poly1305_init_pull( header, crypt_key) data = pysodium.crypto_secretstream_xchacha20poly1305_pull( state, chunk, ad_data)[0] return data, meta2