From 12be7c21781eba9f7a37762e7e307571225c6c47 Mon Sep 17 00:00:00 2001 From: Dmitry Belyavskiy Date: Thu, 30 Aug 2018 11:27:23 +0300 Subject: [PATCH] Indentation --- gost12sum.c | 10 +- gost89.c | 119 +++++++++++------------ gost_ameth.c | 23 ++--- gost_crypt.c | 118 ++++++++++------------- gost_ctl.c | 5 +- gost_ec_keyx.c | 19 ++-- gost_ec_sign.c | 10 +- gost_omac.c | 256 +++++++++++++++++++++++-------------------------- 8 files changed, 267 insertions(+), 293 deletions(-) diff --git a/gost12sum.c b/gost12sum.c index 15e2941..f643b8b 100644 --- a/gost12sum.c +++ b/gost12sum.c @@ -11,14 +11,14 @@ #include #include #ifdef _MSC_VER -#include "getopt.h" +# include "getopt.h" # ifndef PATH_MAX # define PATH_MAX _MAX_PATH # endif -#include +# include typedef SSIZE_T ssize_t; #else -#include +# include #endif #include #include @@ -224,7 +224,7 @@ int hash_stream(gost_hash_ctx * ctx, int fd, char *sum) { unsigned char buffer[BUF_SIZE]; ssize_t bytes; - size_t i; + size_t i; start_hash12(ctx); while ((bytes = read(fd, buffer, BUF_SIZE)) > 0) { @@ -269,7 +269,7 @@ int get_line(FILE *f, char *hash, char *filename, int verbose) return 1; nextline: if (verbose) - printf("%s\n", filename); + printf("%s\n", filename); } return 0; } diff --git a/gost89.c b/gost89.c index 8b1ce06..670dfb5 100644 --- a/gost89.c +++ b/gost89.c @@ -243,10 +243,10 @@ const byte CryptoProKeyMeshingKey[] = { }; const byte ACPKM_D_const[] = { - 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, - 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, - 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, - 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, + 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, + 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, }; /* Initialization of gost_ctx subst blocks*/ @@ -462,8 +462,8 @@ void magma_key(gost_ctx * c, const byte * k) int i, j; for (i = 0, j = 0; i < 8; i++, j += 4) { c->k[i] = - k[j+3] | (k[j + 2] << 8) | (k[j + 1] << 16) | ((word32) k[j] << - 24); + k[j + 3] | (k[j + 2] << 8) | (k[j + 1] << 16) | ((word32) k[j] << + 24); } } @@ -632,29 +632,27 @@ void cryptopro_key_meshing(gost_ctx * ctx, unsigned char *iv) memcpy(iv, newiv, 8); } -void acpkm_magma_key_meshing(gost_ctx *ctx) +void acpkm_magma_key_meshing(gost_ctx * ctx) { unsigned char newkey[32]; - int i, j; - unsigned char buf[8], keybuf[8]; - - for (i = 0; i < 4; i++) - { - for (j = 0; j < 8; j++) - { - buf[j] = ACPKM_D_const[8*i + 7-j]; - } - gostcrypt(ctx, buf, keybuf); - memcpy(newkey+8*i, keybuf+4, 4); - memcpy(newkey+8*i+4, keybuf, 4); - } + int i, j; + unsigned char buf[8], keybuf[8]; + + for (i = 0; i < 4; i++) { + for (j = 0; j < 8; j++) { + buf[j] = ACPKM_D_const[8 * i + 7 - j]; + } + gostcrypt(ctx, buf, keybuf); + memcpy(newkey + 8 * i, keybuf + 4, 4); + memcpy(newkey + 8 * i + 4, keybuf, 4); + } /* set new key */ gost_key(ctx, newkey); } #ifdef ENABLE_UNIT_TESTS -#include -#include +# include +# include static void hexdump(FILE *f, const char *title, const unsigned char *s, int l) { @@ -671,45 +669,44 @@ static void hexdump(FILE *f, const char *title, const unsigned char *s, int l) int main(void) { - const unsigned char initial_key[] = { - 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, - 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, - 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, - 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF - }; - - const unsigned char meshed_key[] = { - 0x86, 0x3E, 0xA0, 0x17, 0x84, 0x2C, 0x3D, 0x37, - 0x2B, 0x18, 0xA8, 0x5A, 0x28, 0xE2, 0x31, 0x7D, - 0x74, 0xBE, 0xFC, 0x10, 0x77, 0x20, 0xDE, 0x0C, - 0x9E, 0x8A, 0xB9, 0x74, 0xAB, 0xD0, 0x0C, 0xA0, - }; - - unsigned char buf[32]; - - gost_ctx ctx; - kboxinit(&ctx, &Gost28147_TC26ParamSetZ); - magma_key(&ctx, initial_key); - magma_get_key(&ctx, buf); - - hexdump(stdout, "Initial key", buf, 32); - - acpkm_magma_key_meshing(&ctx); - magma_get_key(&ctx, buf); - hexdump(stdout, "Meshed key - K2", buf, 32); - - if (memcmp(meshed_key, buf, 32)) - { - fprintf(stderr, "Magma meshing failed"); - } - - acpkm_magma_key_meshing(&ctx); - magma_get_key(&ctx, buf); - hexdump(stdout, "Meshed key - K3", buf, 32); - - acpkm_magma_key_meshing(&ctx); - magma_get_key(&ctx, buf); - hexdump(stdout, "Meshed key - K4", buf, 32); + const unsigned char initial_key[] = { + 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, + 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, + 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, + 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF + }; + + const unsigned char meshed_key[] = { + 0x86, 0x3E, 0xA0, 0x17, 0x84, 0x2C, 0x3D, 0x37, + 0x2B, 0x18, 0xA8, 0x5A, 0x28, 0xE2, 0x31, 0x7D, + 0x74, 0xBE, 0xFC, 0x10, 0x77, 0x20, 0xDE, 0x0C, + 0x9E, 0x8A, 0xB9, 0x74, 0xAB, 0xD0, 0x0C, 0xA0, + }; + + unsigned char buf[32]; + + gost_ctx ctx; + kboxinit(&ctx, &Gost28147_TC26ParamSetZ); + magma_key(&ctx, initial_key); + magma_get_key(&ctx, buf); + + hexdump(stdout, "Initial key", buf, 32); + + acpkm_magma_key_meshing(&ctx); + magma_get_key(&ctx, buf); + hexdump(stdout, "Meshed key - K2", buf, 32); + + if (memcmp(meshed_key, buf, 32)) { + fprintf(stderr, "Magma meshing failed"); + } + + acpkm_magma_key_meshing(&ctx); + magma_get_key(&ctx, buf); + hexdump(stdout, "Meshed key - K3", buf, 32); + + acpkm_magma_key_meshing(&ctx); + magma_get_key(&ctx, buf); + hexdump(stdout, "Meshed key - K4", buf, 32); } diff --git a/gost_ameth.c b/gost_ameth.c index 5e20f1e..84f1491 100644 --- a/gost_ameth.c +++ b/gost_ameth.c @@ -138,7 +138,8 @@ static int gost_decode_nid_params(EVP_PKEY *pkey, int pkey_nid, int param_nid) * Parses GOST algorithm parameters from X509_ALGOR and modifies pkey setting * NID and parameters */ -static int decode_gost_algor_params(EVP_PKEY *pkey, OPENSSL110_const X509_ALGOR *palg) +static int decode_gost_algor_params(EVP_PKEY *pkey, + OPENSSL110_const X509_ALGOR *palg) { OPENSSL110_const ASN1_OBJECT *palg_obj = NULL; int ptype = V_ASN1_UNDEF; @@ -297,8 +298,7 @@ static void pkey_free_gost_ec(EVP_PKEY *key) /* ------------------ private key functions -----------------------------*/ static BIGNUM *unmask_priv_key(EVP_PKEY *pk, - const unsigned char *buf, int len, - int num_masks) + const unsigned char *buf, int len, int num_masks) { BIGNUM *pknum_masked = NULL, *q = NULL; const EC_KEY *key_ptr = (pk) ? EVP_PKEY_get0(pk) : NULL; @@ -338,7 +338,8 @@ static BIGNUM *unmask_priv_key(EVP_PKEY *pk, return pknum_masked; } -static int priv_decode_gost(EVP_PKEY *pk, OPENSSL110_const PKCS8_PRIV_KEY_INFO *p8inf) +static int priv_decode_gost(EVP_PKEY *pk, + OPENSSL110_const PKCS8_PRIV_KEY_INFO *p8inf) { const unsigned char *pkey_buf = NULL, *p = NULL; int priv_len = 0; @@ -450,7 +451,7 @@ static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk) buf[key_len - 1 - i] = tmp; } - if(pk_format != NULL && strcmp(pk_format, PK_WRAP_PARAM) == 0) { + if (pk_format != NULL && strcmp(pk_format, PK_WRAP_PARAM) == 0) { ASN1_STRING *octet = NULL; int priv_len = 0; unsigned char *priv_buf = NULL; @@ -462,7 +463,7 @@ static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk) OPENSSL_free(buf); return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params, - priv_buf, priv_len); + priv_buf, priv_len); } return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params, @@ -681,7 +682,7 @@ static int pub_decode_gost_ec(EVP_PKEY *pk, X509_PUBKEY *pub) return 0; } - BUF_reverse(databuf, octet->data, octet->length); + BUF_reverse(databuf, octet->data, octet->length); len = octet->length / 2; ASN1_OCTET_STRING_free(octet); @@ -722,9 +723,9 @@ static int pub_encode_gost_ec(X509_PUBKEY *pub, const EVP_PKEY *pk) algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk)); - ASN1_STRING *params = encode_gost_algor_params(pk); - pval = params; - ptype = V_ASN1_SEQUENCE; + ASN1_STRING *params = encode_gost_algor_params(pk); + pval = params; + ptype = V_ASN1_SEQUENCE; order = BN_new(); if (!order) { @@ -758,7 +759,7 @@ static int pub_encode_gost_ec(X509_PUBKEY *pub, const EVP_PKEY *pk) store_bignum(X, databuf + data_len / 2, data_len / 2); store_bignum(Y, databuf, data_len / 2); - BUF_reverse(databuf, NULL, data_len); + BUF_reverse(databuf, NULL, data_len); octet = ASN1_OCTET_STRING_new(); if (octet == NULL) { diff --git a/gost_crypt.c b/gost_crypt.c index e248305..930d40a 100644 --- a/gost_crypt.c +++ b/gost_crypt.c @@ -47,12 +47,12 @@ static int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params); static int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr); static int magma_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc); + const unsigned char *iv, int enc); /* Handles block of data in CBC mode */ static int magma_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl); + const unsigned char *in, size_t inl); static int magma_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl); + const unsigned char *in, size_t inl); static EVP_CIPHER *_hidden_Gost28147_89_cipher = NULL; const EVP_CIPHER *cipher_gost(void) @@ -152,8 +152,7 @@ const EVP_CIPHER *cipher_gost_cpacnt(void) gost89_set_asn1_parameters) || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_gost89_cnt, gost89_get_asn1_parameters) - || !EVP_CIPHER_meth_set_ctrl(_hidden_gost89_cnt, - gost_cipher_ctl))) { + || !EVP_CIPHER_meth_set_ctrl(_hidden_gost89_cnt, gost_cipher_ctl))) { EVP_CIPHER_meth_free(_hidden_gost89_cnt); _hidden_gost89_cnt = NULL; } @@ -209,8 +208,7 @@ const EVP_CIPHER *cipher_magma_ctr(void) EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT) - || !EVP_CIPHER_meth_set_init(_hidden_magma_ctr, - magma_cipher_init) + || !EVP_CIPHER_meth_set_init(_hidden_magma_ctr, magma_cipher_init) || !EVP_CIPHER_meth_set_do_cipher(_hidden_magma_ctr, magma_cipher_do_ctr) || !EVP_CIPHER_meth_set_cleanup(_hidden_magma_ctr, @@ -222,8 +220,7 @@ const EVP_CIPHER *cipher_magma_ctr(void) gost89_set_asn1_parameters) || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_magma_ctr, gost89_get_asn1_parameters) - || !EVP_CIPHER_meth_set_ctrl(_hidden_magma_ctr, - gost_cipher_ctl))) { + || !EVP_CIPHER_meth_set_ctrl(_hidden_magma_ctr, gost_cipher_ctl))) { EVP_CIPHER_meth_free(_hidden_magma_ctr); _hidden_magma_ctr = NULL; } @@ -243,8 +240,7 @@ const EVP_CIPHER *cipher_magma_cbc(void) EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT) - || !EVP_CIPHER_meth_set_init(_hidden_magma_cbc, - magma_cipher_init) + || !EVP_CIPHER_meth_set_init(_hidden_magma_cbc, magma_cipher_init) || !EVP_CIPHER_meth_set_do_cipher(_hidden_magma_cbc, magma_cipher_do_cbc) || !EVP_CIPHER_meth_set_cleanup(_hidden_magma_cbc, @@ -256,8 +252,7 @@ const EVP_CIPHER *cipher_magma_cbc(void) gost89_set_asn1_parameters) || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_magma_cbc, gost89_get_asn1_parameters) - || !EVP_CIPHER_meth_set_ctrl(_hidden_magma_cbc, - gost_cipher_ctl))) { + || !EVP_CIPHER_meth_set_ctrl(_hidden_magma_cbc, gost_cipher_ctl))) { EVP_CIPHER_meth_free(_hidden_magma_cbc); _hidden_magma_cbc = NULL; } @@ -306,8 +301,7 @@ EVP_MD *imit_gost_cpa(void) || !EVP_MD_meth_set_result_size(md, 4) || !EVP_MD_meth_set_input_blocksize(md, 8) || !EVP_MD_meth_set_app_datasize(md, - sizeof(struct - ossl_gost_imit_ctx)) + sizeof(struct ossl_gost_imit_ctx)) || !EVP_MD_meth_set_flags(md, 0) || !EVP_MD_meth_set_init(md, gost_imit_init_cpa) || !EVP_MD_meth_set_update(md, gost_imit_update) @@ -338,8 +332,7 @@ EVP_MD *imit_gost_cp_12(void) || !EVP_MD_meth_set_result_size(md, 4) || !EVP_MD_meth_set_input_blocksize(md, 8) || !EVP_MD_meth_set_app_datasize(md, - sizeof(struct - ossl_gost_imit_ctx)) + sizeof(struct ossl_gost_imit_ctx)) || !EVP_MD_meth_set_flags(md, 0) || !EVP_MD_meth_set_init(md, gost_imit_init_cp_12) || !EVP_MD_meth_set_update(md, gost_imit_update) @@ -431,8 +424,7 @@ const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj) static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c, int nid) { const struct gost_cipher_info *param; - param = - get_encryption_params((nid == NID_undef ? NULL : OBJ_nid2obj(nid))); + param = get_encryption_params((nid == NID_undef ? NULL : OBJ_nid2obj(nid))); if (!param) return 0; @@ -467,9 +459,9 @@ static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx, } static int magma_cipher_init_param(EVP_CIPHER_CTX *ctx, - const unsigned char *key, - const unsigned char *iv, int enc, - int paramNID, int mode) + const unsigned char *key, + const unsigned char *iv, int enc, + int paramNID, int mode) { struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx); if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) { @@ -539,10 +531,10 @@ int gost_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char *key, /* Initializes EVP_CIPHER_CTX with default values */ int magma_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) + const unsigned char *iv, int enc) { return magma_cipher_init_param(ctx, key, iv, enc, NID_undef, - EVP_CIPH_CBC_MODE); + EVP_CIPH_CBC_MODE); } /* @@ -633,10 +625,10 @@ int gost_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out, /* MAGMA encryption in CBC mode */ int magma_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) + const unsigned char *in, size_t inl) { unsigned char b[8]; - unsigned char d[8]; + unsigned char d[8]; const unsigned char *in_ptr = in; unsigned char *out_ptr = out; int i; @@ -646,12 +638,12 @@ int magma_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out, while (inl > 0) { for (i = 0; i < 8; i++) { - b[7-i] = iv[i] ^ in_ptr[i]; + b[7 - i] = iv[i] ^ in_ptr[i]; } gostcrypt(&(c->cctx), b, d); for (i = 0; i < 8; i++) { - out_ptr[7-i] = d[i]; + out_ptr[7 - i] = d[i]; } memcpy(iv, out_ptr, 8); out_ptr += 8; @@ -661,11 +653,11 @@ int magma_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out, } else { while (inl > 0) { for (i = 0; i < 8; i++) { - d[7-i] = in_ptr[i]; + d[7 - i] = in_ptr[i]; } gostdecrypt(&(c->cctx), d, b); for (i = 0; i < 8; i++) { - out_ptr[i] = iv[i] ^ b[7-i]; + out_ptr[i] = iv[i] ^ b[7 - i]; } memcpy(iv, in_ptr, 8); out_ptr += 8; @@ -679,12 +671,12 @@ int magma_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out, /* increment counter (64-bit int) by 1 */ static void ctr64_inc(unsigned char *counter) { - inc_counter(counter, 8); + inc_counter(counter, 8); } /* MAGMA encryption in CTR mode */ static int magma_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) + const unsigned char *in, size_t inl) { const unsigned char *in_ptr = in; unsigned char *out_ptr = out; @@ -698,7 +690,7 @@ static int magma_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out, if (EVP_CIPHER_CTX_num(ctx)) { for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl; j++, i++, in_ptr++, out_ptr++) { - *out_ptr = buf[7-j] ^ (*in_ptr); + *out_ptr = buf[7 - j] ^ (*in_ptr); } if (j == 8) { EVP_CIPHER_CTX_set_num(ctx, 0); @@ -709,34 +701,34 @@ static int magma_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out, } /* Process full blocks */ - for (; i + 8 <= inl; i += 8, in_ptr += 8, out_ptr += 8) { - for (j = 0; j < 8; j++) { - b[7-j] = iv[j]; - } - gostcrypt(&(c->cctx), b, buf); - for (j = 0; j < 8; j++) { - out_ptr[j] = buf[7-j] ^ in_ptr[j]; - } - ctr64_inc(iv); - } + for (; i + 8 <= inl; i += 8, in_ptr += 8, out_ptr += 8) { + for (j = 0; j < 8; j++) { + b[7 - j] = iv[j]; + } + gostcrypt(&(c->cctx), b, buf); + for (j = 0; j < 8; j++) { + out_ptr[j] = buf[7 - j] ^ in_ptr[j]; + } + ctr64_inc(iv); + } /* Process the rest of plaintext */ if (i < inl) { - for (j = 0; j < 8; j++) { - b[7-j] = iv[j]; - } - gostcrypt(&(c->cctx), iv, buf); - ctr64_inc(iv); - for (j = 0; i < inl; j++, i++) { - out_ptr[j] = buf[7-j] ^ in_ptr[j]; - } - - EVP_CIPHER_CTX_set_num(ctx, j); + for (j = 0; j < 8; j++) { + b[7 - j] = iv[j]; + } + gostcrypt(&(c->cctx), iv, buf); + ctr64_inc(iv); + for (j = 0; i < inl; j++, i++) { + out_ptr[j] = buf[7 - j] ^ in_ptr[j]; + } + + EVP_CIPHER_CTX_set_num(ctx, j); } else { - EVP_CIPHER_CTX_set_num(ctx, 0); - } + EVP_CIPHER_CTX_set_num(ctx, 0); + } - return 1; + return 1; } /* GOST encryption in CFB mode */ @@ -861,9 +853,8 @@ static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out, /* Cleaning up of EVP_CIPHER_CTX */ int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx) { - gost_destroy(& - ((struct ossl_gost_cipher_ctx *) - EVP_CIPHER_CTX_get_cipher_data(ctx))->cctx); + gost_destroy(&((struct ossl_gost_cipher_ctx *) + EVP_CIPHER_CTX_get_cipher_data(ctx))->cctx); EVP_CIPHER_CTX_set_app_data(ctx, NULL); return 1; } @@ -956,8 +947,7 @@ int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) return 1; } default: - GOSTerr(GOST_F_GOST_CIPHER_CTL, - GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND); + GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND); return -1; } return 1; @@ -1158,8 +1148,7 @@ int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr) return 1; case EVP_MD_CTRL_SET_KEY: { - struct ossl_gost_imit_ctx *gost_imit_ctx = - EVP_MD_CTX_md_data(ctx); + struct ossl_gost_imit_ctx *gost_imit_ctx = EVP_MD_CTX_md_data(ctx); if (EVP_MD_meth_get_init(EVP_MD_CTX_md(ctx)) (ctx) <= 0) { GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET); @@ -1171,8 +1160,7 @@ int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr) struct gost_mac_key *key = (struct gost_mac_key *)ptr; if (key->mac_param_nid != NID_undef) { const struct gost_cipher_info *param = - get_encryption_params(OBJ_nid2obj - (key->mac_param_nid)); + get_encryption_params(OBJ_nid2obj(key->mac_param_nid)); if (param == NULL) { GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_PARAMS); diff --git a/gost_ctl.c b/gost_ctl.c index 04be66d..889df6d 100644 --- a/gost_ctl.c +++ b/gost_ctl.c @@ -15,7 +15,8 @@ #include "gost_lcl.h" static char *gost_params[GOST_PARAM_MAX + 1] = { NULL }; -static const char *gost_envnames[] = { "CRYPT_PARAMS", "GOST_PBE_HMAC", "GOST_PK_FORMAT" }; +static const char *gost_envnames[] = + { "CRYPT_PARAMS", "GOST_PBE_HMAC", "GOST_PK_FORMAT" }; const ENGINE_CMD_DEFN gost_cmds[] = { {GOST_CTRL_CRYPT_PARAMS, @@ -26,7 +27,7 @@ const ENGINE_CMD_DEFN gost_cmds[] = { "PBE_PARAMS", "Shortname of default digest alg for PBE", ENGINE_CMD_FLAG_STRING}, - {GOST_CTRL_PK_FORMAT, + {GOST_CTRL_PK_FORMAT, "GOST_PK_FORMAT", "Private key format params", ENGINE_CMD_FLAG_STRING}, diff --git a/gost_ec_keyx.c b/gost_ec_keyx.c index 30d8e58..a32f4fd 100644 --- a/gost_ec_keyx.c +++ b/gost_ec_keyx.c @@ -33,7 +33,7 @@ static int VKO_compute_key(unsigned char *shared_key, size_t shared_key_size, NID_id_GostR3411_2012_256 : dgst_nid; int buf_len = (dgst_nid == NID_id_GostR3411_2012_512) ? 128 : 64, half_len = buf_len >> 1; - int ret = 0; + int ret = 0; if (!ctx) { GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE); @@ -60,8 +60,7 @@ static int VKO_compute_key(unsigned char *shared_key, size_t shared_key_size, Y = BN_CTX_get(ctx); EC_GROUP_get_order(EC_KEY_get0_group(priv_key), order, ctx); BN_mod_mul(p, key, UKM, order, ctx); - if (!EC_POINT_mul - (EC_KEY_get0_group(priv_key), pnt, NULL, pub_key, p, ctx)) { + if (!EC_POINT_mul(EC_KEY_get0_group(priv_key), pnt, NULL, pub_key, p, ctx)) { GOSTerr(GOST_F_VKO_COMPUTE_KEY, GOST_R_ERROR_POINT_MUL); goto err; } @@ -73,7 +72,7 @@ static int VKO_compute_key(unsigned char *shared_key, size_t shared_key_size, store_bignum(Y, databuf, half_len); store_bignum(X, databuf + half_len, half_len); /* And reverse byte order of whole buffer */ - BUF_reverse(databuf, NULL, buf_len); + BUF_reverse(databuf, NULL, buf_len); mdctx = EVP_MD_CTX_new(); if (!mdctx) { @@ -84,7 +83,7 @@ static int VKO_compute_key(unsigned char *shared_key, size_t shared_key_size, EVP_DigestInit_ex(mdctx, md, NULL); EVP_DigestUpdate(mdctx, databuf, buf_len); EVP_DigestFinal_ex(mdctx, shared_key, NULL); - ret = 32; + ret = 32; err: BN_free(UKM); @@ -185,11 +184,11 @@ int pkey_GOST_ECcp_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out, key_is_ephemeral = 1; if (out) { sec_key = EVP_PKEY_new(); - if (! EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new()) || - ! EVP_PKEY_copy_parameters(sec_key, pubk) || - ! gost_ec_keygen(EVP_PKEY_get0(sec_key))) { - GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, - GOST_R_ERROR_COMPUTING_SHARED_KEY); + if (!EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new()) + || !EVP_PKEY_copy_parameters(sec_key, pubk) + || !gost_ec_keygen(EVP_PKEY_get0(sec_key))) { + GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, + GOST_R_ERROR_COMPUTING_SHARED_KEY); goto err; } } diff --git a/gost_ec_sign.c b/gost_ec_sign.c index 2deb093..e1cfbf5 100644 --- a/gost_ec_sign.c +++ b/gost_ec_sign.c @@ -74,7 +74,8 @@ int fill_GOST_EC_params(EC_KEY *eckey, int nid) R3410_ec_params *params = gost_nid2params(nid); EC_GROUP *grp = NULL; EC_POINT *P = NULL; - BIGNUM *p = NULL, *q = NULL, *a = NULL, *b = NULL, *x = NULL, *y = NULL, *cofactor = NULL; + BIGNUM *p = NULL, *q = NULL, *a = NULL, *b = NULL, *x = NULL, *y = + NULL, *cofactor = NULL; BN_CTX *ctx; int ok = 0; @@ -95,7 +96,7 @@ int fill_GOST_EC_params(EC_KEY *eckey, int nid) x = BN_CTX_get(ctx); y = BN_CTX_get(ctx); q = BN_CTX_get(ctx); - cofactor = BN_CTX_get(ctx); + cofactor = BN_CTX_get(ctx); if (!p || !a || !b || !x || !y || !q || !cofactor) { GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE); goto end; @@ -104,7 +105,7 @@ int fill_GOST_EC_params(EC_KEY *eckey, int nid) if (!BN_hex2bn(&p, params->p) || !BN_hex2bn(&a, params->a) || !BN_hex2bn(&b, params->b) - || !BN_hex2bn(&cofactor, params->cofactor) ) { + || !BN_hex2bn(&cofactor, params->cofactor)) { GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR); goto end; } @@ -310,8 +311,7 @@ int gost_ec_verify(const unsigned char *dgst, int dgst_len, BN_CTX *ctx; const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL; BIGNUM *order; - BIGNUM *md = NULL, *e = NULL, *R = NULL, *v = NULL, - *z1 = NULL, *z2 = NULL; + BIGNUM *md = NULL, *e = NULL, *R = NULL, *v = NULL, *z1 = NULL, *z2 = NULL; const BIGNUM *sig_s = NULL, *sig_r = NULL; BIGNUM *X = NULL, *tmp = NULL; EC_POINT *C = NULL; diff --git a/gost_omac.c b/gost_omac.c index e78fd9d..49113a5 100644 --- a/gost_omac.c +++ b/gost_omac.c @@ -10,10 +10,10 @@ #define min(a,b) (((a) < (b)) ? (a) : (b)) typedef struct omac_ctx { - CMAC_CTX *cmac_ctx; - size_t dgst_size; - int cipher_nid; - int key_set; + CMAC_CTX *cmac_ctx; + size_t dgst_size; + int cipher_nid; + int key_set; } OMAC_CTX; #define MAX_GOST_OMAC_SIZE 16 @@ -21,19 +21,19 @@ typedef struct omac_ctx { static int omac_init(EVP_MD_CTX *ctx, int cipher_nid) { OMAC_CTX *c = EVP_MD_CTX_md_data(ctx); - memset(c, 0, sizeof(OMAC_CTX)); - c->cipher_nid = cipher_nid; - c->key_set = 0; - - switch(cipher_nid) { - case NID_magma_cbc: - c->dgst_size = 4; - break; - - case NID_grasshopper_cbc: - c->dgst_size = 8; - break; - } + memset(c, 0, sizeof(OMAC_CTX)); + c->cipher_nid = cipher_nid; + c->key_set = 0; + + switch (cipher_nid) { + case NID_magma_cbc: + c->dgst_size = 4; + break; + + case NID_grasshopper_cbc: + c->dgst_size = 8; + break; + } return 1; } @@ -51,27 +51,26 @@ static int grasshopper_imit_init(EVP_MD_CTX *ctx) static int omac_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count) { OMAC_CTX *c = EVP_MD_CTX_md_data(ctx); - if (!c->key_set) - { + if (!c->key_set) { GOSTerr(GOST_F_OMAC_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET); return 0; - } + } - return CMAC_Update(c->cmac_ctx, data, count); + return CMAC_Update(c->cmac_ctx, data, count); } int omac_imit_final(EVP_MD_CTX *ctx, unsigned char *md) { OMAC_CTX *c = EVP_MD_CTX_md_data(ctx); - unsigned char mac[MAX_GOST_OMAC_SIZE]; - size_t mac_size = sizeof(mac); + unsigned char mac[MAX_GOST_OMAC_SIZE]; + size_t mac_size = sizeof(mac); if (!c->key_set) { GOSTerr(GOST_F_OMAC_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET); return 0; } - CMAC_Final(c->cmac_ctx, mac, &mac_size); + CMAC_Final(c->cmac_ctx, mac, &mac_size); int md_size = EVP_MD_meth_get_result_size(EVP_MD_CTX_md(ctx)); memcpy(md, mac, min(md_size, c->dgst_size)); @@ -81,29 +80,26 @@ int omac_imit_final(EVP_MD_CTX *ctx, unsigned char *md) int omac_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from) { OMAC_CTX *c_to = EVP_MD_CTX_md_data(to); - const OMAC_CTX *c_from = EVP_MD_CTX_md_data(from); + const OMAC_CTX *c_from = EVP_MD_CTX_md_data(from); if (c_from && c_to) { - c_to->dgst_size = c_from->dgst_size; - c_to->cipher_nid = c_from->cipher_nid; - c_to->key_set = c_from->key_set; + c_to->dgst_size = c_from->dgst_size; + c_to->cipher_nid = c_from->cipher_nid; + c_to->key_set = c_from->key_set; + } else { + return 0; } - else - { - 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); + 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); } /* Clean up imit ctx */ @@ -111,32 +107,30 @@ int omac_imit_cleanup(EVP_MD_CTX *ctx) { OMAC_CTX *c = EVP_MD_CTX_md_data(ctx); - if (c) - { - CMAC_CTX_free(c->cmac_ctx); - memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(OMAC_CTX)); - } + if (c) { + CMAC_CTX_free(c->cmac_ctx); + memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(OMAC_CTX)); + } return 1; } -static int omac_key(OMAC_CTX *c, const EVP_CIPHER *cipher, const unsigned char *key, size_t key_size) +static int omac_key(OMAC_CTX * c, const EVP_CIPHER *cipher, + const unsigned char *key, size_t key_size) { - int ret = 0; - - c->cmac_ctx = CMAC_CTX_new(); - if (c->cmac_ctx == NULL) - { - GOSTerr(GOST_F_OMAC_KEY, ERR_R_MALLOC_FAILURE); - return 0; - } - - ret = CMAC_Init(c->cmac_ctx, key, key_size, cipher, NULL); - if (ret > 0) - { - c->key_set = 1; - } - return 1; -} + int ret = 0; + + c->cmac_ctx = CMAC_CTX_new(); + if (c->cmac_ctx == NULL) { + GOSTerr(GOST_F_OMAC_KEY, ERR_R_MALLOC_FAILURE); + return 0; + } + + ret = CMAC_Init(c->cmac_ctx, key, key_size, cipher, NULL); + if (ret > 0) { + c->key_set = 1; + } + return 1; +} int omac_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr) { @@ -145,76 +139,71 @@ int omac_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr) *((unsigned int *)(ptr)) = 32; return 1; case EVP_MD_CTRL_SET_KEY: - { - OMAC_CTX *c = EVP_MD_CTX_md_data(ctx); - 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) - { - GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_CIPHER_NOT_FOUND); - } - - if (EVP_MD_meth_get_init(EVP_MD_CTX_md(ctx)) (ctx) <= 0) { - GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET); - return 0; - } - EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT); - - if (c->key_set) - { - GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_BAD_ORDER); - return 0; - } - - if (arg == 0) { - struct gost_mac_key *key = (struct gost_mac_key *)ptr; - return omac_key(c, cipher, key->key, 32); - - } else if (arg == 32) { - return omac_key(c, cipher, ptr, 32); - } - GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE); - return 0; - } - case EVP_MD_CTRL_MAC_LEN: { - OMAC_CTX *c = EVP_MD_CTX_md_data(ctx); - switch (c->cipher_nid) - { - case NID_magma_cbc: - if (arg < 1 || arg > 8) { - GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE); + OMAC_CTX *c = EVP_MD_CTX_md_data(ctx); + 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) { + GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_CIPHER_NOT_FOUND); + } + + if (EVP_MD_meth_get_init(EVP_MD_CTX_md(ctx)) (ctx) <= 0) { + GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET); + return 0; + } + EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT); + + if (c->key_set) { + GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_BAD_ORDER); return 0; } - c->dgst_size = arg; - break; - case NID_grasshopper_cbc: - if (arg < 1 || arg > 16) { - GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE); + + if (arg == 0) { + struct gost_mac_key *key = (struct gost_mac_key *)ptr; + return omac_key(c, cipher, key->key, 32); + + } else if (arg == 32) { + return omac_key(c, cipher, ptr, 32); + } + GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE); + return 0; + } + case EVP_MD_CTRL_MAC_LEN: + { + OMAC_CTX *c = EVP_MD_CTX_md_data(ctx); + switch (c->cipher_nid) { + case NID_magma_cbc: + if (arg < 1 || arg > 8) { + GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE); + return 0; + } + c->dgst_size = arg; + break; + case NID_grasshopper_cbc: + if (arg < 1 || arg > 16) { + GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE); + return 0; + } + c->dgst_size = arg; + break; + default: return 0; } - c->dgst_size = arg; - break; - default: - return 0; - } - return 1; + return 1; } default: @@ -243,7 +232,7 @@ EVP_MD *magma_omac(void) EVP_MD_meth_free(md); md = NULL; } - _hidden_magma_mac_md = md; + _hidden_magma_mac_md = md; } return _hidden_magma_mac_md; } @@ -275,7 +264,7 @@ EVP_MD *grasshopper_omac(void) EVP_MD_meth_free(md); md = NULL; } - _hidden_grasshopper_mac_md = md; + _hidden_grasshopper_mac_md = md; } return _hidden_grasshopper_mac_md; } @@ -285,4 +274,3 @@ void grasshopper_omac_destroy(void) EVP_MD_meth_free(_hidden_grasshopper_mac_md); _hidden_grasshopper_mac_md = NULL; } - -- 2.39.2