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 | |
| 47 | These are some timings from my netbook (Linux 2.6, 1.6 GHz Intel Atom |
| 48 | N270 CPU, 2 GB RAM): |
| 49 | |
| 50 | +----------------+------------------+ |
| 51 | | Keysize (bits) | Time to generate | |
| 52 | +================+==================+ |
| 53 | | 32 | 0.01 sec. | |
| 54 | +----------------+------------------+ |
| 55 | | 64 | 0.03 sec. | |
| 56 | +----------------+------------------+ |
| 57 | | 96 | 0.04 sec. | |
| 58 | +----------------+------------------+ |
| 59 | | 128 | 0.08 sec. | |
| 60 | +----------------+------------------+ |
| 61 | | 256 | 0.27 sec. | |
| 62 | +----------------+------------------+ |
| 63 | | 384 | 0.93 sec. | |
| 64 | +----------------+------------------+ |
| 65 | | 512 | 1.21 sec. | |
| 66 | +----------------+------------------+ |
| 67 | | 1024 | 7.93 sec. | |
| 68 | +----------------+------------------+ |
| 69 | | 2048 | 132.97 sec. | |
| 70 | +----------------+------------------+ |
| 71 | |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 72 | |
| 73 | Encryption and decryption |
| 74 | -------------------------------------------------- |
| 75 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 76 | To encrypt or decrypt a message, use :py:func:`rsa.encrypt` resp. |
| 77 | :py:func:`rsa.decrypt`. Let's say that Alice wants to send a message |
| 78 | that only Bob can read. |
| 79 | |
| 80 | #. Bob generates a keypair, and gives the public key to Alice. This is |
| 81 | done such that Alice knows for sure that the key is really Bob's |
| 82 | (for example by handing over a USB stick that contains the key). |
| 83 | |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 84 | >>> (bob_pub, bob_priv) = rsa.newkeys(512) |
| 85 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 86 | #. Alice writes a message |
| 87 | |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 88 | >>> message = 'hello Bob!' |
| 89 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 90 | #. Alice encrypts the message using Bob's public key, and sends the |
| 91 | encrypted message. |
| 92 | |
Sybren A. Stüvel | db34825 | 2011-07-31 19:22:47 +0200 | [diff] [blame] | 93 | >>> crypto = rsa.encrypt(message, bob_pub) |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 94 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 95 | #. Bob receives the message, and decrypts it with his private key. |
| 96 | |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 97 | >>> message = rsa.decrypt(crypto, bob_priv) |
| 98 | >>> print message |
| 99 | hello Bob! |
| 100 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 101 | 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^] | 102 | the only one who can read the message. Bob does *not* know for sure |
| 103 | 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] | 104 | |
| 105 | |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 106 | RSA can only encrypt messages that are smaller than the key. A couple |
| 107 | of bytes are lost on random padding, and the rest is available for the |
| 108 | message itself. For example, a 512-bit key can encode a 53-byte |
| 109 | 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^] | 110 | 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] | 111 | larger files. |
| 112 | |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame^] | 113 | Altering the encrypted information will *likely* cause a |
| 114 | :py:class:`rsa.pkcs1.DecryptionError`. If you want to be *sure*, use |
| 115 | :py:func:`rsa.sign`. |
| 116 | |
| 117 | >>> crypto = encrypt('hello', pub_key) |
| 118 | >>> crypto = 'X' + crypto[1:] # change the first byte |
| 119 | >>> decrypt(crypto, priv_key) |
| 120 | Traceback (most recent call last): |
| 121 | ... |
| 122 | rsa.pkcs1.DecryptionError: Decryption failed |
| 123 | |
| 124 | |
| 125 | .. warning:: |
| 126 | |
| 127 | Never display the stack trace of a |
| 128 | :py:class:`rsa.pkcs1.DecryptionError` exception. It shows where |
| 129 | in the code the exception occurred, and thus leaks information |
| 130 | about the key. It’s only a tiny bit of information, but every bit |
| 131 | makes cracking the keys easier. |
| 132 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 133 | Low-level operations |
| 134 | ++++++++++++++++++++++++++++++ |
| 135 | |
| 136 | The core RSA algorithm operates on large integers. These operations |
| 137 | are considered low-level and are supported by the |
| 138 | :py:func:`rsa.core.encrypt_int` and :py:func:`rsa.core.decrypt_int` |
| 139 | functions. |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 140 | |
| 141 | Signing and verification |
| 142 | -------------------------------------------------- |
| 143 | |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 144 | You can create a detached signature for a message using the |
| 145 | :py:func:`rsa.sign` function: |
| 146 | |
| 147 | >>> (pubkey, privkey) = rsa.newkeys(512) |
| 148 | >>> message = 'Go left at the blue tree' |
| 149 | >>> signature = rsa.sign(message, privkey, 'SHA-1') |
| 150 | |
| 151 | This hashes the message using SHA-1. Other hash methods are also |
| 152 | possible, check the :py:func:`rsa.sign` function documentation for |
| 153 | details. The hash is then signed with the private key. |
| 154 | |
| 155 | In order to verify the signature, use the :py:func:`rsa.verify` |
| 156 | function. |
| 157 | |
| 158 | >>> message = 'Go left at the blue tree' |
| 159 | >>> rsa.verify(message, signature, pubkey) |
| 160 | |
| 161 | Modify the message, and the signature is no longer valid and a |
| 162 | :py:class:`rsa.pkcs1.VerificationError` is thrown: |
| 163 | |
| 164 | >>> message = 'Go right at the blue tree' |
| 165 | >>> rsa.verify(message, signature, pubkey) |
| 166 | Traceback (most recent call last): |
| 167 | File "<stdin>", line 1, in <module> |
| 168 | File "/home/sybren/workspace/python-rsa/rsa/pkcs1.py", line 289, in verify |
| 169 | raise VerificationError('Verification failed') |
| 170 | rsa.pkcs1.VerificationError: Verification failed |
| 171 | |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame^] | 172 | .. warning:: |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 173 | |
| 174 | Never display the stack trace of a |
| 175 | :py:class:`rsa.pkcs1.VerificationError` exception. It shows where |
| 176 | in the code the exception occurred, and thus leaks information |
| 177 | about the key. It's only a tiny bit of information, but every bit |
| 178 | makes cracking the keys easier. |
| 179 | |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 180 | Instead of a message you can also call :py:func:`rsa.sign` and |
| 181 | :py:func:`rsa.verify` with a :py:class:`file`-like object. If the |
| 182 | message object has a ``read(int)`` method it is assumed to be a file. |
| 183 | In that case the file is hashed in 1024-byte blocks at the time. |
| 184 | |
| 185 | >>> with open('somefile', 'rb') as msgfile: |
| 186 | ... signature = rsa.sign(msgfile, privkey, 'SHA-1') |
| 187 | |
| 188 | >>> with open('somefile', 'rb') as msgfile: |
| 189 | ... rsa.verify(msgfile, signature, pubkey) |
| 190 | |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 191 | |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame^] | 192 | .. _bigfiles: |
| 193 | |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 194 | Working with big files |
| 195 | -------------------------------------------------- |
| 196 | |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 197 | RSA can only encrypt messages that are smaller than the key. A couple |
| 198 | of bytes are lost on random padding, and the rest is available for the |
| 199 | message itself. For example, a 512-bit key can encode a 53-byte |
| 200 | message (512 bit = 64 bytes, 11 bytes are used for random padding and |
| 201 | other stuff). |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 202 | |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 203 | How it usually works |
| 204 | ++++++++++++++++++++++++++++++++++++++++ |
| 205 | |
| 206 | The most common way to use RSA with larger files uses a block cypher |
| 207 | like AES or DES3 to encrypt the file with a random key, then encrypt |
| 208 | the random key with RSA. You would send the encrypted file along with |
| 209 | the encrypted key to the recipient. The complete flow is: |
| 210 | |
| 211 | #. Generate a random key |
| 212 | |
| 213 | >>> import rsa.randnum |
| 214 | >>> aes_key = rsa.randnum.read_random_bits(128) |
| 215 | |
| 216 | #. Use that key to encrypt the file with AES. |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame^] | 217 | #. :py:func:`Encrypt <rsa.encrypt>` the AES key with RSA |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 218 | |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame^] | 219 | >>> encrypted_aes_key = rsa.encrypt(aes_key, public_rsa_key) |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 220 | |
| 221 | #. Send the encrypted file together with ``encrypted_aes_key`` |
| 222 | #. The recipient now reverses this process to obtain the encrypted |
| 223 | file. |
| 224 | |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame^] | 225 | .. note:: |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 226 | |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame^] | 227 | The Python-RSA module does not contain functionality to do the AES |
| 228 | encryption for you. |
| 229 | |
| 230 | Only using Python-RSA: the VARBLOCK format |
| 231 | +++++++++++++++++++++++++++++++++++++++++++ |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 232 | |
| 233 | 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^] | 234 | versions of Python-RSA included functionality to encrypt large files |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 235 | with just RSA, and so does this version. The format has been improved, |
| 236 | though. |
| 237 | |
| 238 | Encrypting works as follows: the input file is split into blocks that |
| 239 | are just large enough to encrypt with your RSA key. Every block is |
| 240 | 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^] | 241 | the output file. This file format is called the :ref:`VARBLOCK |
| 242 | <VARBLOCK>` format. |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 243 | |
| 244 | Decrypting works in reverse. The encrypted file is separated into |
| 245 | encrypted blocks. Those are decrypted, and assembled into the original |
| 246 | file. |
| 247 | |
| 248 | .. note:: |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame^] | 249 | |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 250 | The file will get larger after encryption, as each encrypted block |
| 251 | has 8 bytes of random padding and 3 more bytes of overhead. |
| 252 | |
| 253 | Since these encryption/decryption functions are potentially called on |
| 254 | very large files, they use another approach. Where the regular |
| 255 | functions store the message in memory in its entirety, these functions |
| 256 | work on one block at the time. As a result, you should call them with |
| 257 | :py:class:`file`-like objects as the parameters. |
| 258 | |
| 259 | Before using we of course need a keypair: |
| 260 | |
| 261 | >>> import rsa |
| 262 | >>> (pub_key, priv_key) = rsa.newkeys(512) |
| 263 | |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame^] | 264 | Encryption works on file handles using the |
| 265 | :py:func:`rsa.bigfile.encrypt_bigfile` function: |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 266 | |
| 267 | >>> from rsa.bigfile import * |
| 268 | >>> with open('inputfile', 'rb') as infile, open('outputfile', 'wb') as outfile: |
| 269 | ... encrypt_bigfile(infile, outfile, pub_key) |
| 270 | |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame^] | 271 | As does decryption using the :py:func:`rsa.bigfile.decrypt_bigfile` |
| 272 | function: |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 273 | |
| 274 | >>> from rsa.bigfile import * |
| 275 | >>> with open('inputfile', 'rb') as infile, open('outputfile', 'wb') as outfile: |
| 276 | ... decrypt_bigfile(infile, outfile, priv_key) |
| 277 | |
| 278 | .. note:: |
Sybren A. Stüvel | dbea213 | 2011-08-03 13:31:57 +0200 | [diff] [blame^] | 279 | |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 280 | :py:func:`rsa.sign` and :py:func:`rsa.verify` work on arbitrarily |
| 281 | long files, so they do not have a "bigfile" equivalent. |
Sybren A. Stüvel | aa28c04 | 2011-07-30 23:48:00 +0200 | [diff] [blame] | 282 | |
Sybren A. Stüvel | c1c455d | 2011-08-01 23:04:30 +0200 | [diff] [blame] | 283 | |