blob: e4436e4367ecf001d905ab8cbec1668c2c000891 [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
Sybren A. Stüvel58fe9462011-08-03 13:56:32 +020047These are some average timings from my netbook (Linux 2.6, 1.6 GHz
48Intel Atom N270 CPU, 2 GB RAM). Since key generation is a random
49process, times may differ.
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020050
51+----------------+------------------+
52| Keysize (bits) | Time to generate |
53+================+==================+
54| 32 | 0.01 sec. |
55+----------------+------------------+
56| 64 | 0.03 sec. |
57+----------------+------------------+
58| 96 | 0.04 sec. |
59+----------------+------------------+
60| 128 | 0.08 sec. |
61+----------------+------------------+
62| 256 | 0.27 sec. |
63+----------------+------------------+
64| 384 | 0.93 sec. |
65+----------------+------------------+
66| 512 | 1.21 sec. |
67+----------------+------------------+
68| 1024 | 7.93 sec. |
69+----------------+------------------+
70| 2048 | 132.97 sec. |
71+----------------+------------------+
72
Sybren A. Stüvel58fe9462011-08-03 13:56:32 +020073If key generation is too slow for you, you could use OpenSSL to
74generate them for you, then load them in your Python code. See
75:ref:`openssl` for more information.
76
77Key size requirements
78--------------------------------------------------
79
80Python-RSA version 3.0 introduced PKCS#1-style random padding. This
81means that 11 bytes (88 bits) of your key are no longer usable for
82encryption, so keys smaller than this are unusable. The larger the
83key, the higher the security.
84
85Creating signatures also requires a key of a certain size, depending
86on the used hash method:
87
88+-------------+-----------------------------------+
89| Hash method | Suggested minimum key size (bits) |
90+=============+===================================+
91| MD5 | 360 |
92+-------------+-----------------------------------+
93| SHA-1 | 368 |
94+-------------+-----------------------------------+
95| SHA-256 | 496 |
96+-------------+-----------------------------------+
97| SHA-384 | 624 |
98+-------------+-----------------------------------+
99| SHA-512 | 752 |
100+-------------+-----------------------------------+
101
102
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +0200103
104Encryption and decryption
105--------------------------------------------------
106
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200107To encrypt or decrypt a message, use :py:func:`rsa.encrypt` resp.
108:py:func:`rsa.decrypt`. Let's say that Alice wants to send a message
109that only Bob can read.
110
111#. Bob generates a keypair, and gives the public key to Alice. This is
112 done such that Alice knows for sure that the key is really Bob's
113 (for example by handing over a USB stick that contains the key).
114
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200115 >>> (bob_pub, bob_priv) = rsa.newkeys(512)
116
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200117#. Alice writes a message
118
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200119 >>> message = 'hello Bob!'
120
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200121#. Alice encrypts the message using Bob's public key, and sends the
122 encrypted message.
123
Sybren A. Stüveldb348252011-07-31 19:22:47 +0200124 >>> crypto = rsa.encrypt(message, bob_pub)
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200125
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200126#. Bob receives the message, and decrypts it with his private key.
127
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200128 >>> message = rsa.decrypt(crypto, bob_priv)
129 >>> print message
130 hello Bob!
131
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200132Since Bob kept his private key *private*, Alice can be sure that he is
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200133the only one who can read the message. Bob does *not* know for sure
134that it was Alice that sent the message, since she didn't sign it.
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200135
136
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200137RSA can only encrypt messages that are smaller than the key. A couple
138of bytes are lost on random padding, and the rest is available for the
139message itself. For example, a 512-bit key can encode a 53-byte
140message (512 bit = 64 bytes, 11 bytes are used for random padding and
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200141other stuff). See :ref:`bigfiles` for information on how to work with
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200142larger files.
143
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200144Altering the encrypted information will *likely* cause a
145:py:class:`rsa.pkcs1.DecryptionError`. If you want to be *sure*, use
146:py:func:`rsa.sign`.
147
148 >>> crypto = encrypt('hello', pub_key)
149 >>> crypto = 'X' + crypto[1:] # change the first byte
150 >>> decrypt(crypto, priv_key)
151 Traceback (most recent call last):
152 ...
153 rsa.pkcs1.DecryptionError: Decryption failed
154
155
156.. warning::
157
158 Never display the stack trace of a
159 :py:class:`rsa.pkcs1.DecryptionError` exception. It shows where
160 in the code the exception occurred, and thus leaks information
161 about the key. It’s only a tiny bit of information, but every bit
162 makes cracking the keys easier.
163
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200164Low-level operations
165++++++++++++++++++++++++++++++
166
167The core RSA algorithm operates on large integers. These operations
168are considered low-level and are supported by the
169:py:func:`rsa.core.encrypt_int` and :py:func:`rsa.core.decrypt_int`
170functions.
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +0200171
172Signing and verification
173--------------------------------------------------
174
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200175You can create a detached signature for a message using the
176:py:func:`rsa.sign` function:
177
178 >>> (pubkey, privkey) = rsa.newkeys(512)
179 >>> message = 'Go left at the blue tree'
180 >>> signature = rsa.sign(message, privkey, 'SHA-1')
181
182This hashes the message using SHA-1. Other hash methods are also
183possible, check the :py:func:`rsa.sign` function documentation for
184details. The hash is then signed with the private key.
185
186In order to verify the signature, use the :py:func:`rsa.verify`
187function.
188
189 >>> message = 'Go left at the blue tree'
190 >>> rsa.verify(message, signature, pubkey)
191
192Modify the message, and the signature is no longer valid and a
193:py:class:`rsa.pkcs1.VerificationError` is thrown:
194
195 >>> message = 'Go right at the blue tree'
196 >>> rsa.verify(message, signature, pubkey)
197 Traceback (most recent call last):
198 File "<stdin>", line 1, in <module>
199 File "/home/sybren/workspace/python-rsa/rsa/pkcs1.py", line 289, in verify
200 raise VerificationError('Verification failed')
201 rsa.pkcs1.VerificationError: Verification failed
202
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200203.. warning::
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200204
205 Never display the stack trace of a
206 :py:class:`rsa.pkcs1.VerificationError` exception. It shows where
207 in the code the exception occurred, and thus leaks information
208 about the key. It's only a tiny bit of information, but every bit
209 makes cracking the keys easier.
210
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200211Instead of a message you can also call :py:func:`rsa.sign` and
212:py:func:`rsa.verify` with a :py:class:`file`-like object. If the
213message object has a ``read(int)`` method it is assumed to be a file.
214In that case the file is hashed in 1024-byte blocks at the time.
215
216 >>> with open('somefile', 'rb') as msgfile:
217 ... signature = rsa.sign(msgfile, privkey, 'SHA-1')
218
219 >>> with open('somefile', 'rb') as msgfile:
220 ... rsa.verify(msgfile, signature, pubkey)
221
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +0200222
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200223.. _bigfiles:
224
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +0200225Working with big files
226--------------------------------------------------
227
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200228RSA can only encrypt messages that are smaller than the key. A couple
229of bytes are lost on random padding, and the rest is available for the
230message itself. For example, a 512-bit key can encode a 53-byte
231message (512 bit = 64 bytes, 11 bytes are used for random padding and
232other stuff).
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +0200233
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200234How it usually works
235++++++++++++++++++++++++++++++++++++++++
236
237The most common way to use RSA with larger files uses a block cypher
238like AES or DES3 to encrypt the file with a random key, then encrypt
239the random key with RSA. You would send the encrypted file along with
240the encrypted key to the recipient. The complete flow is:
241
242#. Generate a random key
243
244 >>> import rsa.randnum
245 >>> aes_key = rsa.randnum.read_random_bits(128)
246
247#. Use that key to encrypt the file with AES.
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200248#. :py:func:`Encrypt <rsa.encrypt>` the AES key with RSA
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200249
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200250 >>> encrypted_aes_key = rsa.encrypt(aes_key, public_rsa_key)
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200251
252#. Send the encrypted file together with ``encrypted_aes_key``
253#. The recipient now reverses this process to obtain the encrypted
254 file.
255
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200256.. note::
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200257
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200258 The Python-RSA module does not contain functionality to do the AES
259 encryption for you.
260
261Only using Python-RSA: the VARBLOCK format
262+++++++++++++++++++++++++++++++++++++++++++
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200263
264As far as we know, there is no pure-Python AES encryption. Previous
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200265versions of Python-RSA included functionality to encrypt large files
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200266with just RSA, and so does this version. The format has been improved,
267though.
268
269Encrypting works as follows: the input file is split into blocks that
270are just large enough to encrypt with your RSA key. Every block is
271then encrypted using RSA, and the encrypted blocks are assembled into
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200272the output file. This file format is called the :ref:`VARBLOCK
273<VARBLOCK>` format.
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200274
275Decrypting works in reverse. The encrypted file is separated into
276encrypted blocks. Those are decrypted, and assembled into the original
277file.
278
279.. note::
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200280
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200281 The file will get larger after encryption, as each encrypted block
282 has 8 bytes of random padding and 3 more bytes of overhead.
283
284Since these encryption/decryption functions are potentially called on
285very large files, they use another approach. Where the regular
286functions store the message in memory in its entirety, these functions
287work on one block at the time. As a result, you should call them with
288:py:class:`file`-like objects as the parameters.
289
290Before using we of course need a keypair:
291
292>>> import rsa
293>>> (pub_key, priv_key) = rsa.newkeys(512)
294
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200295Encryption works on file handles using the
296:py:func:`rsa.bigfile.encrypt_bigfile` function:
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200297
298>>> from rsa.bigfile import *
299>>> with open('inputfile', 'rb') as infile, open('outputfile', 'wb') as outfile:
300... encrypt_bigfile(infile, outfile, pub_key)
301
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200302As does decryption using the :py:func:`rsa.bigfile.decrypt_bigfile`
303function:
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200304
305>>> from rsa.bigfile import *
306>>> with open('inputfile', 'rb') as infile, open('outputfile', 'wb') as outfile:
307... decrypt_bigfile(infile, outfile, priv_key)
308
309.. note::
Sybren A. Stüveldbea2132011-08-03 13:31:57 +0200310
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200311 :py:func:`rsa.sign` and :py:func:`rsa.verify` work on arbitrarily
312 long files, so they do not have a "bigfile" equivalent.
Sybren A. Stüvelaa28c042011-07-30 23:48:00 +0200313
Sybren A. Stüvelc1c455d2011-08-01 23:04:30 +0200314