Sybren A. Stüvel | aa28c04 | 2011-07-30 23:48:00 +0200 | [diff] [blame] | 1 | Usage |
| 2 | ================================================== |
| 3 | |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 4 | This section describes the usage of the Python-RSA module. |
Sybren A. Stüvel | aa28c04 | 2011-07-30 23:48:00 +0200 | [diff] [blame] | 5 | |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 6 | Before you can use RSA you need keys. You will receive a private key |
| 7 | and 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üvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 14 | The public key is used for encypting a message such that it can only |
| 15 | be read by the owner of the private key. As such it's also referred to |
| 16 | as the *encryption key*. Decrypting a message can only be done using |
| 17 | the private key, hence it's also called the *decryption key*. |
| 18 | |
| 19 | The private key is used for signing a message. With this signature and |
| 20 | the public key, the receiver can verifying that a message was signed |
| 21 | by the owner of the private key, and that the message was not modified |
| 22 | after signing. |
| 23 | |
| 24 | Generating keys |
| 25 | -------------------------------------------------- |
| 26 | |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 27 | You can use the :py:func:`rsa.newkeys` function to create a keypair: |
| 28 | |
| 29 | >>> (pubkey, privkey) = rsa.newkeys(512) |
| 30 | |
| 31 | Alternatively 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üvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 37 | |
| 38 | Generating a keypair may take a long time, depending on the number of |
| 39 | bits required. The number of bits determines the cryptographic |
| 40 | strength of the key, as well as the size of the message you can |
| 41 | encrypt. If you don't mind having a slightly smaller key than you |
| 42 | requested, you can pass ``accurate=False`` to speed up the key |
| 43 | generation process. |
| 44 | |
| 45 | These are some timings from my netbook (Linux 2.6, 1.6 GHz Intel Atom |
| 46 | N270 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üvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 70 | |
| 71 | Encryption and decryption |
| 72 | -------------------------------------------------- |
| 73 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 74 | To 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 |
| 76 | that 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üvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 82 | >>> (bob_pub, bob_priv) = rsa.newkeys(512) |
| 83 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 84 | #. Alice writes a message |
| 85 | |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 86 | >>> message = 'hello Bob!' |
| 87 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 88 | #. Alice encrypts the message using Bob's public key, and sends the |
| 89 | encrypted message. |
| 90 | |
Sybren A. Stüvel | db34825 | 2011-07-31 19:22:47 +0200 | [diff] [blame] | 91 | >>> crypto = rsa.encrypt(message, bob_pub) |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 92 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 93 | #. Bob receives the message, and decrypts it with his private key. |
| 94 | |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 95 | >>> message = rsa.decrypt(crypto, bob_priv) |
| 96 | >>> print message |
| 97 | hello Bob! |
| 98 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 99 | Since Bob kept his private key *private*, Alice can be sure that he is |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 100 | the 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üvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 106 | |
| 107 | |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 108 | RSA can only encrypt messages that are smaller than the key. A couple |
| 109 | of bytes are lost on random padding, and the rest is available for the |
| 110 | message itself. For example, a 512-bit key can encode a 53-byte |
| 111 | message (512 bit = 64 bytes, 11 bytes are used for random padding and |
| 112 | other stuff). |
| 113 | |
| 114 | See `Working with big files`_ for information on how to work with |
| 115 | larger files. |
| 116 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 117 | Low-level operations |
| 118 | ++++++++++++++++++++++++++++++ |
| 119 | |
| 120 | The core RSA algorithm operates on large integers. These operations |
| 121 | are considered low-level and are supported by the |
| 122 | :py:func:`rsa.core.encrypt_int` and :py:func:`rsa.core.decrypt_int` |
| 123 | functions. |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 124 | |
| 125 | Signing and verification |
| 126 | -------------------------------------------------- |
| 127 | |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 128 | You 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 | |
| 135 | This hashes the message using SHA-1. Other hash methods are also |
| 136 | possible, check the :py:func:`rsa.sign` function documentation for |
| 137 | details. The hash is then signed with the private key. |
| 138 | |
| 139 | In order to verify the signature, use the :py:func:`rsa.verify` |
| 140 | function. |
| 141 | |
| 142 | >>> message = 'Go left at the blue tree' |
| 143 | >>> rsa.verify(message, signature, pubkey) |
| 144 | |
| 145 | Modify 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üvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 164 | Instead 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 |
| 166 | message object has a ``read(int)`` method it is assumed to be a file. |
| 167 | In 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üvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 175 | |
| 176 | Working with big files |
| 177 | -------------------------------------------------- |
| 178 | |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 179 | RSA can only encrypt messages that are smaller than the key. A couple |
| 180 | of bytes are lost on random padding, and the rest is available for the |
| 181 | message itself. For example, a 512-bit key can encode a 53-byte |
| 182 | message (512 bit = 64 bytes, 11 bytes are used for random padding and |
| 183 | other stuff). |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 184 | |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 185 | How it usually works |
| 186 | ++++++++++++++++++++++++++++++++++++++++ |
| 187 | |
| 188 | The most common way to use RSA with larger files uses a block cypher |
| 189 | like AES or DES3 to encrypt the file with a random key, then encrypt |
| 190 | the random key with RSA. You would send the encrypted file along with |
| 191 | the 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 | |
| 208 | Only using Python-RSA |
| 209 | ++++++++++++++++++++++++++++++++++++++++ |
| 210 | |
| 211 | As far as we know, there is no pure-Python AES encryption. Previous |
| 212 | versions of Python-RSA included functionality to encrypt large files, |
| 213 | with just RSA, and so does this version. The format has been improved, |
| 214 | though. |
| 215 | |
| 216 | Encrypting works as follows: the input file is split into blocks that |
| 217 | are just large enough to encrypt with your RSA key. Every block is |
| 218 | then encrypted using RSA, and the encrypted blocks are assembled into |
Sybren A. Stüvel | c1c455d | 2011-08-01 23:04:30 +0200 | [diff] [blame] | 219 | the output file. This file format is called the VARBLOCK format. |
Sybren A. Stüvel | b6c04dd | 2011-08-01 21:37:02 +0200 | [diff] [blame] | 220 | |
| 221 | Decrypting works in reverse. The encrypted file is separated into |
| 222 | encrypted blocks. Those are decrypted, and assembled into the original |
| 223 | file. |
| 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 | |
| 229 | Since these encryption/decryption functions are potentially called on |
| 230 | very large files, they use another approach. Where the regular |
| 231 | functions store the message in memory in its entirety, these functions |
| 232 | work on one block at the time. As a result, you should call them with |
| 233 | :py:class:`file`-like objects as the parameters. |
| 234 | |
| 235 | Before using we of course need a keypair: |
| 236 | |
| 237 | >>> import rsa |
| 238 | >>> (pub_key, priv_key) = rsa.newkeys(512) |
| 239 | |
| 240 | Encryption 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 | |
| 246 | As 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üvel | aa28c04 | 2011-07-30 23:48:00 +0200 | [diff] [blame] | 255 | |
Sybren A. Stüvel | c1c455d | 2011-08-01 23:04:30 +0200 | [diff] [blame] | 256 | |