]> www.wagner.pp.ru Git - openssl-gost/engine.git/blobdiff - gost_omac_acpkm.c
Making a gost provider - Refactor the testing foundation
[openssl-gost/engine.git] / gost_omac_acpkm.c
index 3820c904e47fca6c660840ed16a8f384fcac6854..612524c98f4406f1fa542a013515e4b8ac3477a7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2018 vt@altlinux.org. All Rights Reserved.
+ * Copyright (C) 2018,2020 Vitaly Chikunov <vt@altlinux.org>. All Rights Reserved.
  * Copyright (c) 2010 The OpenSSL Project.  All rights reserved.
  *
  * Contents licensed under the terms of the OpenSSL license
@@ -68,6 +68,7 @@ static CMAC_ACPKM_CTX *CMAC_ACPKM_CTX_new(void)
     }
     ctx->actx = EVP_CIPHER_CTX_new();
     if (ctx->actx == NULL) {
+        EVP_CIPHER_CTX_free(ctx->cctx);
         OPENSSL_free(ctx);
         return NULL;
     }
@@ -150,13 +151,14 @@ static int CMAC_ACPKM_Init(CMAC_ACPKM_CTX *ctx, const void *key, size_t keylen,
     /* Non-NULL key means initialisation is complete */
     if (key) {
         unsigned char acpkm_iv[EVP_MAX_BLOCK_LENGTH];
+        int block_size, key_len;
 
         /* Initialize CTR for ACPKM-Master */
         if (!EVP_CIPHER_CTX_cipher(ctx->actx))
             return 0;
         /* block size of ACPKM cipher could be 1, but,
          * cbc cipher is same with correct block_size */
-        const int block_size = EVP_CIPHER_CTX_block_size(ctx->cctx);
+        block_size = EVP_CIPHER_CTX_block_size(ctx->cctx);
         /* Wide IV = 1^{n/2} || 0,
          * where a^r denotes the string that consists of r 'a' bits */
         memset(acpkm_iv, 0xff, block_size / 2);
@@ -164,7 +166,7 @@ static int CMAC_ACPKM_Init(CMAC_ACPKM_CTX *ctx, const void *key, size_t keylen,
         if (!EVP_EncryptInit_ex(ctx->actx, NULL, NULL, key, acpkm_iv))
             return 0;
         /* EVP_CIPHER key_len may be different from EVP_CIPHER_CTX key_len */
-        int key_len = EVP_CIPHER_key_length(EVP_CIPHER_CTX_cipher(ctx->actx));
+        key_len = EVP_CIPHER_key_length(EVP_CIPHER_CTX_cipher(ctx->actx));
 
         /* Generate first key material (K^1 || K^1_1) */
         if (!EVP_Cipher(ctx->actx, ctx->km, zero_iv, key_len + block_size))
@@ -254,7 +256,8 @@ static int CMAC_ACPKM_Update(CMAC_ACPKM_CTX *ctx, const void *in, size_t dlen)
 static int CMAC_ACPKM_Final(CMAC_ACPKM_CTX *ctx, unsigned char *out,
                             size_t *poutlen)
 {
-    int i, bl, lb;
+    int i, bl, lb, key_len;
+    unsigned char *k1, k2[EVP_MAX_BLOCK_LENGTH];
     if (ctx->nlast_block == -1)
         return 0;
     bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
@@ -265,10 +268,9 @@ static int CMAC_ACPKM_Final(CMAC_ACPKM_CTX *ctx, unsigned char *out,
 
     if (!CMAC_ACPKM_Mesh(ctx))
         return 0;
-    int key_len = EVP_CIPHER_key_length(EVP_CIPHER_CTX_cipher(ctx->actx));
+    key_len = EVP_CIPHER_key_length(EVP_CIPHER_CTX_cipher(ctx->actx));
     /* Keys k1 and k2 */
-    unsigned char *k1 = ctx->km + key_len;
-    unsigned char k2[EVP_MAX_BLOCK_LENGTH];
+    k1 = ctx->km + key_len;
     make_kn(k2, ctx->km + key_len, bl);
 
     /* Is last block complete? */
@@ -355,7 +357,7 @@ int omac_acpkm_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
     return 1;
 }
 
-int omac_acpkm_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
+static int omac_acpkm_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
 {
     OMAC_ACPKM_CTX *c_to = EVP_MD_CTX_md_data(to);
     const OMAC_ACPKM_CTX *c_from = EVP_MD_CTX_md_data(from);
@@ -381,7 +383,7 @@ int omac_acpkm_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
 }
 
 /* Clean up imit ctx */
-int omac_acpkm_imit_cleanup(EVP_MD_CTX *ctx)
+static int omac_acpkm_imit_cleanup(EVP_MD_CTX *ctx)
 {
     OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
 
@@ -494,36 +496,16 @@ int omac_acpkm_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
     }
 }
 
-static EVP_MD *_hidden_grasshopper_omac_acpkm_md = NULL;
-
-EVP_MD *grasshopper_omac_acpkm(void)
-{
-    if (_hidden_grasshopper_omac_acpkm_md == NULL) {
-        EVP_MD *md;
-
-        if ((md =
-             EVP_MD_meth_new(NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
-              NID_undef)) == NULL
-            || !EVP_MD_meth_set_result_size(md, MAX_GOST_OMAC_ACPKM_SIZE)
-            || !EVP_MD_meth_set_input_blocksize(md, GRASSHOPPER_BLOCK_SIZE)
-            || !EVP_MD_meth_set_app_datasize(md, sizeof(OMAC_ACPKM_CTX))
-            || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_XOF)
-            || !EVP_MD_meth_set_init(md, grasshopper_omac_acpkm_init)
-            || !EVP_MD_meth_set_update(md, omac_acpkm_imit_update)
-            || !EVP_MD_meth_set_final(md, omac_acpkm_imit_final)
-            || !EVP_MD_meth_set_copy(md, omac_acpkm_imit_copy)
-            || !EVP_MD_meth_set_cleanup(md, omac_acpkm_imit_cleanup)
-            || !EVP_MD_meth_set_ctrl(md, omac_acpkm_imit_ctrl)) {
-            EVP_MD_meth_free(md);
-            md = NULL;
-        }
-        _hidden_grasshopper_omac_acpkm_md = md;
-    }
-    return _hidden_grasshopper_omac_acpkm_md;
-}
-
-void grasshopper_omac_acpkm_destroy(void)
-{
-    EVP_MD_meth_free(_hidden_grasshopper_omac_acpkm_md);
-    _hidden_grasshopper_omac_acpkm_md = NULL;
-}
+GOST_digest kuznyechik_ctracpkm_omac_digest = {
+    .nid = NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
+    .result_size = MAX_GOST_OMAC_ACPKM_SIZE,
+    .input_blocksize = GRASSHOPPER_BLOCK_SIZE,
+    .app_datasize = sizeof(OMAC_ACPKM_CTX),
+    .flags = EVP_MD_FLAG_XOF,
+    .init = grasshopper_omac_acpkm_init,
+    .update = omac_acpkm_imit_update,
+    .final = omac_acpkm_imit_final,
+    .copy = omac_acpkm_imit_copy,
+    .cleanup = omac_acpkm_imit_cleanup,
+    .ctrl = omac_acpkm_imit_ctrl,
+};