CC13xx Driver Library
rom_crypto.c
Go to the documentation of this file.
1 /*******************************************************************************
2 * Filename: rom_crypto.c
3 * Revised: 2015-06-10 19:22:27 +0200 (Wed, 10 Jun 2015)
4 * Revision: 43869
5 *
6 * Description: This is the implementation for the API to the AES, ECC and
7 * SHA256 functions built into ROM on the CC26xx.
8 *
9 * Copyright (c) 2015, Texas Instruments Incorporated
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions are met:
14 *
15 * 1) Redistributions of source code must retain the above copyright notice,
16 * this list of conditions and the following disclaimer.
17 *
18 * 2) Redistributions in binary form must reproduce the above copyright notice,
19 * this list of conditions and the following disclaimer in the documentation
20 * and/or other materials provided with the distribution.
21 *
22 * 3) Neither the name of the ORGANIZATION nor the names of its contributors may
23 * be used to endorse or promote products derived from this software without
24 * specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 *
38 *******************************************************************************/
39 
40 #include <stdint.h>
41 #include <driverlib/rom_crypto.h>
42 
44 
45 /* AES - ECB */
46 typedef void(*aes_ecb_encrypt_t)(uint8_t *, uint16_t, uint8_t *);
48 
49 typedef void(*aes_ecb_decrypt_t)(uint8_t *, uint16_t, uint8_t *);
51 
52 //*****************************************************************************
53 // AES_ECB_EncryptData
54 //*****************************************************************************
55 void AES_ECB_EncryptData(uint8_t *text, uint16_t textLen, uint8_t *aesKey)
56 {
57  aes_ecb_encrypt(text, textLen, aesKey);
58 }
59 
60 //*****************************************************************************
61 // AES_ECB_DecryptData
62 //*****************************************************************************
63 void AES_ECB_DecryptData(uint8_t *text, uint16_t textLen, uint8_t *aesKey)
64 {
65  aes_ecb_decrypt(text, textLen, aesKey);
66 }
67 
68 /* AES - CCM */
69 typedef int8_t(*aes_ccm_encrypt_t)(uint8_t, uint8_t, uint8_t *, uint8_t *,
70  uint16_t, uint8_t *, uint16_t, uint8_t *,
71  uint8_t *, uint8_t);
73 
74 typedef int8_t(*aes_ccm_decrypt_t)(uint8_t, uint8_t, uint8_t *, uint8_t *,
75  uint16_t, uint8_t *, uint16_t, uint8_t *,
76  uint8_t *, uint8_t);
78 
79 //*****************************************************************************
80 // AES_CCM_EncryptData
81 //*****************************************************************************
82 int8_t AES_CCM_EncryptData(uint8_t encryptFlag, uint8_t MACLen, uint8_t *nonce,
83  uint8_t *plainText, uint16_t textLen,
84  uint8_t *addDataBuf, uint16_t addBufLen,
85  uint8_t *aesKey, uint8_t *MAC, uint8_t ccmLVal)
86 {
87  return aes_ccm_encrypt(encryptFlag, MACLen, nonce, plainText, textLen,
88  addDataBuf, addBufLen, aesKey, MAC, ccmLVal);
89 }
90 
91 //*****************************************************************************
92 // AES_CCM_DecryptData
93 //*****************************************************************************
94 int8_t AES_CCM_DecryptData(uint8_t decryptFlag, uint8_t MACLen, uint8_t *nonce,
95  uint8_t *cipherText, uint16_t textLen,
96  uint8_t *addDataBuf, uint16_t addBufLen,
97  uint8_t *aesKey, uint8_t *MAC, uint8_t ccmLVal)
98 {
99  return aes_ccm_decrypt(decryptFlag, MACLen, nonce, cipherText, textLen,
100  addDataBuf, addBufLen, aesKey, MAC, ccmLVal);
101 
102 }
103 
104 /* AES - CTR */
105 typedef uint8_t(*aes_ctr_encrypt_t)(uint8_t *, uint16_t, uint8_t *, uint8_t *,
106  uint8_t *);
108 
109 typedef uint8_t(*aes_ctr_decrypt_t)(uint8_t *, uint16_t, uint8_t *, uint8_t *,
110  uint8_t *);
112 
113 //*****************************************************************************
114 // AES_CTR_EncryptData
115 //*****************************************************************************
116 uint8_t AES_CTR_EncryptData(uint8_t *plainText, uint16_t textLen,
117  uint8_t *aesKey, uint8_t *nonce,
118  uint8_t *initVector)
119 {
120  return aes_ctr_encrypt(plainText, textLen, aesKey, nonce, initVector);
121 }
122 
123 //*****************************************************************************
124 // AES_CTR_DecryptData
125 //*****************************************************************************
126 uint8_t AES_CTR_DecryptData(uint8_t *cipherText, uint16_t textLen,
127  uint8_t *aesKey, uint8_t *nonce,
128  uint8_t *initVector)
129 {
130  return aes_ctr_decrypt(cipherText, textLen, aesKey, nonce, initVector);
131 }
132 
134 #ifdef ECC_PRIME_NIST256_CURVE
135 //#define TEST_NIST256
136 //#define PARAM_P NIST256_p;
137 #define PARAM_P 0x10018b0c;
138 
139 //#define PARAM_R NIST256_r;
140 #define PARAM_R 0x10018b30;
141 
142 //#define PARAM_A NIST256_a;
143 #define PARAM_A 0x10018b54;
144 
145 //#define PARAM_B NIST256_b;
146 #define PARAM_B 0x10018b78;
147 
148 //#define PARAM_GX NIST256_Gx;
149 #define PARAM_GX 0x10018b9c;
150 
151 //#define PARAM_GY NIST256_Gy;
152 #define PARAM_GY 0x10018bc0;
153 
154 #endif
155 
156 
157 //*****************************************************************************
158 // ECC_initialize
159 //*****************************************************************************
160 void ECC_initialize(uint32_t *pWorkzone)
161 {
162  // Initialize curve parameters
163  //data_p = (uint32_t *)PARAM_P;
164  *((uint32_t **)0x20004f48) = (uint32_t *)PARAM_P;
165 
166  //data_r = (uint32_t *)PARAM_R;
167  *((uint32_t **)0x20004f4c) = (uint32_t *)PARAM_R;
168 
169  //data_a = (uint32_t *)PARAM_A;
170  *((uint32_t **)0x20004f50) = (uint32_t *)PARAM_A;
171 
172  //data_b = (uint32_t *)PARAM_B;
173  *((uint32_t **)0x20004fa8) = (uint32_t *)PARAM_B;
174 
175  //data_Gx = (uint32_t *)PARAM_GX;
176  *((uint32_t **)0x20004fa0) = (uint32_t *)PARAM_GX;
177 
178  //data_Gy = (uint32_t *)PARAM_GY;
179  *((uint32_t **)0x20004fa4) = (uint32_t *)PARAM_GY;
180 
181  // Initialize window size
182  //win = (uint8_t) ECC_WINDOW_SIZE;
183  *((uint8_t *)0x20004f40) = (uint8_t) ECC_WINDOW_SIZE;
184 
185  // Initialize work zone
186  //workzone = (uint32_t *) pWorkzone;
187  *((uint32_t **)0x20004f44) = (uint32_t *) pWorkzone;
188 }
189 
190 typedef uint8_t(*ecc_keygen_t)(uint32_t *, uint32_t *,uint32_t *, uint32_t *);
192 
193 typedef uint8_t(*ecdsa_sign_t)(uint32_t *, uint32_t *,uint32_t *, uint32_t *, uint32_t *);
195 
196 typedef uint8_t(*ecdsa_verify_t)(uint32_t *, uint32_t *,uint32_t *, uint32_t *, uint32_t *);
198 
199 typedef uint8_t(*ecdh_computeSharedSecret_t)(uint32_t *, uint32_t *,uint32_t *, uint32_t *, uint32_t *);
201 
202 //*****************************************************************************
203 // ECC_generateKey
204 //*****************************************************************************
205 uint8_t ECC_generateKey(uint32_t *randString, uint32_t *privateKey,
206  uint32_t *publicKey_x, uint32_t *publicKey_y)
207 {
208  return (uint8_t)ecc_generatekey((uint32_t*)randString, (uint32_t*)privateKey,
209  (uint32_t*)publicKey_x, (uint32_t*)publicKey_y);
210 
211 }
212 
213 //*****************************************************************************
214 // ECC_ECDSA_sign
215 //*****************************************************************************
216 uint8_t ECC_ECDSA_sign(uint32_t *secretKey, uint32_t *text, uint32_t *randString,
217  uint32_t *sign1, uint32_t *sign2)
218 {
219  return (uint8_t)ecc_ecdsa_sign((uint32_t*)secretKey, (uint32_t*)text, (uint32_t*)randString,
220  (uint32_t*)sign1, (uint32_t*)sign2);
221 }
222 
223 //*****************************************************************************
224 // ECC_ECDSA_verify
225 //*****************************************************************************
226 uint8_t ECC_ECDSA_verify(uint32_t *publicKey_x, uint32_t *publicKey_y,
227  uint32_t *text, uint32_t *sign1, uint32_t *sign2)
228 {
229  return (uint8_t)ecc_ecdsa_verify((uint32_t*)publicKey_x, (uint32_t*)publicKey_y, (uint32_t*)text,
230  (uint32_t*)sign1, (uint32_t*)sign2);
231 }
232 
233 //*****************************************************************************
234 // ECC_ECDH_computeSharedSecret
235 //*****************************************************************************
236 uint8_t ECC_ECDH_computeSharedSecret(uint32_t *privateKey, uint32_t *publicKey_x,
237  uint32_t *publicKey_y, uint32_t *sharedSecret_x,
238  uint32_t *sharedSecret_y)
239 {
240  return (uint8_t)ecdh_computeSharedSecret((uint32_t*)privateKey, (uint32_t*)publicKey_x,
241  (uint32_t*)publicKey_y, (uint32_t*)sharedSecret_x,
242  (uint32_t*)sharedSecret_y);
243 }
244 
245 
247 
248 typedef uint8_t(*sha256_full_t)(SHA256_memory_t *, uint8_t *, uint8_t *, uint32_t);
250 
251 typedef uint8_t(*sha256_init_t)(SHA256_memory_t *);
253 
254 typedef uint8_t(*sha256_process_t)(SHA256_memory_t *, uint8_t *, uint32_t);
256 
257 typedef uint8_t(*sha256_final_t)(SHA256_memory_t *, uint8_t *);
259 
260 //*****************************************************************************
261 // SHA256_runFullAlgorithm
262 //*****************************************************************************
263 uint8_t SHA256_runFullAlgorithm(SHA256_memory_t *memory, uint8_t *pBufIn,
264  uint32_t bufLen, uint8_t *pBufOut)
265 {
266  return (uint8_t)sha256_runfullalg(memory, pBufOut, pBufIn, bufLen);
267 }
268 
269 //*****************************************************************************
270 // SHA256_initialize
271 //*****************************************************************************
272 uint8_t SHA256_initialize(SHA256_memory_t *memory)
273 {
274  return (uint8_t)sha256_initialize(memory);
275 }
276 
277 //*****************************************************************************
278 // SHA256_execute
279 //*****************************************************************************
280 uint8_t SHA256_execute(SHA256_memory_t *memory, uint8_t *pBufIn, uint32_t bufLen)
281 {
282  return (uint8_t)sha256_execute(memory,pBufIn, bufLen);
283 }
284 
285 //*****************************************************************************
286 // SHA256_output
287 //*****************************************************************************
288 uint8_t SHA256_output(SHA256_memory_t *memory, uint8_t *pBufOut)
289 {
290  return (uint8_t)sha256_output(memory, pBufOut);
291 }
uint8_t SHA256_initialize(SHA256_memory_t *memory)
Intializes the SHA256 engine.
Definition: rom_crypto.c:272
uint8_t(* aes_ctr_decrypt_t)(uint8_t *, uint16_t, uint8_t *, uint8_t *, uint8_t *)
Definition: rom_crypto.c:109
aes_ecb_encrypt_t aes_ecb_encrypt
Definition: rom_crypto.c:47
aes_ccm_decrypt_t aes_ccm_decrypt
Definition: rom_crypto.c:77
A SHA256_memory_t variable of this type must be allocated before running any SHA256 functions...
Definition: rom_crypto.h:317
uint8_t(* ecc_keygen_t)(uint32_t *, uint32_t *, uint32_t *, uint32_t *)
Definition: rom_crypto.c:190
uint8_t AES_CTR_DecryptData(uint8_t *cipherText, uint16_t textLen, uint8_t *aesKey, uint8_t *nonce, uint8_t *initVector)
Decrypt ciphertext using the AES key, nonce and initialization vector.
Definition: rom_crypto.c:126
int8_t(* aes_ccm_encrypt_t)(uint8_t, uint8_t, uint8_t *, uint8_t *, uint16_t, uint8_t *, uint16_t, uint8_t *, uint8_t *, uint8_t)
Definition: rom_crypto.c:69
uint8_t(* ecdsa_sign_t)(uint32_t *, uint32_t *, uint32_t *, uint32_t *, uint32_t *)
Definition: rom_crypto.c:193
ecdsa_verify_t ecc_ecdsa_verify
Definition: rom_crypto.c:197
uint8_t(* sha256_init_t)(SHA256_memory_t *)
Definition: rom_crypto.c:251
uint8_t AES_CTR_EncryptData(uint8_t *plainText, uint16_t textLen, uint8_t *aesKey, uint8_t *nonce, uint8_t *initVector)
Encrypt plaintext using the AES key, nonce and initialization vector.
Definition: rom_crypto.c:116
uint8_t(* sha256_final_t)(SHA256_memory_t *, uint8_t *)
Definition: rom_crypto.c:257
uint8_t(* ecdh_computeSharedSecret_t)(uint32_t *, uint32_t *, uint32_t *, uint32_t *, uint32_t *)
Definition: rom_crypto.c:199
void(* aes_ecb_decrypt_t)(uint8_t *, uint16_t, uint8_t *)
Definition: rom_crypto.c:49
int8_t AES_CCM_DecryptData(uint8_t decryptFlag, uint8_t MACLen, uint8_t *nonce, uint8_t *cipherText, uint16_t textLen, uint8_t *addDataBuf, uint16_t addBufLen, uint8_t *aesKey, uint8_t *MAC, uint8_t ccmLVal)
Authenticate and optionally decrypt message cipherText.
Definition: rom_crypto.c:94
sha256_process_t sha256_execute
Definition: rom_crypto.c:255
uint8_t ECC_generateKey(uint32_t *randString, uint32_t *privateKey, uint32_t *publicKey_x, uint32_t *publicKey_y)
Generate a key.
Definition: rom_crypto.c:205
uint8_t SHA256_execute(SHA256_memory_t *memory, uint8_t *pBufIn, uint32_t bufLen)
Perform SHA256.
Definition: rom_crypto.c:280
uint8_t(* sha256_full_t)(SHA256_memory_t *, uint8_t *, uint8_t *, uint32_t)
Definition: rom_crypto.c:248
void AES_ECB_DecryptData(uint8_t *text, uint16_t textLen, uint8_t *aesKey)
Use a random 128 bit key to decrypt data with the AES.
Definition: rom_crypto.c:63
aes_ctr_encrypt_t aes_ctr_encrypt
Definition: rom_crypto.c:107
ecc_keygen_t ecc_generatekey
Definition: rom_crypto.c:191
int8_t AES_CCM_EncryptData(uint8_t encryptFlag, uint8_t MACLen, uint8_t *nonce, uint8_t *plainText, uint16_t textLen, uint8_t *addDataBuf, uint16_t addBufLen, uint8_t *aesKey, uint8_t *MAC, uint8_t ccmLVal)
Authenticate and optionally encrypt message plainText.
Definition: rom_crypto.c:82
uint8_t ECC_ECDSA_sign(uint32_t *secretKey, uint32_t *text, uint32_t *randString, uint32_t *sign1, uint32_t *sign2)
Sign data.
Definition: rom_crypto.c:216
void AES_ECB_EncryptData(uint8_t *text, uint16_t textLen, uint8_t *aesKey)
Use a random 128 bit key to encrypt data with the AES.
Definition: rom_crypto.c:55
sha256_init_t sha256_initialize
Definition: rom_crypto.c:252
aes_ccm_encrypt_t aes_ccm_encrypt
Definition: rom_crypto.c:72
aes_ctr_decrypt_t aes_ctr_decrypt
Definition: rom_crypto.c:111
aes_ecb_decrypt_t aes_ecb_decrypt
Definition: rom_crypto.c:50
ecdh_computeSharedSecret_t ecdh_computeSharedSecret
Definition: rom_crypto.c:200
uint8_t(* sha256_process_t)(SHA256_memory_t *, uint8_t *, uint32_t)
Definition: rom_crypto.c:254
sha256_full_t sha256_runfullalg
Definition: rom_crypto.c:249
void(* aes_ecb_encrypt_t)(uint8_t *, uint16_t, uint8_t *)
Definition: rom_crypto.c:46
uint8_t ECC_ECDH_computeSharedSecret(uint32_t *privateKey, uint32_t *publicKey_x, uint32_t *publicKey_y, uint32_t *sharedSecret_x, uint32_t *sharedSecret_y)
Compute the shared secret.
Definition: rom_crypto.c:236
ecdsa_sign_t ecc_ecdsa_sign
Definition: rom_crypto.c:194
uint8_t SHA256_output(SHA256_memory_t *memory, uint8_t *pBufOut)
Complete the process by passing the modified data back.
Definition: rom_crypto.c:288
void ECC_initialize(uint32_t *pWorkzone)
Pass pointer to ECC memory allocation to ECC engine.
Definition: rom_crypto.c:160
sha256_final_t sha256_output
Definition: rom_crypto.c:258
uint8_t SHA256_runFullAlgorithm(SHA256_memory_t *memory, uint8_t *pBufIn, uint32_t bufLen, uint8_t *pBufOut)
Perform SHA256 on the the input data.
Definition: rom_crypto.c:263
uint8_t ECC_ECDSA_verify(uint32_t *publicKey_x, uint32_t *publicKey_y, uint32_t *text, uint32_t *sign1, uint32_t *sign2)
Verify signature.
Definition: rom_crypto.c:226
#define ECC_WINDOW_SIZE
Definition: rom_crypto.h:179
uint8_t(* aes_ctr_encrypt_t)(uint8_t *, uint16_t, uint8_t *, uint8_t *, uint8_t *)
Definition: rom_crypto.c:105
int8_t(* aes_ccm_decrypt_t)(uint8_t, uint8_t, uint8_t *, uint8_t *, uint16_t, uint8_t *, uint16_t, uint8_t *, uint8_t *, uint8_t)
Definition: rom_crypto.c:74
uint8_t(* ecdsa_verify_t)(uint32_t *, uint32_t *, uint32_t *, uint32_t *, uint32_t *)
Definition: rom_crypto.c:196