Skip to content

Commit

Permalink
fixup! Add a clang-format file and prepare for clang-formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
bbarenblat committed Aug 8, 2023
1 parent 234acdc commit 6f92ab3
Show file tree
Hide file tree
Showing 79 changed files with 6,153 additions and 5,740 deletions.
38 changes: 25 additions & 13 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_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_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,8 +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 @@ -92,8 +91,15 @@ int ae_init( ae_ctx *ctx, const void *key, int key_len, int nonce_len,
*
* ----------------------------------------------------------------------- */

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 @@ -122,8 +128,14 @@ int ae_encrypt( ae_ctx *ctx, const void *nonce, const void *pt, int pt_len,
*
* ----------------------------------------------------------------------- */

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 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 );
/* --------------------------------------------------------------------------
*
Expand Down
77 changes: 36 additions & 41 deletions src/crypto/base64.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,33 @@
#include "base64.h"
#include "fatal_assert.h"

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

static const unsigned char reverse[] = {
// clang-format off
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
// clang-format on
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c,
0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a,
0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
};

/* 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 ) {
return reverse[ c ];
/* 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 )
{
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 );

Expand All @@ -81,41 +76,41 @@ bool base64_decode( const char *b64, const size_t b64_len, uint8_t *raw,
bytes |= sixbit;
/* write groups of 3 */
if ( i % 4 == 3 ) {
raw[ 0 ] = bytes >> 16;
raw[ 1 ] = bytes >> 8;
raw[ 2 ] = bytes;
raw[0] = bytes >> 16;
raw[1] = bytes >> 8;
raw[2] = bytes;
raw += 3;
bytes = 0;
}
}
/* 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 ];
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[ 2 ] = '=';
b64[ 3 ] = '=';
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 <stdint.h>

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 );
56 changes: 29 additions & 27 deletions src/crypto/byteorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,53 +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 6f92ab3

Please sign in to comment.