diff --git a/Makefile.am b/Makefile.am
--- a/Makefile.am
+++ b/Makefile.am
@@ -79,3 +79,25 @@
 
 stowinstall:
 	$(MAKE) $(AM_MAKEFLAGS) install prefix=/usr/local/stow/libksba
+
+# Coverage targets
+if HAVE_GCOV
+
+.PHONY: clean-coverage
+clean-coverage:
+	@echo Removing old coverage results
+	-find -name '*.gcda' -print | xargs -r rm
+	-find -name '*.gcno' -print | xargs -r rm
+	-rm -rf coverage.info coveragereport
+
+.PHONY: coverage-html clean-coverage
+coverage-html: check
+	@echo Collecting coverage data with lcov
+	$(top_srcdir)/autogen.sh --coverage $(LCOV) $(GCOV) $(abs_builddir) \
+		tests src
+	$(top_srcdir)/autogen.sh --report $(LCOV) $(GENHTML) $(abs_builddir) \
+		'*/tests/*' '*/asn1-parse.c' '*/asn1-parse.y'
+
+clean-local: clean-coverage
+
+endif # HAVE_GCOV
diff --git a/autogen.sh b/autogen.sh
--- a/autogen.sh
+++ b/autogen.sh
@@ -171,6 +171,14 @@
         fatal "**Error**: invalid build option $1"
         shift
         ;;
+    --coverage)
+        myhost="coverage"
+        shift
+        ;;
+    --report)
+        myhost="report"
+        shift
+        ;;
     *)
         ;;
 esac
@@ -239,6 +247,62 @@
 fi
 # **** end FIND VERSION ****
 
+# **** COVERAGE ****
+# This is a helper for the code coverage collection
+# Called
+#   ./autogen.sh --coverage lcov gcov [dirs...]
+if [ "$myhost" = "coverage" ]; then
+  LCOV="$1"
+  GCOV="$2"
+  BASE=`realpath "$3"`
+  COVINFO=""
+
+  shift 3
+
+  while [ x"$1" != "x" ]; do
+    P="$1"
+    $LCOV --gcov-tool $GCOV \
+          --base-directory "$BASE"/"$P" \
+          --directory "$P" \
+          --output-file coverage.info.`basename "$P"` \
+          --capture --no-checksum --compat-libtool \
+          --rc lcov_branch_coverage=1
+    COVINFO="$COVINFO -a coverage.info."`basename "$P"`
+    shift
+  done
+
+  $LCOV $COVINFO --base-directory "$BASE" --output-file coverage.info \
+          --no-checksum \
+          --rc lcov_branch_coverage=1
+
+  exit 0
+fi
+# **** end COVERAGE ****
+
+# **** COVERAGE_REPORT ****
+# This is a helper for the code coverage report
+# Called
+#   ./autogen.sh --report lcov genhtml [exclude...]
+if [ "$myhost" = "report" ]; then
+  LCOV="$1"
+  GENHTML="$2"
+
+  shift 2
+
+  while [ x"$1" != "x" ]; do
+    P="$1"
+    $LCOV --remove coverage.info "$P" -o coverage.info \
+          --rc lcov_branch_coverage=1
+    shift
+  done
+
+  LANG=C $GENHTML --output-directory coveragereport --title "Code Coverage" \
+                  --legend --show-details coverage.info \
+                  --rc lcov_branch_coverage=1
+
+  exit 0
+fi
+# **** end COVERAGE_REPORT ****
 
 if [ ! -f "$tsdir/build-aux/config.guess" ]; then
     fatal "$tsdir/build-aux/config.guess not found"
diff --git a/configure.ac b/configure.ac
--- a/configure.ac
+++ b/configure.ac
@@ -127,6 +127,14 @@
 AC_PROG_YACC
 AX_PROG_BISON([have_bison=yes],[have_bison=no])
 
+if test "$USE_MAINTAINER_MODE" = "yes"; then
+	# gcov coverage reporting
+	AC_CHECK_PROGS(GCOV, [gcov], gcov)
+	AC_TDD_GCOV
+	AC_SUBST(COVERAGE_CFLAGS)
+	AC_SUBST(COVERAGE_LDFLAGS)
+fi
+
 AC_C_INLINE
 
 # We need to compile and run a program on the build machine.
diff --git a/m4/Makefile.am b/m4/Makefile.am
--- a/m4/Makefile.am
+++ b/m4/Makefile.am
@@ -1,5 +1,5 @@
 
 EXTRA_DIST = autobuild.m4  gnupg-typedef.m4  gpg-error.m4  libgcrypt.m4 \
