Skip to content

Commit 1aee345

Browse files
committed
core/cf_nettle: adds provider based on Nettle low-level cryptographic library
1 parent d24f12f commit 1aee345

11 files changed

+284
-0
lines changed

CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ find_package(Botan QUIET)
1111
find_package(Crypto++ QUIET)
1212
find_package(OpenSSL QUIET)
1313
find_package(MHASH QUIET)
14+
find_package(Nettle QUIET)
1415
find_package(TomCrypt QUIET)
1516
find_package(gcrypt QUIET)
1617
find_package(Lua51 QUIET)
@@ -19,6 +20,7 @@ CMAKE_DEPENDENT_OPTION(BUILD_CF_BEECRYPT "Build cf_beecrypt interface module" ON
1920
CMAKE_DEPENDENT_OPTION(BUILD_CF_BOTAN "Build cf_botan interface module" ON "BOTAN_LIBRARIES" OFF)
2021
CMAKE_DEPENDENT_OPTION(BUILD_CF_CRYPTOPP "Build cf_cryptopp interface module" ON "CRYPTO++_LIBRARIES" OFF)
2122
CMAKE_DEPENDENT_OPTION(BUILD_CF_MHASH "Build cf_mhash interface module" ON "MHASH_LIBRARIES" OFF)
23+
CMAKE_DEPENDENT_OPTION(BUILD_CF_NETTLE "Build cf_nettle interface module" ON "NETTLE_FOUND" OFF)
2224
CMAKE_DEPENDENT_OPTION(BUILD_CF_TOMCRYPT "Build cf_tomcrypt interface module" ON "TOMCRYPT_LIBRARIES" OFF)
2325
CMAKE_DEPENDENT_OPTION(BUILD_CF_GCRYPT "Build cf_gcrypt interface module" ON "GCRYPT_LIBRARIES" OFF)
2426
CMAKE_DEPENDENT_OPTION(BUILD_CF_OPENSSL "Build cf_openssl interface module" ON "OPENSSL_LIBRARIES" OFF)
@@ -51,6 +53,10 @@ IF(BUILD_CF_MHASH)
5153
ADD_SUBDIRECTORY(cf_mhash)
5254
ENDIF(BUILD_CF_MHASH)
5355

56+
IF(BUILD_CF_NETTLE)
57+
ADD_SUBDIRECTORY(cf_nettle)
58+
ENDIF(BUILD_CF_NETTLE)
59+
5460
IF(BUILD_CF_TOMCRYPT)
5561
ADD_SUBDIRECTORY(cf_tomcrypt)
5662
ENDIF(BUILD_CF_TOMCRYPT)

LICENSE

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ This is to protect the project and its future.
4242
* cf_mhash
4343
Cryptoface interface to Mhash cryptographic hash library.
4444

45+
* cf_nettle
46+
Cryptoface interface to Nettle cryptographic library. Complicated license scheme
47+
both GPL/LGPL depending on library parts used.
48+
4549
* cf_gcrypt
4650
Cryptoface interface to the libgcrypt cryptographic library.
4751

Modules/FindNettle.cmake

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Find the native Nettle includes, library, and flags
2+
#
3+
# NETTLE_INCLUDE_DIR - where to find nettle.h, etc.
4+
# NETTLE_LIBRARIES - List of libraries when using Nettle.
5+
# NETTLE_FOUND - True if Nettle found.
6+
7+
IF (NETTLE_INCLUDE_DIR)
8+
# Already in cache, be silent
9+
SET(NETTLE_FIND_QUIETLY TRUE)
10+
ENDIF (NETTLE_INCLUDE_DIR)
11+
12+
FIND_PATH(NETTLE_INCLUDE_DIR nettle/nettle-meta.h)
13+
14+
SET(NETTLE_NAMES nettle)
15+
FIND_LIBRARY(NETTLE_LIBRARY NAMES ${NETTLE_NAMES} )
16+
17+
# handle the QUIETLY and REQUIRED arguments and set NETTLE_FOUND to TRUE if
18+
# all listed variables are TRUE
19+
INCLUDE(FindPackageHandleStandardArgs)
20+
FIND_PACKAGE_HANDLE_STANDARD_ARGS(NETTLE DEFAULT_MSG NETTLE_LIBRARY NETTLE_INCLUDE_DIR)
21+
22+
IF(NETTLE_FOUND)
23+
SET(NETTLE_LIBRARIES ${NETTLE_LIBRARY})
24+
ELSE(NETTLE_FOUND)
25+
SET(NETTLE_LIBRARIES )
26+
ENDIF(NETTLE_FOUND)
27+
28+
MARK_AS_ADVANCED(NETTLE_LIBRARY NETTLE_INCLUDE_DIR)

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ Currently exposes basic digest functionality.
6666
Cryptoface provider using [Mhash] as the underlying implementation.
6767
Currently exposes basic digest functionality.
6868

69+
### cf_nettle
70+
Cryptoface provider using [Nettle] as the underlying implementation.
71+
Currently exposes basic digest functionality.
72+
6973
### cf_gcrypt
7074
Cryptoface provider using [libgcrypt] (see [gnupg]) as the underlying implementation.
7175
Currently exposes basic digest functionality.
@@ -94,6 +98,7 @@ Subproject dependencies:
9498
* cf_cryptopp: Crypto++ - tested against 5.6.0 (Linux)
9599
* cf_openssl: OpenSSL - tested against 1.0.0a (Linux)
96100
* cf_mhash: MHash - tested against 0.9.9 (Linux and Windows)
101+
* cf_nettle: Nettle - tested against 2.0 (Linux)
97102
* cf_gcrypt: libgcrypt - tested against 1.4.6 (Linux)
98103
* cg_tomcrypt: LibTomcrypt - tested against 1.17 (Linux)
99104
* luacf: lua 5.1.x
@@ -103,6 +108,7 @@ Subproject dependencies:
103108
[Crypto++]: http://www.cryptopp.com/
104109
[OpenSSL]: http://www.openssl.org
105110
[mhash]: http://mhash.sourceforge.net/
111+
[Nettle]: http://www.lysator.liu.se/~nisse/nettle/
106112
[gnupg]: http://www.gnupg.org/
107113
[libgcrypt]: http://www.gnupg.org/documentation/manuals/gcrypt/
108114
[LibTomCrypt]: http://libtom.org/?page=features&whatfile=crypt

cf_nettle/CMakeLists.txt

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
cmake_minimum_required(VERSION 2.6)
2+
project(cf_nettle C)
3+
4+
find_package(Nettle REQUIRED)
5+
6+
SET(SOURCES
7+
src/nettle_provider.c
8+
src/nettle_digest.c
9+
src/digest_list.h
10+
)
11+
SET(HEADERS
12+
include/nettle_digest.h
13+
include/nettle_provider.h
14+
)
15+
16+
add_library(cf_nettle MODULE ${SOURCES} ${HEADERS})
17+
18+
set_target_properties(cf_nettle
19+
PROPERTIES
20+
PREFIX "")
21+
22+
target_link_libraries(cf_nettle cryptoface)
23+
24+
target_link_libraries(cf_nettle ${NETTLE_LIBRARIES})
25+
26+
include_directories(
27+
include
28+
../cryptoface/include
29+
${NETTLE_INCLUDE_DIR}
30+
)

cf_nettle/include/nettle_digest.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright (c) 2009-2011 Thomas Harning Jr.
3+
* Released under the MIT license. See included LICENSE details.
4+
*/
5+
#ifndef NETTLE_DIGEST_H_
6+
#define NETTLE_DIGEST_H_
7+
8+
#include "digest_impl.h"
9+
10+
extern struct cf_digest_ops nettle_digest_ops;
11+
12+
void cleanup_nettle_digest();
13+
14+
#endif /* NETTLE_DIGEST_H_ */

cf_nettle/include/nettle_provider.h

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2009-2011 Thomas Harning Jr.
3+
* Released under the MIT license. See included LICENSE details.
4+
*/
5+
#ifndef NETTLE_PROVIDER_H
6+
#define NETTLE_PROVIDER_H
7+
8+
#include "provider.h"
9+
10+
#ifdef WIN32
11+
#ifdef cf_nettle_EXPORTS
12+
#define CF_NETTLE_API __declspec(dllexport)
13+
#else
14+
#define CF_NETTLE_API __declspec(dllimport)
15+
#endif
16+
#else
17+
#define CF_NETTLE_API __attribute__((visibility("default"))) extern
18+
#endif
19+
20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
24+
cf_rv_t CF_NETTLE_API cf_init_loaded_provider(cf_provider_t *provider, const cf_attrs_t attrs, const char *path);
25+
26+
#ifdef __cplusplus
27+
}
28+
#endif
29+
30+
#endif /* NETTLE_PROVIDER_H */

