Adapt the provider digests for more use of OSSL_PARAM
authorRichard Levitte <levitte@openssl.org>
Tue, 16 Jul 2019 04:06:26 +0000 (06:06 +0200)
committerRichard Levitte <levitte@openssl.org>
Mon, 12 Aug 2019 11:35:18 +0000 (13:35 +0200)
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9391)

12 files changed:
providers/common/digests/sha2_prov.c
providers/common/digests/sha3_prov.c
providers/default/digests/blake2_prov.c
providers/default/digests/md5_prov.c
providers/default/digests/md5_sha1_prov.c
providers/default/digests/null_prov.c
providers/default/digests/sm3_prov.c
providers/legacy/digests/md2_prov.c
providers/legacy/digests/md4_prov.c
providers/legacy/digests/mdc2_prov.c
providers/legacy/digests/ripemd_prov.c
providers/legacy/digests/wp_prov.c

index 0e49ae679c74e065b1aa50bb4d198d7cbfe4b4f7..e823c27039c9f47d4005eb700ab382479947dc3e 100644 (file)
@@ -9,6 +9,7 @@
 
 #include <openssl/crypto.h>
 #include <openssl/core_numbers.h>
+#include <openssl/evp.h>
 #include <openssl/sha.h>
 #include <openssl/evp.h>
 #include <openssl/params.h>
@@ -17,7 +18,7 @@
 #include "internal/provider_algs.h"
 #include "internal/sha.h"
 
-static OSSL_OP_digest_set_params_fn sha1_set_params;
+static OSSL_OP_digest_ctx_set_params_fn sha1_set_params;
 
 /* Special set_params method for SSL3 */
 static int sha1_set_params(void *vctx, const OSSL_PARAM params[])
@@ -36,30 +37,37 @@ static int sha1_set_params(void *vctx, const OSSL_PARAM params[])
 
 OSSL_FUNC_DIGEST_CONSTRUCT_PARAMS(sha1, SHA_CTX,
                            SHA_CBLOCK, SHA_DIGEST_LENGTH,
+                           EVP_MD_FLAG_DIGALGID_ABSENT,
                            SHA1_Init, SHA1_Update, SHA1_Final,
                            sha1_set_params)
 
 OSSL_FUNC_DIGEST_CONSTRUCT(sha224, SHA256_CTX,
                            SHA256_CBLOCK, SHA224_DIGEST_LENGTH,
+                           EVP_MD_FLAG_DIGALGID_ABSENT,
                            SHA224_Init, SHA224_Update, SHA224_Final)
 
 OSSL_FUNC_DIGEST_CONSTRUCT(sha256, SHA256_CTX,
                            SHA256_CBLOCK, SHA256_DIGEST_LENGTH,
+                           EVP_MD_FLAG_DIGALGID_ABSENT,
                            SHA256_Init, SHA256_Update, SHA256_Final)
 
 OSSL_FUNC_DIGEST_CONSTRUCT(sha384, SHA512_CTX,
                            SHA512_CBLOCK, SHA384_DIGEST_LENGTH,
+                           EVP_MD_FLAG_DIGALGID_ABSENT,
                            SHA384_Init, SHA384_Update, SHA384_Final)
 
 OSSL_FUNC_DIGEST_CONSTRUCT(sha512, SHA512_CTX,
                            SHA512_CBLOCK, SHA512_DIGEST_LENGTH,
+                           EVP_MD_FLAG_DIGALGID_ABSENT,
                            SHA512_Init, SHA512_Update, SHA512_Final)
 
 OSSL_FUNC_DIGEST_CONSTRUCT(sha512_224, SHA512_CTX,
                            SHA512_CBLOCK, SHA224_DIGEST_LENGTH,
+                           EVP_MD_FLAG_DIGALGID_ABSENT,
                            sha512_224_init, SHA512_Update, SHA512_Final)
 
 OSSL_FUNC_DIGEST_CONSTRUCT(sha512_256, SHA512_CTX,
                            SHA512_CBLOCK, SHA256_DIGEST_LENGTH,
+                           EVP_MD_FLAG_DIGALGID_ABSENT,
                            sha512_256_init, SHA512_Update, SHA512_Final)
 
index 15cd670a0efd8580970978a93e9ad1637f229f6c..63a57dbcb321d9bb9cc256b8340470edae5744c7 100644 (file)
@@ -26,7 +26,7 @@ static OSSL_OP_digest_update_fn keccak_update;
 static OSSL_OP_digest_final_fn keccak_final;
 static OSSL_OP_digest_freectx_fn keccak_freectx;
 static OSSL_OP_digest_dupctx_fn keccak_dupctx;
-static OSSL_OP_digest_set_params_fn shake_set_params;
+static OSSL_OP_digest_ctx_set_params_fn shake_set_params;
 static sha3_absorb_fn generic_sha3_absorb;
 static sha3_final_fn generic_sha3_final;
 
@@ -202,27 +202,33 @@ static void *uname##_newctx(void *provctx) \
     return ctx; \
 }
 
-#define OSSL_FUNC_SHA3_DIGEST(name, bitlen, dgstsize, stparams) \
-static OSSL_OP_digest_size_fn name##_size; \
-static OSSL_OP_digest_block_size_fn name##_block_size; \
-static size_t name##_block_size(void) \
-{ \
-    return SHA3_BLOCKSIZE(bitlen); \
-} \
-static size_t name##_size(void) \
-{ \
-    return dgstsize; \
-} \
-const OSSL_DISPATCH name##_functions[] = { \
-    { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx }, \
-    { OSSL_FUNC_DIGEST_INIT, (void (*)(void))keccak_init }, \
-    { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))keccak_update }, \
-    { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))keccak_final }, \
-    { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))keccak_freectx }, \
-    { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))keccak_dupctx }, \
-    { OSSL_FUNC_DIGEST_SIZE, (void (*)(void))name##_size }, \
-    { OSSL_FUNC_DIGEST_BLOCK_SIZE, (void (*)(void))name##_block_size }, \
-    { OSSL_FUNC_DIGEST_SET_PARAMS, (void (*)(void))stparams },\
+#define OSSL_FUNC_SHA3_DIGEST(name, bitlen, blksize, dgstsize, flags,   \
+                              stparams)                                 \
+static OSSL_OP_digest_get_params_fn name##_get_params;                  \
+static int name##_get_params(OSSL_PARAM params[])                       \
+{                                                                       \
+    OSSL_PARAM *p = NULL;                                               \
+                                                                        \
+    p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_BLOCK_SIZE);        \
+    if (p != NULL && !OSSL_PARAM_set_int(p, (blksize)))                 \
+        return 0;                                                       \
+    p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE);              \
+    if (p != NULL && !OSSL_PARAM_set_int(p, (dgstsize)))                \
+        return 0;                                                       \
+    p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_FLAGS);             \
+    if (p != NULL && !OSSL_PARAM_set_ulong(p, (flags)))                 \
+        return 0;                                                       \
+    return 1;                                                           \
+}                                                                       \
+const OSSL_DISPATCH name##_functions[] = {                              \
+    { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx },         \
+    { OSSL_FUNC_DIGEST_INIT, (void (*)(void))keccak_init },             \
+    { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))keccak_update },         \
+    { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))keccak_final },           \
+    { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))keccak_freectx },       \
+    { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))keccak_dupctx },         \
+    { OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))name##_get_params }, \
+    { OSSL_FUNC_DIGEST_CTX_SET_PARAMS, (void (*)(void))stparams },      \
 OSSL_FUNC_DIGEST_CONSTRUCT_END
 
 static void keccak_freectx(void *vctx)
