gost_lcl.h revision 256281
1327952Sdim#ifndef GOST_TOOLS_H 2284677Sdim#define GOST_TOOLS_H 3284677Sdim/********************************************************************** 4284677Sdim * gost_lcl.h * 5284677Sdim * Copyright (c) 2006 Cryptocom LTD * 6284677Sdim * This file is distributed under the same license as OpenSSL * 7284677Sdim * * 8284677Sdim * Internal declarations used in GOST engine * 9284677Sdim * OpenSSL 0.9.9 libraries required to compile and use * 10327952Sdim * this code * 11309124Sdim **********************************************************************/ 12284677Sdim#include <openssl/bn.h> 13285181Sdim#include <openssl/evp.h> 14309124Sdim#include <openssl/dsa.h> 15341825Sdim#include <openssl/asn1t.h> 16321369Sdim#include <openssl/x509.h> 17285181Sdim#include <openssl/engine.h> 18309124Sdim#include <openssl/ec.h> 19284677Sdim#include "gost89.h" 20314564Sdim#include "gosthash.h" 21321369Sdim/* Control commands */ 22321369Sdim#define GOST_PARAM_CRYPT_PARAMS 0 23309124Sdim#define GOST_PARAM_MAX 0 24284677Sdim#define GOST_CTRL_CRYPT_PARAMS (ENGINE_CMD_BASE+GOST_PARAM_CRYPT_PARAMS) 25314564Sdim 26284677Sdim extern const ENGINE_CMD_DEFN gost_cmds[]; 27284677Sdim int gost_control_func(ENGINE *e,int cmd, long i, void *p, void (*f)(void)); 28321369Sdim const char *get_gost_engine_param(int param); 29314564Sdim int gost_set_default_param(int param, const char *value); 30284677Sdim void gost_param_free(void); 31284677Sdim 32284677Sdim/* method registration */ 33314564Sdim 34284677Sdim int register_ameth_gost (int nid, EVP_PKEY_ASN1_METHOD **ameth, const char* pemstr, const char* info); 35284677Sdim int register_pmeth_gost (int id, EVP_PKEY_METHOD **pmeth, int flags); 36284677Sdim 37314564Sdim/* Gost-specific pmeth control-function parameters */ 38284677Sdim/* For GOST R34.10 parameters */ 39309124Sdim#define param_ctrl_string "paramset" 40284677Sdim#define EVP_PKEY_CTRL_GOST_PARAMSET (EVP_PKEY_ALG_CTRL+1) 41284677Sdim/* For GOST 28147 MAC */ 42284677Sdim#define key_ctrl_string "key" 43314564Sdim#define hexkey_ctrl_string "hexkey" 44327952Sdim#define EVP_PKEY_CTRL_GOST_MAC_HEXKEY (EVP_PKEY_ALG_CTRL+3) 45341825Sdim/* Pmeth internal representation */ 46314564Sdim struct gost_pmeth_data { 47327952Sdim int sign_param_nid; /* Should be set whenever parameters are filled */ 48314564Sdim EVP_MD *md; 49341825Sdim unsigned char *shared_ukm; 50314564Sdim int peer_key_used; 51314564Sdim }; 52344779Sdim 53284677Sdim struct gost_mac_pmeth_data { 54284677Sdim int key_set; 55314564Sdim EVP_MD *md; 56314564Sdim unsigned char key[32]; 57314564Sdim } ; 58314564Sdim/* GOST-specific ASN1 structures */ 59314564Sdim 60314564Sdim 61314564Sdimtypedef struct { 62314564Sdim ASN1_OCTET_STRING *encrypted_key; 63284677Sdim ASN1_OCTET_STRING *imit; 64284677Sdim} GOST_KEY_INFO; 65314564Sdim 66341825SdimDECLARE_ASN1_FUNCTIONS(GOST_KEY_INFO) 67284677Sdim 68284677Sdimtypedef struct { 69284677Sdim ASN1_OBJECT *cipher; 70314564Sdim X509_PUBKEY *ephem_key; 71284677Sdim ASN1_OCTET_STRING *eph_iv; 72309124Sdim} GOST_KEY_AGREEMENT_INFO; 73309124Sdim 74314564SdimDECLARE_ASN1_FUNCTIONS(GOST_KEY_AGREEMENT_INFO) 75314564Sdim 76314564Sdimtypedef struct { 77314564Sdim GOST_KEY_INFO *key_info; 78284677Sdim GOST_KEY_AGREEMENT_INFO *key_agreement_info; 79284677Sdim} GOST_KEY_TRANSPORT; 80284677Sdim 81284677SdimDECLARE_ASN1_FUNCTIONS(GOST_KEY_TRANSPORT) 82284677Sdim 83284677Sdimtypedef struct { /* FIXME incomplete */ 84284677Sdim GOST_KEY_TRANSPORT *gkt; 85284677Sdim} GOST_CLIENT_KEY_EXCHANGE_PARAMS; 86284677Sdim 87314564Sdim/* Hacks to shorten symbols to 31 characters or less, or OpenVMS. 88284677Sdim This mimics what's done in symhacks.h, but since this is a very 89284677Sdim local header file, I prefered to put this hack directly here. 90321369Sdim -- Richard Levitte */ 91314564Sdim#ifdef OPENSSL_SYS_VMS 92284677Sdim#undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_it 93327952Sdim#define GOST_CLIENT_KEY_EXCHANGE_PARAMS_it GOST_CLIENT_KEY_EXC_PARAMS_it 94284677Sdim#undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_new 95309124Sdim#define GOST_CLIENT_KEY_EXCHANGE_PARAMS_new GOST_CLIENT_KEY_EXC_PARAMS_new 96314564Sdim#undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_free 97314564Sdim#define GOST_CLIENT_KEY_EXCHANGE_PARAMS_free GOST_CLIENT_KEY_EXC_PARAMS_free 98314564Sdim#undef d2i_GOST_CLIENT_KEY_EXCHANGE_PARAMS 99309124Sdim#define d2i_GOST_CLIENT_KEY_EXCHANGE_PARAMS d2i_GOST_CLIENT_KEY_EXC_PARAMS 100309124Sdim#undef i2d_GOST_CLIENT_KEY_EXCHANGE_PARAMS 101309124Sdim#define i2d_GOST_CLIENT_KEY_EXCHANGE_PARAMS i2d_GOST_CLIENT_KEY_EXC_PARAMS 102309124Sdim#endif /* End of hack */ 103309124SdimDECLARE_ASN1_FUNCTIONS(GOST_CLIENT_KEY_EXCHANGE_PARAMS) 104309124Sdimtypedef struct { 105309124Sdim ASN1_OBJECT *key_params; 106309124Sdim ASN1_OBJECT *hash_params; 107309124Sdim ASN1_OBJECT *cipher_params; 108309124Sdim} GOST_KEY_PARAMS; 109309124Sdim 110309124SdimDECLARE_ASN1_FUNCTIONS(GOST_KEY_PARAMS) 111309124Sdim 112309124Sdimtypedef struct { 113309124Sdim ASN1_OCTET_STRING *iv; 114309124Sdim ASN1_OBJECT *enc_param_set; 115309124Sdim} GOST_CIPHER_PARAMS; 116309124Sdim 117309124SdimDECLARE_ASN1_FUNCTIONS(GOST_CIPHER_PARAMS) 118309124Sdim/*============== Message digest and cipher related structures ==========*/ 119309124Sdim /* Structure used as EVP_MD_CTX-md_data. 120309124Sdim * It allows to avoid storing in the md-data pointers to 121309124Sdim * dynamically allocated memory. 122309124Sdim * 123309124Sdim * I cannot invent better way to avoid memory leaks, because 124309124Sdim * openssl insist on invoking Init on Final-ed digests, and there 125309124Sdim * is no reliable way to find out whether pointer in the passed 126309124Sdim * md_data is valid or not. 127309124Sdim * */ 128309124Sdimstruct ossl_gost_digest_ctx { 129309124Sdim gost_hash_ctx dctx; 130309124Sdim gost_ctx cctx; 131309124Sdim}; 132284677Sdim/* EVP_MD structure for GOST R 34.11 */ 133284677Sdimextern EVP_MD digest_gost; 134284677Sdim/* EVP_MD structure for GOST 28147 in MAC mode */ 135341825Sdimextern EVP_MD imit_gost_cpa; 136309124Sdim/* Cipher context used for EVP_CIPHER operation */ 137309124Sdimstruct ossl_gost_cipher_ctx { 138309124Sdim int paramNID; 139284677Sdim unsigned int count; 140327952Sdim int key_meshing; 141309124Sdim gost_ctx cctx; 142309124Sdim}; 143284677Sdim/* Structure to map parameter NID to S-block */ 144284677Sdimstruct gost_cipher_info { 145284677Sdim int nid; 146341825Sdim gost_subst_block *sblock; 147309124Sdim int key_meshing; 148309124Sdim}; 149309124Sdim/* Context for MAC */ 150309124Sdimstruct ossl_gost_imit_ctx { 151309124Sdim gost_ctx cctx; 152309124Sdim unsigned char buffer[8]; 153309124Sdim unsigned char partial_block[8]; 154309124Sdim unsigned int count; 155309124Sdim int key_meshing; 156309124Sdim int bytes_left; 157309124Sdim int key_set; 158309124Sdim}; 159309124Sdim/* Table which maps parameter NID to S-blocks */ 160344779Sdimextern struct gost_cipher_info gost_cipher_list[]; 161309124Sdim/* Find encryption params from ASN1_OBJECT */ 162314564Sdimconst struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj); 163314564Sdim/* Implementation of GOST 28147-89 cipher in CFB and CNT modes */ 164314564Sdimextern EVP_CIPHER cipher_gost; 165344779Sdimextern EVP_CIPHER cipher_gost_cpacnt; 166309124Sdim#define EVP_MD_CTRL_KEY_LEN (EVP_MD_CTRL_ALG_CTRL+3) 167314564Sdim#define EVP_MD_CTRL_SET_KEY (EVP_MD_CTRL_ALG_CTRL+4) 168309124Sdim/* EVP_PKEY_METHOD key encryption callbacks */ 169314564Sdim/* From gost94_keyx.c */ 170314564Sdimint pkey_GOST94cp_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char* key, size_t key_len ); 171321369Sdim 172321369Sdimint pkey_GOST94cp_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char* in, size_t in_len ); 173321369Sdim/* From gost2001_keyx.c */ 174321369Sdimint pkey_GOST01cp_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char* key, size_t key_len ); 175321369Sdim 176327952Sdimint pkey_GOST01cp_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char* in, size_t in_len ); 177327952Sdim/* derive functions */ 178284677Sdim/* From gost2001_keyx.c */ 179284677Sdimint pkey_gost2001_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen); 180284677Sdim/* From gost94_keyx.c */ 181284677Sdimint pkey_gost94_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen); 182284677Sdim/* Internal functions for signature algorithms */ 183284677Sdimint fill_GOST94_params(DSA *dsa,int nid); 184284677Sdimint fill_GOST2001_params(EC_KEY *eckey, int nid); 185284677Sdimint gost_sign_keygen(DSA *dsa) ; 186314564Sdimint gost2001_keygen(EC_KEY *ec) ; 187314564Sdim 188284677SdimDSA_SIG *gost_do_sign(const unsigned char *dgst,int dlen, DSA *dsa) ; 189309124SdimDSA_SIG *gost2001_do_sign(const unsigned char *dgst,int dlen, EC_KEY *eckey); 190284677Sdim 191284677Sdimint gost_do_verify(const unsigned char *dgst, int dgst_len, 192284677Sdim DSA_SIG *sig, DSA *dsa) ; 193284677Sdimint gost2001_do_verify(const unsigned char *dgst,int dgst_len, 194314564Sdim DSA_SIG *sig, EC_KEY *ec); 195309124Sdimint gost2001_compute_public(EC_KEY *ec) ; 196284677Sdimint gost94_compute_public(DSA *dsa) ; 197284677Sdim/*============== miscellaneous functions============================= */ 198284677Sdim/* from gost_sign.c */ 199284677Sdim/* Convert GOST R 34.11 hash sum to bignum according to standard */ 200284677SdimBIGNUM *hashsum2bn(const unsigned char *dgst) ; 201284677Sdim/* Store bignum in byte array of given length, prepending by zeros 202284677Sdim * if nesseccary */ 203284677Sdimint store_bignum(BIGNUM *bn, unsigned char *buf,int len); 204284677Sdim/* Read bignum, which can have few MSB all-zeros from buffer*/ 205309124SdimBIGNUM *getbnfrombuf(const unsigned char *buf,size_t len); 206309124Sdim/* Pack GOST R 34.10 signature according to CryptoPro rules */ 207309124Sdimint pack_sign_cp(DSA_SIG *s,int order,unsigned char *sig, size_t *siglen); 208284677Sdim/* Unpack GOST R 34.10 signature according to CryptoPro rules */ 209309124SdimDSA_SIG *unpack_cp_signature(const unsigned char *sig,size_t siglen) ; 210309124Sdim/* from ameth.c */ 211284677Sdim/* Get private key as BIGNUM from both R 34.10-94 and R 34.10-2001 keys*/ 212309124Sdim/* Returns pointer into EVP_PKEY structure */ 213309124SdimBIGNUM* gost_get0_priv_key(const EVP_PKEY *pkey) ; 214309124Sdim/* Find NID by GOST 94 parameters */ 215309124Sdimint gost94_nid_by_params(DSA *p) ; 216309124Sdim 217284677Sdim 218284677Sdim#endif 219284677Sdim