#include <stdio.h>
#include <stdlib.h>
#ifdef _MSC_VER
-#include "getopt.h"
+# include "getopt.h"
# ifndef PATH_MAX
# define PATH_MAX _MAX_PATH
# endif
-#include <BaseTsd.h>
+# include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#else
-#include <unistd.h>
+# include <unistd.h>
#endif
#include <limits.h>
#include <fcntl.h>
{
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) {
return 1;
nextline:
if (verbose)
- printf("%s\n", filename);
+ printf("%s\n", filename);
}
return 0;
}
};
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*/
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);
}
}
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 <stdio.h>
-#include <string.h>
+# include <stdio.h>
+# include <string.h>
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);
}
* 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;
/* ------------------ 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;
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;
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;
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,
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);
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) {
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) {
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)
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;
}
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,
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;
}
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,
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;
}
|| !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)
|| !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)
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;
}
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) {
/* 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);
}
/*
/* 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;
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;
} 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;
/* 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;
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);
}
/* 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 */
/* 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;
}
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;
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);
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);
#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,
"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},
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);
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;
}
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) {
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);
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;
}
}
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;
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;
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;
}
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;
#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
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;
}
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));
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 */
{
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)
{
*((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:
EVP_MD_meth_free(md);
md = NULL;
}
- _hidden_magma_mac_md = md;
+ _hidden_magma_mac_md = md;
}
return _hidden_magma_mac_md;
}
EVP_MD_meth_free(md);
md = NULL;
}
- _hidden_grasshopper_mac_md = md;
+ _hidden_grasshopper_mac_md = md;
}
return _hidden_grasshopper_mac_md;
}
EVP_MD_meth_free(_hidden_grasshopper_mac_md);
_hidden_grasshopper_mac_md = NULL;
}
-