Commit 9e7a6f90 authored by Davide Galassi's avatar Davide Galassi

Merge branch 'feature/include_style_review' into 'master'

Code style and organization review

Closes #2

See merge request !5
parents e32dfe8a 84702780
Pipeline #41384725 passed with stages
in 1 minute and 15 seconds
......@@ -26,7 +26,8 @@ Given a version number MAJOR.MINOR.PATCH
- Project released under the MIT license
- MPI Toom-Cook-3 multiplier
- Hill classical cipher
- Trivium stream cipher
- Hill cipher
- Polyalphabetic affine cipher
[0.0.6] - 2017-07-09
......
......@@ -51,7 +51,7 @@ endif
endif
objs-y :=
objects-y :=
paths-y :=
objects_list :=
......@@ -59,9 +59,9 @@ define include_subdir
$(shell mkdir -p $(call src_to_bin_dir,$1))
subdirs-y :=
current := $1
objs-y :=
objects-y :=
include $1/subdir.mk
objects_list += $$(if $$(objs-y),$$(addprefix $1/,$$(objs-y)))
objects_list += $$(if $$(objects-y),$$(addprefix $1/,$$(objects-y)))
paths-y += $$(current)
subdirs-y := $$(addprefix $$(current)/, $$(subdirs-y))
$$(foreach subdir, $$(subdirs-y),$$(eval $$(call include_subdir,$$(subdir))))
......@@ -72,12 +72,13 @@ $(eval $(call include_subdir,src))
objects = $(call src_to_bin_dir,$(objects_list))
depends = $(patsubst %.o,%.d,$(objects))
CPPFLAGS = $(includes-y)
CFLAGS = $(cflags-y)
AFLAGS = $(aflags-y)
LDFLAGS = $(lflags-y)
CPPFLAGS = $(includes-y)
CFLAGS = $(cflags-y)
AFLAGS = $(aflags-y)
LDFLAGS = $(lflags-y)
DATE := $(shell date +'%y%m%d')
DATE := $(shell date +'%y%m%d')
.PHONY: all cry clean test testclean
......@@ -86,11 +87,10 @@ all: cry
cry: $(target)
clean:
$(RM) $(binary_dir) $(config) *.a
$(RM) `find . -type f \( -name \*.gcda -o -name \*.gcno \)`
@$(RM) $(binary_dir) $(config) *.a
@$(RM) `find . -type f \( -name \*.gcda -o -name \*.gcno \)`
$(objects): $(config)
$(objects): Makefile config.mk $(config)
$(target): $(objects)
$(AR) rcs $@ $^
......
......@@ -3,8 +3,8 @@
* @brief Base-64 encoder/decoder.
*/
#ifndef _CRY_BASE64_H_
#define _CRY_BASE64_H_
#ifndef CRY_BASE64_H_
#define CRY_BASE64_H_
#ifdef __cplusplus
extern "C"{
......@@ -20,4 +20,4 @@ size_t cry_base64_decode(char *out, const char *in, size_t len);
}
#endif
#endif /* _CRY_BASE64_H_ */
#endif /* CRY_BASE64_H_ */
......@@ -6,7 +6,7 @@
#ifndef CRY_CBC_H_
#define CRY_CBC_H_
#include "cry/ciph.h"
#include <cry/ciph.h>
/** CBC block size. */
#define CRY_CBC_BLOCK_SIZE 16
......
......@@ -3,8 +3,8 @@
* @brief Generic cipher interface
*/
#ifndef _CRY_CIPH_H_
#define _CRY_CIPH_H_
#ifndef CRY_CIPH_H_
#define CRY_CIPH_H_
#ifdef __cplusplus
extern "C"{
......@@ -80,4 +80,4 @@ struct cry_ciph_itf {
typedef struct cry_ciph_itf cry_ciph_itf;
#endif /* _CRY_CIPH_H_ */
#endif /* CRY_CIPH_H_ */
......@@ -5,8 +5,8 @@
* Also known as OMAC1 (One-key mac 1).
*/
#ifndef _CRY_CMAC_H_
#define _CRY_CMAC_H_
#ifndef CRY_CMAC_H_
#define CRY_CMAC_H_
#include <stddef.h>
......@@ -30,4 +30,4 @@ void cry_cmac_digest(unsigned char *mac, const unsigned char *input,
}
#endif
#endif /* _CRY_CMAC_H_ */
#endif /* CRY_CMAC_H_ */
......@@ -6,8 +6,8 @@
* Polynomial_representations_of_cyclic_redundancy_checks
*/
#ifndef _CRY_CRC_H_
#define _CRY_CRC_H_
#ifndef CRY_CRC_H_
#define CRY_CRC_H_
#include <stddef.h>
#include <stdint.h>
......@@ -114,9 +114,6 @@ void cry_crc16_ibm_init(struct cry_crc16_ctx *ctx);
*/
uint16_t cry_crc16_ibm(const unsigned char *in, size_t n);
/**
* Initialize a generic CRC-32 algoritm context.
*
......@@ -171,4 +168,4 @@ uint32_t cry_crc32_eth(const unsigned char *in, size_t n);
}
#endif
#endif /* _CRY_CRC_H_ */
#endif /* CRY_CRC_H_ */
......@@ -13,7 +13,7 @@
#ifndef CRY_CTR_H_
#define CRY_CTR_H_
#include "cry/ciph.h"
#include <cry/ciph.h>
/** Block size. */
#define CRY_CTR_BLOCK_SIZE 16
......
......@@ -3,8 +3,8 @@
* @brief DES block cipher.
*/
#ifndef _CRY_DES_H_
#define _CRY_DES_H_
#ifndef CRY_DES_H_
#define CRY_DES_H_
#include <string.h>
......@@ -32,4 +32,4 @@ void cry_des_encrypt(cry_des_ctx *ctx, unsigned char *dst,
void cry_des_decrypt(cry_des_ctx *ctx, unsigned char *dst,
const unsigned char *src, unsigned int size);
#endif /* _CRY_DES_H_ */
#endif /* CRY_DES_H_ */
......@@ -3,10 +3,10 @@
* @brief Diffie-Hellman key exchange algorithm.
*/
#ifndef _CRY_DH_
#define _CRY_DH_
#ifndef CRY_DH_
#define CRY_DH_
#include "cry/mpi.h"
#include <cry/mpi.h>
/** Diffie-Hellman algorithm context */
struct cry_dh_ctx {
......@@ -41,4 +41,4 @@ int cry_dh_get_key(cry_dh_ctx *ctx, unsigned char *out, size_t out_len);
}
#endif
#endif /* _CRY_DH_ */
#endif /* CRY_DH_ */
......@@ -3,10 +3,10 @@
* @brief Digital Signature Algorithm
*/
#ifndef _CRY_DSA_H_
#define _CRY_DSA_H_
#ifndef CRY_DSA_H_
#define CRY_DSA_H_
#include "cry/mpi.h"
#include <cry/mpi.h>
struct cry_dsa_ctx {
cry_mpi g;
......@@ -39,4 +39,4 @@ int cry_dsa_verify(cry_dsa_ctx *ctx, const cry_dsa_signature *sign,
}
#endif
#endif /* _CRY_DSA_H_ */
#endif /* CRY_DSA_H_ */
......@@ -3,10 +3,10 @@
* @brief Elliptic curve equation
*/
#ifndef _CRY_EC_H_
#define _CRY_EC_H_
#ifndef CRY_EC_H_
#define CRY_EC_H_
#include "cry/ecp.h"
#include <cry/ecp.h>
/*
* Describes an elliptic curve equation:
......@@ -42,4 +42,4 @@ int cry_ec_set_nist_p256(cry_ec *ec);
}
#endif
#endif /* _CRY_EC_H_ */
#endif /* CRY_EC_H_ */
......@@ -3,10 +3,10 @@
* @brief Elliptic Curve Diffie Hellman
*/
#ifndef _CRY_ECDH_
#define _CRY_ECDH_
#ifndef CRY_ECDH_
#define CRY_ECDH_
#include "cry/ec.h"
#include <cry/ec.h>
struct cry_ecdh_ctx {
cry_ec ec; /**> Elliptic curve parameters */
......@@ -33,4 +33,4 @@ int cry_ecdh_final(cry_ecdh_ctx *ctx);
}
#endif
#endif /* _CRY_ECDH_ */
#endif /* CRY_ECDH_ */
......@@ -3,10 +3,10 @@
* @brief Elliptic Curve Digital Signature Algorithm
*/
#ifndef _CRY_ECDSA_H_
#define _CRY_ECDSA_H_
#ifndef CRY_ECDSA_H_
#define CRY_ECDSA_H_
#include "cry/ec.h"
#include <cry/ec.h>
struct cry_ecdsa_ctx {
cry_ec ec; /**< Elliptic curve parameters */
......@@ -39,4 +39,4 @@ int cry_ecdsa_keygen(const cry_ec *ec, cry_mpi *d, cry_ecp *q);
}
#endif
#endif /* _CRY_ECDSA_H_ */
#endif /* CRY_ECDSA_H_ */
......@@ -3,10 +3,10 @@
* @brief Elliptic curve point arithmetic
*/
#ifndef _CRY_ECP_H_
#define _CRY_ECP_H_
#ifndef CRY_ECP_H_
#define CRY_ECP_H_
#include "cry/mpi.h"
#include <cry/mpi.h>
/** Elliptic curve point structure */
struct cry_ecp {
......@@ -46,4 +46,4 @@ int cry_ecp_mul(cry_ecp *pr, const cry_ecp *p1, const cry_mpi *k,
}
#endif
#endif /* _CRY_ECP_H_ */
#endif /* CRY_ECP_H_ */
......@@ -6,7 +6,7 @@
#ifndef CRY_GCM_H_
#define CRY_GCM_H_
#include "cry/ciph.h"
#include <cry/ciph.h>
/** Size of a GCM block. */
#define CRY_GCM_BLOCK_SIZE 16
......
......@@ -3,8 +3,8 @@
* @brief Generic hash algorithm interface
*/
#ifndef _CRY_HASH_H_
#define _CRY_HASH_H_
#ifndef CRY_HASH_H_
#define CRY_HASH_H_
#ifdef __cplusplus
extern "C"{
......@@ -62,4 +62,4 @@ typedef struct cry_hash_itf cry_hash_itf;
}
#endif
#endif /* _CRY_HASH_H_ */
#endif /* CRY_HASH_H_ */
......@@ -3,10 +3,10 @@
* @brief Hash MAC
*/
#include "cry/hash.h"
#include <cry/hash.h>
#ifndef _CRY_HMAC_H_
#define _CRY_HMAC_H_
#ifndef CRY_HMAC_H_
#define CRY_HMAC_H_
#define CRY_HMAC_BLOCK_MAX 64
......@@ -28,4 +28,4 @@ void cry_hmac_update(cry_hmac_ctx *ctx, unsigned char *in, size_t in_len);
void cry_hmac_digest(cry_hmac_ctx *ctx, unsigned char *mac);
#endif /* _CRY_HMAC_H_ */
#endif /* CRY_HMAC_H_ */
......@@ -3,8 +3,8 @@
* @brief MD5 message digest algorithm
*/
#ifndef _CRY_MD5_H_
#define _CRY_MD5_H_
#ifndef CRY_MD5_H_
#define CRY_MD5_H_
#include <stddef.h>
#include <stdint.h>
......@@ -56,4 +56,4 @@ void cry_md5_digest(struct cry_md5_ctx *ctx, unsigned char *digest);
}
#endif
#endif /* _CRY_MD5_H_ */
#endif /* CRY_MD5_H_ */
#ifndef _CRY_MEMXOR_H_
#define _CRY_MEMXOR_H_
#ifndef CRY_MEMXOR_H_
#define CRY_MEMXOR_H_
#ifdef __cplusplus
extern "C"{
......@@ -35,4 +35,4 @@ void cry_memxor2(unsigned char *dst, const unsigned char *src1,
}
#endif
#endif /* _CRY_MEMXOR_H_ */
#endif /* CRY_MEMXOR_H_ */
......@@ -3,8 +3,8 @@
* @brief Multiple precision integers.
*/
#ifndef _CRY_MPI_H_
#define _CRY_MPI_H_
#ifndef CRY_MPI_H_
#define CRY_MPI_H_
#include <stddef.h>
......@@ -176,4 +176,4 @@ int cry_mpi_mul_dig(cry_mpi *r, const cry_mpi *a, cry_mpi_digit b);
}
#endif
#endif /* _CRY_MPI_H_ */
#endif /* CRY_MPI_H_ */
......@@ -3,8 +3,8 @@
* @brief Pseudo Random Number Generator
*/
#ifndef _CRY_PRNG_H_
#define _CRY_PRNG_H_
#ifndef CRY_PRNG_H_
#define CRY_PRNG_H_
#ifdef __cplusplus
extern "C"{
......@@ -38,4 +38,4 @@ int cry_prng_rand(unsigned char *buf, size_t siz);
}
#endif
#endif /* _CRY_PRNG_H_ */
#endif /* CRY_PRNG_H_ */
......@@ -3,10 +3,10 @@
* @brief RSA cipher.
*/
#ifndef _CRY_RSA_H_
#define _CRY_RSA_H_
#ifndef CRY_RSA_H_
#define CRY_RSA_H_
#include "cry/mpi.h"
#include <cry/mpi.h>
#define CRY_RSA_FLAG_SIGN (1 << 0)
......@@ -36,4 +36,4 @@ int cry_rsa_decrypt(cry_rsa_ctx *ctx, unsigned char **out, size_t *outlen,
}
#endif
#endif /* _CRY_RSA_H_ */
#endif /* CRY_RSA_H_ */
......@@ -3,8 +3,8 @@
* @brief Secure Hash Algorithm (256)
*/
#ifndef _CRY_SHA256_H_
#define _CRY_SHA256_H_
#ifndef CRY_SHA256_H_
#define CRY_SHA256_H_
#include <stdint.h>
#include <stddef.h>
......@@ -35,4 +35,4 @@ void cry_sha256_digest(struct cry_sha256_ctx *ctx, unsigned char *digest);
}
#endif
#endif /* _CRY_SHA256_H */
#endif /* CRY_SHA256_H */
......@@ -3,8 +3,8 @@
* @brief Common checksum algorithms.
*/
#ifndef _CRY_SUM_H_
#define _CRY_SUM_H_
#ifndef CRY_SUM_H_
#define CRY_SUM_H_
#include <stddef.h>
......@@ -34,4 +34,4 @@ unsigned char cry_lrc(const unsigned char *in, size_t n);
}
#endif
#endif /* _CRY_SUM_H_ */
#endif /* CRY_SUM_H_ */
......@@ -3,8 +3,8 @@
* @brief Library version.
*/
#ifndef _CRY_VERSION_H_
#define _CRY_VERSION_H_
#ifndef CRY_VERSION_H_
#define CRY_VERSION_H_
/** Library version major number */
#define CRY_MAJOR 0
......@@ -24,4 +24,4 @@
*/
long cry_version(void);
#endif /* _CRY_VERSION_H_ */
#endif /* CRY_VERSION_H_ */
# src/classic/subdir.mk
objs-y := hill.o \
affine.o
# src/crc/subdir.mk
objs-y := crc16.o \
crc16_ccitt.o \
crc16_ibm.o \
crc32.o \
crc32_eth.o
#include "cry/aes.h"
#include <cry/aes.h>
#include "misc.h"
#include <stdint.h>
......
#include "cry/affine.h"
#include <cry/affine.h>
#include "misc.h"
#include <string.h>
......
#include "cry/base64.h"
#include <cry/base64.h>
static const char *base64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
......
#include "cry/cbc.h"
#include "cry/ciph.h"
#include <cry/cbc.h>
#include <cry/ciph.h>
#include "memxor.h"
#include "misc.h"
#include <string.h>
......
#include "cry/aes.h"
#include <cry/aes.h>
#include <string.h>
static void xor_128(unsigned char *o, const unsigned char *a,
......
#include "cry/ctr.h"
#include <cry/ctr.h>
#include "memxor.h"
#include "misc.h"
#include <string.h>
......
#include "cry/des.h"
#include <cry/des.h>
#define GET_BIT(buf, bit) \
(buf[(bit) / 8] & (0x80 >> ((bit) % 8)))
......
#include "cry/dh.h"
#include <cry/dh.h>
int cry_dh_init(cry_dh_ctx *dh)
{
......
#include "cry/dsa.h"
#include <cry/dsa.h>
#include "mpi/mpi_pvt.h"
#define CHK(exp) do { if ((res = (exp)) != 0) goto e; } while (0)
......
#include "cry/ecdh.h"
#include <cry/ecdh.h>
int cry_ecdh_init(cry_ecdh_ctx *ctx)
{
......
#include "cry/ecdsa.h"
#include <cry/ecdsa.h>
#include "mpi/mpi_pvt.h"
#define CHK(exp) do { if ((res = (exp)) != 0) goto e; } while (0)
......
#include "cry/gcm.h"
#include <cry/gcm.h>
#include "memxor.h"
#include "misc.h"
#include <string.h>
......
#include "cry/hill.h"
#include "cry/prng.h"
#include <cry/hill.h>
#include <cry/prng.h>
#include "misc.h"
#include <string.h>
......
#include "cry/hmac.h"
#include <cry/hmac.h>
#include <string.h>
#define BLOCK_SIZE CRY_HMAC_BLOCK_MAX
......
#ifndef CRY_MEMXOR_H_
#define CRY_MEMXOR_H_
#ifdef __cplusplus
extern "C"{
#endif
/**
* XOR two memory regions.
*
* The result is stored in the first operand.
* The memory segments may overlap and can work inplace.
*
* @param dst First source and destination.
* @param src Second source.
* @param size Number of bytes to XOR.
*/
void cry_memxor(unsigned char *dst, const unsigned char *src,
unsigned int size);
/**
* XOR two memory regions.
*
* The memory segments may overlap and can work inplace.
*
* @param dst Destination.
* @param src1 First source.
* @param src2 Second source.
* @param size Number of bytes to XOR.
*/
void cry_memxor2(unsigned char *dst, const unsigned char *src1,
const unsigned char *src2, unsigned int size);
#ifdef __cplusplus
}
#endif
#endif /* CRY_MEMXOR_H_ */
#include "cry/rsa.h"
#include "cry/prng.h"
#include <cry/rsa.h>
#include <cry/prng.h>
#include <string.h>
#include <stdlib.h>
......
# src/bcmo/subdir.mk
objects-y := \
memxor.o \
aes.o \
des.o \
cbc.o \
ctr.o \
gcm.o \
rsa.o \
trivium.o \
dh.o \
ecdh.o \
cmac.o \
hmac.o \
dsa.o \
ecdsa.o \
base64.o \
affine.o \
hill.o
#include "cry/ec.h"
#include <cry/ec.h>
/* NIST Curve P-256 parameters */
......
#include "cry/ecp.h"
#include <cry/ecp.h>
#define CHK(exp) do { if ((res = (exp)) != 0) goto e; } while (0)
......
#include "cry/ecp.h"
#include <cry/ecp.h>
int cry_ecp_copy(cry_ecp *d, const cry_ecp *s)
{
......
#include "cry/ecp.h"
#include <cry/ecp.h>
#define CHK(exp) do { if ((res = (exp)) != 0) goto e; } while (0)
......
#include "cry/ecp.h"
#include <cry/ecp.h>
#include "mpi/mpi_pvt.h" /* CRY_MPI_DIGIT_BITS */
#include <stdlib.h> /* malloc() */
......
# src/ecp/subdir.mk
objs-y := ecp_copy.o \
ecp_add.o \
ecp_dbl.o \
ecp_mul.o \
ec_nist_p256.o
objects-y := \
ecp_copy.o \
ecp_add.o \
ecp_dbl.o \
ecp_mul.o \
ec_nist_p256.o
\ No newline at end of file
#include "cry/md5.h"
#include <cry/md5.h>
/* F, G and H are basic MD5 functions: selection, majority, parity */
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
......
#include "cry/sha256.h"
#include <cry/sha256.h>
#include <string.h>
#define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b))))
......
# src/crc/subdir.mk
objects-y := \
md5.o \
sha256.o
\ No newline at end of file
#include "cry/mpi.h"
#include <cry/mpi.h>
#include <stdio.h>
#include <stdlib.h>
......
#ifndef _CRY_MPI_PVT_H_
#define _CRY_MPI_PVT_H_
#ifndef CRY_MPI_PVT_H_
#define CRY_MPI_PVT_H_
#include "cry/mpi.h"
#include <cry/mpi.h>
#include <string.h> /* memset */
#include <limits.h>
#include <stdint.h>
......@@ -58,4 +58,4 @@ int cry_mpi_shld(cry_mpi *a, int n);
((a)->data[(bit) / CRY_MPI_DIGIT_BITS] |= \
1U << ((bit) % CRY_MPI_DIGIT_BITS))
#endif /* _CRY_MPI_PVT_H_ */