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 | |
| 40 | Generating a keypair may take a long time, depending on the number of |
| 41 | bits required. The number of bits determines the cryptographic |
| 42 | strength of the key, as well as the size of the message you can |
| 43 | encrypt. If you don't mind having a slightly smaller key than you |
| 44 | requested, you can pass ``accurate=False`` to speed up the key |
| 45 | generation process. |
| 46 | |
Sybren A. Stüvel | 58fe946 | 2011-08-03 13:56:32 +0200 | [diff] [blame] | 47 | These are some average timings from my netbook (Linux 2.6, 1.6 GHz |
| 48 | Intel Atom N270 CPU, 2 GB RAM). Since key generation is a random |
| 49 | process, times may differ. |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 50 | |
| 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üvel | 58fe946 | 2011-08-03 13:56:32 +0200 | [diff] [blame] | 73 | If key generation is too slow for you, you could use OpenSSL to |
| 74 | generate them for you, then load them in your Python code. See |
| 75 | :ref:`openssl` for more information. |
| 76 | |
| 77 | Key size requirements |
| 78 | -------------------------------------------------- |
| 79 | |
| 80 | Python-RSA version 3.0 introduced PKCS#1-style random padding. This |
| 81 | means that 11 bytes (88 bits) of your key are no longer usable for |
| 82 | encryption, so keys smaller than this are unusable. The larger the |
| 83 | key, the higher the security. |
| 84 | |
| 85 | Creating signatures also requires a key of a certain size, depending |
| 86 | on 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üvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 103 | |
| 104 | Encryption and decryption |
| 105 | -------------------------------------------------- |
| 106 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 107 | To 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 |
| 109 | that 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üvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 115 | >>> (bob_pub, bob_priv) = rsa.newkeys(512) |
| 116 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 117 | #. Alice writes a message |
| 118 | |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 119 | >>> message = 'hello Bob!' |
| 120 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 121 | #. Alice encrypts the message using Bob's public key, and sends the |
| 122 | encrypted message. |
| 123 | |
Sybren A. Stüvel | db34825 | 2011-07-31 19:22:47 +0200 | [diff] [blame] | 124 | >>> crypto = rsa.encrypt(message, bob_pub) |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 125 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 126 | #. Bob receives the message, and decrypts it with his private key. |
| 127 | |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 128 | >>> message = rsa.decrypt(crypto, bob_priv) |
| 129 | >>> print message |
| 130 | hello Bob! |
| 131 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 132 | 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] | 133 | the only one who can read the message. Bob does *not* know for sure |
| 134 | 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] | 135 | |
| 136 | |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 137 | RSA can only encrypt messages that are smaller than the key. A couple |
| 138 | of bytes are lost on random padding, and the rest is available for the |
| 139 | message itself. For example, a 512-bit key can encode a 53-byte |
| 140 | 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] | 141 | 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] | 142 | larger files. |
| 143 | |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 144 | Altering 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üvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 164 | Low-level operations |
| 165 | ++++++++++++++++++++++++++++++ |
| 166 | |
| 167 | The core RSA algorithm operates on large integers. These operations |
| 168 | are considered low-level and are supported by the |
| 169 | :py:func:`rsa.core.encrypt_int` and :py:func:`rsa.core.decrypt_int` |
| 170 | functions. |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 171 | |
| 172 | Signing and verification |
| 173 | -------------------------------------------------- |
| 174 | |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 175 | You 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 | |
| 182 | This hashes the message using SHA-1. Other hash methods are also |
| 183 | possible, check the :py:func:`rsa.sign` function documentation for |
| 184 | details. The hash is then signed with the private key. |
| 185 | |
| 186 | In order to verify the signature, use the :py:func:`rsa.verify` |
| 187 | function. |
| 188 | |
| 189 | >>> message = 'Go left at the blue tree' |
| 190 | >>> rsa.verify(message, signature, pubkey) |
| 191 | |
| 192 | Modify 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üvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 203 | .. warning:: |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 204 | |
| 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üvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 211 | Instead 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 |
| 213 | message object has a ``read(int)`` method it is assumed to be a file. |
| 214 | In 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üvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 222 | |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 223 | .. _bigfiles: |
| 224 | |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 225 | Working with big files |
| 226 | -------------------------------------------------- |
| 227 | |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 228 | RSA can only encrypt messages that are smaller than the key. A couple |
| 229 | of bytes are lost on random padding, and the rest is available for the |
| 230 | message itself. For example, a 512-bit key can encode a 53-byte |
| 231 | message (512 bit = 64 bytes, 11 bytes are used for random padding and |
| 232 | other stuff). |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 233 | |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 234 | How it usually works |
| 235 | ++++++++++++++++++++++++++++++++++++++++ |
| 236 | |
| 237 | The most common way to use RSA with larger files uses a block cypher |
| 238 | like AES or DES3 to encrypt the file with a random key, then encrypt |
| 239 | the random key with RSA. You would send the encrypted file along with |
| 240 | the 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üvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 248 | #. :py:func:`Encrypt <rsa.encrypt>` the AES key with RSA |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 249 | |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 250 | >>> encrypted_aes_key = rsa.encrypt(aes_key, public_rsa_key) |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 251 | |
| 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üvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 256 | .. note:: |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 257 | |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 258 | The Python-RSA module does not contain functionality to do the AES |
| 259 | encryption for you. |
| 260 | |
| 261 | Only using Python-RSA: the VARBLOCK format |
| 262 | +++++++++++++++++++++++++++++++++++++++++++ |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 263 | |
| 264 | 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] | 265 | versions of Python-RSA included functionality to encrypt large files |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 266 | with just RSA, and so does this version. The format has been improved, |
| 267 | though. |
| 268 | |
| 269 | Encrypting works as follows: the input file is split into blocks that |
| 270 | are just large enough to encrypt with your RSA key. Every block is |
| 271 | 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] | 272 | the output file. This file format is called the :ref:`VARBLOCK |
| 273 | <VARBLOCK>` format. |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 274 | |
| 275 | Decrypting works in reverse. The encrypted file is separated into |
| 276 | encrypted blocks. Those are decrypted, and assembled into the original |
| 277 | file. |
| 278 | |
| 279 | .. note:: |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 280 | |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 281 | 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 | |
| 284 | Since these encryption/decryption functions are potentially called on |
| 285 | very large files, they use another approach. Where the regular |
| 286 | functions store the message in memory in its entirety, these functions |
| 287 | work on one block at the time. As a result, you should call them with |
| 288 | :py:class:`file`-like objects as the parameters. |
| 289 | |
| 290 | Before using we of course need a keypair: |
| 291 | |
| 292 | >>> import rsa |
| 293 | >>> (pub_key, priv_key) = rsa.newkeys(512) |
| 294 | |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 295 | Encryption works on file handles using the |
| 296 | :py:func:`rsa.bigfile.encrypt_bigfile` function: |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 297 | |
| 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üvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 302 | As does decryption using the :py:func:`rsa.bigfile.decrypt_bigfile` |
| 303 | function: |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 304 | |
| 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üvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame] | 310 | |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 311 | :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üvel | aa28c04 | 2011-07-30 23:48:00 +0200 | [diff] [blame] | 313 | |
Sybren A. Stüvel | c1c455d | 2011-08-01 23:04:30 +0200 | [diff] [blame] | 314 | |