Skip to content

Commit

Permalink
add new format style
Browse files Browse the repository at this point in the history
  • Loading branch information
postables committed Jul 13, 2020
1 parent 46947dd commit 6f7e833
Show file tree
Hide file tree
Showing 59 changed files with 886 additions and 444 deletions.
9 changes: 5 additions & 4 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
BasedOnStyle: LLVM
# BasedOnStyle: LLVM
Language: Cpp
AlignTrailingComments: true
BinPackParameters: true
AlignEscapedNewlinesLeft: true
BreakBeforeBraces: Attach
ContinuationIndentWidth: 4
IndentCaseLabels: true
IndentWidth: 4
ColumnLimit: 80
ColumnLimit: 85
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
SortIncludes: true
SpaceBeforeParens: ControlStatements
SpaceBeforeParens: ControlStatements
BinPackArguments: true
BinPackParameters: true
6 changes: 4 additions & 2 deletions include/crypto/aes.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ int libp2p_crypto_aes_key_generate(char *key);
* @param output_size the length of the memory allocated for output
* @returns true(1) on success, otherwise false(0)
*/
int libp2p_crypto_aes_encrypt(char *key, char *iv, char *input, size_t input_size, unsigned char **output, size_t *output_size);
int libp2p_crypto_aes_encrypt(char *key, char *iv, char *input, size_t input_size,
unsigned char **output, size_t *output_size);

/**
* Decrypt a block of text
Expand All @@ -29,4 +30,5 @@ int libp2p_crypto_aes_encrypt(char *key, char *iv, char *input, size_t input_siz
* @param output_size the length of the memory allocated for output
* @returns true(1) on success, otherwise false(0)
*/
int libp2p_crypto_aes_decrypt(char *key, char *iv, char *input, size_t input_size, unsigned char **output, size_t *output_size);
int libp2p_crypto_aes_decrypt(char *key, char *iv, char *input, size_t input_size,
unsigned char **output, size_t *output_size);
19 changes: 14 additions & 5 deletions include/crypto/encoding/base16.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
* @param incoming the incoming bytes
* @param incoming_length the length of the incoming bytes
* @param results where to put the results
* @param results_length the size of the buffer, and returns the actual length used
* @param results_length the size of the buffer, and returns the actual length
* used
* @returns true(1) on success
*/
int libp2p_crypto_encoding_base16_encode(const unsigned char *incoming, size_t incoming_length, unsigned char *results, size_t *results_length);
int libp2p_crypto_encoding_base16_encode(const unsigned char *incoming,
size_t incoming_length,
unsigned char *results,
size_t *results_length);

/**
* Calculate the size of the buffer necessary to encode
Expand All @@ -20,12 +24,17 @@ int libp2p_crypto_encoding_base16_encode_size(size_t incoming_length);
/**
* Decode from Base16 format
* @param incoming the incoming base16 encoded string
* @param incoming_length the length of the incoming string (no need to include null)
* @param incoming_length the length of the incoming string (no need to include
* null)
* @param results where to put the results
* @param results_length the size of the buffer, and returns the actual length used
* @param results_length the size of the buffer, and returns the actual length
* used
* @returns true(1) on success
*/
int libp2p_crypto_encoding_base16_decode(const unsigned char *incoming, size_t incoming_length, unsigned char *results, size_t *results_length);
int libp2p_crypto_encoding_base16_decode(const unsigned char *incoming,
size_t incoming_length,
unsigned char *results,
size_t *results_length);

/**
* Calculate the size of the buffer necessary to decode
Expand Down
19 changes: 14 additions & 5 deletions include/crypto/encoding/base32.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
* @param incoming the incoming bytes
* @param incoming_length the length of the incoming bytes
* @param results where to put the results
* @param results_length the size of the buffer, and returns the actual length used
* @param results_length the size of the buffer, and returns the actual length
* used
* @returns true(1) on success
*/
int libp2p_crypto_encoding_base32_encode(const unsigned char *incoming, size_t incoming_length, unsigned char *results, size_t *results_length);
int libp2p_crypto_encoding_base32_encode(const unsigned char *incoming,
size_t incoming_length,
unsigned char *results,
size_t *results_length);

