digest: Add get_digest_algorithm
authorMateusz Kulikowski <m.kulikowski@samsung.com>
Mon, 4 Apr 2016 13:23:38 +0000 (15:23 +0200)
committerMateusz Kulikowski <m.kulikowski@samsung.com>
Fri, 8 Apr 2016 11:26:23 +0000 (13:26 +0200)
Add API to translate yaca_digest_algo_e to EVP_MD.

It is needed by other modules (like pbkdf2, message sign/verify etc.).

Change-Id: I0b73eb51e076e443ebc269f96772c438479c81f5
Signed-off-by: Mateusz Kulikowski <m.kulikowski@samsung.com>
src/digest.c
src/internal.h

index f0acd71..8893e57 100644 (file)
@@ -70,51 +70,68 @@ static void destroy_digest_context(yaca_ctx_h ctx)
        EVP_MD_CTX_destroy(c->mdctx);
 }
 
-API int yaca_digest_init(yaca_ctx_h *ctx, yaca_digest_algo_e algo)
+int get_digest_algorithm(yaca_digest_algo_e algo, const EVP_MD **md)
 {
-       int ret;
-       struct yaca_digest_ctx_s *nc = NULL;
+       int ret = 0;
 
-       if (ctx == NULL)
+       if (!md)
                return YACA_ERROR_INVALID_ARGUMENT;
 
-       nc = yaca_malloc(sizeof(struct yaca_digest_ctx_s));
-       if (nc == NULL)
-               return YACA_ERROR_OUT_OF_MEMORY;
-
-       nc->ctx.type = YACA_CTX_DIGEST;
-       nc->ctx.ctx_destroy = destroy_digest_context;
-       nc->ctx.get_output_length = get_digest_output_length;
+       *md = NULL;
 
        switch (algo)
        {
        case YACA_DIGEST_MD5:
-               nc->md = EVP_md5();
+               *md = EVP_md5();
                break;
        case YACA_DIGEST_SHA1:
-               nc->md = EVP_sha1();
+               *md = EVP_sha1();
                break;
        case YACA_DIGEST_SHA224:
-               nc->md = EVP_sha224();
+               *md = EVP_sha224();
                break;
        case YACA_DIGEST_SHA256:
-               nc->md = EVP_sha256();
+               *md = EVP_sha256();
                break;
        case YACA_DIGEST_SHA384:
-               nc->md = EVP_sha384();
+               *md = EVP_sha384();
                break;
        case YACA_DIGEST_SHA512:
-               nc->md = EVP_sha512();
+               *md = EVP_sha512();
+               break;
+       case YACA_DIGEST_CMAC:
+               ret = YACA_ERROR_NOT_IMPLEMENTED;
                break;
        default:
                ret = YACA_ERROR_INVALID_ARGUMENT;
-               goto free;
+               break;
        }
 
-       if (nc->md == NULL) {
+       if (ret == 0 && *md == NULL)
                ret = YACA_ERROR_OPENSSL_FAILURE;
+
+       return ret;
+}
+
+API int yaca_digest_init(yaca_ctx_h *ctx, yaca_digest_algo_e algo)
+{
+       int ret;
+       struct yaca_digest_ctx_s *nc = NULL;
+
+       if (ctx == NULL)
+               return YACA_ERROR_INVALID_ARGUMENT;
+
+       nc = yaca_malloc(sizeof(struct yaca_digest_ctx_s));
+       if (nc == NULL)
+               return YACA_ERROR_OUT_OF_MEMORY;
+
+       nc->ctx.type = YACA_CTX_DIGEST;
+       nc->ctx.ctx_destroy = destroy_digest_context;
+       nc->ctx.get_output_length = get_digest_output_length;
+
+       ret = get_digest_algorithm(algo, &nc->md);
+       if (ret < 0)
                goto free;
-       }
 
        nc->mdctx = EVP_MD_CTX_create();
        if (nc->mdctx == NULL) {
index ea7d0ab..75d42c2 100644 (file)
@@ -25,6 +25,7 @@
 #define INTERNAL_H
 
 #include <stddef.h>
+#include <openssl/ossl_typ.h>
 
 #include <yaca/types.h>
 
@@ -54,4 +55,6 @@ struct yaca_key_s
        int (*get_key_length)(const struct yaca_key_s *key);
 };
 
+int get_digest_algorithm(yaca_digest_algo_e algo, const EVP_MD **md);
+
 #endif