Skip to content
This repository
Browse code

use libgcrypt instead of OpenSSL

commit a09ba40dbcb835444b695525fa16f148e929a27a 1 parent 5cba612
Nestal Wan authored June 05, 2012
50  cmake/Modules/FindLibGcrypt.cmake
... ...
@@ -0,0 +1,50 @@
  1
+
  2
+# - Try to find the Gcrypt library
  3
+# Once run this will define
  4
+#
  5
+#  LIBGCRYPT_FOUND - set if the system has the gcrypt library
  6
+#  LIBGCRYPT_CFLAGS - the required gcrypt compilation flags
  7
+#  LIBGCRYPT_LIBRARIES - the linker libraries needed to use the gcrypt library
  8
+#
  9
+# Copyright (c) 2006 Brad Hards <bradh@kde.org>
  10
+#
  11
+# Redistribution and use is allowed according to the terms of the BSD license.
  12
+# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
  13
+
  14
+# libgcrypt is moving to pkg-config, but earlier version don't have it
  15
+
  16
+#search in typical paths for libgcrypt-config
  17
+FIND_PROGRAM(LIBGCRYPTCONFIG_EXECUTABLE NAMES libgcrypt-config)
  18
+
  19
+#reset variables
  20
+set(LIBGCRYPT_LIBRARIES)
  21
+set(LIBGCRYPT_CFLAGS)
  22
+
  23
+# if libgcrypt-config has been found
  24
+IF(LIBGCRYPTCONFIG_EXECUTABLE)
  25
+
  26
+  EXEC_PROGRAM(${LIBGCRYPTCONFIG_EXECUTABLE} ARGS --libs RETURN_VALUE _return_VALUE OUTPUT_VARIABLE LIBGCRYPT_LIBRARIES)
  27
+
  28
+  EXEC_PROGRAM(${LIBGCRYPTCONFIG_EXECUTABLE} ARGS --cflags RETURN_VALUE _return_VALUE OUTPUT_VARIABLE LIBGCRYPT_CFLAGS)
  29
+
  30
+  IF(${LIBGCRYPT_CFLAGS} MATCHES "\n")
  31
+    SET(LIBGCRYPT_CFLAGS " ")
  32
+  ENDIF(${LIBGCRYPT_CFLAGS} MATCHES "\n")
  33
+
  34
+  IF(LIBGCRYPT_LIBRARIES AND LIBGCRYPT_CFLAGS)
  35
+    SET(LIBGCRYPT_FOUND TRUE)
  36
+  ENDIF(LIBGCRYPT_LIBRARIES AND LIBGCRYPT_CFLAGS)
  37
+
  38
+ENDIF(LIBGCRYPTCONFIG_EXECUTABLE)
  39
+
  40
+if (LIBGCRYPT_FOUND)
  41
+   if (NOT LibGcrypt_FIND_QUIETLY)
  42
+      message(STATUS "Found libgcrypt: ${LIBGCRYPT_LIBRARIES}")
  43
+   endif (NOT LibGcrypt_FIND_QUIETLY)
  44
+else (LIBGCRYPT_FOUND)
  45
+   if (LibGcrypt_FIND_REQUIRED)
  46
+      message(FATAL_ERROR "Could not find libgcrypt libraries")
  47
+   endif (LibGcrypt_FIND_REQUIRED)
  48
+endif (LIBGCRYPT_FOUND)
  49
+
  50
+MARK_AS_ADVANCED(LIBGCRYPT_CFLAGS LIBGCRYPT_LIBRARIES)
17  grive/src/main.cc
@@ -32,6 +32,8 @@
32 32
 
33 33
 #include <boost/exception/all.hpp>
34 34
 
  35
+#include <gcrypt.h>
  36
+
35 37
 #include <cassert>
36 38
 #include <cstdlib>
37 39
 #include <iostream>
@@ -40,10 +42,25 @@
40 42
 const std::string client_id		= "22314510474.apps.googleusercontent.com" ;
41 43
 const std::string client_secret	= "bl4ufi89h-9MkFlypcI7R785" ;
42 44
 
  45
+// libgcrypt insist this to be done in application, not library
  46
+void InitGCrypt()
  47