cf_nettle/src/digest_list.h

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* TODO: GENERATE AUTOMATICALLY */
2+
/* LIST OF DIGEST */
3+
DIGEST(nettle_md2)
4+
DIGEST(nettle_md4)
5+
DIGEST(nettle_md5)
6+
DIGEST(nettle_sha1)
7+
DIGEST(nettle_sha256)

cf_nettle/src/nettle_digest.c

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright (c) 2009-2011 Thomas Harning Jr.
3+
* Released under the MIT license. See included LICENSE details.
4+
*/
5+
#include "cryptoface.h"
6+
#include "cryptoface_impl.h"
7+
#include "digest_impl.h"
8+
9+
#include <string.h> /* memcpy */
10+
#include <nettle/nettle-meta.h>
11+
12+
struct _nettle_digest {
13+
struct cf_digest digest;
14+
const struct nettle_hash *hash;
15+
char context[1];
16+
};
17+
#define NETTLE_DIGEST_STRUCT_SIZE(context_size) (sizeof (struct _nettle_digest) - 1 + context_size)
18+
19+
static cf_digest_t create_hash_container(const struct nettle_hash *hash);
20+
21+
static cf_rv_t _digest_update(cf_digest_t digest, void *data, size_t data_len) {
22+
struct _nettle_digest *impl = (struct _nettle_digest*)digest;
23+
impl->hash->update((void*)impl->context, data_len, data);
24+
return CF_S_OK;
25+
}
26+
27+
static cf_rv_t _digest_finish(cf_digest_t digest, void *output, size_t *output_len) {
28+
struct _nettle_digest *impl = (struct _nettle_digest*)digest;
29+
size_t real_len = impl->hash->digest_size;
30+
if(!output && output_len) {
31+
*output_len = real_len;
32+
return CF_S_OK;
33+
}
34+
if(!output && !output_len) {
35+
free(impl);
36+
return CF_S_OK;
37+
}
38+
if(*output_len < real_len) {
39+
*output_len = real_len;
40+
return CF_E_INSUFFICIENT_BUFFER;
41+
}
42+
*output_len = real_len;
43+
impl->hash->digest((void*)impl->context, *output_len, output);
44+
free(impl);
45+
return CF_S_OK;
46+
}
47+
48+
static cf_rv_t _digest_clone(cf_digest_t *new_digest, cf_digest_t source) {
49+
struct _nettle_digest *impl = (struct _nettle_digest*)impl;
50+
struct _nettle_digest *new_impl;
51+
*new_digest = create_hash_container(impl->hash);
52+
if (!*new_digest) {
53+
return CF_E_MEM;
54+
}
55+
new_impl = (struct _nettle_digest*)*new_digest;
56+
memcpy(new_impl->context, impl->context, impl->hash->context_size);
57+
return CF_S_OK;
58+
}
59+
60+
static struct cf_digest_instance_ops nettle_digest_instance_ops = {
61+
_digest_update,
62+
_digest_finish,
63+
_digest_clone
64+
};
65+
66+
static cf_digest_t create_hash_container(const struct nettle_hash *hash) {
67+
struct _nettle_digest *impl = (struct _nettle_digest*)calloc(1, NETTLE_DIGEST_STRUCT_SIZE(hash->context_size));
68+
if(!impl) {
69+
return NULL;
70+
}
71+
impl->hash = hash;
72+
impl->digest.ops = &nettle_digest_instance_ops;
73+
return (cf_digest_t)impl;
74+
}
75+
76+
static cf_rv_t _digest_init(cf_digest_t *digest, cf_provider_t provider, cf_digest_id_t id) {
77+
const struct nettle_hash *hash = (struct nettle_hash *)id;
78+
struct _nettle_digest *impl;
79+
*digest = create_hash_container(hash);
80+
impl = (struct _nettle_digest*)*digest;
81+
if(!impl) {
82+
return CF_E_MEM;
83+
}
84+
hash->init((void*)impl->context);
85+
return CF_S_OK;
86+
}
87+
88+
#define DIGEST(digest) (&digest),
89+
90+
static const struct nettle_hash *hash_list[] = {
91+
#include "digest_list.h"
92+
NULL
93+
};
94+
#undef DIGEST
95+
96+
/* collect information */
97+
static cf_rv_t _digest_list_begin(cf_provider_t provider, void **iter) {
98+
*iter = (void*)hash_list;
99+
return CF_S_OK;
100+
}
101+
static cf_rv_t _digest_list_next(cf_provider_t provider, void **iter, struct cf_digest_info *info) {
102+
const struct nettle_hash** hash_iter = (const struct nettle_hash**)*iter;
103+
const struct nettle_hash *hash = hash_iter ? *hash_iter : NULL;
104+
if(!hash) {
105+
return CF_S_COMPLETE;
106+
}
107+
info->id = (cf_digest_id_t)hash;
108+
info->name = hash->name;
109+
info->block_size = hash->digest_size;
110+
*iter = (void*)(hash_iter + 1);
111+
return CF_S_OK;
112+
}
113+
static cf_rv_t _digest_list_end(cf_provider_t provider, void **iter) {
114+
*iter = NULL;
115+
return CF_S_OK;
116+
}
117+
118+
struct cf_digest_ops nettle_digest_ops = {
119+
_digest_init,
120+
_digest_list_begin,
121+
_digest_list_next,
122+
_digest_list_end
123+
};

cf_nettle/src/nettle_provider.c

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2009-2011 Thomas Harning Jr.
3+
* Released under the MIT license. See included LICENSE details.
4+
*/
5+
#include "nettle_provider.h"
6+
7+
#include "cryptoface.h"
8+
#include "cryptoface_impl.h"
9+
#include "nettle_digest.h"
10+
11+
12+
static cf_rv_t _nettle_destroy_provider(cf_provider_t provider);
13+
14+
static struct cf_provider_ops nettle_provider_ops = {
15+
_nettle_destroy_provider
16+
};
17+
18+
static cf_rv_t _nettle_init_provider(cf_provider_t *provider, const cf_attrs_t attrs, const char *path) {
19+
*provider = (cf_provider_t)calloc(1, sizeof(**provider));
20+
if(!*provider) {
21+
return CF_E_MEM;
22+
}
23+
(*provider)->provider_ops = nettle_provider_ops;
24+
(*provider)->digest_ops = nettle_digest_ops;
25+
return CF_S_OK;
26+
}
27+
static cf_rv_t _nettle_destroy_provider(cf_provider_t provider) {
28+
free(provider);
29+
return CF_S_OK;
30+
}
31+
32+
cf_rv_t cf_init_loaded_provider(cf_provider_t *provider, const cf_attrs_t attrs, const char *path) {
33+
return _nettle_init_provider(provider, attrs, path);
34+
}

cf_test/src/cf_test.c

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const char *paths[] = {
1414
".\\cf_botan.dll",
1515
".\\cf_cryptopp.dll",
1616
".\\cf_mhash.dll",
17+
".\\cf_nettle.dll",
1718
".\\cf_openssl.dll",
1819
".\\cf_tomcrypt.dll",
1920
".\\cf_gcrypt.dll",
@@ -25,6 +26,7 @@ const char *paths[] = {
2526
"./cf_botan.so",
2627
"./cf_cryptopp.so",
2728
"./cf_mhash.so",
29+
"./cf_nettle.so",
2830
"./cf_openssl.so",
2931
"./cf_tomcrypt.so",
3032
"./cf_gcrypt.so",

0 commit comments

Comments
 (0)