LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
aes_18xx_43xx.c
Go to the documentation of this file.
1 /*
2  * @brief LPC18xx/43xx AES Engine driver
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2012
6  * All rights reserved.
7  *
8  * @par
9  * Software that is described herein is for illustrative purposes only
10  * which provides customers with programming information regarding the
11  * LPC products. This software is supplied "AS IS" without any warranties of
12  * any kind, and NXP Semiconductors and its licensor disclaim any and
13  * all warranties, express or implied, including all implied warranties of
14  * merchantability, fitness for a particular purpose and non-infringement of
15  * intellectual property rights. NXP Semiconductors assumes no responsibility
16  * or liability for the use of the software, conveys no license or rights under any
17  * patent, copyright, mask work right, or any other intellectual property rights in
18  * or to any products. NXP Semiconductors reserves the right to make changes
19  * in the software without notification. NXP Semiconductors also makes no
20  * representation or warranty that such application will be suitable for the
21  * specified use without further testing or modification.
22  *
23  * @par
24  * Permission to use, copy, modify, and distribute this software and its
25  * documentation is hereby granted, under NXP Semiconductors' and its
26  * licensor's relevant copyrights in the software, without fee, provided that it
27  * is used in conjunction with NXP Semiconductors microcontrollers. This
28  * copyright, permission, and disclaimer notice must appear in all copies of
29  * this code.
30  */
31 
32 #include "chip.h"
33 
34 /*****************************************************************************
35  * Private types/enumerations/variables
36  ****************************************************************************/
37 
38 #define BOOTROM_BASE 0x10400100
39 #define AES_API_TABLE_OFFSET 0x2
40 
41 static unsigned long *BOOTROM_API_TABLE;
42 
43 /*****************************************************************************
44  * Public types/enumerations/variables
45  ****************************************************************************/
46 
47 /*****************************************************************************
48  * Private functions
49  ****************************************************************************/
50 
52 static void (*aes_LoadKey1)(void);
53 static void (*aes_LoadKey2)(void);
54 static void (*aes_LoadKeyRNG)(void);
55 static void (*aes_LoadKeySW)(uint8_t *pKey);
56 static void (*aes_LoadIV_SW)(uint8_t *pVector);
57 static void (*aes_LoadIV_IC)(void);
58 static uint32_t (*aes_Operate)(uint8_t *pDatOut, uint8_t *pDatIn, uint32_t size);
59 static uint32_t (*aes_ProgramKey1)(uint8_t *pKey);
60 static uint32_t (*aes_ProgramKey2)(uint8_t *pKey);
61 
62 /*****************************************************************************
63  * Public functions
64  ****************************************************************************/
65 
66 /* CHIP AES Initialisation function */
67 void Chip_AES_Init(void)
68 {
69  uint32_t (*ROM_aes_Init)(void);
70 
71  BOOTROM_API_TABLE = *((unsigned long * *) BOOTROM_BASE + AES_API_TABLE_OFFSET);
72 
73  ROM_aes_Init = (uint32_t (*)(void))BOOTROM_API_TABLE[0];
75  aes_LoadKey1 = (void (*)(void))BOOTROM_API_TABLE[2];
76  aes_LoadKey2 = (void (*)(void))BOOTROM_API_TABLE[3];
77  aes_LoadKeyRNG = (void (*)(void))BOOTROM_API_TABLE[4];
78  aes_LoadKeySW = (void (*)(uint8_t *pKey))BOOTROM_API_TABLE[5];
79  aes_LoadIV_SW = (void (*)(uint8_t *pVector))BOOTROM_API_TABLE[6];
80  aes_LoadIV_IC = (void (*)(void))BOOTROM_API_TABLE[7];
81  aes_Operate = (uint32_t (*)(uint8_t *pDatOut, uint8_t *pDatIn, uint32_t Size))BOOTROM_API_TABLE[8];
82  aes_ProgramKey1 = (uint32_t (*)(uint8_t *pKey))BOOTROM_API_TABLE[9];
83  aes_ProgramKey2 = (uint32_t (*)(uint8_t *pKey))BOOTROM_API_TABLE[10];
84 
85  ROM_aes_Init();
86 }
87 
88 /* Set Operation mode in AES Engine */
90 {
91  return aes_SetMode(AesMode);
92 }
93 
94 /* Load 128-bit user key in AES Engine */
96 {
97  if (keyNum) {
98  aes_LoadKey2();
99  }
100  else {
101  aes_LoadKey1();
102  }
103 }
104 
105 /* Load randomly generated key in AES engine */
107 {
108  aes_LoadKeyRNG();
109 }
110 
111 /* Load 128-bit AES software defined user key */
112 void Chip_AES_LoadKeySW(uint8_t *pKey)
113 {
114  aes_LoadKeySW(pKey);
115 }
116 
117 /* Load 128-bit AES initialization vector */
118 void Chip_AES_LoadIV_SW(uint8_t *pVector)
119 {
120  aes_LoadIV_SW(pVector);
121 }
122 
123 /* Load IC specific 128-bit AES initialization vector */
125 {
126  aes_LoadIV_IC();
127 }
128 
129 /* Operate AES Engine */
130 uint32_t Chip_AES_Operate(uint8_t *pDatOut, uint8_t *pDatIn, uint32_t Size)
131 {
132  return aes_Operate(pDatOut, pDatIn, Size);
133 }
134 
135 /* Program 128-bit AES Key in OTP */
136 uint32_t Chip_AES_ProgramKey(uint32_t KeyNum, uint8_t *pKey)
137 {
139 
140  if (KeyNum) {
141  status = aes_ProgramKey2(pKey);
142  }
143  else {
144  status = aes_ProgramKey1(pKey);
145  }
146  return status;
147 }