📄 objcnt.c
字号:
/* ============================================================================ Project Name : jayaCard Module Name : proto/bios/odata/objcnt.c Version : $Id: objcnt.c,v 1.5 2004/01/11 09:56:31 dgil Exp $ Description: BIOS HELPER FUNCTIONS for COUNTERS The Original Code is jayaCard code. The Initial Developer of the Original Code is Gilles Dumortier. Portions created by the Initial Developer are Copyright (C) 2002-2004 the Initial Developer. All Rights Reserved. Contributor(s): This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see http://www.gnu.org/licenses/gpl.html History Rev Description 112203 dgil wrote it from scratch ============================================================================*/#include "precomp.h"/* ============================================================================ __bios_inc_counter() increment counter n in the current EF secure: use global semaphore ========================================================================= */jdword __bios_inc_counter(jbyte n,jdword v){ LOCAL(jbyte,res); LOG3("COUNTERS","__bios_inc_counter(): fid=%.4X counter=%d add=%d",current_EF.fid,n,v); /* check the key(s) file : binary content + internal file */ if ((current_EF.fdesc&(FDESC_TYPE_MASK|FDESC_INTERNAL_EF))!=(FDESC_TYPE_BINARY|FDESC_INTERNAL_EF)) { BIOS_SETERR(ERR_INVALID_FILE_TYPE); return 0; } #ifdef JAYACFG_FILESYSTEM_INVALIDATE if ( (current_EF.fdesc&FDESC_INVALIDATED) == FDESC_INVALIDATED ) { LOG1("FS","__bios_inc_counter() file invalidated fid=%.4X !",current_EF.fid); BIOS_SETERR(ERR_FILE_INVALIDATED); return 0; } #endif /* check this is a counter file ! */ if ((current_EF.sfi&FDESC2_MASK)!=FDESC2_COUNTERS) { BIOS_SETERR(ERR_INVALID_FILE_TYPE); return 0; } /* check the access conditions of the file */ gGlobalSem = JSEC_FAIL; res = FS_CHECK_AC(ACC_CHECK_EF|ACC_INCREASE); gGlobalSem++; if (res != JSEC_OK) { BIOS_SETERR(ERR_ACCESS_DENIED); return 0; } if (gGlobalSem!=JSEC_SEM) { LOG("ATTACK","__bios_inc_counter() #1 - check security failure !"); BIOS_SETERR(ERR_ACCESS_DENIED); HAL_HALT(); return 0; } /* init a stream for the counter */ BIOS_INIT_STREAM_FROM_CURRENT_EF(&streamSrc); if (lasterr != SUCCESS) { LOG2("COUNTERS","Error %d accessing counter %d !",lasterr,n); return 0; } streamSrc.m_pos += n*sizeof(jdword); /* check counter exist ! */ gBiosSem = BIOS_TEST_STREAM(&streamSrc,sizeof(jdword)); if ((gBiosSem==JSEC_FAIL) || (lasterr!=SUCCESS)) { LOG1("COUNTERS","Out of range counter %d !",n); return 0; } /* read the current value */ BIOS_READ_STREAM(&u.bBlock[JAYA_BCRYPTO_COUNTER],&streamSrc,sizeof(jdword)); gBiosSem++; if (lasterr!=SUCCESS) { LOG1("COUNTERS","Can't read the counter %d !",n); return 0; } LOG2("COUNTERS","counter %d value = %ld",n,u.dwBlock[JAYA_DWCRYPTO_COUNTER]); /* check the counter before proceeding */ if ((u.dwBlock[JAYA_DWCRYPTO_COUNTER]+v)<u.dwBlock[JAYA_DWCRYPTO_COUNTER]) { BIOS_SETERR(ERR_COUNTER_OVERFLOW); return 0; } gBiosSem++; /* calculation */ u.dwBlock[JAYA_DWCRYPTO_COUNTER] = u.dwBlock[JAYA_DWCRYPTO_COUNTER] + v; gBiosSem++; LOG2("COUNTERS","counter %d new value = %ld",n,u.dwBlock[JAYA_DWCRYPTO_COUNTER]); /* store the new count */ BIOS_TRANS_ADD(&u.bBlock[JAYA_BCRYPTO_COUNTER],streamSrc.m_pos-sizeof(jdword),sizeof(jdword)); if (lasterr!=SUCCESS) { LOG1("COUNTERS","Can't write the counter %d !",n); return 0; } /* check sequence */ if (gBiosSem!=JSEC_SEM) { LOG("ATTACK","__bios_inc_counter() #2 - check security failure !"); BIOS_SETERR(ERR_ACCESS_DENIED); HAL_HALT(); return 0; } return u.dwBlock[JAYA_DWCRYPTO_COUNTER];}/* ============================================================================ __bios_dec_counter() decrement counter n in the current EF secure: use global semaphore ========================================================================= */jdword __bios_dec_counter(jbyte n,jdword v){ LOCAL(jbyte,res); LOG3("COUNTERS","__bios_dec_counter(): fid=%.4X counter=%d dec=%d",current_EF.fid,n,v); /* check the key(s) file : binary content + internal file */ if ((current_EF.fdesc&(FDESC_TYPE_MASK|FDESC_INTERNAL_EF))!=(FDESC_TYPE_BINARY|FDESC_INTERNAL_EF)) { BIOS_SETERR(ERR_INVALID_FILE_TYPE); return 0; } /* check this is a counter file ! */ if ((current_EF.sfi&FDESC2_MASK)!=FDESC2_COUNTERS) { BIOS_SETERR(ERR_INVALID_FILE_TYPE); return 0; } #ifdef JAYACFG_FILESYSTEM_INVALIDATE if ( (current_EF.fdesc&FDESC_INVALIDATED) == FDESC_INVALIDATED ) { LOG1("FS","__bios_dec_counter() file invalidated fid=%.4X !",current_EF.fid); BIOS_SETERR(ERR_FILE_INVALIDATED); return 0; } #endif /* check the access conditions of the file */ gGlobalSem = JSEC_FAIL; res = FS_CHECK_AC(ACC_CHECK_EF|ACC_DECREASE); gGlobalSem++; if (res != JSEC_OK) { BIOS_SETERR(ERR_ACCESS_DENIED); return 0; } if (gGlobalSem!=JSEC_SEM) { LOG("ATTACK","__bios_dec_counter() #1 - check security failure !"); BIOS_SETERR(ERR_ACCESS_DENIED); HAL_HALT(); return 0; } /* init a stream for the counter */ BIOS_INIT_STREAM_FROM_CURRENT_EF(&streamSrc); if (lasterr != SUCCESS) { LOG2("COUNTERS","Error %d accessing counter %d !",lasterr,n); return 0; } streamSrc.m_pos += n*sizeof(jdword); /* check counter exist ! */ gBiosSem = BIOS_TEST_STREAM(&streamSrc,sizeof(jdword)); if ((gBiosSem==JSEC_FAIL) || (lasterr!=SUCCESS)) { LOG1("COUNTERS","Out of range counter %d !",n); return 0; } /* read the current value */ BIOS_READ_STREAM(&u.bBlock[JAYA_BCRYPTO_COUNTER],&streamSrc,sizeof(jdword)); gBiosSem++; if (lasterr!=SUCCESS) { LOG1("COUNTERS","Can't read the counter %d !",n); return 0; } LOG2("COUNTERS","counter %d value = %ld",n,u.dwBlock[JAYA_DWCRYPTO_COUNTER]); /* check the counter before proceeding */ if (u.dwBlock[JAYA_DWCRYPTO_COUNTER]<v) { BIOS_SETERR(ERR_COUNTER_UNDERFLOW); return 0; } gBiosSem++; /* calculation */ u.dwBlock[JAYA_DWCRYPTO_COUNTER] = u.dwBlock[JAYA_DWCRYPTO_COUNTER] - v; gBiosSem++; LOG2("COUNTERS","counter %d new value = %ld",n,u.dwBlock[JAYA_DWCRYPTO_COUNTER]); /* store the new count */ BIOS_TRANS_ADD(&u.bBlock[JAYA_BCRYPTO_COUNTER],streamSrc.m_pos-sizeof(jdword),sizeof(jdword)); if (lasterr!=SUCCESS) { LOG1("COUNTERS","Can't write the counter %d !",n); return 0; } /* check sequence */ if (gBiosSem!=JSEC_SEM) { LOG("ATTACK","__bios_dec_counter() #2 - check security failure !"); BIOS_SETERR(ERR_ACCESS_DENIED); HAL_HALT(); return 0; } return u.dwBlock[JAYA_DWCRYPTO_COUNTER];}/* ========================================================================= That's all folks ! ========================================================================= */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -