blob: 611e8688da755ec4de91316814227450caef4ffb [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
Sybren A. Stüvel360d0422011-08-10 12:52:59 +020040
41Time to generate a key
42++++++++++++++++++++++++++++++++++++++++
43
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020044Generating a keypair may take a long time, depending on the number of
45bits required. The number of bits determines the cryptographic
46strength of the key, as well as the size of the message you can
47encrypt. If you don't mind having a slightly smaller key than you
48requested, you can pass ``accurate=False`` to speed up the key
49generation process.
50
Sybren A. Stüvel360d0422011-08-10 12:52:59 +020051Another way to speed up the key generation process is to use multiple
52processes in parallel to speed up the key generation. Use no more than
53the number of processes that your machine can run in parallel; a
54dual-core machine should use ``poolsize=2``; a quad-core
55hyperthreading machine can run two threads on each core, and thus can
56use ``poolsize=8``.
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020057
Sybren A. Stüvel360d0422011-08-10 12:52:59 +020058 >>> (pubkey, privkey) = rsa.newkeys(512, poolsize=8)
59
60These are some average timings from my desktop machine (Linux 2.6,
612.93 GHz quad-core Intel Core i7, 16 GB RAM) using 64-bit CPython 2.7.
62Since key generation is a random process, times may differ even on
63similar hardware. On all tests, we used the default ``accurate=True``.
64
65+----------------+------------------+------------------+
66| Keysize (bits) | single process | eight processes |
67+================+==================+==================+
68| 128 | 0.01 sec. | 0.01 sec. |
69+----------------+------------------+------------------+
70| 256 | 0.03 sec. | 0.02 sec. |
71+----------------+------------------+------------------+
72| 384 | 0.09 sec. | 0.04 sec. |
73+----------------+------------------+------------------+
74| 512 | 0.11 sec. | 0.07 sec. |
75+----------------+------------------+------------------+
76| 1024 | 0.79 sec. | 0.30 sec. |
77+----------------+------------------+------------------+
78| 2048 | 6.55 sec. | 1.60 sec. |
79+----------------+------------------+------------------+
80| 3072 | 23.4 sec. | 7.14 sec. |
81+----------------+------------------+------------------+
82| 4096 | 72.0 sec. | 24.4 sec. |
83+----------------+------------------+------------------+
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020084
Sybren A. Stüvel58fe9462011-08-03 13:56:32 +020085If key generation is too slow for you, you could use OpenSSL to
Sybren A. Stüvel360d0422011-08-10 12:52:59 +020086generate them for you, then load them in your Python code. OpenSSL
87generates a 4096-bit key in 3.5 seconds on the same machine as used
88above. See :ref:`openssl` for more information.
Sybren A. Stüvel58fe9462011-08-03 13:56:32 +020089
90Key size requirements
91--------------------------------------------------
92
93Python-RSA version 3.0 introduced PKCS#1-style random padding. This
94means that 11 bytes (88 bits) of your key are no longer usable for
95encryption, so keys smaller than this are unusable. The larger the
96key, the higher the security.
97
98Creating signatures also requires a key of a certain size, depending
99on the used hash method:
100
101+-------------+-----------------------------------+
102| Hash method | Suggested minimum key size (bits) |
103+=============+===================================+
104| MD5 | 360 |
105+-------------+-----------------------------------+
106| SHA-1 | 368 |
107+-------------+-----------------------------------+
108| SHA-256 | 496 |
109+-------------+-----------------------------------+
110| SHA-384 | 624 |
111+-------------+-----------------------------------+
112| SHA-512 | 752 |
113+-------------+-----------------------------------+
114
115
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +0200116
117Encryption and decryption
118--------------------------------------------------
119
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200120To encrypt or decrypt a message, use :py:func:`rsa.encrypt` resp.
121:py:func:`rsa.decrypt`. Let's say that Alice wants to send a message
122that only Bob can read.
123
124#. Bob generates a keypair, and gives the public key to Alice. This is
125 done such that Alice knows for sure that the key is really Bob's
126 (for example by handing over a USB stick that contains the key).
127
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200128 >>> (bob_pub, bob_priv) = rsa.newkeys(512)
129
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200130#. Alice writes a message
131
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200132 >>> message = 'hello Bob!'
133
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200134#. Alice encrypts the message using Bob's public key, and sends the
135 encrypted message.
136
Sybren A. Stüveldb348252011-07-31 19:22:47 +0200137 >>> crypto = rsa.encrypt(message, bob_pub)
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200138
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200139#. Bob receives the message, and decrypts it with his private key.
140
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200141 >>> message = rsa.decrypt(crypto, bob_priv)
142 >>> print message
143 hello Bob!
144
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200145Since Bob kept his private key *private*, Alice can be sure that he is
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200146the only one who can read the message. Bob does *not* know for sure
147that it was Alice that sent the message, since she didn't sign it.
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200148
149
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200150RSA can only encrypt messages that are smaller than the key. A couple
151of bytes are lost on random padding, and the rest is available for the
152message itself. For example, a 512-bit key can encode a 53-byte
153message (512 bit = 64 bytes, 11 bytes are used for random padding and
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200154other stuff). See :ref:`bigfiles` for information on how to work with
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200155larger files.
156
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200157Altering the encrypted information will *likely* cause a
158:py:class:`rsa.pkcs1.DecryptionError`. If you want to be *sure*, use
159:py:func:`rsa.sign`.
160
161 >>> crypto = encrypt('hello', pub_key)
162 >>> crypto = 'X' + crypto[1:] # change the first byte
163 >>> decrypt(crypto, priv_key)
164 Traceback (most recent call last):
165 ...
166 rsa.pkcs1.DecryptionError: Decryption failed
167
168
169.. warning::
170
171 Never display the stack trace of a
172 :py:class:`rsa.pkcs1.DecryptionError` exception. It shows where
173 in the code the exception occurred, and thus leaks information
174 about the key. It’s only a tiny bit of information, but every bit
175 makes cracking the keys easier.
176
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200177Low-level operations
178++++++++++++++++++++++++++++++
179
180The core RSA algorithm operates on large integers. These operations
181are considered low-level and are supported by the
182:py:func:`rsa.core.encrypt_int` and :py:func:`rsa.core.decrypt_int`
183functions.
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +0200184
185Signing and verification
186--------------------------------------------------
187
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200188You can create a detached signature for a message using the
189:py:func:`rsa.sign` function:
190
191 >>> (pubkey, privkey) = rsa.newkeys(512)
192 >>> message = 'Go left at the blue tree'
193 >>> signature = rsa.sign(message, privkey, 'SHA-1')
194
195This hashes the message using SHA-1. Other hash methods are also
196possible, check the :py:func:`rsa.sign` function documentation for
197details. The hash is then signed with the private key.
198
199In order to verify the signature, use the :py:func:`rsa.verify`
200function.
201
202 >>> message = 'Go left at the blue tree'
203 >>> rsa.verify(message, signature, pubkey)
204
205Modify the message, and the signature is no longer valid and a
206:py:class:`rsa.pkcs1.VerificationError` is thrown:
207
208 >>> message = 'Go right at the blue tree'
209 >>> rsa.verify(message, signature, pubkey)
210 Traceback (most recent call last):
211 File "<stdin>", line 1, in <module>
212 File "/home/sybren/workspace/python-rsa/rsa/pkcs1.py", line 289, in verify
213 raise VerificationError('Verification failed')
214 rsa.pkcs1.VerificationError: Verification failed
215
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200216.. warning::
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200217
218 Never display the stack trace of a
219 :py:class:`rsa.pkcs1.VerificationError` exception. It shows where
220 in the code the exception occurred, and thus leaks information
221 about the key. It's only a tiny bit of information, but every bit
222 makes cracking the keys easier.
223
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200224Instead of a message you can also call :py:func:`rsa.sign` and
225:py:func:`rsa.verify` with a :py:class:`file`-like object. If the
226message object has a ``read(int)`` method it is assumed to be a file.
227In that case the file is hashed in 1024-byte blocks at the time.
228
229 >>> with open('somefile', 'rb') as msgfile:
230 ... signature = rsa.sign(msgfile, privkey, 'SHA-1')
231
232 >>> with open('somefile', 'rb') as msgfile:
233 ... rsa.verify(msgfile, signature, pubkey)
234
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +0200235
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200236.. _bigfiles:
237
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +0200238Working with big files
239--------------------------------------------------
240
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200241RSA can only encrypt messages that are smaller than the key. A couple
242of bytes are lost on random padding, and the rest is available for the
243message itself. For example, a 512-bit key can encode a 53-byte
244message (512 bit = 64 bytes, 11 bytes are used for random padding and
245other stuff).
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +0200246
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200247How it usually works
248++++++++++++++++++++++++++++++++++++++++
249
250The most common way to use RSA with larger files uses a block cypher
251like AES or DES3 to encrypt the file with a random key, then encrypt
252the random key with RSA. You would send the encrypted file along with
253the encrypted key to the recipient. The complete flow is:
254
255#. Generate a random key
256
257 >>> import rsa.randnum
258 >>> aes_key = rsa.randnum.read_random_bits(128)
259
260#. Use that key to encrypt the file with AES.
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200261#. :py:func:`Encrypt <rsa.encrypt>` the AES key with RSA
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200262
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200263 >>> encrypted_aes_key = rsa.encrypt(aes_key, public_rsa_key)
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200264
265#. Send the encrypted file together with ``encrypted_aes_key``
266#. The recipient now reverses this process to obtain the encrypted
267 file.
268
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200269.. note::
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200270
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200271 The Python-RSA module does not contain functionality to do the AES
272 encryption for you.
273
274Only using Python-RSA: the VARBLOCK format
275+++++++++++++++++++++++++++++++++++++++++++
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200276
277As far as we know, there is no pure-Python AES encryption. Previous
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200278versions of Python-RSA included functionality to encrypt large files
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200279with just RSA, and so does this version. The format has been improved,
280though.
281
282Encrypting works as follows: the input file is split into blocks that
283are just large enough to encrypt with your RSA key. Every block is
284then encrypted using RSA, and the encrypted blocks are assembled into
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200285the output file. This file format is called the :ref:`VARBLOCK
286<VARBLOCK>` format.
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200287
288Decrypting works in reverse. The encrypted file is separated into
289encrypted blocks. Those are decrypted, and assembled into the original
290file.
291
292.. note::
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200293
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200294 The file will get larger after encryption, as each encrypted block
295 has 8 bytes of random padding and 3 more bytes of overhead.
296
297Since these encryption/decryption functions are potentially called on
298very large files, they use another approach. Where the regular
299functions store the message in memory in its entirety, these functions
300work on one block at the time. As a result, you should call them with
301:py:class:`file`-like objects as the parameters.
302
303Before using we of course need a keypair:
304
305>>> import rsa
306>>> (pub_key, priv_key) = rsa.newkeys(512)
307
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200308Encryption works on file handles using the
309:py:func:`rsa.bigfile.encrypt_bigfile` function:
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200310
311>>> from rsa.bigfile import *
312>>> with open('inputfile', 'rb') as infile, open('outputfile', 'wb') as outfile:
313... encrypt_bigfile(infile, outfile, pub_key)
314
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200315As does decryption using the :py:func:`rsa.bigfile.decrypt_bigfile`
316function:
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200317
318>>> from rsa.bigfile import *
319>>> with open('inputfile', 'rb') as infile, open('outputfile', 'wb') as outfile:
320... decrypt_bigfile(infile, outfile, priv_key)
321
322.. note::
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200323
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200324 :py:func:`rsa.sign` and :py:func:`rsa.verify` work on arbitrarily
325 long files, so they do not have a "bigfile" equivalent.
Sybren A. Stüvelaa28c042011-07-30 23:48:00 +0200326
Sybren A. Stüvelc1c455d2011-08-01 23:04:30 +0200327