📄 sccom.c
字号:
key_info.subjectkey.nbits = sec_key->nbits; key_info.subjectkey.bits = sec_key->bits; switch (algenc) { case DES: key_info.subjectAI = desCBC; break; default: sc_errno = EALGO; sc_errmsg = sct_error[sc_errno].msg; return (-1); break; } more = END; /* allocate memory for out_bits */ /* the memory must be a multiple of 8 Bytes */ if ((in_octets.noctets % 8) != 0) memolen = (in_octets.noctets - (in_octets.noctets % 8)) + 8; else memolen = in_octets.noctets; out_bits.nbits = 0;#ifdef STREAM fprintf(sc_trfp, " allocate out_bits = %d\n", memolen);#endif#ifdef MALLOC out_bits.bits = malloc(memolen); /* will be set free in this * proc. */ if (out_bits.bits == NULL) { sc_errno = EMEMAVAIL; sc_errmsg = sct_error[sc_errno].msg; return (-1); }#endif memolen = des_encrypt(&in_octets, &out_bits, more, &key_info); if (memolen == -1) { sc_errno = EDESENC; sc_errmsg = sct_error[sc_errno].msg; aux_free2_BitString(&out_bits); return (-1); }#ifdef STREAM fprintf(sc_trfp, " out_bits.nbits = %d\n", out_bits.nbits); fprintf(sc_trfp, " out_bits.bits = \n"); aux_fxdump(sc_trfp, out_bits.bits, out_bits.nbits / 8, 0);#endif memolen = in_octets.noctets + maclen;#ifdef MALLOC out_apdu->bytes = malloc(memolen); /* if no error => return; */ /* else will be set free in this proc. */ if (out_apdu->bytes == NULL) { sc_errno = EMEMAVAIL; sc_errmsg = sct_error[sc_errno].msg; aux_free2_BitString(&out_bits); return (-1); }#endif out_apdu->nbytes = memolen; ptr = out_apdu->bytes; for (i = 0; i < in_octets.noctets; i++) { *ptr = *(in_octets.octets + i); ptr++; } /* if only 1 block encrypted => take the first 4 Bytes for MAC */ /* else take the last 4 bytes of the last block */ if ((out_bits.nbits / 8) > 8) mac_ptr = out_bits.bits + ((out_bits.nbits / 8) - 8); else mac_ptr = out_bits.bits; for (i = 0; i < maclen; i++) { *ptr = *(mac_ptr + i); ptr++; }; aux_free2_BitString(&out_bits);#ifdef STREAM fprintf(sc_trfp, " out_apdu->nbytes = %d\n", out_apdu->nbytes); fprintf(sc_trfp, " out_apdu->bytes = \n"); aux_fxdump(sc_trfp, out_apdu->bytes, out_apdu->nbytes, 0); fprintf(sc_trfp, "TRACE-END in sc_crmac\n");#endif return (0);}/*-------------------------------------------------------------*//* E N D O F P R O C E D U R E sc_crmac *//*-------------------------------------------------------------*//*--------------------------------------------------------*//* | GMD *//* +-----*//* PROC sc_enc VERSION 2.0 *//* DATE November 1991 *//* BY L.Eckstein,GMD *//* *//* DESCRIPTION *//* Encrypt SC-COMMAND-APDU (without CLA-Byte) *//* This Procedure can be called in case of *//* secure messaging = CONCEALED and in case of *//* secure messaging = COMBINED after calling the *//* procedure sc_crmac./**//* *//* *//* IN DESCRIPTION *//* sec_key Secure Messaging key *//* *//* in_apdu Pointer of SC-APDU *//* The SC-APDU must have the *//* structur: *//* __________________________ *//* | CLA,INS,P1,P2,L,SSC,DATA |*//* __________________________ *//* (= output of the procedure *//* sc_create) *//* or *//* ______________________________ *//* | CLA,INS,P1,P2,L,SSC,DATA,MAC |*//* ______________________________ *//* (= output of the procedure *//* sc_crmac) *//* *//* algenc Encryption method *//* *//* OUT *//* out_apdu Pointer of SEC-APDU *//* out_apdu->bytes will be *//* allocated by the called *//* program *//* and must be set free by the*//* calling program *//* The SEC-APDU has the *//* structure: *//* _____________________ *//* | CLA,ENCRYPTED DATA | *//* _____________________ *//* *//*/**//* RETURN DESCRIPTION *//* 0 o.k *//* -1 Error *//* EMEMAVAIL *//* EDESENC *//* EALGO *//* *//* CALLED FUNCTIONS *//* des_encrypt *//* aux_fxdump *//* aux_free2_BitString *//* *//* Bemerkung: *//* Derzeit wird nur der DES-CBC-Mode unterstuetzt. *//* Der DES-3-CBC-Mode noch nicht. *//*--------------------------------------------------------*/intsc_enc(sec_key, in_apdu, out_apdu, algenc) BitString *sec_key;/* secure messaging key */ Bytestring *in_apdu;/* SC-APDU */ Bytestring *out_apdu; /* SC-SEC-APDU */ AlgEnc algenc; /* encryption method */{ /*----------------------------------------------------------*/ /* Definitions */ /*----------------------------------------------------------*/ OctetString in_octets; char *ptr; int i; int memolen; BitString out_bits; KeyInfo key_info; More more; /*----------------------------------------------------------*/ /* Statements */ /*----------------------------------------------------------*/ out_apdu->nbytes = 0; out_apdu->bytes = NULL; in_octets.noctets = in_apdu->nbytes; in_octets.octets = in_apdu->bytes; /*---------------------------------------------------------*/ /* encrypt data (INS,P1,P2,L,SSC,DATA) */ /* with Secure Messaging Key */ /*---------------------------------------------------------*/ in_octets.noctets -= 1; in_octets.octets++;#ifdef STREAM fprintf(sc_trfp, "TRACE in sc_enc\n"); fprintf(sc_trfp, " in_octets.noctets = %d\n", in_octets.noctets); fprintf(sc_trfp, " in_octets.octets = \n"); aux_fxdump(sc_trfp, in_octets.octets, in_octets.noctets, 0);#endif key_info.subjectkey.nbits = sec_key->nbits; key_info.subjectkey.bits = sec_key->bits; switch (algenc) { case DES: key_info.subjectAI = desCBC; break; default: sc_errno = EALGO; sc_errmsg = sct_error[sc_errno].msg; return (-1); break; } more = END; /* allocate memory for out_bits */ /* the memory must be a multiple of 8 Bytes */ if ((in_octets.noctets % 8) != 0) memolen = (in_octets.noctets - (in_octets.noctets % 8)) + 8; else memolen = in_octets.noctets; out_bits.nbits = 0;#ifdef STREAM fprintf(sc_trfp, " allocate out_bits = %d\n", memolen);#endif#ifdef MALLOC out_bits.bits = malloc(memolen); /* will be set free in this * proc. */ if (out_bits.bits == NULL) { sc_errno = EMEMAVAIL; sc_errmsg = sct_error[sc_errno].msg; return (-1); }#endif memolen = des_encrypt(&in_octets, &out_bits, more, &key_info); if (memolen == -1) { sc_errno = EDESENC; sc_errmsg = sct_error[sc_errno].msg; aux_free2_BitString(&out_bits); return (-1); }#ifdef STREAM fprintf(sc_trfp, " out_bits.nbits = %d\n", out_bits.nbits); fprintf(sc_trfp, " out_bits.bits = \n"); aux_fxdump(sc_trfp, out_bits.bits, out_bits.nbits / 8, 0);#endif memolen = (out_bits.nbits / 8) + 1;#ifdef MALLOC out_apdu->bytes = malloc(memolen); /* if no error => return */ /* else will gbe set free in this proc. */ if (out_apdu->bytes == NULL) { sc_errno = EMEMAVAIL; sc_errmsg = sct_error[sc_errno].msg; aux_free2_BitString(&out_bits); return (-1); }#endif out_apdu->nbytes = memolen; ptr = out_apdu->bytes; *ptr = *in_apdu->bytes; /* transfer CLA-Byte */ ptr++; for (i = 0; i < (out_bits.nbits / 8); i++) { *ptr = *(out_bits.bits + i); ptr++; }; aux_free2_BitString(&out_bits);#ifdef STREAM fprintf(sc_trfp, " out_apdu->nbytes = %d\n", out_apdu->nbytes); fprintf(sc_trfp, " out_apdu->bytes = \n"); aux_fxdump(sc_trfp, out_apdu->bytes, out_apdu->nbytes, 0); fprintf(sc_trfp, "TRACE-END in sc_enc\n");#endif return (0);}/*-------------------------------------------------------------*//* E N D O F P R O C E D U R E sc_enc *//*-------------------------------------------------------------*//*--------------------------------------------------------*//* | GMD *//* +-----*//* PROC sc_checkmac VERSION 2.0 *//* DATE November 1991 *//* BY L.Eckstein,GMD *//* *//* DESCRIPTION *//* Check MAC and SSC of a received SC-RESPONSE-APDU *//* This procedure can be called in case of *//* secure messaging = AUTHENTIC or in case of *//* secure messaging = COMBINED after calling the *//* procedure sc_dec. *//* *//* *//* *//* IN DESCRIPTION *//* sec_key Secure Messaging key *//* *//* ssc Send Sequence Counter *//* *//* in_apdu Pointer of SEC-APDU *//* The SC-APDU must have the *//* structur: *//* ________________________ *//* | L,SSC,DATA,MAC,SW1,SW2 | *//* ________________________ *//* *//* algenc Encryption method *//* *//* maclen Length of MAC (0 - 8) *//* In the current Version *//* only 4 is allowed */ /* *//* OUT *//* out_apdu Pointer of SC-APDU *//* (without SSC and MAC) *//* L,DATA,SW1,SW2 will be *//* returned *//* out_apdu->bytes will be *//* allocated by the called *//* program *//* and must be set free by the*//* calling program *//* The APDU has the structure:*//* _________________ *//* | L,DATA,SW1,SW2 | *//* _________________ *//* *//* *//* RETURN DESCRIPTION *//* 0 o.k *//* -1 Error *//* EMEMAVAIL *//* EDESENC *//* ESSC *//* EMAC *//* EALGO *//* *//* CALLED FUNCTIONS *//* des_encrypt *//* aux_fxdump *//* aux_free2_OctetString *//* aux_free2_BitString *//* *//* Bemerkung: *//* Derzeit wird nur der DES-CBC-Mode unterstuetzt. *//* Der DES-3-CBC-Mode noch nicht. *//*--------------------------------------------------------*/intsc_checkmac(sec_key, ssc, in_apdu, out_apdu, algenc, maclen) BitString *sec_key;/* secure messaging key */ int ssc; /* Send sequence Counter */ Bytestring *in_apdu;/* SEC-APDU */ Bytestring *out_apdu; /* SC-APDU */ AlgEnc algenc; /* encryption method */ int maclen; /* Length of MAC */{ /*----------------------------------------------------------*/ /* Definitions */ /*----------------------------------------------------------*/ OctetString in_octets; char *ptr, *apdu_ptr, *mac_ptr; int i; int memolen; BitString out_bits; KeyInfo key_info; More more; int rec_ssc, data_len, mac_len; char *mac_field;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -