blob: ff650c4f1b00196d17bf5ef3c4f60204d7ead3d9 [file] [log] [blame]
Alex Stapletonc5fffd32014-03-18 15:29:00 +00001Submitting patches
Paul Kehrer0839aa82014-02-11 22:36:51 -06002==================
3
4* Always make a new branch for your work.
5* Patches should be small to facilitate easier review. `Studies have shown`_
6 that review quality falls off as patch size grows. Sometimes this will result
7 in many small PRs to land a single large feature.
8* Larger changes should be discussed on `our mailing list`_ before submission.
9* New features and significant bug fixes should be documented in the
10 :doc:`/changelog`.
Alex Gaynorfda92472014-11-06 17:05:48 -030011* You must have legal permission to distribute any code you contribute to
12 ``cryptography``, and it must be available under both the BSD and Apache
13 Software License Version 2.0 licenses.
Paul Kehrer0839aa82014-02-11 22:36:51 -060014
15If you believe you've identified a security issue in ``cryptography``, please
16follow the directions on the :doc:`security page </security>`.
17
18Code
19----
20
Paul Kehrer5b6ce2a2014-02-24 20:16:10 -060021When in doubt, refer to :pep:`8` for Python code. You can check if your code
22meets our automated requirements by running ``flake8`` against it. If you've
23installed the development requirements this will automatically use our
24configuration. You can also run the ``tox`` job with ``tox -e pep8``.
Paul Kehrer0839aa82014-02-11 22:36:51 -060025
26`Write comments as complete sentences.`_
27
Alex Gaynor5951f462014-11-16 09:08:42 -080028Every code file must start with the boilerplate licensing notice:
29
30.. code-block:: python
31
32 # This file is dual licensed under the terms of the Apache License, Version
33 # 2.0, and the BSD License. See the LICENSE file in the root of this repository
34 # for complete details.
35
Paul Kehrer0839aa82014-02-11 22:36:51 -060036Additionally, every Python code file must contain
37
38.. code-block:: python
39
40 from __future__ import absolute_import, division, print_function
41
Alex Stapletonc5fffd32014-03-18 15:29:00 +000042API considerations
Paul Kehrer0839aa82014-02-11 22:36:51 -060043~~~~~~~~~~~~~~~~~~
44
45Most projects' APIs are designed with a philosophy of "make easy things easy,
46and make hard things possible". One of the perils of writing cryptographic code
47is that secure code looks just like insecure code, and its results are almost
48always indistinguishable. As a result ``cryptography`` has, as a design
49philosophy: "make it hard to do insecure things". Here are a few strategies for
50API design that should be both followed, and should inspire other API choices:
51
52If it is necessary to compare a user provided value with a computed value (for
53example, verifying a signature), there should be an API provided that performs
54the verification in a secure way (for example, using a constant time
55comparison), rather than requiring the user to perform the comparison
56themselves.
57
58If it is incorrect to ignore the result of a method, it should raise an
59exception, and not return a boolean ``True``/``False`` flag. For example, a
60method to verify a signature should raise ``InvalidSignature``, and not return
61whether the signature was valid.
62
63.. code-block:: python
64
65 # This is bad.
66 def verify(sig):
67 # ...
68 return is_valid
69
70 # Good!
71 def verify(sig):
72 # ...
73 if not is_valid:
74 raise InvalidSignature
75
76Every recipe should include a version or algorithmic marker of some sort in its
77output in order to allow transparent upgrading of the algorithms in use, as
78the algorithms or parameters needed to achieve a given security margin evolve.
79
80APIs at the :doc:`/hazmat/primitives/index` layer should always take an
81explicit backend, APIs at the recipes layer should automatically use the
82:func:`~cryptography.hazmat.backends.default_backend`, but optionally allow
83specifying a different backend.
84
85C bindings
86~~~~~~~~~~
87
Laurens Van Houtven0a1d9e12014-06-23 14:06:16 +020088More information on C bindings can be found in :doc:`the dedicated
89section of the documentation <c-bindings>`.
90
Paul Kehrer0839aa82014-02-11 22:36:51 -060091Tests
92-----
93
94All code changes must be accompanied by unit tests with 100% code coverage (as
95measured by the combined metrics across our build matrix).
96
97When implementing a new primitive or recipe ``cryptography`` requires that you
Paul Kehrer1681a692014-02-11 23:43:51 -060098provide a set of test vectors. See :doc:`/development/test-vectors` for more
99details.
Paul Kehrer0839aa82014-02-11 22:36:51 -0600100
101Documentation
102-------------
103
Paul Kehrer813b2942014-06-05 12:40:56 -0500104All features should be documented with prose in the ``docs`` section. To ensure
105it builds and passes `doc8`_ style checks you can run ``tox -e docs``.
Paul Kehrer0839aa82014-02-11 22:36:51 -0600106
107Because of the inherent challenges in implementing correct cryptographic
108systems, we want to make our documentation point people in the right directions
109as much as possible. To that end:
110
111* When documenting a generic interface, use a strong algorithm in examples.
112 (e.g. when showing a hashing example, don't use
113 :class:`~cryptography.hazmat.primitives.hashes.MD5`)
114* When giving prescriptive advice, always provide references and supporting
115 material.
116* When there is real disagreement between cryptographic experts, represent both
117 sides of the argument and describe the trade-offs clearly.
118
119When documenting a new module in the ``hazmat`` package, its documentation
120should begin with the "Hazardous Materials" warning:
121
122.. code-block:: rest
123
124 .. hazmat::
125
126When referring to a hypothetical individual (such as "a person receiving an
127encrypted message") use gender neutral pronouns (they/them/their).
128
129Docstrings are typically only used when writing abstract classes, but should
130be written like this if required:
131
132.. code-block:: python
133
134 def some_function(some_arg):
135 """
136 Does some things.
137
138 :param some_arg: Some argument.
139 """
140
141So, specifically:
142
143* Always use three double quotes.
144* Put the three double quotes on their own line.
145* No blank line at the end.
146* Use Sphinx parameter/attribute documentation `syntax`_.
147
148
149.. _`Write comments as complete sentences.`: http://nedbatchelder.com/blog/201401/comments_should_be_sentences.html
150.. _`syntax`: http://sphinx-doc.org/domains.html#info-field-lists
151.. _`Studies have shown`: http://www.ibm.com/developerworks/rational/library/11-proven-practices-for-peer-review/
152.. _`our mailing list`: https://mail.python.org/mailman/listinfo/cryptography-dev
Paul Kehrer813b2942014-06-05 12:40:56 -0500153.. _`doc8`: https://github.com/stackforge/doc8