|
| 1 | +# crau |
| 2 | + |
| 3 | +`crau` is a header-only library to help define |
| 4 | +[crypto-auditing][crypto-auditing] probes in C applications. |
| 5 | + |
| 6 | +## Getting started |
| 7 | + |
| 8 | +1. Define `ENABLE_CRYPTO_AUDITING` to 1, e.g., through |
| 9 | + `<config.h>`. Also check if `<sys/sdt.h>` is installed and |
| 10 | + `DTRACE_PROBE*` macros are defined there. |
| 11 | + |
| 12 | +1. Include `<crau/crau.h>`. One of the C files should define |
| 13 | + `CRAU_IMPLEMENTATION` to get the functions defined. Also decide |
| 14 | + where to manage the context stack: if it is part of another struct, |
| 15 | + add field with type `struct crau_context_stack_st`; if it is |
| 16 | + maintained globally as thread-local, define a thread-local |
| 17 | + variable. |
| 18 | + |
| 19 | +1. (Optional) Customize the implementation with configuration macros, |
| 20 | + e.g., `CRAU_CONTEXT_STACK_DEPTH` for your needs. See |
| 21 | + `<crau/crau.h>` for the details. |
| 22 | + |
| 23 | +1. Instrument the code as follows. See `<crau/crau.h>` and |
| 24 | + `<crau/macros.h>` for the documentation: |
| 25 | + |
| 26 | +```c |
| 27 | +/* Public key signing operation starts (but the algorithm is not known yet) */ |
| 28 | +crau_new_context_with_data( |
| 29 | + &stack, |
| 30 | + "name", CRAU_STRING, "pk::sign", |
| 31 | + NULL) |
| 32 | +... |
| 33 | +/* Signing algorithm and bits are known at this point */ |
| 34 | +crau_data( |
| 35 | + &stack, |
| 36 | + "pk::algorithm", CRAU_STRING, "mldsa", |
| 37 | + "pk::bits", CRAU_WORD, 1952 * 8, |
| 38 | + NULL) |
| 39 | + |
| 40 | +/* Do the operation */ |
| 41 | +sig = mldsa_sign(...); |
| 42 | + |
| 43 | +/* Pop the operation context */ |
| 44 | +crau_pop_context(&stack); |
| 45 | +``` |
| 46 | +
|
| 47 | +## Low level macros |
| 48 | +
|
| 49 | +Instead of using those helper functions (`crau_*`), it is also |
| 50 | +possible to directly instrument the library with `CRAU_` macros |
| 51 | +defined in `macros.h`: |
| 52 | +
|
| 53 | +```c |
| 54 | +/* Public key signing operation starts (but the algorithm is not known yet) */ |
| 55 | +CRAU_NEW_CONTEXT_WITH_DATAV( |
| 56 | + (long)this_function, |
| 57 | + (long)parent_function, |
| 58 | + CRAU_STRING_DATA("name", "pk::sign")); |
| 59 | +... |
| 60 | +/* Signing algorithm and bits are known at this point */ |
| 61 | +CRAU_DATAV( |
| 62 | + (long)this_function, |
| 63 | + CRAU_STRING_DATA("pk::algorithm", "mldsa"), |
| 64 | + CRAU_WORD_DATA("pk::bits", 1952 * 8)) |
| 65 | +
|
| 66 | +/* Do the operation */ |
| 67 | +sig = mldsa_sign(...); |
| 68 | +``` |
| 69 | + |
| 70 | +Note that those macros don't do context management. |
| 71 | + |
| 72 | +## License |
| 73 | + |
| 74 | +MIT OR Unlicense |
| 75 | + |
| 76 | +[crypto-auditing]: https://github.com/latchset/crypto-auditing |
0 commit comments