blob: 9b5fc171b582d6832ae2d187c097aa6145df93f6 [file] [log] [blame]
Sybren A. Stüveldbea2132011-08-03 13:31:57 +02001.. _usage:
2
Sybren A. Stüvelaa28c042011-07-30 23:48:00 +02003Usage
4==================================================
5
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +02006This section describes the usage of the Python-RSA module.
Sybren A. Stüvelaa28c042011-07-30 23:48:00 +02007
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +02008Before you can use RSA you need keys. You will receive a private key
9and a public key.
10
Sybren A. Stüveldbea2132011-08-03 13:31:57 +020011.. important::
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +020012
13 The private key is called *private* for a reason. Never share this
14 key with anyone.
15
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020016The public key is used for encypting a message such that it can only
17be read by the owner of the private key. As such it's also referred to
18as the *encryption key*. Decrypting a message can only be done using
19the private key, hence it's also called the *decryption key*.
20
21The private key is used for signing a message. With this signature and
22the public key, the receiver can verifying that a message was signed
23by the owner of the private key, and that the message was not modified
24after signing.
25
26Generating keys
27--------------------------------------------------
28
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +020029You can use the :py:func:`rsa.newkeys` function to create a keypair:
30
31 >>> (pubkey, privkey) = rsa.newkeys(512)
32
33Alternatively you can use :py:meth:`rsa.PrivateKey.load_pkcs1` and
34:py:meth:`rsa.PublicKey.load_pkcs1` to load keys from a file:
35
36 >>> with open('private.pem') as privatefile:
37 ... keydata = privatefile.read()
38 >>> pubkey = rsa.PrivateKey.load_pkcs1(keydata)
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020039
40Generating a keypair may take a long time, depending on the number of
41bits required. The number of bits determines the cryptographic
42strength of the key, as well as the size of the message you can
43encrypt. If you don't mind having a slightly smaller key than you
44requested, you can pass ``accurate=False`` to speed up the key
45generation process.
46
47These are some timings from my netbook (Linux 2.6, 1.6 GHz Intel Atom
48N270 CPU, 2 GB RAM):
49
50+----------------+------------------+
51| Keysize (bits) | Time to generate |
52+================+==================+
53| 32 | 0.01 sec. |
54+----------------+------------------+
55| 64 | 0.03 sec. |
56+----------------+------------------+
57| 96 | 0.04 sec. |
58+----------------+------------------+
59| 128 | 0.08 sec. |
60+----------------+------------------+
61| 256 | 0.27 sec. |
62+----------------+------------------+
63| 384 | 0.93 sec. |
64+----------------+------------------+
65| 512 | 1.21 sec. |
66+----------------+------------------+
67| 1024 | 7.93 sec. |
68+----------------+------------------+
69| 2048 | 132.97 sec. |
70+----------------+------------------+
71
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +020072
73Encryption and decryption
74--------------------------------------------------
75
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020076To encrypt or decrypt a message, use :py:func:`rsa.encrypt` resp.
77:py:func:`rsa.decrypt`. Let's say that Alice wants to send a message
78that only Bob can read.
79
80#. Bob generates a keypair, and gives the public key to Alice. This is
81 done such that Alice knows for sure that the key is really Bob's
82 (for example by handing over a USB stick that contains the key).
83
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +020084 >>> (bob_pub, bob_priv) = rsa.newkeys(512)
85
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020086#. Alice writes a message
87
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +020088 >>> message = 'hello Bob!'
89
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020090#. Alice encrypts the message using Bob's public key, and sends the
91 encrypted message.
92
Sybren A. Stüveldb348252011-07-31 19:22:47 +020093 >>> crypto = rsa.encrypt(message, bob_pub)
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +020094
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020095#. Bob receives the message, and decrypts it with his private key.
96
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +020097 >>> message = rsa.decrypt(crypto, bob_priv)
98 >>> print message
99 hello Bob!
100
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200101Since Bob kept his private key *private*, Alice can be sure that he is
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200102the only one who can read the message. Bob does *not* know for sure
103that it was Alice that sent the message, since she didn't sign it.
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200104
105
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200106RSA can only encrypt messages that are smaller than the key. A couple
107of bytes are lost on random padding, and the rest is available for the
108message itself. For example, a 512-bit key can encode a 53-byte
109message (512 bit = 64 bytes, 11 bytes are used for random padding and
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200110other stuff). See :ref:`bigfiles` for information on how to work with
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200111larger files.
112
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200113Altering the encrypted information will *likely* cause a
114:py:class:`rsa.pkcs1.DecryptionError`. If you want to be *sure*, use
115:py:func:`rsa.sign`.
116
117 >>> crypto = encrypt('hello', pub_key)
118 >>> crypto = 'X' + crypto[1:] # change the first byte
119 >>> decrypt(crypto, priv_key)
120 Traceback (most recent call last):
121 ...
122 rsa.pkcs1.DecryptionError: Decryption failed
123
124
125.. warning::
126
127 Never display the stack trace of a
128 :py:class:`rsa.pkcs1.DecryptionError` exception. It shows where
129 in the code the exception occurred, and thus leaks information
130 about the key. It’s only a tiny bit of information, but every bit
131 makes cracking the keys easier.
132
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200133Low-level operations
134++++++++++++++++++++++++++++++
135
136The core RSA algorithm operates on large integers. These operations
137are considered low-level and are supported by the
138:py:func:`rsa.core.encrypt_int` and :py:func:`rsa.core.decrypt_int`
139functions.
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +0200140
141Signing and verification
142--------------------------------------------------
143
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200144You can create a detached signature for a message using the
145:py:func:`rsa.sign` function:
146
147 >>> (pubkey, privkey) = rsa.newkeys(512)
148 >>> message = 'Go left at the blue tree'
149 >>> signature = rsa.sign(message, privkey, 'SHA-1')
150
151This hashes the message using SHA-1. Other hash methods are also
152possible, check the :py:func:`rsa.sign` function documentation for
153details. The hash is then signed with the private key.
154
155In order to verify the signature, use the :py:func:`rsa.verify`
156function.
157
158 >>> message = 'Go left at the blue tree'
159 >>> rsa.verify(message, signature, pubkey)
160
161Modify the message, and the signature is no longer valid and a
162:py:class:`rsa.pkcs1.VerificationError` is thrown:
163
164 >>> message = 'Go right at the blue tree'
165 >>> rsa.verify(message, signature, pubkey)
166 Traceback (most recent call last):
167 File "<stdin>", line 1, in <module>
168 File "/home/sybren/workspace/python-rsa/rsa/pkcs1.py", line 289, in verify
169 raise VerificationError('Verification failed')
170 rsa.pkcs1.VerificationError: Verification failed
171
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200172.. warning::
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200173
174 Never display the stack trace of a
175 :py:class:`rsa.pkcs1.VerificationError` exception. It shows where
176 in the code the exception occurred, and thus leaks information
177 about the key. It's only a tiny bit of information, but every bit
178 makes cracking the keys easier.
179
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200180Instead of a message you can also call :py:func:`rsa.sign` and
181:py:func:`rsa.verify` with a :py:class:`file`-like object. If the
182message object has a ``read(int)`` method it is assumed to be a file.
183In that case the file is hashed in 1024-byte blocks at the time.
184
185 >>> with open('somefile', 'rb') as msgfile:
186 ... signature = rsa.sign(msgfile, privkey, 'SHA-1')
187
188 >>> with open('somefile', 'rb') as msgfile:
189 ... rsa.verify(msgfile, signature, pubkey)
190
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +0200191
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200192.. _bigfiles:
193
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +0200194Working with big files
195--------------------------------------------------
196
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200197RSA can only encrypt messages that are smaller than the key. A couple
198of bytes are lost on random padding, and the rest is available for the
199message itself. For example, a 512-bit key can encode a 53-byte
200message (512 bit = 64 bytes, 11 bytes are used for random padding and
201other stuff).
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +0200202
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200203How it usually works
204++++++++++++++++++++++++++++++++++++++++
205
206The most common way to use RSA with larger files uses a block cypher
207like AES or DES3 to encrypt the file with a random key, then encrypt
208the random key with RSA. You would send the encrypted file along with
209the encrypted key to the recipient. The complete flow is:
210
211#. Generate a random key
212
213 >>> import rsa.randnum
214 >>> aes_key = rsa.randnum.read_random_bits(128)
215
216#. Use that key to encrypt the file with AES.
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200217#. :py:func:`Encrypt <rsa.encrypt>` the AES key with RSA
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200218
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200219 >>> encrypted_aes_key = rsa.encrypt(aes_key, public_rsa_key)
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200220
221#. Send the encrypted file together with ``encrypted_aes_key``
222#. The recipient now reverses this process to obtain the encrypted
223 file.
224
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200225.. note::
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200226
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200227 The Python-RSA module does not contain functionality to do the AES
228 encryption for you.
229
230Only using Python-RSA: the VARBLOCK format
231+++++++++++++++++++++++++++++++++++++++++++
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200232
233As far as we know, there is no pure-Python AES encryption. Previous
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200234versions of Python-RSA included functionality to encrypt large files
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200235with just RSA, and so does this version. The format has been improved,
236though.
237
238Encrypting works as follows: the input file is split into blocks that
239are just large enough to encrypt with your RSA key. Every block is
240then encrypted using RSA, and the encrypted blocks are assembled into
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200241the output file. This file format is called the :ref:`VARBLOCK
242<VARBLOCK>` format.
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200243
244Decrypting works in reverse. The encrypted file is separated into
245encrypted blocks. Those are decrypted, and assembled into the original
246file.
247
248.. note::
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200249
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200250 The file will get larger after encryption, as each encrypted block
251 has 8 bytes of random padding and 3 more bytes of overhead.
252
253Since these encryption/decryption functions are potentially called on
254very large files, they use another approach. Where the regular
255functions store the message in memory in its entirety, these functions
256work on one block at the time. As a result, you should call them with
257:py:class:`file`-like objects as the parameters.
258
259Before using we of course need a keypair:
260
261>>> import rsa
262>>> (pub_key, priv_key) = rsa.newkeys(512)
263
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200264Encryption works on file handles using the
265:py:func:`rsa.bigfile.encrypt_bigfile` function:
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200266
267>>> from rsa.bigfile import *
268>>> with open('inputfile', 'rb') as infile, open('outputfile', 'wb') as outfile:
269... encrypt_bigfile(infile, outfile, pub_key)
270
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200271As does decryption using the :py:func:`rsa.bigfile.decrypt_bigfile`
272function:
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200273
274>>> from rsa.bigfile import *
275>>> with open('inputfile', 'rb') as infile, open('outputfile', 'wb') as outfile:
276... decrypt_bigfile(infile, outfile, priv_key)
277
278.. note::
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200279
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200280 :py:func:`rsa.sign` and :py:func:`rsa.verify` work on arbitrarily
281 long files, so they do not have a "bigfile" equivalent.
Sybren A. Stüvelaa28c042011-07-30 23:48:00 +0200282
Sybren A. Stüvelc1c455d2011-08-01 23:04:30 +0200283