X-Git-Url: http://wagner.pp.ru/gitweb/?a=blobdiff_plain;f=gost_omac.c;h=e78fd9d7c5de76e3e7b00c80e7a18a1e5b26eeb9;hb=3feabe343608ffbcb8afc887e3931796dd7f5b46;hp=ad96662f04fdc03a540a9a96bc68e90d17d0ab08;hpb=14e654cab19089027b00733594480eb03d8c6da5;p=openssl-gost%2Fengine.git diff --git a/gost_omac.c b/gost_omac.c index ad96662..e78fd9d 100644 --- a/gost_omac.c +++ b/gost_omac.c @@ -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; } @@ -89,6 +92,17 @@ int omac_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from) { return 0; } + if (!c_from->cmac_ctx) { + if (c_to->cmac_ctx) { + CMAC_CTX_free(c_to->cmac_ctx); + c_to->cmac_ctx = NULL; + } + return 1; + } + if (c_to->cmac_ctx == c_from->cmac_ctx) + { + c_to->cmac_ctx = CMAC_CTX_new(); + } return CMAC_CTX_copy(c_to->cmac_ctx, c_from->cmac_ctx); } @@ -133,7 +147,23 @@ int omac_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr) case EVP_MD_CTRL_SET_KEY: { OMAC_CTX *c = EVP_MD_CTX_md_data(ctx); - const EVP_CIPHER *cipher = EVP_get_cipherbynid(c->cipher_nid); + const EVP_MD *md = EVP_MD_CTX_md(ctx); + const EVP_CIPHER *cipher = NULL; + + if (c->cipher_nid == NID_undef) + { + switch (EVP_MD_nid(md)) + { + case NID_magma_mac: + c->cipher_nid = NID_magma_cbc; + break; + + case NID_grasshopper_mac: + c->cipher_nid = NID_grasshopper_cbc; + break; + } + } + cipher = EVP_get_cipherbynid(c->cipher_nid); if (cipher == NULL) { @@ -232,7 +262,7 @@ EVP_MD *grasshopper_omac(void) EVP_MD *md; if ((md = EVP_MD_meth_new(NID_grasshopper_mac, NID_undef)) == NULL - || !EVP_MD_meth_set_result_size(md, 4) + || !EVP_MD_meth_set_result_size(md, 8) || !EVP_MD_meth_set_input_blocksize(md, 8) || !EVP_MD_meth_set_app_datasize(md, sizeof(OMAC_CTX)) || !EVP_MD_meth_set_flags(md, 0)