blob: 6e11a35365d41a77890603381926b473f0c07f99 [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
Sybren A. Stüvele7c6e742015-08-31 21:49:42 +020026
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020027Generating keys
28--------------------------------------------------
29
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +020030You can use the :py:func:`rsa.newkeys` function to create a keypair:
31
Sybren A. Stüvele7c6e742015-08-31 21:49:42 +020032 >>> import rsa
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +020033 >>> (pubkey, privkey) = rsa.newkeys(512)
34
35Alternatively you can use :py:meth:`rsa.PrivateKey.load_pkcs1` and
36:py:meth:`rsa.PublicKey.load_pkcs1` to load keys from a file:
37
Sybren A. Stüvele7c6e742015-08-31 21:49:42 +020038 >>> import rsa
Sybren A. Stüvelf68c52a2016-01-18 15:39:50 +010039 >>> with open('private.pem', mode='rb') as privatefile:
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +020040 ... keydata = privatefile.read()
Sybren A. Stüvelf68c52a2016-01-18 15:39:50 +010041 >>> privkey = rsa.PrivateKey.load_pkcs1(keydata)
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020042
Sybren A. Stüvel360d0422011-08-10 12:52:59 +020043
44Time to generate a key
45++++++++++++++++++++++++++++++++++++++++
46
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020047Generating a keypair may take a long time, depending on the number of
48bits required. The number of bits determines the cryptographic
49strength of the key, as well as the size of the message you can
50encrypt. If you don't mind having a slightly smaller key than you
51requested, you can pass ``accurate=False`` to speed up the key
52generation process.
53
Sybren A. Stüvel360d0422011-08-10 12:52:59 +020054Another way to speed up the key generation process is to use multiple
55processes in parallel to speed up the key generation. Use no more than
56the number of processes that your machine can run in parallel; a
57dual-core machine should use ``poolsize=2``; a quad-core
58hyperthreading machine can run two threads on each core, and thus can
59use ``poolsize=8``.
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020060
Sybren A. Stüvel360d0422011-08-10 12:52:59 +020061 >>> (pubkey, privkey) = rsa.newkeys(512, poolsize=8)
62
63These are some average timings from my desktop machine (Linux 2.6,
642.93 GHz quad-core Intel Core i7, 16 GB RAM) using 64-bit CPython 2.7.
65Since key generation is a random process, times may differ even on
66similar hardware. On all tests, we used the default ``accurate=True``.
67
68+----------------+------------------+------------------+
69| Keysize (bits) | single process | eight processes |
70+================+==================+==================+
71| 128 | 0.01 sec. | 0.01 sec. |
72+----------------+------------------+------------------+
73| 256 | 0.03 sec. | 0.02 sec. |
74+----------------+------------------+------------------+
75| 384 | 0.09 sec. | 0.04 sec. |
76+----------------+------------------+------------------+
77| 512 | 0.11 sec. | 0.07 sec. |
78+----------------+------------------+------------------+
79| 1024 | 0.79 sec. | 0.30 sec. |
80+----------------+------------------+------------------+
81| 2048 | 6.55 sec. | 1.60 sec. |
82+----------------+------------------+------------------+
83| 3072 | 23.4 sec. | 7.14 sec. |
84+----------------+------------------+------------------+
85| 4096 | 72.0 sec. | 24.4 sec. |
86+----------------+------------------+------------------+
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020087
Sybren A. Stüvel58fe9462011-08-03 13:56:32 +020088If key generation is too slow for you, you could use OpenSSL to
Sybren A. Stüvel360d0422011-08-10 12:52:59 +020089generate them for you, then load them in your Python code. OpenSSL
90generates a 4096-bit key in 3.5 seconds on the same machine as used
91above. See :ref:`openssl` for more information.
Sybren A. Stüvel58fe9462011-08-03 13:56:32 +020092
93Key size requirements
94--------------------------------------------------
95
96Python-RSA version 3.0 introduced PKCS#1-style random padding. This
97means that 11 bytes (88 bits) of your key are no longer usable for
98encryption, so keys smaller than this are unusable. The larger the
99key, the higher the security.
100
101Creating signatures also requires a key of a certain size, depending
102on the used hash method:
103
104+-------------+-----------------------------------+
105| Hash method | Suggested minimum key size (bits) |
106+=============+===================================+
107| MD5 | 360 |
108+-------------+-----------------------------------+
109| SHA-1 | 368 |
110+-------------+-----------------------------------+
111| SHA-256 | 496 |
112+-------------+-----------------------------------+
113| SHA-384 | 624 |
114+-------------+-----------------------------------+
115| SHA-512 | 752 |
116+-------------+-----------------------------------+
117
118
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +0200119
120Encryption and decryption
121--------------------------------------------------
122
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200123To encrypt or decrypt a message, use :py:func:`rsa.encrypt` resp.
124:py:func:`rsa.decrypt`. Let's say that Alice wants to send a message
125that only Bob can read.
126
127#. Bob generates a keypair, and gives the public key to Alice. This is
128 done such that Alice knows for sure that the key is really Bob's
129 (for example by handing over a USB stick that contains the key).
130
Sybren A. Stüvele7c6e742015-08-31 21:49:42 +0200131 >>> import rsa
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200132 >>> (bob_pub, bob_priv) = rsa.newkeys(512)
133
Sybren A. Stüvele7c6e742015-08-31 21:49:42 +0200134#. Alice writes a message, and encodes it in UTF-8. The RSA module
135 only operates on bytes, and not on strings, so this step is
136 necessary.
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200137
Sybren A. Stüvele7c6e742015-08-31 21:49:42 +0200138 >>> message = 'hello Bob!'.encode('utf8')
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200139
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200140#. Alice encrypts the message using Bob's public key, and sends the
141 encrypted message.
142
Sybren A. Stüvele7c6e742015-08-31 21:49:42 +0200143 >>> import rsa
Sybren A. Stüveldb348252011-07-31 19:22:47 +0200144 >>> crypto = rsa.encrypt(message, bob_pub)
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200145
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200146#. Bob receives the message, and decrypts it with his private key.
147
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200148 >>> message = rsa.decrypt(crypto, bob_priv)
Sybren A. Stüvele7c6e742015-08-31 21:49:42 +0200149 >>> print(message.decode('utf8'))
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200150 hello Bob!
151
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200152Since Bob kept his private key *private*, Alice can be sure that he is
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200153the only one who can read the message. Bob does *not* know for sure
154that it was Alice that sent the message, since she didn't sign it.
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200155
156
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200157RSA can only encrypt messages that are smaller than the key. A couple
158of bytes are lost on random padding, and the rest is available for the
159message itself. For example, a 512-bit key can encode a 53-byte
160message (512 bit = 64 bytes, 11 bytes are used for random padding and
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200161other stuff). See :ref:`bigfiles` for information on how to work with
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200162larger files.
163
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200164Altering the encrypted information will *likely* cause a
165:py:class:`rsa.pkcs1.DecryptionError`. If you want to be *sure*, use
166:py:func:`rsa.sign`.
167
Sybren A. Stüvele7c6e742015-08-31 21:49:42 +0200168 >>> crypto = rsa.encrypt(b'hello', bob_pub)
169 >>> crypto = crypto[:-1] + b'X' # change the last byte
170 >>> rsa.decrypt(crypto, bob_priv)
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200171 Traceback (most recent call last):
172 ...
173 rsa.pkcs1.DecryptionError: Decryption failed
174
175
176.. warning::
177
178 Never display the stack trace of a
179 :py:class:`rsa.pkcs1.DecryptionError` exception. It shows where
180 in the code the exception occurred, and thus leaks information
181 about the key. It’s only a tiny bit of information, but every bit
182 makes cracking the keys easier.
183
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200184Low-level operations
185++++++++++++++++++++++++++++++
186
187The core RSA algorithm operates on large integers. These operations
188are considered low-level and are supported by the
189:py:func:`rsa.core.encrypt_int` and :py:func:`rsa.core.decrypt_int`
190functions.
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +0200191
192Signing and verification
193--------------------------------------------------
194
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200195You can create a detached signature for a message using the
196:py:func:`rsa.sign` function:
197
198 >>> (pubkey, privkey) = rsa.newkeys(512)
199 >>> message = 'Go left at the blue tree'
200 >>> signature = rsa.sign(message, privkey, 'SHA-1')
201
202This hashes the message using SHA-1. Other hash methods are also
203possible, check the :py:func:`rsa.sign` function documentation for
204details. The hash is then signed with the private key.
205
206In order to verify the signature, use the :py:func:`rsa.verify`
Tim Heckman7446f0a2012-10-17 21:09:43 -0400207function. This function returns True if the verification is successful:
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200208
209 >>> message = 'Go left at the blue tree'
210 >>> rsa.verify(message, signature, pubkey)
Tim Heckman7446f0a2012-10-17 21:09:43 -0400211 True
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200212
213Modify the message, and the signature is no longer valid and a
214:py:class:`rsa.pkcs1.VerificationError` is thrown:
215
216 >>> message = 'Go right at the blue tree'
217 >>> rsa.verify(message, signature, pubkey)
218 Traceback (most recent call last):
219 File "<stdin>", line 1, in <module>
220 File "/home/sybren/workspace/python-rsa/rsa/pkcs1.py", line 289, in verify
221 raise VerificationError('Verification failed')
222 rsa.pkcs1.VerificationError: Verification failed
223
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200224.. warning::
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200225
226 Never display the stack trace of a
227 :py:class:`rsa.pkcs1.VerificationError` exception. It shows where
228 in the code the exception occurred, and thus leaks information
229 about the key. It's only a tiny bit of information, but every bit
230 makes cracking the keys easier.
231
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200232Instead of a message you can also call :py:func:`rsa.sign` and
233:py:func:`rsa.verify` with a :py:class:`file`-like object. If the
234message object has a ``read(int)`` method it is assumed to be a file.
235In that case the file is hashed in 1024-byte blocks at the time.
236
237 >>> with open('somefile', 'rb') as msgfile:
238 ... signature = rsa.sign(msgfile, privkey, 'SHA-1')
239
240 >>> with open('somefile', 'rb') as msgfile:
241 ... rsa.verify(msgfile, signature, pubkey)
242
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +0200243
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200244.. _bigfiles:
245
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +0200246Working with big files
247--------------------------------------------------
248
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200249RSA can only encrypt messages that are smaller than the key. A couple
250of bytes are lost on random padding, and the rest is available for the
251message itself. For example, a 512-bit key can encode a 53-byte
252message (512 bit = 64 bytes, 11 bytes are used for random padding and
253other stuff).
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +0200254
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200255How it usually works
256++++++++++++++++++++++++++++++++++++++++
257
258The most common way to use RSA with larger files uses a block cypher
259like AES or DES3 to encrypt the file with a random key, then encrypt
260the random key with RSA. You would send the encrypted file along with
261the encrypted key to the recipient. The complete flow is:
262
263#. Generate a random key
264
265 >>> import rsa.randnum
266 >>> aes_key = rsa.randnum.read_random_bits(128)
267
268#. Use that key to encrypt the file with AES.
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200269#. :py:func:`Encrypt <rsa.encrypt>` the AES key with RSA
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200270
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200271 >>> encrypted_aes_key = rsa.encrypt(aes_key, public_rsa_key)
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200272
273#. Send the encrypted file together with ``encrypted_aes_key``
274#. The recipient now reverses this process to obtain the encrypted
275 file.
276
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200277.. note::
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200278
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200279 The Python-RSA module does not contain functionality to do the AES
280 encryption for you.
281
282Only using Python-RSA: the VARBLOCK format
283+++++++++++++++++++++++++++++++++++++++++++
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200284
Sybren A. Stüvel1681a0b2016-01-22 13:54:52 +0100285.. warning::
286
287 The VARBLOCK format is NOT recommended for general use, has been deprecated since
288 Python-RSA 3.4, and will be removed in a future release. It's vulnerable to a
289 number of attacks:
290
291 1. decrypt/encrypt_bigfile() does not implement `Authenticated encryption`_ nor
292 uses MACs to verify messages before decrypting public key encrypted messages.
293
294 2. decrypt/encrypt_bigfile() does not use hybrid encryption (it uses plain RSA)
295 and has no method for chaining, so block reordering is possible.
296
297 See `issue #19 on Github`_ for more information.
298
299.. _Authenticated encryption: https://en.wikipedia.org/wiki/Authenticated_encryption
300.. _issue #19 on Github: https://github.com/sybrenstuvel/python-rsa/issues/13
301
302
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200303As far as we know, there is no pure-Python AES encryption. Previous
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200304versions of Python-RSA included functionality to encrypt large files
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200305with just RSA, and so does this version. The format has been improved,
306though.
307
308Encrypting works as follows: the input file is split into blocks that
309are just large enough to encrypt with your RSA key. Every block is
310then encrypted using RSA, and the encrypted blocks are assembled into
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200311the output file. This file format is called the :ref:`VARBLOCK
312<VARBLOCK>` format.
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200313
314Decrypting works in reverse. The encrypted file is separated into
315encrypted blocks. Those are decrypted, and assembled into the original
316file.
317
318.. note::
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200319
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200320 The file will get larger after encryption, as each encrypted block
321 has 8 bytes of random padding and 3 more bytes of overhead.
322
323Since these encryption/decryption functions are potentially called on
324very large files, they use another approach. Where the regular
325functions store the message in memory in its entirety, these functions
326work on one block at the time. As a result, you should call them with
327:py:class:`file`-like objects as the parameters.
328
329Before using we of course need a keypair:
330
331>>> import rsa
332>>> (pub_key, priv_key) = rsa.newkeys(512)
333
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200334Encryption works on file handles using the
335:py:func:`rsa.bigfile.encrypt_bigfile` function:
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200336
337>>> from rsa.bigfile import *
338>>> with open('inputfile', 'rb') as infile, open('outputfile', 'wb') as outfile:
339... encrypt_bigfile(infile, outfile, pub_key)
340
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200341As does decryption using the :py:func:`rsa.bigfile.decrypt_bigfile`
342function:
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200343
344>>> from rsa.bigfile import *
345>>> with open('inputfile', 'rb') as infile, open('outputfile', 'wb') as outfile:
346... decrypt_bigfile(infile, outfile, priv_key)
347
348.. note::
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200349
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200350 :py:func:`rsa.sign` and :py:func:`rsa.verify` work on arbitrarily
351 long files, so they do not have a "bigfile" equivalent.
Sybren A. Stüvelaa28c042011-07-30 23:48:00 +0200352
Sybren A. Stüvelc1c455d2011-08-01 23:04:30 +0200353