Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 1 | .. _usage: |
| 2 | |
Sybren A. Stüvel | aa28c04 | 2011-07-30 23:48:00 +0200 | [diff] [blame] | 3 | Usage |
| 4 | ================================================== |
| 5 | |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 6 | This section describes the usage of the Python-RSA module. |
Sybren A. Stüvel | aa28c04 | 2011-07-30 23:48:00 +0200 | [diff] [blame] | 7 | |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 8 | Before you can use RSA you need keys. You will receive a private key |
| 9 | and a public key. |
| 10 | |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 11 | .. important:: |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 12 | |
| 13 | The private key is called *private* for a reason. Never share this |
| 14 | key with anyone. |
| 15 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 16 | The public key is used for encypting a message such that it can only |
| 17 | be read by the owner of the private key. As such it's also referred to |
| 18 | as the *encryption key*. Decrypting a message can only be done using |
| 19 | the private key, hence it's also called the *decryption key*. |
| 20 | |
| 21 | The private key is used for signing a message. With this signature and |
| 22 | the public key, the receiver can verifying that a message was signed |
| 23 | by the owner of the private key, and that the message was not modified |
| 24 | after signing. |
| 25 | |
| 26 | Generating keys |
| 27 | -------------------------------------------------- |
| 28 | |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 29 | You can use the :py:func:`rsa.newkeys` function to create a keypair: |
| 30 | |
| 31 | >>> (pubkey, privkey) = rsa.newkeys(512) |
| 32 | |
| 33 | Alternatively 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üvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 39 | |
Sybren A. Stüvel | 360d042 | 2011-08-10 12:52:59 +0200 | [diff] [blame^] | 40 | |
| 41 | Time to generate a key |
| 42 | ++++++++++++++++++++++++++++++++++++++++ |
| 43 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 44 | Generating a keypair may take a long time, depending on the number of |
| 45 | bits required. The number of bits determines the cryptographic |
| 46 | strength of the key, as well as the size of the message you can |
| 47 | encrypt. If you don't mind having a slightly smaller key than you |
| 48 | requested, you can pass ``accurate=False`` to speed up the key |
| 49 | generation process. |
| 50 | |
Sybren A. Stüvel | 360d042 | 2011-08-10 12:52:59 +0200 | [diff] [blame^] | 51 | Another way to speed up the key generation process is to use multiple |
| 52 | processes in parallel to speed up the key generation. Use no more than |
| 53 | the number of processes that your machine can run in parallel; a |
| 54 | dual-core machine should use ``poolsize=2``; a quad-core |
| 55 | hyperthreading machine can run two threads on each core, and thus can |
| 56 | use ``poolsize=8``. |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 57 | |
Sybren A. Stüvel | 360d042 | 2011-08-10 12:52:59 +0200 | [diff] [blame^] | 58 | >>> (pubkey, privkey) = rsa.newkeys(512, poolsize=8) |
| 59 | |
| 60 | These are some average timings from my desktop machine (Linux 2.6, |
| 61 | 2.93 GHz quad-core Intel Core i7, 16 GB RAM) using 64-bit CPython 2.7. |
| 62 | Since key generation is a random process, times may differ even on |
| 63 | similar 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üvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 84 | |
Sybren A. Stüvel | 58fe946 | 2011-08-03 13:56:32 +0200 | [diff] [blame] | 85 | If key generation is too slow for you, you could use OpenSSL to |
Sybren A. Stüvel | 360d042 | 2011-08-10 12:52:59 +0200 | [diff] [blame^] | 86 | generate them for you, then load them in your Python code. OpenSSL |
| 87 | generates a 4096-bit key in 3.5 seconds on the same machine as used |
| 88 | above. See :ref:`openssl` for more information. |
Sybren A. Stüvel | 58fe946 | 2011-08-03 13:56:32 +0200 | [diff] [blame] | 89 | |
| 90 | Key size requirements |
| 91 | -------------------------------------------------- |
| 92 | |
| 93 | Python-RSA version 3.0 introduced PKCS#1-style random padding. This |
| 94 | means that 11 bytes (88 bits) of your key are no longer usable for |
| 95 | encryption, so keys smaller than this are unusable. The larger the |
| 96 | key, the higher the security. |
| 97 | |
| 98 | Creating signatures also requires a key of a certain size, depending |
| 99 | on 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üvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 116 | |
| 117 | Encryption and decryption |
| 118 | -------------------------------------------------- |
| 119 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 120 | To 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 |
| 122 | that 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üvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 128 | >>> (bob_pub, bob_priv) = rsa.newkeys(512) |
| 129 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 130 | #. Alice writes a message |
| 131 | |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 132 | >>> message = 'hello Bob!' |
| 133 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 134 | #. Alice encrypts the message using Bob's public key, and sends the |
| 135 | encrypted message. |
| 136 | |
Sybren A. Stüvel | db34825 | 2011-07-31 19:22:47 +0200 | [diff] [blame] | 137 | >>> crypto = rsa.encrypt(message, bob_pub) |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 138 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 139 | #. Bob receives the message, and decrypts it with his private key. |
| 140 | |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 141 | >>> message = rsa.decrypt(crypto, bob_priv) |
| 142 | >>> print message |
| 143 | hello Bob! |
| 144 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 145 | Since Bob kept his private key *private*, Alice can be sure that he is |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 146 | the only one who can read the message. Bob does *not* know for sure |
| 147 | that it was Alice that sent the message, since she didn't sign it. |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 148 | |
| 149 | |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 150 | RSA can only encrypt messages that are smaller than the key. A couple |
| 151 | of bytes are lost on random padding, and the rest is available for the |
| 152 | message itself. For example, a 512-bit key can encode a 53-byte |
| 153 | message (512 bit = 64 bytes, 11 bytes are used for random padding and |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 154 | other stuff). See :ref:`bigfiles` for information on how to work with |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 155 | larger files. |
| 156 | |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 157 | Altering 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üvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 177 | Low-level operations |
| 178 | ++++++++++++++++++++++++++++++ |
| 179 | |
| 180 | The core RSA algorithm operates on large integers. These operations |
| 181 | are considered low-level and are supported by the |
| 182 | :py:func:`rsa.core.encrypt_int` and :py:func:`rsa.core.decrypt_int` |
| 183 | functions. |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 184 | |
| 185 | Signing and verification |
| 186 | -------------------------------------------------- |
| 187 | |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 188 | You 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 | |
| 195 | This hashes the message using SHA-1. Other hash methods are also |
| 196 | possible, check the :py:func:`rsa.sign` function documentation for |
| 197 | details. The hash is then signed with the private key. |
| 198 | |
| 199 | In order to verify the signature, use the :py:func:`rsa.verify` |
| 200 | function. |
| 201 | |
| 202 | >>> message = 'Go left at the blue tree' |
| 203 | >>> rsa.verify(message, signature, pubkey) |
| 204 | |
| 205 | Modify 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üvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 216 | .. warning:: |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 217 | |
| 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üvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 224 | Instead 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 |
| 226 | message object has a ``read(int)`` method it is assumed to be a file. |
| 227 | In 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üvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 235 | |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 236 | .. _bigfiles: |
| 237 | |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 238 | Working with big files |
| 239 | -------------------------------------------------- |
| 240 | |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 241 | RSA can only encrypt messages that are smaller than the key. A couple |
| 242 | of bytes are lost on random padding, and the rest is available for the |
| 243 | message itself. For example, a 512-bit key can encode a 53-byte |
| 244 | message (512 bit = 64 bytes, 11 bytes are used for random padding and |
| 245 | other stuff). |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 246 | |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 247 | How it usually works |
| 248 | ++++++++++++++++++++++++++++++++++++++++ |
| 249 | |
| 250 | The most common way to use RSA with larger files uses a block cypher |
| 251 | like AES or DES3 to encrypt the file with a random key, then encrypt |
| 252 | the random key with RSA. You would send the encrypted file along with |
| 253 | the 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üvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 261 | #. :py:func:`Encrypt <rsa.encrypt>` the AES key with RSA |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 262 | |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 263 | >>> encrypted_aes_key = rsa.encrypt(aes_key, public_rsa_key) |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 264 | |
| 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üvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 269 | .. note:: |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 270 | |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 271 | The Python-RSA module does not contain functionality to do the AES |
| 272 | encryption for you. |
| 273 | |
| 274 | Only using Python-RSA: the VARBLOCK format |
| 275 | +++++++++++++++++++++++++++++++++++++++++++ |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 276 | |
| 277 | As far as we know, there is no pure-Python AES encryption. Previous |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 278 | versions of Python-RSA included functionality to encrypt large files |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 279 | with just RSA, and so does this version. The format has been improved, |
| 280 | though. |
| 281 | |
| 282 | Encrypting works as follows: the input file is split into blocks that |
| 283 | are just large enough to encrypt with your RSA key. Every block is |
| 284 | then encrypted using RSA, and the encrypted blocks are assembled into |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 285 | the output file. This file format is called the :ref:`VARBLOCK |
| 286 | <VARBLOCK>` format. |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 287 | |
| 288 | Decrypting works in reverse. The encrypted file is separated into |
| 289 | encrypted blocks. Those are decrypted, and assembled into the original |
| 290 | file. |
| 291 | |
| 292 | .. note:: |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 293 | |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 294 | 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 | |
| 297 | Since these encryption/decryption functions are potentially called on |
| 298 | very large files, they use another approach. Where the regular |
| 299 | functions store the message in memory in its entirety, these functions |
| 300 | work on one block at the time. As a result, you should call them with |
| 301 | :py:class:`file`-like objects as the parameters. |
| 302 | |
| 303 | Before using we of course need a keypair: |
| 304 | |
| 305 | >>> import rsa |
| 306 | >>> (pub_key, priv_key) = rsa.newkeys(512) |
| 307 | |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 308 | Encryption works on file handles using the |
| 309 | :py:func:`rsa.bigfile.encrypt_bigfile` function: |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 310 | |
| 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üvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 315 | As does decryption using the :py:func:`rsa.bigfile.decrypt_bigfile` |
| 316 | function: |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 317 | |
| 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üvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 323 | |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 324 | :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üvel | aa28c04 | 2011-07-30 23:48:00 +0200 | [diff] [blame] | 326 | |
Sybren A. Stüvel | c1c455d | 2011-08-01 23:04:30 +0200 | [diff] [blame] | 327 | |