[PATCH 2/5] Add a function to retrieve algorithm used by MAC handler

Dmitry Eremin-Solenikov dbaryshkov at gmail.com
Mon Dec 30 21:39:55 CET 2013


* cipher/mac.c (_gcry_mac_get_algo): New function, returns used algo.
* src/visibility.c (gcry_mac_get_algo): New wrapper.
* src/visibility.h: Hanlde gcry_mac_get_algo.
* src/gcrypt-int.h (_gcry_mac_get_algo): New.
* src/gcrypt.h.in (gcry_mac_get_algo): New.
* src/libgcrypt.def (gcry_mac_get_algo): New.
* src/libgcrypt.vers (gcry_mac_get_algo): New.
* doc/gcrypt.texi: Document gcry_mac_get_algo.
* tests/basic.c (check_one_mac): Verify gcry_mac_get_algo.

Signed-off-by: Dmitry Eremin-Solenikov <dbaryshkov at gmail.com>
---
 cipher/mac.c       |  7 +++++++
 doc/gcrypt.texi    | 10 ++++++++++
 src/gcrypt-int.h   |  1 +
 src/gcrypt.h.in    |  3 +++
 src/libgcrypt.def  |  1 +
 src/libgcrypt.vers |  2 +-
 src/visibility.c   |  6 ++++++
 src/visibility.h   |  2 ++
 tests/basic.c      |  6 ++++++
 9 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/cipher/mac.c b/cipher/mac.c
index 3daf2ee..7805467 100644
--- a/cipher/mac.c
+++ b/cipher/mac.c
@@ -369,6 +369,13 @@ _gcry_mac_verify (gcry_mac_hd_t hd, const void *buf, size_t buflen)
 }
 
 
