blob: 1595cd430313d9fd78d918f158a299b021b67522 [file] [log] [blame]
Alex Gaynorc72e63f2013-09-09 21:44:26 -07001Contributing
2============
3
4Process
5-------
6
7As an open source project, ``cryptography`` welcomes contributions of all
8forms. These can include:
9
10* Bug reports and feature requests
11* Pull requests for both code and documentation
12* Patch reviews
13
Alex Gaynor2c67c822013-09-09 23:44:13 -070014You can file bugs and submit pull requests on `GitHub`_. To discuss larger
Alex Gaynorc72e63f2013-09-09 21:44:26 -070015changes you can start a conversation on `our mailing list`_.
16
17Because cryptography is so complex, and the implications of getting it wrong so
18devastating, ``cryptography`` has a strict code review policy:
19
20* Patches must *never* be pushed directly to ``master``, all changes (even the
21 most trivial typo fixes!) must be submitted as a pull request.
22* A committer may *never* merge their own pull request, a second party must
Alex Gaynore6466a52013-10-18 14:53:04 -070023 merge their changes. If multiple people work on a pull request, it must be
24 merged by someone who did not work on it.
Alex Gaynorc72e63f2013-09-09 21:44:26 -070025* A patch which breaks tests, or introduces regressions by changing or removing
26 existing tests should not be merged. Tests must always be passing on
27 ``master``.
28* If somehow the tests get into a failing state on ``master`` (such as by a
29 backwards incompatible release of a dependency) no pull requests may be
30 merged until this is rectified.
Alex Gaynorf3f00182013-12-13 19:22:33 -080031* All merged patches must have 100% test coverage.
Alex Gaynor3f230402014-01-08 09:21:57 -080032* New features and significant bug fixes should be documented in the
33 :doc:`/changelog`.
Alex Gaynorc72e63f2013-09-09 21:44:26 -070034
35The purpose of these policies is to minimize the chances we merge a change
36which jeopardizes our users' security.
37
Alex Gaynor99b69d92013-10-19 17:52:58 -070038If you believe you've identified a security issue in ``cryptography``, please
39follow the directions on the :doc:`security page </security>`.
Alex Gaynorc72e63f2013-09-09 21:44:26 -070040
41Code
42----
43
44When in doubt, refer to `PEP 8`_ for Python code.
45
46Every code file must start with the boilerplate notice of the Apache License.
47Additionally, every Python code file must contain
48
49.. code-block:: python
50
51 from __future__ import absolute_import, division, print_function
52
Alex Gaynore21b0b22013-12-12 12:39:05 -080053API Considerations
54~~~~~~~~~~~~~~~~~~
55
56Most projects' APIs are designed with a philosophy of "make easy things easy,
57and make hard things possible". One of the perils of writing cryptographic code
Alex Gaynor95243f52013-12-21 19:37:24 -080058is that secure code looks just like insecure code, and its results are almost
Alex Gaynor15cf6b92013-12-21 19:22:39 -080059always indistinguishable. As a result ``cryptography`` has, as a design
60philosophy: "make it hard to do insecure things". Here are a few strategies for
61API design which should be both followed, and should inspire other API choices:
Alex Gaynore21b0b22013-12-12 12:39:05 -080062
Alex Gaynor61137bc2014-01-27 16:37:04 -080063If it is necessary to compare a user provided value with a computed value (for
64example, verifying a signature), there should be an API provided which performs
65the verification in a secure way (for example, using a constant time
66comparison), rather than requiring the user to perform the comparison
67themselves.
Alex Gaynor24eb6772014-01-27 16:15:54 -080068
Alex Gaynore21b0b22013-12-12 12:39:05 -080069If it is incorrect to ignore the result of a method, it should raise an
70exception, and not return a boolean ``True``/``False`` flag. For example, a
71method to verify a signature should raise ``InvalidSignature``, and not return
72whether the signature was valid.
73
74.. code-block:: python
75
76 # This is bad.
77 def verify(sig):
78 # ...
79 return is_valid
80
81 # Good!
82 def verify(sig):
83 # ...
84 if not is_valid:
85 raise InvalidSignature
86
Alex Gaynor6955ea32013-12-21 19:26:19 -080087Every recipe should include a version or algorithmic marker of some sort in its
88output in order to allow transparent upgrading of the algorithms in use, as
89the algorithms or parameters needed to achieve a given security margin evolve.
90
Alex Gaynore21b0b22013-12-12 12:39:05 -080091APIs at the :doc:`/hazmat/primitives/index` layer should always take an
92explicit backend, APIs at the recipes layer should automatically use the
Alex Gaynorf8796b12013-12-13 20:28:55 -080093:func:`~cryptography.hazmat.backends.default_backend`, but optionally allow
Alex Gaynor2a5b4a82013-12-12 17:53:08 -080094specifying a different backend.
Alex Gaynore21b0b22013-12-12 12:39:05 -080095
Alex Gaynore6466a52013-10-18 14:53:04 -070096C bindings
Alex Gaynor5246e2d2013-12-12 12:23:18 -080097~~~~~~~~~~
Alex Gaynore6466a52013-10-18 14:53:04 -070098
99When binding C code with ``cffi`` we have our own style guide, it's pretty
100simple.
101
102Don't name parameters:
103
104.. code-block:: c
105
106 // Good
107 long f(long);
108 // Bad
109 long f(long x);
110
Paul Kehrer3ed80ba2013-10-19 20:00:26 -0500111...unless they're inside a struct:
112
113.. code-block:: c
114
115 struct my_struct {
116 char *name;
117 int number;
118 ...;
119 };
120
Paul Kehrer047dab82013-12-27 16:45:52 -0600121Include ``void`` if the function takes no arguments:
Alex Gaynore6466a52013-10-18 14:53:04 -0700122
123.. code-block:: c
124
125 // Good
Alex Gaynore6466a52013-10-18 14:53:04 -0700126 long f(void);
Paul Kehrer047dab82013-12-27 16:45:52 -0600127 // Bad
128 long f();
Alex Gaynore6466a52013-10-18 14:53:04 -0700129
130Wrap lines at 80 characters like so:
131
132.. code-block:: c
133
134 // Pretend this went to 80 characters
135 long f(long, long,
136 int *)
137
Alex Gaynor1e8744a2013-10-18 14:57:18 -0700138Include a space after commas between parameters:
139
140.. code-block:: c
141
142 // Good
143 long f(int, char *)
144 // Bad
145 long f(int,char *)
146
Paul Kehrer745ee072013-12-27 20:42:54 -0600147Values set by ``#define`` should be assigned the appropriate type. If you see
Paul Kehrerccd9c002013-12-27 20:25:06 -0600148this:
149
150.. code-block:: c
151
Alex Stapleton9020b482013-12-28 16:28:59 +0000152 #define SOME_INTEGER_LITERAL 0x0;
153 #define SOME_UNSIGNED_INTEGER_LITERAL 0x0001U;
154 #define SOME_STRING_LITERAL "hello";
Paul Kehrerccd9c002013-12-27 20:25:06 -0600155
156...it should be added to the bindings like so:
157
158.. code-block:: c
159
Alex Stapleton9020b482013-12-28 16:28:59 +0000160 static const int SOME_INTEGER_LITERAL;
161 static const unsigned int SOME_UNSIGNED_INTEGER_LITERAL;
162 static const char *const SOME_STRING_LITERAL;
Paul Kehrerccd9c002013-12-27 20:25:06 -0600163
Alex Gaynorc72e63f2013-09-09 21:44:26 -0700164Documentation
165-------------
166
167All features should be documented with prose.
168
169Docstrings should be written like this:
170
171.. code-block:: python
172
173 def some_function(some_arg):
174 """
175 Does some things.
176
177 :param some_arg: Some argument.
178 """
179
180So, specifically:
181
Alex Gaynor05a190e2013-10-29 17:11:25 -0700182* Always use three double quotes.
183* Put the three double quotes on their own line.
184* No blank line at the end.
185* Use Sphinx parameter/attribute documentation `syntax`_.
186
Alex Gaynora6596882013-11-13 12:54:03 -0800187Because of the inherent challenges in implementing correct cryptographic
Alex Gaynore9d64d72013-11-13 10:28:01 -0800188systems, we want to make our documentation point people in the right directions
189as much as possible. To that end:
190
191* When documenting a generic interface, use a strong algorithm in examples.
192 (e.g. when showing a hashing example, don't use
Alex Gaynor15cf6b92013-12-21 19:22:39 -0800193 :class:`~cryptography.hazmat.primitives.hashes.MD5`)
Alex Gaynor5cbab0c2013-11-13 11:55:57 -0800194* When giving prescriptive advice, always provide references and supporting
Alex Gaynore9d64d72013-11-13 10:28:01 -0800195 material.
Alex Gaynord118c912013-11-13 11:56:49 -0800196* When there is real disagreement between cryptographic experts, represent both
Alex Gaynor54e04002013-11-15 16:44:41 -0800197 sides of the argument and describe the trade-offs clearly.
Alex Gaynore9d64d72013-11-13 10:28:01 -0800198
Alex Gaynor05a190e2013-10-29 17:11:25 -0700199When documenting a new module in the ``hazmat`` package, its documentation
200should begin with the "Hazardous Materials" warning:
201
202.. code-block:: rest
203
204 .. hazmat::
Alex Gaynorc72e63f2013-09-09 21:44:26 -0700205
Alex Gaynor953ebf82013-12-08 10:28:30 -0800206When referring to a hypothetical individual (such as "a person receiving an
207encrypted message") use gender neutral pronouns (they/them/their).
208
Richard Wall40cde822013-10-01 20:20:15 +0100209Development Environment
210-----------------------
Richard Wall0d9bb142013-10-01 16:17:24 +0100211
212Working on ``cryptography`` requires the installation of a small number of
Alex Gaynor166cbd32013-10-01 13:30:29 -0700213development dependencies. These are listed in ``dev-requirements.txt`` and they
214can be installed in a `virtualenv`_ using `pip`_. Once you've installed the
215dependencies, install ``cryptography`` in ``editable`` mode. For example:
Richard Wall0d9bb142013-10-01 16:17:24 +0100216
Alex Gaynorae5c9072013-10-06 11:04:08 -0700217.. code-block:: console
Richard Wall0d9bb142013-10-01 16:17:24 +0100218
Alex Gaynor7587ded2013-10-06 12:14:05 -0700219 $ # Create a virtualenv and activate it
Richard Wall7d4ca1e2013-10-01 21:10:45 +0100220 $ pip install --requirement dev-requirements.txt
221 $ pip install --editable .
Richard Wall0d9bb142013-10-01 16:17:24 +0100222
223You are now ready to run the tests and build the documentation.
Richard Wall0d9bb142013-10-01 16:17:24 +0100224
Richard Wall40cde822013-10-01 20:20:15 +0100225Running Tests
Alex Gaynor5246e2d2013-12-12 12:23:18 -0800226~~~~~~~~~~~~~
Richard Wall0d9bb142013-10-01 16:17:24 +0100227
Alex Gaynor166cbd32013-10-01 13:30:29 -0700228``cryptography`` unit tests are found in the ``tests/`` directory and are
229designed to be run using `pytest`_. `pytest`_ will discover the tests
230automatically, so all you have to do is:
Richard Wall0d9bb142013-10-01 16:17:24 +0100231
Alex Gaynorae5c9072013-10-06 11:04:08 -0700232.. code-block:: console
Richard Wall0d9bb142013-10-01 16:17:24 +0100233
Richard Wall7d4ca1e2013-10-01 21:10:45 +0100234 $ py.test
Richard Wall0d9bb142013-10-01 16:17:24 +0100235 ...
Alex Gaynor15cf6b92013-12-21 19:22:39 -0800236 62746 passed in 220.43 seconds
Richard Wall0d9bb142013-10-01 16:17:24 +0100237
238This runs the tests with the default Python interpreter.
239
240You can also verify that the tests pass on other supported Python interpreters.
Richard Wallc3d1eb52013-10-01 16:29:07 +0100241For this we use `tox`_, which will automatically create a `virtualenv`_ for
Richard Wall40cde822013-10-01 20:20:15 +0100242each supported Python version and run the tests. For example:
Richard Wall0d9bb142013-10-01 16:17:24 +0100243
Alex Gaynorae5c9072013-10-06 11:04:08 -0700244.. code-block:: console
Richard Wall0d9bb142013-10-01 16:17:24 +0100245
Richard Wall7d4ca1e2013-10-01 21:10:45 +0100246 $ tox
Richard Wall0d9bb142013-10-01 16:17:24 +0100247 ...
Richard Wall40cde822013-10-01 20:20:15 +0100248 ERROR: py26: InterpreterNotFound: python2.6
249 py27: commands succeeded
250 ERROR: pypy: InterpreterNotFound: pypy
251 ERROR: py32: InterpreterNotFound: python3.2
252 py33: commands succeeded
253 docs: commands succeeded
254 pep8: commands succeeded
Richard Wall0d9bb142013-10-01 16:17:24 +0100255
Alex Gaynor166cbd32013-10-01 13:30:29 -0700256You may not have all the required Python versions installed, in which case you
257will see one or more ``InterpreterNotFound`` errors.
Richard Wall0d9bb142013-10-01 16:17:24 +0100258
Paul Kehrer2502ce52014-01-18 09:32:47 -0600259
260Explicit Backend Selection
261~~~~~~~~~~~~~~~~~~~~~~~~~~
262
263While testing you may want to run tests against a subset of the backends that
264cryptography supports. Explicit backend selection can be done via the
265``--backend`` flag. This flag should be passed to ``py.test`` with a comma
266delimited list of backend names. To use it with ``tox`` you must pass it as
Paul Kehrerad4f6462014-01-20 09:36:57 -0600267``tox -- --backend=openssl``.
Paul Kehrer2502ce52014-01-18 09:32:47 -0600268
Richard Wall0d9bb142013-10-01 16:17:24 +0100269Building Documentation
Alex Gaynor5246e2d2013-12-12 12:23:18 -0800270~~~~~~~~~~~~~~~~~~~~~~
Richard Wall0d9bb142013-10-01 16:17:24 +0100271
Alex Gaynor166cbd32013-10-01 13:30:29 -0700272``cryptography`` documentation is stored in the ``docs/`` directory. It is
273written in `reStructured Text`_ and rendered using `Sphinx`_.
Richard Wall0d9bb142013-10-01 16:17:24 +0100274
Richard Wall7d4ca1e2013-10-01 21:10:45 +0100275Use `tox`_ to build the documentation. For example:
Richard Wall0d9bb142013-10-01 16:17:24 +0100276
Alex Gaynorae5c9072013-10-06 11:04:08 -0700277.. code-block:: console
Richard Wall0d9bb142013-10-01 16:17:24 +0100278
Richard Wall7d4ca1e2013-10-01 21:10:45 +0100279 $ tox -e docs
Richard Wall0d9bb142013-10-01 16:17:24 +0100280 ...
Richard Wallc3d1eb52013-10-01 16:29:07 +0100281 docs: commands succeeded
Richard Wall0d9bb142013-10-01 16:17:24 +0100282 congratulations :)
283
Alex Gaynor15cf6b92013-12-21 19:22:39 -0800284The HTML documentation index can now be found at
285``docs/_build/html/index.html``.
Richard Wall40cde822013-10-01 20:20:15 +0100286
Alex Gaynorc72e63f2013-09-09 21:44:26 -0700287
Donald Stufft85707942013-10-04 23:55:27 -0400288.. _`GitHub`: https://github.com/pyca/cryptography
Alex Gaynorc72e63f2013-09-09 21:44:26 -0700289.. _`our mailing list`: https://mail.python.org/mailman/listinfo/cryptography-dev
Alex Stapletonefdd8ac2014-02-03 15:20:30 +0000290.. _`PEP 8`: http://www.python.org/dev/peps/pep-0008/
Alex Gaynorc72e63f2013-09-09 21:44:26 -0700291.. _`syntax`: http://sphinx-doc.org/domains.html#info-field-lists
Richard Wallc3d1eb52013-10-01 16:29:07 +0100292.. _`pytest`: https://pypi.python.org/pypi/pytest
293.. _`tox`: https://pypi.python.org/pypi/tox
294.. _`virtualenv`: https://pypi.python.org/pypi/virtualenv
295.. _`pip`: https://pypi.python.org/pypi/pip
Alex Gaynor3c1eb122014-01-23 07:46:21 -0600296.. _`sphinx`: https://pypi.python.org/pypi/Sphinx
Alex Gaynora05358d2013-11-06 11:01:22 -0800297.. _`reStructured Text`: http://sphinx-doc.org/rest.html