@@ -257,16 +263,20 @@ static int shake_set_params(void *vctx, const OSSL_PARAM params[])
 
 #define SHA3(bitlen) \
     SHA3_newctx(sha3, SHA3_##bitlen, sha3_##bitlen, bitlen, '\x06') \
-    OSSL_FUNC_SHA3_DIGEST(sha3_##bitlen, bitlen, SHA3_MDSIZE(bitlen), NULL)
+    OSSL_FUNC_SHA3_DIGEST(sha3_##bitlen, bitlen, \
+                          SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen), \
+                          EVP_MD_FLAG_DIGALGID_ABSENT, NULL)
 
 #define SHAKE(bitlen) \
     SHA3_newctx(shake, SHAKE_##bitlen, shake_##bitlen, bitlen, '\x1f') \
-    OSSL_FUNC_SHA3_DIGEST(shake_##bitlen, bitlen, SHA3_MDSIZE(bitlen), \
-                          shake_set_params)
+    OSSL_FUNC_SHA3_DIGEST(shake_##bitlen, bitlen, \
+                          SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen), \
+                          EVP_MD_FLAG_XOF, shake_set_params)
 #define KMAC(bitlen) \
     KMAC_newctx(keccak_kmac_##bitlen, bitlen, '\x04') \
-    OSSL_FUNC_SHA3_DIGEST(keccak_kmac_##bitlen, bitlen, KMAC_MDSIZE(bitlen), \
-                          shake_set_params)
+    OSSL_FUNC_SHA3_DIGEST(keccak_kmac_##bitlen, bitlen, \
+                          SHA3_BLOCKSIZE(bitlen), KMAC_MDSIZE(bitlen), \
+                          EVP_MD_FLAG_XOF, shake_set_params)
 
 SHA3(224)
 SHA3(256)
index 18a8d60b0c1444a27cde552c04df0c044a563de7..87c090c0928b044f1ce4a736eba44f74932a42ab 100644 (file)
@@ -32,9 +32,9 @@ int blake2b512_init(void *ctx)
 }
 
 OSSL_FUNC_DIGEST_CONSTRUCT(blake2s256, BLAKE2S_CTX,
-                           BLAKE2S_BLOCKBYTES, BLAKE2S_DIGEST_LENGTH,
+                           BLAKE2S_BLOCKBYTES, BLAKE2S_DIGEST_LENGTH, 0,
                            blake2s256_init, blake2s_update, blake2s_final)
 
 OSSL_FUNC_DIGEST_CONSTRUCT(blake2b512, BLAKE2B_CTX,
-                           BLAKE2B_BLOCKBYTES, BLAKE2B_DIGEST_LENGTH,
+                           BLAKE2B_BLOCKBYTES, BLAKE2B_DIGEST_LENGTH, 0,
                            blake2b512_init, blake2b_update, blake2b_final)
index 81636f670c9babd16f3ee46a36588fd32875db76..39b8d886139ba5a43de09d3f24551f25d1010c93 100644 (file)
@@ -13,5 +13,5 @@
 #include "internal/provider_algs.h"
 
 OSSL_FUNC_DIGEST_CONSTRUCT(md5, MD5_CTX,
-                           MD5_CBLOCK, MD5_DIGEST_LENGTH,
+                           MD5_CBLOCK, MD5_DIGEST_LENGTH, 0,
                            MD5_Init, MD5_Update, MD5_Final)
index 490134e7ebe851a92092e5cbc24fdc07a9e5d6a5..2ed36d03b3c6284712d365efdb35c786efa7b9af 100644 (file)
@@ -17,7 +17,7 @@
 #include "internal/md5_sha1.h"
 #include "internal/provider_algs.h"
 
-static OSSL_OP_digest_set_params_fn md5_sha1_set_params;
+static OSSL_OP_digest_ctx_set_params_fn md5_sha1_set_params;
 
 /* Special set_params method for SSL3 */
 static int md5_sha1_set_params(void *vctx, const OSSL_PARAM params[])
@@ -35,6 +35,6 @@ static int md5_sha1_set_params(void *vctx, const OSSL_PARAM params[])
 }
 
 OSSL_FUNC_DIGEST_CONSTRUCT_PARAMS(md5_sha1, MD5_SHA1_CTX,
-                                  MD5_SHA1_CBLOCK, MD5_SHA1_DIGEST_LENGTH,
+                                  MD5_SHA1_CBLOCK, MD5_SHA1_DIGEST_LENGTH, 0,
                                   md5_sha1_init, md5_sha1_update, md5_sha1_final,
                                   md5_sha1_set_params)
index d7644f4cbfb8f00f0706b68041c6e2d835011215..893737b5aea2f9da86e0d9227d705bd66bbb4dad 100644 (file)
@@ -8,6 +8,8 @@
  */
 
 #include <openssl/core_numbers.h>
+#include <openssl/core_names.h>
+#include <openssl/params.h>
 #include <openssl/whrlpool.h>
 #include "internal/provider_algs.h"
 
@@ -19,18 +21,7 @@ static OSSL_OP_digest_final_fn nullmd_final;
 static OSSL_OP_digest_newctx_fn nullmd_newctx;
 static OSSL_OP_digest_freectx_fn nullmd_freectx;
 static OSSL_OP_digest_dupctx_fn nullmd_dupctx;
-static OSSL_OP_digest_size_fn nullmd_size;
-static OSSL_OP_digest_block_size_fn nullmd_block_size;
-
-static size_t nullmd_block_size(void)
-{
-    return 0;
-}
-
-static size_t nullmd_size(void)
-{
-    return 0;
-}
+static OSSL_OP_digest_get_params_fn nullmd_get_params;
 
 static int nullmd_init(void *vctx)
 {
@@ -62,6 +53,22 @@ static void *nullmd_dupctx(void *ctx)
     return &nullmd_dummy;
 }
 
+static int nullmd_get_params(OSSL_PARAM params[])
+{
+    OSSL_PARAM *p = NULL;
+
+    p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_BLOCK_SIZE);
+    if (p != NULL && !OSSL_PARAM_set_int(p, 0))
+        return 0;
+    p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE);
+    if (p != NULL && !OSSL_PARAM_set_int(p, 0))
+        return 0;
+    p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_FLAGS);
+    if (p != NULL && !OSSL_PARAM_set_ulong(p, 0))
+        return 0;
+    return 1;
+}
+
 const OSSL_DISPATCH nullmd_functions[] = {
     { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))nullmd_newctx },
     { OSSL_FUNC_DIGEST_INIT, (void (*)(void))nullmd_init },
