Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:12:40

0001 
0002 #ifndef sodium_utils_H
0003 #define sodium_utils_H
0004 
0005 #include <stddef.h>
0006 
0007 #include "export.h"
0008 
0009 #ifdef __cplusplus
0010 extern "C" {
0011 #endif
0012 
0013 #ifndef SODIUM_C99
0014 # if defined(__cplusplus) || !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
0015 #  define SODIUM_C99(X)
0016 # else
0017 #  define SODIUM_C99(X) X
0018 # endif
0019 #endif
0020 
0021 SODIUM_EXPORT
0022 void sodium_memzero(void * const pnt, const size_t len);
0023 
0024 SODIUM_EXPORT
0025 void sodium_stackzero(const size_t len);
0026 
0027 /*
0028  * WARNING: sodium_memcmp() must be used to verify if two secret keys
0029  * are equal, in constant time.
0030  * It returns 0 if the keys are equal, and -1 if they differ.
0031  * This function is not designed for lexicographical comparisons.
0032  */
0033 SODIUM_EXPORT
0034 int sodium_memcmp(const void * const b1_, const void * const b2_, size_t len)
0035             __attribute__ ((warn_unused_result));
0036 
0037 /*
0038  * sodium_compare() returns -1 if b1_ < b2_, 1 if b1_ > b2_ and 0 if b1_ == b2_
0039  * It is suitable for lexicographical comparisons, or to compare nonces
0040  * and counters stored in little-endian format.
0041  * However, it is slower than sodium_memcmp().
0042  */
0043 SODIUM_EXPORT
0044 int sodium_compare(const unsigned char *b1_, const unsigned char *b2_,
0045                    size_t len) __attribute__ ((warn_unused_result));
0046 
0047 SODIUM_EXPORT
0048 int sodium_is_zero(const unsigned char *n, const size_t nlen);
0049 
0050 SODIUM_EXPORT
0051 void sodium_increment(unsigned char *n, const size_t nlen);
0052 
0053 SODIUM_EXPORT
0054 void sodium_add(unsigned char *a, const unsigned char *b, const size_t len);
0055 
0056 SODIUM_EXPORT
0057 void sodium_sub(unsigned char *a, const unsigned char *b, const size_t len);
0058 
0059 SODIUM_EXPORT
0060 char *sodium_bin2hex(char * const hex, const size_t hex_maxlen,
0061                      const unsigned char * const bin, const size_t bin_len)
0062             __attribute__ ((nonnull(1)));
0063 
0064 SODIUM_EXPORT
0065 int sodium_hex2bin(unsigned char * const bin, const size_t bin_maxlen,
0066                    const char * const hex, const size_t hex_len,
0067                    const char * const ignore, size_t * const bin_len,
0068                    const char ** const hex_end)
0069             __attribute__ ((nonnull(1)));
0070 
0071 #define sodium_base64_VARIANT_ORIGINAL            1
0072 #define sodium_base64_VARIANT_ORIGINAL_NO_PADDING 3
0073 #define sodium_base64_VARIANT_URLSAFE             5
0074 #define sodium_base64_VARIANT_URLSAFE_NO_PADDING  7
0075 
0076 /*
0077  * Computes the required length to encode BIN_LEN bytes as a base64 string
0078  * using the given variant. The computed length includes a trailing \0.
0079  */
0080 #define sodium_base64_ENCODED_LEN(BIN_LEN, VARIANT) \
0081     (((BIN_LEN) / 3U) * 4U + \
0082     ((((BIN_LEN) - ((BIN_LEN) / 3U) * 3U) | (((BIN_LEN) - ((BIN_LEN) / 3U) * 3U) >> 1)) & 1U) * \
0083      (4U - (~((((VARIANT) & 2U) >> 1) - 1U) & (3U - ((BIN_LEN) - ((BIN_LEN) / 3U) * 3U)))) + 1U)
0084 
0085 SODIUM_EXPORT
0086 size_t sodium_base64_encoded_len(const size_t bin_len, const int variant);
0087 
0088 SODIUM_EXPORT
0089 char *sodium_bin2base64(char * const b64, const size_t b64_maxlen,
0090                         const unsigned char * const bin, const size_t bin_len,
0091                         const int variant) __attribute__ ((nonnull(1)));
0092 
0093 SODIUM_EXPORT
0094 int sodium_base642bin(unsigned char * const bin, const size_t bin_maxlen,
0095                       const char * const b64, const size_t b64_len,
0096                       const char * const ignore, size_t * const bin_len,
0097                       const char ** const b64_end, const int variant)
0098             __attribute__ ((nonnull(1)));
0099 
0100 SODIUM_EXPORT
0101 int sodium_mlock(void * const addr, const size_t len)
0102             __attribute__ ((nonnull));
0103 
0104 SODIUM_EXPORT
0105 int sodium_munlock(void * const addr, const size_t len)
0106             __attribute__ ((nonnull));
0107 
0108 /* WARNING: sodium_malloc() and sodium_allocarray() are not general-purpose
0109  * allocation functions.
0110  *
0111  * They return a pointer to a region filled with 0xd0 bytes, immediately
0112  * followed by a guard page.
0113  * As a result, accessing a single byte after the requested allocation size
0114  * will intentionally trigger a segmentation fault.
0115  *
0116  * A canary and an additional guard page placed before the beginning of the
0117  * region may also kill the process if a buffer underflow is detected.
0118  *
0119  * The memory layout is:
0120  * [unprotected region size (read only)][guard page (no access)][unprotected pages (read/write)][guard page (no access)]
0121  * With the layout of the unprotected pages being:
0122  * [optional padding][16-bytes canary][user region]
0123  *
0124  * However:
0125  * - These functions are significantly slower than standard functions
0126  * - Each allocation requires 3 or 4 additional pages
0127  * - The returned address will not be aligned if the allocation size is not
0128  *   a multiple of the required alignment. For this reason, these functions
0129  *   are designed to store data, such as secret keys and messages.
0130  *
0131  * sodium_malloc() can be used to allocate any libsodium data structure.
0132  *
0133  * The crypto_generichash_state structure is packed and its length is
0134  * either 357 or 361 bytes. For this reason, when using sodium_malloc() to
0135  * allocate a crypto_generichash_state structure, padding must be added in
0136  * order to ensure proper alignment. crypto_generichash_statebytes()
0137  * returns the rounded up structure size, and should be preferred to sizeof():
0138  * state = sodium_malloc(crypto_generichash_statebytes());
0139  */
0140 
0141 SODIUM_EXPORT
0142 void *sodium_malloc(const size_t size)
0143             __attribute__ ((malloc));
0144 
0145 SODIUM_EXPORT
0146 void *sodium_allocarray(size_t count, size_t size)
0147             __attribute__ ((malloc));
0148 
0149 SODIUM_EXPORT
0150 void sodium_free(void *ptr);
0151 
0152 SODIUM_EXPORT
0153 int sodium_mprotect_noaccess(void *ptr) __attribute__ ((nonnull));
0154 
0155 SODIUM_EXPORT
0156 int sodium_mprotect_readonly(void *ptr) __attribute__ ((nonnull));
0157 
0158 SODIUM_EXPORT
0159 int sodium_mprotect_readwrite(void *ptr) __attribute__ ((nonnull));
0160 
0161 SODIUM_EXPORT
0162 int sodium_pad(size_t *padded_buflen_p, unsigned char *buf,
0163                size_t unpadded_buflen, size_t blocksize, size_t max_buflen)
0164             __attribute__ ((nonnull(2)));
0165 
0166 SODIUM_EXPORT
0167 int sodium_unpad(size_t *unpadded_buflen_p, const unsigned char *buf,
0168                  size_t padded_buflen, size_t blocksize)
0169             __attribute__ ((nonnull(2)));
0170 
0171 /* -------- */
0172 
0173 int _sodium_alloc_init(void);
0174 
0175 #ifdef __cplusplus
0176 }
0177 #endif
0178 
0179 #endif