1313
1414#include "e_gost_err.h"
1515#include "gost_lcl.h"
16- #include "gost_grasshopper_defines.h"
17- #include "gost_grasshopper_cipher.h"
1816
19- #define ACPKM_T_MAX (GRASSHOPPER_KEY_SIZE + GRASSHOPPER_BLOCK_SIZE)
17+ #define ACPKM_T_MAX (EVP_MAX_KEY_LENGTH + EVP_MAX_BLOCK_LENGTH)
18+
2019/*
2120 * CMAC code from crypto/cmac/cmac.c with ACPKM tweaks
2221 */
2322struct CMAC_ACPKM_CTX_st {
2423 /* Cipher context to use */
2524 EVP_CIPHER_CTX * cctx ;
2625 /* CTR-ACPKM cipher */
26+ EVP_CIPHER * fetched_acpkm ;
2727 EVP_CIPHER_CTX * actx ;
2828 unsigned char km [ACPKM_T_MAX ]; /* Key material */
2929 /* Temporary block */
@@ -94,6 +94,7 @@ static void CMAC_ACPKM_CTX_free(CMAC_ACPKM_CTX *ctx)
9494 return ;
9595 CMAC_ACPKM_CTX_cleanup (ctx );
9696 EVP_CIPHER_CTX_free (ctx -> cctx );
97+ EVP_CIPHER_free (ctx -> fetched_acpkm );
9798 EVP_CIPHER_CTX_free (ctx -> actx );
9899 OPENSSL_free (ctx );
99100}
@@ -105,6 +106,11 @@ static int CMAC_ACPKM_CTX_copy(CMAC_ACPKM_CTX *out, const CMAC_ACPKM_CTX *in)
105106 return 0 ;
106107 if (!EVP_CIPHER_CTX_copy (out -> cctx , in -> cctx ))
107108 return 0 ;
109+ if (in -> fetched_acpkm ) {
110+ if (!EVP_CIPHER_up_ref (in -> fetched_acpkm ))
111+ return 0 ;
112+ }
113+ out -> fetched_acpkm = in -> fetched_acpkm ;
108114 if (!EVP_CIPHER_CTX_copy (out -> actx , in -> actx ))
109115 return 0 ;
110116 bl = EVP_CIPHER_CTX_block_size (in -> cctx );
@@ -117,11 +123,22 @@ static int CMAC_ACPKM_CTX_copy(CMAC_ACPKM_CTX *out, const CMAC_ACPKM_CTX *in)
117123 return 1 ;
118124}
119125
126+ static const EVP_CIPHER * get_cipher (const char * cipher_name , EVP_CIPHER * * fetched_cipher ) {
127+ const EVP_CIPHER * cipher = NULL ;
128+ cipher = EVP_get_cipherbyname (cipher_name );
129+ if (cipher )
130+ return cipher ;
131+
132+ * fetched_cipher = EVP_CIPHER_fetch (NULL , cipher_name , NULL );
133+ cipher = * fetched_cipher ;
134+ return cipher ;
135+ }
136+
120137static int CMAC_ACPKM_Init (CMAC_ACPKM_CTX * ctx , const void * key , size_t keylen ,
121- const EVP_CIPHER * cipher , ENGINE * impl )
138+ const EVP_CIPHER * cipher )
122139{
123140 /* All zeros means restart */
124- if (!key && !cipher && ! impl && keylen == 0 ) {
141+ if (!key && !cipher && keylen == 0 ) {
125142 /* Not initialised */
126143 if (ctx -> nlast_block == -1 )
127144 return 0 ;
@@ -136,28 +153,28 @@ static int CMAC_ACPKM_Init(CMAC_ACPKM_CTX *ctx, const void *key, size_t keylen,
136153 if (cipher ) {
137154 const EVP_CIPHER * acpkm = NULL ;
138155
139- if (!EVP_EncryptInit_ex (ctx -> cctx , cipher , impl , NULL , NULL ))
156+ if (!EVP_EncryptInit_ex (ctx -> cctx , cipher , NULL , NULL , NULL ))
140157 return 0 ;
141158 /* Unfortunately, EVP_CIPHER_is_a is bugged for an engine, EVP_CIPHER_nid is bugged for a provider. */
142159 if (EVP_CIPHER_nid (cipher ) == NID_undef ) {
143160 /* Looks like a provider */
144161 if (EVP_CIPHER_is_a (cipher , SN_magma_cbc ))
145- acpkm = cipher_gost_magma_ctracpkm ( );
162+ acpkm = get_cipher ( SN_magma_ctr_acpkm , & ( ctx -> fetched_acpkm ) );
146163 else if (EVP_CIPHER_is_a (cipher , SN_grasshopper_cbc ))
147- acpkm = cipher_gost_grasshopper_ctracpkm ( );
164+ acpkm = get_cipher ( SN_kuznyechik_ctr_acpkm , & ( ctx -> fetched_acpkm ) );
148165 }
149166 else {
150167 /* Looks like an engine */
151168 if (EVP_CIPHER_nid (cipher ) == NID_magma_cbc )
152- acpkm = cipher_gost_magma_ctracpkm ( );
169+ acpkm = get_cipher ( SN_magma_ctr_acpkm , & ( ctx -> fetched_acpkm ) );
153170 else if (EVP_CIPHER_nid (cipher ) == NID_grasshopper_cbc )
154- acpkm = cipher_gost_grasshopper_ctracpkm ( );
171+ acpkm = get_cipher ( SN_kuznyechik_ctr_acpkm , & ( ctx -> fetched_acpkm ) );
155172 }
156173
157174 if (acpkm == NULL )
158175 return 0 ;
159176
160- if (!EVP_EncryptInit_ex (ctx -> actx , acpkm , impl , NULL , NULL ))
177+ if (!EVP_EncryptInit_ex (ctx -> actx , acpkm , NULL , NULL , NULL ))
161178 return 0 ;
162179 }
163180 /* Non-NULL key means initialisation is complete */
@@ -365,7 +382,7 @@ static int omac_acpkm_imit_update(EVP_MD_CTX *ctx, const void *data,
365382 return CMAC_ACPKM_Update (c -> cmac_ctx , data , count );
366383}
367384
368- int omac_acpkm_imit_final (EVP_MD_CTX * ctx , unsigned char * md )
385+ static int omac_acpkm_imit_final (EVP_MD_CTX * ctx , unsigned char * md )
369386{
370387 OMAC_ACPKM_CTX * c = EVP_MD_CTX_md_data (ctx );
371388 unsigned char mac [MAX_GOST_OMAC_ACPKM_SIZE ];
@@ -432,14 +449,14 @@ static int omac_acpkm_key(OMAC_ACPKM_CTX *c, const EVP_CIPHER *cipher,
432449 return 0 ;
433450 }
434451
435- ret = CMAC_ACPKM_Init (c -> cmac_ctx , key , key_size , cipher , NULL );
452+ ret = CMAC_ACPKM_Init (c -> cmac_ctx , key , key_size , cipher );
436453 if (ret > 0 ) {
437454 c -> key_set = 1 ;
438455 }
439456 return 1 ;
440457}
441458
442- int omac_acpkm_imit_ctrl (EVP_MD_CTX * ctx , int type , int arg , void * ptr )
459+ static int omac_acpkm_imit_ctrl (EVP_MD_CTX * ctx , int type , int arg , void * ptr )
443460{
444461 switch (type ) {
445462 case EVP_MD_CTRL_KEY_LEN :
@@ -548,7 +565,7 @@ int omac_acpkm_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
548565GOST_digest kuznyechik_ctracpkm_omac_digest = {
549566 .nid = NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac ,
550567 .result_size = MAX_GOST_OMAC_ACPKM_SIZE ,
551- .input_blocksize = GRASSHOPPER_BLOCK_SIZE ,
568+ .input_blocksize = 16 ,
552569 .app_datasize = sizeof (OMAC_ACPKM_CTX ),
553570 .flags = EVP_MD_FLAG_XOF ,
554571 .init = grasshopper_omac_acpkm_init ,
0 commit comments