From: Vitaly Chikunov Date: Fri, 20 Jul 2018 01:52:40 +0000 (+0300) Subject: Merge branch 'magma_impl' into openssl_1_0_2_alt X-Git-Tag: v3.0.0~412 X-Git-Url: http://wagner.pp.ru/gitweb/?a=commitdiff_plain;ds=sidebyside;h=0ee4f8fc839278672e47213549bfb152ad3d2654;hp=-c;p=openssl-gost%2Fengine.git Merge branch 'magma_impl' into openssl_1_0_2_alt --- 0ee4f8fc839278672e47213549bfb152ad3d2654 diff --combined CMakeLists.txt index 5741610,ba0cc71..4d83414 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@@ -3,7 -3,7 +3,7 @@@ project(gost-engine LANGUAGES C include(GNUInstallDirs) -find_package(OpenSSL 1.1 REQUIRED) +find_package(OpenSSL 1.0.2 REQUIRED) include_directories(${OPENSSL_INCLUDE_DIR}) if (CMAKE_C_COMPILER_ID MATCHES "Clang") @@@ -88,12 -88,17 +88,17 @@@ set(GOST_EC_SOURCE_FILE gost_ec_sign.c ) + set (GOST_OMAC_SOURCE_FILES + gost_omac.c + ) + set(GOST_LIB_SOURCE_FILES ${GOST_89_SOURCE_FILES} ${GOST_HASH_SOURCE_FILES} ${GOST_HASH_2012_SOURCE_FILES} ${GOST_GRASSHOPPER_SOURCE_FILES} ${GOST_EC_SOURCE_FILES} + ${GOST_OMAC_SOURCE_FILES} ) set(GOST_ENGINE_SOURCE_FILES @@@ -102,6 -107,7 +107,7 @@@ gost_md.c gost_md2012.c gost_pmeth.c + gost_omac.c ) add_library(gost_core STATIC ${GOST_LIB_SOURCE_FILES}) @@@ -125,6 -131,11 +131,11 @@@ set(GOST_12_SUM_SOURCE_FILE add_executable(gost12sum ${GOST_12_SUM_SOURCE_FILES}) target_link_libraries(gost12sum gost_core) + set_source_files_properties(tags PROPERTIES GENERATED true) + add_custom_target(tags + COMMAND ctags -R . ${OPENSSL_ROOT_DIR} + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}) + # install set(OPENSSL_ENGINES_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}/engines-${OPENSSL_VERSION_MAJOR}_${OPENSSL_VERSION_MINOR}) set(OPENSSL_MAN_INSTALL_DIR ${CMAKE_INSTALL_MANDIR}/man1) diff --combined gost_ameth.c index 58f4df4,5089e2a..5b93ea6 --- a/gost_ameth.c +++ b/gost_ameth.c @@@ -131,9 -131,9 +131,9 @@@ static int gost_decode_nid_params(EVP_P * Parses GOST algorithm parameters from X509_ALGOR and modifies pkey setting * NID and parameters */ -static int decode_gost_algor_params(EVP_PKEY *pkey, const X509_ALGOR *palg) +static int decode_gost_algor_params(EVP_PKEY *pkey, X509_ALGOR *palg) { - const ASN1_OBJECT *palg_obj = NULL; + ASN1_OBJECT *palg_obj = NULL; int ptype = V_ASN1_UNDEF; int pkey_nid = NID_undef, param_nid = NID_undef; ASN1_STRING *pval = NULL; @@@ -142,7 -142,7 +142,7 @@@ if (!pkey || !palg) return 0; - X509_ALGOR_get0(&palg_obj, &ptype, (const void **)&pval, palg); + X509_ALGOR_get0(&palg_obj, &ptype, (void **)&pval, palg); if (ptype != V_ASN1_SEQUENCE) { GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS, GOST_R_BAD_KEY_PARAMETERS_FORMAT); @@@ -181,7 -181,7 +181,7 @@@ static int gost_set_priv_key(EVP_PKEY * if (!EC_KEY_set_private_key(ec, priv)) return 0; if (!EVP_PKEY_missing_parameters(pkey)) - gost_ec_compute_public(ec); + return gost_ec_compute_public(ec); break; } default: @@@ -331,14 -331,14 +331,14 @@@ static BIGNUM *unmask_priv_key(EVP_PKE return pknum_masked; } -static int priv_decode_gost(EVP_PKEY *pk, const PKCS8_PRIV_KEY_INFO *p8inf) +static int priv_decode_gost(EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf) { const unsigned char *pkey_buf = NULL, *p = NULL; int priv_len = 0; BIGNUM *pk_num = NULL; int ret = 0; - const X509_ALGOR *palg = NULL; - const ASN1_OBJECT *palg_obj = NULL; + X509_ALGOR *palg = NULL; + ASN1_OBJECT *palg_obj = NULL; ASN1_INTEGER *priv_key = NULL; int expected_key_len = 32; @@@ -842,6 -842,30 +842,30 @@@ static int mac_ctrl_gost_12(EVP_PKEY *p return -2; } + static int mac_ctrl_magma(EVP_PKEY *pkey, int op, long arg1, void *arg2) + { + switch (op) { + case ASN1_PKEY_CTRL_DEFAULT_MD_NID: + if (arg2) { + *(int *)arg2 = NID_magma_mac; + return 2; + } + } + return -2; + } + + static int mac_ctrl_grasshopper(EVP_PKEY *pkey, int op, long arg1, void *arg2) + { + switch (op) { + case ASN1_PKEY_CTRL_DEFAULT_MD_NID: + if (arg2) { + *(int *)arg2 = NID_grasshopper_mac; + return 2; + } + } + return -2; + } + static int gost2001_param_encode(const EVP_PKEY *pkey, unsigned char **pder) { int nid = @@@ -922,6 -946,14 +946,14 @@@ int register_ameth_gost(int nid, EVP_PK EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost); EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost_12); break; + case NID_magma_mac: + EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost); + EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_magma); + break; + case NID_grasshopper_mac: + EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost); + EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_grasshopper); + break; } return 1; } diff --combined gost_eng.c index 517a7da,1f25654..caedf9a --- a/gost_eng.c +++ b/gost_eng.c @@@ -47,11 -47,13 +47,13 @@@ static int gost_cipher_nids[] = NID_grasshopper_cfb, NID_grasshopper_ofb, NID_grasshopper_ctr, + NID_magma_cbc, + NID_magma_ctr, 0 }; static int gost_digest_nids(const int** nids) { - static int digest_nids[6] = {0, 0, 0, 0, 0, 0}; + static int digest_nids[8] = {0, 0, 0, 0, 0, 0, 0, 0}; static int pos = 0; static int init = 0; @@@ -67,6 -69,12 +69,12 @@@ digest_nids[pos++] = EVP_MD_type(md); if ((md = imit_gost_cp_12()) != NULL) digest_nids[pos++] = EVP_MD_type(md); + if ((md = magma_omac()) != NULL) + digest_nids[pos++] = EVP_MD_type(md); + if ((md = grasshopper_omac()) != NULL) + digest_nids[pos++] = EVP_MD_type(md); + + digest_nids[pos] = 0; init = 1; } @@@ -80,18 -88,22 +88,22 @@@ static int gost_pkey_meth_nids[] = NID_id_GostR3410_2012_256, NID_id_GostR3410_2012_512, NID_gost_mac_12, + NID_magma_mac, + NID_grasshopper_mac, 0 }; static EVP_PKEY_METHOD* pmeth_GostR3410_2001 = NULL, * pmeth_GostR3410_2012_256 = NULL, * pmeth_GostR3410_2012_512 = NULL, - * pmeth_Gost28147_MAC = NULL, * pmeth_Gost28147_MAC_12 = NULL; + * pmeth_Gost28147_MAC = NULL, * pmeth_Gost28147_MAC_12 = NULL, + * pmeth_magma_mac = NULL, * pmeth_grasshopper_mac = NULL; static EVP_PKEY_ASN1_METHOD* ameth_GostR3410_2001 = NULL, * ameth_GostR3410_2012_256 = NULL, * ameth_GostR3410_2012_512 = NULL, - * ameth_Gost28147_MAC = NULL, * ameth_Gost28147_MAC_12 = NULL; + * ameth_Gost28147_MAC = NULL, * ameth_Gost28147_MAC_12 = NULL, + * ameth_magma_mac = NULL, * ameth_grasshopper_mac = NULL; static int gost_engine_init(ENGINE* e) { return 1; @@@ -102,33 -114,38 +114,39 @@@ static int gost_engine_finish(ENGINE* e } static int gost_engine_destroy(ENGINE* e) { - digest_gost_destroy(); - digest_gost2012_256_destroy(); - digest_gost2012_512_destroy(); - - imit_gost_cpa_destroy(); - imit_gost_cp_12_destroy(); - magma_omac_destroy(); - grasshopper_omac_destroy(); - - cipher_gost_destroy(); - - gost_param_free(); - - pmeth_GostR3410_2001 = NULL; - pmeth_Gost28147_MAC = NULL; - pmeth_GostR3410_2012_256 = NULL; - pmeth_GostR3410_2012_512 = NULL; - pmeth_Gost28147_MAC_12 = NULL; - pmeth_magma_mac = NULL; - pmeth_grasshopper_mac = NULL; - - ameth_GostR3410_2001 = NULL; - ameth_Gost28147_MAC = NULL; - ameth_GostR3410_2012_256 = NULL; - ameth_GostR3410_2012_512 = NULL; - ameth_Gost28147_MAC_12 = NULL; - ameth_magma_mac = NULL; - ameth_grasshopper_mac = NULL; - - ERR_unload_GOST_strings(); - - return 1; + digest_gost_destroy(); + digest_gost2012_256_destroy(); + digest_gost2012_512_destroy(); + + imit_gost_cpa_destroy(); + imit_gost_cp_12_destroy(); ++ magma_omac_destroy(); ++ grasshopper_omac_destroy(); + + cipher_gost_destroy(); + cipher_gost_grasshopper_destroy(); + + gost_param_free(); + + pmeth_GostR3410_2001 = NULL; + pmeth_Gost28147_MAC = NULL; + pmeth_GostR3410_2012_256 = NULL; + pmeth_GostR3410_2012_512 = NULL; + pmeth_Gost28147_MAC_12 = NULL; ++ pmeth_magma_mac = NULL; ++ pmeth_grasshopper_mac = NULL; + + ameth_GostR3410_2001 = NULL; + ameth_Gost28147_MAC = NULL; + ameth_GostR3410_2012_256 = NULL; + ameth_GostR3410_2012_512 = NULL; + ameth_Gost28147_MAC_12 = NULL; ++ ameth_magma_mac = NULL; ++ ameth_grasshopper_mac = NULL; + + ERR_unload_GOST_strings(); + + return 1; } static int bind_gost(ENGINE* e, const char* id) { @@@ -197,6 -214,12 +215,12 @@@ "GOST-MAC-12", "GOST 28147-89 MAC with 2012 params")) goto end; + if (!register_ameth_gost(NID_magma_mac, &ameth_magma_mac, + "MAGMA-MAC", "GOST R 34.13-2015 Magma MAC")) + goto end; + if (!register_ameth_gost(NID_grasshopper_mac, &ameth_grasshopper_mac, + "GRASSHOPPER-MAC", "GOST R 34.13-2015 Grasshopper MAC")) + goto end; if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0)) goto end; @@@ -212,6 -235,10 +236,10 @@@ goto end; if (!register_pmeth_gost(NID_gost_mac_12, &pmeth_Gost28147_MAC_12, 0)) goto end; + if (!register_pmeth_gost(NID_magma_mac, &pmeth_magma_mac, 0)) + goto end; + if (!register_pmeth_gost(NID_grasshopper_mac, &pmeth_grasshopper_mac, 0)) + goto end; if (!ENGINE_register_ciphers(e) || !ENGINE_register_digests(e) || !ENGINE_register_pkey_meths(e) @@@ -225,11 -252,15 +253,15 @@@ || !EVP_add_cipher(cipher_gost_grasshopper_cfb()) || !EVP_add_cipher(cipher_gost_grasshopper_ofb()) || !EVP_add_cipher(cipher_gost_grasshopper_ctr()) + || !EVP_add_cipher(cipher_magma_cbc()) + || !EVP_add_cipher(cipher_magma_ctr()) || !EVP_add_digest(digest_gost()) || !EVP_add_digest(digest_gost2012_512()) || !EVP_add_digest(digest_gost2012_256()) || !EVP_add_digest(imit_gost_cpa()) || !EVP_add_digest(imit_gost_cp_12()) + || !EVP_add_digest(magma_omac()) + || !EVP_add_digest(grasshopper_omac()) ) { goto end; } @@@ -263,6 -294,10 +295,10 @@@ static int gost_digests(ENGINE* e, cons *digest = digest_gost2012_512(); } else if (nid == NID_gost_mac_12) { *digest = imit_gost_cp_12(); + } else if (nid == NID_magma_mac) { + *digest = magma_omac(); + } else if (nid == NID_grasshopper_mac) { + *digest = grasshopper_omac(); } else { ok = 0; *digest = NULL; @@@ -296,6 -331,10 +332,10 @@@ static int gost_ciphers(ENGINE* e, cons *cipher = cipher_gost_grasshopper_ofb(); } else if (nid == NID_grasshopper_ctr) { *cipher = cipher_gost_grasshopper_ctr(); + } else if (nid == NID_magma_cbc) { + *cipher = cipher_magma_cbc(); + } else if (nid == NID_magma_ctr) { + *cipher = cipher_magma_ctr(); } else { ok = 0; *cipher = NULL; @@@ -326,6 -365,12 +366,12 @@@ static int gost_pkey_meths(ENGINE* e, E case NID_gost_mac_12: *pmeth = pmeth_Gost28147_MAC_12; return 1; + case NID_magma_mac: + *pmeth = pmeth_magma_mac; + return 1; + case NID_grasshopper_mac: + *pmeth = pmeth_grasshopper_mac; + return 1; default:; } @@@ -357,6 -402,12 +403,12 @@@ static int gost_pkey_asn1_meths(ENGINE case NID_gost_mac_12: *ameth = ameth_Gost28147_MAC_12; return 1; + case NID_magma_mac: + *ameth = ameth_magma_mac; + return 1; + case NID_grasshopper_mac: + *ameth = ameth_grasshopper_mac; + return 1; default:; } diff --combined gost_grasshopper_cipher.c index 2cce251,e7c8dd6..da68057 --- a/gost_grasshopper_cipher.c +++ b/gost_grasshopper_cipher.c @@@ -17,6 -17,7 +17,7 @@@ extern "C" #include #include + #include "gost_lcl.h" #include "e_gost_err.h" enum GRASSHOPPER_CIPHER_TYPE { @@@ -279,11 -280,10 +280,10 @@@ int gost_grasshopper_cipher_do_cbc(EVP_ return 1; } - /* increment counter (128-bit int) by 1 */ - static void ctr128_inc(unsigned char *counter) + void inc_counter(unsigned char* counter, size_t counter_bytes) { - unsigned int n = 16; unsigned char c; + unsigned int n = counter_bytes; do { --n; @@@ -294,6 -294,12 +294,12 @@@ } while (n); } + /* increment counter (128-bit int) by 1 */ + static void ctr128_inc(unsigned char *counter) + { + inc_counter(counter, 16); + } + int gost_grasshopper_cipher_do_ctr(EVP_CIPHER_CTX* ctx, unsigned char* out, const unsigned char* in, size_t inl) { gost_grasshopper_cipher_ctx_ctr* c = (gost_grasshopper_cipher_ctx_ctr*) EVP_CIPHER_CTX_get_cipher_data(ctx); @@@ -541,7 -547,7 +547,7 @@@ int gost_grasshopper_set_asn1_parameter if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) { OPENSSL_free(buf); - GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE); + GOSTerr(GOST_F_GOST_GRASSHOPPER_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE); return 0; } OPENSSL_free(buf); @@@ -564,13 -570,13 +570,13 @@@ int gost_grasshopper_cipher_ctl(EVP_CIP switch (type) { case EVP_CTRL_RAND_KEY: { if (RAND_bytes((unsigned char*) ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) { - GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_RNG_ERROR); + GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, GOST_R_RNG_ERROR); return -1; } break; } default: - GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND); + GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND); return -1; } return 1; @@@ -650,20 -656,6 +656,20 @@@ const GRASSHOPPER_INLINE EVP_CIPHER* ci return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE, GRASSHOPPER_CIPHER_CTR); } +void cipher_gost_grasshopper_destroy(void) +{ + EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB]); + gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB] = NULL; + EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC]); + gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC] = NULL; + EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB]); + gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB] = NULL; + EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB]); + gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB] = NULL; + EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR]); + gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR] = NULL; +} + #if defined(__cplusplus) } #endif