Laurens Van Houtven | 7d2c74e | 2014-06-23 13:49:58 +0200 | [diff] [blame] | 1 | C bindings |
| 2 | ========== |
| 3 | |
| 4 | C bindings are bindings to C libraries, using cffi_ whenever possible. |
| 5 | |
Laurens Van Houtven | 4e00614 | 2014-06-23 14:05:10 +0200 | [diff] [blame] | 6 | .. _cffi: http://cffi.readthedocs.org |
Laurens Van Houtven | 7d2c74e | 2014-06-23 13:49:58 +0200 | [diff] [blame] | 7 | |
| 8 | Bindings live in :py:mod:`cryptography.hazmat.bindings`. |
Laurens Van Houtven | efa5cfb | 2014-06-23 13:51:35 +0200 | [diff] [blame] | 9 | |
Laurens Van Houtven | 220a98d | 2014-06-23 14:08:27 +0200 | [diff] [blame] | 10 | Style guide |
| 11 | ----------- |
| 12 | |
| 13 | Don't name parameters: |
| 14 | |
| 15 | .. code-block:: c |
| 16 | |
| 17 | /* Good */ |
| 18 | long f(long); |
| 19 | /* Bad */ |
| 20 | long f(long x); |
| 21 | |
| 22 | ...unless they're inside a struct: |
| 23 | |
| 24 | .. code-block:: c |
| 25 | |
| 26 | struct my_struct { |
| 27 | char *name; |
| 28 | int number; |
| 29 | ...; |
| 30 | }; |
| 31 | |
| 32 | Include ``void`` if the function takes no arguments: |
| 33 | |
| 34 | .. code-block:: c |
| 35 | |
| 36 | /* Good */ |
| 37 | long f(void); |
| 38 | /* Bad */ |
| 39 | long f(); |
| 40 | |
| 41 | Wrap lines at 80 characters like so: |
| 42 | |
| 43 | .. code-block:: c |
| 44 | |
| 45 | /* Pretend this went to 80 characters */ |
| 46 | long f(long, long, |
| 47 | int *) |
| 48 | |
| 49 | Include a space after commas between parameters: |
| 50 | |
| 51 | .. code-block:: c |
| 52 | |
| 53 | /* Good */ |
| 54 | long f(int, char *) |
| 55 | /* Bad */ |
| 56 | long f(int,char *) |
| 57 | |
| 58 | Use C-style ``/* */`` comments instead of C++-style ``//``: |
| 59 | |
| 60 | .. code-block:: c |
| 61 | |
| 62 | // Bad |
| 63 | /* Good */ |
| 64 | |
| 65 | Values set by ``#define`` should be assigned the appropriate type. If you see |
| 66 | this: |
| 67 | |
| 68 | .. code-block:: c |
| 69 | |
| 70 | #define SOME_INTEGER_LITERAL 0x0; |
| 71 | #define SOME_UNSIGNED_INTEGER_LITERAL 0x0001U; |
| 72 | #define SOME_STRING_LITERAL "hello"; |
| 73 | |
| 74 | ...it should be added to the bindings like so: |
| 75 | |
| 76 | .. code-block:: c |
| 77 | |
| 78 | static const int SOME_INTEGER_LITERAL; |
| 79 | static const unsigned int SOME_UNSIGNED_INTEGER_LITERAL; |
| 80 | static const char *const SOME_STRING_LITERAL; |
| 81 | |
Laurens Van Houtven | efa5cfb | 2014-06-23 13:51:35 +0200 | [diff] [blame] | 82 | Adding constant, types, functions... |
| 83 | ------------------------------------ |
| 84 | |
| 85 | You can create bindings for any name that exists in some version of |
| 86 | the library you're binding against. However, the project also has to |
| 87 | keep supporting older versions of the library. In order to acchieve |
| 88 | this, binding modules have ``CUSTOMIZATIONS`` and |
| 89 | ``CONDITIONAL_NAMES`` constants. |
| 90 | |
| 91 | Let's say you want to enable quantum transmogrification. The upstream |
| 92 | library implements this as the following API:: |
| 93 | |
| 94 | static const int QM_TRANSMOGRIFICATION_ALIGNMENT_LEFT; |
| 95 | static const int QM_TRANSMOGRIFICATION_ALIGNMENT_RIGHT; |
| 96 | typedef ... QM_TRANSMOGRIFICATION_CTX; |
| 97 | int QM_transmogrify(QM_TRANSMOGRIFICATION_CTX *, int); |
| 98 | |
| 99 | To start, create a new constant that defines if the *actual* library |
| 100 | has the feature you want, and add it to ``TYPES``:: |
| 101 | |
| 102 | static const long Cryptography_HAS_QUANTUM_TRANSMOGRIFICATION; |
| 103 | |
| 104 | This should start with ``Cryptography_``, since we're adding it in |
| 105 | this library. This prevents namespace collisions. |
| 106 | |
| 107 | Then, define the actual features (constants, types, functions...) you |
| 108 | want to expose. If it's a constant, just add it to ``TYPES``:: |
| 109 | |
| 110 | static const int QM_TRANSMOGRIFICATION_ALIGNMENT_LEFT; |
| 111 | static const int QM_TRANSMOGRIFICATION_ALIGNMENT_RIGHT; |
| 112 | |
| 113 | If it's a struct, add it to ``TYPES`` as well. The following is an |
| 114 | opaque struct:: |
| 115 | |
| 116 | typedef ... QM_TRANSMOGRIFICATION_CTX; |
| 117 | |
| 118 | ... but you can also make some or all items in the struct accessible:: |
| 119 | |
| 120 | typedef struct { |
| 121 | /* Fundamental constant k for your particular universe */ |
| 122 | BIGNUM *k; |
| 123 | ...; |
| 124 | } QM_TRANSMOGRIFICATION_CTX; |
| 125 | |
| 126 | Confusingly, functions that aren't always available on all supported |
| 127 | versions of the library, should be defined in ``MACROS`` and *not* in |
| 128 | ``FUNCTIONS``. Fortunately, you just have to copy the signature:: |
| 129 | |
| 130 | int QM_transmogrify(QM_TRANSMOGRIFICATION_CTX *, int); |
| 131 | |
| 132 | Then, we define the ``CUSTOMIZATIONS`` entry. To do that, we have to |
| 133 | come up with a C preprocessor expression that decides whether or not a |
| 134 | feature exists in the library. For example:: |
| 135 | |
| 136 | #ifdef QM_transmogrify |
| 137 | |
| 138 | Then, we set the flag that signifies the feature exists:: |
| 139 | |
| 140 | static const long Cryptography_HAS_QUANTUM_TRANSMOGRIFICATION = 1; |
| 141 | |
| 142 | Otherwise, we set that flag to 0:: |
| 143 | |
| 144 | #else |
| 145 | static const long Cryptography_HAS_QUANTUM_TRANSMOGRIFICATION = 0; |
| 146 | |
| 147 | Then, in that ``#else`` block, we define a number of fallbacks. For an |
| 148 | integer constant, just define it as 0:: |
| 149 | |
| 150 | static const int QM_TRANSMOGRIFICATION_ALIGNMENT_LEFT = 0; |
| 151 | static const int QM_TRANSMOGRIFICATION_ALIGNMENT_RIGHT = 0; |
| 152 | |
| 153 | For a function, it's a bit trickier. You have to define a function |
| 154 | pointer of the appropriate type to be NULL:: |
| 155 | |
| 156 | int (*QM_transmogrify)(QM_TRANSMOGRIFICATION_CTX *, int) = NULL; |
| 157 | |
Laurens Van Houtven | 1dc0b14 | 2014-06-23 13:55:21 +0200 | [diff] [blame] | 158 | (To do that, copy the signature, put a ``*`` in front of the function |
Laurens Van Houtven | efa5cfb | 2014-06-23 13:51:35 +0200 | [diff] [blame] | 159 | name and wrap it in parens, and then put ``= NULL`` at the end). |
| 160 | |
| 161 | Note how types don't need to be conditionally defined, as long as all |
| 162 | the necessarily typedefs are in place. |
| 163 | |
| 164 | Finally, add an entry to ``CONDITIONAL_NAMES`` with all of the things |
| 165 | you want to conditionally export:: |
| 166 | |
| 167 | CONDITIONAL_NAMES = { |
| 168 | ... |
| 169 | "Cryptography_HAS_QUANTUM_TRANSMOGRIFICATION": [ |
| 170 | "QM_TRANSMOGRIFICATION_ALIGNMENT_LEFT", |
| 171 | "QM_TRANSMOGRIFICATION_ALIGNMENT_RIGHT", |
| 172 | "QM_transmogrify" |
| 173 | ] |
| 174 | } |
| 175 | |
| 176 | Caveats |
| 177 | ~~~~~~~ |
| 178 | |
| 179 | Sometimes, a set of loosely related features are added in the same |
| 180 | version, and it's impractical to create ``#ifdef`` statements for each |
| 181 | one. In that case, it may make sense to either check for a particular |
Laurens Van Houtven | 1c07ddf | 2014-06-23 13:55:27 +0200 | [diff] [blame] | 182 | version. For example, to check for OpenSSL 1.0.0 or newer:: |
Laurens Van Houtven | efa5cfb | 2014-06-23 13:51:35 +0200 | [diff] [blame] | 183 | |
| 184 | #if OPENSSL_VERSION_NUMBER >= 0x10000000L |
| 185 | |
Laurens Van Houtven | 260e887 | 2014-06-23 16:22:32 +0200 | [diff] [blame^] | 186 | Sometimes, the version of a library on a particular platform will have |
Laurens Van Houtven | efa5cfb | 2014-06-23 13:51:35 +0200 | [diff] [blame] | 187 | features that you thought it wouldn't, based on its version. |
| 188 | Occasionally, packagers appear to ship arbitrary VCS checkouts. As a |
| 189 | result, sometimes you may have to add separate ``#ifdef`` statements |
| 190 | for particular features. This kind of issue is typically only caught |
| 191 | by running the tests on a wide variety of systems, which is the job of |
| 192 | our continuous integration infrastructure. |