@[translated] module sodium // External C type declarations (from headers) struct C.Crypto_aead_aes256gcm_state_ {} fn C.sodium_memzero(pnt voidptr, len usize) pub fn sodium_memzero(pnt voidptr, len usize) { C.sodium_memzero(pnt, len) } fn C.sodium_stackzero(len usize) pub fn sodium_stackzero(len usize) { C.sodium_stackzero(len) } // // *WARNING: sodium_memcmp() must be used to verify if two secret keys // *are equal, in constant time. // *It returns 0 if the keys are equal, and -1 if they differ. // *This function is not designed for lexicographical comparisons. // fn C.sodium_memcmp(b1_ voidptr, b2_ voidptr, len usize) int pub fn sodium_memcmp(b1_ voidptr, b2_ voidptr, len usize) int { return C.sodium_memcmp(b1_, b2_, len) } // // *sodium_compare() returns -1 if b1_ < b2_, 1 if b1_ > b2_ and 0 if b1_ == b2_ // *It is suitable for lexicographical comparisons, or to compare nonces // *and counters stored in little-endian format. // *However, it is slower than sodium_memcmp(). // fn C.sodium_compare(b1_ &u8, b2_ &u8, len usize) int pub fn sodium_compare(b1_ &u8, b2_ &u8, len usize) int { return C.sodium_compare(b1_, b2_, len) } fn C.sodium_increment(n &u8, nlen usize) pub fn sodium_increment(n &u8, nlen usize) { C.sodium_increment(n, nlen) } fn C.sodium_add(a &u8, b &u8, len usize) pub fn sodium_add(a &u8, b &u8, len usize) { C.sodium_add(a, b, len) } fn C.sodium_sub(a &u8, b &u8, len usize) pub fn sodium_sub(a &u8, b &u8, len usize) { C.sodium_sub(a, b, len) } fn C.sodium_bin2hex(hex &i8, hex_maxlen usize, bin &u8, bin_len usize) &i8 pub fn sodium_bin2hex(hex &i8, hex_maxlen usize, bin &u8, bin_len usize) &i8 { return C.sodium_bin2hex(hex, hex_maxlen, bin, bin_len) } fn C.sodium_hex2bin(bin &u8, bin_maxlen usize, hex &i8, hex_len usize, ignore &i8, bin_len &usize, hex_end &&u8) int pub fn sodium_hex2bin(bin &u8, bin_maxlen usize, hex &i8, hex_len usize, ignore &i8, bin_len &usize, hex_end &&u8) int { return C.sodium_hex2bin(bin, bin_maxlen, hex, hex_len, ignore, bin_len, hex_end) } // // *Computes the required length to encode BIN_LEN bytes as a base64 string // *using the given variant. The computed length includes a trailing \0. // fn C.sodium_base64_encoded_len(bin_len usize, variant int) usize pub fn sodium_base64_encoded_len(bin_len usize, variant int) usize { return C.sodium_base64_encoded_len(bin_len, variant) } fn C.sodium_bin2base64(b64 &i8, b64_maxlen usize, bin &u8, bin_len usize, variant int) &i8 pub fn sodium_bin2base64(b64 &i8, b64_maxlen usize, bin &u8, bin_len usize, variant int) &i8 { return C.sodium_bin2base64(b64, b64_maxlen, bin, bin_len, variant) } fn C.sodium_base642bin(bin &u8, bin_maxlen usize, b64 &i8, b64_len usize, ignore &i8, bin_len &usize, b64_end &&u8, variant int) int pub fn sodium_base642bin(bin &u8, bin_maxlen usize, b64 &i8, b64_len usize, ignore &i8, bin_len &usize, b64_end &&u8, variant int) int { return C.sodium_base642bin(bin, bin_maxlen, b64, b64_len, ignore, bin_len, b64_end, variant) } fn C.sodium_ip2bin(bin &u8, ip &i8, ip_len_ usize) int pub fn sodium_ip2bin(bin &u8, ip &i8, ip_len_ usize) int { return C.sodium_ip2bin(bin, ip, ip_len_) } fn C.sodium_bin2ip(ip &i8, ip_maxlen usize, bin &u8) &i8 pub fn sodium_bin2ip(ip &i8, ip_maxlen usize, bin &u8) &i8 { return C.sodium_bin2ip(ip, ip_maxlen, bin) } fn C.sodium_mlock(addr voidptr, len usize) int pub fn sodium_mlock(addr voidptr, len usize) int { return C.sodium_mlock(addr, len) } fn C.sodium_munlock(addr voidptr, len usize) int pub fn sodium_munlock(addr voidptr, len usize) int { return C.sodium_munlock(addr, len) } // WARNING: sodium_malloc() and sodium_allocarray() are not general-purpose // *allocation functions. // * *They return a pointer to a region filled with 0xd0 bytes, immediately // *followed by a guard page. As a result, accessing a single byte after the // *requested allocation size will intentionally trigger a segmentation fault. // * *A canary and an additional guard page placed before the beginning of the // *region may also kill the process if a buffer underflow is detected. // * *The memory layout is: // *[unprotected region size (read only)][guard page (no access)][unprotected pages // *(read/write)][guard page (no access)] // * *The layout of the unprotected pages is: // *[optional padding][16-bytes canary][user region] // * *Important limitations: // *- These functions are significantly slower than standard allocation functions. // *- Each allocation requires 3 or 4 additional pages. // *- The returned address will not be aligned if the allocation size is not // * a multiple of the required alignment. For this reason, these functions // * are designed to store data such as secret keys and messages. // * *sodium_malloc() can be used to allocate any libsodium data structure. // * *The crypto_generichash_state structure is packed and its length is // *either 357 or 361 bytes. When using sodium_malloc() to allocate a // *crypto_generichash_state structure, padding must be added to ensure // *proper alignment. Use crypto_generichash_statebytes() rather than sizeof(): // * * state = sodium_malloc(crypto_generichash_statebytes()); // fn C.sodium_malloc(size usize) voidptr pub fn sodium_malloc(size usize) voidptr { return C.sodium_malloc(size) } fn C.sodium_allocarray(count usize, size usize) voidptr pub fn sodium_allocarray(count usize, size usize) voidptr { return C.sodium_allocarray(count, size) } fn C.sodium_free(ptr voidptr) pub fn sodium_free(ptr voidptr) { C.sodium_free(ptr) } fn C.sodium_mprotect_noaccess(ptr voidptr) int pub fn sodium_mprotect_noaccess(ptr voidptr) int { return C.sodium_mprotect_noaccess(ptr) } fn C.sodium_mprotect_readonly(ptr voidptr) int pub fn sodium_mprotect_readonly(ptr voidptr) int { return C.sodium_mprotect_readonly(ptr) } fn C.sodium_mprotect_readwrite(ptr voidptr) int pub fn sodium_mprotect_readwrite(ptr voidptr) int { return C.sodium_mprotect_readwrite(ptr) } fn C.sodium_pad(padded_buflen_p &usize, buf &u8, unpadded_buflen usize, blocksize usize, max_buflen usize) int pub fn sodium_pad(padded_buflen_p &usize, buf &u8, unpadded_buflen usize, blocksize usize, max_buflen usize) int { return C.sodium_pad(padded_buflen_p, buf, unpadded_buflen, blocksize, max_buflen) } fn C.sodium_unpad(unpadded_buflen_p &usize, buf &u8, padded_buflen usize, blocksize usize) int pub fn sodium_unpad(unpadded_buflen_p &usize, buf &u8, padded_buflen usize, blocksize usize) int { return C.sodium_unpad(unpadded_buflen_p, buf, padded_buflen, blocksize) } // --------