📄 hi_aes_api.c
字号:
control_word &= 0xfe; reverse_array(aes_iv); reverse_array(pcipher); if(aes_alg_operate(src,aest,byte_length,pcipher) != 0) { printf("aes crypt failture.\n"); return -1; } reverse_array(pcipher); reverse_array(aes_iv); return 0;}int hi_aes_decrypt(unsigned char * src,unsigned char * aest,unsigned int byte_length,unsigned char *pcipher){ if(NULL==src) { printf("the pointer decrypt is null!"); return -1; } if(NULL==aest) { printf("the pointer decrypt is null!"); return -1; } if(NULL==pcipher) { printf("the pointer decrypt is null!"); return -1; } //plaintext = (unsigned int *)src; //outtext = (unsigned int *)aest; if((byte_length % 16) != 0) { printf("aes decrypt data length is error.\n"); return -1; } control_word |= 0x01; reverse_array(aes_iv); reverse_array(pcipher); if(aes_alg_operate(src,aest,byte_length,pcipher) != 0) { printf("aes decrypt failture.\n"); return -1; } reverse_array(pcipher); reverse_array(aes_iv); return 0;}static int aes_alg_operate(unsigned char * pplaintext,unsigned char * pouttext,unsigned int plaintext_length,unsigned char *pcipher){ int cltword,mode; unsigned char temp_plain_8[16]; unsigned char temp_plain_1[16]; unsigned char temp_iv[16]; static struct trans temp_out; unsigned char out[16],rout[16]; int i,j,k,loop; //unsigned long long cfb_temp_iv[2]; if(NULL==pplaintext) { printf("the pointer operate is null!"); return -1; } if(NULL==pouttext) { printf("the pointer operate is null!"); return -1; } if(NULL==pcipher) { printf("the pointer operate is null!"); return -1; } cltword = control_word; mode = cltword & 0x0e; switch(mode) { case AES_MODE_ECB: { if(aes_ctrl((AES_SET_KEY|cltword), pcipher) != 0) { return -1; } for(i=0;i<plaintext_length;i+=16) { reverse_array(&pplaintext[i]); if(aes_ctrl((AES_SET_M|cltword),&pplaintext[i]) != 0) { return -1; } if(aes_ctrl((AES_SET_START|cltword),0) != 0) { return -1; } if(aes_ctrl((AES_GET_DATA|cltword),(unsigned char*)&temp_out) != 0) { return -1; } if(pouttext == NULL) { for(j=0;j<16;j++) { pplaintext[j + i] = temp_out.outtrans[15-j]; } } else { for(j=0;j<16;j++) { pouttext[j + i] = temp_out.outtrans[15-j]; } } reverse_array(&pplaintext[i]); //reverse_array((unsigned char*)&pouttext[cnt]); } return 0; } case AES_MODE_CBC: { for(j=0;j<16;j++) { temp_iv[j] = aes_iv[j]; } if(aes_ctrl((AES_SET_KEY|cltword),pcipher) != 0) { return -1; } for(i=0;i<plaintext_length;i+=16) { if(i != 0) { for(j=0;j<16;j++) { temp_iv[j] = temp_out.ivouttrans[j]; } } reverse_array(&pplaintext[i]); if(aes_ctrl((AES_SET_IV|cltword),temp_iv) != 0) { return -1; } if(aes_ctrl((AES_SET_M|cltword),&pplaintext[i]) != 0) { return -1; } if(aes_ctrl((AES_SET_START|cltword),0) !=0) { return -1; } if(aes_ctrl((AES_GET_DATA|cltword),(unsigned char*)&temp_out) != 0) { return -1; } if(pouttext == NULL) { for(j=0;j<16;j++) { pplaintext[j + i] = temp_out.outtrans[15-j]; } } else { for(j=0;j<16;j++) { pouttext[j + i] = temp_out.outtrans[15-j]; //printf("the pouttext is:%x \n",pouttext[j + i]); } } reverse_array((unsigned char *)&pplaintext[i]); //reverse_array((unsigned char*)&pouttext[cnt]); } return 0; } case AES_MODE_CFB1: { for(j=0;j<16;j++) { temp_iv[j] = aes_iv[j]; } if(aes_ctrl((AES_SET_KEY|cltword),pcipher) != 0) return -1; for(i=0;i<plaintext_length;i+=16) { // reverse_1_array(&pplaintext[i]); for(k=0;k<16;k++) { out[k] = 0; for(j=0;j<8;j++) { if(!((i == 0) && (j == 0) && (k == 0))) { for(loop=0;loop<16;loop++) { temp_iv[loop] = temp_out.ivouttrans[loop]; } } if(aes_ctrl((AES_SET_IV|cltword),temp_iv) != 0) return -1; temp_plain_1[15] = pplaintext[i+k] << j; #ifdef DEBUG printf("pplaintext[3-k+cnt]:%x\n",pplaintext[i+k]); #endif if(aes_ctrl((AES_SET_M|cltword),temp_plain_1) != 0) return -1; if(aes_ctrl((AES_SET_START|cltword),0) != 0) return -1; if(aes_ctrl((AES_GET_DATA|cltword),(unsigned char*)&temp_out) != 0) return -1; rout[k] = (temp_out.outtrans[15] & 0x80) >> j; out[k] |= rout[k]; } if(pouttext == NULL) { pplaintext[i+k] = out[k]; } else { pouttext[i+k] = out[k]; } } //reverse_1_array(&pplaintext[cnt]); //reverse_1_array(&pouttext[cnt]); //cnt += 4; } return 0; } case AES_MODE_CFB8: { for(j=0;j<16;j++) { temp_iv[j] = aes_iv[j]; } if(aes_ctrl((AES_SET_KEY|cltword),pcipher) != 0) return -1; for(i=0;i<plaintext_length;i+=16) { //reverse_array((unsigned char *)(&pplaintext[cnt])); for(j=0;j<16;j++) { if(!((i == 0) && (j == 0) )) { for(loop=0;loop<16;loop++) { temp_iv[loop] = temp_out.ivouttrans[loop]; } } if(aes_ctrl((AES_SET_IV|cltword),temp_iv) != 0) return -1; temp_plain_8[15] = pplaintext[i+j] ; if(aes_ctrl((AES_SET_M|cltword),temp_plain_8) != 0) return -1; if(aes_ctrl((AES_SET_START|cltword),0) != 0) return -1; if(aes_ctrl((AES_GET_DATA|cltword),(unsigned char*)&temp_out) != 0) return -1; rout[j] = temp_out.outtrans[15] ; out[j] = rout[j]; if(pouttext == NULL) pplaintext[i+j] = out[j]; else pouttext[i+j] = out[j]; } //reverse_array((unsigned char *)(&pplaintext[cnt])); //reverse_array((unsigned char *)(&pouttext[cnt])); } return 0; } case AES_MODE_CFB128: { for(j=0;j<16;j++) { temp_iv[j] = aes_iv[j]; } if(aes_ctrl((AES_SET_KEY|cltword),pcipher) != 0) return -1; for(i=0;i<plaintext_length;i+=16) { if(i!=0) { for(j=0;j<16;j++) { temp_iv[j] = temp_out.ivouttrans[j]; } } reverse_array(&pplaintext[i]); if(aes_ctrl((AES_SET_IV|cltword),temp_iv) != 0) return -1; if(aes_ctrl((AES_SET_M|cltword),&pplaintext[i]) != 0) return -1; if(aes_ctrl((AES_SET_START|cltword),0) != 0) return -1; if(aes_ctrl((AES_GET_DATA|cltword),(unsigned char*)&temp_out) != 0) return -1; if(pouttext == NULL) { for(j=0;j<16;j++) { pplaintext[i+j] = temp_out.outtrans[15-j]; } } else { for(j=0;j<16;j++) { pouttext[i+j] = temp_out.outtrans[15-j]; } } reverse_array((unsigned char *)&pplaintext[i]); //reverse_array((unsigned char*)&pouttext[cnt]); } return 0; } case AES_MODE_OFB: { for(j=0;j<16;j++) { temp_iv[j] = aes_iv[j]; } if(aes_ctrl((AES_SET_KEY|cltword),pcipher) != 0) return -1; for(i=0;i<plaintext_length;i+=16) { if(i!=0) { for(j=0;j<16;j++) { temp_iv[j] = temp_out.ivouttrans[j]; } } reverse_array(&pplaintext[i]); if(aes_ctrl((AES_SET_IV|cltword),temp_iv) != 0) return -1; if(aes_ctrl((AES_SET_M|cltword),&pplaintext[i]) != 0) return -1; if(aes_ctrl((AES_SET_START|cltword),0) != 0) return -1; if(aes_ctrl((AES_GET_DATA|cltword),(unsigned char*)&temp_out) != 0) return -1; if(pouttext == NULL) { for(j=0;j<16;j++) { pplaintext[j + i] = temp_out.outtrans[15-j]; } } else { for(j=0;j<16;j++) { pouttext[j + i] = temp_out.outtrans[15-j]; } } reverse_array((unsigned char *)&pplaintext[i]); //reverse_array((unsigned char*)&pouttext[i]); } return 0; } default: { return -EINVAL; } } }int aes_ctr_crypt(unsigned char * src,unsigned char * aest,unsigned int byte_length,unsigned char *pcipher,unsigned char * ctr_iv){ if(NULL==src) { printf("the pointer ctr is null!"); return -1; } if(NULL==aest) { printf("the pointer ctr is null!"); return -1; } if(NULL==pcipher) { printf("the pointer ctr is null!"); return -1; } if(NULL==ctr_iv) { printf("the pointer ctr is null!"); return -1; } if((byte_length % 16) != 0) { printf("aes ctr mode crypt data length is error.\n"); return -1; } control_word &= 0xfe; reverse_array(pcipher); if(aes_ctr_operate(src,aest,byte_length,pcipher,ctr_iv) != 0) { printf("aes ctr mode crypt failture.\n"); return -1; } reverse_array(pcipher); return 0;}int aes_ctr_decrypt(unsigned char * src,unsigned char * aest,unsigned int byte_length,unsigned char *pcipher,unsigned char * ctr_iv){ if(NULL==src) { printf("the pointer ctr_de is null!"); return -1; } if(NULL==aest) { printf("the pointer ctr_de is null!"); return -1; } if(NULL==pcipher) { printf("the pointer ctr_de is null!"); return -1; } if(NULL==ctr_iv) { printf("the pointer ctr_de is null!"); return -1; } if((byte_length % 16) != 0) { printf("aes ctr mode decrypt data length is error.\n"); return -1; } control_word |= 0x01; reverse_array(pcipher); if(aes_ctr_operate(src,aest,byte_length,pcipher,ctr_iv) != 0) { printf("aes ctr mode decrypt failture.\n"); return -1; } reverse_array((unsigned char *)pcipher); return 0;}static int aes_ctr_operate(unsigned char * pplaintext,unsigned char * pouttext,unsigned int plaintext_length,unsigned char *pcipher,unsigned char *pctr_iv){ int cltword; static struct trans temp_out; int i,j; if(NULL==pplaintext) { printf("the pointer ctr_ope is null!"); return -1; } if(NULL==pouttext) { printf("the pointer ctr_ope is null!"); return -1; } if(NULL==pcipher) { printf("the pointer ctr_ope is null!"); return -1; } if(NULL==pctr_iv) { printf("the pointer ctr_ope is null!"); return -1; } cltword = control_word; if(aes_ctrl((AES_SET_KEY|cltword),pcipher) != 0) return -1; for(i=0;i<plaintext_length;i+=16) { reverse_array(&pctr_iv[i]); reverse_array(&pplaintext[i]); if(aes_ctrl((AES_SET_IV|cltword),&pctr_iv[i]) != 0) return -1; if(aes_ctrl((AES_SET_M|cltword),&pplaintext[i]) != 0) return -1; if(aes_ctrl((AES_SET_START|cltword),0) != 0) return -1; if(aes_ctrl((AES_GET_DATA|cltword),(unsigned char*)&temp_out) != 0) return -1; if(pouttext == NULL) { for(j=0;j<16;j++) { pplaintext[j + i] = temp_out.outtrans[15-j]; } } else { for(j=0;j<16;j++) { pouttext[j + i] = temp_out.outtrans[15-j]; #ifdef DEBUG printf("poutext[j+cnt]:%x\n",pouttext[j + i]); #endif } } reverse_array((unsigned char *)&pctr_iv[i]); reverse_array((unsigned char *)&pplaintext[i]); //reverse_array((unsigned char *)&pouttext[cnt]); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -