1 module sodium.utils; 2 3 extern (C) : 4 5 /** 6 * Zeroing memory. 7 * After use, sensitive data should be overwritten, but memset() and hand-written code can be 8 * silently stripped out by an optimizing compiler or by the linker. 9 * The sodium_memzero() function tries to effectively zero len bytes starting at pnt , even if 10 * optimizations are being applied to the code. */ 11 void sodium_memzero(const(void*) pnt, const size_t len); 12 13 /** 14 * Constant-time test for equality. 15 * When a comparison involves secret data (e.g. key, authentication tag), is it critical to use a 16 * constant-time comparison function in order to mitigate side-channel attacks. 17 * The sodium_memcmp() function can be used for this purpose.<br> 18 * The function returns 0 if the len bytes pointed to by b1_ match the len bytes pointed 19 * to by b2_ . Otherwise, it returns -1 .<br> 20 * WARNING: sodium_memcmp() must be used to verify if two secret keys are equal, in constant time.<br> 21 * This function is not designed for lexicographical comparisons and is not a generic replacement 22 * for memcmp(). 23 */ 24 int sodium_memcmp(const(void*) b1_, const(void*) b2_, size_t len); 25 26 /** 27 * Comparing large numbers. 28 * sodium_compare() returns -1 if b1_ < b2_, 1 if b1_ > b2_ and 0 if b1_ == b2_ 29 * It is suitable for lexicographical comparisons, or to compare nonces 30 * and counters stored in little-endian format. 31 * However, it is slower than sodium_memcmp(). 32 */ 33 int sodium_compare(const(ubyte)* b1_, const(ubyte)* b2_, size_t len); 34 35 /** 36 * Testing for all zeros. 37 * This function returns 1 if the nlen bytes vector pointed by n contains only zeros. 38 * It returns 0 if non-zero bits are found.<br> 39 * Its execution time is constant for a given length.<br> 40 * This function was introduced in libsodium 1.0.7. */ 41 int sodium_is_zero(const(ubyte)* n, const size_t nlen); 42 43 /** 44 * Incrementing large numbers. 45 * The sodium_increment() function takes a pointer to an arbitrary-long unsigned number, and 46 * increments it. 47 * It runs in constant-time for a given length, and considers the number to be encoded in little- 48 * endian format. 49 * sodium_increment() can be used to increment nonces in constant time. 50 * This function was introduced in libsodium 1.0.4. */ 51 void sodium_increment(ubyte* n, const size_t nlen); 52 53 /** 54 * Adding large numbers 55 * The sodium_add() function accepts two pointers to unsigned numbers encoded in little- 56 * endian format, a and b , both of size len bytes. 57 * It computes (a + b) mod 2^(8*len) in constant time for a given length, and overwrites a 58 * with the result. 59 * This function was introduced in libsodium 1.0.7. */ 60 void sodium_add(ubyte* a, const(ubyte)* b, const size_t len); 61 62 /** 63 * Hexadecimal encoding. 64 * The sodium_bin2hex() function converts bin_len bytes stored at bin into a hexadecimal 65 * string. 66 * The string is stored into hex and includes a nul byte ( \0 ) terminator. 67 * hex_maxlen is the maximum number of bytes that the function is allowed to write starting at 68 * hex . It should be at least bin_len * 2 + 1 . 69 * The function returns hex on success, or null on overflow. It evaluates in constant time for 70 * a given size. */ 71 char* sodium_bin2hex(const(char*) hex, const size_t hex_maxlen, const(ubyte*) bin, const size_t bin_len); 72 73 74 /** 75 * Hexadecimal decoding. 76 * The sodium_hex2bin() function parses a hexadecimal string hex and converts it to a byte 77 * sequence. 78 * hex does not have to be nul terminated, as the number of characters to parse is supplied 79 * via the hex_len parameter. 80 * ignore is a string of characters to skip. For example, the string ": " allows columns and 81 * spaces to be present at any locations in the hexadecimal string. These characters will just 82 * be ignored. As a result, "69:FC" , "69 FC" , "69 : FC" and "69FC" will be valid inputs, 83 * and will produce the same output. 84 * ignore can be set to null in order to disallow any non-hexadecimal character. 85 * bin_maxlen is the maximum number of bytes to put into bin . 86 * The parser stops when a non-hexadecimal, non-ignored character is found or when 87 * bin_maxlen bytes have been written. 88 * The function returns -1 if more than bin_maxlen bytes would be required to store the 89 * parsed string. It returns 0 on success and sets hex_end , if it is not null , to a pointer to 90 * the character following the last parsed character. 91 * It evaluates in constant time for a given length and format. */ 92 int sodium_hex2bin(const(ubyte)* bin, const size_t bin_maxlen, const(char*) hex, const size_t hex_len, const(char*) ignore, const(size_t*) bin_len, const(char**) hex_end); 93 94 /** 95 * Locking memory 96 * The sodium_mlock() function locks at least len bytes of memory starting at addr. 97 * This can help avoid swapping sensitive data to disk. 98 * In addition, it is recommended to totally disable swap partitions on machines processing 99 * sensitive data, or, as a second choice, use encrypted swap partitions. 100 * For similar reasons, on Unix systems, one should also disable core dumps when running 101 * crypto code outside a development environment. This can be achieved using a shell built-in 102 * such as ulimit or programatically using setrlimit(RLIMIT_CORE, &(struct rlimit) {0, 0}) . 103 * On operating systems where this feature is implemented, kernel crash dumps should also be 104 * disabled (e.g. https://help.ubuntu.com/lts/serverguide/kernel-crash-dump.html). 105 * sodium_mlock() wraps mlock() and VirtualLock() . Note: Many systems place limits on 106 * the amount of memory that may be locked by a process. Care should be taken to raise those 107 * limits (e.g. Unix ulimits) where neccessary. sodium_lock() will return -1 when any limit is 108 * reached. 109 * On systems where it is supported, sodium_mlock() also wraps madvise() and advises the 110 * kernel not to include the locked memory in core dumps. 111 */ 112 int sodium_mlock(const(void*) addr, const size_t len); 113 114 /** 115 * The sodium_munlock() function should be called after locked memory is not being used any 116 * more. It will zero len bytes starting at addr before actually flagging the pages as 117 * swappable again. Calling sodium_memzero() prior to sodium_munlock() is thus not required. 118 */ 119 int sodium_munlock(const(void*) addr, const size_t len); 120 121 /** WARNING: sodium_malloc() and sodium_allocarray() are not general-purpose 122 * allocation functions. 123 * 124 * They return a pointer to a region filled with 0xd0 bytes, immediately 125 * followed by a guard page. 126 * As a result, accessing a single byte after the requested allocation size 127 * will intentionally trigger a segmentation fault. 128 * 129 * A canary and an additional guard page placed before the beginning of the 130 * region may also kill the process if a buffer underflow is detected. 131 * 132 * The memory layout is: 133 * [unprotected region size (read only)][guard page (no access)][unprotected pages (read/write)][guard page (no access)] 134 * With the layout of the unprotected pages being: 135 * [optional padding][16-bytes canary][user region] 136 * 137 * However: 138 * - These functions are significantly slower than standard functions 139 * - Each allocation requires 3 or 4 additional pages 140 * - The returned address will not be aligned if the allocation size is not 141 * a multiple of the required alignment. For this reason, these functions 142 * are designed to store data, such as secret keys and messages. 143 * 144 * sodium_malloc() can be used to allocate any libsodium data structure, 145 * with the exception of crypto_generichash_state. 146 * 147 * The crypto_generichash_state structure is packed and its length is 148 * either 357 or 361 bytes. For this reason, when using sodium_malloc() to 149 * allocate a crypto_generichash_state structure, padding must be added in 150 * order to ensure proper alignment: 151 * state = sodium_malloc((crypto_generichash_statebytes() + (size_t) 63U) 152 * & ~(size_t) 63U); 153 */ 154 void* sodium_malloc(const size_t size); 155 156 /** WARNING: sodium_malloc() and sodium_allocarray() are not general-purpose 157 * allocation functions. 158 * 159 * They return a pointer to a region filled with 0xd0 bytes, immediately 160 * followed by a guard page. 161 * As a result, accessing a single byte after the requested allocation size 162 * will intentionally trigger a segmentation fault. 163 * 164 * A canary and an additional guard page placed before the beginning of the 165 * region may also kill the process if a buffer underflow is detected. 166 * 167 * The memory layout is: 168 * [unprotected region size (read only)][guard page (no access)][unprotected pages (read/write)][guard page (no access)] 169 * With the layout of the unprotected pages being: 170 * [optional padding][16-bytes canary][user region] 171 * 172 * However: 173 * - These functions are significantly slower than standard functions 174 * - Each allocation requires 3 or 4 additional pages 175 * - The returned address will not be aligned if the allocation size is not 176 * a multiple of the required alignment. For this reason, these functions 177 * are designed to store data, such as secret keys and messages. 178 * 179 * sodium_malloc() can be used to allocate any libsodium data structure, 180 * with the exception of crypto_generichash_state. 181 * 182 * The crypto_generichash_state structure is packed and its length is 183 * either 357 or 361 bytes. For this reason, when using sodium_malloc() to 184 * allocate a crypto_generichash_state structure, padding must be added in 185 * order to ensure proper alignment: 186 * state = sodium_malloc((crypto_generichash_statebytes() + (size_t) 63U) 187 * & ~(size_t) 63U); 188 */ 189 void* sodium_allocarray(size_t count, size_t size); 190 void sodium_free(void* ptr); 191 192 /** 193 * The sodium_mprotect_noaccess() function makes a region allocated using sodium_malloc() 194 * or sodium_allocarray() inaccessible. It cannot be read or written, but the data are 195 * preserved. 196 * This function can be used to make confidential data inaccessible except when actually 197 * needed for a specific operation. 198 */ 199 int sodium_mprotect_noaccess(void* ptr); 200 201 /** 202 * The sodium_mprotect_readonly() function marks a region allocated using sodium_malloc() 203 * or sodium_allocarray() as read-only. 204 * Attempting to modify the data will cause the process to terminate. 205 */ 206 int sodium_mprotect_readonly(void* ptr); 207 208 /** 209 * The sodium_mprotect_readwrite() function marks a region allocated using sodium_malloc() 210 * or sodium_allocarray() as readable and writable, after having been protected using 211 * sodium_mprotect_readonly() or sodium_mprotect_noaccess(). 212 */ 213 int sodium_mprotect_readwrite(void* ptr); 214