blob: c05f56d190879789d00eacfaa5eceaf407860237 [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`.
11
12If you believe you've identified a security issue in ``cryptography``, please
13follow the directions on the :doc:`security page </security>`.
14
15Code
16----
17
Paul Kehrer5b6ce2a2014-02-24 20:16:10 -060018When in doubt, refer to :pep:`8` for Python code. You can check if your code
19meets our automated requirements by running ``flake8`` against it. If you've
20installed the development requirements this will automatically use our
21configuration. You can also run the ``tox`` job with ``tox -e pep8``.
Paul Kehrer0839aa82014-02-11 22:36:51 -060022
23`Write comments as complete sentences.`_
24
25Every code file must start with the boilerplate notice of the Apache License.
26Additionally, every Python code file must contain
27
28.. code-block:: python
29
30 from __future__ import absolute_import, division, print_function
31
Alex Stapletonc5fffd32014-03-18 15:29:00 +000032API considerations
Paul Kehrer0839aa82014-02-11 22:36:51 -060033~~~~~~~~~~~~~~~~~~
34
35Most projects' APIs are designed with a philosophy of "make easy things easy,
36and make hard things possible". One of the perils of writing cryptographic code
37is that secure code looks just like insecure code, and its results are almost
38always indistinguishable. As a result ``cryptography`` has, as a design
39philosophy: "make it hard to do insecure things". Here are a few strategies for
40API design that should be both followed, and should inspire other API choices:
41
42If it is necessary to compare a user provided value with a computed value (for
43example, verifying a signature), there should be an API provided that performs
44the verification in a secure way (for example, using a constant time
45comparison), rather than requiring the user to perform the comparison
46themselves.
47
48If it is incorrect to ignore the result of a method, it should raise an
49exception, and not return a boolean ``True``/``False`` flag. For example, a
50method to verify a signature should raise ``InvalidSignature``, and not return
51whether the signature was valid.
52
53.. code-block:: python
54
55 # This is bad.
56 def verify(sig):
57 # ...
58 return is_valid
59
60 # Good!
61 def verify(sig):
62 # ...
63 if not is_valid:
64 raise InvalidSignature
65
66Every recipe should include a version or algorithmic marker of some sort in its
67output in order to allow transparent upgrading of the algorithms in use, as
68the algorithms or parameters needed to achieve a given security margin evolve.
69
70APIs at the :doc:`/hazmat/primitives/index` layer should always take an
71explicit backend, APIs at the recipes layer should automatically use the
72:func:`~cryptography.hazmat.backends.default_backend`, but optionally allow
73specifying a different backend.
74
75C bindings
76~~~~~~~~~~
77
78When binding C code with ``cffi`` we have our own style guide, it's pretty
79simple.
80
81Don't name parameters:
82
83.. code-block:: c
84
Laurens Van Houtvened3d15b2014-06-23 13:50:35 +020085 /* Good */
Paul Kehrer0839aa82014-02-11 22:36:51 -060086 long f(long);
Laurens Van Houtvened3d15b2014-06-23 13:50:35 +020087 /* Bad */
Paul Kehrer0839aa82014-02-11 22:36:51 -060088 long f(long x);
89
90...unless they're inside a struct:
91
92.. code-block:: c
93
94 struct my_struct {
95 char *name;
96 int number;
97 ...;
98 };
99
100Include ``void`` if the function takes no arguments:
101
102.. code-block:: c
103
Laurens Van Houtvened3d15b2014-06-23 13:50:35 +0200104 /* Good */
Paul Kehrer0839aa82014-02-11 22:36:51 -0600105 long f(void);
Laurens Van Houtvened3d15b2014-06-23 13:50:35 +0200106 /* Bad */
Paul Kehrer0839aa82014-02-11 22:36:51 -0600107 long f();
108
109Wrap lines at 80 characters like so:
110
111.. code-block:: c
112
Laurens Van Houtvened3d15b2014-06-23 13:50:35 +0200113 /* Pretend this went to 80 characters */
Paul Kehrer0839aa82014-02-11 22:36:51 -0600114 long f(long, long,
115 int *)
116
117Include a space after commas between parameters:
118
119.. code-block:: c
120
Laurens Van Houtvened3d15b2014-06-23 13:50:35 +0200121 /* Good */
Paul Kehrer0839aa82014-02-11 22:36:51 -0600122 long f(int, char *)
Laurens Van Houtvened3d15b2014-06-23 13:50:35 +0200123 /* Bad */
Paul Kehrer0839aa82014-02-11 22:36:51 -0600124 long f(int,char *)
125
Laurens Van Houtven0d1122d2014-06-23 13:50:51 +0200126Use C-style ``/* */`` comments instead of C++-style ``//``:
127
128.. code-block:: c
129
130 // Bad
131 /* Good */
132
Paul Kehrer0839aa82014-02-11 22:36:51 -0600133Values set by ``#define`` should be assigned the appropriate type. If you see
134this:
135
136.. code-block:: c
137
138 #define SOME_INTEGER_LITERAL 0x0;
139 #define SOME_UNSIGNED_INTEGER_LITERAL 0x0001U;
140 #define SOME_STRING_LITERAL "hello";
141
142...it should be added to the bindings like so:
143
144.. code-block:: c
145
146 static const int SOME_INTEGER_LITERAL;
147 static const unsigned int SOME_UNSIGNED_INTEGER_LITERAL;
148 static const char *const SOME_STRING_LITERAL;
149
150Tests
151-----
152
153All code changes must be accompanied by unit tests with 100% code coverage (as
154measured by the combined metrics across our build matrix).
155
156When implementing a new primitive or recipe ``cryptography`` requires that you
Paul Kehrer1681a692014-02-11 23:43:51 -0600157provide a set of test vectors. See :doc:`/development/test-vectors` for more
158details.
Paul Kehrer0839aa82014-02-11 22:36:51 -0600159
160Documentation
161-------------
162
Paul Kehrer813b2942014-06-05 12:40:56 -0500163All features should be documented with prose in the ``docs`` section. To ensure
164it builds and passes `doc8`_ style checks you can run ``tox -e docs``.
Paul Kehrer0839aa82014-02-11 22:36:51 -0600165
166Because of the inherent challenges in implementing correct cryptographic
167systems, we want to make our documentation point people in the right directions
168as much as possible. To that end:
169
170* When documenting a generic interface, use a strong algorithm in examples.
171 (e.g. when showing a hashing example, don't use
172 :class:`~cryptography.hazmat.primitives.hashes.MD5`)
173* When giving prescriptive advice, always provide references and supporting
174 material.
175* When there is real disagreement between cryptographic experts, represent both
176 sides of the argument and describe the trade-offs clearly.
177
178When documenting a new module in the ``hazmat`` package, its documentation
179should begin with the "Hazardous Materials" warning:
180
181.. code-block:: rest
182
183 .. hazmat::
184
185When referring to a hypothetical individual (such as "a person receiving an
186encrypted message") use gender neutral pronouns (they/them/their).
187
188Docstrings are typically only used when writing abstract classes, but should
189be written like this if required:
190
191.. code-block:: python
192
193 def some_function(some_arg):
194 """
195 Does some things.
196
197 :param some_arg: Some argument.
198 """
199
200So, specifically:
201
202* Always use three double quotes.
203* Put the three double quotes on their own line.
204* No blank line at the end.
205* Use Sphinx parameter/attribute documentation `syntax`_.
206
207
208.. _`Write comments as complete sentences.`: http://nedbatchelder.com/blog/201401/comments_should_be_sentences.html
209.. _`syntax`: http://sphinx-doc.org/domains.html#info-field-lists
210.. _`Studies have shown`: http://www.ibm.com/developerworks/rational/library/11-proven-practices-for-peer-review/
211.. _`our mailing list`: https://mail.python.org/mailman/listinfo/cryptography-dev
Paul Kehrer813b2942014-06-05 12:40:56 -0500212.. _`doc8`: https://github.com/stackforge/doc8