utils.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #ifndef sodium_utils_H
  2. #define sodium_utils_H
  3. #include <stddef.h>
  4. #include "export.h"
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. #ifndef SODIUM_C99
  9. # if defined(__cplusplus) || !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
  10. # define SODIUM_C99(X)
  11. # else
  12. # define SODIUM_C99(X) X
  13. # endif
  14. #endif
  15. SODIUM_EXPORT
  16. void sodium_memzero(void * const pnt, const size_t len);
  17. SODIUM_EXPORT
  18. void sodium_stackzero(const size_t len);
  19. /*
  20. * WARNING: sodium_memcmp() must be used to verify if two secret keys
  21. * are equal, in constant time.
  22. * It returns 0 if the keys are equal, and -1 if they differ.
  23. * This function is not designed for lexicographical comparisons.
  24. */
  25. SODIUM_EXPORT
  26. int sodium_memcmp(const void * const b1_, const void * const b2_, size_t len)
  27. __attribute__ ((warn_unused_result));
  28. /*
  29. * sodium_compare() returns -1 if b1_ < b2_, 1 if b1_ > b2_ and 0 if b1_ == b2_
  30. * It is suitable for lexicographical comparisons, or to compare nonces
  31. * and counters stored in little-endian format.
  32. * However, it is slower than sodium_memcmp().
  33. */
  34. SODIUM_EXPORT
  35. int sodium_compare(const unsigned char *b1_, const unsigned char *b2_,
  36. size_t len) __attribute__ ((warn_unused_result));
  37. SODIUM_EXPORT
  38. int sodium_is_zero(const unsigned char *n, const size_t nlen);
  39. SODIUM_EXPORT
  40. void sodium_increment(unsigned char *n, const size_t nlen);
  41. SODIUM_EXPORT
  42. void sodium_add(unsigned char *a, const unsigned char *b, const size_t len);
  43. SODIUM_EXPORT
  44. void sodium_sub(unsigned char *a, const unsigned char *b, const size_t len);
  45. SODIUM_EXPORT
  46. char *sodium_bin2hex(char * const hex, const size_t hex_maxlen,
  47. const unsigned char * const bin, const size_t bin_len)
  48. __attribute__ ((nonnull(1)));
  49. SODIUM_EXPORT
  50. int sodium_hex2bin(unsigned char * const bin, const size_t bin_maxlen,
  51. const char * const hex, const size_t hex_len,
  52. const char * const ignore, size_t * const bin_len,
  53. const char ** const hex_end)
  54. __attribute__ ((nonnull(1)));
  55. #define sodium_base64_VARIANT_ORIGINAL 1
  56. #define sodium_base64_VARIANT_ORIGINAL_NO_PADDING 3
  57. #define sodium_base64_VARIANT_URLSAFE 5
  58. #define sodium_base64_VARIANT_URLSAFE_NO_PADDING 7
  59. /*
  60. * Computes the required length to encode BIN_LEN bytes as a base64 string
  61. * using the given variant. The computed length includes a trailing \0.
  62. */
  63. #define sodium_base64_ENCODED_LEN(BIN_LEN, VARIANT) \
  64. (((BIN_LEN) / 3U) * 4U + \
  65. ((((BIN_LEN) - ((BIN_LEN) / 3U) * 3U) | (((BIN_LEN) - ((BIN_LEN) / 3U) * 3U) >> 1)) & 1U) * \
  66. (4U - (~((((VARIANT) & 2U) >> 1) - 1U) & (3U - ((BIN_LEN) - ((BIN_LEN) / 3U) * 3U)))) + 1U)
  67. SODIUM_EXPORT
  68. size_t sodium_base64_encoded_len(const size_t bin_len, const int variant);
  69. SODIUM_EXPORT
  70. char *sodium_bin2base64(char * const b64, const size_t b64_maxlen,
  71. const unsigned char * const bin, const size_t bin_len,
  72. const int variant) __attribute__ ((nonnull(1)));
  73. SODIUM_EXPORT
  74. int sodium_base642bin(unsigned char * const bin, const size_t bin_maxlen,
  75. const char * const b64, const size_t b64_len,
  76. const char * const ignore, size_t * const bin_len,
  77. const char ** const b64_end, const int variant)
  78. __attribute__ ((nonnull(1)));
  79. SODIUM_EXPORT
  80. int sodium_mlock(void * const addr, const size_t len)
  81. __attribute__ ((nonnull));
  82. SODIUM_EXPORT
  83. int sodium_munlock(void * const addr, const size_t len)
  84. __attribute__ ((nonnull));
  85. /* WARNING: sodium_malloc() and sodium_allocarray() are not general-purpose
  86. * allocation functions.
  87. *
  88. * They return a pointer to a region filled with 0xd0 bytes, immediately
  89. * followed by a guard page.
  90. * As a result, accessing a single byte after the requested allocation size
  91. * will intentionally trigger a segmentation fault.
  92. *
  93. * A canary and an additional guard page placed before the beginning of the
  94. * region may also kill the process if a buffer underflow is detected.
  95. *
  96. * The memory layout is:
  97. * [unprotected region size (read only)][guard page (no access)][unprotected pages (read/write)][guard page (no access)]
  98. * With the layout of the unprotected pages being:
  99. * [optional padding][16-bytes canary][user region]
  100. *
  101. * However:
  102. * - These functions are significantly slower than standard functions
  103. * - Each allocation requires 3 or 4 additional pages
  104. * - The returned address will not be aligned if the allocation size is not
  105. * a multiple of the required alignment. For this reason, these functions
  106. * are designed to store data, such as secret keys and messages.
  107. *
  108. * sodium_malloc() can be used to allocate any libsodium data structure.
  109. *
  110. * The crypto_generichash_state structure is packed and its length is
  111. * either 357 or 361 bytes. For this reason, when using sodium_malloc() to
  112. * allocate a crypto_generichash_state structure, padding must be added in
  113. * order to ensure proper alignment. crypto_generichash_statebytes()
  114. * returns the rounded up structure size, and should be prefered to sizeof():
  115. * state = sodium_malloc(crypto_generichash_statebytes());
  116. */
  117. SODIUM_EXPORT
  118. void *sodium_malloc(const size_t size)
  119. __attribute__ ((malloc));
  120. SODIUM_EXPORT
  121. void *sodium_allocarray(size_t count, size_t size)
  122. __attribute__ ((malloc));
  123. SODIUM_EXPORT
  124. void sodium_free(void *ptr);
  125. SODIUM_EXPORT
  126. int sodium_mprotect_noaccess(void *ptr) __attribute__ ((nonnull));
  127. SODIUM_EXPORT
  128. int sodium_mprotect_readonly(void *ptr) __attribute__ ((nonnull));
  129. SODIUM_EXPORT
  130. int sodium_mprotect_readwrite(void *ptr) __attribute__ ((nonnull));
  131. SODIUM_EXPORT
  132. int sodium_pad(size_t *padded_buflen_p, unsigned char *buf,
  133. size_t unpadded_buflen, size_t blocksize, size_t max_buflen)
  134. __attribute__ ((nonnull(2)));
  135. SODIUM_EXPORT
  136. int sodium_unpad(size_t *unpadded_buflen_p, const unsigned char *buf,
  137. size_t padded_buflen, size_t blocksize)
  138. __attribute__ ((nonnull(2)));
  139. /* -------- */
  140. int _sodium_alloc_init(void);
  141. #ifdef __cplusplus
  142. }
  143. #endif
  144. #endif