@@ -69,7 +76,6 @@ const OSSL_DISPATCH nullmd_functions[] = {
     { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))nullmd_final },
     { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))nullmd_freectx },
     { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))nullmd_dupctx },
-    { OSSL_FUNC_DIGEST_SIZE, (void (*)(void))nullmd_size },
-    { OSSL_FUNC_DIGEST_BLOCK_SIZE, (void (*)(void))nullmd_block_size },
+    { OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))nullmd_get_params },
     { 0, NULL }
 };
index bd039a8863be4654f09bc21744b21ccdd4473cf6..60789a469e1fd97589c3c6272e6362db25462adf 100644 (file)
@@ -13,5 +13,5 @@
 #include "internal/provider_algs.h"
 
 OSSL_FUNC_DIGEST_CONSTRUCT(sm3, SM3_CTX,
-                           SM3_CBLOCK, SM3_DIGEST_LENGTH,
+                           SM3_CBLOCK, SM3_DIGEST_LENGTH, 0,
                            sm3_init, sm3_update, sm3_final)
index edd4b78bbd9688ecbb5f1e9ead0a628fd11897b4..833aab768580008ff9dba7fc02041551776bb2e5 100644 (file)
@@ -14,5 +14,5 @@
 #include "internal/provider_algs.h"
 
 OSSL_FUNC_DIGEST_CONSTRUCT(md2, MD2_CTX,
-                           MD2_BLOCK, MD2_DIGEST_LENGTH,
+                           MD2_BLOCK, MD2_DIGEST_LENGTH, 0,
                            MD2_Init, MD2_Update, MD2_Final)
index 86937f7b43014495c6d2460bcc7ba445ebfdf951..9d336f99198c6a3d569f5f4aeee03d83d01006be 100644 (file)
@@ -14,5 +14,5 @@
 #include "internal/provider_algs.h"
 
 OSSL_FUNC_DIGEST_CONSTRUCT(md4, MD4_CTX,
-                           MD4_CBLOCK, MD4_DIGEST_LENGTH,
+                           MD4_CBLOCK, MD4_DIGEST_LENGTH, 0,
                            MD4_Init, MD4_Update, MD4_Final)
index 31e95cb3918ea8e296ac4d68f8d4a1e9693c6108..f77e3992b0813eddf9392db267394cdabcb40576 100644 (file)
@@ -15,7 +15,7 @@
 #include "internal/core_mkdigest.h"
 #include "internal/provider_algs.h"
 
-static OSSL_OP_digest_set_params_fn mdc2_set_params;
+static OSSL_OP_digest_ctx_set_params_fn mdc2_set_params;
 
 static int mdc2_set_params(void *vctx, const OSSL_PARAM params[])
 {
@@ -32,6 +32,6 @@ static int mdc2_set_params(void *vctx, const OSSL_PARAM params[])
 }
 
 OSSL_FUNC_DIGEST_CONSTRUCT_PARAMS(mdc2, MDC2_CTX,
-                                  MDC2_BLOCK, MDC2_DIGEST_LENGTH,
+                                  MDC2_BLOCK, MDC2_DIGEST_LENGTH, 0,
                                   MDC2_Init, MDC2_Update, MDC2_Final,
                                   mdc2_set_params)
index 124351257b3c9260bc2bc58f558add6c187a277e..2b192bf9174608ea934cc6823e216ce98f4166ba 100644 (file)
@@ -14,5 +14,5 @@
 #include "internal/provider_algs.h"
 
 OSSL_FUNC_DIGEST_CONSTRUCT(ripemd160, RIPEMD160_CTX,
-                           RIPEMD160_CBLOCK, RIPEMD160_DIGEST_LENGTH,
+                           RIPEMD160_CBLOCK, RIPEMD160_DIGEST_LENGTH, 0,
                            RIPEMD160_Init, RIPEMD160_Update, RIPEMD160_Final)
index ece67e094101aeb84408f69f9e9be059608da373..297d367667e3355176e12f198dc938896b3ce488 100644 (file)
@@ -14,5 +14,5 @@
 #include "internal/provider_algs.h"
 
 OSSL_FUNC_DIGEST_CONSTRUCT(wp, WHIRLPOOL_CTX,
-                           WHIRLPOOL_BBLOCK / 8, WHIRLPOOL_DIGEST_LENGTH,
+                           WHIRLPOOL_BBLOCK / 8, WHIRLPOOL_DIGEST_LENGTH, 0,
                            WHIRLPOOL_Init, WHIRLPOOL_Update, WHIRLPOOL_Final)