tor  0.4.2.0-alpha-dev
Macros | Functions | Variables
binascii.c File Reference
#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

◆ 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

◆ 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.

References hex_decode_digit(), and tor_assert().

Referenced by decode_hashed_passwords(), getinfo_helper_downloads_bridge(), getinfo_helper_downloads_cert(), getinfo_helper_downloads_desc(), and hexdigest_to_digest().

◆ base16_encode()

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

◆ 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.

References SIZE_T_CEILING, and tor_assert().

Referenced by rend_cache_lookup_v2_desc_as_dir().

◆ 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.

References tor_assert().

Referenced by hs_lookup_last_hid_serv_request(), purge_v2_hidserv_req(), rend_cache_clean_v2_descs_as_dir(), rend_cache_store_v2_desc_as_dir(), and rend_get_service_id().

◆ 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.

References base64_decode_table, and X.

Referenced by decode_hashed_passwords(), digest256_from_base64(), and digest_from_base64().

◆ 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.

References tor_assert().

◆ 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.

References base64_encode_size(), and SIZE_T_CEILING.

Referenced by base64_encode_nopad(), curve25519_public_to_base64(), and pem_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.

References base64_encode(), and tor_assert().

Referenced by 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.

References tor_assert().

Referenced by alloc_http_authenticator(), base64_encode(), 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.

References base16_encode().

Referenced by bridge_resolve_conflicts(), entry_guard_describe(), geoip_db_digest(), and MOCK_IMPL().

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.

Referenced by base64_decode().

◆ 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.