Tor 0.4.9.0-alpha-dev
Macros | Functions | Variables
binascii.c File Reference

Miscellaneous functions for encoding and decoding various things in base{16,32,64}. More...

#include "orconfig.h"
#include "lib/encoding/binascii.h"
#include "lib/log/log.h"
#include "lib/log/util_bug.h"
#include "lib/cc/torint.h"
#include "lib/string/compat_ctype.h"
#include "lib/intmath/muldiv.h"
#include "lib/malloc/malloc.h"
#include <stddef.h>
#include <string.h>
#include <stdlib.h>

Go to the source code of this file.

Macros

#define BASE64_OPENSSL_LINELEN   64
 
#define ENCODE_CHAR(ch)
 
#define ENCODE_N(idx)    ENCODE_CHAR(base64_encode_table[(n >> ((3 - idx) * 6)) & 0x3f])
 
#define ENCODE_PAD()   ENCODE_CHAR('=')
 
#define X   255
 
#define SP   64
 
#define PAD   65
 

Functions

const char * hex_str (const char *from, size_t fromlen)
 
size_t base32_encoded_size (size_t srclen)
 
void base32_encode (char *dest, size_t destlen, const char *src, size_t srclen)
 
int base32_decode (char *dest, size_t destlen, const char *src, size_t srclen)
 
size_t base64_encode_size (size_t srclen, int flags)
 
size_t base64_decode_maxsize (size_t srclen)
 
int base64_encode (char *dest, size_t destlen, const char *src, size_t srclen, int flags)
 
int base64_encode_nopad (char *dest, size_t destlen, const uint8_t *src, size_t srclen)
 
int base64_decode (char *dest, size_t destlen, const char *src, size_t srclen)
 
void base16_encode (char *dest, size_t destlen, const char *src, size_t srclen)
 
int base16_decode (char *dest, size_t destlen, const char *src, size_t srclen)
 

Variables

static const char base64_encode_table [64]
 
static const uint8_t base64_decode_table [256]
 

Detailed Description

Miscellaneous functions for encoding and decoding various things in base{16,32,64}.

Definition in file binascii.c.

Macro Definition Documentation

◆ BASE64_OPENSSL_LINELEN

#define BASE64_OPENSSL_LINELEN   64

Definition at line 153 of file binascii.c.

◆ ENCODE_CHAR

#define ENCODE_CHAR (   ch)
Value:
STMT_BEGIN \
*d++ = ch; \
if (flags & BASE64_ENCODE_MULTILINE) { \
if (++linelen % BASE64_OPENSSL_LINELEN == 0) { \
linelen = 0; \
*d++ = '\n'; \
} \
} \
STMT_END

◆ PAD

#define PAD   65

Definition at line 358 of file binascii.c.

◆ SP

#define SP   64

Definition at line 357 of file binascii.c.

◆ X

#define X   255

Special values used for the base64_decode_table

Definition at line 356 of file binascii.c.

Function Documentation

◆ base16_decode()

int base16_decode ( char *  dest,
size_t  destlen,
const char *  src,
size_t  srclen 
)

Given a hexadecimal string of srclen bytes in src, decode it and store the result in the destlen-byte buffer at dest. Return the number of bytes decoded on success, -1 on failure. If destlen is greater than INT_MAX or less than half of srclen, -1 is returned.

Definition at line 506 of file binascii.c.

Referenced by cdm_entry_get_sha3_value(), connection_dir_retry_bridges(), decode_hashed_passwords(), dir_routerdesc_download_failed(), getinfo_helper_downloads_bridge(), getinfo_helper_downloads_cert(), getinfo_helper_downloads_desc(), hexdigest_to_digest(), and parse_one_diff_hash().

◆ base16_encode()

void base16_encode ( char *  dest,
size_t  destlen,
const char *  src,
size_t  srclen 
)

Encode the srclen bytes at src in a NUL-terminated, uppercase hexadecimal string; store it in the destlen-byte buffer dest.

Definition at line 478 of file binascii.c.

Referenced by authority_cert_is_denylisted(), crypto_pk_get_fingerprint(), crypto_pk_get_hashed_fingerprint(), do_hash_password(), hex_str(), microdesc_relay_is_outdated_dirserver(), node_get_verbose_nickname(), node_get_verbose_nickname_by_id(), orconn_target_get_name(), and rep_hist_dump_stats().

◆ base32_decode()

int base32_decode ( char *  dest,
size_t  destlen,
const char *  src,
size_t  srclen 
)

Implements base32 decoding as in RFC 4648. Return the number of bytes decoded if successful; -1 otherwise.

Definition at line 90 of file binascii.c.

Referenced by hs_parse_address_no_log().

◆ base32_encode()

