📄 testaesa.c
字号:
/**************************************************************************** * testAesa.c - AES known-answer test for SEC2 device driver **************************************************************************** * Copyright (c) 2004-2005 Freescale Semiconductor * All Rights Reserved. Proprietary and Confidential. * * NOTICE: The information contained in this file is proprietary * to Freescale Semiconductor, and is being made available to * Freescale's customers under strict license agreements. * Use or disclosure of this information is permissible only * under the terms of the existing license agreement. ***************************************************************************//* Revision History: * 1.1.0 Dec 05,2004 sec - prep for linux-compatible driver release * 1.2 02-Feb-2005 sec - convert test data to const, fix warnings */#include <stdio.h>#include "sec2drvTest.h"#include "Sec2.h"#define AES_TESTSIZE (32)#define AES_KEYSIZE (16)static const unsigned char keyData[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87};static const unsigned char AesaData[] ={"Now is the time for all good men"}; /* 32 bytes */static unsigned char decryptBuf[AES_TESTSIZE], encryptBuf[AES_TESTSIZE];extern int SEC2_ioctl(register int ioctlCode, register void *param);extern int waitCompletion(char *testname, int iostat, void *req);extern volatile char proceed;int testAesECB(int algoType){ AESA_CRYPT_REQ aesaencReq; AESA_CRYPT_REQ aesadecReq; int status; printf("\n*** Test AESA ECB ***\n"); memset(decryptBuf, 0, AES_TESTSIZE); memset(encryptBuf, 0, AES_TESTSIZE); memset(&aesaencReq, 0, sizeof(aesaencReq)); memset(&aesadecReq, 0, sizeof(aesadecReq)); /* encrypt cycle */ aesaencReq.opId = DPD_AESA_ECB_ENCRYPT_CRYPT; aesaencReq.keyBytes = AES_KEYSIZE; aesaencReq.keyData = (unsigned char *)keyData; aesaencReq.inBytes = AES_TESTSIZE; aesaencReq.inData = (unsigned char *)AesaData; aesaencReq.outData = encryptBuf; status = SEC2_ioctl(IOCTL_PROC_REQ, &aesaencReq); if ((status = waitCompletion("testAesa(): ECB encryption test", status, (void*)&aesaencReq))) { return status; } /* decrypt cycle */ aesadecReq.opId = DPD_AESA_ECB_DECRYPT_CRYPT; aesadecReq.keyBytes = AES_KEYSIZE; aesadecReq.keyData = (unsigned char *)keyData; aesadecReq.inBytes = AES_TESTSIZE; aesadecReq.inData = encryptBuf; aesadecReq.outData = decryptBuf; status = SEC2_ioctl(IOCTL_PROC_REQ, &aesaencReq); if ((status = waitCompletion("testAesa(): ECB encryption test", status, (void*)&aesadecReq))) { return status; } /* run results comparison */ if ((memcmp(AesaData, decryptBuf, AES_TESTSIZE)) == 0) { printf("*** Test AES ECB Passed ***\n"); status = 0; } else { printf("*** Test AES ECB Failed ***\n"); status = -1; } return status;}int testAesCBC(int algoType){ AESA_CRYPT_REQ aesaencReq; AESA_CRYPT_REQ aesadecReq; unsigned char aesCtxOut[AES_KEYSIZE]; unsigned char iv_in[AES_KEYSIZE] = {0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10, 0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10}; int status; printf("\n*** Test AESA CBC ***\n"); memset(decryptBuf, 0, AES_TESTSIZE); memset(encryptBuf, 0, AES_TESTSIZE); memset(&aesaencReq, 0, sizeof(aesaencReq)); memset(&aesadecReq, 0, sizeof(aesadecReq)); /* encrypt cycle */ aesaencReq.opId = DPD_AESA_CBC_ENCRYPT_CRYPT; aesaencReq.inIvBytes = AES_KEYSIZE; aesaencReq.inIvData = (unsigned char *)iv_in; aesaencReq.keyBytes = AES_KEYSIZE; aesaencReq.keyData = (unsigned char *)keyData; aesaencReq.inBytes = AES_TESTSIZE; aesaencReq.inData = (unsigned char *)AesaData; aesaencReq.outData = encryptBuf; aesaencReq.outCtxBytes = AES_KEYSIZE; aesaencReq.outCtxData = (unsigned char *)aesCtxOut; status = ioctl(algoType, IOCTL_PROC_REQ, (int)&aesaencReq); if ((status = waitCompletion("testAesa(): CBC encryption test", status, &aesaencReq))) { return status; } /* decrypt cycle */ aesadecReq.opId = DPD_AESA_CBC_DECRYPT_CRYPT; aesadecReq.inIvBytes = AES_KEYSIZE; aesadecReq.inIvData = (unsigned char *)iv_in; aesadecReq.keyBytes = AES_KEYSIZE; aesadecReq.keyData = (unsigned char *)keyData; aesadecReq.inBytes = AES_TESTSIZE; aesadecReq.inData = encryptBuf; aesadecReq.outData = decryptBuf; aesadecReq.outCtxBytes = AES_KEYSIZE; aesadecReq.outCtxData = (unsigned char *)aesCtxOut; status = SEC2_ioctl(IOCTL_PROC_REQ, &aesaencReq); if ((status = waitCompletion("testAesa(): CBC encryption test", status, &aesadecReq))) { return status; } /* compare results */ if ((memcmp(AesaData, decryptBuf, AES_TESTSIZE)) == 0) { printf("*** Test AES CBC Mode Passed ***\n"); status = 0; } else { printf("*** Test AES CBC Mode Failed ***\n"); status = -1; } return status;}int testAesCTR(int algoType){ AESA_CRYPT_REQ aesaencReq; AESA_CRYPT_REQ aesadecReq; int status; printf("\n*** Test AESA Counter Mode***\n"); memset(decryptBuf, 0, AES_TESTSIZE); memset(encryptBuf, 0, AES_TESTSIZE); memset(&aesaencReq, 0, sizeof(aesaencReq)); memset(&aesadecReq, 0, sizeof(aesadecReq)); aesaencReq.opId = DPD_AESA_CTR_CRYPT; aesaencReq.keyBytes = AES_KEYSIZE; aesaencReq.keyData = (unsigned char *)keyData; aesaencReq.inBytes = AES_TESTSIZE; aesaencReq.inData = (unsigned char *)AesaData; aesaencReq.outData = encryptBuf; status = ioctl(algoType, IOCTL_PROC_REQ, (int)&aesaencReq); if ((status = waitCompletion("testAesa(): counter mode encryption test", status, &aesaencReq))) { return status; } aesadecReq.opId = DPD_AESA_CTR_CRYPT; aesadecReq.keyBytes = AES_KEYSIZE; aesadecReq.keyData = (unsigned char *)keyData; aesadecReq.inBytes = AES_TESTSIZE; aesadecReq.inData = encryptBuf; aesadecReq.outData = decryptBuf; status = SEC2_ioctl(IOCTL_PROC_REQ, &aesaencReq); if ((status = waitCompletion("testAesa(): counter mode decryption test", status, &aesadecReq))) { return status; } if ((memcmp(AesaData, decryptBuf, AES_TESTSIZE)) == 0) { printf("*** Test AESA Counter Mode Passed ***\n"); status = 0; } else { printf("*** Test AESA Counter Mode Failed ***\n"); status = -1; } return status;}int testAesa(){ int status; int algoType; status = testAesECB(algoType); if (status) return status; status = testAesCBC(algoType); if (status) return status; status = testAesCTR(algoType); return status;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -