Skip to content

Commit

Permalink
fixup! clang-format Mosh
Browse files Browse the repository at this point in the history
  • Loading branch information
bbarenblat committed Jan 20, 2023
1 parent 44d1dfd commit 00a2eb2
Show file tree
Hide file tree
Showing 67 changed files with 1,253 additions and 1,387 deletions.
17 changes: 9 additions & 8 deletions src/crypto/base64.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,21 @@ 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 )
{
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 ) { return false; }
if ( sixbit > 0x3f ) {
return false;
}
bytes <<= 6;
bytes |= sixbit;
/* write groups of 3 */
Expand All @@ -90,13 +90,14 @@ bool base64_decode( const char *b64, const size_t b64_len, uint8_t *raw,
}
/* last byte of output */
*raw = bytes >> 4;
if ( b64[ 0 ] != '=' || b64[ 1 ] != '=' ) { return false; }
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 )
{
const size_t b64_len ) {
fatal_assert( b64_len == 24 ); /* only useful for Mosh keys */
fatal_assert( raw_len == 16 );

Expand Down
12 changes: 4 additions & 8 deletions src/crypto/byteorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@

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

inline uint64_t htobe64( uint64_t x )
{
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 ),
Expand All @@ -88,8 +87,7 @@ inline uint64_t htobe64( uint64_t x )
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;
Expand All @@ -101,8 +99,7 @@ inline uint64_t be64toh( uint64_t x )
| ( uint64_t( u.p8[ 6 ] ) << 8 ) | ( uint64_t( u.p8[ 7 ] ) );
}

