* Pack bignum into byte buffer of given size, filling all leading bytes by
* zeros
*/
-int store_bignum(BIGNUM *bn, unsigned char *buf, int len)
+int store_bignum(const BIGNUM *bn, unsigned char *buf, int len)
{
int bytes = BN_num_bytes(bn);
* Parses GOST algorithm parameters from X509_ALGOR and modifies pkey setting
* NID and parameters
*/
-static int decode_gost_algor_params(EVP_PKEY *pkey, X509_ALGOR *palg)
+static int decode_gost_algor_params(EVP_PKEY *pkey, const X509_ALGOR *palg)
{
- ASN1_OBJECT *palg_obj = NULL;
+ const ASN1_OBJECT *palg_obj = NULL;
int ptype = V_ASN1_UNDEF;
int pkey_nid = NID_undef, param_nid = NID_undef;
ASN1_STRING *pval = NULL;
if (!pkey || !palg)
return 0;
- X509_ALGOR_get0(&palg_obj, &ptype, (void **)&pval, palg);
+ X509_ALGOR_get0(&palg_obj, &ptype, (const void **)&pval, palg);
if (ptype != V_ASN1_SEQUENCE) {
GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
GOST_R_BAD_KEY_PARAMETERS_FORMAT);
return pknum_masked;
}
-static int priv_decode_gost(EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf)
+static int priv_decode_gost(EVP_PKEY *pk, const PKCS8_PRIV_KEY_INFO *p8inf)
{
const unsigned char *pkey_buf = NULL, *p = NULL;
int priv_len = 0;
BIGNUM *pk_num = NULL;
int ret = 0;
- X509_ALGOR *palg = NULL;
- ASN1_OBJECT *palg_obj = NULL;
+ const X509_ALGOR *palg = NULL;
+ const ASN1_OBJECT *palg_obj = NULL;
ASN1_INTEGER *priv_key = NULL;
int expected_key_len = 32;
store_bignum(X, databuf + data_len / 2, data_len / 2);
store_bignum(Y, databuf, data_len / 2);
+ BUF_reverse(NULL, databuf, data_len);
+
octet = ASN1_OCTET_STRING_new();
if (octet == NULL) {
GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
goto err;
}
- ASN1_STRING_set(octet, NULL, data_len);
- sptr = ASN1_STRING_data(octet);
- for (i = 0, j = data_len - 1; i < data_len; i++, j--) {
- sptr[i] = databuf[j];
+
+ if (0 == ASN1_STRING_set(octet, databuf, data_len)) {
+ GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
+ goto err;
}
ret = i2d_ASN1_OCTET_STRING(octet, &buf);
BIGNUM *r = NULL, *s = NULL, *X = NULL, *tmp = NULL, *tmp2 = NULL,
*k = NULL, *e = NULL;
- BIGNUM *new_r = NULL, *new_s = NULL;
+ const BIGNUM *new_r = NULL, *new_s = NULL;
EC_POINT *C = NULL;
BN_CTX *ctx;
}
while (BN_is_zero(s));
- DSA_SIG_get0(&new_r, &new_s, newsig);
+ DSA_SIG_get0(newsig, &new_r, &new_s);
new_s = BN_dup(s);
new_r = BN_dup(r);
if (!new_s || !new_r) {
BIGNUM *order;
BIGNUM *md = NULL, *e = NULL, *R = NULL, *v = NULL,
*z1 = NULL, *z2 = NULL;
- BIGNUM *sig_s = NULL, *sig_r = NULL;
+ const BIGNUM *sig_s = NULL, *sig_r = NULL;
BIGNUM *X = NULL, *tmp = NULL;
EC_POINT *C = NULL;
const EC_POINT *pub_key = NULL;
goto err;
}
- DSA_SIG_get0(&sig_r, &sig_s, sig);
+ DSA_SIG_get0(sig, &sig_r, &sig_s);
if (BN_is_zero(sig_s) || BN_is_zero(sig_r) ||
(BN_cmp(sig_s, order) >= 1) || (BN_cmp(sig_r, order) >= 1)) {
};
/* Set 256 bit key into context */
-static GRASSHOPPER_INLINE void gost_grasshopper_cipher_key(gost_grasshopper_cipher_ctx* c, const uint8_t* k) {
+GRASSHOPPER_INLINE void gost_grasshopper_cipher_key(gost_grasshopper_cipher_ctx* c, const uint8_t* k) {
int i;
for (i = 0; i < 2; i++) {
grasshopper_copy128(&c->key.k.k[i], (const grasshopper_w128_t*) (k + i * 16));
}
/* Cleans up key from context */
-static GRASSHOPPER_INLINE void gost_grasshopper_cipher_destroy(gost_grasshopper_cipher_ctx* c) {
+GRASSHOPPER_INLINE void gost_grasshopper_cipher_destroy(gost_grasshopper_cipher_ctx* c) {
int i;
for (i = 0; i < 2; i++) {
grasshopper_zero128(&c->key.k.k[i]);
ctx->counter = 0;
}
-static int gost_grasshopper_cipher_init(EVP_CIPHER_CTX* ctx, const unsigned char* key,
+int gost_grasshopper_cipher_init(EVP_CIPHER_CTX* ctx, const unsigned char* key,
const unsigned char* iv, int enc) {
gost_grasshopper_cipher_ctx* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
return 1;
}
-static GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ecb(EVP_CIPHER_CTX* ctx, const unsigned char* key,
+GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ecb(EVP_CIPHER_CTX* ctx, const unsigned char* key,
const unsigned char* iv,
int enc) {
gost_grasshopper_cipher_ctx* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
return gost_grasshopper_cipher_init(ctx, key, iv, enc);
}
-static GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_cbc(EVP_CIPHER_CTX* ctx, const unsigned char* key,
+GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_cbc(EVP_CIPHER_CTX* ctx, const unsigned char* key,
const unsigned char* iv,
int enc) {
gost_grasshopper_cipher_ctx* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
return gost_grasshopper_cipher_init(ctx, key, iv, enc);
}
-static GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ofb(EVP_CIPHER_CTX* ctx, const unsigned char* key,
+GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ofb(EVP_CIPHER_CTX* ctx, const unsigned char* key,
const unsigned char* iv,
int enc) {
gost_grasshopper_cipher_ctx_ofb* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
return gost_grasshopper_cipher_init(ctx, key, iv, enc);
}
-static GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_cfb(EVP_CIPHER_CTX* ctx, const unsigned char* key,
+GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_cfb(EVP_CIPHER_CTX* ctx, const unsigned char* key,
const unsigned char* iv,
int enc) {
gost_grasshopper_cipher_ctx* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
return gost_grasshopper_cipher_init(ctx, key, iv, enc);
}
-static GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ctr(EVP_CIPHER_CTX* ctx, const unsigned char* key,
+GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ctr(EVP_CIPHER_CTX* ctx, const unsigned char* key,
const unsigned char* iv,
int enc) {
gost_grasshopper_cipher_ctx_ctr* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
return gost_grasshopper_cipher_init(ctx, key, iv, enc);
}
-static GRASSHOPPER_INLINE int gost_grasshopper_cipher_do(EVP_CIPHER_CTX* ctx, unsigned char* out,
+GRASSHOPPER_INLINE int gost_grasshopper_cipher_do(EVP_CIPHER_CTX* ctx, unsigned char* out,
const unsigned char* in, size_t inl) {
gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
struct GRASSHOPPER_CIPHER_PARAMS* params = &gost_cipher_params[c->type];
return params->do_cipher(ctx, out, in, inl);
}
-static int gost_grasshopper_cipher_do_ecb(EVP_CIPHER_CTX* ctx, unsigned char* out,
+int gost_grasshopper_cipher_do_ecb(EVP_CIPHER_CTX* ctx, unsigned char* out,
const unsigned char* in, size_t inl) {
gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
return 1;
}
-static int gost_grasshopper_cipher_do_cbc(EVP_CIPHER_CTX* ctx, unsigned char* out,
+int gost_grasshopper_cipher_do_cbc(EVP_CIPHER_CTX* ctx, unsigned char* out,
const unsigned char* in, size_t inl) {
gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
return 1;
}
-static int gost_grasshopper_cipher_do_ctr(EVP_CIPHER_CTX* ctx, unsigned char* out,
+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);
unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
grasshopper_encrypt_block(&ctx->c.encrypt_round_keys, &ctx->buffer1, buf, &ctx->c.buffer);
}
-static int gost_grasshopper_cipher_do_ofb(EVP_CIPHER_CTX* ctx, unsigned char* out,
+int gost_grasshopper_cipher_do_ofb(EVP_CIPHER_CTX* ctx, unsigned char* out,
const unsigned char* in, size_t inl) {
gost_grasshopper_cipher_ctx_ofb* c = (gost_grasshopper_cipher_ctx_ofb*) EVP_CIPHER_CTX_get_cipher_data(ctx);
const unsigned char* in_ptr = in;
return 1;
}
-static int gost_grasshopper_cipher_do_cfb(EVP_CIPHER_CTX* ctx, unsigned char* out,
+int gost_grasshopper_cipher_do_cfb(EVP_CIPHER_CTX* ctx, unsigned char* out,
const unsigned char* in, size_t inl) {
gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
const unsigned char* in_ptr = in;
return 1;
}
-static int gost_grasshopper_cipher_cleanup(EVP_CIPHER_CTX* ctx) {
+int gost_grasshopper_cipher_cleanup(EVP_CIPHER_CTX* ctx) {
gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
struct GRASSHOPPER_CIPHER_PARAMS* params = &gost_cipher_params[c->type];
return 1;
}
-static int gost_grasshopper_set_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params) {
+int gost_grasshopper_set_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params) {
int len = 0;
unsigned char* buf = NULL;
unsigned char* p = NULL;
return 1;
}
-static GRASSHOPPER_INLINE int gost_grasshopper_get_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params) {
+GRASSHOPPER_INLINE int gost_grasshopper_get_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params) {
int ret = -1;
if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
return 1;
}
-static int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX* ctx, int type, int arg, void* ptr) {
+int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX* ctx, int type, int arg, void* ptr) {
switch (type) {
case EVP_CTRL_RAND_KEY: {
if (RAND_bytes((unsigned char*) ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
return 1;
}
-static GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_create(int cipher_type, int block_size) {
+GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_create(int cipher_type, int block_size) {
return EVP_CIPHER_meth_new(cipher_type,
block_size /* block_size */,
GRASSHOPPER_KEY_SIZE /* key_size */);
EVP_CIPHER_meth_set_do_cipher(cipher, gost_grasshopper_cipher_do);
}
-static const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper(uint8_t mode, uint8_t num) {
+const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper(uint8_t mode, uint8_t num) {
EVP_CIPHER** cipher;
struct GRASSHOPPER_CIPHER_PARAMS* params;
typedef void (* grasshopper_destroy_cipher_func)(gost_grasshopper_cipher_ctx* c);
-static void gost_grasshopper_cipher_key(gost_grasshopper_cipher_ctx* c, const uint8_t* k);
+void gost_grasshopper_cipher_key(gost_grasshopper_cipher_ctx* c, const uint8_t* k);
-static void gost_grasshopper_cipher_destroy(gost_grasshopper_cipher_ctx* c);
+void gost_grasshopper_cipher_destroy(gost_grasshopper_cipher_ctx* c);
-static int gost_grasshopper_cipher_init_ecb(EVP_CIPHER_CTX* ctx, const unsigned char* key, const unsigned char* iv, int enc);
+int gost_grasshopper_cipher_init_ecb(EVP_CIPHER_CTX* ctx, const unsigned char* key, const unsigned char* iv, int enc);
-static int gost_grasshopper_cipher_init_cbc(EVP_CIPHER_CTX* ctx, const unsigned char* key, const unsigned char* iv, int enc);
+int gost_grasshopper_cipher_init_cbc(EVP_CIPHER_CTX* ctx, const unsigned char* key, const unsigned char* iv, int enc);
-static int gost_grasshopper_cipher_init_ofb(EVP_CIPHER_CTX* ctx, const unsigned char* key, const unsigned char* iv, int enc);
+int gost_grasshopper_cipher_init_ofb(EVP_CIPHER_CTX* ctx, const unsigned char* key, const unsigned char* iv, int enc);
-static int gost_grasshopper_cipher_init_cfb(EVP_CIPHER_CTX* ctx, const unsigned char* key, const unsigned char* iv, int enc);
+int gost_grasshopper_cipher_init_cfb(EVP_CIPHER_CTX* ctx, const unsigned char* key, const unsigned char* iv, int enc);
-static int gost_grasshopper_cipher_init_ctr(EVP_CIPHER_CTX* ctx, const unsigned char* key, const unsigned char* iv, int enc);
+int gost_grasshopper_cipher_init_ctr(EVP_CIPHER_CTX* ctx, const unsigned char* key, const unsigned char* iv, int enc);
-static int gost_grasshopper_cipher_init(EVP_CIPHER_CTX* ctx, const unsigned char* key,
- const unsigned char* iv, int enc);
+int gost_grasshopper_cipher_init(EVP_CIPHER_CTX* ctx, const unsigned char* key,
+ const unsigned char* iv, int enc);
-static int gost_grasshopper_cipher_do(EVP_CIPHER_CTX* ctx, unsigned char* out,
- const unsigned char* in, size_t inl);
+int gost_grasshopper_cipher_do(EVP_CIPHER_CTX* ctx, unsigned char* out,
+ const unsigned char* in, size_t inl);
-static int gost_grasshopper_cipher_do_ecb(EVP_CIPHER_CTX* ctx, unsigned char* out,
- const unsigned char* in, size_t inl);
+int gost_grasshopper_cipher_do_ecb(EVP_CIPHER_CTX* ctx, unsigned char* out,
+ const unsigned char* in, size_t inl);
-static int gost_grasshopper_cipher_do_cbc(EVP_CIPHER_CTX* ctx, unsigned char* out,
- const unsigned char* in, size_t inl);
+int gost_grasshopper_cipher_do_cbc(EVP_CIPHER_CTX* ctx, unsigned char* out,
+ const unsigned char* in, size_t inl);
-static int gost_grasshopper_cipher_do_ofb(EVP_CIPHER_CTX* ctx, unsigned char* out,
- const unsigned char* in, size_t inl);
+int gost_grasshopper_cipher_do_ofb(EVP_CIPHER_CTX* ctx, unsigned char* out,
+ const unsigned char* in, size_t inl);
-static int gost_grasshopper_cipher_do_cfb(EVP_CIPHER_CTX* ctx, unsigned char* out,
- const unsigned char* in, size_t inl);
+int gost_grasshopper_cipher_do_cfb(EVP_CIPHER_CTX* ctx, unsigned char* out,
+ const unsigned char* in, size_t inl);
-static int gost_grasshopper_cipher_do_ctr(EVP_CIPHER_CTX* ctx, unsigned char* out,
- const unsigned char* in, size_t inl);
+int gost_grasshopper_cipher_do_ctr(EVP_CIPHER_CTX* ctx, unsigned char* out,
+ const unsigned char* in, size_t inl);
-static int gost_grasshopper_cipher_cleanup(EVP_CIPHER_CTX* ctx);
+int gost_grasshopper_cipher_cleanup(EVP_CIPHER_CTX* ctx);
-static int gost_grasshopper_set_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params);
+int gost_grasshopper_set_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params);
-static int gost_grasshopper_get_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params);
+int gost_grasshopper_get_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params);
-static int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX* ctx, int type, int arg, void* ptr);
+int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX* ctx, int type, int arg, void* ptr);
-static EVP_CIPHER* cipher_gost_grasshopper_create(int cipher_type, int block_size);
+EVP_CIPHER* cipher_gost_grasshopper_create(int cipher_type, int block_size);
-static const int cipher_gost_grasshopper_setup(EVP_CIPHER* cipher, uint8_t mode, int iv_size, bool padding);
+const int cipher_gost_grasshopper_setup(EVP_CIPHER* cipher, uint8_t mode, int iv_size, bool padding);
-static const EVP_CIPHER* cipher_gost_grasshopper(uint8_t mode, uint8_t num);
+const EVP_CIPHER* cipher_gost_grasshopper(uint8_t mode, uint8_t num);
extern const EVP_CIPHER* cipher_gost_grasshopper_ecb();
extern const EVP_CIPHER* cipher_gost_grasshopper_cbc();
#include "gost_grasshopper_defines.h"
#include "gost_grasshopper_math.h"
-grasshopper_w128_t grasshopper_pil_enc128[GRASSHOPPER_MAX_BIT_PARTS][256] = {
+const grasshopper_w128_t grasshopper_pil_enc128[GRASSHOPPER_MAX_BIT_PARTS][256] = {
{
{
233, 251, 213, 12, 122, 192, 128, 150, 25, 17, 135, 147, 27, 201, 174, 181,
},
},
};
-grasshopper_w128_t grasshopper_pil_dec128[GRASSHOPPER_MAX_BIT_PARTS][256] = {
+const grasshopper_w128_t grasshopper_pil_dec128[GRASSHOPPER_MAX_BIT_PARTS][256] = {
{
{
165, 204, 14, 134, 194, 79, 186, 89, 59, 227, 239, 121, 130, 83, 17, 240,
},
},
};
-grasshopper_w128_t grasshopper_l_dec128[GRASSHOPPER_MAX_BIT_PARTS][256] = {
+const grasshopper_w128_t grasshopper_l_dec128[GRASSHOPPER_MAX_BIT_PARTS][256] = {
{
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
#include "gost_grasshopper_defines.h"
#include "gost_grasshopper_math.h"
-extern grasshopper_w128_t grasshopper_pil_enc128[GRASSHOPPER_MAX_BIT_PARTS][256];
+extern const grasshopper_w128_t grasshopper_pil_enc128[GRASSHOPPER_MAX_BIT_PARTS][256];
-extern grasshopper_w128_t grasshopper_l_dec128[GRASSHOPPER_MAX_BIT_PARTS][256];
+extern const grasshopper_w128_t grasshopper_l_dec128[GRASSHOPPER_MAX_BIT_PARTS][256];
-extern grasshopper_w128_t grasshopper_pil_dec128[GRASSHOPPER_MAX_BIT_PARTS][256];
+extern const grasshopper_w128_t grasshopper_pil_dec128[GRASSHOPPER_MAX_BIT_PARTS][256];
#endif
* Store bignum in byte array of given length, prepending by zeros if
* nesseccary
*/
-int store_bignum(BIGNUM *bn, unsigned char *buf, int len);
+int store_bignum(const BIGNUM *bn, unsigned char *buf, int len);
/* Pack GOST R 34.10 signature according to CryptoPro rules */
int pack_sign_cp(DSA_SIG *s, int order, unsigned char *sig, size_t *siglen);
/* from ameth.c */
*/
int pack_sign_cp(DSA_SIG *s, int order, unsigned char *sig, size_t *siglen)
{
- BIGNUM *sig_r = NULL, *sig_s = NULL;
- DSA_SIG_get0(&sig_r, &sig_s, s);
+ const BIGNUM *sig_r = NULL, *sig_s = NULL;
+ DSA_SIG_get0(s, &sig_r, &sig_s);
*siglen = 2 * order;
memset(sig, 0, *siglen);
store_bignum(sig_s, sig, order);
DSA_SIG *unpack_cp_signature(const unsigned char *sig, size_t siglen)
{
DSA_SIG *s;
- BIGNUM *sig_r = NULL, *sig_s = NULL;
+ const BIGNUM *sig_r = NULL, *sig_s = NULL;
s = DSA_SIG_new();
if (s == NULL) {
GOSTerr(GOST_F_UNPACK_CP_SIGNATURE, ERR_R_MALLOC_FAILURE);
return NULL;
}
- DSA_SIG_get0(&sig_r, &sig_s, s);
+ DSA_SIG_get0(s, &sig_r, &sig_s);
sig_s = BN_bin2bn(sig, siglen / 2, NULL);
sig_r = BN_bin2bn(sig + siglen / 2, siglen / 2, NULL);
return s;