-             libtool.m4 ax_prog_bison.m4
+             libtool.m4 ax_prog_bison.m4 gcov.m4
 
 
diff --git a/m4/gcov.m4 b/m4/gcov.m4
new file mode 100644
--- /dev/null
+++ b/m4/gcov.m4
@@ -0,0 +1,93 @@
+# Copyright 2012 Canonical Ltd.
+#
+# This program is free software: you can redistribute it and/or modify it
+# under the terms of the GNU General Public License version 3, as published
+# by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranties of
+# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+# PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Checks for existence of coverage tools:
+#  * gcov
+#  * lcov
+#  * genhtml
+#  * gcovr
+#
+# Sets ac_cv_check_gcov to yes if tooling is present
+# and reports the executables to the variables LCOV, GCOVR and GENHTML.
+AC_DEFUN([AC_TDD_GCOV],
+[
+  AC_ARG_ENABLE(gcov,
+  AS_HELP_STRING([--enable-gcov],
+		 [enable coverage testing with gcov]),
+  [use_gcov=yes], [use_gcov=no])
+
+  AM_CONDITIONAL(HAVE_GCOV, test "x$use_gcov" = "xyes")
+
+  if test "x$use_gcov" = "xyes"; then
+  # we need gcc:
+  if test "$GCC" != "yes"; then
+    AC_MSG_ERROR([GCC is required for --enable-gcov])
+  fi
+
+  # Check if ccache is being used
+  AC_CHECK_PROG(SHTOOL, shtool, shtool)
+  if test "$SHTOOL"; then
+    AS_CASE([`$SHTOOL path $CC`],
+                [*ccache*], [gcc_ccache=yes],
+                [gcc_ccache=no])
+  fi
+
+  if test "$gcc_ccache" = "yes" && (test -z "$CCACHE_DISABLE" || test "$CCACHE_DISABLE" != "1"); then
+    AC_MSG_ERROR([ccache must be disabled when --enable-gcov option is used. You can disable ccache by setting environment variable CCACHE_DISABLE=1.])
+  fi
+
+  lcov_version_list="1.6 1.7 1.8 1.9 1.10 1.11 1.12 1.13"
+  AC_CHECK_PROG(LCOV, lcov, lcov)
+  AC_CHECK_PROG(GENHTML, genhtml, genhtml)
+
+  if test "$LCOV"; then
+    AC_CACHE_CHECK([for lcov version], glib_cv_lcov_version, [
+      glib_cv_lcov_version=invalid
+      lcov_version=`$LCOV -v 2>/dev/null | $SED -e 's/^.* //'`
+      for lcov_check_version in $lcov_version_list; do
+        if test "$lcov_version" = "$lcov_check_version"; then
+          glib_cv_lcov_version="$lcov_check_version (ok)"
+        fi
+      done
+    ])
+  else
+    lcov_msg="To enable code coverage reporting you must have one of the following lcov versions installed: $lcov_version_list"
+    AC_MSG_ERROR([$lcov_msg])
+  fi
+
+  case $glib_cv_lcov_version in
+    ""|invalid[)]
+      lcov_msg="You must have one of the following versions of lcov: $lcov_version_list (found: $lcov_version)."
+      AC_MSG_ERROR([$lcov_msg])
+      LCOV="exit 0;"
+      ;;
+  esac
+
+  if test -z "$GENHTML"; then
+    AC_MSG_ERROR([Could not find genhtml from the lcov package])
+  fi
+
+  # Remove all optimization flags from CFLAGS
+  changequote({,})
+  CFLAGS=`echo "$CFLAGS" | $SED -e 's/-O[0-9]*//g'`
+  CPPFLAGS=`echo "$CPPFLAGS" | $SED -e 's/-O[0-9]*//g'`
+  changequote([,])
+
+  # Add the special gcc flags
+  COVERAGE_CFLAGS="--coverage -DDEBUG"
+  COVERAGE_CXXFLAGS="--coverage -DDEBUG"
+  COVERAGE_LDFLAGS="--coverage -lgcov"
+
+fi
+]) # AC_TDD_GCOV
diff --git a/src/Makefile.am b/src/Makefile.am
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -36,7 +36,7 @@
 DISTCLEANFILES = asn1-tables.c
 
 AM_CPPFLAGS =  -I$(top_builddir)/gl -I$(top_srcdir)/gl
-AM_CFLAGS = $(GPG_ERROR_CFLAGS)
+AM_CFLAGS = $(GPG_ERROR_CFLAGS) $(COVERAGE_CFLAGS)
 
 
 if HAVE_LD_VERSION_SCRIPT
@@ -82,7 +82,8 @@
 
 libksba_la_LDFLAGS = $(no_undefined) $(export_symbols) $(extra_ltoptions) \
       $(libksba_version_script_cmd) -version-info \
-      @LIBKSBA_LT_CURRENT@:@LIBKSBA_LT_REVISION@:@LIBKSBA_LT_AGE@
+      @LIBKSBA_LT_CURRENT@:@LIBKSBA_LT_REVISION@:@LIBKSBA_LT_AGE@ \
+      $(COVERAGE_LDFLAGS)
 libksba_la_INCLUDES = -I$(top_srcdir)/lib
 libksba_la_DEPENDENCIES = $(srcdir)/libksba.vers $(ksba_deps)
 libksba_la_LIBADD = $(ksba_res) @LTLIBOBJS@ @GPG_ERROR_LIBS@
diff --git a/tests/Makefile.am b/tests/Makefile.am
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -41,8 +41,8 @@
 
 TESTS = cert-basic t-crl-parser t-dnparser t-oid t-reader
 
-AM_CFLAGS = $(GPG_ERROR_CFLAGS)
-AM_LDFLAGS = -no-install
+AM_CFLAGS = $(GPG_ERROR_CFLAGS) $(COVERAGE_CFLAGS)
+AM_LDFLAGS = -no-install $(COVERAGE_LDFLAGS)
 
 noinst_HEADERS = t-common.h
 noinst_PROGRAMS = $(TESTS) t-cms-parser t-crl-parser t-dnparser t-ocsp