inline uint16_t htobe16( uint16_t x )
{
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 {
Expand All @@ -113,8 +110,7 @@ inline uint16_t htobe16( uint16_t x )
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;
Expand Down
71 changes: 34 additions & 37 deletions src/crypto/crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@

using namespace Crypto;

long int myatoi( const char *str )
{
long int myatoi( const char *str ) {
char *end;

errno = 0;
Expand All @@ -60,17 +59,17 @@ long int myatoi( const char *str )
return ret;
}

uint64_t Crypto::unique( void )
{
uint64_t Crypto::unique( void ) {
static uint64_t counter = 0;
uint64_t rv = counter++;
if ( counter == 0 ) { throw CryptoException( "Counter wrapped", true ); }
if ( counter == 0 ) {
throw CryptoException( "Counter wrapped", true );
}
return rv;
}

AlignedBuffer::AlignedBuffer( size_t len, const char *data )
: m_len( len ), m_allocated( NULL ), m_data( NULL )
{
: m_len( len ), m_allocated( NULL ), m_data( NULL ) {
size_t alloc_len = len ? len : 1;
#if defined( HAVE_POSIX_MEMALIGN )
if ( ( 0 != posix_memalign( &m_allocated, 16, alloc_len ) )
Expand All @@ -83,10 +82,14 @@ AlignedBuffer::AlignedBuffer( size_t len, const char *data )
/* malloc() a region 15 bytes larger than we need, and find
the aligned offset within. */
m_allocated = malloc( 15 + alloc_len );
if ( m_allocated == NULL ) { throw std::bad_alloc(); }
if ( m_allocated == NULL ) {
throw std::bad_alloc();
}

uintptr_t iptr = (uintptr_t)m_allocated;
if ( iptr & 0xF ) { iptr += 16 - ( iptr & 0xF ); }
if ( iptr & 0xF ) {
iptr += 16 - ( iptr & 0xF );
}
assert( !( iptr & 0xF ) );
assert( iptr >= (uintptr_t)m_allocated );
assert( iptr <= ( 15 + (uintptr_t)m_allocated ) );
Expand All @@ -95,11 +98,12 @@ AlignedBuffer::AlignedBuffer( size_t len, const char *data )

#endif /* !defined(HAVE_POSIX_MEMALIGN) */

if ( data ) { memcpy( m_data, data, len ); }
if ( data ) {
memcpy( m_data, data, len );
}
}

Base64Key::Base64Key( string printable_key )
{
Base64Key::Base64Key( string printable_key ) {
if ( printable_key.length() != 22 ) {
throw CryptoException( "Key must be 22 letters long." );
}
Expand All @@ -111,7 +115,9 @@ Base64Key::Base64Key( string printable_key )
throw CryptoException( "Key must be well-formed base64." );
}

if ( len != 16 ) { throw CryptoException( "Key must represent 16 octets." ); }
if ( len != 16 ) {
throw CryptoException( "Key must represent 16 octets." );
}

/* to catch changes after the first 128 bits */
if ( printable_key != this->printable_key() ) {
Expand All @@ -123,8 +129,7 @@ Base64Key::Base64Key() { PRNG().fill( key, sizeof( key ) ); }

Base64Key::Base64Key( PRNG &prng ) { prng.fill( key, sizeof( key ) ); }

string Base64Key::printable_key( void ) const
{
string Base64Key::printable_key( void ) const {
char base64[ 24 ];

base64_encode( key, 16, base64, 24 );
Expand All @@ -139,38 +144,34 @@ string Base64Key::printable_key( void ) const
}

Session::Session( Base64Key s_key )
: key( s_key )
, ctx_buf( ae_ctx_sizeof() )
, ctx( (ae_ctx *)ctx_buf.data() )
, blocks_encrypted( 0 )
, plaintext_buffer( RECEIVE_MTU )
, ciphertext_buffer( RECEIVE_MTU )
, nonce_buffer( Nonce::NONCE_LEN )
{
: key( s_key ),
ctx_buf( ae_ctx_sizeof() ),
ctx( (ae_ctx *)ctx_buf.data() ),
blocks_encrypted( 0 ),
plaintext_buffer( RECEIVE_MTU ),
ciphertext_buffer( RECEIVE_MTU ),
nonce_buffer( Nonce::NONCE_LEN ) {
if ( AE_SUCCESS != ae_init( ctx, key.data(), 16, 12, 16 ) ) {
throw CryptoException( "Could not initialize AES-OCB context." );
}
}

Session::~Session() { fatal_assert( ae_clear( ctx ) == AE_SUCCESS ); }

Nonce::Nonce( uint64_t val )
{
Nonce::Nonce( uint64_t val ) {
uint64_t val_net = htobe64( val );

memset( bytes, 0, 4 );
memcpy( bytes + 4, &val_net, 8 );
}

uint64_t Nonce::val( void ) const
{
uint64_t Nonce::val( void ) const {
uint64_t ret;
memcpy( &ret, bytes + 4, 8 );
return be64toh( ret );
}

Nonce::Nonce( const char *s_bytes, size_t len )
{
Nonce::Nonce( const char *s_bytes, size_t len ) {
if ( len != 8 ) {
throw CryptoException( "Nonce representation must be 8 octets long." );
}
Expand All @@ -179,8 +180,7 @@ Nonce::Nonce( const char *s_bytes, size_t len )
memcpy( bytes + 4, s_bytes, 8 );
}

const string Session::encrypt( const Message &plaintext )
{
const string Session::encrypt( const Message &plaintext ) {
const size_t pt_len = plaintext.text.size();
const int ciphertext_len = pt_len + 16;

Expand Down Expand Up @@ -230,8 +230,7 @@ const string Session::encrypt( const Message &plaintext )
return plaintext.nonce.cc_str() + text;
}

const Message Session::decrypt( const char *str, size_t len )
{
const Message Session::decrypt( const char *str, size_t len ) {
if ( len < 24 ) {
throw CryptoException( "Ciphertext must contain nonce and tag." );
}
Expand Down Expand Up @@ -274,8 +273,7 @@ static rlim_t saved_core_rlimit;

/* Disable dumping core, as a precaution to avoid saving sensitive data
to disk. */
void Crypto::disable_dumping_core( void )
{
void Crypto::disable_dumping_core( void ) {
struct rlimit limit;
if ( 0 != getrlimit( RLIMIT_CORE, &limit ) ) {
/* We don't throw CryptoException because this is called very early
Expand All @@ -292,8 +290,7 @@ void Crypto::disable_dumping_core( void )
}
}

void Crypto::reenable_dumping_core( void )
{
void Crypto::reenable_dumping_core( void ) {
/* Silent failure is safe. */
struct rlimit limit;
if ( 0 == getrlimit( RLIMIT_CORE, &limit ) ) {
Expand Down
11 changes: 3 additions & 8 deletions src/crypto/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,10 @@ namespace Crypto {

Message( const char *nonce_bytes, size_t nonce_len, const char *text_bytes,
size_t text_len )
: nonce( nonce_bytes, nonce_len ), text( text_bytes, text_len )
{
}
: nonce( nonce_bytes, nonce_len ), text( text_bytes, text_len ) {}

Message( const Nonce &s_nonce, const string &s_text )
: nonce( s_nonce ), text( s_text )
{
}
: nonce( s_nonce ), text( s_text ) {}
};

class Session {
Expand All @@ -152,8 +148,7 @@ namespace Crypto {

const string encrypt( const Message &plaintext );
const Message decrypt( const char *str, size_t len );
const Message decrypt( const string &ciphertext )
{
const Message decrypt( const string &ciphertext ) {
return decrypt( ciphertext.data(), ciphertext.size() );
}

Expand Down
Loading

0 comments on commit 00a2eb2

Please sign in to comment.