/**
* Calculate the size of the buffer necessary to encode
Expand All @@ -23,12 +27,17 @@ size_t libp2p_crypto_encoding_base32_encode_size(size_t incoming_length);
/**
* Decode from Base16 format
* @param incoming the incoming base16 encoded string
* @param incoming_length the length of the incoming string (no need to include null)
* @param incoming_length the length of the incoming string (no need to include
* null)
* @param results where to put the results
* @param results_length the size of the buffer, and returns the actual length used
* @param results_length the size of the buffer, and returns the actual length
* used
* @returns true(1) on success
*/
int libp2p_crypto_encoding_base32_decode(const unsigned char *incoming, size_t incoming_length, unsigned char *results, size_t *results_length);
int libp2p_crypto_encoding_base32_decode(const unsigned char *incoming,
size_t incoming_length,
unsigned char *results,
size_t *results_length);

/**
* Calculate the size of the buffer necessary to decode
Expand Down
8 changes: 6 additions & 2 deletions include/crypto/encoding/base58.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
* @param binary_data_size the size of the results buffer
* @returns true(1) on success
*/
int libp2p_crypto_encoding_base58_decode(const char *b58, size_t base58_size, unsigned char **bin, size_t *binszp);
int libp2p_crypto_encoding_base58_decode(const char *b58, size_t base58_size,
unsigned char **bin, size_t *binszp);

/**
* encode an array of bytes into a base58 string
Expand All @@ -20,7 +21,10 @@ int libp2p_crypto_encoding_base58_decode(const char *b58, size_t base58_size, un
* @param base58_size the size of the results buffer
* @returns true(1) on success
*/
int libp2p_crypto_encoding_base58_encode(const unsigned char *binary_data, size_t binary_data_size, unsigned char **base58, size_t *base58_size);
int libp2p_crypto_encoding_base58_encode(const unsigned char *binary_data,
size_t binary_data_size,
unsigned char **base58,
size_t *base58_size);

