Skip to content

Commit

Permalink
clang-format Mosh
Browse files Browse the repository at this point in the history
Run clang-format over the Mosh source tree. This is a large change and
has been factored into its own commit for auditability. Reproduce it
with

    find . -name \*.cc -or -name \*.h | while read f; do clang-format -i --style=file $f; done
  • Loading branch information
bbarenblat committed Aug 8, 2023
1 parent 45dcafe commit 36a5a1e
Show file tree
Hide file tree
Showing 77 changed files with 4,900 additions and 4,910 deletions.
60 changes: 28 additions & 32 deletions src/crypto/ae.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ extern "C" {
/* Return status codes: Negative return values indicate an error occurred.
* For full explanations of error values, consult the implementation's
* documentation. */
#define AE_SUCCESS ( 0) /* Indicates successful completion of call */
#define AE_INVALID (-1) /* Indicates bad tag during decryption */
#define AE_NOT_SUPPORTED (-2) /* Indicates unsupported option requested */
#define AE_SUCCESS ( 0 ) /* Indicates successful completion of call */
#define AE_INVALID ( -1 ) /* Indicates bad tag during decryption */
#define AE_NOT_SUPPORTED ( -2 ) /* Indicates unsupported option requested */

/* Flags: When data can be processed "incrementally", these flags are used
* to indicate whether the submitted data is the last or not. */
#define AE_FINALIZE (1) /* This is the last of data */
#define AE_PENDING (0) /* More data of is coming */
#define AE_FINALIZE ( 1 ) /* This is the last of data */
#define AE_PENDING ( 0 ) /* More data of is coming */

/* --------------------------------------------------------------------------
*
Expand All @@ -55,10 +55,10 @@ typedef struct _ae_ctx ae_ctx;
*
* ----------------------------------------------------------------------- */

ae_ctx* ae_allocate (void *misc); /* Allocate ae_ctx, set optional ptr */
void ae_free (ae_ctx *ctx); /* Deallocate ae_ctx struct */
int ae_clear (ae_ctx *ctx); /* Undo initialization */
int ae_ctx_sizeof(void); /* Return sizeof(ae_ctx) */
ae_ctx* ae_allocate( void* misc ); /* Allocate ae_ctx, set optional ptr */
void ae_free( ae_ctx* ctx ); /* Deallocate ae_ctx struct */
int ae_clear( ae_ctx* ctx ); /* Undo initialization */
int ae_ctx_sizeof( void ); /* Return sizeof(ae_ctx) */
/* ae_allocate() allocates an ae_ctx structure, but does not initialize it.
* ae_free() deallocates an ae_ctx structure, but does not zeroize it.
* ae_clear() zeroes sensitive values associated with an ae_ctx structure
Expand All @@ -72,11 +72,7 @@ int ae_ctx_sizeof(void); /* Return sizeof(ae_ctx) */
*
* ----------------------------------------------------------------------- */

int ae_init(ae_ctx *ctx,
const void *key,
int key_len,
int nonce_len,
int tag_len);
int ae_init( ae_ctx* ctx, const void* key, int key_len, int nonce_len, int tag_len );
/* --------------------------------------------------------------------------
*
* Initialize an ae_ctx context structure.
Expand All @@ -95,15 +91,15 @@ int ae_init(ae_ctx *ctx,
*
* ----------------------------------------------------------------------- */

int ae_encrypt(ae_ctx *ctx,
const void *nonce,
const void *pt,
int pt_len,
const void *ad,
int ad_len,
void *ct,
void *tag,
int final);
int ae_encrypt( ae_ctx* ctx,
const void* nonce,
const void* pt,
int pt_len,
const void* ad,
int ad_len,
void* ct,
void* tag,
int final );
/* --------------------------------------------------------------------------
*
* Encrypt plaintext; provide for authentication of ciphertext/associated data.
Expand Down Expand Up @@ -132,15 +128,15 @@ int ae_encrypt(ae_ctx *ctx,
*
* ----------------------------------------------------------------------- */

int ae_decrypt(ae_ctx *ctx,
const void *nonce,
const void *ct,
int ct_len,
const void *ad,
int ad_len,
void *pt,
const void *tag,
int final);
int ae_decrypt( ae_ctx* ctx,
const void* nonce,
const void* ct,
int ct_len,
const void* ad,
int ad_len,
void* pt,
const void* tag,
int final );
/* --------------------------------------------------------------------------
*
* Decrypt ciphertext; provide authenticity of plaintext and associated data.
Expand Down
38 changes: 18 additions & 20 deletions src/crypto/base64.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
#include <cstdlib>
#include <cstring>

#include "src/util/fatal_assert.h"
#include "src/crypto/base64.h"
#include "src/util/fatal_assert.h"

static const char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

Expand All @@ -60,27 +60,26 @@ static const unsigned char reverse[] = {
};

/* Reverse maps from an ASCII char to a base64 sixbit value. Returns > 0x3f on failure. */
static unsigned char base64_char_to_sixbit(unsigned char c)
static unsigned char base64_char_to_sixbit( unsigned char c )
{
return reverse[c];
}

bool base64_decode( const char *b64, const size_t b64_len,
uint8_t *raw, size_t *raw_len )
bool base64_decode( const char* b64, const size_t b64_len, uint8_t* raw, size_t* raw_len )
{
fatal_assert( b64_len == 24 ); /* only useful for Mosh keys */
fatal_assert( *raw_len == 16 );

uint32_t bytes = 0;
for (int i = 0; i < 22; i++) {
unsigned char sixbit = base64_char_to_sixbit(*(b64++));
if (sixbit > 0x3f) {
for ( int i = 0; i < 22; i++ ) {
unsigned char sixbit = base64_char_to_sixbit( *( b64++ ) );
if ( sixbit > 0x3f ) {
return false;
}
bytes <<= 6;
bytes |= sixbit;
/* write groups of 3 */
if (i % 4 == 3) {
if ( i % 4 == 3 ) {
raw[0] = bytes >> 16;
raw[1] = bytes >> 8;
raw[2] = bytes;
Expand All @@ -90,33 +89,32 @@ bool base64_decode( const char *b64, const size_t b64_len,
}
/* last byte of output */
*raw = bytes >> 4;
if (b64[0] != '=' || b64[1] != '=') {
if ( b64[0] != '=' || b64[1] != '=' ) {
return false;
}
return true;
}

void base64_encode( const uint8_t *raw, const size_t raw_len,
char *b64, const size_t b64_len )
void base64_encode( const uint8_t* raw, const size_t raw_len, char* b64, const size_t b64_len )
{
fatal_assert( b64_len == 24 ); /* only useful for Mosh keys */
fatal_assert( raw_len == 16 );

/* first 15 bytes of input */
for (int i = 0; i < 5; i++) {
uint32_t bytes = (raw[0] << 16) | (raw[1] << 8) | raw[2];
b64[0] = table[(bytes >> 18) & 0x3f];
b64[1] = table[(bytes >> 12) & 0x3f];
b64[2] = table[(bytes >> 6) & 0x3f];
b64[3] = table[(bytes) & 0x3f];
for ( int i = 0; i < 5; i++ ) {
uint32_t bytes = ( raw[0] << 16 ) | ( raw[1] << 8 ) | raw[2];
b64[0] = table[( bytes >> 18 ) & 0x3f];
b64[1] = table[( bytes >> 12 ) & 0x3f];
b64[2] = table[( bytes >> 6 ) & 0x3f];
b64[3] = table[(bytes)&0x3f];
raw += 3;
b64 += 4;
}

/* last byte of input, last 4 of output */
uint8_t lastchar = *raw;
b64[0] = table[(lastchar >> 2) & 0x3f];
b64[1] = table[(lastchar << 4) & 0x3f];
b64[0] = table[( lastchar >> 2 ) & 0x3f];
b64[1] = table[( lastchar << 4 ) & 0x3f];
b64[2] = '=';
b64[3] = '=';
}
6 changes: 2 additions & 4 deletions src/crypto/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@

#include <cstdint>

bool base64_decode( const char *b64, const size_t b64_len,
uint8_t *raw, size_t *raw_len );
bool base64_decode( const char* b64, const size_t b64_len, uint8_t* raw, size_t* raw_len );

void base64_encode( const uint8_t *raw, const size_t raw_len,
char *b64, const size_t b64_len );
void base64_encode( const uint8_t* raw, const size_t raw_len, char* b64, const size_t b64_len );
85 changes: 40 additions & 45 deletions src/crypto/byteorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,24 @@

#if HAVE_DECL_BE64TOH || HAVE_DECL_BETOH64

# if defined(HAVE_ENDIAN_H)
# include <endian.h>
# elif defined(HAVE_SYS_ENDIAN_H)
# include <sys/types.h>
# include <sys/endian.h>
# endif
#if defined( HAVE_ENDIAN_H )
#include <endian.h>
#elif defined( HAVE_SYS_ENDIAN_H )
#include <sys/endian.h>
#include <sys/types.h>
#endif

#if !HAVE_DECL_BE64TOH && HAVE_DECL_BETOH64
#define be64toh betoh64
#define be16toh betoh16
#endif

#elif HAVE_OSX_SWAP
# include <libkern/OSByteOrder.h>
# define htobe64 OSSwapHostToBigInt64
# define be64toh OSSwapBigToHostInt64
# define htobe16 OSSwapHostToBigInt16
# define be16toh OSSwapBigToHostInt16
#include <libkern/OSByteOrder.h>
#define htobe64 OSSwapHostToBigInt64
#define be64toh OSSwapBigToHostInt64
#define htobe16 OSSwapHostToBigInt16
#define be16toh OSSwapBigToHostInt16

#else

Expand All @@ -70,60 +70,55 @@

/* Use unions rather than casts, to comply with strict aliasing rules. */

inline uint64_t htobe64( uint64_t x ) {
uint8_t xs[ 8 ] = {
static_cast<uint8_t>( ( x >> 56 ) & 0xFF ),
static_cast<uint8_t>( ( x >> 48 ) & 0xFF ),
static_cast<uint8_t>( ( x >> 40 ) & 0xFF ),
static_cast<uint8_t>( ( x >> 32 ) & 0xFF ),
static_cast<uint8_t>( ( x >> 24 ) & 0xFF ),
static_cast<uint8_t>( ( x >> 16 ) & 0xFF ),
static_cast<uint8_t>( ( x >> 8 ) & 0xFF ),
static_cast<uint8_t>( ( x ) & 0xFF ) };
inline uint64_t htobe64( uint64_t x )
{
uint8_t xs[8] = { static_cast<uint8_t>( ( x >> 56 ) & 0xFF ),
static_cast<uint8_t>( ( x >> 48 ) & 0xFF ),
static_cast<uint8_t>( ( x >> 40 ) & 0xFF ),
static_cast<uint8_t>( ( x >> 32 ) & 0xFF ),
static_cast<uint8_t>( ( x >> 24 ) & 0xFF ),
static_cast<uint8_t>( ( x >> 16 ) & 0xFF ),
static_cast<uint8_t>( ( x >> 8 ) & 0xFF ),
static_cast<uint8_t>( (x)&0xFF ) };
union {
const uint8_t *p8;
const uint64_t *p64;
const uint8_t* p8;
const uint64_t* p64;
} u;
u.p8 = xs;
return *u.p64;
}

inline uint64_t be64toh( uint64_t x ) {
inline uint64_t be64toh( uint64_t x )
{
union {
const uint8_t *p8;
const uint64_t *p64;
const uint8_t* p8;
const uint64_t* p64;
} u;
u.p64 = &x;
return ( uint64_t( u.p8[ 0 ] ) << 56 )
| ( uint64_t( u.p8[ 1 ] ) << 48 )
| ( uint64_t( u.p8[ 2 ] ) << 40 )
| ( uint64_t( u.p8[ 3 ] ) << 32 )
| ( uint64_t( u.p8[ 4 ] ) << 24 )
| ( uint64_t( u.p8[ 5 ] ) << 16 )
| ( uint64_t( u.p8[ 6 ] ) << 8 )
| ( uint64_t( u.p8[ 7 ] ) );
return ( uint64_t( u.p8[0] ) << 56 ) | ( uint64_t( u.p8[1] ) << 48 ) | ( uint64_t( u.p8[2] ) << 40 )
| ( uint64_t( u.p8[3] ) << 32 ) | ( uint64_t( u.p8[4] ) << 24 ) | ( uint64_t( u.p8[5] ) << 16 )
| ( uint64_t( u.p8[6] ) << 8 ) | ( uint64_t( u.p8[7] ) );
}

inline uint16_t htobe16( uint16_t x ) {
uint8_t xs[ 2 ] = {
static_cast<uint8_t>( ( x >> 8 ) & 0xFF ),
static_cast<uint8_t>( ( x ) & 0xFF ) };
inline uint16_t htobe16( uint16_t x )
{
uint8_t xs[2] = { static_cast<uint8_t>( ( x >> 8 ) & 0xFF ), static_cast<uint8_t>( (x)&0xFF ) };
union {
const uint8_t *p8;
const uint16_t *p16;
const uint8_t* p8;
const uint16_t* p16;
} u;
u.p8 = xs;
return *u.p16;
}

inline uint16_t be16toh( uint16_t x ) {
inline uint16_t be16toh( uint16_t x )
{
union {
const uint8_t *p8;
const uint16_t *p16;
const uint8_t* p8;
const uint16_t* p16;
} u;
u.p16 = &x;
return ( uint16_t( u.p8[ 0 ] ) << 8 )
| ( uint16_t( u.p8[ 1 ] ) );
return ( uint16_t( u.p8[0] ) << 8 ) | ( uint16_t( u.p8[1] ) );
}

#endif
Expand Down
Loading

0 comments on commit 36a5a1e

Please sign in to comment.