📄 stacrypt.c
字号:
/*---------------------------------------------------------------------------+-----*//* | GMD *//* SYSTEM STAPAC - Version 1.0 +-----*//* *//* *//* *//* *//*---------------------------------------------------------------------------------*//* *//* PACKAGE STAMOD-stacrypt VERSION 1.0 *//* DATE Januar 1992 *//* BY Ursula Viebeg *//* Levona Eckstein*//* *//* FILENAME *//* stacrypt.c *//* *//* DESCRIPTION *//* This modul provides all cryptographic functions of the smartcard *//* application interface (SCA-IF). *//* *//* Observe that: *//* 1. the functions sca_verify, sca_sign and sca_hash are compiled and *//* 2. the external functions rsa_get_key and hash_sqmodn are used, *//* only if ASSEMBLER is defined. *//* *//* *//* EXPORT DESCRIPTION *//* sca_gen_user_key() Generate user key (DES or RSA) *//* *//* sca_get_rno() Generate random octetstring *//* *//* sca_del_user_key() Delete user key stored in an SCT *//* *//* sca_sign() Sign octetstring *//* *//* sca_verify() Verify digital signature *//* *//* sca_encrypt() Encrypt octetstring *//* *//* sca_decrypt() Decrypt octetstring *//* *//* sca_hash() Hash octetstring *//* *//* sca_enc_des_key() Encrypt a DES key with the RSA algorithm *//* *//* sca_dec_des_key() Decrypt an rsa-encrypted DES key *//* *//* *//* IMPORT DESCRIPTION *//* - aux_xdmp.c (libcrypt) *//* *//* aux_fxdump() dump buffer in File *//* *//* - aux_util.c (libcrypt) *//* *//* aux_cmp_ObjId() compare two object_ids (part of alg_id) *//* *//* aux_ObjId2ParmType() test the parameter type of the algorithm *//* *//* - aux_free.c (libcrypt) *//* *//* aux_free_OctetString() releases storage of OctetString *//* *//* - rsa.c (libcrypt) *//* *//* rsa_get_key() sets key in an internal function *//* (used for the hash function sqmodn) *//* *//* hash_sqmodn() Hash function square mod n *//* *//* - md2_if.c (libcrypt) *//* *//* md2_hash() Hash function MD2 *//* *//* - md4_if.c (libcrypt) *//* *//* md4_hash() Hash function MD4 *//* *//* - md5_if.c (libcrypt) *//* *//* md5_hash() Hash function MD5 *//* *//* *//* - sta_free.c (libsm) *//* *//* sta_aux_bytestr_free() set the bytes-buffer in Bytestring free *//* *//* *//* - sctint.c (libsm) *//* sct_interface() Send SCT command / receive SCT response *//* *//* sct_errno global error variable set by SCT-interface *//* *//* sct_errmsg global pointer to error message set by *//* SCT-interface *//* *//* *//* - sta_dev.c (libsm) *//* *//* get_sct_keyid() check key_id and get key_id in char *//* representation *//* *//* get_sct_algid() check alg_id and get SCT specific alg_id *//* *//* check_sct_sc() check SCT and SC *//* *//* check_key_attr_list() check key attribute list *//* *//* check_sec_mess() check security mode(s) for command and response*//* *//* set_errmsg() set sca_errmsg *//* *//* err_analyse() error analyse and handling *//* *//* sca_errno global error variable set by STAMOD *//* *//* sca_errmsg global pointer to error message set by STAMOD*//* *//* *//* - stasc.c (libsm) *//* *//* create_trans() send SC command *//* *//* cr_header() create SC-Command header *//* *//* request global variable for create_trans *//* *//* response global variable for create_trans *//* *//* sc_param global variable for create_trans *//* */ /* sc_apdu global variable for create_trans *//* *//* - staprint.c (libsm) for TEST-output *//* *//* print_keyid() *//* print_secmess() */ /* * print_keyattrlist() *//* I NTERNAL *//* compare() compares two strings (independent of '\0') *//* *//* *//*---------------------------------------------------------------------------------*//*-------------------------------------------------------------*//* include-Files *//*-------------------------------------------------------------*/#include "stamod.h"#include "stamsg.h"#include "sctint.h"#include "sccom.h"#include <stdio.h>#include <fcntl.h>#include <string.h>#ifndef MAC#include <sys/types.h>#include <sys/stat.h>#else #include <stdlib.h>#endif /* !MAC *//*-------------------------------------------------------------*//* extern declarations *//*-------------------------------------------------------------*/extern void aux_fxdump();extern int aux_cmp_ObjId();extern ParmType aux_ObjId2ParmType();extern void aux_free_OctetString();extern void sta_aux_bytestr_free();#ifdef TESTextern void print_keyid();extern void print_secmess();extern void print_keyattrlist();#endif#ifdef ASSEMBLERextern int rsa_get_key();extern int hash_sqmodn();#endifextern int md2_hash();extern int md4_hash();extern int md5_hash();extern int sct_interface();extern char get_sct_keyid();extern char get_sct_algid();extern int check_sct_sc();extern int check_key_attr_list();extern int check_sec_mess();extern int set_errmsg();extern void err_analyse();extern int create_trans();extern int cr_header();extern unsigned int sct_errno; /* error number set by SCT-Interface */extern char *sct_errmsg; /* pointer to error msg set by */ /* SCT-Interface */extern unsigned int sca_errno; /* error number set by STAMOD */extern char *sca_errmsg; /* pointer to error msg set by */ /* STAMOD *//* the following variables are declared in the sourec file stasc.c */extern Request request; /* body of the SCT commands */extern Bytestring response; /* body of the response of the SCT */extern int command; /* INS-Code of the SCT command */extern struct s_command sc_param;extern Bytestring sc_apdu;/*-------------------------------------------------------------*//* local function declarations *//*-------------------------------------------------------------*/static int compare();/*-------------------------------------------------------------*//* type definitions *//*-------------------------------------------------------------*/typedef enum { F_null, F_encrypt, F_decrypt, F_hash, F_sign, F_verify} FTYPE;/* definitions for the SC-Interface */#define SCCMD sc_param.sc_header.inscode#define SCHEAD sc_param.sc_header#define SCCRYPT sc_param.sc_uval.sc_crypt/*-------------------------------------------------------------*//* macro definitions *//*-------------------------------------------------------------*/#define ALLOC_CHAR(v,s) {if (0 == (v = malloc(s))) {sca_errno = M_EMEMORY; set_errmsg(); goto errcase;}}#define ALLOC_OCTET(v,t) {if (0 == (v = (t *)malloc(sizeof(t)))) {sca_errno = M_EMEMORY; set_errmsg(); goto errcase;}}/*-------------------------------------------------------------*//* global Variable definitions *//*-------------------------------------------------------------*/static Boolean sc_expect; /* = TRUE indicates: SC expected */ /* = FALSE indicates: SC not needed */static char fermat_f4[3] = {'\001', '\000', '\001'}; /* public exponent */static int fermat_f4_len = 3;static FTYPE act_function = F_null; /* used if encrypt, decrypt, sign */ /* or verify are called with more = */ /* MORE */static int max_length; /* maximal datalength for encrypt/decrypt */static int in_rest_len = 0;static int FIRST_CRYPT_CMD;static char *in_rest = NULL;static char cbc_initvek[8];/*-------------------------------------------------------------*//* | GMD *//* +-----*//* PROC sca_gen_user_key VERSION 1.0 *//* DATE Januar 1992 *//* BY U.Viebeg *//* *//* DESCRIPTION *//* Generate user key (DES or RSA) *//* *//* DES-Key: - if level of key is set to SC_MF, SC_DF, SC_SF*//* => Generate S_GEN_USER_KEY *//* Generate S_INST_USER_KEY *//* - if level of key is set to SCT *//* => Generate S_GEN_USER_KEY *//* RSA-Key: - if level of key is set to SC_MF, SC_DF, SC_SF*//* => Generate S_GEN_USER_KEY *//* Generate S_INST_USER_KEY *//* - if level of key is set to SCT *//* => Return error *//* *//* If key shall be installed on the smartcard, a smartcard *//* must be inserted and parameter key_attr_list must be set. *//* *//* *//* IN DESCRIPTION *//* sct_id SCT identifier *//* *//* key_sel Structure which determines the *//* generated key. *//* *//* key_attr_list Structure which contains *//* additional information for *//* storing the generated key on *//* the SC or the NULL pointer *//* *//* OUT *//* key_sel->key_bits In case of RSA, the public key *//* is returned. Memory is provided *//* by this function and must be *//* released by calling routine. *//* *//* *//* RETURN DESCRIPTION *//* 0 o.k *//* 1 M_KEYREPL *//* -1 error *//* M_ELEVEL *//* EINVALGID *//* M_EPOINTER *//* M_EMEMORY *//* *//* CALLED FUNCTIONS */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -