| 1 |
Salsa20 |
| 2 |
======== |
| 3 |
|
| 4 |
`Salsa20`_ is a stream cipher designed by Daniel J. Bernstein. |
| 5 |
The secret key is by preference 256 bits long, but it can also |
| 6 |
work with 128 bit keys. |
| 7 |
|
| 8 |
This is an example of how ``Salsa20`` can encrypt data: |
| 9 |
|
| 10 |
>>> from Crypto.Cipher import Salsa20 |
| 11 |
>>> |
| 12 |
>>> plaintext = b'Attack at dawn' |
| 13 |
>>> secret = b'*Thirty-two byte (256 bits) key*' |
| 14 |
>>> cipher = Salsa20.new(key=secret) |
| 15 |
>>> msg = cipher.nonce + cipher.encrypt(plaintext) |
| 16 |
|
| 17 |
And this is how you would decrypt it: |
| 18 |
|
| 19 |
>>> from Crypto.Cipher import Salsa20 |
| 20 |
>>> |
| 21 |
>>> secret = b'*Thirty-two byte (256 bits) key*' |
| 22 |
>>> msg_nonce = msg[:8] |
| 23 |
>>> ciphertext = msg[8:] |
| 24 |
>>> cipher = Salsa20.new(key=secret, nonce=msg_nonce) |
| 25 |
>>> plaintext = cipher.decrypt(ciphertext) |
| 26 |
|
| 27 |
.. warning:: |
| 28 |
|
| 29 |
``Salsa20`` does not guarantee authenticity of the data you decrypt! |
| 30 |
In other words, an attacker may manipulate the data in transit. |
| 31 |
In order to prevent that, you must also use a *Message Authentication |
| 32 |
Code* (such as :doc:`HMAC <../hash/hmac>`) to authenticate the ciphertext |
| 33 |
(*encrypt-then-mac*). |
| 34 |
|
| 35 |
.. _Salsa20: http://cr.yp.to/snuffle/spec.pdf |
| 36 |
|
| 37 |
.. automodule:: Crypto.Cipher.Salsa20 |
| 38 |
:members: |