blob: 54005d27cb9d7db8bf79c4229619889d6b759c5b [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
28Every code file must start with the boilerplate notice of the Apache License.
29Additionally, every Python code file must contain
30
31.. code-block:: python
32
33 from __future__ import absolute_import, division, print_function
34
Alex Stapletonc5fffd32014-03-18 15:29:00 +000035API considerations
Paul Kehrer0839aa82014-02-11 22:36:51 -060036~~~~~~~~~~~~~~~~~~
37
38Most projects' APIs are designed with a philosophy of "make easy things easy,
39and make hard things possible". One of the perils of writing cryptographic code
40is that secure code looks just like insecure code, and its results are almost
41always indistinguishable. As a result ``cryptography`` has, as a design
42philosophy: "make it hard to do insecure things". Here are a few strategies for
43API design that should be both followed, and should inspire other API choices:
44
45If it is necessary to compare a user provided value with a computed value (for
46example, verifying a signature), there should be an API provided that performs
47the verification in a secure way (for example, using a constant time
48comparison), rather than requiring the user to perform the comparison
49themselves.
50
51If it is incorrect to ignore the result of a method, it should raise an
52exception, and not return a boolean ``True``/``False`` flag. For example, a
53method to verify a signature should raise ``InvalidSignature``, and not return
54whether the signature was valid.
55
56.. code-block:: python
57
58 # This is bad.
59 def verify(sig):
60 # ...
61 return is_valid
62
63 # Good!
64 def verify(sig):
65 # ...
66 if not is_valid:
67 raise InvalidSignature
68
69Every recipe should include a version or algorithmic marker of some sort in its
70output in order to allow transparent upgrading of the algorithms in use, as
71the algorithms or parameters needed to achieve a given security margin evolve.
72
73APIs at the :doc:`/hazmat/primitives/index` layer should always take an
74explicit backend, APIs at the recipes layer should automatically use the
75:func:`~cryptography.hazmat.backends.default_backend`, but optionally allow
76specifying a different backend.
77
78C bindings
79~~~~~~~~~~
80
Laurens Van Houtven0a1d9e12014-06-23 14:06:16 +020081More information on C bindings can be found in :doc:`the dedicated
82section of the documentation <c-bindings>`.
83
Paul Kehrer0839aa82014-02-11 22:36:51 -060084Tests
85-----
86
87All code changes must be accompanied by unit tests with 100% code coverage (as
88measured by the combined metrics across our build matrix).
89
90When implementing a new primitive or recipe ``cryptography`` requires that you
Paul Kehrer1681a692014-02-11 23:43:51 -060091provide a set of test vectors. See :doc:`/development/test-vectors` for more
92details.
Paul Kehrer0839aa82014-02-11 22:36:51 -060093
94Documentation
95-------------
96
Paul Kehrer813b2942014-06-05 12:40:56 -050097All features should be documented with prose in the ``docs`` section. To ensure
98it builds and passes `doc8`_ style checks you can run ``tox -e docs``.
Paul Kehrer0839aa82014-02-11 22:36:51 -060099
100Because of the inherent challenges in implementing correct cryptographic
101systems, we want to make our documentation point people in the right directions
102as much as possible. To that end:
103
104* When documenting a generic interface, use a strong algorithm in examples.
105 (e.g. when showing a hashing example, don't use
106 :class:`~cryptography.hazmat.primitives.hashes.MD5`)
107* When giving prescriptive advice, always provide references and supporting
108 material.
109* When there is real disagreement between cryptographic experts, represent both
110 sides of the argument and describe the trade-offs clearly.
111
112When documenting a new module in the ``hazmat`` package, its documentation
113should begin with the "Hazardous Materials" warning:
114
115.. code-block:: rest
116
117 .. hazmat::
118
119When referring to a hypothetical individual (such as "a person receiving an
120encrypted message") use gender neutral pronouns (they/them/their).
121
122Docstrings are typically only used when writing abstract classes, but should
123be written like this if required:
124
125.. code-block:: python
126
127 def some_function(some_arg):
128 """
129 Does some things.
130
131 :param some_arg: Some argument.
132 """
133
134So, specifically:
135
136* Always use three double quotes.
137* Put the three double quotes on their own line.
138* No blank line at the end.
139* Use Sphinx parameter/attribute documentation `syntax`_.
140
141
142.. _`Write comments as complete sentences.`: http://nedbatchelder.com/blog/201401/comments_should_be_sentences.html
143.. _`syntax`: http://sphinx-doc.org/domains.html#info-field-lists
144.. _`Studies have shown`: http://www.ibm.com/developerworks/rational/library/11-proven-practices-for-peer-review/
145.. _`our mailing list`: https://mail.python.org/mailman/listinfo/cryptography-dev
Paul Kehrer813b2942014-06-05 12:40:56 -0500146.. _`doc8`: https://github.com/stackforge/doc8