您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. # BoringSSL Style Guide
  2. BoringSSL usually follows the
  3. [Google C++ style guide](https://google.github.io/styleguide/cppguide.html),
  4. The rest of this document describes differences and clarifications on
  5. top of the base guide.
  6. ## Legacy code
  7. As a derivative of OpenSSL, BoringSSL contains a lot of legacy code that
  8. does not follow this style guide. Particularly where public API is
  9. concerned, balance consistency within a module with the benefits of a
  10. given rule. Module-wide deviations on naming should be respected while
  11. integer and return value conventions take precedence over consistency.
  12. Modules from OpenSSL's legacy ASN.1 and X.509 stack are retained for
  13. compatibility and left largely unmodified. To ease importing patches from
  14. upstream, they match OpenSSL's new indentation style. For Emacs,
  15. `doc/openssl-c-indent.el` from OpenSSL may be helpful in this.
  16. ## Language
  17. The majority of the project is in C, so C++-specific rules in the
  18. Google style guide do not apply. Support for C99 features depends on
  19. our target platforms. Typically, Chromium's target MSVC is the most
  20. restrictive.
  21. Variable declarations in the middle of a function or inside a `for` loop are
  22. allowed and preferred where possible. Note that the common `goto err` cleanup
  23. pattern requires lifting some variable declarations.
  24. Comments should be `// C99-style` for consistency with C++.
  25. When declaring pointer types, `*` should be placed next to the variable name,
  26. not the type. So
  27. uint8_t *ptr;
  28. not
  29. uint8_t* ptr;
  30. Rather than `malloc()` and `free()`, use the wrappers `OPENSSL_malloc()`
  31. and `OPENSSL_free()`. Use the standard C `assert()` function freely.
  32. Use the following wrappers, found in `crypto/internal.h` instead of the
  33. corresponding C standard library functions. They behave the same but avoid
  34. confusing undefined behavior.
  35. * `OPENSSL_memchr`
  36. * `OPENSSL_memcmp`
  37. * `OPENSSL_memcpy`
  38. * `OPENSSL_memmove`
  39. * `OPENSSL_memset`
  40. For new constants, prefer enums when the values are sequential and typed
  41. constants for flags. If adding values to an existing set of `#define`s,
  42. continue with `#define`.
  43. ## libssl
  44. libssl was originally written in C but is being incrementally rewritten in
  45. C++11. As of writing, much of the style matches our C conventions rather than
  46. Google C++. Additionally, libssl on Linux currently may not depend on the C++
  47. runtime. See the C++ utilities in `ssl/internal.h` for replacements for
  48. problematic C++ constructs. The `util/check_imported_libraries.go` script may be
  49. used with a shared library build to check if a new construct is okay.
  50. If unsure, match surrounding code. Discrepancies between it and Google C++ style
  51. will be fixed over time.
  52. ## Formatting
  53. Single-statement blocks are not allowed. All conditions and loops must
  54. use braces:
  55. if (foo) {
  56. do_something();
  57. }
  58. not
  59. if (foo)
  60. do_something();
  61. ## Integers
  62. Prefer using explicitly-sized integers where appropriate rather than
  63. generic C ones. For instance, to represent a byte, use `uint8_t`, not
  64. `unsigned char`. Likewise, represent a two-byte field as `uint16_t`, not
  65. `unsigned short`.
  66. Sizes are represented as `size_t`.
  67. Within a struct that is retained across the lifetime of an SSL
  68. connection, if bounds of a size are known and it's easy, use a smaller
  69. integer type like `uint8_t`. This is a "free" connection footprint
  70. optimization for servers. Don't make code significantly more complex for
  71. it, and do still check the bounds when passing in and out of the
  72. struct. This narrowing should not propagate to local variables and
  73. function parameters.
  74. When doing arithmetic, account for overflow conditions.
  75. Except with platform APIs, do not use `ssize_t`. MSVC lacks it, and
  76. prefer out-of-band error signaling for `size_t` (see Return values).
  77. ## Naming
  78. Follow Google naming conventions in C++ files. In C files, use the
  79. following naming conventions for consistency with existing OpenSSL and C
  80. styles:
  81. Define structs with typedef named `TYPE_NAME`. The corresponding struct
  82. should be named `struct type_name_st`.
  83. Name public functions as `MODULE_function_name`, unless the module
  84. already uses a different naming scheme for legacy reasons. The module
  85. name should be a type name if the function is a method of a particular
  86. type.
  87. Some types are allocated within the library while others are initialized
  88. into a struct allocated by the caller, often on the stack. Name these
  89. functions `TYPE_NAME_new`/`TYPE_NAME_free` and
  90. `TYPE_NAME_init`/`TYPE_NAME_cleanup`, respectively. All `TYPE_NAME_free`
  91. functions must do nothing on `NULL` input.
  92. If a variable is the length of a pointer value, it has the suffix
  93. `_len`. An output parameter is named `out` or has an `out_` prefix. For
  94. instance, For instance:
  95. uint8_t *out,
  96. size_t *out_len,
  97. const uint8_t *in,
  98. size_t in_len,
  99. Name public headers like `include/openssl/evp.h` with header guards like
  100. `OPENSSL_HEADER_EVP_H`. Name internal headers like
  101. `crypto/ec/internal.h` with header guards like
  102. `OPENSSL_HEADER_EC_INTERNAL_H`.
  103. Name enums like `enum unix_hacker_t`. For instance:
  104. enum should_free_handshake_buffer_t {
  105. free_handshake_buffer,
  106. dont_free_handshake_buffer,
  107. };
  108. ## Return values
  109. As even `malloc` may fail in BoringSSL, the vast majority of functions
  110. will have a failure case. Functions should return `int` with one on
  111. success and zero on error. Do not overload the return value to both
  112. signal success/failure and output an integer. For example:
  113. OPENSSL_EXPORT int CBS_get_u16(CBS *cbs, uint16_t *out);
  114. If a function needs more than a true/false result code, define an enum
  115. rather than arbitrarily assigning meaning to int values.
  116. If a function outputs a pointer to an object on success and there are no
  117. other outputs, return the pointer directly and `NULL` on error.
  118. ## Parameters
  119. Where not constrained by legacy code, parameter order should be:
  120. 1. context parameters
  121. 2. output parameters
  122. 3. input parameters
  123. For example,
  124. /* CBB_add_asn sets |*out_contents| to a |CBB| into which the contents of an
  125. * ASN.1 object can be written. The |tag| argument will be used as the tag for
  126. * the object. It returns one on success or zero on error. */
  127. OPENSSL_EXPORT int CBB_add_asn1(CBB *cbb, CBB *out_contents, unsigned tag);
  128. ## Documentation
  129. All public symbols must have a documentation comment in their header
  130. file. The style is based on that of Go. The first sentence begins with
  131. the symbol name, optionally prefixed with "A" or "An". Apart from the
  132. initial mention of symbol, references to other symbols or parameter
  133. names should be surrounded by |pipes|.
  134. Documentation should be concise but completely describe the exposed
  135. behavior of the function. Pay special note to success/failure behaviors
  136. and caller obligations on object lifetimes. If this sacrifices
  137. conciseness, consider simplifying the function's behavior.
  138. // EVP_DigestVerifyUpdate appends |len| bytes from |data| to the data which
  139. // will be verified by |EVP_DigestVerifyFinal|. It returns one on success and
  140. // zero otherwise.
  141. OPENSSL_EXPORT int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data,
  142. size_t len);
  143. Explicitly mention any surprising edge cases or deviations from common
  144. return value patterns in legacy functions.
  145. // RSA_private_encrypt encrypts |flen| bytes from |from| with the private key in
  146. // |rsa| and writes the encrypted data to |to|. The |to| buffer must have at
  147. // least |RSA_size| bytes of space. It returns the number of bytes written, or
  148. // -1 on error. The |padding| argument must be one of the |RSA_*_PADDING|
  149. // values. If in doubt, |RSA_PKCS1_PADDING| is the most common.
  150. //
  151. // WARNING: this function is dangerous because it breaks the usual return value
  152. // convention. Use |RSA_sign_raw| instead.
  153. OPENSSL_EXPORT int RSA_private_encrypt(int flen, const uint8_t *from,
  154. uint8_t *to, RSA *rsa, int padding);
  155. Document private functions in their `internal.h` header or, if static,
  156. where defined.
  157. ## Build logic
  158. BoringSSL is used by many projects with many different build tools.
  159. Reimplementing and maintaining build logic in each downstream build is
  160. cumbersome, so build logic should be avoided where possible. Platform-specific
  161. files should be excluded by wrapping the contents in `#ifdef`s, rather than
  162. computing platform-specific file lists. Generated source files such as perlasm
  163. and `err_data.c` may be used in the standalone CMake build but, for downstream
  164. builds, they should be pre-generated in `generate_build_files.py`.