/***
* calculate the size of the results based on an incoming string
Expand Down
10 changes: 8 additions & 2 deletions include/crypto/encoding/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
* @param bytes_written the number of bytes written to output_data
* @returns true(1) on success, otherwise false
*/
int libp2p_crypto_encoding_base64_encode(const unsigned char *input_data, size_t input_length, unsigned char *output_data, size_t max_output_length,
int libp2p_crypto_encoding_base64_encode(const unsigned char *input_data,
size_t input_length,
unsigned char *output_data,
size_t max_output_length,
size_t *bytes_written);

/**
Expand All @@ -31,7 +34,10 @@ size_t libp2p_crypto_encoding_base64_encode_size(size_t input_length);
* @param bytes_written the number of bytes written to output_data
* @returns a pointer to the decoded data
*/
int libp2p_crypto_encoding_base64_decode(const unsigned char *input_data, size_t input_length, unsigned char *output_data, size_t max_output_length,
int libp2p_crypto_encoding_base64_decode(const unsigned char *input_data,
size_t input_length,
unsigned char *output_data,
size_t max_output_length,
size_t *bytes_written);

/**
Expand Down
9 changes: 6 additions & 3 deletions include/crypto/encoding/x509.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@
* @param bytes an array of 1600 bytes to hold the DER formatted data
* @returns true(1) on success
*/
int libp2p_crypto_encoding_x509_private_key_to_der(struct RsaPrivateKey *private_key, unsigned char *bytes[1600]);
int libp2p_crypto_encoding_x509_private_key_to_der(struct RsaPrivateKey *private_key,
unsigned char *bytes[1600]);

/**
* Take a DER formatted char array and turn it into an RsaPrivateKey
* @param der the DER formatted char array
* @param der_length the number of bytes in the char array
* @param private_key the struct to put the data in. Memory should have already been allocated for the struct.
* @param private_key the struct to put the data in. Memory should have already
* been allocated for the struct.
* @returns true(1) on success
*/
int libp2p_crypto_encoding_x509_der_to_private_key(unsigned char *der, size_t der_length, struct RsaPrivateKey *private_key);
int libp2p_crypto_encoding_x509_der_to_private_key(
unsigned char *der, size_t der_length, struct RsaPrivateKey *private_key);

#endif /* x509_h */
8 changes: 5 additions & 3 deletions include/crypto/ephemeral.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ struct EphemeralPrivateKey {
* @param private_key where to store the private key
* @reutrns true(1) on success, otherwise false(0)
*/
int libp2p_crypto_ephemeral_keypair_generate(char *curve, struct EphemeralPrivateKey **private_key);
int libp2p_crypto_ephemeral_keypair_generate(
char *curve, struct EphemeralPrivateKey **private_key);

/**
* Generate a shared secret
Expand All @@ -48,8 +49,9 @@ int libp2p_crypto_ephemeral_keypair_generate(char *curve, struct EphemeralPrivat
* @param remote_public_key_size the size of the remote public key
* @reutrns true(1) on success, otherwise false(0)
*/
int libp2p_crypto_ephemeral_generate_shared_secret(struct EphemeralPrivateKey *private_key, const unsigned char *remote_public_key,
size_t remote_public_key_size);
int libp2p_crypto_ephemeral_generate_shared_secret(
struct EphemeralPrivateKey *private_key, const unsigned char *remote_public_key,
size_t remote_public_key_size);

/***
* Remove resources used by generation of ephemeral private key
Expand Down
24 changes: 18 additions & 6 deletions include/crypto/key.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,35 @@ void libp2p_crypto_public_key_free(struct PublicKey *in);

struct PrivateKey *libp2p_crypto_private_key_new(void);
void libp2p_crypto_private_key_free(struct PrivateKey *in);
int libp2p_crypto_private_key_copy(const struct PrivateKey *source, struct PrivateKey *destination);
int libp2p_crypto_private_key_copy(const struct PrivateKey *source,
struct PrivateKey *destination);

/**
* Unmarshal a public key from a protobuf
*/
int libp2p_crypto_public_key_protobuf_decode(unsigned char *buffer, size_t buffer_length, struct PublicKey **out);
int libp2p_crypto_public_key_protobuf_decode(unsigned char *buffer,
size_t buffer_length,
struct PublicKey **out);
size_t libp2p_crypto_public_key_protobuf_encode_size(const struct PublicKey *in);
int libp2p_crypto_public_key_protobuf_encode(const struct PublicKey *in, unsigned char *buffer, size_t max_buffer_length, size_t *bytes_written);
int libp2p_crypto_public_key_protobuf_encode(const struct PublicKey *in,
unsigned char *buffer,
size_t max_buffer_length,
size_t *bytes_written);
// private key
int libp2p_crypto_private_key_protobuf_decode(unsigned char *buffer, size_t buffer_length, struct PrivateKey **out);
int libp2p_crypto_private_key_protobuf_decode(unsigned char *buffer,
size_t buffer_length,
struct PrivateKey **out);
size_t libp2p_crypto_private_key_protobuf_encode_size(const struct PrivateKey *in);
int libp2p_crypto_private_key_protobuf_encode(const struct PrivateKey *in, unsigned char *buffer, size_t max_buffer_length, size_t *bytes_written);
int libp2p_crypto_private_key_protobuf_encode(const struct PrivateKey *in,
unsigned char *buffer,
size_t max_buffer_length,
size_t *bytes_written);

/**
* convert a public key into a peer id
* @param public_key the public key struct
* @param peer_id the results, in a null-terminated string
* @returns true(1) on success, otherwise false(0)
*/
int libp2p_crypto_public_key_to_peer_id(struct PublicKey *public_key, char **peer_id);
int libp2p_crypto_public_key_to_peer_id(struct PublicKey *public_key,
char **peer_id);
29 changes: 17 additions & 12 deletions include/crypto/peerutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
#define uchar unsigned char // 8-bit byte
#define juint unsigned int // 32-bit word
// DBL_INT_ADD treats two unsigned ints a and b as one 64-bit integer and adds c to it
#define DBL_INT_ADD(a,b,c) if (a > 0xffffffff - (c)) ++b; a += c;
#define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b))))
#define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b))))
// DBL_INT_ADD treats two unsigned ints a and b as one 64-bit integer and adds c
to it #define DBL_INT_ADD(a,b,c) if (a > 0xffffffff - (c)) ++b; a += c; #define
ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b)))) #define ROTRIGHT(a,b) (((a) >>
(b)) | ((a) << (32-(b))))
#define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
#define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
Expand Down Expand Up @@ -49,9 +49,9 @@ void sha256_transform(SHA256_CTX *ctx, uchar data[])
juint a,b,c,d,e,f,g,h,i,j,t1,t2,m[64];
for (i=0,j=0; i < 16; ++i, j += 4)
m[i] = (data[j] << 24) | (data[j+1] << 16) | (data[j+2] << 8) | (data[j+3]);
for ( ; i < 64; ++i)
m[i] = SIG1(m[i-2]) + m[i-7] + SIG0(m[i-15]) + m[i-16];
m[i] = (data[j] << 24) | (data[j+1] << 16) | (data[j+2] << 8) |
(data[j+3]); for ( ; i < 64; ++i) m[i] = SIG1(m[i-2]) + m[i-7] + SIG0(m[i-15]) +
m[i-16];
a = ctx->state[0];
b = ctx->state[1];
Expand Down Expand Up @@ -147,7 +147,8 @@ void sha256_final(SHA256_CTX *ctx, uchar hash[])
ctx->data[56] = ctx->bitlen[1] >> 24;
sha256_transform(ctx,ctx->data);
// Since this implementation uses little endian byte ordering and SHA uses big endian,
// Since this implementation uses little endian byte ordering and SHA uses
big endian,
// reverse all the bytes when copying the final state to the output hash.
for (i=0; i < 4; ++i) {
hash[i] = (ctx->state[0] >> (24-i*8)) & 0x000000ff;
Expand Down Expand Up @@ -175,22 +176,26 @@ void a_store_hash(unsigned char * result,unsigned char hash[])
*/

/**
* base58 encode a string NOTE: this also puts the prefix 'Qm' in front as the ID is a multihash
* base58 encode a string NOTE: this also puts the prefix 'Qm' in front as the
* ID is a multihash
* @param pointyaddr where the results will go
* @param rezbuflen the length of the results buffer. It will also put how much was used here
* @param rezbuflen the length of the results buffer. It will also put how much
* was used here
* @param ID_BUF the input text (usually a SHA256 hash)
* @param ID_BUF_SIZE the input size (normally a SHA256, therefore 32 bytes)
* @returns true(1) on success
*/
int PrettyID(unsigned char *pointyaddr, size_t *rezbuflen, unsigned char *ID_BUF, size_t ID_BUF_SIZE); // b58 encoded ID buf
int PrettyID(unsigned char *pointyaddr, size_t *rezbuflen, unsigned char *ID_BUF,
size_t ID_BUF_SIZE); // b58 encoded ID buf

/****
* Make a SHA256 hash of what is usually the DER formatted private key.
* @param result where to store the result. Should be 32 chars long
* @param texttohash the text to hash
* @param text_size the size of the text
*/
void ID_FromPK_non_null_terminated(char *result, unsigned char *texttohash, size_t text_size);
void ID_FromPK_non_null_terminated(char *result, unsigned char *texttohash,
size_t text_size);

/****
* Make a SHA256 hash of what is usually the DER formatted private key.
Expand Down
14 changes: 10 additions & 4 deletions include/crypto/rsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ struct RsaPrivateKey *libp2p_crypto_private_key_to_rsa(struct PrivateKey *in);
* @param num_bits_for_keypair the size of the key (1024 minimum)
* @returns true(1) on success
*/
int libp2p_crypto_rsa_generate_keypair(struct RsaPrivateKey *private_key, unsigned long num_bits_for_keypair);
int libp2p_crypto_rsa_generate_keypair(struct RsaPrivateKey *private_key,
unsigned long num_bits_for_keypair);

/**
* Use the private key DER to fill in the public key DER
Expand All @@ -68,11 +69,16 @@ struct RsaPrivateKey *libp2p_crypto_rsa_rsa_private_key_new(void);
* @param private_key the private key
* @param message the message to be signed
* @param message_length the length of message
* @param result the resultant signature. Note: should be pre-allocated and be the size of the private key (i.e. 2048)
* @param result the resultant signature. Note: should be pre-allocated and be
* the size of the private key (i.e. 2048)
* @returns true(1) on successs, otherwise false(0)
*/
int libp2p_crypto_rsa_sign(struct RsaPrivateKey *private_key, const char *message, size_t message_length, unsigned char **result, size_t *result_size);
int libp2p_crypto_rsa_sign(struct RsaPrivateKey *private_key, const char *message,
size_t message_length, unsigned char **result,
size_t *result_size);

int libp2p_crypto_rsa_verify(struct RsaPublicKey *public_key, const unsigned char *message, size_t message_length, const unsigned char *signature);
int libp2p_crypto_rsa_verify(struct RsaPublicKey *public_key,
const unsigned char *message, size_t message_length,
const unsigned char *signature);

#endif /* rsa_h */
3 changes: 2 additions & 1 deletion include/crypto/sha1.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void SHA1_Final(SHA1_CTX *context, uint8_t digest[SHA1_DIGEST_SIZE]);
* @param output where the output is placed, should be 40 bytes in width
* @returns the number of bytes written, or 0 on error
*/
int libp2p_crypto_hashing_sha1(const unsigned char *input, size_t input_length, unsigned char *output);
int libp2p_crypto_hashing_sha1(const unsigned char *input, size_t input_length,
unsigned char *output);

#endif /* _SHA1_H */
10 changes: 7 additions & 3 deletions include/crypto/sha256.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
* @param output where to place the results
* @returns 1
*/
int libp2p_crypto_hashing_sha256(const unsigned char *input, size_t input_length, unsigned char *output);
int libp2p_crypto_hashing_sha256(const unsigned char *input, size_t input_length,
unsigned char *output);

/**
* Initialize a sha256 hmac process
Expand All @@ -25,15 +26,18 @@ int libp2p_crypto_hashing_sha256_init(mbedtls_sha256_context *ctx);
* @param input_size the size of input
* @returns true(1) on success, otherwise false(0)
*/
int libp2p_crypto_hashing_sha256_update(mbedtls_sha256_context *ctx, const unsigned char *input, size_t input_size);
int libp2p_crypto_hashing_sha256_update(mbedtls_sha256_context *ctx,
const unsigned char *input,
size_t input_size);

/**
* finalize a sha256 hmac process
* @param ctx the context
* @param hash where to put the results (for SHA256, should be 32 bytes long)
* @returns true(1) on success, otherwise false(0)
*/
int libp2p_crypto_hashing_sha256_finish(mbedtls_sha256_context *ctx, unsigned char *hash);
int libp2p_crypto_hashing_sha256_finish(mbedtls_sha256_context *ctx,
unsigned char *hash);

/**
* Clean up allocated memory
Expand Down
3 changes: 2 additions & 1 deletion include/crypto/sha512.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
* @param output where to place the results, should be 64 bytes
* @returns number of bytes written, or 0
*/
int libp2p_crypto_hashing_sha512(const unsigned char *input, size_t input_length, unsigned char *output);
int libp2p_crypto_hashing_sha512(const unsigned char *input, size_t input_length,
unsigned char *output);
Loading

0 comments on commit 6f7e833

Please sign in to comment.