Add KBOOT.

This commit is contained in:
László Monda
2016-08-10 01:45:15 +02:00
commit e6c1fce5b4
9392 changed files with 3751375 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
FREESCALE END-USER SOFTWARE LICENSE AGREEMENT
This is a license agreement between you (either as an individual or as an authorized representative acting on behalf of your employer) and Freescale Semiconductor, Inc. (“Freescale”). It concerns your rights to use the software provided to you in binary or source code form and any accompanying written materials (the “Software”). The Software may include any updates or error corrections or documentation relating to the Software provided to you by Freescale under this License. In consideration for Freescale allowing you to access the Software, you are agreeing to be bound by the terms of this Agreement. If you do not agree to all of the terms of this Agreement, do not download or install the Software. If you change your mind later, stop using the Software and delete all copies of the Software in your possession or control. Any copies of the Software that you have already distributed, where permitted, and do not destroy will continue to be governed by this Agreement. Your prior use will also continue to be governed by this Agreement.
1. LICENSE GRANT. Freescale grants to you, free of charge, the non-exclusive, non-transferable, non-sublicensable right (1) to use the Software, (2) to reproduce the Software, (3) to prepare derivative works of the Software, 4) to distribute the Software and derivative works thereof in object (machinereadable) form as part of a programmable processing unit (e.g. a microprocessor, microcontroller, or digital signal processor) supplied directly or indirectly from Freescale (“Freescale System”), or in source (human-readable) form for the sole purpose of developing object (machine-readable) code for use of a Freescale System and (5) to sublicense to others the right to use the distributed Software, provided that any and all such sublicenses include the same terms and conditions of this Agreement. Notwithstanding the limitation on damages in Section 8, Licensee will indemnify, defend, and hold harmless Freescale against any and all claims, costs, damages, liabilities, judgments and attorneys fees resulting from or arising out of any breach by the sublicensee, or resulting from or arising out of any action by the sublicensee inconsistent with this Agreement.
2. OTHER RESTRICTIONS. Subject to the license grant above, the following restrictions apply:
a. Freescale reserves all rights not expressly granted herein.
b. You may not rent, lease, sublicense, lend or encumber the Software, unless otherwise expressly agreed to within this Agreement
c. You may not distribute, manufacture, have manufactured, sublicense or otherwise reproduce the Software for purposes other than intended in this Agreement.
d. You may not remove or alter any proprietary legends, notices, or trademarks contained in the Licensed Software,
e. The terms and conditions of this Agreement will apply to any Software updates, provided to you at Freescales discretion, that replace and/or supplement the original Software, unless such update contains a separate license.
f. You may not translate, reverse engineer, decompile, or disassemble the Software provided to you solely in object code format (machine readable) except to the extent applicable law specifically prohibits such restriction. You will prohibit your sublicensees from translating, reverse engineering, decompiling, or disassembling the Software except to the extent applicable law specifically prohibits such restriction.
3. OPEN SOURCE. Any open source software included in the Software licensed herein is not licensed under the terms of this Agreement, but is instead licensed under the terms of applicable open source license(s), such as the BSD License, Apache License or the Lesser GNU General Public License. Your use of such open source software is subject to the terms of each applicable license. You must agree to the terms of each such applicable license, or you should not use the open source software.
4. COPYRIGHT. The Software is licensed to you, not sold. Freescale owns the Software, and United States copyright laws and international treaty provisions protect the Software. Therefore, you must treat the Software like any other copyrighted material (e.g. a book or musical recording). You may not use or copy the Software for any other purpose than what is described in this Agreement. Except as expressly provided herein, Freescale does not grant to you any express or implied rights under any Freescale or third party patents, copyrights, trademarks, or trade secrets. Additionally, you must reproduce and apply any copyright or other proprietary rights notices included on or embedded in the Software to any copies made thereof, in whole or in part, if any. You may not remove any copyright notices of Freescale incorporated in the Software.
5. TERM AND TERMINATION. The term of this Agreement shall commence on the date of installation or download and shall continue perpetually, unless earlier terminated in accordance with this Agreement. Freescale has the right to terminate this Agreement without notice and require that you stop using and delete all copies of the Software in your possession or control if you violate any of the terms or restrictions of this Agreement. Freescale may terminate this Agreement should any of the Software become, or in Freescale's reasonable opinion is likely to become, the subject of a claim of intellectual infringement or trade secret misappropriation. Upon termination, you must cease use of and destroy, the Software and confirm compliance in writing to Freescale. Upon termination, the license granted pursuant to this Agreement immediately terminates and the provisions of Sections 4 through 18 will survive any termination of this Agreement.
6. SUPPORT. Freescale is NOT obligated to provide any support, upgrades or new releases of the Software. If you wish, you may contact Freescale and report problems and provide suggestions regarding the Software. Freescale has no obligation whatsoever to respond in any way to such a problem report or suggestion. Freescale may make changes to the Software at any time, without any obligation to notify or provide updated versions of the Software to you.
7. NO WARRANTY. TO THE MAXIMUM EXTENT PERMITTED BY LAW, FREESCALE EXPRESSLY DISCLAIMS ANY WARRANTY FOR THE SOFTWARE. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. YOU ASSUME THE ENTIRE RISK ARISING OUT OF THE USE OR PERFORMANCE OF THE SOFTWARE, OR ANY SYSTEMS YOU DESIGN USING THE SOFTWARE (IF ANY). NOTHING IN THIS AGREEMENT MAY BE CONSTRUED AS A WARRANTY OR REPRESENTATION BY FREESCALE THAT THE SOFTWARE OR ANY DERIVATIVE WORK DEVELOPED WITH OR INCORPORATING THE SOFTWARE WILL BE FREE FROM INFRINGEMENT OF THE INTELLECTUAL PROPERTY RIGHTS OF THIRD PARTIES.
8. INDEMNITY. You agree to fully defend and indemnify Freescale from any and all claims, liabilities, and costs (including reasonable attorneys fees) related to (1) your use (including your sublicensees use, if permitted) of the Software or (2) your violation of the terms and conditions of this Agreement.
9. LIMITATION OF LIABILITY. IN NO EVENT WILL FREESCALE BE LIABLE, WHETHER IN CONTRACT, TORT, OR OTHERWISE, FOR ANY INCIDENTAL, SPECIAL, INDIRECT, CONSEQUENTIAL OR PUNITIVE DAMAGES, INCLUDING, BUT NOT LIMITED TO, DAMAGES FOR ANY LOSS OF USE, LOSS OF TIME, INCONVENIENCE, COMMERCIAL LOSS, OR LOST PROFITS, SAVINGS, OR REVENUES TO THE FULL EXTENT SUCH MAY BE DISCLAIMED BY LAW EVEN IF INFORMED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES. FREESCALES LIABILITY WILL IN ANY EVENT AND UNDER ANY THEORY OF RECOVERY BE LIMITED TO THE TOTAL AMOUNT RECEIVED BY FREESCALE UNDER THIS AGREEMENT.
10. COMPLIANCE WITH LAWS; EXPORT RESTRICTIONS. You must not resell, re-export, or provide, directly or indirectly, the licensed software or direct product thereof, in any form without obtaining appropriate export or re-export licenses from the United States Government and from the country from which the export or re-export is to occur. An export occurs when products, technology, or software is transferred from one country to another by any means, including physical shipments, FTP file transfers, E-mails, faxes, remote server access, conversations, and the like. An export also occurs when technology or software is transferred to a foreign national in the United States, or foreign national of the country in which the business activity is taking place. A foreign national is any person who is neither a citizen nor permanent resident of the United States, or the country in which the business activity is taking place. Furthermore, if an export/import license, permit or other government required authority (collectively referred to as “government authorization”) is required to transfer technology, software, hardware or other Freescale property to non- Freescale party(ies) and is not approved, then Freescale is not obligated to transfer the Software under this Agreement until such “government authorization” is granted..
11. GOVERNMENT RIGHTS. The Licensed Software is a “Commercial Item” as defined in 48 C.F.R. §2.101, consisting of “Commercial Computer Software” and “Commercial Computer Software Documentation,” as such terms are used in 48 C.F.R. § 12.212 or 48 C.F.R. §227.7202, as applicable and are only licensed to U.S. Government end users with the rights as are set forth herein..
12. HIGH RISK ACTIVITIES. You acknowledge that the Software is not fault tolerant and is not designed, manufactured or intended by Freescale for incorporation into products intended for use or resale in on-line control equipment in hazardous, dangerous to life or potentially life-threatening environments requiring fail-safe performance, such as in the operation of nuclear facilities, aircraft navigation or communication systems, air traffic control, direct life support machines or weapons systems, in which the failure of products could lead directly to death, personal injury or severe physical or environmental damage (“High Risk Activities”). You specifically represent and warrant that you will not use the Software or any derivative work of the Software for High Risk Activities.
13. CHOICE OF LAW; VENUE; LIMITATIONS. You agree that the statutes and laws of the United States and the State of Texas, USA, without regard to conflicts of laws principles, will apply to all matters relating to this Agreement or the Software, and you agree that any litigation will be subject to the exclusive jurisdiction of the state or federal courts in Texas, USA. You agree that regardless of any statute or law to the contrary, any claim or cause of action arising out of or related to this Agreement or the Software must be filed within one (1) year after such claim or cause of action arose or be forever barred.
14. CONFIDENTIAL INFORMATION. You must treat the Software as confidential information and you agree to retain the Software in confidence perpetually, with respect to Software in source code form (human readable), or for a period of five (5) years from the date of termination of this Agreement, with respect to all other parts of the Software. During this period you may not disclose any part of the Software to anyone other than employees who have a need to know of the Software and who have executed written agreements obligating them to protect such Licensed Software to at least the same degree of care as in this Agreement. You agree to use the same degree of care, but no less than a reasonable degree of care, with the Software as you do with your own confidential information. You may disclose Software to the extent required by a court or under operation of law or order provided that you notify Freescale of such requirement prior to disclosure, which you only disclose information required, and that you allow Freescale the opportunity to object to such court or other legal body requiring such disclosure.
15. PRODUCT LABELING. You are not authorized to use any Freescale trademarks, brand names, or logos.
16. ENTIRE AGREEMENT. This Agreement constitutes the entire agreement between you and Freescale regarding the subject matter of this Agreement, and supersedes all prior communications, negotiations, understandings, agreements or representations, either written or oral, if any. This Agreement may only be amended in written form, executed by you and Freescale.
17. SEVERABILITY. If any provision of this Agreement is held for any reason to be invalid or unenforceable, then the remaining provisions of this Agreement will be unimpaired and, unless a modification or replacement of the invalid or unenforceable provision is further held to deprive you or Freescale of a material benefit, in which case the Agreement will immediately terminate, the invalid or unenforceable provision will be replaced with a provision that is valid and enforceable and that comes closest to the intention underlying the invalid or unenforceable provision.
18. NO WAIVER. The waiver by Freescale of any breach of any provision of this Agreement will not operate or be construed as a waiver of any other or a subsequent breach of the same or a different provision.

71
src/drivers/mmcau/mmcau.h Normal file
View File

@@ -0,0 +1,71 @@
/*
* Copyright (c) 2014, Freescale Semiconductor, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* o Neither the name of Freescale Semiconductor, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _mmcau_h
#define _mmcau_h
#include "bootloader_common.h"
//! @addtogroup mmcau
//! @{
////////////////////////////////////////////////////////////////////////////////
// API
////////////////////////////////////////////////////////////////////////////////
#if defined(__cplusplus)
extern "C" {
#endif
//! @name mmcau_aes_security
//@{
//! @brief An initialization function for the decryption peripheral
void mmcau_aes_init(uint32_t *key, uint32_t *keySchedule, uint32_t *rcon);
//! @brief Encrypts a 16 byte block of data
//! in and out may use the same address so encrypting in place is supported
void mmcau_aes_encrypt(uint32_t *in, uint32_t *key, uint32_t *keySchedule, uint32_t *out);
//! @brief Decrypts a 16 byte block of data
//! in and out may use the same address so decrypting in place is supported
void mmcau_aes_decrypt(uint32_t *in, uint32_t *key, uint32_t *keySchedule, uint32_t *out);
//@}
#if defined(__cplusplus)
}
#endif
//! @}
#endif // _mmcau_h
////////////////////////////////////////////////////////////////////////////////
// EOF
////////////////////////////////////////////////////////////////////////////////

View File

@@ -0,0 +1,270 @@
/********************************************************************************/
/********************************************************************************/
/**/
/* Copyright (c) Freescale Semiconductor, Inc 2011.*/
/**/
/* FILE NAME : mmcau_aes_functions.c*/
/* VERSION : $Id: mmcau_aes_functions.c.rca 1.2 Sat Jan 8 01:21:45 2011 rzsx60 Experimental $*/
/* TYPE : Source C library code*/
/* DEPARTMENT : MSG R&D Core and Platforms*/
/* AUTHOR : David Schimke*/
/* AUTHOR'S EMAIL : David.Schimke@freescale.com*/
/* -----------------------------------------------------------------------------*/
/* Release history*/
/* VERSION Date AUTHOR DESCRIPTION*/
/* 08-2010 David Schimke Initial Release*/
/* 01-2011 David Schimke Add byte reverse to correct double word*/
/* read of byte array inputs for little*/
/* endian, header added, comment cleanup*/
/**/
/********************************************************************************/
/********************************************************************************/
#include "security/aes_security.h"
#include "fsl_device_registers.h"
#define AES_128_NUMBER_ROUNDS 10
#define mmcau_1_cmd(c1) (0x80000000 + ((c1) << 22))
#define mmcau_2_cmds(c1, c2) (0x80100000 + ((c1) << 22) + ((c2) << 11))
#define mmcau_3_cmds(c1, c2, c3) (0x80100200 + ((c1) << 22) + ((c2) << 11) + c3)
#define byterev(d) (((d) >> 24) | (((d) >> 8) & 0xff00) | (((d) << 8) & 0xff0000) | ((d) << 24))
// Opcodes
#define TL 0
#define TS 0
#define CASR 0
#define CAA 1
#define CA0 2
#define CA1 3
#define CA2 4
#define CA3 5
#define CA4 6
#define CA5 7
#define CA6 8
#define CA7 9
#define CA8 10
#define CNOP 0x000
#define LDR 0x010
#define STR 0x020
#define ADR 0x030
#define RADR 0x040
#define ADRA 0x050
#define XOR 0x060
#define ROTL 0x070
#define MVRA 0x080
#define MVAR 0x090
#define AESS 0x0a0
#define AESIS 0x0b0
#define AESC 0x0c0
#define AESIC 0x0d0
#define AESR 0x0e0
#define AESIR 0x0f0
#define DESR 0x100
#define DESK 0x110
#define HASH 0x120
#define SHS 0x130
#define MDS 0x140
#define SHS2 0x150
#define ILL 0x1f0
#define IP 8
#define FP 4
#define DC 1
#define CP 2
#define KSL1 0
#define KSL2 1
#define KSR1 2
#define KSR2 3
#define HFF 0
#define HFG 1
#define HFH 2
#define HFI 3
#define HFP 2
#define HFC 4
#define HFM 5
#define HF2C 6
#define HF2M 7
#define HF2S 8
#define HF2T 9
#define HF2U 10
#define HF2V 11
#define MMCAU_INDIRECT_MASK 0x800
#define MMCAU_PPB_DIRECT ((volatile uint32_t *)CAU_BASE)
#define MMCAU_PPB_INDIRECT ((volatile uint32_t *)(CAU_BASE | MMCAU_INDIRECT_MASK))
/********************************************************************************/
/********************************************************************************/
/********************************************************************************/
/**/
/* AES: Performs an AES key expansion*/
/* arguments*/
/* *key pointer to input key 128 bits in length */
/* *keySchedule pointer to key schedule 128 bits in length */
/* *rcon needs to be set to the following structure which must be located outside */
/* outside of this function in order to make this code relocatable */
/* unsigned int rcon[10] = {0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, */
/* 0x20000000, 0x40000000, 0x80000000, 0x1b000000, 0x36000000}; */
/**/
void mmcau_aes_init(uint32_t *key, uint32_t *keySchedule, uint32_t *rcon)
{
int32_t i, j;
for (i = 0; i < 4; i++)
{
keySchedule[i] = byterev(key[i]);
}
*(MMCAU_PPB_INDIRECT + (LDR + CAA)) = keySchedule[i - 1]; /* CAA= key[3]*/
for (j = 0; j < 10; j++)
{
*(MMCAU_PPB_INDIRECT + (ROTL + CAA)) = 8; /* rotate 8*/
*(MMCAU_PPB_DIRECT) = mmcau_1_cmd(AESS + CAA); /* SubBytes*/
*(MMCAU_PPB_INDIRECT + (XOR + CAA)) = rcon[j]; /* XOR rcon[j]*/
/* 1st calculation for keySchedule[4+0,8+0,12+0,16+0,20+0,24+0,28+0,32+0,36+0,40+0]*/
*(MMCAU_PPB_INDIRECT + (XOR + CAA)) = keySchedule[i - 4]; /*XOR key[i-4]*/
keySchedule[i++] = *(MMCAU_PPB_INDIRECT + (STR + CAA)); /*store key[i]*/
/* 2nd calculation for keySchedule[4+1,8+1,12+1,16+1,20+1,24+1,28+1,32+1,36+1,40+1]*/
*(MMCAU_PPB_INDIRECT + (XOR + CAA)) = keySchedule[i - 4]; /*XOR key[i-4]*/
keySchedule[i++] = *(MMCAU_PPB_INDIRECT + (STR + CAA)); /*store key[i]*/
/* 3rd calculation for keySchedule[4+2,8+2,12+2,16+2,20+2,24+2,28+2,32+2,36+2,40+2]*/
*(MMCAU_PPB_INDIRECT + (XOR + CAA)) = keySchedule[i - 4]; /*XOR key[i-4]*/
keySchedule[i++] = *(MMCAU_PPB_INDIRECT + (STR + CAA)); /*store key[i]*/
/* 4th calculation for keySchedule[4+3,8+3,12+3,16+3,20+3,24+3,28+3,32+3,36+3,40+3]*/
*(MMCAU_PPB_INDIRECT + (XOR + CAA)) = keySchedule[i - 4]; /*XOR key[i-4]*/
keySchedule[i++] = *(MMCAU_PPB_INDIRECT + (STR + CAA)); /*store key[i]*/
}
}
/********************************************************************************/
/********************************************************************************/
/**/
/* AES: Encrypts a single 16-byte block*/
/* arguments*/
/* *in pointer to 16-byte block of input plaintext*/
/* *key pointer to input key 128 bits in length*/
/* *keySchedule pointer to key schedule 128 bits in length*/
/* *out pointer to 16-byte block of output ciphertext*/
/**/
/* NOTE Input and output blocks may overlap*/
/**/
void mmcau_aes_encrypt(uint32_t *in, uint32_t *key, uint32_t *keySchedule, uint32_t *out)
{
int32_t i, j;
/* load the 4 plain test bytes into the CAU's CA0 - CA3 registers*/
*(MMCAU_PPB_INDIRECT + (LDR + CA0)) = byterev(in[0]); /* load in[0]-> CA0*/
*(MMCAU_PPB_INDIRECT + (LDR + CA1)) = byterev(in[1]); /* load in[1]-> CA1*/
*(MMCAU_PPB_INDIRECT + (LDR + CA2)) = byterev(in[2]); /* load in[2]-> CA2*/
*(MMCAU_PPB_INDIRECT + (LDR + CA3)) = byterev(in[3]); /* load in[3]-> CA3*/
/* XOR the first 4 keys into the CAU's CA0 - CA3 registers*/
*(MMCAU_PPB_INDIRECT + (XOR + CA0)) = keySchedule[0]; /* XOR keys*/
*(MMCAU_PPB_INDIRECT + (XOR + CA1)) = keySchedule[1];
*(MMCAU_PPB_INDIRECT + (XOR + CA2)) = keySchedule[2];
*(MMCAU_PPB_INDIRECT + (XOR + CA3)) = keySchedule[3];
/* send a series of cau commands to perform the encryption*/
for (i = 0, j = 4; i < AES_128_NUMBER_ROUNDS - 1; i++, j += 4)
{
*(MMCAU_PPB_DIRECT) = mmcau_3_cmds(AESS + CA0, AESS + CA1, AESS + CA2); /*Sbytes*/
*(MMCAU_PPB_DIRECT) = mmcau_2_cmds(AESS + CA3, AESR); /*Sbyte,Shft*/
*(MMCAU_PPB_INDIRECT + (AESC + CA0)) = keySchedule[j]; /* MixCols*/
*(MMCAU_PPB_INDIRECT + (AESC + CA1)) = keySchedule[j + 1];
*(MMCAU_PPB_INDIRECT + (AESC + CA2)) = keySchedule[j + 2];
*(MMCAU_PPB_INDIRECT + (AESC + CA3)) = keySchedule[j + 3];
}
*(MMCAU_PPB_DIRECT) = mmcau_3_cmds(AESS + CA0, AESS + CA1, AESS + CA2); /* SubBytes*/
*(MMCAU_PPB_DIRECT) = mmcau_2_cmds(AESS + CA3, AESR); /*SByte,Shft*/
*(MMCAU_PPB_INDIRECT + (XOR + CA0)) = keySchedule[j]; /* XOR keys*/
*(MMCAU_PPB_INDIRECT + (XOR + CA1)) = keySchedule[j + 1];
*(MMCAU_PPB_INDIRECT + (XOR + CA2)) = keySchedule[j + 2];
*(MMCAU_PPB_INDIRECT + (XOR + CA3)) = keySchedule[j + 3];
/* store the 16-byte ciphertext output block into memory*/
out[0] = *(MMCAU_PPB_INDIRECT + (STR + CA0)); /* store 1st 4 bytes*/
out[1] = *(MMCAU_PPB_INDIRECT + (STR + CA1)); /* store 2nd 4 bytes*/
out[2] = *(MMCAU_PPB_INDIRECT + (STR + CA2)); /* store 3rd 4 bytes*/
out[3] = *(MMCAU_PPB_INDIRECT + (STR + CA3)); /* store 4th 4 bytes*/
out[0] = byterev(out[0]); /* Reverse 1st 4 bytes*/
out[1] = byterev(out[1]); /* Reverse 2nd 4 bytes*/
out[2] = byterev(out[2]); /* Reverse 3rd 4 bytes*/
out[3] = byterev(out[3]); /* Reverse 4th 4 bytes*/
}
/********************************************************************************/
/********************************************************************************/
/**/
/* AES: Decrypts a single 16-byte block*/
/* arguments*/
/* *in pointer to 16-byte block of input chiphertext*/
/* *key pointer to input key 128 bits in length*/
/* *keySchedule pointer to key schedule 128 bits in length*/
/* *out pointer to 16-byte block of output plaintext*/
/**/
/* NOTE Input and output blocks may overlap*/
/**/
void mmcau_aes_decrypt(uint32_t *in, uint32_t *key, uint32_t *keySchedule, uint32_t *out)
{
int32_t i;
/* load the cipher bytes into the CAU's CA0 - CA3 registers*/
*(MMCAU_PPB_INDIRECT + (LDR + CA0)) = byterev(in[0]); /* load in[0] -> CA0*/
*(MMCAU_PPB_INDIRECT + (LDR + CA1)) = byterev(in[1]); /* load in[1] -> CA1*/
*(MMCAU_PPB_INDIRECT + (LDR + CA2)) = byterev(in[2]); /* load in[2] -> CA2*/
*(MMCAU_PPB_INDIRECT + (LDR + CA3)) = byterev(in[3]); /* load in[3] -> CA3*/
/* the keySchedule index (i) is adjusted to define the end of the elements*/
/* the adjustment factor = f(nr) is defined by the expression:*/
/* end of keySchedule = 4 x (nr + 1) for nr = {10, 12, 14}*/
i = 4 * (AES_128_NUMBER_ROUNDS + 1);
/* XOR the keys into the CAU's CA0 - CA3 registers*/
*(MMCAU_PPB_INDIRECT + (XOR + CA3)) = keySchedule[--i]; /* XOR keys*/
*(MMCAU_PPB_INDIRECT + (XOR + CA2)) = keySchedule[--i];
*(MMCAU_PPB_INDIRECT + (XOR + CA1)) = keySchedule[--i];
*(MMCAU_PPB_INDIRECT + (XOR + CA0)) = keySchedule[--i];
/* send a series of cau commands to perform the decryption*/
while (i > 4)
{
*(MMCAU_PPB_DIRECT) = mmcau_3_cmds(AESIR, AESIS + CA3, AESIS + CA2);
/* InvShiftRows,InvSubBytes*/
*(MMCAU_PPB_DIRECT) = mmcau_2_cmds(AESIS + CA1, AESIS + CA0); /* InvSubByts*/
*(MMCAU_PPB_INDIRECT + (AESIC + CA3)) = keySchedule[--i]; /* InvMxCols*/
*(MMCAU_PPB_INDIRECT + (AESIC + CA2)) = keySchedule[--i];
*(MMCAU_PPB_INDIRECT + (AESIC + CA1)) = keySchedule[--i];
*(MMCAU_PPB_INDIRECT + (AESIC + CA0)) = keySchedule[--i];
}
*(MMCAU_PPB_DIRECT) = mmcau_3_cmds(AESIR, AESIS + CA3, AESIS + CA2);
/* InvShiftRows,InvSubBytes*/
*(MMCAU_PPB_DIRECT) = mmcau_2_cmds(AESIS + CA1, AESIS + CA0); /* InvSBytes*/
*(MMCAU_PPB_INDIRECT + (XOR + CA3)) = keySchedule[--i]; /* XOR keys*/
*(MMCAU_PPB_INDIRECT + (XOR + CA2)) = keySchedule[--i];
*(MMCAU_PPB_INDIRECT + (XOR + CA1)) = keySchedule[--i];
*(MMCAU_PPB_INDIRECT + (XOR + CA0)) = keySchedule[--i];
/* store the 16-byte ciphertext output block into memory*/
out[0] = *(MMCAU_PPB_INDIRECT + (STR + CA0)); /* store 1st 4 bytes*/
out[1] = *(MMCAU_PPB_INDIRECT + (STR + CA1)); /* store 2nd 4 bytes*/
out[2] = *(MMCAU_PPB_INDIRECT + (STR + CA2)); /* store 3rd 4 bytes*/
out[3] = *(MMCAU_PPB_INDIRECT + (STR + CA3)); /* store 4th 4 bytes*/
out[0] = byterev(out[0]); /* Reverse 1st 4 bytes*/
out[1] = byterev(out[1]); /* Reverse 2nd 4 bytes*/
out[2] = byterev(out[2]); /* Reverse 3rd 4 bytes*/
out[3] = byterev(out[3]); /* Reverse 4th 4 bytes*/
}