From 06a757e87887e85d285133ff8b004b5ae7637424 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Sun, 4 Sep 2016 14:56:39 +0200 Subject: [PATCH] Big reorganization and cleanup --- configure.ac | 7 +- include/cryb/Makefile.am | 6 +- include/cryb/aes.h | 4 + include/cryb/algorithm.h | 4 + include/cryb/attributes.h | 4 + include/cryb/bitwise.h | 4 + include/cryb/cipher.h | 6 + include/cryb/{version.h => core.h} | 12 +- include/cryb/coverage.h | 4 + include/cryb/cpe.h | 23 ++ include/cryb/ctype.h | 4 + include/cryb/digest.h | 6 + include/cryb/enc.h | 6 + include/cryb/endian.h | 4 + include/cryb/hash.h | 14 +- include/cryb/hmac.h | 4 + include/cryb/hmac_sha1.h | 4 + include/cryb/hmac_sha224.h | 4 + include/cryb/hmac_sha256.h | 4 + include/cryb/hmac_sha384.h | 4 + include/cryb/hmac_sha512.h | 4 + include/cryb/impl.h | 4 + include/cryb/mac.h | 6 + include/cryb/md2.h | 4 + include/cryb/md4.h | 4 + include/cryb/md5.h | 4 + include/cryb/memset_s.h | 4 + include/cryb/mpi.h | 26 ++ include/cryb/oath.h | 23 +- include/cryb/oath_constants.h | 4 + include/cryb/oath_hotp.h | 49 ++++ include/cryb/oath_totp.h | 49 ++++ include/cryb/oath_types.h | 4 + include/cryb/rand.h | 6 + include/cryb/rc4.h | 4 + include/cryb/rfc3986.h | 4 + include/cryb/rfc4648.h | 4 + include/cryb/sha1.h | 4 + include/cryb/sha224.h | 4 + include/cryb/sha256.h | 4 + include/cryb/sha384.h | 4 + include/cryb/sha512.h | 4 + include/cryb/string.h | 4 + include/cryb/strlcat.h | 4 + include/cryb/strlcmp.h | 4 + include/cryb/strlcpy.h | 4 + include/cryb/test.h | 6 + include/cryb/to.h | 4 +- include/cryb/wstring.h | 4 + lib/cipher/Makefile.am | 6 +- lib/cipher/{aes.c => cryb_aes.c} | 0 lib/cipher/cryb_cipher.c | 9 + lib/cipher/{rc4.c => cryb_rc4.c} | 0 lib/core/Makefile.am | 6 +- lib/core/cryb_core.c | 13 +- lib/core/{string.c => cryb_string.c} | 2 +- lib/core/{_string.c => cryb_string_impl.c} | 0 lib/core/{wstring.c => cryb_wstring.c} | 2 +- lib/cpe/Makefile.am | 15 +- lib/cpe/cpe.c | 158 ------------ lib/cpe/cryb_cpe.c | 45 ++++ lib/cpe/cryb_cpe_clone.c | 56 +++++ lib/cpe/cryb_cpe_copy_attr.c | 61 +++++ lib/cpe/cryb_cpe_destroy.c | 50 ++++ lib/cpe/cryb_cpe_new.c | 51 ++++ lib/cpe/{cpe_string.c => cryb_cpe_string.c} | 0 lib/cpe/cryb_cpe_upgrade.c | 54 ++++ lib/cpe/cryb_cpe_upgrade22.c | 65 +++++ lib/cpe/{cpe_uri.c => cryb_cpe_uri.c} | 0 lib/cpe/{cpe_wfn.c => cryb_cpe_wfn.c} | 0 lib/digest/Makefile.am | 18 +- lib/digest/cryb_digest.c | 9 + lib/digest/{md2.c => cryb_md2.c} | 0 lib/digest/{md4.c => cryb_md4.c} | 0 lib/digest/{md5.c => cryb_md5.c} | 0 lib/digest/{sha1.c => cryb_sha1.c} | 0 lib/digest/{sha224.c => cryb_sha224.c} | 0 lib/digest/{sha256.c => cryb_sha256.c} | 0 lib/digest/{sha384.c => cryb_sha384.c} | 0 lib/digest/{sha512.c => cryb_sha512.c} | 0 lib/enc/Makefile.am | 11 +- .../{cryb_base32.c => cryb_base32_decode.c} | 61 ----- lib/enc/cryb_base32_encode.c | 98 ++++++++ .../{cryb_base64.c => cryb_base64_decode.c} | 49 ---- lib/enc/cryb_base64_encode.c | 86 +++++++ include/cryb/hotp.h => lib/enc/cryb_enc.c | 20 +- .../{cryb_percent.c => cryb_percent_decode.c} | 35 --- lib/enc/cryb_percent_encode.c | 72 ++++++ lib/hash/Makefile.am | 14 +- lib/hash/{adler32.c => cryb_adler32.c} | 0 lib/hash/{fletcher16.c => cryb_fletcher16.c} | 0 lib/hash/{fletcher32.c => cryb_fletcher32.c} | 0 lib/hash/{fletcher64.c => cryb_fletcher64.c} | 0 lib/hash/cryb_hash.c | 44 ++++ lib/hash/{murmur3_32.c => cryb_murmur3_32.c} | 0 lib/hash/{pearson.c => cryb_pearson.c} | 0 lib/mac/Makefile.am | 12 +- lib/mac/{hmac_sha1.c => cryb_hmac_sha1.c} | 0 lib/mac/{hmac_sha224.c => cryb_hmac_sha224.c} | 0 lib/mac/{hmac_sha256.c => cryb_hmac_sha256.c} | 0 lib/mac/{hmac_sha384.c => cryb_hmac_sha384.c} | 0 lib/mac/{hmac_sha512.c => cryb_hmac_sha512.c} | 0 include/cryb/totp.h => lib/mac/cryb_mac.c | 20 +- lib/mpi/Makefile.am | 29 ++- lib/mpi/cryb_mpi.c | 44 ++++ lib/mpi/cryb_mpi_add.c | 65 +++++ lib/mpi/{mpi_add.c => cryb_mpi_add_abs.c} | 30 +-- lib/mpi/cryb_mpi_cmp.c | 58 +++++ lib/mpi/{mpi_cmp.c => cryb_mpi_cmp_abs.c} | 45 +--- lib/mpi/cryb_mpi_copy.c | 62 +++++ lib/mpi/cryb_mpi_destroy.c | 54 ++++ lib/mpi/cryb_mpi_eq.c | 49 ++++ lib/mpi/cryb_mpi_eq_abs.c | 49 ++++ lib/mpi/cryb_mpi_grow.c | 66 +++++ lib/mpi/cryb_mpi_impl.h | 40 +++ lib/mpi/cryb_mpi_init.c | 51 ++++ lib/mpi/cryb_mpi_load.c | 84 +++++++ lib/mpi/cryb_mpi_lshift.c | 74 ++++++ lib/mpi/cryb_mpi_negate.c | 51 ++++ lib/mpi/{mpi_shift.c => cryb_mpi_rshift.c} | 39 +-- lib/mpi/cryb_mpi_set.c | 64 +++++ lib/mpi/cryb_mpi_sub.c | 64 +++++ lib/mpi/{mpi_sub.c => cryb_mpi_sub_abs.c} | 29 +-- lib/mpi/cryb_mpi_swap.c | 57 +++++ lib/mpi/cryb_mpi_zero.c | 56 +++++ lib/mpi/mpi.c | 235 ------------------ lib/oath/Makefile.am | 18 +- lib/oath/cryb_oath.c | 44 ++++ lib/oath/{oath_hotp.c => cryb_oath_hotp.c} | 0 lib/oath/{oath_key.c => cryb_oath_key.c} | 0 ...oath_key_alloc.c => cryb_oath_key_alloc.c} | 0 ...th_key_create.c => cryb_oath_key_create.c} | 0 ...oath_key_dummy.c => cryb_oath_key_dummy.c} | 0 .../{oath_key_free.c => cryb_oath_key_free.c} | 0 ..._from_file.c => cryb_oath_key_from_file.c} | 0 ...ey_from_uri.c => cryb_oath_key_from_uri.c} | 0 lib/oath/{oath_mode.c => cryb_oath_mode.c} | 0 lib/oath/{oath_totp.c => cryb_oath_totp.c} | 0 lib/rand/Makefile.am | 5 +- lib/rand/cryb_rand.c | 34 +-- lib/rand/cryb_rand_bytes.c | 64 +++++ lib/test/Makefile.am | 12 +- lib/test/{t_const.c => cryb_t_const.c} | 0 lib/test/{t_file.c => cryb_t_file.c} | 0 lib/test/{t_main.c => cryb_t_main.c} | 0 lib/test/{t_malloc.c => cryb_t_malloc.c} | 0 lib/test/{t_util.c => cryb_t_util.c} | 0 lib/test/cryb_test.c | 44 ++++ 148 files changed, 2349 insertions(+), 790 deletions(-) rename include/cryb/{version.h => core.h} (89%) create mode 100644 include/cryb/oath_hotp.h create mode 100644 include/cryb/oath_totp.h rename lib/cipher/{aes.c => cryb_aes.c} (100%) rename lib/cipher/{rc4.c => cryb_rc4.c} (100%) rename lib/core/{string.c => cryb_string.c} (98%) rename lib/core/{_string.c => cryb_string_impl.c} (100%) rename lib/core/{wstring.c => cryb_wstring.c} (98%) delete mode 100644 lib/cpe/cpe.c create mode 100644 lib/cpe/cryb_cpe.c create mode 100644 lib/cpe/cryb_cpe_clone.c create mode 100644 lib/cpe/cryb_cpe_copy_attr.c create mode 100644 lib/cpe/cryb_cpe_destroy.c create mode 100644 lib/cpe/cryb_cpe_new.c rename lib/cpe/{cpe_string.c => cryb_cpe_string.c} (100%) create mode 100644 lib/cpe/cryb_cpe_upgrade.c create mode 100644 lib/cpe/cryb_cpe_upgrade22.c rename lib/cpe/{cpe_uri.c => cryb_cpe_uri.c} (100%) rename lib/cpe/{cpe_wfn.c => cryb_cpe_wfn.c} (100%) rename lib/digest/{md2.c => cryb_md2.c} (100%) rename lib/digest/{md4.c => cryb_md4.c} (100%) rename lib/digest/{md5.c => cryb_md5.c} (100%) rename lib/digest/{sha1.c => cryb_sha1.c} (100%) rename lib/digest/{sha224.c => cryb_sha224.c} (100%) rename lib/digest/{sha256.c => cryb_sha256.c} (100%) rename lib/digest/{sha384.c => cryb_sha384.c} (100%) rename lib/digest/{sha512.c => cryb_sha512.c} (100%) rename lib/enc/{cryb_base32.c => cryb_base32_decode.c} (75%) create mode 100644 lib/enc/cryb_base32_encode.c rename lib/enc/{cryb_base64.c => cryb_base64_decode.c} (81%) create mode 100644 lib/enc/cryb_base64_encode.c rename include/cryb/hotp.h => lib/enc/cryb_enc.c (84%) rename lib/enc/{cryb_percent.c => cryb_percent_decode.c} (77%) create mode 100644 lib/enc/cryb_percent_encode.c rename lib/hash/{adler32.c => cryb_adler32.c} (100%) rename lib/hash/{fletcher16.c => cryb_fletcher16.c} (100%) rename lib/hash/{fletcher32.c => cryb_fletcher32.c} (100%) rename lib/hash/{fletcher64.c => cryb_fletcher64.c} (100%) create mode 100644 lib/hash/cryb_hash.c rename lib/hash/{murmur3_32.c => cryb_murmur3_32.c} (100%) rename lib/hash/{pearson.c => cryb_pearson.c} (100%) rename lib/mac/{hmac_sha1.c => cryb_hmac_sha1.c} (100%) rename lib/mac/{hmac_sha224.c => cryb_hmac_sha224.c} (100%) rename lib/mac/{hmac_sha256.c => cryb_hmac_sha256.c} (100%) rename lib/mac/{hmac_sha384.c => cryb_hmac_sha384.c} (100%) rename lib/mac/{hmac_sha512.c => cryb_hmac_sha512.c} (100%) rename include/cryb/totp.h => lib/mac/cryb_mac.c (84%) create mode 100644 lib/mpi/cryb_mpi.c create mode 100644 lib/mpi/cryb_mpi_add.c rename lib/mpi/{mpi_add.c => cryb_mpi_add_abs.c} (86%) create mode 100644 lib/mpi/cryb_mpi_cmp.c rename lib/mpi/{mpi_cmp.c => cryb_mpi_cmp_abs.c} (74%) create mode 100644 lib/mpi/cryb_mpi_copy.c create mode 100644 lib/mpi/cryb_mpi_destroy.c create mode 100644 lib/mpi/cryb_mpi_eq.c create mode 100644 lib/mpi/cryb_mpi_eq_abs.c create mode 100644 lib/mpi/cryb_mpi_grow.c create mode 100644 lib/mpi/cryb_mpi_impl.h create mode 100644 lib/mpi/cryb_mpi_init.c create mode 100644 lib/mpi/cryb_mpi_load.c create mode 100644 lib/mpi/cryb_mpi_lshift.c create mode 100644 lib/mpi/cryb_mpi_negate.c rename lib/mpi/{mpi_shift.c => cryb_mpi_rshift.c} (75%) create mode 100644 lib/mpi/cryb_mpi_set.c create mode 100644 lib/mpi/cryb_mpi_sub.c rename lib/mpi/{mpi_sub.c => cryb_mpi_sub_abs.c} (85%) create mode 100644 lib/mpi/cryb_mpi_swap.c create mode 100644 lib/mpi/cryb_mpi_zero.c delete mode 100644 lib/mpi/mpi.c create mode 100644 lib/oath/cryb_oath.c rename lib/oath/{oath_hotp.c => cryb_oath_hotp.c} (100%) rename lib/oath/{oath_key.c => cryb_oath_key.c} (100%) rename lib/oath/{oath_key_alloc.c => cryb_oath_key_alloc.c} (100%) rename lib/oath/{oath_key_create.c => cryb_oath_key_create.c} (100%) rename lib/oath/{oath_key_dummy.c => cryb_oath_key_dummy.c} (100%) rename lib/oath/{oath_key_free.c => cryb_oath_key_free.c} (100%) rename lib/oath/{oath_key_from_file.c => cryb_oath_key_from_file.c} (100%) rename lib/oath/{oath_key_from_uri.c => cryb_oath_key_from_uri.c} (100%) rename lib/oath/{oath_mode.c => cryb_oath_mode.c} (100%) rename lib/oath/{oath_totp.c => cryb_oath_totp.c} (100%) create mode 100644 lib/rand/cryb_rand_bytes.c rename lib/test/{t_const.c => cryb_t_const.c} (100%) rename lib/test/{t_file.c => cryb_t_file.c} (100%) rename lib/test/{t_main.c => cryb_t_main.c} (100%) rename lib/test/{t_malloc.c => cryb_t_malloc.c} (100%) rename lib/test/{t_util.c => cryb_t_util.c} (100%) create mode 100644 lib/test/cryb_test.c diff --git a/configure.ac b/configure.ac index 11a9d62..c97da0c 100644 --- a/configure.ac +++ b/configure.ac @@ -1,6 +1,6 @@ AC_PREREQ([2.63]) AC_INIT([cryb.to], [devel], [des@des.no], [cryb-to], [http://cryb.to/]) -AC_CONFIG_SRCDIR([include/cryb/to.h]) +AC_CONFIG_SRCDIR([include/cryb/core.h]) AC_CONFIG_MACRO_DIR([m4]) AM_INIT_AUTOMAKE([foreign no-dist-gzip dist-xz]) AM_CONFIG_HEADER([include/config.h]) @@ -83,9 +83,8 @@ AC_SUBST(RT_LIBS) AC_ARG_ENABLE([doc], AC_HELP_STRING([--disable-doc], [do not build the documentation]), - [], - [with_doc=yes]) -AM_CONDITIONAL([WITH_DOC], [test x"$with_doc" = x"yes"]) + [with_doc=$enableval]) +AM_CONDITIONAL([WITH_DOC], [test x"$with_doc" != x"no"]) ############################################################################ # diff --git a/include/cryb/Makefile.am b/include/cryb/Makefile.am index cc9798f..2a5a5f0 100644 --- a/include/cryb/Makefile.am +++ b/include/cryb/Makefile.am @@ -15,6 +15,7 @@ cryb_HEADERS += \ algorithm.h \ attributes.h \ bitwise.h \ + core.h \ ctype.h \ endian.h \ memset_s.h \ @@ -22,7 +23,6 @@ cryb_HEADERS += \ strlcat.h \ strlcmp.h \ strlcpy.h \ - version.h \ wstring.h \ \ to.h @@ -84,10 +84,10 @@ endif CRYB_MPI if CRYB_OATH cryb_HEADERS += \ - hotp.h \ oath_constants.h \ + oath_hotp.h \ + oath_totp.h \ oath_types.h \ - totp.h \ \ oath.h endif CRYB_OATH diff --git a/include/cryb/aes.h b/include/cryb/aes.h index 3308e0d..7555be9 100644 --- a/include/cryb/aes.h +++ b/include/cryb/aes.h @@ -30,6 +30,10 @@ #ifndef CRYB_AES_H_INCLUDED #define CRYB_AES_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #include #define AES_BLOCK_LEN 16 diff --git a/include/cryb/algorithm.h b/include/cryb/algorithm.h index d21f257..07cb6ae 100644 --- a/include/cryb/algorithm.h +++ b/include/cryb/algorithm.h @@ -31,6 +31,10 @@ #ifndef CRYB_ALGORITHM_H_INCLUDED #define CRYB_ALGORITHM_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + typedef enum algorithm_type { digest_algorithm, mac_algorithm, diff --git a/include/cryb/attributes.h b/include/cryb/attributes.h index 84dcd37..f23fb5f 100644 --- a/include/cryb/attributes.h +++ b/include/cryb/attributes.h @@ -30,6 +30,10 @@ #ifndef CRYB_ATTRIBUTES_H_INCLUDED #define CRYB_ATTRIBUTES_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #if defined(__GNUC__) || defined(__clang__) # define CRYB_PRINTF(fmt, arg) __attribute__((format(printf, fmt, arg))) # define CRYB_NORETURN __attribute__((noreturn)) diff --git a/include/cryb/bitwise.h b/include/cryb/bitwise.h index cb4a4cf..204014d 100644 --- a/include/cryb/bitwise.h +++ b/include/cryb/bitwise.h @@ -31,6 +31,10 @@ #ifndef CRYB_BITWISE_H_INCLUDED #define CRYB_BITWISE_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #define CRYB_ROL_ROR(N) \ static inline uint##N##_t rol##N(uint##N##_t i, int n) \ { \ diff --git a/include/cryb/cipher.h b/include/cryb/cipher.h index fba870a..22bdaea 100644 --- a/include/cryb/cipher.h +++ b/include/cryb/cipher.h @@ -30,6 +30,12 @@ #ifndef CRYB_CIPHER_H_INCLUDED #define CRYB_CIPHER_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + +const char *cryb_cipher_version(void); + #define cipher_ctx cryb_cipher_ctx #define cipher_init_func cryb_cipher_init_func #define cipher_update_func cryb_cipher_update_func diff --git a/include/cryb/version.h b/include/cryb/core.h similarity index 89% rename from include/cryb/version.h rename to include/cryb/core.h index 8a00bc5..4f6ec7f 100644 --- a/include/cryb/version.h +++ b/include/cryb/core.h @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2014-2016 Dag-Erling Smørgrav + * Copyright (c) 2015 Dag-Erling Smørgrav * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -27,9 +27,13 @@ * SUCH DAMAGE. */ -#ifndef CRYB_VERSION_H_INCLUDED -#define CRYB_VERSION_H_INCLUDED +#ifndef CRYB_CORE_H_INCLUDED +#define CRYB_CORE_H_INCLUDED -#define CRYB_VERSION 20160321 +#ifndef CRYB_TO +#include +#endif + +const char *cryb_core_version(void); #endif diff --git a/include/cryb/coverage.h b/include/cryb/coverage.h index e7ce347..c23d477 100644 --- a/include/cryb/coverage.h +++ b/include/cryb/coverage.h @@ -30,6 +30,10 @@ #ifndef CRYB_COVERAGE_H_INCLUDED #define CRYB_COVERAGE_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #if _BullseyeCoverage # define CRYB_COVERAGE 1 # define CRYB_DISABLE_COVERAGE _Pragma("BullseyeCoverage save off") diff --git a/include/cryb/cpe.h b/include/cryb/cpe.h index 681720b..4d51064 100644 --- a/include/cryb/cpe.h +++ b/include/cryb/cpe.h @@ -30,6 +30,12 @@ #ifndef CRYB_CPE_H_INCLUDED #define CRYB_CPE_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + +const char *cryb_cpe_version(void); + #define cpe_name cryb_cpe_name #define cpe22_ver 202 @@ -71,6 +77,19 @@ typedef struct cpe_name { wchar_t *attr[]; } cpe_name; +#define cpe_clone cryb_cpe_clone +#define cpe_copy_attr cryb_cpe_copy_attr +#define cpe_destroy cryb_cpe_destroy +#define cpe_new cryb_cpe_new +#define cpe_bind_to_fs cryb_cpe_bind_to_fs +#define cpe_unbind_fs cryb_cpe_unbind_fs +#define cpe_upgrade22 cryb_cpe_upgrade22 +#define cpe_upgrade cryb_cpe_upgrade +#define cpe_bind_to_uri cryb_cpe_bind_to_uri +#define cpe_unbind_uri cryb_cpe_unbind_uri +#define cpe_from_wfn cryb_cpe_from_wfn +#define cpe_to_wfn cryb_cpe_to_wfn + cpe_name *cpe_new(void); void cpe_destroy(cpe_name *); cpe_name *cpe_clone(const cpe_name *); @@ -86,4 +105,8 @@ wstring *cpe_bind_to_uri(const cpe_name *); cpe_name *cpe_from_wfn(const wchar_t *); wchar_t *cpe_to_wfn(const cpe_name *); +/* not part of public API */ +int cpe_copy_attr(cpe_name *, const cpe_name *, int, int); +cpe_name *cpe_upgrade22(const cpe_name *); + #endif diff --git a/include/cryb/ctype.h b/include/cryb/ctype.h index a493b63..5b08a5f 100644 --- a/include/cryb/ctype.h +++ b/include/cryb/ctype.h @@ -30,6 +30,10 @@ #ifndef CRYB_CTYPE_H_INCLUDED #define CRYB_CTYPE_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + /* * Evaluates to non-zero if the argument is a digit. */ diff --git a/include/cryb/digest.h b/include/cryb/digest.h index 42ae568..188c566 100644 --- a/include/cryb/digest.h +++ b/include/cryb/digest.h @@ -31,6 +31,12 @@ #ifndef CRYB_DIGEST_H_INCLUDED #define CRYB_DIGEST_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + +const char *cryb_digest_version(void); + #define digest_ctx cryb_digest_ctx #define digest_init_func cryb_digest_init_func #define digest_update_func cryb_digest_update_func diff --git a/include/cryb/enc.h b/include/cryb/enc.h index e0f7bcd..fd6ec10 100644 --- a/include/cryb/enc.h +++ b/include/cryb/enc.h @@ -30,7 +30,13 @@ #ifndef CRYB_ENC_H_INCLUDED #define CRYB_ENC_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #include #include +const char *cryb_enc_version(void); + #endif diff --git a/include/cryb/endian.h b/include/cryb/endian.h index 1eb34b5..eab1c32 100644 --- a/include/cryb/endian.h +++ b/include/cryb/endian.h @@ -30,6 +30,10 @@ #ifndef CRYB_ENDIAN_H_INCLUDED #define CRYB_ENDIAN_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #if HAVE_SYS_ENDIAN_H #include #endif diff --git a/include/cryb/hash.h b/include/cryb/hash.h index e5d946e..2e070a0 100644 --- a/include/cryb/hash.h +++ b/include/cryb/hash.h @@ -30,16 +30,26 @@ #ifndef CRYB_HASH_H_INCLUDED #define CRYB_HASH_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + +const char *cryb_hash_version(void); + +#define adler32_hash cryb_adler32_hash +#define fletcher16_hash cryb_fletcher16_hash +#define fletcher32_hash cryb_fletcher32_hash +#define fletcher64_hash cryb_fletcher64_hash +#define murmur3_32_hash cryb_murmur3_32_hash #define pearson_hash cryb_pearson_hash #define pearson_hash_str cryb_pearson_hash_str -#define murmur3_32_hash cryb_murmur3_32_hash uint32_t adler32_hash(const void *, size_t); uint16_t fletcher16_hash(const void *, size_t); uint32_t fletcher32_hash(const void *, size_t); uint64_t fletcher64_hash(const void *, size_t); +uint32_t murmur3_32_hash(const void *, size_t, uint32_t); uint8_t pearson_hash(const void *, size_t); uint8_t pearson_hash_str(const char *); -uint32_t murmur3_32_hash(const void *, size_t, uint32_t); #endif diff --git a/include/cryb/hmac.h b/include/cryb/hmac.h index 3fce1dc..d2b491f 100644 --- a/include/cryb/hmac.h +++ b/include/cryb/hmac.h @@ -30,6 +30,10 @@ #ifndef CRYB_HMAC_H_INCLUDED #define CRYB_HMAC_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #include #include #include diff --git a/include/cryb/hmac_sha1.h b/include/cryb/hmac_sha1.h index 03db666..ac51d0b 100644 --- a/include/cryb/hmac_sha1.h +++ b/include/cryb/hmac_sha1.h @@ -30,6 +30,10 @@ #ifndef CRYB_HMAC_SHA1_H_INCLUDED #define CRYB_HMAC_SHA1_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #include #define HMAC_SHA1_MAC_LEN 20 diff --git a/include/cryb/hmac_sha224.h b/include/cryb/hmac_sha224.h index 30a5eaa..5faf71f 100644 --- a/include/cryb/hmac_sha224.h +++ b/include/cryb/hmac_sha224.h @@ -30,6 +30,10 @@ #ifndef CRYB_HMAC_SHA224_H_INCLUDED #define CRYB_HMAC_SHA224_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #include #define HMAC_SHA224_MAC_LEN 28 diff --git a/include/cryb/hmac_sha256.h b/include/cryb/hmac_sha256.h index 57fc83a..66ad95c 100644 --- a/include/cryb/hmac_sha256.h +++ b/include/cryb/hmac_sha256.h @@ -30,6 +30,10 @@ #ifndef CRYB_HMAC_SHA256_H_INCLUDED #define CRYB_HMAC_SHA256_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #include #define HMAC_SHA256_MAC_LEN 32 diff --git a/include/cryb/hmac_sha384.h b/include/cryb/hmac_sha384.h index d2375ed..a36c7c9 100644 --- a/include/cryb/hmac_sha384.h +++ b/include/cryb/hmac_sha384.h @@ -30,6 +30,10 @@ #ifndef CRYB_HMAC_SHA384_H_INCLUDED #define CRYB_HMAC_SHA384_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #include #define HMAC_SHA384_MAC_LEN 48 diff --git a/include/cryb/hmac_sha512.h b/include/cryb/hmac_sha512.h index c354487..cd88732 100644 --- a/include/cryb/hmac_sha512.h +++ b/include/cryb/hmac_sha512.h @@ -30,6 +30,10 @@ #ifndef CRYB_HMAC_SHA512_H_INCLUDED #define CRYB_HMAC_SHA512_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #include #define HMAC_SHA512_MAC_LEN 64 diff --git a/include/cryb/impl.h b/include/cryb/impl.h index 87eccfd..06cf12b 100644 --- a/include/cryb/impl.h +++ b/include/cryb/impl.h @@ -34,6 +34,10 @@ # include "config.h" #endif +#ifndef CRYB_TO +#include +#endif + #include #include diff --git a/include/cryb/mac.h b/include/cryb/mac.h index 9afa5ab..2d2ddb0 100644 --- a/include/cryb/mac.h +++ b/include/cryb/mac.h @@ -30,6 +30,12 @@ #ifndef CRYB_MAC_H_INCLUDED #define CRYB_MAC_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #include +const char *cryb_mac_version(void); + #endif diff --git a/include/cryb/md2.h b/include/cryb/md2.h index 3766ff3..7e500aa 100644 --- a/include/cryb/md2.h +++ b/include/cryb/md2.h @@ -30,6 +30,10 @@ #ifndef CRYB_MD2_H_INCLUDED #define CRYB_MD2_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #include #define MD2_BLOCK_LEN 16 diff --git a/include/cryb/md4.h b/include/cryb/md4.h index 5196435..02d223b 100644 --- a/include/cryb/md4.h +++ b/include/cryb/md4.h @@ -30,6 +30,10 @@ #ifndef CRYB_MD4_H_INCLUDED #define CRYB_MD4_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #include #define MD4_BLOCK_LEN 64 diff --git a/include/cryb/md5.h b/include/cryb/md5.h index b093f41..a7d70e9 100644 --- a/include/cryb/md5.h +++ b/include/cryb/md5.h @@ -31,6 +31,10 @@ #ifndef CRYB_MD5_H_INCLUDED #define CRYB_MD5_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #include #define MD5_BLOCK_LEN 64 diff --git a/include/cryb/memset_s.h b/include/cryb/memset_s.h index a091433..16760ea 100644 --- a/include/cryb/memset_s.h +++ b/include/cryb/memset_s.h @@ -30,6 +30,10 @@ #ifndef CRYB_MEMSET_S_H_INCLUDED #define CRYB_MEMSET_S_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + int cryb_memset_s(void *, size_t, int, size_t); #if !HAVE_MEMSET_S diff --git a/include/cryb/mpi.h b/include/cryb/mpi.h index e10f4c8..8f46707 100644 --- a/include/cryb/mpi.h +++ b/include/cryb/mpi.h @@ -30,6 +30,32 @@ #ifndef CRYB_MPI_H_INCLUDED #define CRYB_MPI_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + +const char *cryb_mpi_version(void); + +#define mpi_add cryb_mpi_add +#define mpi_add_abs cryb_mpi_add_abs +#define mpi_cmp cryb_mpi_cmp +#define mpi_cmp_abs cryb_mpi_cmp_abs +#define mpi_copy cryb_mpi_copy +#define mpi_destroy cryb_mpi_destroy +#define mpi_eq cryb_mpi_eq +#define mpi_eq_abs cryb_mpi_eq_abs +#define mpi_grow cryb_mpi_grow +#define mpi_init cryb_mpi_init +#define mpi_load cryb_mpi_load +#define mpi_lshift cryb_mpi_lshift +#define mpi_negate cryb_mpi_negate +#define mpi_rshift cryb_mpi_rshift +#define mpi_set cryb_mpi_set +#define mpi_sub cryb_mpi_sub +#define mpi_sub_abs cryb_mpi_sub_abs +#define mpi_swap cryb_mpi_swap +#define mpi_zero cryb_mpi_zero + #define CRYB_MPI_SWORDS 16 typedef struct cryb_mpi { diff --git a/include/cryb/oath.h b/include/cryb/oath.h index 1443cd1..9d19a20 100644 --- a/include/cryb/oath.h +++ b/include/cryb/oath.h @@ -31,8 +31,23 @@ #ifndef OATH_H_INCLUDED #define OATH_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #include #include +#include +#include + +const char *cryb_oath_version(void); + +#define oath_key_alloc cryb_oath_key_alloc +#define oath_key_create cryb_oath_key_create +#define oath_key_dummy cryb_oath_key_dummy +#define oath_key_from_uri cryb_oath_key_from_uri +#define oath_key_free cryb_oath_key_free +#define oath_key_to_uri cryb_oath_key_to_uri struct oath_key *oath_key_alloc(void); struct oath_key *oath_key_create(const char *, enum oath_mode, @@ -46,12 +61,4 @@ struct oath_key *oath_key_dummy(enum oath_mode, enum oath_hash, unsigned int); enum oath_mode oath_mode(const char *); -unsigned int oath_hotp(const uint8_t *, size_t, uint64_t, unsigned int); -unsigned int oath_hotp_current(struct oath_key *); -int oath_hotp_match(struct oath_key *, unsigned int, int); - -unsigned int oath_totp(const uint8_t *, size_t, unsigned int); -unsigned int oath_totp_current(const struct oath_key *); -int oath_totp_match(struct oath_key *, unsigned int, int); - #endif diff --git a/include/cryb/oath_constants.h b/include/cryb/oath_constants.h index 9de2e45..17630ac 100644 --- a/include/cryb/oath_constants.h +++ b/include/cryb/oath_constants.h @@ -30,6 +30,10 @@ #ifndef OATH_CONSTANTS_H_INCLUDED #define OATH_CONSTANTS_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + /* * OATH modes */ diff --git a/include/cryb/oath_hotp.h b/include/cryb/oath_hotp.h new file mode 100644 index 0000000..36d4b35 --- /dev/null +++ b/include/cryb/oath_hotp.h @@ -0,0 +1,49 @@ +/*- + * Copyright (c) 2012-2014 The University of Oslo + * Copyright (c) 2013-2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef OATH_HOTP_H_INCLUDED +#define OATH_HOTP_H_INCLUDED + +#ifndef CRYB_TO +#include +#endif + +#include +#include + +#define oath_hotp cryb_oath_hotp +#define oath_hotp_current cryb_oath_hotp_current +#define oath_hotp_match cryb_oath_hotp_match + +unsigned int oath_hotp(const uint8_t *, size_t, uint64_t, unsigned int); +unsigned int oath_hotp_current(struct oath_key *); +int oath_hotp_match(struct oath_key *, unsigned int, int); + +#endif diff --git a/include/cryb/oath_totp.h b/include/cryb/oath_totp.h new file mode 100644 index 0000000..724d484 --- /dev/null +++ b/include/cryb/oath_totp.h @@ -0,0 +1,49 @@ +/*- + * Copyright (c) 2012-2014 The University of Oslo + * Copyright (c) 2013-2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef OATH_TOTP_H_INCLUDED +#define OATH_TOTP_H_INCLUDED + +#ifndef CRYB_TO +#include +#endif + +#include +#include + +#define oath_totp cryb_oath_totp +#define oath_totp_current cryb_oath_totp_current +#define oath_totp_match cryb_oath_totp_match + +unsigned int oath_totp(const uint8_t *, size_t, unsigned int); +unsigned int oath_totp_current(const struct oath_key *); +int oath_totp_match(struct oath_key *, unsigned int, int); + +#endif diff --git a/include/cryb/oath_types.h b/include/cryb/oath_types.h index b5c5de8..91edd99 100644 --- a/include/cryb/oath_types.h +++ b/include/cryb/oath_types.h @@ -30,6 +30,10 @@ #ifndef OATH_TYPES_H_INCLUDED #define OATH_TYPES_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + /* * OATH key and associated parameters */ diff --git a/include/cryb/rand.h b/include/cryb/rand.h index 9352164..56cdc29 100644 --- a/include/cryb/rand.h +++ b/include/cryb/rand.h @@ -30,6 +30,12 @@ #ifndef CRYB_RAND_H_INCLUDED #define CRYB_RAND_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + +const char *cryb_rand_version(void); + #define rand_bytes cryb_rand_bytes int rand_bytes(uint8_t *, size_t); diff --git a/include/cryb/rc4.h b/include/cryb/rc4.h index 5dc6956..f976aa1 100644 --- a/include/cryb/rc4.h +++ b/include/cryb/rc4.h @@ -30,6 +30,10 @@ #ifndef CRYB_RC4_H_INCLUDED #define CRYB_RC4_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #include #define rc4_digest cryb_rc4_digest diff --git a/include/cryb/rfc3986.h b/include/cryb/rfc3986.h index a11d91a..7ad60df 100644 --- a/include/cryb/rfc3986.h +++ b/include/cryb/rfc3986.h @@ -30,6 +30,10 @@ #ifndef CRYB_RFC3986_H_INCLUDED #define CRYB_RFC3986_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + /* estimate of output length for percent encoding / decoding */ #define percent_enclen(l) (size_t)((l) * 3) #define percent_declen(l) (size_t)(l) diff --git a/include/cryb/rfc4648.h b/include/cryb/rfc4648.h index 47cf172..d8a7765 100644 --- a/include/cryb/rfc4648.h +++ b/include/cryb/rfc4648.h @@ -30,6 +30,10 @@ #ifndef CRYB_RFC4648_H_INCLUDED #define CRYB_RFC4648_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + /* estimate of output length for base32 encoding / decoding */ #define base32_enclen(l) (size_t)(((l + 4) / 5) * 8) #define base32_declen(l) (size_t)(((l + 7) / 8) * 5) diff --git a/include/cryb/sha1.h b/include/cryb/sha1.h index 0c30567..5a9ecbc 100644 --- a/include/cryb/sha1.h +++ b/include/cryb/sha1.h @@ -31,6 +31,10 @@ #ifndef CRYB_SHA1_H_INCLUDED #define CRYB_SHA1_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #include #define SHA1_BLOCK_LEN 64 diff --git a/include/cryb/sha224.h b/include/cryb/sha224.h index 5588688..075c125 100644 --- a/include/cryb/sha224.h +++ b/include/cryb/sha224.h @@ -27,6 +27,10 @@ #ifndef CRYB_SHA224_H_INCLUDED #define CRYB_SHA224_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #include #define SHA224_BLOCK_LEN 64 diff --git a/include/cryb/sha256.h b/include/cryb/sha256.h index ac12bee..1e9c7b1 100644 --- a/include/cryb/sha256.h +++ b/include/cryb/sha256.h @@ -27,6 +27,10 @@ #ifndef CRYB_SHA256_H_INCLUDED #define CRYB_SHA256_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #include #define SHA256_BLOCK_LEN 64 diff --git a/include/cryb/sha384.h b/include/cryb/sha384.h index c418675..965eef7 100644 --- a/include/cryb/sha384.h +++ b/include/cryb/sha384.h @@ -30,6 +30,10 @@ #ifndef CRYB_SHA384_H_INCLUDED #define CRYB_SHA384_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #include #define SHA384_BLOCK_LEN 128 diff --git a/include/cryb/sha512.h b/include/cryb/sha512.h index 32df10b..5fb6087 100644 --- a/include/cryb/sha512.h +++ b/include/cryb/sha512.h @@ -30,6 +30,10 @@ #ifndef CRYB_SHA512_H_INCLUDED #define CRYB_SHA512_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #include #define SHA512_BLOCK_LEN 128 diff --git a/include/cryb/string.h b/include/cryb/string.h index 408eb5b..bf26256 100644 --- a/include/cryb/string.h +++ b/include/cryb/string.h @@ -30,6 +30,10 @@ #ifndef CRYB_STRING_H_INCLUDED #define CRYB_STRING_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #define string_new cryb_string_new #define string_len cryb_string_len #define string_dup cryb_string_dup diff --git a/include/cryb/strlcat.h b/include/cryb/strlcat.h index 64a3a7f..06b787c 100644 --- a/include/cryb/strlcat.h +++ b/include/cryb/strlcat.h @@ -30,6 +30,10 @@ #ifndef CRYB_STRLCAT_H_INCLUDED #define CRYB_STRLCAT_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + size_t cryb_strlcat(char *, const char *, size_t); #if !HAVE_STRLCAT diff --git a/include/cryb/strlcmp.h b/include/cryb/strlcmp.h index 22b08b7..63d022d 100644 --- a/include/cryb/strlcmp.h +++ b/include/cryb/strlcmp.h @@ -30,6 +30,10 @@ #ifndef CRYB_STRLCMP_H_INCLUDED #define CRYB_STRLCMP_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + int cryb_strlcmp(const char *, const char *, size_t); #if !HAVE_STRLCMP diff --git a/include/cryb/strlcpy.h b/include/cryb/strlcpy.h index ee80bd2..8558635 100644 --- a/include/cryb/strlcpy.h +++ b/include/cryb/strlcpy.h @@ -30,6 +30,10 @@ #ifndef CRYB_STRLCPY_H_INCLUDED #define CRYB_STRLCPY_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + size_t cryb_strlcpy(char *, const char *, size_t); #if !HAVE_STRLCPY diff --git a/include/cryb/test.h b/include/cryb/test.h index 9e3aac1..276d027 100644 --- a/include/cryb/test.h +++ b/include/cryb/test.h @@ -30,11 +30,17 @@ #ifndef CRYB_TEST_H_INCLUDED #define CRYB_TEST_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #include #include CRYB_DISABLE_COVERAGE +const char *cryb_test_version(void); + /* * Structure describing a test. Includes a pointer to the function that * performs the test, a pointer to the default description or comment diff --git a/include/cryb/to.h b/include/cryb/to.h index bd15afc..1782b67 100644 --- a/include/cryb/to.h +++ b/include/cryb/to.h @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2014 Dag-Erling Smørgrav + * Copyright (c) 2014-2016 Dag-Erling Smørgrav * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,6 +30,6 @@ #ifndef CRYB_TO_H_INCLUDED #define CRYB_TO_H_INCLUDED -#include +#define CRYB_TO 20160321 #endif diff --git a/include/cryb/wstring.h b/include/cryb/wstring.h index 81867f9..477b986 100644 --- a/include/cryb/wstring.h +++ b/include/cryb/wstring.h @@ -30,6 +30,10 @@ #ifndef CRYB_WSTRING_H_INCLUDED #define CRYB_WSTRING_H_INCLUDED +#ifndef CRYB_TO +#include +#endif + #define wstring_new cryb_wstring_new #define wstring_len cryb_wstring_len #define wstring_dup cryb_wstring_dup diff --git a/lib/cipher/Makefile.am b/lib/cipher/Makefile.am index cf2a2ce..60dd5d5 100644 --- a/lib/cipher/Makefile.am +++ b/lib/cipher/Makefile.am @@ -3,7 +3,7 @@ AM_CPPFLAGS = -I$(top_srcdir)/include lib_LTLIBRARIES = libcryb-cipher.la libcryb_cipher_la_SOURCES = \ - cryb_cipher.c \ + cryb_aes.c \ + cryb_rc4.c \ \ - aes.c \ - rc4.c + cryb_cipher.c diff --git a/lib/cipher/aes.c b/lib/cipher/cryb_aes.c similarity index 100% rename from lib/cipher/aes.c rename to lib/cipher/cryb_aes.c diff --git a/lib/cipher/cryb_cipher.c b/lib/cipher/cryb_cipher.c index f52fc87..19f0379 100644 --- a/lib/cipher/cryb_cipher.c +++ b/lib/cipher/cryb_cipher.c @@ -37,6 +37,15 @@ #include #include +static const char *cryb_cipher_version_string = PACKAGE_VERSION; + +const char * +cryb_cipher_version(void) +{ + + return (cryb_cipher_version_string); +} + static const cipher_algorithm **cryb_cipher_algorithms; static void diff --git a/lib/cipher/rc4.c b/lib/cipher/cryb_rc4.c similarity index 100% rename from lib/cipher/rc4.c rename to lib/cipher/cryb_rc4.c diff --git a/lib/core/Makefile.am b/lib/core/Makefile.am index 2baa10d..32dbb8c 100644 --- a/lib/core/Makefile.am +++ b/lib/core/Makefile.am @@ -5,13 +5,13 @@ lib_LTLIBRARIES = libcryb-core.la libcryb_core_la_SOURCES = \ cryb_core.c \ cryb_memset_s.c \ - string.c \ + cryb_string.c \ cryb_strlcat.c \ cryb_strlcpy.c \ cryb_strlcmp.c \ - wstring.c + cryb_wstring.c -EXTRA_DIST = _string.c +EXTRA_DIST = cryb_string_impl.c dist_man3_MANS = \ cryb_strlcat.3 \ diff --git a/lib/core/cryb_core.c b/lib/core/cryb_core.c index 40f0c8f..fb2292d 100644 --- a/lib/core/cryb_core.c +++ b/lib/core/cryb_core.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2014 Dag-Erling Smørgrav + * Copyright (c) 2015 Dag-Erling Smørgrav * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -29,4 +29,13 @@ #include "cryb/impl.h" -#include +#include + +static const char *cryb_core_version_string = PACKAGE_VERSION; + +const char * +cryb_core_version(void) +{ + + return (cryb_core_version_string); +} diff --git a/lib/core/string.c b/lib/core/cryb_string.c similarity index 98% rename from lib/core/string.c rename to lib/core/cryb_string.c index 2a85381..66e879b 100644 --- a/lib/core/string.c +++ b/lib/core/cryb_string.c @@ -38,4 +38,4 @@ #define char_t char -#include "_string.c" +#include "cryb_string_impl.c" diff --git a/lib/core/_string.c b/lib/core/cryb_string_impl.c similarity index 100% rename from lib/core/_string.c rename to lib/core/cryb_string_impl.c diff --git a/lib/core/wstring.c b/lib/core/cryb_wstring.c similarity index 98% rename from lib/core/wstring.c rename to lib/core/cryb_wstring.c index 59d140e..fa946ea 100644 --- a/lib/core/wstring.c +++ b/lib/core/cryb_wstring.c @@ -59,4 +59,4 @@ #define string_compare wstring_compare #define string_equal wstring_equal -#include "_string.c" +#include "cryb_string_impl.c" diff --git a/lib/cpe/Makefile.am b/lib/cpe/Makefile.am index e159fef..c053b48 100644 --- a/lib/cpe/Makefile.am +++ b/lib/cpe/Makefile.am @@ -3,10 +3,17 @@ AM_CPPFLAGS = -I$(top_srcdir)/include lib_LTLIBRARIES = libcryb-cpe.la libcryb_cpe_la_SOURCES = \ - cpe.c \ - cpe_string.c \ - cpe_uri.c \ - cpe_wfn.c + cryb_cpe_clone.c \ + cryb_cpe_copy_attr.c \ + cryb_cpe_destroy.c \ + cryb_cpe_new.c \ + cryb_cpe_string.c \ + cryb_cpe_upgrade22.c \ + cryb_cpe_upgrade.c \ + cryb_cpe_uri.c \ + cryb_cpe_wfn.c \ + \ + cryb_cpe.c libcryb_cpe_la_LIBADD = \ $(top_builddir)/lib/core/libcryb-core.la diff --git a/lib/cpe/cpe.c b/lib/cpe/cpe.c deleted file mode 100644 index 64f30b5..0000000 --- a/lib/cpe/cpe.c +++ /dev/null @@ -1,158 +0,0 @@ -/*- - * Copyright (c) 2014 Dag-Erling Smørgrav - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include "cryb/impl.h" - -#include -#include -#include - -#include -#include - -/* - * Free all memory allocated to a cpe structure. - */ -void -cpe_destroy(cpe_name *cpe) -{ - - if (cpe == NULL) - return; - for (int i = 0; i < cpe->nattr; ++i) - free(cpe->attr[i]); - free(cpe); -} - -/* - * Copy one or more attributes from a source cpe structure to a - * destination cpe structure. If an attribute in the source is NULL, the - * corresponding attribute in the destination will be an empty string. - */ -static int -cpe_copy_attr(cpe_name *dst, const cpe_name *src, int base, int nattr) -{ - - if (base < 0 || nattr < 0 || base + nattr > src->nattr || - base + nattr > dst->nattr) - return (-1); - if (nattr == 0) - nattr = src->nattr - base; - for (int i = base; i < base + nattr; ++i) { - if (src->attr[i] != NULL) - dst->attr[i] = wcsdup(src->attr[i]); - else - dst->attr[i] = wcsdup(L""); - if (dst->attr[i] == NULL) - return (-1); - } - return (nattr); -} - -/* - * Duplicate a cpe structure. Any attributes that are NULL in the source - * will be empty strings in the destination. - */ -cpe_name * -cpe_clone(const cpe_name *cpe) -{ - cpe_name *ncpe; - - ncpe = calloc(1, sizeof *ncpe + - cpe->nattr * sizeof *ncpe->attr); - if (ncpe == NULL) - return (NULL); - if (cpe_copy_attr(ncpe, cpe, 0, 0) < 0) { - cpe_destroy(ncpe); - return (NULL); - } - return (ncpe); -} - -/* - * Allocate a new cpe structure. - */ -cpe_name * -cpe_new(void) -{ - cpe_name *ncpe; - - if ((ncpe = calloc(1, sizeof *ncpe)) == NULL) - return (NULL); - ncpe->ver = cpe23_ver; - ncpe->nattr = cpe23_nattr; - return (ncpe); -} - -/* - * Upgrade a cpe 2.2 structure to the latest supported version. - */ -cpe_name * -cpe_upgrade22(const cpe_name *cpe) -{ - cpe_name *ncpe; - - if ((ncpe = cpe_new()) == NULL) - return (NULL); - /* copy existing attributes */ - if (cpe_copy_attr(ncpe, cpe, 0, cpe22_nattr) < 0) { - cpe_destroy(ncpe); - return (NULL); - } - /* extended attributes? */ - if (ncpe->attr[cpe22_edition][0] == L'~') { - /* - * XXX pseudo-code: - * - * - Split into fields (return an error if there are more - * than four) - * - Assign these to sw_edition, target_sw, target_hw and - * other, in that order. - */ - } - return (ncpe); -} - -/* - * Upgrade a cpe structure to the latest supported version. - */ -cpe_name * -cpe_upgrade(const cpe_name *cpe) -{ - - switch (cpe->ver) { - case cpe23_ver: - /* already latest */ - return (cpe_clone(cpe)); - case cpe22_ver: - return (cpe_upgrade22(cpe)); - default: - return (NULL); - } -} diff --git a/lib/cpe/cryb_cpe.c b/lib/cpe/cryb_cpe.c new file mode 100644 index 0000000..9858b8d --- /dev/null +++ b/lib/cpe/cryb_cpe.c @@ -0,0 +1,45 @@ +/*- + * Copyright (c) 2015 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include + +#include +#include + +static const char *cryb_cpe_version_string = PACKAGE_VERSION; + +const char * +cryb_cpe_version(void) +{ + + return (cryb_cpe_version_string); +} diff --git a/lib/cpe/cryb_cpe_clone.c b/lib/cpe/cryb_cpe_clone.c new file mode 100644 index 0000000..dcb2c78 --- /dev/null +++ b/lib/cpe/cryb_cpe_clone.c @@ -0,0 +1,56 @@ +/*- + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include + +#include +#include + +/* + * Duplicate a cpe structure. Any attributes that are NULL in the source + * will be empty strings in the destination. + */ +cpe_name * +cpe_clone(const cpe_name *cpe) +{ + cpe_name *ncpe; + + ncpe = calloc(1, sizeof *ncpe + + cpe->nattr * sizeof *ncpe->attr); + if (ncpe == NULL) + return (NULL); + if (cpe_copy_attr(ncpe, cpe, 0, 0) < 0) { + cpe_destroy(ncpe); + return (NULL); + } + return (ncpe); +} diff --git a/lib/cpe/cryb_cpe_copy_attr.c b/lib/cpe/cryb_cpe_copy_attr.c new file mode 100644 index 0000000..3b7eb01 --- /dev/null +++ b/lib/cpe/cryb_cpe_copy_attr.c @@ -0,0 +1,61 @@ +/*- + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include + +#include +#include + +/* + * Copy one or more attributes from a source cpe structure to a + * destination cpe structure. If an attribute in the source is NULL, the + * corresponding attribute in the destination will be an empty string. + */ +int +cpe_copy_attr(cpe_name *dst, const cpe_name *src, int base, int nattr) +{ + + if (base < 0 || nattr < 0 || base + nattr > src->nattr || + base + nattr > dst->nattr) + return (-1); + if (nattr == 0) + nattr = src->nattr - base; + for (int i = base; i < base + nattr; ++i) { + if (src->attr[i] != NULL) + dst->attr[i] = wcsdup(src->attr[i]); + else + dst->attr[i] = wcsdup(L""); + if (dst->attr[i] == NULL) + return (-1); + } + return (nattr); +} diff --git a/lib/cpe/cryb_cpe_destroy.c b/lib/cpe/cryb_cpe_destroy.c new file mode 100644 index 0000000..73f5d15 --- /dev/null +++ b/lib/cpe/cryb_cpe_destroy.c @@ -0,0 +1,50 @@ +/*- + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include + +#include +#include + +/* + * Free all memory allocated to a cpe structure. + */ +void +cpe_destroy(cpe_name *cpe) +{ + + if (cpe == NULL) + return; + for (int i = 0; i < cpe->nattr; ++i) + free(cpe->attr[i]); + free(cpe); +} diff --git a/lib/cpe/cryb_cpe_new.c b/lib/cpe/cryb_cpe_new.c new file mode 100644 index 0000000..d1071bc --- /dev/null +++ b/lib/cpe/cryb_cpe_new.c @@ -0,0 +1,51 @@ +/*- + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include + +#include +#include + +/* + * Allocate a new cpe structure. + */ +cpe_name * +cpe_new(void) +{ + cpe_name *ncpe; + + if ((ncpe = calloc(1, sizeof *ncpe)) == NULL) + return (NULL); + ncpe->ver = cpe23_ver; + ncpe->nattr = cpe23_nattr; + return (ncpe); +} diff --git a/lib/cpe/cpe_string.c b/lib/cpe/cryb_cpe_string.c similarity index 100% rename from lib/cpe/cpe_string.c rename to lib/cpe/cryb_cpe_string.c diff --git a/lib/cpe/cryb_cpe_upgrade.c b/lib/cpe/cryb_cpe_upgrade.c new file mode 100644 index 0000000..12367a5 --- /dev/null +++ b/lib/cpe/cryb_cpe_upgrade.c @@ -0,0 +1,54 @@ +/*- + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include + +#include +#include + +/* + * Upgrade a cpe structure to the latest supported version. + */ +cpe_name * +cpe_upgrade(const cpe_name *cpe) +{ + + switch (cpe->ver) { + case cpe23_ver: + /* already latest */ + return (cpe_clone(cpe)); + case cpe22_ver: + return (cpe_upgrade22(cpe)); + default: + return (NULL); + } +} diff --git a/lib/cpe/cryb_cpe_upgrade22.c b/lib/cpe/cryb_cpe_upgrade22.c new file mode 100644 index 0000000..002f42f --- /dev/null +++ b/lib/cpe/cryb_cpe_upgrade22.c @@ -0,0 +1,65 @@ +/*- + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include + +#include +#include + +/* + * Upgrade a cpe 2.2 structure to the latest supported version. + */ +cpe_name * +cpe_upgrade22(const cpe_name *cpe) +{ + cpe_name *ncpe; + + if ((ncpe = cpe_new()) == NULL) + return (NULL); + /* copy existing attributes */ + if (cpe_copy_attr(ncpe, cpe, 0, cpe22_nattr) < 0) { + cpe_destroy(ncpe); + return (NULL); + } + /* extended attributes? */ + if (ncpe->attr[cpe22_edition][0] == L'~') { + /* + * XXX pseudo-code: + * + * - Split into fields (return an error if there are more + * than four) + * - Assign these to sw_edition, target_sw, target_hw and + * other, in that order. + */ + } + return (ncpe); +} diff --git a/lib/cpe/cpe_uri.c b/lib/cpe/cryb_cpe_uri.c similarity index 100% rename from lib/cpe/cpe_uri.c rename to lib/cpe/cryb_cpe_uri.c diff --git a/lib/cpe/cpe_wfn.c b/lib/cpe/cryb_cpe_wfn.c similarity index 100% rename from lib/cpe/cpe_wfn.c rename to lib/cpe/cryb_cpe_wfn.c diff --git a/lib/digest/Makefile.am b/lib/digest/Makefile.am index 9691471..a84e17a 100644 --- a/lib/digest/Makefile.am +++ b/lib/digest/Makefile.am @@ -3,16 +3,16 @@ AM_CPPFLAGS = -I$(top_srcdir)/include lib_LTLIBRARIES = libcryb-digest.la libcryb_digest_la_SOURCES = \ - cryb_digest.c \ + cryb_md2.c \ + cryb_md4.c \ + cryb_md5.c \ + cryb_sha1.c \ + cryb_sha224.c \ + cryb_sha256.c \ + cryb_sha384.c \ + cryb_sha512.c \ \ - md2.c \ - md4.c \ - md5.c \ - sha1.c \ - sha224.c \ - sha256.c \ - sha384.c \ - sha512.c + cryb_digest.c dist_man3_MANS = \ cryb_sha1.3 diff --git a/lib/digest/cryb_digest.c b/lib/digest/cryb_digest.c index 2f6f2e4..ab13c97 100644 --- a/lib/digest/cryb_digest.c +++ b/lib/digest/cryb_digest.c @@ -42,6 +42,15 @@ #include #include +static const char *cryb_digest_version_string = PACKAGE_VERSION; + +const char * +cryb_digest_version(void) +{ + + return (cryb_digest_version_string); +} + static const digest_algorithm **cryb_digest_algorithms; static void diff --git a/lib/digest/md2.c b/lib/digest/cryb_md2.c similarity index 100% rename from lib/digest/md2.c rename to lib/digest/cryb_md2.c diff --git a/lib/digest/md4.c b/lib/digest/cryb_md4.c similarity index 100% rename from lib/digest/md4.c rename to lib/digest/cryb_md4.c diff --git a/lib/digest/md5.c b/lib/digest/cryb_md5.c similarity index 100% rename from lib/digest/md5.c rename to lib/digest/cryb_md5.c diff --git a/lib/digest/sha1.c b/lib/digest/cryb_sha1.c similarity index 100% rename from lib/digest/sha1.c rename to lib/digest/cryb_sha1.c diff --git a/lib/digest/sha224.c b/lib/digest/cryb_sha224.c similarity index 100% rename from lib/digest/sha224.c rename to lib/digest/cryb_sha224.c diff --git a/lib/digest/sha256.c b/lib/digest/cryb_sha256.c similarity index 100% rename from lib/digest/sha256.c rename to lib/digest/cryb_sha256.c diff --git a/lib/digest/sha384.c b/lib/digest/cryb_sha384.c similarity index 100% rename from lib/digest/sha384.c rename to lib/digest/cryb_sha384.c diff --git a/lib/digest/sha512.c b/lib/digest/cryb_sha512.c similarity index 100% rename from lib/digest/sha512.c rename to lib/digest/cryb_sha512.c diff --git a/lib/enc/Makefile.am b/lib/enc/Makefile.am index 85736f3..a7f2541 100644 --- a/lib/enc/Makefile.am +++ b/lib/enc/Makefile.am @@ -3,9 +3,14 @@ AM_CPPFLAGS = -I$(top_srcdir)/include lib_LTLIBRARIES = libcryb-enc.la libcryb_enc_la_SOURCES = \ - cryb_base32.c \ - cryb_base64.c \ - cryb_percent.c + cryb_base32_decode.c \ + cryb_base32_encode.c \ + cryb_base64_decode.c \ + cryb_base64_encode.c \ + cryb_percent_decode.c \ + cryb_percent_encode.c \ + \ + cryb_enc.c dist_man3_MANS = \ cryb_percent.3 diff --git a/lib/enc/cryb_base32.c b/lib/enc/cryb_base32_decode.c similarity index 75% rename from lib/enc/cryb_base32.c rename to lib/enc/cryb_base32_decode.c index c269eee..c7cc061 100644 --- a/lib/enc/cryb_base32.c +++ b/lib/enc/cryb_base32_decode.c @@ -36,9 +36,6 @@ #include -static const char b32enc[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; - static const char b32dec[256] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, @@ -74,64 +71,6 @@ static const char b32dec[256] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, }; -/* - * Encode data in RFC 4648 base 32 representation. The target buffer must - * have room for base32_enclen(len) characters and a terminating NUL. - */ -int -base32_encode(const uint8_t *in, size_t ilen, char *out, size_t *olen) -{ - uint64_t bits; - - if (*olen <= base32_enclen(ilen)) { - *olen = base32_enclen(ilen) + 1; - errno = ENOSPC; - return (-1); - } - *olen = base32_enclen(ilen) + 1; - while (ilen >= 5) { - bits = 0; - bits |= (uint64_t)*in++ << 32; - bits |= (uint64_t)*in++ << 24; - bits |= (uint64_t)*in++ << 16; - bits |= (uint64_t)*in++ << 8; - bits |= (uint64_t)*in++; - *out++ = b32enc[bits >> 35 & 0x1f]; - *out++ = b32enc[bits >> 30 & 0x1f]; - *out++ = b32enc[bits >> 25 & 0x1f]; - *out++ = b32enc[bits >> 20 & 0x1f]; - *out++ = b32enc[bits >> 15 & 0x1f]; - *out++ = b32enc[bits >> 10 & 0x1f]; - *out++ = b32enc[bits >> 5 & 0x1f]; - *out++ = b32enc[bits & 0x1f]; - ilen -= 5; - } - if (ilen > 0) { - bits = 0; - switch (ilen) { - case 4: - bits |= (uint64_t)in[3] << 8; - case 3: - bits |= (uint64_t)in[2] << 16; - case 2: - bits |= (uint64_t)in[1] << 24; - case 1: - bits |= (uint64_t)in[0] << 32; - CRYB_NO_DEFAULT_CASE; - } - *out++ = b32enc[bits >> 35 & 0x1f]; - *out++ = b32enc[bits >> 30 & 0x1f]; - *out++ = ilen > 1 ? b32enc[bits >> 25 & 0x1f] : '='; - *out++ = ilen > 1 ? b32enc[bits >> 20 & 0x1f] : '='; - *out++ = ilen > 2 ? b32enc[bits >> 15 & 0x1f] : '='; - *out++ = ilen > 3 ? b32enc[bits >> 10 & 0x1f] : '='; - *out++ = ilen > 3 ? b32enc[bits >> 5 & 0x1f] : '='; - *out++ = '='; - } - *out++ = '\0'; - return (0); -} - /* * Decode data in RFC 4648 base 32 representation, stopping at the * terminating NUL, the first invalid (non-base32, non-whitespace) diff --git a/lib/enc/cryb_base32_encode.c b/lib/enc/cryb_base32_encode.c new file mode 100644 index 0000000..b77d05d --- /dev/null +++ b/lib/enc/cryb_base32_encode.c @@ -0,0 +1,98 @@ +/*- + * Copyright (c) 2013-2014 The University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include + +#include +#include + +#include + +static const char b32enc[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; + +/* + * Encode data in RFC 4648 base 32 representation. The target buffer must + * have room for base32_enclen(len) characters and a terminating NUL. + */ +int +base32_encode(const uint8_t *in, size_t ilen, char *out, size_t *olen) +{ + uint64_t bits; + + if (*olen <= base32_enclen(ilen)) { + *olen = base32_enclen(ilen) + 1; + errno = ENOSPC; + return (-1); + } + *olen = base32_enclen(ilen) + 1; + while (ilen >= 5) { + bits = 0; + bits |= (uint64_t)*in++ << 32; + bits |= (uint64_t)*in++ << 24; + bits |= (uint64_t)*in++ << 16; + bits |= (uint64_t)*in++ << 8; + bits |= (uint64_t)*in++; + *out++ = b32enc[bits >> 35 & 0x1f]; + *out++ = b32enc[bits >> 30 & 0x1f]; + *out++ = b32enc[bits >> 25 & 0x1f]; + *out++ = b32enc[bits >> 20 & 0x1f]; + *out++ = b32enc[bits >> 15 & 0x1f]; + *out++ = b32enc[bits >> 10 & 0x1f]; + *out++ = b32enc[bits >> 5 & 0x1f]; + *out++ = b32enc[bits & 0x1f]; + ilen -= 5; + } + if (ilen > 0) { + bits = 0; + switch (ilen) { + case 4: + bits |= (uint64_t)in[3] << 8; + case 3: + bits |= (uint64_t)in[2] << 16; + case 2: + bits |= (uint64_t)in[1] << 24; + case 1: + bits |= (uint64_t)in[0] << 32; + CRYB_NO_DEFAULT_CASE; + } + *out++ = b32enc[bits >> 35 & 0x1f]; + *out++ = b32enc[bits >> 30 & 0x1f]; + *out++ = ilen > 1 ? b32enc[bits >> 25 & 0x1f] : '='; + *out++ = ilen > 1 ? b32enc[bits >> 20 & 0x1f] : '='; + *out++ = ilen > 2 ? b32enc[bits >> 15 & 0x1f] : '='; + *out++ = ilen > 3 ? b32enc[bits >> 10 & 0x1f] : '='; + *out++ = ilen > 3 ? b32enc[bits >> 5 & 0x1f] : '='; + *out++ = '='; + } + *out++ = '\0'; + return (0); +} diff --git a/lib/enc/cryb_base64.c b/lib/enc/cryb_base64_decode.c similarity index 81% rename from lib/enc/cryb_base64.c rename to lib/enc/cryb_base64_decode.c index b1c63aa..d4a6b38 100644 --- a/lib/enc/cryb_base64.c +++ b/lib/enc/cryb_base64_decode.c @@ -36,11 +36,6 @@ #include -static const char b64enc[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789+/"; - static const char b64dec[256] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, @@ -76,50 +71,6 @@ static const char b64dec[256] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, }; -/* - * Encode data in RFC 4648 base 64 representation. The target buffer must - * have room for base64_enclen(len) characters and a terminating NUL. - */ -int -base64_encode(const uint8_t *in, size_t ilen, char *out, size_t *olen) -{ - uint32_t bits; - - if (*olen <= base64_enclen(ilen)) { - *olen = base64_enclen(ilen) + 1; - errno = ENOSPC; - return (-1); - } - *olen = base64_enclen(ilen) + 1; - while (ilen >= 3) { - bits = 0; - bits |= (uint32_t)*in++ << 16; - bits |= (uint32_t)*in++ << 8; - bits |= (uint32_t)*in++; - ilen -= 3; - *out++ = b64enc[bits >> 18 & 0x3f]; - *out++ = b64enc[bits >> 12 & 0x3f]; - *out++ = b64enc[bits >> 6 & 0x3f]; - *out++ = b64enc[bits & 0x3f]; - } - if (ilen > 0) { - bits = 0; - switch (ilen) { - case 2: - bits |= (uint32_t)in[1] << 8; - case 1: - bits |= (uint32_t)in[0] << 16; - CRYB_NO_DEFAULT_CASE; - } - *out++ = b64enc[bits >> 18 & 0x3f]; - *out++ = b64enc[bits >> 12 & 0x3f]; - *out++ = ilen > 1 ? b64enc[bits >> 6 & 0x3f] : '='; - *out++ = '='; - } - *out++ = '\0'; - return (0); -} - /* * Decode data in RFC 4648 base 64 representation, stopping at the * terminating NUL, the first invalid (non-base64, non-whitespace) diff --git a/lib/enc/cryb_base64_encode.c b/lib/enc/cryb_base64_encode.c new file mode 100644 index 0000000..ea03306 --- /dev/null +++ b/lib/enc/cryb_base64_encode.c @@ -0,0 +1,86 @@ +/*- + * Copyright (c) 2013-2014 The University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include + +#include +#include + +#include + +static const char b64enc[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; + +/* + * Encode data in RFC 4648 base 64 representation. The target buffer must + * have room for base64_enclen(len) characters and a terminating NUL. + */ +int +base64_encode(const uint8_t *in, size_t ilen, char *out, size_t *olen) +{ + uint32_t bits; + + if (*olen <= base64_enclen(ilen)) { + *olen = base64_enclen(ilen) + 1; + errno = ENOSPC; + return (-1); + } + *olen = base64_enclen(ilen) + 1; + while (ilen >= 3) { + bits = 0; + bits |= (uint32_t)*in++ << 16; + bits |= (uint32_t)*in++ << 8; + bits |= (uint32_t)*in++; + ilen -= 3; + *out++ = b64enc[bits >> 18 & 0x3f]; + *out++ = b64enc[bits >> 12 & 0x3f]; + *out++ = b64enc[bits >> 6 & 0x3f]; + *out++ = b64enc[bits & 0x3f]; + } + if (ilen > 0) { + bits = 0; + switch (ilen) { + case 2: + bits |= (uint32_t)in[1] << 8; + case 1: + bits |= (uint32_t)in[0] << 16; + CRYB_NO_DEFAULT_CASE; + } + *out++ = b64enc[bits >> 18 & 0x3f]; + *out++ = b64enc[bits >> 12 & 0x3f]; + *out++ = ilen > 1 ? b64enc[bits >> 6 & 0x3f] : '='; + *out++ = '='; + } + *out++ = '\0'; + return (0); +} diff --git a/include/cryb/hotp.h b/lib/enc/cryb_enc.c similarity index 84% rename from include/cryb/hotp.h rename to lib/enc/cryb_enc.c index a1ef045..2aa8c43 100644 --- a/include/cryb/hotp.h +++ b/lib/enc/cryb_enc.c @@ -1,6 +1,5 @@ /*- - * Copyright (c) 2012 The University of Oslo - * Copyright (c) 2012 Dag-Erling Smørgrav + * Copyright (c) 2015 Dag-Erling Smørgrav * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -28,7 +27,18 @@ * SUCH DAMAGE. */ -#ifndef CRYB_HOTP_H_INCLUDED -#define CRYB_HOTP_H_INCLUDED +#include "cryb/impl.h" -#endif +#include +#include + +#include + +static const char *cryb_enc_version_string = PACKAGE_VERSION; + +const char * +cryb_enc_version(void) +{ + + return (cryb_enc_version_string); +} diff --git a/lib/enc/cryb_percent.c b/lib/enc/cryb_percent_decode.c similarity index 77% rename from lib/enc/cryb_percent.c rename to lib/enc/cryb_percent_decode.c index 6dd2a01..b19010b 100644 --- a/lib/enc/cryb_percent.c +++ b/lib/enc/cryb_percent_decode.c @@ -36,46 +36,11 @@ #include #include -static const char hex[] = "0123456789ABCDEF"; - #define unhex(ch) \ ((ch >= '0' && ch <= '9') ? ch - '0' : \ (ch >= 'A' && ch <= 'F') ? 0xa + ch - 'A' : \ (ch >= 'a' && ch <= 'f') ? 0xa + ch - 'a' : 0) -/* - * Encodes a string in RFC 3986 percent-encoded representation. - */ -int -percent_encode(const char *in, size_t ilen, char *out, size_t *olen) -{ - size_t len; - - for (len = 0; ilen > 0 && *in != '\0'; --ilen, ++in) { - if (is_uri(*in)) { - if (++len < *olen && out != NULL) - *out++ = *in; - } else { - if (++len < *olen && out != NULL) - *out++ = '%'; - if (++len < *olen && out != NULL) - *out++ = hex[(uint8_t)*in >> 4]; - if (++len < *olen && out != NULL) - *out++ = hex[(uint8_t)*in & 0xf]; - } - } - if (*olen > 0 && out != NULL) - *out = '\0'; - if (++len > *olen && out != NULL) { - /* overflow */ - *olen = len; - errno = ENOSPC; - return (-1); - } - *olen = len; - return (0); -} - /* * Decodes a string in RFC 3986 percent-encoded representation. */ diff --git a/lib/enc/cryb_percent_encode.c b/lib/enc/cryb_percent_encode.c new file mode 100644 index 0000000..80b694d --- /dev/null +++ b/lib/enc/cryb_percent_encode.c @@ -0,0 +1,72 @@ +/*- + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include +#include + +#include +#include + +static const char hex[] = "0123456789ABCDEF"; + +/* + * Encodes a string in RFC 3986 percent-encoded representation. + */ +int +percent_encode(const char *in, size_t ilen, char *out, size_t *olen) +{ + size_t len; + + for (len = 0; ilen > 0 && *in != '\0'; --ilen, ++in) { + if (is_uri(*in)) { + if (++len < *olen && out != NULL) + *out++ = *in; + } else { + if (++len < *olen && out != NULL) + *out++ = '%'; + if (++len < *olen && out != NULL) + *out++ = hex[(uint8_t)*in >> 4]; + if (++len < *olen && out != NULL) + *out++ = hex[(uint8_t)*in & 0xf]; + } + } + if (*olen > 0 && out != NULL) + *out = '\0'; + if (++len > *olen && out != NULL) { + /* overflow */ + *olen = len; + errno = ENOSPC; + return (-1); + } + *olen = len; + return (0); +} diff --git a/lib/hash/Makefile.am b/lib/hash/Makefile.am index 01e0e4e..e2779bc 100644 --- a/lib/hash/Makefile.am +++ b/lib/hash/Makefile.am @@ -3,12 +3,14 @@ AM_CPPFLAGS = -I$(top_srcdir)/include lib_LTLIBRARIES = libcryb-hash.la libcryb_hash_la_SOURCES = \ - adler32.c \ - fletcher16.c \ - fletcher32.c \ - fletcher64.c \ - murmur3_32.c \ - pearson.c + cryb_adler32.c \ + cryb_fletcher16.c \ + cryb_fletcher32.c \ + cryb_fletcher64.c \ + cryb_murmur3_32.c \ + cryb_pearson.c \ + \ + cryb_hash.c dist_man3_MANS = \ cryb_fletcher.3 \ diff --git a/lib/hash/adler32.c b/lib/hash/cryb_adler32.c similarity index 100% rename from lib/hash/adler32.c rename to lib/hash/cryb_adler32.c diff --git a/lib/hash/fletcher16.c b/lib/hash/cryb_fletcher16.c similarity index 100% rename from lib/hash/fletcher16.c rename to lib/hash/cryb_fletcher16.c diff --git a/lib/hash/fletcher32.c b/lib/hash/cryb_fletcher32.c similarity index 100% rename from lib/hash/fletcher32.c rename to lib/hash/cryb_fletcher32.c diff --git a/lib/hash/fletcher64.c b/lib/hash/cryb_fletcher64.c similarity index 100% rename from lib/hash/fletcher64.c rename to lib/hash/cryb_fletcher64.c diff --git a/lib/hash/cryb_hash.c b/lib/hash/cryb_hash.c new file mode 100644 index 0000000..ba19502 --- /dev/null +++ b/lib/hash/cryb_hash.c @@ -0,0 +1,44 @@ +/*- + * Copyright (c) 2015 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include + +#include + +static const char *cryb_hash_version_string = PACKAGE_VERSION; + +const char * +cryb_hash_version(void) +{ + + return (cryb_hash_version_string); +} diff --git a/lib/hash/murmur3_32.c b/lib/hash/cryb_murmur3_32.c similarity index 100% rename from lib/hash/murmur3_32.c rename to lib/hash/cryb_murmur3_32.c diff --git a/lib/hash/pearson.c b/lib/hash/cryb_pearson.c similarity index 100% rename from lib/hash/pearson.c rename to lib/hash/cryb_pearson.c diff --git a/lib/mac/Makefile.am b/lib/mac/Makefile.am index 361a679..e7e4d11 100644 --- a/lib/mac/Makefile.am +++ b/lib/mac/Makefile.am @@ -3,11 +3,13 @@ AM_CPPFLAGS = -I$(top_srcdir)/include lib_LTLIBRARIES = libcryb-mac.la libcryb_mac_la_SOURCES = \ - hmac_sha1.c \ - hmac_sha224.c \ - hmac_sha256.c \ - hmac_sha384.c \ - hmac_sha512.c + cryb_hmac_sha1.c \ + cryb_hmac_sha224.c \ + cryb_hmac_sha256.c \ + cryb_hmac_sha384.c \ + cryb_hmac_sha512.c \ + \ + cryb_mac.c libcryb_mac_la_LIBADD = \ $(top_builddir)/lib/digest/libcryb-digest.la diff --git a/lib/mac/hmac_sha1.c b/lib/mac/cryb_hmac_sha1.c similarity index 100% rename from lib/mac/hmac_sha1.c rename to lib/mac/cryb_hmac_sha1.c diff --git a/lib/mac/hmac_sha224.c b/lib/mac/cryb_hmac_sha224.c similarity index 100% rename from lib/mac/hmac_sha224.c rename to lib/mac/cryb_hmac_sha224.c diff --git a/lib/mac/hmac_sha256.c b/lib/mac/cryb_hmac_sha256.c similarity index 100% rename from lib/mac/hmac_sha256.c rename to lib/mac/cryb_hmac_sha256.c diff --git a/lib/mac/hmac_sha384.c b/lib/mac/cryb_hmac_sha384.c similarity index 100% rename from lib/mac/hmac_sha384.c rename to lib/mac/cryb_hmac_sha384.c diff --git a/lib/mac/hmac_sha512.c b/lib/mac/cryb_hmac_sha512.c similarity index 100% rename from lib/mac/hmac_sha512.c rename to lib/mac/cryb_hmac_sha512.c diff --git a/include/cryb/totp.h b/lib/mac/cryb_mac.c similarity index 84% rename from include/cryb/totp.h rename to lib/mac/cryb_mac.c index f27542e..0f24b39 100644 --- a/include/cryb/totp.h +++ b/lib/mac/cryb_mac.c @@ -1,6 +1,5 @@ /*- - * Copyright (c) 2012 The University of Oslo - * Copyright (c) 2012 Dag-Erling Smørgrav + * Copyright (c) 2015 Dag-Erling Smørgrav * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -28,7 +27,18 @@ * SUCH DAMAGE. */ -#ifndef CRYB_TOTP_H_INCLUDED -#define CRYB_TOTP_H_INCLUDED +#include "cryb/impl.h" -#endif +#include +#include + +#include + +static const char *cryb_mac_version_string = PACKAGE_VERSION; + +const char * +cryb_mac_version(void) +{ + + return (cryb_mac_version_string); +} diff --git a/lib/mpi/Makefile.am b/lib/mpi/Makefile.am index cf732bb..b10eb82 100644 --- a/lib/mpi/Makefile.am +++ b/lib/mpi/Makefile.am @@ -3,8 +3,27 @@ AM_CPPFLAGS = -I$(top_srcdir)/include lib_LTLIBRARIES = libcryb-mpi.la libcryb_mpi_la_SOURCES = \ - mpi.c \ - mpi_add.c \ - mpi_sub.c \ - mpi_shift.c \ - mpi_cmp.c + cryb_mpi_add.c \ + cryb_mpi_add_abs.c \ + cryb_mpi_cmp.c \ + cryb_mpi_cmp_abs.c \ + cryb_mpi_copy.c \ + cryb_mpi_destroy.c \ + cryb_mpi_eq.c \ + cryb_mpi_eq_abs.c \ + cryb_mpi_grow.c \ + cryb_mpi_init.c \ + cryb_mpi_load.c \ + cryb_mpi_lshift.c \ + cryb_mpi_negate.c \ + cryb_mpi_rshift.c \ + cryb_mpi_set.c \ + cryb_mpi_sub.c \ + cryb_mpi_sub_abs.c \ + cryb_mpi_swap.c \ + cryb_mpi_zero.c \ + \ + cryb_mpi.c + +noinst_HEADERS = \ + cryb_mpi_impl.h diff --git a/lib/mpi/cryb_mpi.c b/lib/mpi/cryb_mpi.c new file mode 100644 index 0000000..65b5a28 --- /dev/null +++ b/lib/mpi/cryb_mpi.c @@ -0,0 +1,44 @@ +/*- + * Copyright (c) 2015 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include + +#include + +static const char *cryb_mpi_version_string = PACKAGE_VERSION; + +const char * +cryb_mpi_version(void) +{ + + return (cryb_mpi_version_string); +} diff --git a/lib/mpi/cryb_mpi_add.c b/lib/mpi/cryb_mpi_add.c new file mode 100644 index 0000000..c6f5d09 --- /dev/null +++ b/lib/mpi/cryb_mpi_add.c @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include + +#include + +#include "cryb_mpi_impl.h" + +/* + * Add two numbers together. + */ +int +mpi_add(cryb_mpi *X, cryb_mpi *A, cryb_mpi *B) +{ + + if (A->neg && B->neg) { + if (mpi_add_abs(X, A, B) < 0) + return (-1); + X->neg = 1; + } else if (A->neg) { + if (mpi_sub_abs(X, A, B) < 0) + return (-1); + X->neg = (mpi_cmp_abs(A, B) < 0); + } else if (B->neg) { + if (mpi_sub_abs(X, A, B) < 0) + return (-1); + X->neg = (mpi_cmp_abs(A, B) > 0); + } else { + if (mpi_add_abs(X, A, B) < 0) + return (-1); + X->neg = 0; + } + return (0); +} + diff --git a/lib/mpi/mpi_add.c b/lib/mpi/cryb_mpi_add_abs.c similarity index 86% rename from lib/mpi/mpi_add.c rename to lib/mpi/cryb_mpi_add_abs.c index 7a0579e..74d199f 100644 --- a/lib/mpi/mpi_add.c +++ b/lib/mpi/cryb_mpi_add_abs.c @@ -34,6 +34,8 @@ #include +#include "cryb_mpi_impl.h" + /* * Store the sum of the absolutes values of A and B in X. */ @@ -97,31 +99,3 @@ mpi_add_abs(cryb_mpi *X, cryb_mpi *A, cryb_mpi *B) X->msb += i * 32 + 1; return (0); } - -/* - * Add two numbers together. - */ -int -mpi_add(cryb_mpi *X, cryb_mpi *A, cryb_mpi *B) -{ - - if (A->neg && B->neg) { - if (mpi_add_abs(X, A, B) < 0) - return (-1); - X->neg = 1; - } else if (A->neg) { - if (mpi_sub_abs(X, A, B) < 0) - return (-1); - X->neg = (mpi_cmp_abs(A, B) < 0); - } else if (B->neg) { - if (mpi_sub_abs(X, A, B) < 0) - return (-1); - X->neg = (mpi_cmp_abs(A, B) > 0); - } else { - if (mpi_add_abs(X, A, B) < 0) - return (-1); - X->neg = 0; - } - return (0); -} - diff --git a/lib/mpi/cryb_mpi_cmp.c b/lib/mpi/cryb_mpi_cmp.c new file mode 100644 index 0000000..72edcee --- /dev/null +++ b/lib/mpi/cryb_mpi_cmp.c @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include +#include + +#include + +#include "cryb_mpi_impl.h" + +/* + * Compare signed values + */ +int +mpi_cmp(cryb_mpi *X, cryb_mpi *Y) +{ + + if (X->neg) { + if (Y->neg) + return (-mpi_cmp_abs(X, Y)); + else + return (-1); + } else { + if (Y->neg) + return (1); + else + return (mpi_cmp_abs(X, Y)); + } +} diff --git a/lib/mpi/mpi_cmp.c b/lib/mpi/cryb_mpi_cmp_abs.c similarity index 74% rename from lib/mpi/mpi_cmp.c rename to lib/mpi/cryb_mpi_cmp_abs.c index f8f7d59..7531160 100644 --- a/lib/mpi/mpi_cmp.c +++ b/lib/mpi/cryb_mpi_cmp_abs.c @@ -35,8 +35,7 @@ #include -/* n rounded up to nearest multiple of p */ -#define RUP(n, p) ((((n) + (p) - 1) / (p)) * (p)) +#include "cryb_mpi_impl.h" /* * Compare absolute values @@ -60,45 +59,3 @@ mpi_cmp_abs(cryb_mpi *X, cryb_mpi *Y) } return (0); } - -/* - * Compare signed values - */ -int -mpi_cmp(cryb_mpi *X, cryb_mpi *Y) -{ - - if (X->neg) { - if (Y->neg) - return (-mpi_cmp_abs(X, Y)); - else - return (-1); - } else { - if (Y->neg) - return (1); - else - return (mpi_cmp_abs(X, Y)); - } -} - -/* - * Compare for equality - */ -int -mpi_eq_abs(cryb_mpi *A, cryb_mpi *B) -{ - - return (A->msb == B->msb && - memcmp(A->words, B->words, (A->msb + 31) / 32) == 0); -} - -/* - * Compare for equality - */ -int -mpi_eq(cryb_mpi *A, cryb_mpi *B) -{ - - return (A->neg == B->neg && A->msb == B->msb && - memcmp(A->words, B->words, (A->msb + 31) / 32) == 0); -} diff --git a/lib/mpi/cryb_mpi_copy.c b/lib/mpi/cryb_mpi_copy.c new file mode 100644 index 0000000..a521f0d --- /dev/null +++ b/lib/mpi/cryb_mpi_copy.c @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include +#include +#include + +#include +#include + +#include "cryb_mpi_impl.h" + +/* + * Copy the contents of Y into X + */ +int +mpi_copy(cryb_mpi *X, const cryb_mpi *Y) +{ + + if (X == Y) + return (0); + if (Y->msb == 0) { + mpi_zero(X); + return (0); + } + if (mpi_grow(X, Y->msb) != 0) + return (-1); + mpi_zero(X); + X->msb = Y->msb; + X->neg = Y->neg; + memcpy(X->words, Y->words, X->size * sizeof *X->words); + return (0); +} diff --git a/lib/mpi/cryb_mpi_destroy.c b/lib/mpi/cryb_mpi_destroy.c new file mode 100644 index 0000000..fd876b8 --- /dev/null +++ b/lib/mpi/cryb_mpi_destroy.c @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include +#include +#include + +#include +#include + +#include "cryb_mpi_impl.h" + +/* + * Destroy an mpi. + */ +void +mpi_destroy(cryb_mpi *X) +{ + + if (X->words != NULL && X->words != X->swords) { + memset(X->words, 0, X->size); + free(X->words); + } + mpi_init(X); +} diff --git a/lib/mpi/cryb_mpi_eq.c b/lib/mpi/cryb_mpi_eq.c new file mode 100644 index 0000000..72aab95 --- /dev/null +++ b/lib/mpi/cryb_mpi_eq.c @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include +#include + +#include + +#include "cryb_mpi_impl.h" + +/* + * Compare signed values for equality + */ +int +mpi_eq(cryb_mpi *A, cryb_mpi *B) +{ + + return (A->neg == B->neg && A->msb == B->msb && + memcmp(A->words, B->words, (A->msb + 31) / 32) == 0); +} diff --git a/lib/mpi/cryb_mpi_eq_abs.c b/lib/mpi/cryb_mpi_eq_abs.c new file mode 100644 index 0000000..32c6c77 --- /dev/null +++ b/lib/mpi/cryb_mpi_eq_abs.c @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include +#include + +#include + +#include "cryb_mpi_impl.h" + +/* + * Compare absolute values for equality + */ +int +mpi_eq_abs(cryb_mpi *A, cryb_mpi *B) +{ + + return (A->msb == B->msb && + memcmp(A->words, B->words, (A->msb + 31) / 32) == 0); +} diff --git a/lib/mpi/cryb_mpi_grow.c b/lib/mpi/cryb_mpi_grow.c new file mode 100644 index 0000000..afdc3d7 --- /dev/null +++ b/lib/mpi/cryb_mpi_grow.c @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include +#include +#include + +#include +#include + +#include "cryb_mpi_impl.h" + +/* + * Grow the underlying storage to fit at least n bits + */ +int +mpi_grow(cryb_mpi *X, unsigned int n) +{ + uint32_t *words; + size_t size; + + if (X->words == NULL) + MPI_FAST_INIT(X); + size = (n + 31) / 32; + if (X->size >= size) + return (0); + size = RUP(size, CRYB_MPI_SWORDS); + if ((words = calloc(1, size * sizeof *words)) == NULL) + return (-1); + memcpy(words, X->words, X->size * sizeof *words); + memset(X->words, 0, X->size * sizeof *words); + if (X->words != X->swords) + free(X->words); + X->words = words; + X->size = size; + return (0); +} diff --git a/lib/mpi/cryb_mpi_impl.h b/lib/mpi/cryb_mpi_impl.h new file mode 100644 index 0000000..9dd840c --- /dev/null +++ b/lib/mpi/cryb_mpi_impl.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* n rounded up to nearest multiple of p */ +#define RUP(n, p) ((((n) + (p) - 1) / (p)) * (p)) + +/* + * Initialize an all-zeroes mpi. + */ +#define MPI_FAST_INIT(X) \ + do { \ + (X)->words = (X)->swords; \ + (X)->size = CRYB_MPI_SWORDS; \ + } while (0) diff --git a/lib/mpi/cryb_mpi_init.c b/lib/mpi/cryb_mpi_init.c new file mode 100644 index 0000000..e0318f9 --- /dev/null +++ b/lib/mpi/cryb_mpi_init.c @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include +#include +#include + +#include +#include + +#include "cryb_mpi_impl.h" + +/* + * Initialize an mpi. + */ +void +mpi_init(cryb_mpi *X) +{ + + memset(X, 0, sizeof *X); + MPI_FAST_INIT(X); +} diff --git a/lib/mpi/cryb_mpi_load.c b/lib/mpi/cryb_mpi_load.c new file mode 100644 index 0000000..652cca4 --- /dev/null +++ b/lib/mpi/cryb_mpi_load.c @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include +#include +#include + +#include +#include + +#include "cryb_mpi_impl.h" + +/* + * Load value from an array of bytes + */ +int +mpi_load(cryb_mpi *X, const uint8_t *a, size_t len) +{ + int i; + + mpi_zero(X); + /* skip zeroes */ + while (len > 0 && *a == 0) + --len, ++a; + if (len == 0) + return (0); + /* make room */ + if (mpi_grow(X, len * 8) != 0) + return (-1); + /* load whole words */ + for (i = 0; len >= 4; ++i, len -= 4) + X->words[i] = be32dec(&a[len - 4]); + /* load remaining bytes */ + switch (len) { + case 3: + X->words[i] |= a[len - 3] << 16; + case 2: + X->words[i] |= a[len - 2] << 8; + case 1: + X->words[i] |= a[len - 1]; + break; + case 0: + --i; + CRYB_NO_DEFAULT_CASE; + } + /* i now points to the msw */ + /* compute msb of msw */ + /* XXX use flsl() */ + for (X->msb = 31; X->msb > 0; --X->msb) + if (X->words[i] & (1 << X->msb)) + break; + /* add msw offset */ + X->msb += i * 32 + 1; + return (0); +} diff --git a/lib/mpi/cryb_mpi_lshift.c b/lib/mpi/cryb_mpi_lshift.c new file mode 100644 index 0000000..1b20b7f --- /dev/null +++ b/lib/mpi/cryb_mpi_lshift.c @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include + +#include + +#include "cryb_mpi_impl.h" + +/* + * Left-shift X by c bytes + */ +int +mpi_lshift(cryb_mpi *X, unsigned int c) +{ + unsigned int cl, i; + + /* operands are zero */ + if (c == 0 || X->msb == 0) + return (0); + /* make room */ + if (mpi_grow(X, X->msb + c) != 0) + return (-1); + /* preemptively adjust msb */ + X->msb += c; + /* try to move whole words first */ + if (c >= 32) { + cl = c / 32; + c = c % 32; + memmove(X->words + cl, X->words, + (X->size - cl) * sizeof *X->words); + memset(X->words, 0, cl * sizeof *X->words); + } + /* done? */ + if (c == 0) + return (0); + /* the rest has to be done the hard way */ + for (i = X->msb / 32; i > 0; --i) { + X->words[i] <<= c; + X->words[i] |= X->words[i - 1] >> (32 - c); + } + X->words[0] <<= c; + /* done! */ + return (0); +} diff --git a/lib/mpi/cryb_mpi_negate.c b/lib/mpi/cryb_mpi_negate.c new file mode 100644 index 0000000..5e88480 --- /dev/null +++ b/lib/mpi/cryb_mpi_negate.c @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include +#include +#include + +#include +#include + +#include "cryb_mpi_impl.h" + +/* + * Flip sign + */ +void +mpi_negate(cryb_mpi *X) +{ + + if (X->msb > 0) + X->neg = !X->neg; +} diff --git a/lib/mpi/mpi_shift.c b/lib/mpi/cryb_mpi_rshift.c similarity index 75% rename from lib/mpi/mpi_shift.c rename to lib/mpi/cryb_mpi_rshift.c index 789d0dd..00c0817 100644 --- a/lib/mpi/mpi_shift.c +++ b/lib/mpi/cryb_mpi_rshift.c @@ -34,45 +34,10 @@ #include -/* - * Left-shift X by c bytes - */ -int -mpi_lshift(cryb_mpi *X, unsigned int c) -{ - unsigned int cl, i; - - /* operands are zero */ - if (c == 0 || X->msb == 0) - return (0); - /* make room */ - if (mpi_grow(X, X->msb + c) != 0) - return (-1); - /* preemptively adjust msb */ - X->msb += c; - /* try to move whole words first */ - if (c >= 32) { - cl = c / 32; - c = c % 32; - memmove(X->words + cl, X->words, - (X->size - cl) * sizeof *X->words); - memset(X->words, 0, cl * sizeof *X->words); - } - /* done? */ - if (c == 0) - return (0); - /* the rest has to be done the hard way */ - for (i = X->msb / 32; i > 0; --i) { - X->words[i] <<= c; - X->words[i] |= X->words[i - 1] >> (32 - c); - } - X->words[0] <<= c; - /* done! */ - return (0); -} +#include "cryb_mpi_impl.h" /* - * Right-shift: X >>= c + * Right-shift X by c bytes */ int mpi_rshift(cryb_mpi *X, unsigned int c) diff --git a/lib/mpi/cryb_mpi_set.c b/lib/mpi/cryb_mpi_set.c new file mode 100644 index 0000000..ada3bcf --- /dev/null +++ b/lib/mpi/cryb_mpi_set.c @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include +#include +#include + +#include +#include + +#include "cryb_mpi_impl.h" + +/* + * Set value of X from integer z + */ +int +mpi_set(cryb_mpi *X, int32_t z) +{ + uint32_t zabs; + + mpi_zero(X); + if (z < 0) { + X->neg = 1; + zabs = -z; + } else { + zabs = z; + } + X->words[0] = zabs; + /* XXX use flsl() */ + while (zabs > 0) { + X->msb++; + zabs >>= 1; + } + return (0); +} diff --git a/lib/mpi/cryb_mpi_sub.c b/lib/mpi/cryb_mpi_sub.c new file mode 100644 index 0000000..85fdc87 --- /dev/null +++ b/lib/mpi/cryb_mpi_sub.c @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include + +#include + +#include "cryb_mpi_impl.h" + +/* + * Subtract one number from another. + */ +int +mpi_sub(cryb_mpi *X, cryb_mpi *A, cryb_mpi *B) +{ + + if (A->neg && B->neg) { + if (mpi_sub_abs(X, A, B) < 0) + return (-1); + X->neg = (mpi_cmp_abs(A, B) > 0); + } else if (A->neg) { + if (mpi_add_abs(X, A, B) < 0) + return (-1); + X->neg = 1; + } else if (B->neg) { + if (mpi_add_abs(X, A, B) < 0) + return (-1); + X->neg = 0; + } else { + if (mpi_sub_abs(X, A, B) < 0) + return (-1); + X->neg = (mpi_cmp_abs(A, B) < 0); + } + return (0); +} diff --git a/lib/mpi/mpi_sub.c b/lib/mpi/cryb_mpi_sub_abs.c similarity index 85% rename from lib/mpi/mpi_sub.c rename to lib/mpi/cryb_mpi_sub_abs.c index 41b889f..867e4b9 100644 --- a/lib/mpi/mpi_sub.c +++ b/lib/mpi/cryb_mpi_sub_abs.c @@ -34,6 +34,8 @@ #include +#include "cryb_mpi_impl.h" + /* * Store the difference between the absolute values of A and B in X. */ @@ -92,30 +94,3 @@ mpi_sub_abs(cryb_mpi *X, cryb_mpi *A, cryb_mpi *B) X->msb += i * 32 + 1; return (0); } - -/* - * Subtract one number from another. - */ -int -mpi_sub(cryb_mpi *X, cryb_mpi *A, cryb_mpi *B) -{ - - if (A->neg && B->neg) { - if (mpi_sub_abs(X, A, B) < 0) - return (-1); - X->neg = (mpi_cmp_abs(A, B) > 0); - } else if (A->neg) { - if (mpi_add_abs(X, A, B) < 0) - return (-1); - X->neg = 1; - } else if (B->neg) { - if (mpi_add_abs(X, A, B) < 0) - return (-1); - X->neg = 0; - } else { - if (mpi_sub_abs(X, A, B) < 0) - return (-1); - X->neg = (mpi_cmp_abs(A, B) < 0); - } - return (0); -} diff --git a/lib/mpi/cryb_mpi_swap.c b/lib/mpi/cryb_mpi_swap.c new file mode 100644 index 0000000..2d740a3 --- /dev/null +++ b/lib/mpi/cryb_mpi_swap.c @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include +#include +#include + +#include +#include + +#include "cryb_mpi_impl.h" + +/* + * Swap the contents of X and Y + */ +void +mpi_swap(cryb_mpi *X, cryb_mpi *Y) +{ + cryb_mpi T; + + memcpy(&T, X, sizeof T); + memcpy(X, Y, sizeof *X); + memcpy(Y, &T, sizeof *Y); + if (X->words == Y->swords) + X->words = X->swords; + if (Y->words == X->swords) + Y->words = Y->swords; +} diff --git a/lib/mpi/cryb_mpi_zero.c b/lib/mpi/cryb_mpi_zero.c new file mode 100644 index 0000000..f2b09a7 --- /dev/null +++ b/lib/mpi/cryb_mpi_zero.c @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include +#include +#include + +#include +#include + +#include "cryb_mpi_impl.h" + +/* + * Set X to zero + */ +void +mpi_zero(cryb_mpi *X) +{ + + if (X->words == NULL) { + MPI_FAST_INIT(X); + } else if (X->msb != 0) { + memset(X->words, 0, X->size); + X->msb = 0; + } + X->neg = 0; +} diff --git a/lib/mpi/mpi.c b/lib/mpi/mpi.c deleted file mode 100644 index 4152e81..0000000 --- a/lib/mpi/mpi.c +++ /dev/null @@ -1,235 +0,0 @@ -/* - * Copyright (c) 2014 Dag-Erling Smørgrav - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include "cryb/impl.h" - -#include -#include -#include -#include - -#include -#include - -/* n rounded up to nearest multiple of p */ -#define RUP(n, p) ((((n) + (p) - 1) / (p)) * (p)) - -/* - * Initialize an all-zeroes mpi. - */ -#define MPI_FAST_INIT(X) \ - do { \ - (X)->words = (X)->swords; \ - (X)->size = CRYB_MPI_SWORDS; \ - } while (0) - -/* - * Initialize an mpi. - */ -void -mpi_init(cryb_mpi *X) -{ - - memset(X, 0, sizeof *X); - MPI_FAST_INIT(X); -} - -/* - * Destroy an mpi. - */ -void -mpi_destroy(cryb_mpi *X) -{ - - if (X->words != NULL && X->words != X->swords) { - memset(X->words, 0, X->size); - free(X->words); - } - mpi_init(X); -} - -/* - * Grow the underlying storage to fit at least n bits - */ -int -mpi_grow(cryb_mpi *X, unsigned int n) -{ - uint32_t *words; - size_t size; - - if (X->words == NULL) - MPI_FAST_INIT(X); - size = (n + 31) / 32; - if (X->size >= size) - return (0); - size = RUP(size, CRYB_MPI_SWORDS); - if ((words = calloc(1, size * sizeof *words)) == NULL) - return (-1); - memcpy(words, X->words, X->size * sizeof *words); - memset(X->words, 0, X->size * sizeof *words); - if (X->words != X->swords) - free(X->words); - X->words = words; - X->size = size; - return (0); -} - -/* - * Set X to zero - */ -void -mpi_zero(cryb_mpi *X) -{ - - if (X->words == NULL) { - MPI_FAST_INIT(X); - } else if (X->msb != 0) { - memset(X->words, 0, X->size); - X->msb = 0; - } - X->neg = 0; -} - -/* - * Flip sign - */ -void -mpi_negate(cryb_mpi *X) -{ - - if (X->msb > 0) - X->neg = !X->neg; -} - -/* - * Copy the contents of Y into X - */ -int -mpi_copy(cryb_mpi *X, const cryb_mpi *Y) -{ - - if (X == Y) - return (0); - if (Y->msb == 0) { - mpi_zero(X); - return (0); - } - if (mpi_grow(X, Y->msb) != 0) - return (-1); - mpi_zero(X); - X->msb = Y->msb; - X->neg = Y->neg; - memcpy(X->words, Y->words, X->size * sizeof *X->words); - return (0); -} - -/* - * Swap the contents of X and Y - */ -void -mpi_swap(cryb_mpi *X, cryb_mpi *Y) -{ - cryb_mpi T; - - memcpy(&T, X, sizeof T); - memcpy(X, Y, sizeof *X); - memcpy(Y, &T, sizeof *Y); - if (X->words == Y->swords) - X->words = X->swords; - if (Y->words == X->swords) - Y->words = Y->swords; -} - -/* - * Load value from an array of bytes - */ -int -mpi_load(cryb_mpi *X, const uint8_t *a, size_t len) -{ - int i; - - mpi_zero(X); - /* skip zeroes */ - while (len > 0 && *a == 0) - --len, ++a; - if (len == 0) - return (0); - /* make room */ - if (mpi_grow(X, len * 8) != 0) - return (-1); - /* load whole words */ - for (i = 0; len >= 4; ++i, len -= 4) - X->words[i] = be32dec(&a[len - 4]); - /* load remaining bytes */ - switch (len) { - case 3: - X->words[i] |= a[len - 3] << 16; - case 2: - X->words[i] |= a[len - 2] << 8; - case 1: - X->words[i] |= a[len - 1]; - break; - case 0: - --i; - CRYB_NO_DEFAULT_CASE; - } - /* i now points to the msw */ - /* compute msb of msw */ - /* XXX use flsl() */ - for (X->msb = 31; X->msb > 0; --X->msb) - if (X->words[i] & (1 << X->msb)) - break; - /* add msw offset */ - X->msb += i * 32 + 1; - return (0); -} - -/* - * Set value of X from integer z - */ -int -mpi_set(cryb_mpi *X, int32_t z) -{ - uint32_t zabs; - - mpi_zero(X); - if (z < 0) { - X->neg = 1; - zabs = -z; - } else { - zabs = z; - } - X->words[0] = zabs; - /* XXX use flsl() */ - while (zabs > 0) { - X->msb++; - zabs >>= 1; - } - return (0); -} diff --git a/lib/oath/Makefile.am b/lib/oath/Makefile.am index f9a656c..fbd607c 100644 --- a/lib/oath/Makefile.am +++ b/lib/oath/Makefile.am @@ -3,14 +3,16 @@ AM_CPPFLAGS = -I$(top_srcdir)/include lib_LTLIBRARIES = libcryb-oath.la libcryb_oath_la_SOURCES = \ - oath_hotp.c \ - oath_totp.c \ - oath_key_alloc.c \ - oath_key_create.c \ - oath_key_dummy.c \ - oath_key_from_uri.c \ - oath_key_free.c \ - oath_key.c + cryb_oath_hotp.c \ + cryb_oath_totp.c \ + cryb_oath_key_alloc.c \ + cryb_oath_key_create.c \ + cryb_oath_key_dummy.c \ + cryb_oath_key_from_uri.c \ + cryb_oath_key_free.c \ + cryb_oath_key.c \ + \ + cryb_oath.c libcryb_oath_la_LIBADD = \ $(top_builddir)/lib/digest/libcryb-digest.la \ diff --git a/lib/oath/cryb_oath.c b/lib/oath/cryb_oath.c new file mode 100644 index 0000000..c81e552 --- /dev/null +++ b/lib/oath/cryb_oath.c @@ -0,0 +1,44 @@ +/*- + * Copyright (c) 2015 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include + +#include + +static const char *cryb_oath_version_string = PACKAGE_VERSION; + +const char * +cryb_oath_version(void) +{ + + return (cryb_oath_version_string); +} diff --git a/lib/oath/oath_hotp.c b/lib/oath/cryb_oath_hotp.c similarity index 100% rename from lib/oath/oath_hotp.c rename to lib/oath/cryb_oath_hotp.c diff --git a/lib/oath/oath_key.c b/lib/oath/cryb_oath_key.c similarity index 100% rename from lib/oath/oath_key.c rename to lib/oath/cryb_oath_key.c diff --git a/lib/oath/oath_key_alloc.c b/lib/oath/cryb_oath_key_alloc.c similarity index 100% rename from lib/oath/oath_key_alloc.c rename to lib/oath/cryb_oath_key_alloc.c diff --git a/lib/oath/oath_key_create.c b/lib/oath/cryb_oath_key_create.c similarity index 100% rename from lib/oath/oath_key_create.c rename to lib/oath/cryb_oath_key_create.c diff --git a/lib/oath/oath_key_dummy.c b/lib/oath/cryb_oath_key_dummy.c similarity index 100% rename from lib/oath/oath_key_dummy.c rename to lib/oath/cryb_oath_key_dummy.c diff --git a/lib/oath/oath_key_free.c b/lib/oath/cryb_oath_key_free.c similarity index 100% rename from lib/oath/oath_key_free.c rename to lib/oath/cryb_oath_key_free.c diff --git a/lib/oath/oath_key_from_file.c b/lib/oath/cryb_oath_key_from_file.c similarity index 100% rename from lib/oath/oath_key_from_file.c rename to lib/oath/cryb_oath_key_from_file.c diff --git a/lib/oath/oath_key_from_uri.c b/lib/oath/cryb_oath_key_from_uri.c similarity index 100% rename from lib/oath/oath_key_from_uri.c rename to lib/oath/cryb_oath_key_from_uri.c diff --git a/lib/oath/oath_mode.c b/lib/oath/cryb_oath_mode.c similarity index 100% rename from lib/oath/oath_mode.c rename to lib/oath/cryb_oath_mode.c diff --git a/lib/oath/oath_totp.c b/lib/oath/cryb_oath_totp.c similarity index 100% rename from lib/oath/oath_totp.c rename to lib/oath/cryb_oath_totp.c diff --git a/lib/rand/Makefile.am b/lib/rand/Makefile.am index bc9ce1c..d2cc077 100644 --- a/lib/rand/Makefile.am +++ b/lib/rand/Makefile.am @@ -2,7 +2,10 @@ AM_CPPFLAGS = -I$(top_srcdir)/include lib_LTLIBRARIES = libcryb-rand.la -libcryb_rand_la_SOURCES = cryb_rand.c +libcryb_rand_la_SOURCES = \ + cryb_rand_bytes.c + \ + cryb_rand.c libcryb_rand_la_LIBADD = \ $(top_builddir)/lib/core/libcryb-core.la diff --git a/lib/rand/cryb_rand.c b/lib/rand/cryb_rand.c index e03e4a7..c4b0a25 100644 --- a/lib/rand/cryb_rand.c +++ b/lib/rand/cryb_rand.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2014 Dag-Erling Smørgrav + * Copyright (c) 2015 Dag-Erling Smørgrav * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -29,36 +29,16 @@ #include "cryb/impl.h" -#include -#include #include -#include #include #include -/* - * Working placeholder until we come up with a proper API and start adding - * more methods. - */ -int -rand_bytes(uint8_t *buf, size_t len) -{ - ssize_t rlen; - int fd, serrno; +static const char *cryb_rand_version_string = PACKAGE_VERSION; - if ((fd = open("/dev/random", O_RDONLY)) < 0) - return (-1); - if ((rlen = read(fd, buf, len)) < 0) { - serrno = errno; - close(fd); - errno = serrno; - return (-1); - } - close(fd); - if (rlen != (ssize_t)len) { - errno = EIO; - return (-1); - } - return (0); +const char * +cryb_rand_version(void) +{ + + return (cryb_rand_version_string); } diff --git a/lib/rand/cryb_rand_bytes.c b/lib/rand/cryb_rand_bytes.c new file mode 100644 index 0000000..e03e4a7 --- /dev/null +++ b/lib/rand/cryb_rand_bytes.c @@ -0,0 +1,64 @@ +/*- + * Copyright (c) 2014 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include +#include +#include +#include + +#include + +/* + * Working placeholder until we come up with a proper API and start adding + * more methods. + */ +int +rand_bytes(uint8_t *buf, size_t len) +{ + ssize_t rlen; + int fd, serrno; + + if ((fd = open("/dev/random", O_RDONLY)) < 0) + return (-1); + if ((rlen = read(fd, buf, len)) < 0) { + serrno = errno; + close(fd); + errno = serrno; + return (-1); + } + close(fd); + if (rlen != (ssize_t)len) { + errno = EIO; + return (-1); + } + return (0); +} diff --git a/lib/test/Makefile.am b/lib/test/Makefile.am index 2339c45..75214be 100644 --- a/lib/test/Makefile.am +++ b/lib/test/Makefile.am @@ -2,8 +2,10 @@ AM_CPPFLAGS = -I$(top_srcdir)/include lib_LTLIBRARIES = libcryb-test.la libcryb_test_la_SOURCES = \ - t_const.c \ - t_file.c \ - t_main.c \ - t_malloc.c \ - t_util.c + cryb_t_const.c \ + cryb_t_file.c \ + cryb_t_main.c \ + cryb_t_malloc.c \ + cryb_t_util.c \ + \ + cryb_test.c diff --git a/lib/test/t_const.c b/lib/test/cryb_t_const.c similarity index 100% rename from lib/test/t_const.c rename to lib/test/cryb_t_const.c diff --git a/lib/test/t_file.c b/lib/test/cryb_t_file.c similarity index 100% rename from lib/test/t_file.c rename to lib/test/cryb_t_file.c diff --git a/lib/test/t_main.c b/lib/test/cryb_t_main.c similarity index 100% rename from lib/test/t_main.c rename to lib/test/cryb_t_main.c diff --git a/lib/test/t_malloc.c b/lib/test/cryb_t_malloc.c similarity index 100% rename from lib/test/t_malloc.c rename to lib/test/cryb_t_malloc.c diff --git a/lib/test/t_util.c b/lib/test/cryb_t_util.c similarity index 100% rename from lib/test/t_util.c rename to lib/test/cryb_t_util.c diff --git a/lib/test/cryb_test.c b/lib/test/cryb_test.c new file mode 100644 index 0000000..4bfd6e5 --- /dev/null +++ b/lib/test/cryb_test.c @@ -0,0 +1,44 @@ +/*- + * Copyright (c) 2015 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include + +#include + +static const char *cryb_test_version_string = PACKAGE_VERSION; + +const char * +cryb_test_version(void) +{ + + return (cryb_test_version_string); +}