📄 stacrypt.c
字号:
/* check_sct_sc ERROR-Codes *//* ENOCARD *//* ESIDUNK *//* ENOSHELL *//* EOPERR *//* EEMPTY *//* EMEMAVAIL *//* ECLERR *//* ESIDUNK *//* ERDERR *//* EINVARG *//* ETOOLONG *//* sw1/sw2 from SCT response *//* T1 - ERROR *//* *//* get_sct_algid ERROR-Codes *//* EINVALGID *//* EKEYLENINV *//* *//* get_sct_keyid ERROR-Codes *//* EINVKID *//* *//* check_key_attr_list ERROR-Codes *//* M_EKEYATTR *//* *//* sct_interface ERROR-Codes *//* EINVARG *//* ETOOLONG *//* EMEMAVAIL *//* ESIDUNK *//* EPARMISSED *//* INVPAR *//* EINVINS *//* sw1/sw2 from SCT response *//* T1 - ERROR *//* *//* err_analyse ERROR_Codes *//* ENOSHELL *//* EOPERR *//* EEMPTY *//* ECLERR *//* ESIDUNK *//* ERDERR *//* *//* set_errmsg *//* *//* sta_aux_bytestr_free *//* *//*-------------------------------------------------------------*/intsca_gen_user_key(sct_id, key_sel, key_attr_list) int sct_id; KeySel *key_sel; KeyAttrList *key_attr_list;{ /*----------------------------------------------------------*/ /* Definitions */ /*----------------------------------------------------------*/ int i, algorithm; int rc; char sct_keyid; /* char representation of the key_id */ KeyAlgId sct_algid; /* SCT specific alg_id */ /*----------------------------------------------------------*/ /* Statements */ /*----------------------------------------------------------*/ rc = 0; sca_errno = M_NOERR; sca_errmsg = NULL;#ifdef TEST fprintf(stdout, "\n***** STAMOD-Routine sca_gen_user_key *****\n\n"); fprintf(stdout, "TRACE of the input parameters : \n"); fprintf(stdout, "sct_id : %d\n", sct_id); fprintf(stdout, "key_sel : \n"); if (key_sel != NULL) { fprintf(stdout, " key_algid : %s\n", aux_ObjId2Name(key_sel->key_algid->objid)); print_keyid(&key_sel->key_id); } else fprintf(stdout, "key_sel : NULL\n"); print_keyattrlist(key_attr_list);#endif /*-----------------------------------------------------*/ /* Check input parameters */ /*-----------------------------------------------------*/ /*-----------------------------------------------------*/ /* check algid and get sct specific alg_id */ /*-----------------------------------------------------*/ if ((sct_algid = get_sct_algid(key_sel->key_algid)) == -1) return (-1); /*-----------------------------------------------------*/ /* check key_id and get keyid in char representation */ /*-----------------------------------------------------*/ if ((sct_keyid = get_sct_keyid(&key_sel->key_id)) == -1) return (-1); /*-----------------------------------------------------*/ /* if key shall be installed on the SC, */ /* then - check key attribute list and */ /* - check whether SC is inserted */ /*-----------------------------------------------------*/ if ((key_sel->key_id.key_level == SC_MF) || (key_sel->key_id.key_level == SC_DF) || (key_sel->key_id.key_level == SC_SF)) { if (check_key_attr_list(USER_KEY, key_attr_list) == -1) return (-1); sc_expect = TRUE; } /*-----------------------------------------------------*/ /* if key shall be stored in the SCT and alg_id = RSA, */ /* then - return(error) */ /* An RSA key must be installed on the SC. */ /*-----------------------------------------------------*/ else { if (sct_algid == S_RSA_F4) { sca_errno = M_ELEVEL; set_errmsg(); return (-1); } sc_expect = FALSE; } /*-----------------------------------------------------*/ /* call check_sct_sc */ /*-----------------------------------------------------*/ if (check_sct_sc(sct_id, sc_expect) == -1) return (-1); /*-----------------------------------------------------*/ /* if sct_algid = S_RSA_F4, */ /* then key_sel->key_bits must be valid for */ /* the returned public key. */ /*-----------------------------------------------------*/ if ((sct_algid == S_RSA_F4) && (key_sel->key_bits == NULL)) { sca_errno = M_EPOINTER; set_errmsg(); return (-1); }/************** input parameter check done *********************************/ /*-----------------------------------------------------*/ /* Generate key (S_GEN_USER_KEY) */ /*-----------------------------------------------------*/ /*-----------------------------------------------------*/ /* Prepare parameters for the SCT Interface */ /* */ /* At the SCA-IF the keysize is given in bits, */ /* at the SCT-IF the keysize is delivered in bytes, */ /* therefor the keysize in bits is divided by 8. */ /*-----------------------------------------------------*/ command = S_GEN_USER_KEY; request.rq_p1.kid = sct_keyid; request.rq_p2.algid = sct_algid; if (sct_algid == S_RSA_F4) request.rq_datafield.keylen = RSA_PARM(key_sel->key_algid->parm) / 8; else request.rq_datafield.keylen = 0;#ifdef TEST if (sct_algid == S_RSA_F4) fprintf(stdout, "keysize of RSA key: %d\n", RSA_PARM(key_sel->key_algid->parm));#endif /*-----------------------------------------------------*/ /* Call SCT Interface */ /*-----------------------------------------------------*/ rc = sct_interface(sct_id, command, &request, &response); if (rc < 0) { sca_errno = sct_errno; sca_errmsg = sct_errmsg; err_analyse(sct_id); return (-1); } /*-----------------------------------------------------*/ /* If an existing key in the SCT has been overwritten */ /* then return (warning) */ /*-----------------------------------------------------*/ if (key_sel->key_id.key_level == SCT) if (rc == S_KEYREPL) sca_errno = M_KEYREPL; /*-----------------------------------------------------*/ /* If sct_algid = S_RSA_F4 */ /* then get modulus from SCT response and */ /* construct public key (modulus, Fermat-F4) */ /* and return pk in key_sel->key_bits */ /*-----------------------------------------------------*/ if (sct_algid == S_RSA_F4) { key_sel->key_bits->part1.noctets = response.nbytes; if ((key_sel->key_bits->part1.octets = (char *) malloc(response.nbytes)) == NULL) { sca_errno = M_EMEMORY; set_errmsg(); sta_aux_bytestr_free(&response); return (-1); } for (i = 0; i < response.nbytes; i++) key_sel->key_bits->part1.octets[i] = response.bytes[i]; /* get fermat-f4 as public exponent */ key_sel->key_bits->part2.noctets = 3; if ((key_sel->key_bits->part2.octets = (char *) malloc(3)) == NULL) { sca_errno = M_EMEMORY; set_errmsg(); sta_aux_bytestr_free(&response); return (-1); } key_sel->key_bits->part3.noctets =0; key_sel->key_bits->part4.noctets =0; memcpy(key_sel->key_bits->part2.octets, fermat_f4, 3);#ifdef TEST fprintf(stdout, "modulus of public key:\n"); aux_fxdump(stdout, key_sel->key_bits->part1.octets, key_sel->key_bits->part1.noctets, 0); fprintf(stdout, "\n"); fprintf(stdout, "public exponent of public key:\n"); aux_fxdump(stdout, key_sel->key_bits->part2.octets, key_sel->key_bits->part2.noctets, 0); fprintf(stdout, "\n");#endif } /*-----------------------------------------------------*/ /* (Release storage) */ /*-----------------------------------------------------*/ sta_aux_bytestr_free(&response);/************** key is now generated *********************************/ /*-----------------------------------------------------*/ /* if key shall not be installed on SC, */ /* then work is done */ /*-----------------------------------------------------*/ if (key_sel->key_id.key_level == SCT) return (sca_errno); /*-----------------------------------------------------*/ /* otherwise (if key shall be installed on SC), */ /* then install key on SC (S_INST_USER_KEY) */ /* and delete key in SCT (S_DEL_USER_KEY) */ /*-----------------------------------------------------*/ /*-----------------------------------------------------*/ /* Prepare parameters for the SCT Interface */ /*-----------------------------------------------------*/ command = S_INST_USER_KEY; request.rq_p1.kid = sct_keyid; request.rq_datafield.keyattrlist = key_attr_list; /*-----------------------------------------------------*/ /* Call SCT Interface */ /*-----------------------------------------------------*/ rc = sct_interface(sct_id, command, &request, &response); if (rc < 0) { sca_errno = sct_errno; sca_errmsg = sct_errmsg; err_analyse(sct_id); return (-1); } /*-----------------------------------------------------*/ /* Normal End (Release storage) */ /*-----------------------------------------------------*/ sta_aux_bytestr_free(&response); /*-----------------------------------------------------*/ /* Prepare parameters for the SCT Interface */ /*-----------------------------------------------------*/ command = S_DEL_USER_KEY; request.rq_p1.kid = sct_keyid; /*-----------------------------------------------------*/ /* Call SCT Interface */ /*-----------------------------------------------------*/ rc = sct_interface(sct_id, command, &request, &response); if (rc < 0) { sca_errno = sct_errno; sca_errmsg = sct_errmsg; err_analyse(sct_id); return (-1); } /*-----------------------------------------------------*/ /* Normal End (Release storage) */ /*-----------------------------------------------------*/ sta_aux_bytestr_free(&response);#ifdef TEST fprintf(stdout, "\n***** Normal end of sca_gen_user_key *********************************************\n\n");#endif return (sca_errno);} /* end sca_gen_user_key *//*-------------------------------------------------------------*//* E N D O F P R O C E D U R E sca_gen_user_key *//*-------------------------------------------------------------*//*-------------------------------------------------------------*//* | GMD *//* +-----*//* PROC sca_get_rno VERSION 1.0 *//* DATE Juli 1992 *//* BY L. Eckstein *//* *//* DESCRIPTION *//* Get random number from SCT *//* A smartcard is not expected. *//* *//* *//* IN DESCRIPTION *//* sct_id SCT identifier *//* *//* rnd_len required length of the random *//* number *//* *//* OUT *//* *//* */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -