blob: 68bffcaf56df5a36a06a5720e1472be5b1d1582e [file] [log] [blame]
Sybren A. Stüvelaa28c042011-07-30 23:48:00 +02001Usage
2==================================================
3
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +02004This section describes the usage of the Python-RSA module.
Sybren A. Stüvelaa28c042011-07-30 23:48:00 +02005
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +02006Before you can use RSA you need keys. You will receive a private key
7and a public key.
8
9.. note::
10
11 The private key is called *private* for a reason. Never share this
12 key with anyone.
13
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020014The public key is used for encypting a message such that it can only
15be read by the owner of the private key. As such it's also referred to
16as the *encryption key*. Decrypting a message can only be done using
17the private key, hence it's also called the *decryption key*.
18
19The private key is used for signing a message. With this signature and
20the public key, the receiver can verifying that a message was signed
21by the owner of the private key, and that the message was not modified
22after signing.
23
24Generating keys
25--------------------------------------------------
26
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +020027You can use the :py:func:`rsa.newkeys` function to create a keypair:
28
29 >>> (pubkey, privkey) = rsa.newkeys(512)
30
31Alternatively you can use :py:meth:`rsa.PrivateKey.load_pkcs1` and
32:py:meth:`rsa.PublicKey.load_pkcs1` to load keys from a file:
33
34 >>> with open('private.pem') as privatefile:
35 ... keydata = privatefile.read()
36 >>> pubkey = rsa.PrivateKey.load_pkcs1(keydata)
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020037
38Generating a keypair may take a long time, depending on the number of
39bits required. The number of bits determines the cryptographic
40strength of the key, as well as the size of the message you can
41encrypt. If you don't mind having a slightly smaller key than you
42requested, you can pass ``accurate=False`` to speed up the key
43generation process.
44
45These are some timings from my netbook (Linux 2.6, 1.6 GHz Intel Atom
46N270 CPU, 2 GB RAM):
47
48+----------------+------------------+
49| Keysize (bits) | Time to generate |
50+================+==================+
51| 32 | 0.01 sec. |
52+----------------+------------------+
53| 64 | 0.03 sec. |
54+----------------+------------------+
55| 96 | 0.04 sec. |
56+----------------+------------------+
57| 128 | 0.08 sec. |
58+----------------+------------------+
59| 256 | 0.27 sec. |
60+----------------+------------------+
61| 384 | 0.93 sec. |
62+----------------+------------------+
63| 512 | 1.21 sec. |
64+----------------+------------------+
65| 1024 | 7.93 sec. |
66+----------------+------------------+
67| 2048 | 132.97 sec. |
68+----------------+------------------+
69
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +020070
71Encryption and decryption
72--------------------------------------------------
73
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020074To encrypt or decrypt a message, use :py:func:`rsa.encrypt` resp.
75:py:func:`rsa.decrypt`. Let's say that Alice wants to send a message
76that only Bob can read.
77
78#. Bob generates a keypair, and gives the public key to Alice. This is
79 done such that Alice knows for sure that the key is really Bob's
80 (for example by handing over a USB stick that contains the key).
81
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +020082 >>> (bob_pub, bob_priv) = rsa.newkeys(512)
83
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020084#. Alice writes a message
85
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +020086 >>> message = 'hello Bob!'
87
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020088#. Alice encrypts the message using Bob's public key, and sends the
89 encrypted message.
90
Sybren A. Stüveldb348252011-07-31 19:22:47 +020091 >>> crypto = rsa.encrypt(message, bob_pub)
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +020092
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020093#. Bob receives the message, and decrypts it with his private key.
94
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +020095 >>> message = rsa.decrypt(crypto, bob_priv)
96 >>> print message
97 hello Bob!
98
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020099Since Bob kept his private key *private*, Alice can be sure that he is
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200100the only one who can read the message.
101
102.. note::
103
104 Bob does *not* know for sure that it was Alice that sent the
105 message, since she didn't sign it.
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200106
107
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200108RSA can only encrypt messages that are smaller than the key. A couple
109of bytes are lost on random padding, and the rest is available for the
110message itself. For example, a 512-bit key can encode a 53-byte
111message (512 bit = 64 bytes, 11 bytes are used for random padding and
112other stuff).
113
114See `Working with big files`_ for information on how to work with
115larger files.
116
Sybren A. Stüveld92b6672011-07-31 17:44:44 +0200117Low-level operations
118++++++++++++++++++++++++++++++
119
120The core RSA algorithm operates on large integers. These operations
121are considered low-level and are supported by the
122:py:func:`rsa.core.encrypt_int` and :py:func:`rsa.core.decrypt_int`
123functions.
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +0200124
125Signing and verification
126--------------------------------------------------
127
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200128You can create a detached signature for a message using the
129:py:func:`rsa.sign` function:
130
131 >>> (pubkey, privkey) = rsa.newkeys(512)
132 >>> message = 'Go left at the blue tree'
133 >>> signature = rsa.sign(message, privkey, 'SHA-1')
134
135This hashes the message using SHA-1. Other hash methods are also
136possible, check the :py:func:`rsa.sign` function documentation for
137details. The hash is then signed with the private key.
138
139In order to verify the signature, use the :py:func:`rsa.verify`
140function.
141
142 >>> message = 'Go left at the blue tree'
143 >>> rsa.verify(message, signature, pubkey)
144
145Modify the message, and the signature is no longer valid and a
146:py:class:`rsa.pkcs1.VerificationError` is thrown:
147
148 >>> message = 'Go right at the blue tree'
149 >>> rsa.verify(message, signature, pubkey)
150 Traceback (most recent call last):
151 File "<stdin>", line 1, in <module>
152 File "/home/sybren/workspace/python-rsa/rsa/pkcs1.py", line 289, in verify
153 raise VerificationError('Verification failed')
154 rsa.pkcs1.VerificationError: Verification failed
155
156.. note::
157
158 Never display the stack trace of a
159 :py:class:`rsa.pkcs1.VerificationError` 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üvelb6c04dd2011-08-01 21:37:02 +0200164Instead of a message you can also call :py:func:`rsa.sign` and
165:py:func:`rsa.verify` with a :py:class:`file`-like object. If the
166message object has a ``read(int)`` method it is assumed to be a file.
167In that case the file is hashed in 1024-byte blocks at the time.
168
169 >>> with open('somefile', 'rb') as msgfile:
170 ... signature = rsa.sign(msgfile, privkey, 'SHA-1')
171
172 >>> with open('somefile', 'rb') as msgfile:
173 ... rsa.verify(msgfile, signature, pubkey)
174
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +0200175
176Working with big files
177--------------------------------------------------
178
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200179RSA can only encrypt messages that are smaller than the key. A couple
180of bytes are lost on random padding, and the rest is available for the
181message itself. For example, a 512-bit key can encode a 53-byte
182message (512 bit = 64 bytes, 11 bytes are used for random padding and
183other stuff).
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +0200184
Sybren A. Stüvelb6c04dd2011-08-01 21:37:02 +0200185How it usually works
186++++++++++++++++++++++++++++++++++++++++
187
188The most common way to use RSA with larger files uses a block cypher
189like AES or DES3 to encrypt the file with a random key, then encrypt
190the random key with RSA. You would send the encrypted file along with
191the encrypted key to the recipient. The complete flow is:
192
193#. Generate a random key
194
195 >>> import rsa.randnum
196 >>> aes_key = rsa.randnum.read_random_bits(128)
197
198#. Use that key to encrypt the file with AES.
199#. Encrypt the AES key with RSA
200
201 >>> encrypted_aes_key = rsa.encrypt(aes_key, public_key)
202
203#. Send the encrypted file together with ``encrypted_aes_key``
204#. The recipient now reverses this process to obtain the encrypted
205 file.
206
207
208Only using Python-RSA
209++++++++++++++++++++++++++++++++++++++++
210
211As far as we know, there is no pure-Python AES encryption. Previous
212versions of Python-RSA included functionality to encrypt large files,
213with just RSA, and so does this version. The format has been improved,
214though.
215
216Encrypting works as follows: the input file is split into blocks that
217are just large enough to encrypt with your RSA key. Every block is
218then encrypted using RSA, and the encrypted blocks are assembled into
219the output file.
220
221Decrypting works in reverse. The encrypted file is separated into
222encrypted blocks. Those are decrypted, and assembled into the original
223file.
224
225.. note::
226 The file will get larger after encryption, as each encrypted block
227 has 8 bytes of random padding and 3 more bytes of overhead.
228
229Since these encryption/decryption functions are potentially called on
230very large files, they use another approach. Where the regular
231functions store the message in memory in its entirety, these functions
232work on one block at the time. As a result, you should call them with
233:py:class:`file`-like objects as the parameters.
234
235Before using we of course need a keypair:
236
237>>> import rsa
238>>> (pub_key, priv_key) = rsa.newkeys(512)
239
240Encryption works on file handles:
241
242>>> from rsa.bigfile import *
243>>> with open('inputfile', 'rb') as infile, open('outputfile', 'wb') as outfile:
244... encrypt_bigfile(infile, outfile, pub_key)
245
246As does decryption:
247
248>>> from rsa.bigfile import *
249>>> with open('inputfile', 'rb') as infile, open('outputfile', 'wb') as outfile:
250... decrypt_bigfile(infile, outfile, priv_key)
251
252.. note::
253 :py:func:`rsa.sign` and :py:func:`rsa.verify` work on arbitrarily
254 long files, so they do not have a "bigfile" equivalent.
Sybren A. Stüvelaa28c042011-07-30 23:48:00 +0200255