+{
  48
+	if ( !gcry_check_version(GCRYPT_VERSION) )
  49
+		throw gr::Exception() << gr::expt::ErrMsg( "libgcrypt version mismatch" ) ;
  50
+
  51
+	// disable secure memory
  52
+	gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
  53
+
  54
+	// tell Libgcrypt that initialization has completed
  55
+	gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
  56
+}
  57
+
43 58
 int main( int argc, char **argv )
44 59
 {
45 60
 	using namespace gr ;
46 61
 
  62
+	InitGCrypt() ;
  63
+	
47 64
 	Config config ;
48 65
 	
49 66
 	std::auto_ptr<log::CompositeLog> comp_log(new log::CompositeLog) ;
4  libgrive/CMakeLists.txt
@@ -2,7 +2,7 @@ project(libgrive)
2 2
 
3 3
 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
4 4
 
5  
-find_package(OpenSSL REQUIRED)
  5
+find_package(LibGcrypt REQUIRED)
6 6
 find_package(JSONC REQUIRED)
7 7
 find_package(CURL REQUIRED)
8 8
 find_package(EXPAT REQUIRED)
@@ -73,7 +73,7 @@ add_library( grive SHARED ${LIBGRIVE_SRC} ${OPT_SRC} )
73 73
 target_link_libraries( grive
74 74
 	${CURL_LIBRARIES}
75 75
 	${JSONC_LIBRARY}
76  
-	${OPENSSL_LIBRARIES}
  76
+	${LIBGCRYPT_LIBRARIES}
77 77
 	${GDBM_LIBRARIES}
78 78
 	${Boost_LIBRARIES}
79 79
 	${IBERTY_LIBRARY}
27  libgrive/src/util/Crypt.cc
@@ -20,12 +20,14 @@
20 20
 #include "Crypt.hh"
21 21
 
22 22
 #include "StdioFile.hh"
  23
+#include "Exception.hh"
23 24
 
24 25
 #include <iomanip>
25 26
 #include <sstream>
26 27
 
27 28
 // dependent libraries
28  
-#include <openssl/evp.h>
  29
+#include <gcrypt.h>
  30
+#include <boost/throw_exception.hpp>
29 31
 
30 32
 namespace gr { namespace crypt {
31 33
 
@@ -47,23 +49,28 @@ std::string MD5( const fs::path& file )
47 49
 std::string MD5( StdioFile& file )
48 50
 {
49 51
 	char buf[read_size] ;
50  
-	EVP_MD_CTX	md ;
51  
-	EVP_MD_CTX_init( &md );
52  
-	EVP_DigestInit_ex( &md, EVP_md5(), 0 ) ;
  52
+	
  53
+	gcry_md_hd_t hd ;
  54
+	gcry_error_t err = gcry_md_open( &hd, GCRY_MD_MD5, GCRY_MD_FLAG_HMAC ) ;
  55
+	if ( err != GPG_ERR_NO_ERROR )
  56
+	{
  57
+		BOOST_THROW_EXCEPTION( Exception() << expt::ErrMsg( gcry_strerror(err) ) ) ;
  58
+	}
53 59
 	
54 60
 	std::size_t count = 0 ;
55 61
 	while ( (count = file.Read( buf, sizeof(buf) )) > 0 )
56  
-		EVP_DigestUpdate( &md, buf, count ) ;
57  
-	
58  
-	unsigned int md5_size = EVP_MAX_MD_SIZE ;
59  
-	unsigned char md5[EVP_MAX_MD_SIZE] ;
60  
-	EVP_DigestFinal_ex( &md, md5, &md5_size ) ;
  62
+		gcry_md_write( hd, buf, count ) ;
  63
+	gcry_md_final(hd) ;
  64
+
  65
+	unsigned char *md5 = gcry_md_read( hd, GCRY_MD_MD5 ) ;
61 66
 	
62 67
 	// format the MD5 string
63 68
 	std::ostringstream ss ;
64  
-	for ( unsigned int i = 0 ; i < md5_size ; i++ )
  69
+	for ( unsigned int i = 0 ; i < gcry_md_get_algo_dlen(GCRY_MD_MD5) ; i++ )
65 70
 		ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(md5[i]) ;
66 71
 	
  72
+	gcry_md_close( hd ) ;
  73
+	
67 74
 	return ss.str() ;
68 75
 }
69 76
 

0 notes on commit a09ba40

Please sign in to comment.
Something went wrong with that request. Please try again.