+int
+_gcry_mac_get_algo (gcry_mac_hd_t hd)
+{
+  return hd->algo;
+}
+
+
 unsigned int
 _gcry_mac_get_algo_maclen (int algo)
 {
diff --git a/doc/gcrypt.texi b/doc/gcrypt.texi
index 37f5a8e..e2495c4 100644
--- a/doc/gcrypt.texi
+++ b/doc/gcrypt.texi
@@ -3720,6 +3720,16 @@ the MAC calculated in object @var{h}.
 @end deftypefun
 
 
+In some situations it might be hard to remember the algorithm used for
+the MAC calculation. The following function might be used to get that
+information:
+
+ at deftypefun {int} gcry_mac_get_algo (gcry_mac_hd_t @var{h})
+
+Retrieve the algorithm used with the handle @var{h}.
+ at end deftypefun
+
+
 @c ***********************************
 @c ***** MAC info functions **********
 @c ***********************************
diff --git a/src/gcrypt-int.h b/src/gcrypt-int.h
index 65dcb4d..8a6df84 100644
--- a/src/gcrypt-int.h
+++ b/src/gcrypt-int.h
@@ -180,6 +180,7 @@ gpg_err_code_t _gcry_mac_write (gcry_mac_hd_t hd, const void *buffer,
 gpg_err_code_t _gcry_mac_read (gcry_mac_hd_t hd, void *buffer, size_t *buflen);
 gpg_err_code_t _gcry_mac_verify (gcry_mac_hd_t hd, const void *buffer,
                                  size_t buflen);
+int _gcry_mac_get_algo (gcry_mac_hd_t hd);
 unsigned int _gcry_mac_get_algo_maclen (int algo);
 unsigned int _gcry_mac_get_algo_keylen (int algo);
 const char *_gcry_mac_algo_name (int algorithm) _GCRY_GCC_ATTR_PURE;
diff --git a/src/gcrypt.h.in b/src/gcrypt.h.in
index 5c771e5..402e4fd 100644
--- a/src/gcrypt.h.in
+++ b/src/gcrypt.h.in
@@ -1382,6 +1382,9 @@ gcry_error_t gcry_mac_read (gcry_mac_hd_t hd, void *buffer, size_t *buflen);
 gcry_error_t gcry_mac_verify (gcry_mac_hd_t hd, const void *buffer,
                               size_t buflen);
 
+/* Retrieve the algorithm used with MAC. */
+int gcry_mac_get_algo (gcry_mac_hd_t hd);
+
 /* Retrieve the length in bytes of the MAC yielded by algorithm ALGO. */
 unsigned int gcry_mac_get_algo_maclen (int algo);
 
diff --git a/src/libgcrypt.def b/src/libgcrypt.def
index a90efce..57ed490 100644
--- a/src/libgcrypt.def
+++ b/src/libgcrypt.def
@@ -274,6 +274,7 @@ EXPORTS
       gcry_mac_read             @240
       gcry_mac_verify           @241
       gcry_mac_ctl              @242
+      gcry_mac_get_algo         @243
 
 
 ;; end of file with public symbols for Windows.
diff --git a/src/libgcrypt.vers b/src/libgcrypt.vers
index 5118c81..7ee0541 100644
--- a/src/libgcrypt.vers
+++ b/src/libgcrypt.vers
@@ -54,7 +54,7 @@ GCRYPT_1.6 {
     gcry_cipher_authenticate; gcry_cipher_gettag; gcry_cipher_checktag;
 
     gcry_mac_algo_info; gcry_mac_algo_name; gcry_mac_map_name;
-    gcry_mac_get_algo_maclen; gcry_mac_get_algo_keylen;
+    gcry_mac_get_algo_maclen; gcry_mac_get_algo_keylen; gcry_mac_get_algo;
     gcry_mac_open; gcry_mac_close; gcry_mac_setkey; gcry_mac_setiv;
     gcry_mac_write; gcry_mac_read; gcry_mac_verify; gcry_mac_ctl;
 
diff --git a/src/visibility.c b/src/visibility.c
index 2989498..6ed57ca 100644
--- a/src/visibility.c
+++ b/src/visibility.c
@@ -855,6 +855,12 @@ gcry_mac_map_name (const char *string)
   return _gcry_mac_map_name (string);
 }
 
+int
+gcry_mac_get_algo (gcry_mac_hd_t hd)
+{
+  return _gcry_mac_get_algo (hd);
+}
+
 unsigned int
 gcry_mac_get_algo_maclen (int algo)
 {
diff --git a/src/visibility.h b/src/visibility.h
index 4127a43..96b5235 100644
--- a/src/visibility.h
+++ b/src/visibility.h
@@ -137,6 +137,7 @@ MARK_VISIBLEX (gcry_cipher_open)
 MARK_VISIBLEX (gcry_mac_algo_info)
 MARK_VISIBLEX (gcry_mac_algo_name)
 MARK_VISIBLEX (gcry_mac_map_name)
+MARK_VISIBLEX (gcry_mac_get_algo)
 MARK_VISIBLEX (gcry_mac_get_algo_maclen)
 MARK_VISIBLEX (gcry_mac_get_algo_keylen)
 MARK_VISIBLEX (gcry_mac_open)
@@ -380,6 +381,7 @@ MARK_VISIBLEX (_gcry_mpi_get_const)
 #define gcry_mac_algo_info          _gcry_USE_THE_UNDERSCORED_FUNCTION
 #define gcry_mac_algo_name          _gcry_USE_THE_UNDERSCORED_FUNCTION
 #define gcry_mac_map_name           _gcry_USE_THE_UNDERSCORED_FUNCTION
+#define gcry_mac_get_algo           _gcry_USE_THE_UNDERSCORED_FUNCTION
 #define gcry_mac_get_algo_maclen    _gcry_USE_THE_UNDERSCORED_FUNCTION
 #define gcry_mac_get_algo_keylen    _gcry_USE_THE_UNDERSCORED_FUNCTION
 #define gcry_mac_open               _gcry_USE_THE_UNDERSCORED_FUNCTION
diff --git a/tests/basic.c b/tests/basic.c
index 055b7b6..a127f93 100644
--- a/tests/basic.c
+++ b/tests/basic.c
@@ -4469,6 +4469,12 @@ check_one_mac (int algo, const char *data, int datalen,
       return;
     }
 
+  i = gcry_mac_get_algo (hd);
+  if (i != algo)
+    {
+      fail ("algo %d, gcry_mac_get_algo failed: %d\n", algo, i);
+    }
+
   maclen = gcry_mac_get_algo_maclen (algo);
   if (maclen < 1 || maclen > 500)
     {
-- 
1.8.5.2




More information about the Gcrypt-devel mailing list