From cc8da63f21d43fb29a2892fdef8b6ea488c26ce3 Mon Sep 17 00:00:00 2001 From: Michael Frey Date: Wed, 13 Jun 2018 10:11:14 +0200 Subject: [PATCH 1/2] removed source file includes in ccnl-utils module --- src/ccnl-utils/CMakeLists.txt | 66 ++++---- src/ccnl-utils/include/base64.h | 55 +++++++ src/ccnl-utils/include/ccnl-common.h | 154 ++++++++++++++++++ src/ccnl-utils/include/ccnl-crypto.h | 47 ++++++ src/ccnl-utils/include/ccnl-ext-hmac.h | 78 +++++++++ src/ccnl-utils/include/ccnl-socket.h | 57 +++++++ src/ccnl-utils/include/lib-sha256.h | 128 +++++++++++++++ src/ccnl-utils/{ => src}/base64.c | 21 ++- src/ccnl-utils/{ => src}/ccn-lite-ccnb2xml.c | 4 +- .../{ => src}/ccn-lite-cryptoserver.c | 4 +- src/ccnl-utils/{ => src}/ccn-lite-ctrl.c | 4 +- src/ccnl-utils/{ => src}/ccn-lite-deF.c | 2 +- src/ccnl-utils/{ => src}/ccn-lite-fetch.c | 5 +- src/ccnl-utils/{ => src}/ccn-lite-mkC.c | 6 +- src/ccnl-utils/{ => src}/ccn-lite-mkF.c | 2 +- src/ccnl-utils/{ => src}/ccn-lite-mkI.c | 2 +- .../{ => src}/ccn-lite-ndnstream.py | 0 src/ccnl-utils/{ => src}/ccn-lite-peek.c | 2 +- .../{ => src}/ccn-lite-peekcomputation.c | 2 +- src/ccnl-utils/{ => src}/ccn-lite-pktdump.c | 5 +- src/ccnl-utils/{ => src}/ccn-lite-produce.c | 4 +- src/ccnl-utils/{ => src}/ccn-lite-rpc.c | 2 +- src/ccnl-utils/{ => src}/ccn-lite-simplenfn.c | 2 +- src/ccnl-utils/{ => src}/ccn-lite-valid.c | 4 +- src/ccnl-utils/{ => src}/ccnl-common.c | 3 +- src/ccnl-utils/{ => src}/ccnl-crypto.c | 1 + src/ccnl-utils/{ => src}/ccnl-ext-hmac.c | 9 +- src/ccnl-utils/{ => src}/ccnl-socket.c | 3 + src/ccnl-utils/{ => src}/lib-sha256.c | 73 +-------- 29 files changed, 598 insertions(+), 147 deletions(-) create mode 100644 src/ccnl-utils/include/base64.h create mode 100644 src/ccnl-utils/include/ccnl-common.h create mode 100644 src/ccnl-utils/include/ccnl-crypto.h create mode 100644 src/ccnl-utils/include/ccnl-ext-hmac.h create mode 100644 src/ccnl-utils/include/ccnl-socket.h create mode 100644 src/ccnl-utils/include/lib-sha256.h rename src/ccnl-utils/{ => src}/base64.c (92%) rename src/ccnl-utils/{ => src}/ccn-lite-ccnb2xml.c (99%) rename src/ccnl-utils/{ => src}/ccn-lite-cryptoserver.c (99%) rename src/ccnl-utils/{ => src}/ccn-lite-ctrl.c (99%) rename src/ccnl-utils/{ => src}/ccn-lite-deF.c (99%) rename src/ccnl-utils/{ => src}/ccn-lite-fetch.c (99%) rename src/ccnl-utils/{ => src}/ccn-lite-mkC.c (98%) rename src/ccnl-utils/{ => src}/ccn-lite-mkF.c (99%) rename src/ccnl-utils/{ => src}/ccn-lite-mkI.c (99%) rename src/ccnl-utils/{ => src}/ccn-lite-ndnstream.py (100%) rename src/ccnl-utils/{ => src}/ccn-lite-peek.c (99%) rename src/ccnl-utils/{ => src}/ccn-lite-peekcomputation.c (99%) rename src/ccnl-utils/{ => src}/ccn-lite-pktdump.c (99%) rename src/ccnl-utils/{ => src}/ccn-lite-produce.c (99%) rename src/ccnl-utils/{ => src}/ccn-lite-rpc.c (99%) rename src/ccnl-utils/{ => src}/ccn-lite-simplenfn.c (99%) rename src/ccnl-utils/{ => src}/ccn-lite-valid.c (99%) rename src/ccnl-utils/{ => src}/ccnl-common.c (99%) rename src/ccnl-utils/{ => src}/ccnl-crypto.c (99%) rename src/ccnl-utils/{ => src}/ccnl-ext-hmac.c (97%) rename src/ccnl-utils/{ => src}/ccnl-socket.c (98%) rename src/ccnl-utils/{ => src}/lib-sha256.c (77%) diff --git a/src/ccnl-utils/CMakeLists.txt b/src/ccnl-utils/CMakeLists.txt index 3caa98358..e0f553c6d 100644 --- a/src/ccnl-utils/CMakeLists.txt +++ b/src/ccnl-utils/CMakeLists.txt @@ -9,83 +9,81 @@ link_directories( ${CMAKE_CURRENT_BINARY_DIR}/../lib ) -include_directories( - ../ccnl-pkt/include - ../ccnl-fwd/include - ../ccnl-core/include - ../ccnl-unix/include - ../ccnl-nfn/include -) +# set include directories +include_directories(include ../ccnl-pkt/include ../ccnl-fwd/include ../ccnl-core/include ../ccnl-unix/include ../ccnl-nfn/include) + +add_library(common STATIC src/ccnl-common.c src/base64.c src/ccnl-socket.c) +add_library(ccnl-crypto STATIC src/ccnl-crypto.c src/ccnl-ext-hmac.c src/lib-sha256.c) -add_executable(ccn-lite-peek ccn-lite-peek.c) +add_executable(ccn-lite-peek src/ccn-lite-peek.c) #add_executable(ccn-lite-peekcomputation ccn-lite-peekcomputation.c) #todo work to do -add_executable(ccn-lite-ctrl ccn-lite-ctrl.c) -add_executable(ccn-lite-fetch ccn-lite-fetch.c) +add_executable(ccn-lite-ctrl src/ccn-lite-ctrl.c) +add_executable(ccn-lite-fetch src/ccn-lite-fetch.c) if(OpenSSL_FOUND) - add_executable(ccn-lite-ccnb2xml ccn-lite-ccnb2xml.c) - add_executable(ccn-lite-cryptoserver ccn-lite-cryptoserver.c) + add_executable(ccn-lite-ccnb2xml src/ccn-lite-ccnb2xml.c) + add_executable(ccn-lite-cryptoserver src/ccn-lite-cryptoserver.c) #add_executable(ccn-lite-deF ccn-lite-deF.c) - add_executable(ccn-lite-mkC ccn-lite-mkC.c) - add_executable(ccn-lite-valid ccn-lite-valid.c) - add_executable(ccn-lite-rpc ccn-lite-rpc.c) + add_executable(ccn-lite-mkC src/ccn-lite-mkC.c) + add_executable(ccn-lite-valid src/ccn-lite-valid.c) + add_executable(ccn-lite-rpc src/ccn-lite-rpc.c) endif() if(USE_FRAG) - add_executable(ccn-lite-mkF ccn-lite-mkF.c) + add_executable(ccn-lite-mkF src/ccn-lite-mkF.c) endif() -add_executable(ccn-lite-mkI ccn-lite-mkI.c) -add_executable(ccn-lite-pktdump ccn-lite-pktdump.c) -add_executable(ccn-lite-produce ccn-lite-produce.c) +add_executable(ccn-lite-mkI src/ccn-lite-mkI.c) +add_executable(ccn-lite-pktdump src/ccn-lite-pktdump.c) +add_executable(ccn-lite-produce src/ccn-lite-produce.c) -add_executable(ccn-lite-simplenfn ccn-lite-simplenfn.c) +add_executable(ccn-lite-simplenfn src/ccn-lite-simplenfn.c) target_link_libraries(ccn-lite-peek ${PROJECT_LINK_LIBS} ${EXT_LINK_LIBS}) -target_link_libraries(ccn-lite-peek ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn) +target_link_libraries(ccn-lite-peek ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn common) #target_link_libraries(ccn-lite-peekcomputation ${PROJECT_LINK_LIBS} ${EXT_LINK_LIBS})#todo work to do #target_link_libraries(ccn-lite-peekcomputation ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn) target_link_libraries(ccn-lite-ctrl ${PROJECT_LINK_LIBS} ${EXT_LINK_LIBS}) -target_link_libraries(ccn-lite-ctrl ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn) +target_link_libraries(ccn-lite-ctrl ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn crypto) if(OpenSSL_FOUND) target_link_libraries(ccn-lite-ccnb2xml ${PROJECT_LINK_LIBS} ${EXT_LINK_LIBS} ${OPENSSL_LIBRARIES}) - target_link_libraries(ccn-lite-ccnb2xml ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn ${OPENSSL_LIBRARIES}) + target_link_libraries(ccn-lite-ccnb2xml ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn ccnl-crypto ${OPENSSL_LIBRARIES}) target_link_libraries(ccn-lite-cryptoserver ${PROJECT_LINK_LIBS} ${EXT_LINK_LIBS} ${OPENSSL_LIBRARIES}) - target_link_libraries(ccn-lite-cryptoserver ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn ${OPENSSL_LIBRARIES}) + target_link_libraries(ccn-lite-cryptoserver ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn ccnl-crypto ${OPENSSL_LIBRARIES} common) #target_link_libraries(ccn-lite-deF ${PROJECT_LINK_LIBS} ${EXT_LINK_LIBS} ${OPENSSL_LIBRARIES}) #target_link_libraries(ccn-lite-deF ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn ${OPENSSL_LIBRARIES}) - target_link_libraries(ccn-lite-mkC ${PROJECT_LINK_LIBS} ${EXT_LINK_LIBS} ${OPENSSL_LIBRARIES}) - target_link_libraries(ccn-lite-mkC ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn ${OPENSSL_LIBRARIES}) + target_link_libraries(ccn-lite-mkC ${PROJECT_LINK_LIBS} ${EXT_LINK_LIBS} ${OPENSSL_LIBRARIES} common) + target_link_libraries(ccn-lite-mkC ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn ccnl-crypto ${OPENSSL_LIBRARIES}) - target_link_libraries(ccn-lite-valid ${PROJECT_LINK_LIBS} ${EXT_LINK_LIBS} ${OPENSSL_LIBRARIES}) + target_link_libraries(ccn-lite-valid ${PROJECT_LINK_LIBS} ${EXT_LINK_LIBS} ${OPENSSL_LIBRARIES} common ccnl-crypto) target_link_libraries(ccn-lite-valid ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn ${OPENSSL_LIBRARIES}) - target_link_libraries(ccn-lite-rpc ${PROJECT_LINK_LIBS} ${EXT_LINK_LIBS} ${OPENSSL_LIBRARIES}) + target_link_libraries(ccn-lite-rpc ${PROJECT_LINK_LIBS} ${EXT_LINK_LIBS} ${OPENSSL_LIBRARIES} common) target_link_libraries(ccn-lite-rpc ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn ${OPENSSL_LIBRARIES}) endif() target_link_libraries(ccn-lite-fetch ${PROJECT_LINK_LIBS} ${EXT_LINK_LIBS}) -target_link_libraries(ccn-lite-fetch ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn) +target_link_libraries(ccn-lite-fetch ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn common) if(USE_FRAG) target_link_libraries(ccn-lite-mkF ${PROJECT_LINK_LIBS} ${EXT_LINK_LIBS}) - target_link_libraries(ccn-lite-mkF ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn) + target_link_libraries(ccn-lite-mkF ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn common) endif() target_link_libraries(ccn-lite-mkI ${PROJECT_LINK_LIBS} ${EXT_LINK_LIBS}) -target_link_libraries(ccn-lite-mkI ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn) +target_link_libraries(ccn-lite-mkI ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn common) target_link_libraries(ccn-lite-pktdump ${PROJECT_LINK_LIBS} ${EXT_LINK_LIBS}) -target_link_libraries(ccn-lite-pktdump ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn) +target_link_libraries(ccn-lite-pktdump ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn common) target_link_libraries(ccn-lite-produce ${PROJECT_LINK_LIBS} ${EXT_LINK_LIBS}) -target_link_libraries(ccn-lite-produce ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn) +target_link_libraries(ccn-lite-produce ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn common ccnl-crypto) target_link_libraries(ccn-lite-simplenfn ${PROJECT_LINK_LIBS} ${EXT_LINK_LIBS}) -target_link_libraries(ccn-lite-simplenfn ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn) +target_link_libraries(ccn-lite-simplenfn ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-nfn common) diff --git a/src/ccnl-utils/include/base64.h b/src/ccnl-utils/include/base64.h new file mode 100644 index 000000000..3d0e3dbd5 --- /dev/null +++ b/src/ccnl-utils/include/base64.h @@ -0,0 +1,55 @@ +// base64.c +// from stackoverflow.com, user RYYST + +#ifndef BASE64_H +#define BASE64_H + +#include +#include +#include +#include + +/** + * @brief Initializes the base64 decoding table + */ +void base64_build_decoding_table(void); + +/** + * @brief Encodes a string \p data into a base64 string + * + * @param[in] data The data to encode + * @param[in] input_length The size of the data to encode + * @param[out] output_length The size of the encoded date + * + * @return Upon success, a base64 encoded representation of \p data + * @return NULL if \p data was NULL + * @return NULL if allocating the result string failed + * @return NULL if \p input_length is not in multiples of four + */ +char *base64_encode(const char *data, + size_t input_length, + size_t *output_length); + +/** + * @brief Decodes a string \p data into a base64 string + * + * @param[in] data The data to decode + * @param[in] input_length The size of the data to decode + * @param[out] output_length The size of the decoded date + * + * @return Upon success, the base64 decoded \p data + * @return NULL if \p data was NULL + * @return NULL if allocating the result string failed + * @return NULL if \p input_length is not in multiples of four + */ +unsigned char *base64_decode(const char *data, + size_t input_length, + size_t *output_length); + +/** + * @brief Frees the previously allocated decoding table + */ +void base64_cleanup(void); + +#endif // EOF +/** @} */ diff --git a/src/ccnl-utils/include/ccnl-common.h b/src/ccnl-utils/include/ccnl-common.h new file mode 100644 index 000000000..ae9e30a92 --- /dev/null +++ b/src/ccnl-utils/include/ccnl-common.h @@ -0,0 +1,154 @@ +/** + * @addtogroup CCNL-utils + * @{ + * + * @file ccnl-common.h + * @brief Common functions for the CCN-lite utilities + * + * Copyright (C) 2013-18 Christian Tschudin, University of Basel + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +#ifndef CCNL_COMMON_H +#define CCNL_COMMON_H + +#ifndef CCNL_UAPI_H_ // if CCNL_UAPI_H_ is defined then the following config is taken care elsewhere in the code composite + + +#define _DEFAULT_SOURCE +#define _BSD_SOURCE +#define _SVID_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "base64.h" + +#include "ccnl-os-includes.h" +#include "ccnl-defs.h" +#include "ccnl-core.h" +#include "ccnl-pkt-builder.h" +#include "ccnl-malloc.h" +#include "ccnl-os-time.h" +#include "ccnl-logging.h" +#include "ccnl-pkt-builder.h" + +#ifndef USE_DEBUG_MALLOC +#define ccnl_malloc(s) malloc(s) +#define ccnl_calloc(n,s) calloc(n,s) +#define ccnl_realloc(p,s) realloc(p,s) +#define ccnl_free(p) free(p) +#endif //USE_DEBUG_MALLOC +#define free_2ptr_list(a,b) ccnl_free(a), ccnl_free(b) + +struct ccnl_prefix_s* ccnl_prefix_new(int suite, int cnt); +int ccnl_pkt_prependComponent(int suite, char *src, int *offset, unsigned char *buf); + +#include "ccnl-core.h" +#include "ccnl-pkt-ccnb.h" +#include "ccnl-pkt-ccntlv.h" +#include "ccnl-pkt-localrpc.h" +#include "ccnl-pkt-ndntlv.h" +#include "ccnl-pkt-switch.h" + +#include "ccnl-socket.h" + +#define ccnl_core_addToCleanup(b) do{}while(0) + +// include only the utils, not the core routines: +#ifdef USE_FRAG +#include "../ccnl-frag.h" +#endif + +#else // CCNL_UAPI_H_ is defined + +#include "base64.c" +#ifdef RIOT_VERSION +#include "ccnl-defs.h" +#include "net/packet.h" +#include +#include "sys/socket.h" +#include "ccn-lite-riot.h" +#include "ccnl-headers.h" +#include "ccnl-pkt-ndntlv.h" +#include "ccnl-pkt-ccntlv.h" +#include "ccnl-pkt-ccnb.h" + + +extern int ccnl_suite2defaultPort(int suite); +#endif + +#endif // CCNL_UAPI_H_ + + +// ---------------------------------------------------------------------- + +const char* ccnl_enc2str(int enc); + +// ---------------------------------------------------------------------- + +#define extractStr(VAR,DTAG) \ + if (typ == CCN_TT_DTAG && num == DTAG) { \ + char *s; unsigned char *valptr; int vallen; \ + if (ccnl_ccnb_consume(typ, num, &buf, &buflen, &valptr, &vallen) < 0) \ + goto Bail; \ + s = ccnl_malloc(vallen+1); if (!s) goto Bail; \ + memcpy(s, valptr, vallen); s[vallen] = '\0'; \ + ccnl_free(VAR); \ + VAR = (unsigned char*) s; \ + continue; \ + } do {} while(0) + +#define extractStr2(VAR,DTAG) \ + if (typ == CCN_TT_DTAG && num == DTAG) { \ + char *s; unsigned char *valptr; int vallen; \ + if (ccnl_ccnb_consume(typ, num, buf, buflen, &valptr, &vallen) < 0) \ + goto Bail; \ + s = ccnl_malloc(vallen+1); if (!s) goto Bail; \ + memcpy(s, valptr, vallen); s[vallen] = '\0'; \ + ccnl_free(VAR); \ + VAR = (unsigned char*) s; \ + continue; \ + } do {} while(0) + +// ---------------------------------------------------------------------- + +struct key_s { + struct key_s *next; + unsigned char* key; + int keylen; +}; + +struct key_s* load_keys_from_file(char *path); + +// ---------------------------------------------------------------------- + +int +ccnl_parseUdp(char *udp, int suite, char **addr, int *port); + +#endif +/** @} */ diff --git a/src/ccnl-utils/include/ccnl-crypto.h b/src/ccnl-utils/include/ccnl-crypto.h new file mode 100644 index 000000000..06773fb47 --- /dev/null +++ b/src/ccnl-utils/include/ccnl-crypto.h @@ -0,0 +1,47 @@ +/** + * @addtogroup CCNL-utils + * @{ + * + * @file ccnl-crypto.h + * @brief Crypto functions for CCN-lite utilities + * + * Copyright (C) 2013-2018, Christian Tschudin, University of Basel + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +#ifndef CCNL_CRYPTO_H +#define CCNL_CRYPTO_H + +#ifdef USE_SIGNATURES +#include +#include +#include +#include +#include +#endif + +int sha(void* input, unsigned long length, unsigned char* md); + +int sign(char* private_key_path, unsigned char *msg, int msg_len, + unsigned char *sig, unsigned int *sig_len); + +int +verify(char* public_key_path, unsigned char *msg, int msg_len, + unsigned char *sig, unsigned int sig_len); + +int +add_signature(unsigned char *out, char *private_key_path, + unsigned char *file, unsigned int fsize); + +#endif +/** @} */ diff --git a/src/ccnl-utils/include/ccnl-ext-hmac.h b/src/ccnl-utils/include/ccnl-ext-hmac.h new file mode 100644 index 000000000..36dc36167 --- /dev/null +++ b/src/ccnl-utils/include/ccnl-ext-hmac.h @@ -0,0 +1,78 @@ +/** + * @addtogroup CCNL-utils + * @{ + * + * @file ccnl-ext-hmac.h + * @brief HMAC-256 signing support + * + * Copyright (C) 2015-18 + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +#ifndef CCNL_EXT_MAC_H_ +#define CCNL_EXT_MAC_H_ + +#include "lib-sha256.h" + +#include "ccnl-pkt-ccnb.h" +#include "ccnl-pkt-ccntlv.h" +#include "ccnl-pkt-localrpc.h" +#include "ccnl-pkt-ndntlv.h" +#include "ccnl-pkt-switch.h" + + +void +ccnl_hmac256_keyval(unsigned char *key, int klen, + unsigned char *keyval); // MUST have 64 bytes (BLOCK_LENGTH) +void +ccnl_hmac256_keyid(unsigned char *key, int klen, + unsigned char *keyid); // MUST have 32 bytes (DIGEST_LENGTH) +// internal +void +ccnl_hmac256_keysetup(SHA256_CTX_t *ctx, unsigned char *keyval, int kvlen, + unsigned char pad); +void +ccnl_hmac256_sign(unsigned char *keyval, int kvlen, + unsigned char *data, int dlen, + unsigned char *md, int *mlen); +#ifdef NEEDS_PACKET_CRAFTING + +#ifdef USE_SUITE_CCNTLV + +// write Content packet *before* buf[offs], adjust offs and return bytes used +int +ccnl_ccntlv_prependSignedContentWithHdr(struct ccnl_prefix_s *name, + unsigned char *payload, int paylen, + unsigned int *lastchunknum, + int *contentpos, + unsigned char *keyval, // 64B + unsigned char *keydigest, // 32B + int *offset, unsigned char *buf); +#endif // USE_SUITE_CCNTLV + +#ifdef USE_SUITE_NDNTLV + +int +ccnl_ndntlv_prependSignedContent(struct ccnl_prefix_s *name, + unsigned char *payload, int paylen, + unsigned int *final_block_id, int *contentpos, + unsigned char *keyval, // 64B + unsigned char *keydigest, // 32B + int *offset, unsigned char *buf); + +#endif // USE_SUITE_NDNTLV + +#endif // NEEDS_PACKET_CRAFTING + +#endif +/** @} */ diff --git a/src/ccnl-utils/include/ccnl-socket.h b/src/ccnl-utils/include/ccnl-socket.h new file mode 100644 index 000000000..28293d641 --- /dev/null +++ b/src/ccnl-utils/include/ccnl-socket.h @@ -0,0 +1,57 @@ +/** + * @addtogroup CCNL-utils + * @{ + * + * @file ccnl-socket.h + * @brief Request content: send an interest open socket etc + * + * Copyright (C) 2013-18, Christian Tschudin, University of Basel + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +#ifndef CCNL_SOCKET_H +#define CCNL_SOCKET_H + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include + +void myexit(int rc); + +int udp_open(void); + +int udp_sendto(int sock, char *dest, unsigned char *data, int len); + + +int ux_open(void); + +int ux_sendto(int sock, char *topath, unsigned char *data, int len); + +int block_on_read(int sock, float wait); + +void request_content(int sock, int (*sendproc)(int,char*,unsigned char*,int), + char *dest, unsigned char *out, int len, float wait); + +#endif +/** @} */ diff --git a/src/ccnl-utils/include/lib-sha256.h b/src/ccnl-utils/include/lib-sha256.h new file mode 100644 index 000000000..964fd8c23 --- /dev/null +++ b/src/ccnl-utils/include/lib-sha256.h @@ -0,0 +1,128 @@ +/* + * @f lib-sha256.c + * @b implementation of NIST SHA256, based on Aaron Gifford's code + * + */ +#include +#include +#include + +#ifdef CCNL_ARDUINO +# define _MEMLOCATION_ PROGMEM +# define K256_(i) pgm_read_dword_near(K256 + i) +#else +# define _MEMLOCATION_ +# define K256_(i) K256[i] +#endif + +/* + * AUTHOR: Aaron D. Gifford - http://www.aarongifford.com/ + * + * Copyright (c) 2000-2001, Aaron D. Gifford + * 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. Neither the name of the copyright holder nor the names of contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``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 CONTRIBUTOR(S) 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. + * + * $Id: sha2.h,v 1.1 2001/11/08 00:02:01 adg Exp adg $ + */ + +// basic definitions + +#define SHA256_BLOCK_LENGTH 64 +#define SHA256_DIGEST_LENGTH 32 +#define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1) +#define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8) + +typedef struct _SHA256_CTX { + uint32_t state[8]; + uint64_t bitcount; + uint8_t buffer[SHA256_BLOCK_LENGTH]; +} SHA256_CTX_t; + +typedef uint8_t sha2_byte; /* Exactly 1 byte */ +typedef uint32_t sha2_word32; /* Exactly 4 bytes */ +typedef uint64_t sha2_word64; /* Exactly 8 bytes */ + + +// reversal macros + +#if BYTE_ORDER == LITTLE_ENDIAN +#define REVERSE32(w,x) { \ + sha2_word32 tmp = (w); \ + tmp = (tmp >> 16) | (tmp << 16); \ + (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \ +} +#define REVERSE64(w,x) { \ + sha2_word64 tmp = (w); \ + tmp = (tmp >> 32) | (tmp << 32); \ + tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \ + ((tmp & 0x00ff00ff00ff00ffULL) << 8); \ + (x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \ + ((tmp & 0x0000ffff0000ffffULL) << 16); \ +} +#endif /* BYTE_ORDER == LITTLE_ENDIAN */ + +#define MEMSET_BZERO(p,l) memset((p), 0, (l)) +#define MEMCPY_BCOPY(d,s,l) memcpy((d), (s), (l)) + +/*** THE SIX LOGICAL FUNCTIONS ****************************************/ +/* + * Bit shifting and rotation (used by the six SHA-XYZ logical functions: + * + * NOTE: The naming of R and S appears backwards here (R is a SHIFT and + * S is a ROTATION) because the SHA-256/384/512 description document + * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this + * same "backwards" definition. + */ +/* Shift-right (used in SHA-256, SHA-384, and SHA-512): */ +#define R(b,x) ((x) >> (b)) +/* 32-bit Rotate-right (used in SHA-256): */ +#define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b)))) +/* 64-bit Rotate-right (used in SHA-384 and SHA-512): */ +#define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b)))) + +/* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */ +#define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z))) +#define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) + +/* Four of six logical functions used in SHA-256: */ +#define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x))) +#define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x))) +#define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x))) +#define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x))) + + +/*** SHA-256: *********************************************************/ + +void ccnl_SHA256_Init(SHA256_CTX_t* context); + +void ccnl_SHA256_Transform(SHA256_CTX_t* context, const sha2_word32* data); + + +void ccnl_SHA256_Update(SHA256_CTX_t* context, const sha2_byte *data, size_t len); + +void ccnl_SHA256_Final(sha2_byte digest[], SHA256_CTX_t* context); + +// eof diff --git a/src/ccnl-utils/base64.c b/src/ccnl-utils/src/base64.c similarity index 92% rename from src/ccnl-utils/base64.c rename to src/ccnl-utils/src/base64.c index f8c1fd04b..c59e9816d 100644 --- a/src/ccnl-utils/base64.c +++ b/src/ccnl-utils/src/base64.c @@ -1,10 +1,8 @@ // base64.c // from stackoverflow.com, user RYYST -#include -#include -#include -#include + +#include "base64.h" static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', @@ -37,8 +35,13 @@ char *output_length = 4 * ((input_length + 2) / 3) + 1; char *encoded_data = (char *) malloc(*output_length); - memset(encoded_data, '\0', *output_length); - if (encoded_data == NULL) return NULL; + + if (encoded_data == NULL) { + return NULL; + } else { + memset(encoded_data, '\0', *output_length); + } + int i, j; for (i = 0, j = 0; (unsigned int) i < input_length;) { @@ -99,7 +102,9 @@ unsigned char void base64_cleanup(void) { - free(decoding_table); + /** only free the table if it has been allocated before */ + if (decoding_table != NULL) { + free(decoding_table); + } } -// eof diff --git a/src/ccnl-utils/ccn-lite-ccnb2xml.c b/src/ccnl-utils/src/ccn-lite-ccnb2xml.c similarity index 99% rename from src/ccnl-utils/ccn-lite-ccnb2xml.c rename to src/ccnl-utils/src/ccn-lite-ccnb2xml.c index 74cd2e0ee..e695e73d3 100644 --- a/src/ccnl-utils/ccn-lite-ccnb2xml.c +++ b/src/ccnl-utils/src/ccn-lite-ccnb2xml.c @@ -20,8 +20,8 @@ * 2012-07-01 created */ -#include "ccnl-common.c" -#include "ccnl-crypto.c" +#include "ccnl-common.h" +#include "ccnl-crypto.h" // ---------------------------------------------------------------------- diff --git a/src/ccnl-utils/ccn-lite-cryptoserver.c b/src/ccnl-utils/src/ccn-lite-cryptoserver.c similarity index 99% rename from src/ccnl-utils/ccn-lite-cryptoserver.c rename to src/ccnl-utils/src/ccn-lite-cryptoserver.c index 5de7383e5..bfeed6a86 100644 --- a/src/ccnl-utils/ccn-lite-cryptoserver.c +++ b/src/ccnl-utils/src/ccn-lite-cryptoserver.c @@ -21,8 +21,8 @@ */ -#include "ccnl-common.c" -#include "ccnl-crypto.c" +#include "ccnl-common.h" +#include "ccnl-crypto.h" // ---------------------------------------------------------------------- diff --git a/src/ccnl-utils/ccn-lite-ctrl.c b/src/ccnl-utils/src/ccn-lite-ctrl.c similarity index 99% rename from src/ccnl-utils/ccn-lite-ctrl.c rename to src/ccnl-utils/src/ccn-lite-ctrl.c index 4f26da16d..9ffb9b2ee 100644 --- a/src/ccnl-utils/ccn-lite-ctrl.c +++ b/src/ccnl-utils/src/ccn-lite-ctrl.c @@ -22,8 +22,8 @@ * of return message */ -#include "ccnl-common.c" -#include "ccnl-crypto.c" +#include "ccnl-common.h" +#include "ccnl-crypto.h" // ---------------------------------------------------------------------- diff --git a/src/ccnl-utils/ccn-lite-deF.c b/src/ccnl-utils/src/ccn-lite-deF.c similarity index 99% rename from src/ccnl-utils/ccn-lite-deF.c rename to src/ccnl-utils/src/ccn-lite-deF.c index 8d01718c9..6e89ff657 100644 --- a/src/ccnl-utils/ccn-lite-deF.c +++ b/src/ccnl-utils/src/ccn-lite-deF.c @@ -20,7 +20,7 @@ * 2013-07-06 created */ -#include "ccnl-common.c" +#include "ccnl-common.h" #include "ccnl-frag.h" // ---------------------------------------------------------------------- diff --git a/src/ccnl-utils/ccn-lite-fetch.c b/src/ccnl-utils/src/ccn-lite-fetch.c similarity index 99% rename from src/ccnl-utils/ccn-lite-fetch.c rename to src/ccnl-utils/src/ccn-lite-fetch.c index 45cce884a..5d22a17c7 100644 --- a/src/ccnl-utils/ccn-lite-fetch.c +++ b/src/ccnl-utils/src/ccn-lite-fetch.c @@ -23,7 +23,10 @@ //#define NEEDS_PACKET_CRAFTING -#include "ccnl-common.c" +#include "ccnl-common.h" + +#include + //#include "ccnl-socket.c" // ---------------------------------------------------------------------- diff --git a/src/ccnl-utils/ccn-lite-mkC.c b/src/ccnl-utils/src/ccn-lite-mkC.c similarity index 98% rename from src/ccnl-utils/ccn-lite-mkC.c rename to src/ccnl-utils/src/ccn-lite-mkC.c index 3fbedd1a2..86775998b 100644 --- a/src/ccnl-utils/ccn-lite-mkC.c +++ b/src/ccnl-utils/src/ccn-lite-mkC.c @@ -23,10 +23,10 @@ //#define NEEDS_PACKET_CRAFTING -#include "ccnl-common.c" -#include "ccnl-crypto.c" +#include "ccnl-common.h" +#include "ccnl-crypto.h" #include "ccnl-pkt-ndntlv.h" -#include "ccnl-ext-hmac.c" +#include "ccnl-ext-hmac.h" // ---------------------------------------------------------------------- diff --git a/src/ccnl-utils/ccn-lite-mkF.c b/src/ccnl-utils/src/ccn-lite-mkF.c similarity index 99% rename from src/ccnl-utils/ccn-lite-mkF.c rename to src/ccnl-utils/src/ccn-lite-mkF.c index f6a97c8ec..30bb7fa84 100644 --- a/src/ccnl-utils/ccn-lite-mkF.c +++ b/src/ccnl-utils/src/ccn-lite-mkF.c @@ -21,7 +21,7 @@ */ -#include "ccnl-common.c" +#include "ccnl-common.h" // ---------------------------------------------------------------------- void diff --git a/src/ccnl-utils/ccn-lite-mkI.c b/src/ccnl-utils/src/ccn-lite-mkI.c similarity index 99% rename from src/ccnl-utils/ccn-lite-mkI.c rename to src/ccnl-utils/src/ccn-lite-mkI.c index c54ecb63d..36cf32542 100644 --- a/src/ccnl-utils/ccn-lite-mkI.c +++ b/src/ccnl-utils/src/ccn-lite-mkI.c @@ -20,7 +20,7 @@ * 2013-07-06 created */ -#include "ccnl-common.c" +#include "ccnl-common.h" // ---------------------------------------------------------------------- diff --git a/src/ccnl-utils/ccn-lite-ndnstream.py b/src/ccnl-utils/src/ccn-lite-ndnstream.py similarity index 100% rename from src/ccnl-utils/ccn-lite-ndnstream.py rename to src/ccnl-utils/src/ccn-lite-ndnstream.py diff --git a/src/ccnl-utils/ccn-lite-peek.c b/src/ccnl-utils/src/ccn-lite-peek.c similarity index 99% rename from src/ccnl-utils/ccn-lite-peek.c rename to src/ccnl-utils/src/ccn-lite-peek.c index fa913c472..a919a2e60 100644 --- a/src/ccnl-utils/ccn-lite-peek.c +++ b/src/ccnl-utils/src/ccn-lite-peek.c @@ -21,7 +21,7 @@ * 2014-06-18 added NDNTLV support */ -#include "ccnl-common.c" +#include "ccnl-common.h" #include #ifndef assert #define assert(...) do {} while(0) diff --git a/src/ccnl-utils/ccn-lite-peekcomputation.c b/src/ccnl-utils/src/ccn-lite-peekcomputation.c similarity index 99% rename from src/ccnl-utils/ccn-lite-peekcomputation.c rename to src/ccnl-utils/src/ccn-lite-peekcomputation.c index 6bdb893a2..9febccddf 100644 --- a/src/ccnl-utils/ccn-lite-peekcomputation.c +++ b/src/ccnl-utils/src/ccn-lite-peekcomputation.c @@ -1,5 +1,5 @@ -#include "ccnl-common.c" +#include "ccnl-common.h" int diff --git a/src/ccnl-utils/ccn-lite-pktdump.c b/src/ccnl-utils/src/ccn-lite-pktdump.c similarity index 99% rename from src/ccnl-utils/ccn-lite-pktdump.c rename to src/ccnl-utils/src/ccn-lite-pktdump.c index 4d0ec5a62..8e460cd57 100644 --- a/src/ccnl-utils/ccn-lite-pktdump.c +++ b/src/ccnl-utils/src/ccn-lite-pktdump.c @@ -22,9 +22,8 @@ * */ - - -#include "ccnl-common.c" +#include "base64.h" +#include "ccnl-common.h" // ---------------------------------------------------------------------- diff --git a/src/ccnl-utils/ccn-lite-produce.c b/src/ccnl-utils/src/ccn-lite-produce.c similarity index 99% rename from src/ccnl-utils/ccn-lite-produce.c rename to src/ccnl-utils/src/ccn-lite-produce.c index f39f52f81..0dcfec665 100644 --- a/src/ccnl-utils/ccn-lite-produce.c +++ b/src/ccnl-utils/src/ccn-lite-produce.c @@ -23,8 +23,8 @@ #define CCNL_MAX_CHUNK_SIZE 4048 -#include "ccnl-common.c" -#include "ccnl-crypto.c" +#include "ccnl-common.h" +#include "ccnl-crypto.h" int main(int argc, char *argv[]) diff --git a/src/ccnl-utils/ccn-lite-rpc.c b/src/ccnl-utils/src/ccn-lite-rpc.c similarity index 99% rename from src/ccnl-utils/ccn-lite-rpc.c rename to src/ccnl-utils/src/ccn-lite-rpc.c index f36685ca9..31cd4114b 100644 --- a/src/ccnl-utils/ccn-lite-rpc.c +++ b/src/ccnl-utils/src/ccn-lite-rpc.c @@ -20,7 +20,7 @@ * 2014-05-11 created */ -#include "ccnl-common.c" +#include "ccnl-common.h" /* use examples: diff --git a/src/ccnl-utils/ccn-lite-simplenfn.c b/src/ccnl-utils/src/ccn-lite-simplenfn.c similarity index 99% rename from src/ccnl-utils/ccn-lite-simplenfn.c rename to src/ccnl-utils/src/ccn-lite-simplenfn.c index 29667a8e7..82f8dbd0d 100644 --- a/src/ccnl-utils/ccn-lite-simplenfn.c +++ b/src/ccnl-utils/src/ccn-lite-simplenfn.c @@ -21,7 +21,7 @@ */ -#include "ccnl-common.c" +#include "ccnl-common.h" #include "ccnl-nfn-parse.h" int diff --git a/src/ccnl-utils/ccn-lite-valid.c b/src/ccnl-utils/src/ccn-lite-valid.c similarity index 99% rename from src/ccnl-utils/ccn-lite-valid.c rename to src/ccnl-utils/src/ccn-lite-valid.c index b110f6d74..6bcd5ba71 100644 --- a/src/ccnl-utils/ccn-lite-valid.c +++ b/src/ccnl-utils/src/ccn-lite-valid.c @@ -21,8 +21,8 @@ */ #define assert(...) do {} while(0) -#include "ccnl-common.c" -#include "ccnl-ext-hmac.c" +#include "ccnl-common.h" +#include "ccnl-ext-hmac.h" struct ccnl_pkt_s* ccnl_parse(unsigned char *data, int datalen) diff --git a/src/ccnl-utils/ccnl-common.c b/src/ccnl-utils/src/ccnl-common.c similarity index 99% rename from src/ccnl-utils/ccnl-common.c rename to src/ccnl-utils/src/ccnl-common.c index 8986e70bf..34d6f9c49 100644 --- a/src/ccnl-utils/ccnl-common.c +++ b/src/ccnl-utils/src/ccnl-common.c @@ -55,7 +55,8 @@ #include #include -#include "base64.c" +#include "base64.h" +//#include "base64.c" #include "ccnl-os-includes.h" #include "ccnl-defs.h" diff --git a/src/ccnl-utils/ccnl-crypto.c b/src/ccnl-utils/src/ccnl-crypto.c similarity index 99% rename from src/ccnl-utils/ccnl-crypto.c rename to src/ccnl-utils/src/ccnl-crypto.c index 9025cbac4..10d257456 100644 --- a/src/ccnl-utils/ccnl-crypto.c +++ b/src/ccnl-utils/src/ccnl-crypto.c @@ -22,6 +22,7 @@ // ---------------------------------------------------------------------- +#include "ccnl-crypto.h" #ifdef USE_SIGNATURES // ---------------------------------------------------------------------- diff --git a/src/ccnl-utils/ccnl-ext-hmac.c b/src/ccnl-utils/src/ccnl-ext-hmac.c similarity index 97% rename from src/ccnl-utils/ccnl-ext-hmac.c rename to src/ccnl-utils/src/ccnl-ext-hmac.c index 5e35700ad..312c6c9b5 100644 --- a/src/ccnl-utils/ccnl-ext-hmac.c +++ b/src/ccnl-utils/src/ccnl-ext-hmac.c @@ -19,14 +19,7 @@ * File history: * 2015-05-08 created */ - - -#include "ccnl-pkt-ccnb.h" -#include "ccnl-pkt-ccntlv.h" -#include "ccnl-pkt-localrpc.h" -#include "ccnl-pkt-ndntlv.h" -#include "ccnl-pkt-switch.h" -#include "lib-sha256.c" +#include "ccnl-ext-hmac.h" #ifdef USE_HMAC256 diff --git a/src/ccnl-utils/ccnl-socket.c b/src/ccnl-utils/src/ccnl-socket.c similarity index 98% rename from src/ccnl-utils/ccnl-socket.c rename to src/ccnl-utils/src/ccnl-socket.c index e2bb2896b..750fb5ea9 100644 --- a/src/ccnl-utils/ccnl-socket.c +++ b/src/ccnl-utils/src/ccnl-socket.c @@ -36,6 +36,9 @@ #include #include +#include "ccnl-defs.h" +#include "ccnl-socket.h" + char *unix_path; void diff --git a/src/ccnl-utils/lib-sha256.c b/src/ccnl-utils/src/lib-sha256.c similarity index 77% rename from src/ccnl-utils/lib-sha256.c rename to src/ccnl-utils/src/lib-sha256.c index 2e76f7846..be92525b4 100644 --- a/src/ccnl-utils/lib-sha256.c +++ b/src/ccnl-utils/src/lib-sha256.c @@ -3,14 +3,7 @@ * @b implementation of NIST SHA256, based on Aaron Gifford's code * */ - -#ifdef CCNL_ARDUINO -# define _MEMLOCATION_ PROGMEM -# define K256_(i) pgm_read_dword_near(K256 + i) -#else -# define _MEMLOCATION_ -# define K256_(i) K256[i] -#endif +#include "lib-sha256.h" /* * AUTHOR: Aaron D. Gifford - http://www.aarongifford.com/ @@ -47,70 +40,6 @@ // basic definitions -#define SHA256_BLOCK_LENGTH 64 -#define SHA256_DIGEST_LENGTH 32 -#define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1) -#define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8) - -typedef struct _SHA256_CTX { - uint32_t state[8]; - uint64_t bitcount; - uint8_t buffer[SHA256_BLOCK_LENGTH]; -} SHA256_CTX_t; - -typedef uint8_t sha2_byte; /* Exactly 1 byte */ -typedef uint32_t sha2_word32; /* Exactly 4 bytes */ -typedef uint64_t sha2_word64; /* Exactly 8 bytes */ - - -// reversal macros - -#if BYTE_ORDER == LITTLE_ENDIAN -#define REVERSE32(w,x) { \ - sha2_word32 tmp = (w); \ - tmp = (tmp >> 16) | (tmp << 16); \ - (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \ -} -#define REVERSE64(w,x) { \ - sha2_word64 tmp = (w); \ - tmp = (tmp >> 32) | (tmp << 32); \ - tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \ - ((tmp & 0x00ff00ff00ff00ffULL) << 8); \ - (x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \ - ((tmp & 0x0000ffff0000ffffULL) << 16); \ -} -#endif /* BYTE_ORDER == LITTLE_ENDIAN */ - -#define MEMSET_BZERO(p,l) memset((p), 0, (l)) -#define MEMCPY_BCOPY(d,s,l) memcpy((d), (s), (l)) - -/*** THE SIX LOGICAL FUNCTIONS ****************************************/ -/* - * Bit shifting and rotation (used by the six SHA-XYZ logical functions: - * - * NOTE: The naming of R and S appears backwards here (R is a SHIFT and - * S is a ROTATION) because the SHA-256/384/512 description document - * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this - * same "backwards" definition. - */ -/* Shift-right (used in SHA-256, SHA-384, and SHA-512): */ -#define R(b,x) ((x) >> (b)) -/* 32-bit Rotate-right (used in SHA-256): */ -#define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b)))) -/* 64-bit Rotate-right (used in SHA-384 and SHA-512): */ -#define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b)))) - -/* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */ -#define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z))) -#define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) - -/* Four of six logical functions used in SHA-256: */ -#define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x))) -#define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x))) -#define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x))) -#define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x))) - - /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/ /* Hash constant words K for SHA-256: */ const sha2_word32 K256[64] _MEMLOCATION_ = { From 2d47ca582ad549981d1524ca4fb8783e9ed523c0 Mon Sep 17 00:00:00 2001 From: Michael Frey Date: Thu, 14 Jun 2018 09:42:57 +0200 Subject: [PATCH 2/2] fixed RFC comment and added documentation in ccnl-utils module --- src/ccnl-utils/include/ccnl-ext-hmac.h | 99 +++++++++++++++++++++----- src/ccnl-utils/src/ccnl-ext-hmac.c | 27 +------ 2 files changed, 85 insertions(+), 41 deletions(-) diff --git a/src/ccnl-utils/include/ccnl-ext-hmac.h b/src/ccnl-utils/include/ccnl-ext-hmac.h index 36dc36167..f63c771d7 100644 --- a/src/ccnl-utils/include/ccnl-ext-hmac.h +++ b/src/ccnl-utils/include/ccnl-ext-hmac.h @@ -3,7 +3,7 @@ * @{ * * @file ccnl-ext-hmac.h - * @brief HMAC-256 signing support + * @brief HMAC-256 signing support based on RFC 2104 * * Copyright (C) 2015-18 * @@ -30,48 +30,115 @@ #include "ccnl-pkt-ndntlv.h" #include "ccnl-pkt-switch.h" +/** + * @brief Generates an HMAC key + * + * @note The \p keyval must be of at least 64 bytes (block length). + * + * The function either copies \p klen bytes of \p key or generates a new + * key. + * + * @param[in] key The value of the key (to be copied or generated) + * @param[in] klen The length of the key + * @param[in] keyval The (final) key (to be copied or generated) + */ +void ccnl_hmac256_keyval(unsigned char *key, int klen, unsigned char *keyval); -void -ccnl_hmac256_keyval(unsigned char *key, int klen, - unsigned char *keyval); // MUST have 64 bytes (BLOCK_LENGTH) +/** + * @brief TODO + * + * @note The \p keyid must be of at least 32 bytes (digest length). + * + * @param[in] key The actual key + * @param[in] klen The length of the key + * @param[in] keyid TODO + */ void ccnl_hmac256_keyid(unsigned char *key, int klen, - unsigned char *keyid); // MUST have 32 bytes (DIGEST_LENGTH) -// internal + unsigned char *keyid); + +/** + * @brief Adds padding bytes to a key + * + * @param[in] ctx Context 'object' of the underlying SHA256 implementation + * @param[in] keyval The key to pad + * @param[in] kvlen The length of the key + * @param[in] pad The padding byte + */ void ccnl_hmac256_keysetup(SHA256_CTX_t *ctx, unsigned char *keyval, int kvlen, unsigned char pad); + +/** + * @brief Generates an HMAC signature + * + * @param[in] keyval The key + * @param[in] kvlen The lengthof the key + * @param[in] data The data to sign + * @param[in] dlen The length of the sign + * @param[in] md The message digest + * @param[out] mlen The length of the message digest + */ void ccnl_hmac256_sign(unsigned char *keyval, int kvlen, unsigned char *data, int dlen, unsigned char *md, int *mlen); -#ifdef NEEDS_PACKET_CRAFTING -#ifdef USE_SUITE_CCNTLV -// write Content packet *before* buf[offs], adjust offs and return bytes used +#ifdef NEEDS_PACKET_CRAFTING +#ifdef USE_SUITE_CCNTLV +/** + * @brief Signs CCNx content and prepends signature with the header + * + * @note The content is before the \p offset in \p buf. The function adjusts the + * \p offset. + * + * @param[in] name The prefix of the content to sign + * @param[in] payload The actual content + * @param[in] paylen The length of \p payload + * @param[in] lastchunknum Position of the last chunk in the \p buf + * @param[in] contentpos Position of the content in the \p buf + * @param[in] keyval The key to use for signing the content (>= 64 bytes) + * @param[in] keydigest The digest (>= 32 bytes) + * @param[out] offset TODO + * @param[out] buf A byte representation of the actual packet + * + * @return Upon success, the function returns the number of used bytes + */ int ccnl_ccntlv_prependSignedContentWithHdr(struct ccnl_prefix_s *name, unsigned char *payload, int paylen, unsigned int *lastchunknum, int *contentpos, - unsigned char *keyval, // 64B - unsigned char *keydigest, // 32B + unsigned char *keyval, + unsigned char *keydigest, int *offset, unsigned char *buf); #endif // USE_SUITE_CCNTLV #ifdef USE_SUITE_NDNTLV - +/** + * @brief Signs an NDO and prepends signature + * + * @param[in] name The prefix of the content to sign + * @param[in] payload The actual content + * @param[in] paylen The length of \p payload + * @param[in] final_block_id Denotes position of optional MetaInfo fields + * @param[in] contentpos Position of the content in the \p buf + * @param[in] keyval The key to use for signing the content (>= 64 bytes) + * @param[in] keydigest The digest (>= 32 bytes) + * @param[out] offset TODO + * @param[out] buf A byte representation of the actual packet + * + * @return Upon success, the function returns the number of used bytes + */ int ccnl_ndntlv_prependSignedContent(struct ccnl_prefix_s *name, unsigned char *payload, int paylen, unsigned int *final_block_id, int *contentpos, - unsigned char *keyval, // 64B - unsigned char *keydigest, // 32B + unsigned char *keyval, + unsigned char *keydigest, int *offset, unsigned char *buf); - #endif // USE_SUITE_NDNTLV - #endif // NEEDS_PACKET_CRAFTING #endif diff --git a/src/ccnl-utils/src/ccnl-ext-hmac.c b/src/ccnl-utils/src/ccnl-ext-hmac.c index 312c6c9b5..38ffbb8ae 100644 --- a/src/ccnl-utils/src/ccnl-ext-hmac.c +++ b/src/ccnl-utils/src/ccnl-ext-hmac.c @@ -1,31 +1,8 @@ -/* - * @f ccnl-ext-hmac.c - * @b HMAC-256 signing support - * - * Copyright (C) 2015 - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - * - * File history: - * 2015-05-08 created - */ #include "ccnl-ext-hmac.h" #ifdef USE_HMAC256 - - -// RFC2014 keyval generation +// RFC2104 keyval generation void ccnl_hmac256_keyval(unsigned char *key, int klen, unsigned char *keyval) // MUST have 64 bytes (BLOCK_LENGTH) @@ -85,7 +62,7 @@ ccnl_hmac256_keysetup(SHA256_CTX_t *ctx, unsigned char *keyval, int kvlen, ccnl_SHA256_Update(ctx, buf, sizeof(buf)); } -// RFC2014 signature generation +// RFC2104 signature generation void ccnl_hmac256_sign(unsigned char *keyval, int kvlen, unsigned char *data, int dlen,