void base32_encode ( char *  dest,
size_t  destlen,
const char *  src,
size_t  srclen 
)

Implements base32 encoding as in RFC 4648.

Definition at line 60 of file binascii.c.

Referenced by hs_build_address(), and hs_lookup_last_hid_serv_request().

◆ base32_encoded_size()

size_t base32_encoded_size ( size_t  srclen)

Definition at line 49 of file binascii.c.

◆ base64_decode()

int base64_decode ( char *  dest,
size_t  destlen,
const char *  src,
size_t  srclen 
)

Base64 decode srclen bytes of data from src. Write the result into dest, if it will fit within destlen bytes. Return the number of bytes written on success; -1 if destlen is too short, or other failure.

NOTE 1: destlen is checked conservatively, as though srclen contained no spaces or padding.

NOTE 2: This implementation does not check for the correct number of padding "=" characters at the end of the string, and does not check for internal padding characters.

Definition at line 396 of file binascii.c.

Referenced by commit_decode(), curve25519_public_from_base64(), decode_auth_client(), decode_hashed_passwords(), digest256_from_base64(), digest_from_base64(), ed25519_signature_from_base64(), and reveal_decode().

◆ base64_decode_maxsize()

size_t base64_decode_maxsize ( size_t  srclen)

Return an upper bound on the number of bytes that might be needed to hold the data from decoding the base64 string srclen. This is only an upper bound, since some part of the base64 string might be padding or space.

Definition at line 187 of file binascii.c.

◆ base64_encode()

int base64_encode ( char *  dest,
size_t  destlen,
const char *  src,
size_t  srclen,
int  flags 
)

Base64 encode srclen bytes of data from src. Write the result into dest, if it will fit within destlen bytes. Return the number of bytes written on success; -1 if destlen is too short, or other failure.

If flags&BASE64_ENCODE_MULTILINE is true, return encoded output in multiline format (64 character, ‘
’ terminated lines).

Definition at line 215 of file binascii.c.

Referenced by base64_encode_nopad(), commit_encode(), curve25519_public_to_base64(), encode_client_auth_cred_for_control_port(), encode_legacy_key(), pem_encode(), reveal_encode(), and sr_srv_encode().

◆ base64_encode_nopad()

int base64_encode_nopad ( char *  dest,
size_t  destlen,
const uint8_t *  src,
size_t  srclen 
)

As base64_encode, but do not add any internal spaces, and remove external padding from the output stream. dest must be at least base64_encode_size(srclen, 0), including space for the removed external padding.

Definition at line 329 of file binascii.c.

Referenced by curve25519_public_to_base64(), digest256_to_base64(), digest_to_base64(), and ed25519_signature_to_base64().

◆ base64_encode_size()

size_t base64_encode_size ( size_t  srclen,
int  flags 
)

Return the Base64 encoded size of srclen bytes of data in bytes.

(WATCH OUT: This API does not count the terminating NUL byte, but base32_encoded_size does.)

If flags&BASE64_ENCODE_MULTILINE is true, return the size of the encoded output as multiline output (64 character, ‘
’ terminated lines).

Definition at line 166 of file binascii.c.

Referenced by alloc_http_authenticator(), base64_encode(), encrypt_desc_data_and_base64(), and pem_encoded_size().

◆ hex_str()

const char * hex_str ( const char *  from,
size_t  fromlen 
)

Return a pointer to a NUL-terminated hexadecimal string encoding the first fromlen bytes of from. (fromlen must be <= 32.) The result does not need to be deallocated, but repeated calls to hex_str will trash old results.

Definition at line 34 of file binascii.c.

Referenced by bridge_resolve_conflicts(), channel_dump_statistics(), entry_guard_describe(), fmt_nonce(), geoip_db_digest(), and hs_control_desc_event_upload().

Variable Documentation

◆ base64_decode_table

const uint8_t base64_decode_table[256]
static
Initial value:
= {
X, X, X, X, X, X, X, X, X, SP, SP, SP, X, SP, X, X,
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
SP, X, X, X, X, X, X, X, X, X, X, 62, X, X, X, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, X, X, X, PAD, X, X,
X, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, X, X, X, X, X,
X, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, X, X, X, X, X,
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
}
#define X
Definition: binascii.c:356

Internal table mapping byte values to what they represent in base64. Numbers 0..63 are 6-bit integers. SPs are spaces, and should be skipped. Xs are invalid and must not appear in base64. PAD indicates end-of-string.

Definition at line 364 of file binascii.c.

◆ base64_encode_table

const char base64_encode_table[64]
static
Initial value:
= {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'
}

Internal table mapping 6 bit values to the Base64 alphabet.

Definition at line 195 of file binascii.c.