diff options
77 files changed, 4900 insertions, 4910 deletions
diff --git a/src/crypto/ae.h b/src/crypto/ae.h index fb5c511..a0d8034 100644 --- a/src/crypto/ae.h +++ b/src/crypto/ae.h @@ -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 */ /* -------------------------------------------------------------------------- * @@ -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 @@ -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. @@ -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. @@ -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. diff --git a/src/crypto/base64.cc b/src/crypto/base64.cc index 129aecb..434ff65 100644 --- a/src/crypto/base64.cc +++ b/src/crypto/base64.cc @@ -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+/"; @@ -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; @@ -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] = '='; } diff --git a/src/crypto/base64.h b/src/crypto/base64.h index 80de7c6..780ed97 100644 --- a/src/crypto/base64.h +++ b/src/crypto/base64.h @@ -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 ); diff --git a/src/crypto/byteorder.h b/src/crypto/byteorder.h index fe7e8cd..f14f638 100644 --- a/src/crypto/byteorder.h +++ b/src/crypto/byteorder.h @@ -37,12 +37,12 @@ #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 @@ -50,11 +50,11 @@ #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 @@ -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 diff --git a/src/crypto/crypto.cc b/src/crypto/crypto.cc index 62e04da..7a79d7e 100644 --- a/src/crypto/crypto.cc +++ b/src/crypto/crypto.cc @@ -39,23 +39,22 @@ #include <sys/resource.h> +#include "src/crypto/base64.h" #include "src/crypto/byteorder.h" #include "src/crypto/crypto.h" -#include "src/crypto/base64.h" -#include "src/util/fatal_assert.h" #include "src/crypto/prng.h" +#include "src/util/fatal_assert.h" using namespace Crypto; -long int myatoi( const char *str ) +long int myatoi( const char* str ) { - char *end; + char* end; errno = 0; long int ret = strtol( str, &end, 10 ); - if ( ( errno != 0 ) - || ( end != str + strlen( str ) ) ) { + if ( ( errno != 0 ) || ( end != str + strlen( str ) ) ) { throw CryptoException( "Bad integer." ); } @@ -72,16 +71,14 @@ uint64_t Crypto::unique( void ) return rv; } -AlignedBuffer::AlignedBuffer( size_t len, const char *data ) - : m_len( len ), m_allocated( NULL ), m_data( NULL ) +AlignedBuffer::AlignedBuffer( size_t len, const char* data ) : 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 ) ) - || ( m_allocated == NULL ) ) { +#if defined( HAVE_POSIX_MEMALIGN ) + if ( ( 0 != posix_memalign( &m_allocated, 16, alloc_len ) ) || ( m_allocated == NULL ) ) { throw std::bad_alloc(); } - m_data = (char *) m_allocated; + m_data = (char*)m_allocated; #else /* malloc() a region 15 bytes larger than we need, and find @@ -91,15 +88,15 @@ AlignedBuffer::AlignedBuffer( size_t len, const char *data ) throw std::bad_alloc(); } - uintptr_t iptr = (uintptr_t) m_allocated; + uintptr_t iptr = (uintptr_t)m_allocated; if ( iptr & 0xF ) { iptr += 16 - ( iptr & 0xF ); } assert( !( iptr & 0xF ) ); - assert( iptr >= (uintptr_t) m_allocated ); - assert( iptr <= ( 15 + (uintptr_t) m_allocated ) ); + assert( iptr >= (uintptr_t)m_allocated ); + assert( iptr <= ( 15 + (uintptr_t)m_allocated ) ); - m_data = (char *) iptr; + m_data = (char*)iptr; #endif /* !defined(HAVE_POSIX_MEMALIGN) */ @@ -136,32 +133,28 @@ Base64Key::Base64Key() PRNG().fill( key, sizeof( key ) ); } -Base64Key::Base64Key(PRNG &prng) +Base64Key::Base64Key( PRNG& prng ) { prng.fill( key, sizeof( key ) ); } std::string Base64Key::printable_key( void ) const { - char base64[ 24 ]; |