📄 main.c
字号:
/**************************************************************************** * Copyright (c) Sigma Designs, Inc. 2006. All rights reserved. *//** * @file main.c * * @brief Tests ABCDRM for the SMP8634 * * @version 0.1 * * @buglog First revision * * @author Thulasi Jeganathan * * @date 2006-04-28 * ****************************************************************************/#define ALLOW_OS_CODE#include <stdio.h>#include <string.h>#include "rmcore/include/rmcore.h"#include "rua/include/rua.h"#include "abcdrm_api.h"#define AES_BLOCK_SIZE 16#define ABCDRM_SERIAL_FLASH_SECTOR 1// #define TUTORIAL_STEP_1 1// #define TUTORIAL_STEP_2 1// #define TUTORIAL_STEP_3 1// #define TUTORIAL_STEP_4 1// #define TUTORIAL_STEP_5 1// #define TUTORIAL_STEP_6 1// #define TUTORIAL_STEP_7 1#ifdef TUTORIAL_STEP_1RMuint8 vector1[AES_BLOCK_SIZE] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};RMuint8 answer1[AES_BLOCK_SIZE] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10};#endif // TUTORIAL_STEP_1#ifdef TUTORIAL_STEP_2RMuint8 vector2a[AES_BLOCK_SIZE] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};RMuint8 vector2b[AES_BLOCK_SIZE] = {0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0};RMuint8 vector2c[AES_BLOCK_SIZE] = {0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13};RMuint8 answer2[AES_BLOCK_SIZE] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};#endif // TUTORIAL_STEP_2#ifdef TUTORIAL_STEP_3RMuint8 aes_key3[AES_BLOCK_SIZE] = {0x49, 0x03, 0x29, 0xba, 0x3d, 0x90, 0x46, 0xbc, 0x74, 0x89, 0x59, 0xc5, 0x38, 0x7a, 0xe4, 0x56};RMuint8 aes_iv3[AES_BLOCK_SIZE] = {0xd9, 0xb7, 0x51, 0xdb, 0xd7, 0x9e, 0x69, 0x44, 0x3c, 0xda, 0xa0, 0xbe, 0xef, 0x2e, 0xe8, 0x8c};#endif // TUTORIAL_STEP_3#ifdef TUTORIAL_STEP_5RMuint8 vector5[AES_BLOCK_SIZE] = {0x49, 0x39, 0xd6, 0xf4, 0xa4, 0x8e, 0xec, 0x97, 0x00, 0x96, 0x3a, 0xdd, 0x47, 0x0a, 0x13, 0x3c};RMuint8 answer5[AES_BLOCK_SIZE] = {0x47, 0x1f, 0xff, 0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};#endif // TUTORIAL_STEP_5#ifdef TUTORIAL_STEP_6#define TS6_BUF_SIZE (AES_BLOCK_SIZE * 8)RMuint32 ts6_base_addr = 0;RMuint8 buffer6[TS6_BUF_SIZE] = {0xb8, 0xff, 0x36, 0x63, 0x75, 0x7f, 0xbc, 0x03, 0xad, 0xdd, 0xad, 0xbf, 0x32, 0x73, 0xbe, 0xb4, 0xc7, 0x81, 0x1a, 0xa2, 0x6b, 0xa8, 0x5b, 0x91, 0x3e, 0xf5, 0x0a, 0xba, 0xd6, 0x30, 0x05, 0xe0, 0xb7, 0xb7, 0x8d, 0x36, 0xa7, 0x32, 0x85, 0x1f, 0xf9, 0x5b, 0x41, 0x06, 0x76, 0x9f, 0x7c, 0xc5, 0x98, 0x01, 0x0c, 0xa8, 0x41, 0xb9, 0x0d, 0xd1, 0xf4, 0x8f, 0x70, 0xb6, 0xfa, 0x58, 0x0d, 0xc3, 0x22, 0xee, 0x6f, 0xbd, 0xb8, 0x3d, 0x7b, 0xcd, 0xb1, 0xf2, 0x63, 0xd8, 0xe4, 0x4e, 0x02, 0x17, 0x7c, 0x5f, 0x07, 0xdb, 0xa5, 0xbd, 0x94, 0xfd, 0x6f, 0xcc, 0x5e, 0x59, 0xe4, 0xf9, 0xf3, 0x24, 0x36, 0x95, 0x62, 0xb4, 0x3b, 0x0d, 0x2d, 0x30, 0x46, 0xe9, 0xe9, 0x47, 0x5b, 0x19, 0x6c, 0xfe, 0xcf, 0xa6, 0xed, 0x81, 0x8e, 0x7e, 0x1f, 0xf3, 0xb2, 0x25, 0xf7, 0xef, 0x1d, 0x37, 0xef, 0xcd};RMuint8 answer6[TS6_BUF_SIZE] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f};#endif // TUTORIAL_STEP_6#ifdef TUTORIAL_STEP_7#define TS7_BUF_SIZE (AES_BLOCK_SIZE * 8)struct RUABufferPool *pDMA = NULL;RMuint8 buffer7[TS7_BUF_SIZE] = {0x49, 0x39, 0xd6, 0xf4, 0xa4, 0x8e, 0xec, 0x97, 0x00, 0x96, 0x3a, 0xdd, 0x47, 0x0a, 0x13, 0x3c, 0x7f, 0xe0, 0xf1, 0x2e, 0x21, 0xaf, 0xef, 0xec, 0xe1, 0xc0, 0x2c, 0x0a, 0xc2, 0xc8, 0x60, 0x3f, 0xe4, 0xdb, 0x5b, 0x91, 0xc9, 0x77, 0x6c, 0x89, 0x77, 0x09, 0x2e, 0xec, 0x73, 0x6f, 0x99, 0x94, 0x45, 0xe5, 0x9c, 0x5a, 0xf5, 0x01, 0xfe, 0x87, 0xc7, 0xd5, 0x8d, 0x13, 0x2c, 0xee, 0x92, 0x7b, 0x9d, 0xc0, 0x6c, 0xd8, 0x00, 0x5e, 0x15, 0xce, 0x54, 0x21, 0xee, 0xca, 0x7c, 0x84, 0xb9, 0xe6, 0x1c, 0xb9, 0x55, 0x3b, 0xcf, 0x78, 0x74, 0x7c, 0x31, 0xed, 0xc7, 0xac, 0xf9, 0x41, 0x2c, 0x48, 0x5f, 0x3b, 0x9c, 0xa9, 0xc4, 0x80, 0x89, 0xfe, 0x6a, 0x1e, 0x59, 0xa8, 0x45, 0x6f, 0x4d, 0x3a, 0xaf, 0x30, 0x6f, 0x07, 0xb3, 0xb7, 0x13, 0x77, 0x7d, 0x9f, 0x69, 0xb8, 0xab, 0xe2, 0x58, 0x1e};RMuint8 answer7[TS7_BUF_SIZE] = {0x47, 0x1f, 0xff, 0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};#endif // TUTORIAL_STEP_7/* Global state variables */struct RUA *pInstance = NULL;RMuint32 xrpc_base_addr = 0;RMbool abcdrm_init_flag = FALSE;static RMstatus test_abcdrm_initialization(){ RMstatus status; /* Initialize global state variables */ pInstance = NULL; xrpc_base_addr = 0; abcdrm_init_flag = FALSE; /* Create an instance of RUA */ status = RUACreateInstance(&pInstance, 0); if (status != RM_OK) { printf("RUACreateInstance failed %s\n", RMstatusToString(status)); pInstance = NULL; return status; } /* Allocate a temporary RUA buffer */ xrpc_base_addr = RUAMalloc(pInstance, 0, RUA_DRAM_UNPROTECTED, ABCDRM_XRPC_SIZE); if (xrpc_base_addr == 0) { printf("RUAMalloc failed\n"); return RM_ERROR; } /* Initialize the ABCDRM shared library and xtask */#ifdef WITH_DRM_PRELOAD_XTASK /* Assumes ruaxrpc -xload xtask_drm_abcdrm.xload into image slot 0 */ status = abcdrm_initialize_with_preload(xrpc_base_addr, ABCDRM_XRPC_SIZE, ABCDRM_SERIAL_FLASH_SECTOR, 0);#else status = abcdrm_initialize(xrpc_base_addr, ABCDRM_XRPC_SIZE, ABCDRM_SERIAL_FLASH_SECTOR);#endif if (status != RM_OK) { printf("abcdrm_initialize failed %s\n", RMstatusToString(status)); return status; } /* ABCDRM has been initialized successfully */ abcdrm_init_flag = TRUE; printf("ABCDRM initialized\n"); return status;}static void test_abcdrm_termination(){ /* Terminate the ABCDRM xtask and shared library */ if (abcdrm_init_flag == TRUE) { if (abcdrm_terminate() != RM_OK) printf("abcdrm_terminate failed\n"); } /* Free temporary RUA buffer */ if (xrpc_base_addr != 0) { RUAFree(pInstance, xrpc_base_addr); } /* Terminate RUA */ if (pInstance != NULL) { RUADestroyInstance(pInstance); } /* Reset global state variables */ pInstance = NULL; xrpc_base_addr = 0; abcdrm_init_flag = FALSE; printf("ABCDRM terminated\n");}static RMstatus test_abcdrm_functionality(){ RMstatus status = RM_OK;#ifdef TUTORIAL_STEP_1 /* Increment a test vector */ status = abcdrm_increment_vector(vector1); if (status != RM_OK) { printf("abcdrm_increment_vector failed %s\n", RMstatusToString(status)); } else { /* Compare the incremented vector with the correct answer */ if (memcmp(vector1, answer1, AES_BLOCK_SIZE) != 0) printf("TUTORIAL STEP 1 vector not incremented correctly\n"); else printf("TUTORIAL STEP 1 successful\n"); }#endif // TUTORIAL_STEP_1#ifdef TUTORIAL_STEP_2 /* Add first and second vectors, writing the result into the third vector */ status = abcdrm_add_vectors(vector2a, vector2b, vector2c); if (status != RM_OK) { printf("abcdrm_add_vectors failed %s\n", RMstatusToString(status)); } else { /* Compare the added vector with the correct answer */ if (memcmp(vector2c, answer2, AES_BLOCK_SIZE) != 0) printf("TUTORIAL STEP 2 vectors not added correctly\n"); else printf("TUTORIAL STEP 2 successful\n"); }#endif // TUTORIAL_STEP_2#ifndef TUTORIAL_STEP_7#ifndef TUTORIAL_STEP_6#ifndef TUTORIAL_STEP_5#ifndef TUTORIAL_STEP_4#ifdef TUTORIAL_STEP_3 /* Save the AES key and iv into the internal serial flash */ status = abcdrm_save_key(aes_key3, aes_iv3); if (status != RM_OK) { printf("abcdrm_save_key failed %s\n", RMstatusToString(status)); } else { printf("TUTORIAL STEP 3 successful\n"); }#endif // TUTORIAL_STEP_3#endif // TUTORIAL_STEP_4#endif // TUTORIAL_STEP_5#endif // TUTORIAL_STEP_6#endif // TUTORIAL_STEP_7#ifdef TUTORIAL_STEP_4 /* Load the AES key and iv from the internal serial flash and display them here */ printf("TUTORIAL STEP 4 exercise to be implemented by students\n");#endif // TUTORIAL_STEP_4#ifndef TUTORIAL_STEP_7#ifndef TUTORIAL_STEP_6#ifdef TUTORIAL_STEP_5 /* Decrypt vector using AES key and iv from the internal serial flash */ status = abcdrm_decrypt_vector(vector5); if (status != RM_OK) { printf("abcdrm_decrypt_vector failed %s\n", RMstatusToString(status)); } else { /* Compare the decrypted vector with the correct answer */ if (memcmp(vector5, answer5, AES_BLOCK_SIZE) != 0) printf("TUTORIAL STEP 5 vector not decrypted correctly\n"); else printf("TUTORIAL STEP 5 successful\n"); }#endif // TUTORIAL_STEP_5#endif // TUTORIAL_STEP_6#endif // TUTORIAL_STEP_7#ifndef TUTORIAL_STEP_7#ifndef TUTORIAL_STEP_5#ifdef TUTORIAL_STEP_6 /* Allocate a temporary RUA buffer */ ts6_base_addr = RUAMalloc(pInstance, 0, RUA_DRAM_UNPROTECTED, TS6_BUF_SIZE); if (ts6_base_addr == 0) { printf("RUAMalloc failed\n"); status = RM_ERROR; } else { /* Lock the temporary RUA buffer so it can be mapped */ status = RUALock(pInstance, ts6_base_addr, TS6_BUF_SIZE); if (status != RM_OK) { printf("RUALock failed\n"); } else { RMuint8 *ts6_map_addr = NULL; /* Map the temporary RUA buffer */ ts6_map_addr = RUAMap(pInstance, ts6_base_addr, TS6_BUF_SIZE); if (ts6_map_addr == NULL) { printf("RUAMap failed\n"); status = RM_ERROR; } else { /* Copy the Tutorial Step 6 encrypted buffer into the mapped RUA buffer */ memcpy(ts6_map_addr, buffer6, TS6_BUF_SIZE); /* Decrypt RUA buffer using AES key and iv from the internal serial flash */ status = abcdrm_decrypt_buffer(ts6_base_addr, TS6_BUF_SIZE); if (status != RM_OK) { printf("abcdrm_decrypt_buffer failed %s\n", RMstatusToString(status)); } else { /* Compare the decrypted buffer with the correct answer */ if (memcmp(ts6_map_addr, answer6, TS6_BUF_SIZE) != 0) printf("TUTORIAL STEP 6 buffer not decrypted correctly\n"); else printf("TUTORIAL STEP 6 successful\n"); } /* Unmap the temporary RUA buffer */ RUAUnMap(pInstance, ts6_map_addr, TS6_BUF_SIZE); } /* Unlock the temporary RUA buffer */ RUAUnLock(pInstance, ts6_base_addr, TS6_BUF_SIZE); } /* Free the temporary RUA buffer */ RUAFree(pInstance, ts6_base_addr); }#endif // TUTORIAL_STEP_6#endif // TUTORIAL_STEP_5#endif // TUTORIAL_STEP_7#ifndef TUTORIAL_STEP_6#ifndef TUTORIAL_STEP_5#ifdef TUTORIAL_STEP_7 /* Open a temporary RUA buffer pool */ pDMA = NULL; status = RUAOpenPool(pInstance, 0, 4, 12, RUA_POOL_DIRECTION_SEND, &pDMA); if ((status != RM_OK) || (pDMA == NULL)) { printf("RUAOpenPool failed\n"); } else { RMuint8 *buf = NULL; /* Get a RUA buffer from the pool */ status = RUAGetBuffer(pDMA, &buf, 0); if ((status != RM_OK) || (buf == NULL)) { printf("RUAGetBuffer failed\n"); } else { RMuint32 ts7_base_addr = 0; /* Copy the Tutorial Step 7 encrypted buffer into the RUA buffer from the pool */ memcpy(buf, buffer7, TS7_BUF_SIZE); /* Get the physical address of the RUA buffer from the pool */ status = RUAGetPhysicalAddress(pDMA, buf, TS7_BUF_SIZE, &ts7_base_addr); if ((status != RM_OK) || (ts7_base_addr == 0)) { printf("RUAGetPhysicalAddress failed\n"); } else { /* Make sure the buffer has been completely dumped from the CPU's cache to the DRAM */ status = RUACleanCache(pDMA, buf, TS7_BUF_SIZE); if (status != RM_OK) { printf("RUACleanCache failed\n"); } else { /* Make sure the CPU's cache lines are invalidated */ /* so decrypted data will be forced in from the DRAM */ status = RUAInvalidateCache(pDMA, buf, TS7_BUF_SIZE); if (status != RM_OK) { printf("RUAInvalidateCache failed\n"); } else { /* Decrypt RUA buffer from the pool using */ /* AES key and iv from the internal serial flash */ status = abcdrm_decrypt_buffer(ts7_base_addr, TS7_BUF_SIZE); if (status != RM_OK) { printf("abcdrm_decrypt_buffer failed %s\n", RMstatusToString(status)); } else { /* Compare the decrypted buffer with the correct answer */ if (memcmp(buf, answer7, TS7_BUF_SIZE) != 0) printf("TUTORIAL STEP 7 buffer not decrypted correctly\n"); else printf("TUTORIAL STEP 7 successful\n"); } } } } /* Return the RUA buffer back to the pool */ RUAReleaseBuffer(pDMA, buf); } /* Close the temporary RUA buffer pool */ RUAClosePool(pDMA); }#endif // TUTORIAL_STEP_7#endif // TUTORIAL_STEP_5#endif // TUTORIAL_STEP_6 return status;}int main(){ /* If ABCDRM initializes successfully, then test functionality */ if (test_abcdrm_initialization() == RM_OK) test_abcdrm_functionality(); /* Always call terminate to clean up ABCDRM */ test_abcdrm_termination(); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -