blob: 336d2acb22d7802db9cfd603b88e2bfcae10d73e [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 Gaynorc72e63f2013-09-09 21:44:26 -070032
33The purpose of these policies is to minimize the chances we merge a change
34which jeopardizes our users' security.
35
Alex Gaynor99b69d92013-10-19 17:52:58 -070036If you believe you've identified a security issue in ``cryptography``, please
37follow the directions on the :doc:`security page </security>`.
Alex Gaynorc72e63f2013-09-09 21:44:26 -070038
39Code
40----
41
42When in doubt, refer to `PEP 8`_ for Python code.
43
44Every code file must start with the boilerplate notice of the Apache License.
45Additionally, every Python code file must contain
46
47.. code-block:: python
48
49 from __future__ import absolute_import, division, print_function
50
Alex Gaynore21b0b22013-12-12 12:39:05 -080051API Considerations
52~~~~~~~~~~~~~~~~~~
53
54Most projects' APIs are designed with a philosophy of "make easy things easy,
55and make hard things possible". One of the perils of writing cryptographic code
Alex Gaynor95243f52013-12-21 19:37:24 -080056is that secure code looks just like insecure code, and its results are almost
Alex Gaynor15cf6b92013-12-21 19:22:39 -080057always indistinguishable. As a result ``cryptography`` has, as a design
58philosophy: "make it hard to do insecure things". Here are a few strategies for
59API design which should be both followed, and should inspire other API choices:
Alex Gaynore21b0b22013-12-12 12:39:05 -080060
61If it is incorrect to ignore the result of a method, it should raise an
62exception, and not return a boolean ``True``/``False`` flag. For example, a
63method to verify a signature should raise ``InvalidSignature``, and not return
64whether the signature was valid.
65
66.. code-block:: python
67
68 # This is bad.
69 def verify(sig):
70 # ...
71 return is_valid
72
73 # Good!
74 def verify(sig):
75 # ...
76 if not is_valid:
77 raise InvalidSignature
78
Alex Gaynor6955ea32013-12-21 19:26:19 -080079Every recipe should include a version or algorithmic marker of some sort in its
80output in order to allow transparent upgrading of the algorithms in use, as
81the algorithms or parameters needed to achieve a given security margin evolve.
82
Alex Gaynore21b0b22013-12-12 12:39:05 -080083APIs at the :doc:`/hazmat/primitives/index` layer should always take an
84explicit backend, APIs at the recipes layer should automatically use the
Alex Gaynorf8796b12013-12-13 20:28:55 -080085:func:`~cryptography.hazmat.backends.default_backend`, but optionally allow
Alex Gaynor2a5b4a82013-12-12 17:53:08 -080086specifying a different backend.
Alex Gaynore21b0b22013-12-12 12:39:05 -080087
Alex Gaynore6466a52013-10-18 14:53:04 -070088C bindings
Alex Gaynor5246e2d2013-12-12 12:23:18 -080089~~~~~~~~~~
Alex Gaynore6466a52013-10-18 14:53:04 -070090
91When binding C code with ``cffi`` we have our own style guide, it's pretty
92simple.
93
94Don't name parameters:
95
96.. code-block:: c
97
98 // Good
99 long f(long);
100 // Bad
101 long f(long x);
102
Paul Kehrer3ed80ba2013-10-19 20:00:26 -0500103...unless they're inside a struct:
104
105.. code-block:: c
106
107 struct my_struct {
108 char *name;
109 int number;
110 ...;
111 };
112
Paul Kehrer047dab82013-12-27 16:45:52 -0600113Include ``void`` if the function takes no arguments:
Alex Gaynore6466a52013-10-18 14:53:04 -0700114
115.. code-block:: c
116
117 // Good
Alex Gaynore6466a52013-10-18 14:53:04 -0700118 long f(void);
Paul Kehrer047dab82013-12-27 16:45:52 -0600119 // Bad
120 long f();
Alex Gaynore6466a52013-10-18 14:53:04 -0700121
122Wrap lines at 80 characters like so:
123
124.. code-block:: c
125
126 // Pretend this went to 80 characters
127 long f(long, long,
128 int *)
129
Alex Gaynor1e8744a2013-10-18 14:57:18 -0700130Include a space after commas between parameters:
131
132.. code-block:: c
133
134 // Good
135 long f(int, char *)
136 // Bad
137 long f(int,char *)
138
Paul Kehrerccd9c002013-12-27 20:25:06 -0600139Values set by #define should be assigned the appropriate type. If you see
140this:
141
142.. code-block:: c
143
144 #define SOME_INTEGER 0x0;
145 #define SOME_UINTEGER (unsigned int)0x0001;
146 #define SOME_STRING "hello";
147
148...it should be added to the bindings like so:
149
150.. code-block:: c
151
152 static const int SOME_INTEGER;
153 static const unsigned int SOME_UINTEGER;
154 static char *const SOME_STRING;
155
Alex Gaynorc72e63f2013-09-09 21:44:26 -0700156Documentation
157-------------
158
159All features should be documented with prose.
160
161Docstrings should be written like this:
162
163.. code-block:: python
164
165 def some_function(some_arg):
166 """
167 Does some things.
168
169 :param some_arg: Some argument.
170 """
171
172So, specifically:
173
Alex Gaynor05a190e2013-10-29 17:11:25 -0700174* Always use three double quotes.
175* Put the three double quotes on their own line.
176* No blank line at the end.
177* Use Sphinx parameter/attribute documentation `syntax`_.
178
Alex Gaynora6596882013-11-13 12:54:03 -0800179Because of the inherent challenges in implementing correct cryptographic
Alex Gaynore9d64d72013-11-13 10:28:01 -0800180systems, we want to make our documentation point people in the right directions
181as much as possible. To that end:
182
183* When documenting a generic interface, use a strong algorithm in examples.
184 (e.g. when showing a hashing example, don't use
Alex Gaynor15cf6b92013-12-21 19:22:39 -0800185 :class:`~cryptography.hazmat.primitives.hashes.MD5`)
Alex Gaynor5cbab0c2013-11-13 11:55:57 -0800186* When giving prescriptive advice, always provide references and supporting
Alex Gaynore9d64d72013-11-13 10:28:01 -0800187 material.
Alex Gaynord118c912013-11-13 11:56:49 -0800188* When there is real disagreement between cryptographic experts, represent both
Alex Gaynor54e04002013-11-15 16:44:41 -0800189 sides of the argument and describe the trade-offs clearly.
Alex Gaynore9d64d72013-11-13 10:28:01 -0800190
Alex Gaynor05a190e2013-10-29 17:11:25 -0700191When documenting a new module in the ``hazmat`` package, its documentation
192should begin with the "Hazardous Materials" warning:
193
194.. code-block:: rest
195
196 .. hazmat::
Alex Gaynorc72e63f2013-09-09 21:44:26 -0700197
Alex Gaynor953ebf82013-12-08 10:28:30 -0800198When referring to a hypothetical individual (such as "a person receiving an
199encrypted message") use gender neutral pronouns (they/them/their).
200
Richard Wall40cde822013-10-01 20:20:15 +0100201Development Environment
202-----------------------
Richard Wall0d9bb142013-10-01 16:17:24 +0100203
204Working on ``cryptography`` requires the installation of a small number of
Alex Gaynor166cbd32013-10-01 13:30:29 -0700205development dependencies. These are listed in ``dev-requirements.txt`` and they
206can be installed in a `virtualenv`_ using `pip`_. Once you've installed the
207dependencies, install ``cryptography`` in ``editable`` mode. For example:
Richard Wall0d9bb142013-10-01 16:17:24 +0100208
Alex Gaynorae5c9072013-10-06 11:04:08 -0700209.. code-block:: console
Richard Wall0d9bb142013-10-01 16:17:24 +0100210
Alex Gaynor7587ded2013-10-06 12:14:05 -0700211 $ # Create a virtualenv and activate it
Richard Wall7d4ca1e2013-10-01 21:10:45 +0100212 $ pip install --requirement dev-requirements.txt
213 $ pip install --editable .
Richard Wall0d9bb142013-10-01 16:17:24 +0100214
215You are now ready to run the tests and build the documentation.
Richard Wall0d9bb142013-10-01 16:17:24 +0100216
Richard Wall40cde822013-10-01 20:20:15 +0100217Running Tests
Alex Gaynor5246e2d2013-12-12 12:23:18 -0800218~~~~~~~~~~~~~
Richard Wall0d9bb142013-10-01 16:17:24 +0100219
Alex Gaynor166cbd32013-10-01 13:30:29 -0700220``cryptography`` unit tests are found in the ``tests/`` directory and are
221designed to be run using `pytest`_. `pytest`_ will discover the tests
222automatically, so all you have to do is:
Richard Wall0d9bb142013-10-01 16:17:24 +0100223
Alex Gaynorae5c9072013-10-06 11:04:08 -0700224.. code-block:: console
Richard Wall0d9bb142013-10-01 16:17:24 +0100225
Richard Wall7d4ca1e2013-10-01 21:10:45 +0100226 $ py.test
Richard Wall0d9bb142013-10-01 16:17:24 +0100227 ...
Alex Gaynor15cf6b92013-12-21 19:22:39 -0800228 62746 passed in 220.43 seconds
Richard Wall0d9bb142013-10-01 16:17:24 +0100229
230This runs the tests with the default Python interpreter.
231
232You can also verify that the tests pass on other supported Python interpreters.
Richard Wallc3d1eb52013-10-01 16:29:07 +0100233For this we use `tox`_, which will automatically create a `virtualenv`_ for
Richard Wall40cde822013-10-01 20:20:15 +0100234each supported Python version and run the tests. For example:
Richard Wall0d9bb142013-10-01 16:17:24 +0100235
Alex Gaynorae5c9072013-10-06 11:04:08 -0700236.. code-block:: console
Richard Wall0d9bb142013-10-01 16:17:24 +0100237
Richard Wall7d4ca1e2013-10-01 21:10:45 +0100238 $ tox
Richard Wall0d9bb142013-10-01 16:17:24 +0100239 ...
Richard Wall40cde822013-10-01 20:20:15 +0100240 ERROR: py26: InterpreterNotFound: python2.6
241 py27: commands succeeded
242 ERROR: pypy: InterpreterNotFound: pypy
243 ERROR: py32: InterpreterNotFound: python3.2
244 py33: commands succeeded
245 docs: commands succeeded
246 pep8: commands succeeded
Richard Wall0d9bb142013-10-01 16:17:24 +0100247
Alex Gaynor166cbd32013-10-01 13:30:29 -0700248You may not have all the required Python versions installed, in which case you
249will see one or more ``InterpreterNotFound`` errors.
Richard Wall0d9bb142013-10-01 16:17:24 +0100250
251Building Documentation
Alex Gaynor5246e2d2013-12-12 12:23:18 -0800252~~~~~~~~~~~~~~~~~~~~~~
Richard Wall0d9bb142013-10-01 16:17:24 +0100253
Alex Gaynor166cbd32013-10-01 13:30:29 -0700254``cryptography`` documentation is stored in the ``docs/`` directory. It is
255written in `reStructured Text`_ and rendered using `Sphinx`_.
Richard Wall0d9bb142013-10-01 16:17:24 +0100256
Richard Wall7d4ca1e2013-10-01 21:10:45 +0100257Use `tox`_ to build the documentation. For example:
Richard Wall0d9bb142013-10-01 16:17:24 +0100258
Alex Gaynorae5c9072013-10-06 11:04:08 -0700259.. code-block:: console
Richard Wall0d9bb142013-10-01 16:17:24 +0100260
Richard Wall7d4ca1e2013-10-01 21:10:45 +0100261 $ tox -e docs
Richard Wall0d9bb142013-10-01 16:17:24 +0100262 ...
Richard Wallc3d1eb52013-10-01 16:29:07 +0100263 docs: commands succeeded
Richard Wall0d9bb142013-10-01 16:17:24 +0100264 congratulations :)
265
Alex Gaynor15cf6b92013-12-21 19:22:39 -0800266The HTML documentation index can now be found at
267``docs/_build/html/index.html``.
Richard Wall40cde822013-10-01 20:20:15 +0100268
Alex Gaynorc72e63f2013-09-09 21:44:26 -0700269
Donald Stufft85707942013-10-04 23:55:27 -0400270.. _`GitHub`: https://github.com/pyca/cryptography
Alex Gaynorc72e63f2013-09-09 21:44:26 -0700271.. _`our mailing list`: https://mail.python.org/mailman/listinfo/cryptography-dev
272.. _`PEP 8`: http://www.peps.io/8/
273.. _`syntax`: http://sphinx-doc.org/domains.html#info-field-lists
Richard Wallc3d1eb52013-10-01 16:29:07 +0100274.. _`pytest`: https://pypi.python.org/pypi/pytest
275.. _`tox`: https://pypi.python.org/pypi/tox
276.. _`virtualenv`: https://pypi.python.org/pypi/virtualenv
277.. _`pip`: https://pypi.python.org/pypi/pip
278.. _`sphinx`: https://pypi.python.org/pypi/sphinx
Alex Gaynora05358d2013-11-06 11:01:22 -0800279.. _`reStructured Text`: http://sphinx-doc.org/rest.html