]> www.wagner.pp.ru Git - openssl-gost/engine.git/commitdiff
Fix possible overflow of digest result writing
authorVitaly Chikunov <vt@altlinux.org>
Thu, 2 Aug 2018 13:59:14 +0000 (16:59 +0300)
committerVitaly Chikunov <vt@altlinux.org>
Wed, 8 Aug 2018 18:15:23 +0000 (21:15 +0300)
Openssl is already have output result size in EVP_MD.md_size
We should not exceed its value when writing digest output.
This should be fixed more consistently, probably, by removing
dgst_size from OMAC_CTX.

compat.h
gost_omac.c

index 91afcf5125f5de72417b972c3fa01d001b5a8e9c..29a2ad3270ef71d159ba39f2de337b5a9cd4f630 100644 (file)
--- a/compat.h
+++ b/compat.h
@@ -252,6 +252,11 @@ static inline int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
     return 1;
 }
 
+static int EVP_MD_meth_get_result_size(const EVP_MD *md)
+{
+    return md->md_size;
+}
+
 static inline int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
 {
     md->block_size = blocksize;
index af6eb2a9e9e6e9cf2260754db5c7b147b2c912d8..d1f897a15c2227b8936e51625eeb73f818b433a3 100644 (file)
@@ -7,6 +7,8 @@
 #include "e_gost_err.h"
 #include "gost_lcl.h"
 
+#define min(a,b) (((a) < (b)) ? (a) : (b))
+
 typedef struct omac_ctx {
        CMAC_CTX *cmac_ctx;
        size_t   dgst_size;
@@ -71,7 +73,8 @@ int omac_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
 
                CMAC_Final(c->cmac_ctx, mac, &mac_size);
 
-    memcpy(md, mac, c->dgst_size);
+    int md_size = EVP_MD_meth_get_result_size(EVP_MD_CTX_md(ctx));
+    memcpy(md, mac, min(md_size, c->dgst_size));
     return 1;
 }