1*8fb009dcSAndroid Build Coastguard Worker# BoringSSL API Conventions 2*8fb009dcSAndroid Build Coastguard Worker 3*8fb009dcSAndroid Build Coastguard WorkerThis document describes conventions for BoringSSL APIs. The [style 4*8fb009dcSAndroid Build Coastguard Workerguide](./STYLE.md) also includes guidelines, but this document is targeted at 5*8fb009dcSAndroid Build Coastguard Workerboth API consumers and developers. API documentation in BoringSSL may assume 6*8fb009dcSAndroid Build Coastguard Workerthese conventions by default, rather than repeating them for every function. 7*8fb009dcSAndroid Build Coastguard Worker 8*8fb009dcSAndroid Build Coastguard Worker 9*8fb009dcSAndroid Build Coastguard Worker## Documentation 10*8fb009dcSAndroid Build Coastguard Worker 11*8fb009dcSAndroid Build Coastguard WorkerAll supported public APIs are documented in the public header files, found in 12*8fb009dcSAndroid Build Coastguard Worker`include/openssl`. The API documentation is also available 13*8fb009dcSAndroid Build Coastguard Worker[online](https://commondatastorage.googleapis.com/chromium-boringssl-docs/headers.html). 14*8fb009dcSAndroid Build Coastguard Worker 15*8fb009dcSAndroid Build Coastguard WorkerExperimental public APIs are found in `include/openssl/experimental`. Use of 16*8fb009dcSAndroid Build Coastguard Workerthese will likely be incompatible with changes in the near future as they are 17*8fb009dcSAndroid Build Coastguard Workerfinalized. 18*8fb009dcSAndroid Build Coastguard Worker 19*8fb009dcSAndroid Build Coastguard Worker## Forward declarations 20*8fb009dcSAndroid Build Coastguard Worker 21*8fb009dcSAndroid Build Coastguard WorkerDo not write `typedef struct foo_st FOO` or try otherwise to define BoringSSL's 22*8fb009dcSAndroid Build Coastguard Workertypes. Including `openssl/base.h` (or `openssl/ossl_typ.h` for consumers who 23*8fb009dcSAndroid Build Coastguard Workerwish to be OpenSSL-compatible) will forward-declare each type without importing 24*8fb009dcSAndroid Build Coastguard Workerthe rest of the library or invasive macros. 25*8fb009dcSAndroid Build Coastguard Worker 26*8fb009dcSAndroid Build Coastguard Worker 27*8fb009dcSAndroid Build Coastguard Worker## Error-handling 28*8fb009dcSAndroid Build Coastguard Worker 29*8fb009dcSAndroid Build Coastguard WorkerMost functions in BoringSSL may fail, either due to allocation failures or input 30*8fb009dcSAndroid Build Coastguard Workererrors. Functions which return an `int` typically return one on success and zero 31*8fb009dcSAndroid Build Coastguard Workeron failure. Functions which return a pointer typically return `NULL` on failure. 32*8fb009dcSAndroid Build Coastguard WorkerHowever, due to legacy constraints, some functions are more complex. Consult the 33*8fb009dcSAndroid Build Coastguard WorkerAPI documentation before using a function. 34*8fb009dcSAndroid Build Coastguard Worker 35*8fb009dcSAndroid Build Coastguard WorkerOn error, most functions also push errors on the error queue, an `errno`-like 36*8fb009dcSAndroid Build Coastguard Workermechanism. See the documentation for 37*8fb009dcSAndroid Build Coastguard Worker[err.h](https://commondatastorage.googleapis.com/chromium-boringssl-docs/err.h.html) 38*8fb009dcSAndroid Build Coastguard Workerfor more details. 39*8fb009dcSAndroid Build Coastguard Worker 40*8fb009dcSAndroid Build Coastguard WorkerAs with `errno`, callers must test the function's return value, not the error 41*8fb009dcSAndroid Build Coastguard Workerqueue to determine whether an operation failed. Some codepaths may not interact 42*8fb009dcSAndroid Build Coastguard Workerwith the error queue, and the error queue may have state from a previous failed 43*8fb009dcSAndroid Build Coastguard Workeroperation. 44*8fb009dcSAndroid Build Coastguard Worker 45*8fb009dcSAndroid Build Coastguard WorkerWhen ignoring a failed operation, it is recommended to call `ERR_clear_error` to 46*8fb009dcSAndroid Build Coastguard Workeravoid the state interacting with future operations. Failing to do so should not 47*8fb009dcSAndroid Build Coastguard Workeraffect the actual behavior of any functions, but may result in errors from both 48*8fb009dcSAndroid Build Coastguard Workeroperations being mixed in error logging. We hope to 49*8fb009dcSAndroid Build Coastguard Worker[improve](https://bugs.chromium.org/p/boringssl/issues/detail?id=38) this 50*8fb009dcSAndroid Build Coastguard Workersituation in the future. 51*8fb009dcSAndroid Build Coastguard Worker 52*8fb009dcSAndroid Build Coastguard WorkerWhere possible, avoid conditioning on specific reason codes and limit usage to 53*8fb009dcSAndroid Build Coastguard Workerlogging. The reason codes are very specific and may change over time. 54*8fb009dcSAndroid Build Coastguard Worker 55*8fb009dcSAndroid Build Coastguard Worker 56*8fb009dcSAndroid Build Coastguard Worker## Memory allocation 57*8fb009dcSAndroid Build Coastguard Worker 58*8fb009dcSAndroid Build Coastguard WorkerBoringSSL allocates memory via `OPENSSL_malloc`, found in `mem.h`. Use 59*8fb009dcSAndroid Build Coastguard Worker`OPENSSL_free`, found in the same header file, to release it. BoringSSL 60*8fb009dcSAndroid Build Coastguard Workerfunctions will fail gracefully on allocation error, but it is recommended to use 61*8fb009dcSAndroid Build Coastguard Workera `malloc` implementation that `abort`s on failure. 62*8fb009dcSAndroid Build Coastguard Worker 63*8fb009dcSAndroid Build Coastguard Worker 64*8fb009dcSAndroid Build Coastguard Worker## Pointers and slices 65*8fb009dcSAndroid Build Coastguard Worker 66*8fb009dcSAndroid Build Coastguard WorkerUnless otherwise specified, pointer parameters that refer to a single object, 67*8fb009dcSAndroid Build Coastguard Workereither as an input or output parameter, may not be `NULL`. In this case, 68*8fb009dcSAndroid Build Coastguard WorkerBoringSSL often will not check for `NULL` before dereferencing, so passing 69*8fb009dcSAndroid Build Coastguard Worker`NULL` may crash or exhibit other undefined behavior. (Sometimes the function 70*8fb009dcSAndroid Build Coastguard Workerwill check for `NULL` anyway, for OpenSSL compatibility, but we still consider 71*8fb009dcSAndroid Build Coastguard Workerpassing `NULL` to be a caller error.) 72*8fb009dcSAndroid Build Coastguard Worker 73*8fb009dcSAndroid Build Coastguard WorkerPointer parameters may also refer to a contiguous sequence of objects, sometimes 74*8fb009dcSAndroid Build Coastguard Workerreferred to as a *slice*. These will typically be a pair of pointer and length 75*8fb009dcSAndroid Build Coastguard Workerparameters named like `plaintext` and `plaintext_len`, or `objs` and `num_objs`. 76*8fb009dcSAndroid Build Coastguard WorkerWe prefer the former for byte buffers and the latter for sequences of other 77*8fb009dcSAndroid Build Coastguard Workertypes. The documentation will usually refer to both parameters together, e.g. 78*8fb009dcSAndroid Build Coastguard Worker"`EVP_DigestUpdate` hashes `len` bytes from `data`." 79*8fb009dcSAndroid Build Coastguard Worker 80*8fb009dcSAndroid Build Coastguard WorkerParameters in C and C++ that use array syntax, such as 81*8fb009dcSAndroid Build Coastguard Worker`uint8_t out[SHA256_DIGEST_LENGTH]`, are really pointers. In BoringSSL's uses of 82*8fb009dcSAndroid Build Coastguard Workerthis syntax, the pointer must point to the specified number of values. 83*8fb009dcSAndroid Build Coastguard Worker 84*8fb009dcSAndroid Build Coastguard WorkerIn other cases, the documentation will describe how the function parameters 85*8fb009dcSAndroid Build Coastguard Workerdetermine the slice's length. For example, a slice's length may be measured in 86*8fb009dcSAndroid Build Coastguard Workerunits other than element count, multiple slice parameters may share a length, or 87*8fb009dcSAndroid Build Coastguard Workera slice's length may be implicitly determined by other means like RSA key size. 88*8fb009dcSAndroid Build Coastguard Worker 89*8fb009dcSAndroid Build Coastguard WorkerBy default, BoringSSL follows C++'s 90*8fb009dcSAndroid Build Coastguard Worker[slice conventions](https://davidben.net/2024/01/15/empty-slices.html) 91*8fb009dcSAndroid Build Coastguard Workerfor pointers. That is, unless otherwise specified, pointers for non-empty 92*8fb009dcSAndroid Build Coastguard Worker(non-zero length) slices must be represented by a valid pointer to that many 93*8fb009dcSAndroid Build Coastguard Workerobjects in memory. Pointers for empty (zero length) slices must either be `NULL` 94*8fb009dcSAndroid Build Coastguard Workeror point within some sequence of objects of a compatible type. 95*8fb009dcSAndroid Build Coastguard Worker 96*8fb009dcSAndroid Build Coastguard WorkerWARNING: The dangling, non-null pointer used by Rust empty slices may *not* be 97*8fb009dcSAndroid Build Coastguard Workerpassed into BoringSSL. Rust FFIs must adjust such pointers to before passing to 98*8fb009dcSAndroid Build Coastguard WorkerBoringSSL. For example, see the `FfiSlice` abstraction in `bssl-crypto`. (We may 99*8fb009dcSAndroid Build Coastguard Workerrelax this if pointer arithmetic rules in C/C++ are adjusted to permit Rust's 100*8fb009dcSAndroid Build Coastguard Workerpointers. Until then, it is impractical for a C/C++ library to act on such a 101*8fb009dcSAndroid Build Coastguard Workerslice representation. See 102*8fb009dcSAndroid Build Coastguard Worker[this document](https://davidben.net/2024/01/15/empty-slices.html) for more 103*8fb009dcSAndroid Build Coastguard Workerdiscussion.) 104*8fb009dcSAndroid Build Coastguard Worker 105*8fb009dcSAndroid Build Coastguard WorkerIn some cases, OpenSSL compatibility requires that a function will treat `NULL` 106*8fb009dcSAndroid Build Coastguard Workerslice pointers differently from non-`NULL` pointers. Such behavior will be 107*8fb009dcSAndroid Build Coastguard Workerdescribed in documentation. For examples, see `EVP_EncryptUpdate`, 108*8fb009dcSAndroid Build Coastguard Worker`EVP_DigestSignFinal`, and `HMAC_Init_ex`. Callers passing potentially empty 109*8fb009dcSAndroid Build Coastguard Workerslices into such functions should take care that the `NULL` case is either 110*8fb009dcSAndroid Build Coastguard Workerunreachable or still has the desired behavior. 111*8fb009dcSAndroid Build Coastguard Worker 112*8fb009dcSAndroid Build Coastguard WorkerIf a `const char *` parameter is described as a "NUL-terminated string" or a 113*8fb009dcSAndroid Build Coastguard Worker"C string", it must point to a sequence of `char` values containing a NUL (zero) 114*8fb009dcSAndroid Build Coastguard Workervalue, which determines the length. Unless otherwise specified, the pointer may 115*8fb009dcSAndroid Build Coastguard Workernot be `NULL`, matching the C standard library. 116*8fb009dcSAndroid Build Coastguard Worker 117*8fb009dcSAndroid Build Coastguard WorkerFor purposes of C and C++'s 118*8fb009dcSAndroid Build Coastguard Worker[strict aliasing](https://en.cppreference.com/w/c/language/object#Strict_aliasing) 119*8fb009dcSAndroid Build Coastguard Workerrequirements, objects passed by pointers must be accessible as the specified 120*8fb009dcSAndroid Build Coastguard Workertype. `uint8_t` may be assumed to be the same type as `unsigned char` and thus 121*8fb009dcSAndroid Build Coastguard Workermay be the pointer type for all object types. BoringSSL does not support 122*8fb009dcSAndroid Build Coastguard Workerplatforms where `uint8_t` is a non-character type. However, there is no 123*8fb009dcSAndroid Build Coastguard Workerstrict aliasing sanitizer, very few C and C++ codebases are valid by strict 124*8fb009dcSAndroid Build Coastguard Workeraliasing, and BoringSSL itself has some 125*8fb009dcSAndroid Build Coastguard Worker[known strict aliasing bugs](https://crbug.com/boringssl/444), thus we strongly 126*8fb009dcSAndroid Build Coastguard Workerrecommend consumers build with `-fno-strict-aliasing`. 127*8fb009dcSAndroid Build Coastguard Worker 128*8fb009dcSAndroid Build Coastguard WorkerPointer parameters additionally have ownership and lifetime requirements, 129*8fb009dcSAndroid Build Coastguard Workerdiscussed in the section below. 130*8fb009dcSAndroid Build Coastguard Worker 131*8fb009dcSAndroid Build Coastguard Worker 132*8fb009dcSAndroid Build Coastguard Worker## Object initialization and cleanup 133*8fb009dcSAndroid Build Coastguard Worker 134*8fb009dcSAndroid Build Coastguard WorkerBoringSSL defines a number of structs for use in its APIs. It is a C library, 135*8fb009dcSAndroid Build Coastguard Workerso the caller is responsible for ensuring these structs are properly 136*8fb009dcSAndroid Build Coastguard Workerinitialized and released. Consult the documentation for a module for the 137*8fb009dcSAndroid Build Coastguard Workerproper use of its types. Some general conventions are listed below. 138*8fb009dcSAndroid Build Coastguard Worker 139*8fb009dcSAndroid Build Coastguard Worker 140*8fb009dcSAndroid Build Coastguard Worker### Heap-allocated types 141*8fb009dcSAndroid Build Coastguard Worker 142*8fb009dcSAndroid Build Coastguard WorkerSome types, such as `RSA`, are heap-allocated. All instances will be allocated 143*8fb009dcSAndroid Build Coastguard Workerand returned from BoringSSL's APIs. It is an error to instantiate a heap- 144*8fb009dcSAndroid Build Coastguard Workerallocated type on the stack or embedded within another object. 145*8fb009dcSAndroid Build Coastguard Worker 146*8fb009dcSAndroid Build Coastguard WorkerHeap-allocated types may have functioned named like `RSA_new` which allocates a 147*8fb009dcSAndroid Build Coastguard Workerfresh blank `RSA`. Other functions may also return newly-allocated instances. 148*8fb009dcSAndroid Build Coastguard WorkerFor example, `RSA_parse_public_key` is documented to return a newly-allocated 149*8fb009dcSAndroid Build Coastguard Worker`RSA` object. 150*8fb009dcSAndroid Build Coastguard Worker 151*8fb009dcSAndroid Build Coastguard WorkerHeap-allocated objects must be released by the corresponding free function, 152*8fb009dcSAndroid Build Coastguard Workernamed like `RSA_free`. Like C's `free` and C++'s `delete`, all free functions 153*8fb009dcSAndroid Build Coastguard Workerinternally check for `NULL`. It is redundant to check for `NULL` before calling. 154*8fb009dcSAndroid Build Coastguard Worker 155*8fb009dcSAndroid Build Coastguard WorkerA heap-allocated type may be reference-counted. In this case, a function named 156*8fb009dcSAndroid Build Coastguard Workerlike `RSA_up_ref` will be available to take an additional reference count. The 157*8fb009dcSAndroid Build Coastguard Workerfree function must be called to decrement the reference count. It will only 158*8fb009dcSAndroid Build Coastguard Workerrelease resources when the final reference is released. For OpenSSL 159*8fb009dcSAndroid Build Coastguard Workercompatibility, these functions return `int`, but callers may assume they always 160*8fb009dcSAndroid Build Coastguard Workersuccessfully return one because reference counts use saturating arithmetic. 161*8fb009dcSAndroid Build Coastguard Worker 162*8fb009dcSAndroid Build Coastguard WorkerC++ consumers are recommended to use `bssl::UniquePtr` to manage heap-allocated 163*8fb009dcSAndroid Build Coastguard Workerobjects. `bssl::UniquePtr<T>`, like other types, is forward-declared in 164*8fb009dcSAndroid Build Coastguard Worker`openssl/base.h`. Code that needs access to the free functions, such as code 165*8fb009dcSAndroid Build Coastguard Workerwhich destroys a `bssl::UniquePtr`, must include the corresponding module's 166*8fb009dcSAndroid Build Coastguard Workerheader. (This matches `std::unique_ptr`'s relationship with forward 167*8fb009dcSAndroid Build Coastguard Workerdeclarations.) Note, despite the name, `bssl::UniquePtr` is also used with 168*8fb009dcSAndroid Build Coastguard Workerreference-counted types. It owns a single reference to the object. To take an 169*8fb009dcSAndroid Build Coastguard Workeradditional reference, use the `bssl::UpRef` function, which will return a 170*8fb009dcSAndroid Build Coastguard Workerseparate `bssl::UniquePtr`. 171*8fb009dcSAndroid Build Coastguard Worker 172*8fb009dcSAndroid Build Coastguard Worker 173*8fb009dcSAndroid Build Coastguard Worker### Stack-allocated types 174*8fb009dcSAndroid Build Coastguard Worker 175*8fb009dcSAndroid Build Coastguard WorkerOther types in BoringSSL are stack-allocated, such as `EVP_MD_CTX`. These 176*8fb009dcSAndroid Build Coastguard Workertypes may be allocated on the stack or embedded within another object. 177*8fb009dcSAndroid Build Coastguard WorkerHowever, they must still be initialized before use. 178*8fb009dcSAndroid Build Coastguard Worker 179*8fb009dcSAndroid Build Coastguard WorkerEvery stack-allocated object in BoringSSL has a *zero state*, analogous to 180*8fb009dcSAndroid Build Coastguard Workerinitializing a pointer to `NULL`. In this state, the object may not be 181*8fb009dcSAndroid Build Coastguard Workercompletely initialized, but it is safe to call cleanup functions. Entering the 182*8fb009dcSAndroid Build Coastguard Workerzero state cannot fail. (It is usually `memset(0)`.) 183*8fb009dcSAndroid Build Coastguard Worker 184*8fb009dcSAndroid Build Coastguard WorkerThe function to enter the zero state is named like `EVP_MD_CTX_init` or 185*8fb009dcSAndroid Build Coastguard Worker`CBB_zero` and will always return `void`. To release resources associated with 186*8fb009dcSAndroid Build Coastguard Workerthe type, call the cleanup function, named like `EVP_MD_CTX_cleanup`. The 187*8fb009dcSAndroid Build Coastguard Workercleanup function must be called on all codepaths, regardless of success or 188*8fb009dcSAndroid Build Coastguard Workerfailure. For example: 189*8fb009dcSAndroid Build Coastguard Worker 190*8fb009dcSAndroid Build Coastguard Worker uint8_t md[EVP_MAX_MD_SIZE]; 191*8fb009dcSAndroid Build Coastguard Worker unsigned md_len; 192*8fb009dcSAndroid Build Coastguard Worker EVP_MD_CTX ctx; 193*8fb009dcSAndroid Build Coastguard Worker EVP_MD_CTX_init(&ctx); /* Enter the zero state. */ 194*8fb009dcSAndroid Build Coastguard Worker int ok = EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL) && 195*8fb009dcSAndroid Build Coastguard Worker EVP_DigestUpdate(&ctx, "hello ", 6) && 196*8fb009dcSAndroid Build Coastguard Worker EVP_DigestUpdate(&ctx, "world", 5) && 197*8fb009dcSAndroid Build Coastguard Worker EVP_DigestFinal_ex(&ctx, md, &md_len); 198*8fb009dcSAndroid Build Coastguard Worker EVP_MD_CTX_cleanup(&ctx); /* Release |ctx|. */ 199*8fb009dcSAndroid Build Coastguard Worker 200*8fb009dcSAndroid Build Coastguard WorkerNote that `EVP_MD_CTX_cleanup` is called whether or not the `EVP_Digest*` 201*8fb009dcSAndroid Build Coastguard Workeroperations succeeded. More complex C functions may use the `goto err` pattern: 202*8fb009dcSAndroid Build Coastguard Worker 203*8fb009dcSAndroid Build Coastguard Worker int ret = 0; 204*8fb009dcSAndroid Build Coastguard Worker EVP_MD_CTX ctx; 205*8fb009dcSAndroid Build Coastguard Worker EVP_MD_CTX_init(&ctx); 206*8fb009dcSAndroid Build Coastguard Worker 207*8fb009dcSAndroid Build Coastguard Worker if (!some_other_operation()) { 208*8fb009dcSAndroid Build Coastguard Worker goto err; 209*8fb009dcSAndroid Build Coastguard Worker } 210*8fb009dcSAndroid Build Coastguard Worker 211*8fb009dcSAndroid Build Coastguard Worker uint8_t md[EVP_MAX_MD_SIZE]; 212*8fb009dcSAndroid Build Coastguard Worker unsigned md_len; 213*8fb009dcSAndroid Build Coastguard Worker if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL) || 214*8fb009dcSAndroid Build Coastguard Worker !EVP_DigestUpdate(&ctx, "hello ", 6) || 215*8fb009dcSAndroid Build Coastguard Worker !EVP_DigestUpdate(&ctx, "world", 5) || 216*8fb009dcSAndroid Build Coastguard Worker !EVP_DigestFinal_ex(&ctx, md, &md_len) { 217*8fb009dcSAndroid Build Coastguard Worker goto err; 218*8fb009dcSAndroid Build Coastguard Worker } 219*8fb009dcSAndroid Build Coastguard Worker 220*8fb009dcSAndroid Build Coastguard Worker ret = 1; 221*8fb009dcSAndroid Build Coastguard Worker 222*8fb009dcSAndroid Build Coastguard Worker err: 223*8fb009dcSAndroid Build Coastguard Worker EVP_MD_CTX_cleanup(&ctx); 224*8fb009dcSAndroid Build Coastguard Worker return ret; 225*8fb009dcSAndroid Build Coastguard Worker 226*8fb009dcSAndroid Build Coastguard WorkerNote that, because `ctx` is set to the zero state before any failures, 227*8fb009dcSAndroid Build Coastguard Worker`EVP_MD_CTX_cleanup` is safe to call even if the first operation fails before 228*8fb009dcSAndroid Build Coastguard Worker`EVP_DigestInit_ex`. However, it would be illegal to move the `EVP_MD_CTX_init` 229*8fb009dcSAndroid Build Coastguard Workerbelow the `some_other_operation` call. 230*8fb009dcSAndroid Build Coastguard Worker 231*8fb009dcSAndroid Build Coastguard WorkerAs a rule of thumb, enter the zero state of stack-allocated structs in the 232*8fb009dcSAndroid Build Coastguard Workersame place they are declared. 233*8fb009dcSAndroid Build Coastguard Worker 234*8fb009dcSAndroid Build Coastguard WorkerC++ consumers are recommended to use the wrappers named like 235*8fb009dcSAndroid Build Coastguard Worker`bssl::ScopedEVP_MD_CTX`, defined in the corresponding module's header. These 236*8fb009dcSAndroid Build Coastguard Workerwrappers are automatically initialized to the zero state and are automatically 237*8fb009dcSAndroid Build Coastguard Workercleaned up. 238*8fb009dcSAndroid Build Coastguard Worker 239*8fb009dcSAndroid Build Coastguard Worker 240*8fb009dcSAndroid Build Coastguard Worker### Data-only types 241*8fb009dcSAndroid Build Coastguard Worker 242*8fb009dcSAndroid Build Coastguard WorkerA few types, such as `SHA_CTX`, are data-only types and do not require cleanup. 243*8fb009dcSAndroid Build Coastguard WorkerThese are usually for low-level cryptographic operations. These types may be 244*8fb009dcSAndroid Build Coastguard Workerused freely without special cleanup conventions. 245*8fb009dcSAndroid Build Coastguard Worker 246*8fb009dcSAndroid Build Coastguard Worker 247*8fb009dcSAndroid Build Coastguard Worker### Ownership and lifetime 248*8fb009dcSAndroid Build Coastguard Worker 249*8fb009dcSAndroid Build Coastguard WorkerWhen working with allocated objects, it is important to think about *ownership* 250*8fb009dcSAndroid Build Coastguard Workerof each object, or what code is responsible for releasing it. This matches the 251*8fb009dcSAndroid Build Coastguard Workercorresponding notion in higher-level languages like C++ and Rust. 252*8fb009dcSAndroid Build Coastguard Worker 253*8fb009dcSAndroid Build Coastguard WorkerOwnership applies to both uniquely-owned types and reference-counted types. For 254*8fb009dcSAndroid Build Coastguard Workerthe latter, ownership means the code is responsible for releasing one 255*8fb009dcSAndroid Build Coastguard Workerreference. Note a *reference* in BoringSSL refers to an increment (and eventual 256*8fb009dcSAndroid Build Coastguard Workerdecrement) of an object's reference count, not `T&` in C++. Thus, to "take a 257*8fb009dcSAndroid Build Coastguard Workerreference" means to increment the reference count and take ownership of 258*8fb009dcSAndroid Build Coastguard Workerdecrementing it. 259*8fb009dcSAndroid Build Coastguard Worker 260*8fb009dcSAndroid Build Coastguard WorkerAs BoringSSL's APIs are primarily in C, ownership and lifetime obligations are 261*8fb009dcSAndroid Build Coastguard Workernot rigorously annotated in the type signatures or checked at compile-time. 262*8fb009dcSAndroid Build Coastguard WorkerInstead, they are described in 263*8fb009dcSAndroid Build Coastguard Worker[API documentation](https://commondatastorage.googleapis.com/chromium-boringssl-docs/headers.html). 264*8fb009dcSAndroid Build Coastguard WorkerThis section describes some conventions. 265*8fb009dcSAndroid Build Coastguard Worker 266*8fb009dcSAndroid Build Coastguard WorkerUnless otherwise documented, functions do not take ownership of pointer 267*8fb009dcSAndroid Build Coastguard Workerarguments. The pointer typically must remain valid for the duration of the 268*8fb009dcSAndroid Build Coastguard Workerfunction call. The function may internally copy information from the argument or 269*8fb009dcSAndroid Build Coastguard Workertake a reference, but the caller is free to release its copy or reference at any 270*8fb009dcSAndroid Build Coastguard Workerpoint after the call completes. 271*8fb009dcSAndroid Build Coastguard Worker 272*8fb009dcSAndroid Build Coastguard WorkerA function may instead be documented to *take* or *transfer* ownership of a 273*8fb009dcSAndroid Build Coastguard Workerpointer. The caller must own the object before the function call and, after 274*8fb009dcSAndroid Build Coastguard Workertransfer, no longer owns it. As a corollary, the caller may no longer reference 275*8fb009dcSAndroid Build Coastguard Workerthe object without a separate guarantee on the lifetime. The function may even 276*8fb009dcSAndroid Build Coastguard Workerrelease the object before returning. Callers that wish to independently retain a 277*8fb009dcSAndroid Build Coastguard Workertransfered object must therefore take a reference or make a copy before 278*8fb009dcSAndroid Build Coastguard Workertransferring. Callers should also take note of whether the function is 279*8fb009dcSAndroid Build Coastguard Workerdocumented to transfer pointers unconditionally or only on success. Unlike C++ 280*8fb009dcSAndroid Build Coastguard Workerand Rust, functions in BoringSSL typically only transfer on success. 281*8fb009dcSAndroid Build Coastguard Worker 282*8fb009dcSAndroid Build Coastguard WorkerLikewise, output pointers may be owning or non-owning. Unless otherwise 283*8fb009dcSAndroid Build Coastguard Workerdocumented, functions output non-owning pointers. The caller is not responsible 284*8fb009dcSAndroid Build Coastguard Workerfor releasing the output pointer, but it must not use the pointer beyond its 285*8fb009dcSAndroid Build Coastguard Workerlifetime. The pointer may be released when the parent object is released or even 286*8fb009dcSAndroid Build Coastguard Workersooner on state change in the parent object. 287*8fb009dcSAndroid Build Coastguard Worker 288*8fb009dcSAndroid Build Coastguard WorkerIf documented to output a *newly-allocated* object or a *reference* or *copy* of 289*8fb009dcSAndroid Build Coastguard Workerone, the caller is responsible for releasing the object when it is done. 290*8fb009dcSAndroid Build Coastguard Worker 291*8fb009dcSAndroid Build Coastguard WorkerBy convention, functions named `get0` return non-owning pointers. Functions 292*8fb009dcSAndroid Build Coastguard Workernamed `new` or `get1` return owning pointers. Functions named `set0` take 293*8fb009dcSAndroid Build Coastguard Workerownership of arguments. Functions named `set1` do not. They typically take a 294*8fb009dcSAndroid Build Coastguard Workerreference or make a copy internally. These names originally referred to the 295*8fb009dcSAndroid Build Coastguard Workereffect on a reference count, but the convention applies equally to 296*8fb009dcSAndroid Build Coastguard Workernon-reference-counted types. 297*8fb009dcSAndroid Build Coastguard Worker 298*8fb009dcSAndroid Build Coastguard WorkerAPI documentation may also describe more complex obligations. For instance, an 299*8fb009dcSAndroid Build Coastguard Workerobject may borrow a pointer for longer than the duration of a single function 300*8fb009dcSAndroid Build Coastguard Workercall, in which case the caller must ensure the lifetime extends accordingly. 301*8fb009dcSAndroid Build Coastguard Worker 302*8fb009dcSAndroid Build Coastguard WorkerMemory errors are one of the most common and dangerous bugs in C and C++, so 303*8fb009dcSAndroid Build Coastguard Workercallers are encouraged to make use of tools such as 304*8fb009dcSAndroid Build Coastguard Worker[AddressSanitizer](https://clang.llvm.org/docs/AddressSanitizer.html) and 305*8fb009dcSAndroid Build Coastguard Workerhigher-level languages. 306*8fb009dcSAndroid Build Coastguard Worker 307*8fb009dcSAndroid Build Coastguard Worker 308*8fb009dcSAndroid Build Coastguard Worker## Thread safety 309*8fb009dcSAndroid Build Coastguard Worker 310*8fb009dcSAndroid Build Coastguard WorkerBoringSSL is internally aware of the platform threading library and calls into 311*8fb009dcSAndroid Build Coastguard Workerit as needed. Consult the API documentation for the threading guarantees of 312*8fb009dcSAndroid Build Coastguard Workerparticular objects. In general, stateless reference-counted objects like `RSA` 313*8fb009dcSAndroid Build Coastguard Workeror `EVP_PKEY` which represent keys may typically be used from multiple threads 314*8fb009dcSAndroid Build Coastguard Workersimultaneously, provided no thread mutates the key. 315