CC13xx Driver Library
rom_crypto.c
Go to the documentation of this file.
1 /*******************************************************************************
2 * Filename: rom_crypto.c
3 * Revised: 2015-09-09 11:55:59 +0200 (Wed, 09 Sep 2015)
4 * Revision: 44536
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
56 AES_ECB_EncryptData(uint8_t *text, uint16_t textLen, uint8_t *aesKey)
57 {
58  aes_ecb_encrypt(text, textLen, aesKey);
59 }
60 
61 //*****************************************************************************
62 // AES_ECB_DecryptData
63 //*****************************************************************************
64 void
65 AES_ECB_DecryptData(uint8_t *text, uint16_t textLen, uint8_t *aesKey)
66 {
67  aes_ecb_decrypt(text, textLen, aesKey);
68 }
69 
70 /* AES - CCM */
71 typedef int8_t(*aes_ccm_encrypt_t)(uint8_t, uint8_t, uint8_t *, uint8_t *,
72  uint16_t, uint8_t *, uint16_t, uint8_t *,
73  uint8_t *, uint8_t);
75 
76 typedef int8_t(*aes_ccm_decrypt_t)(uint8_t, uint8_t, uint8_t *, uint8_t *,
77  uint16_t, uint8_t *, uint16_t, uint8_t *,
78  uint8_t *, uint8_t);
80 
81 //*****************************************************************************
82 // AES_CCM_EncryptData
83 //*****************************************************************************
84 int8_t
85 AES_CCM_EncryptData(uint8_t encryptFlag, uint8_t MACLen, uint8_t *nonce,
86  uint8_t *plainText, uint16_t textLen,
87  uint8_t *addDataBuf, uint16_t addBufLen,
88  uint8_t *aesKey, uint8_t *MAC, uint8_t ccmLVal)
89 {
90  return aes_ccm_encrypt(encryptFlag, MACLen, nonce, plainText, textLen,
91  addDataBuf, addBufLen, aesKey, MAC, ccmLVal);
92 }
93 
94 //*****************************************************************************
95 // AES_CCM_DecryptData
96 //*****************************************************************************
97 int8_t
98 AES_CCM_DecryptData(uint8_t decryptFlag, uint8_t MACLen, uint8_t *nonce,
99  uint8_t *cipherText, uint16_t textLen,
100  uint8_t *addDataBuf, uint16_t addBufLen,
101  uint8_t *aesKey, uint8_t *MAC, uint8_t ccmLVal)
102 {
103  return aes_ccm_decrypt(decryptFlag, MACLen, nonce, cipherText, textLen,
104  addDataBuf, addBufLen, aesKey, MAC, ccmLVal);
105 
106 }
107 
108 /* AES - CTR */
109 typedef uint8_t(*aes_ctr_encrypt_t)(uint8_t *, uint16_t, uint8_t *, uint8_t *,
110  uint8_t *);
112 
113 typedef uint8_t(*aes_ctr_decrypt_t)(uint8_t *, uint16_t, uint8_t *, uint8_t *,
114  uint8_t *);
116 
117 //*****************************************************************************
118 // AES_CTR_EncryptData
119 //*****************************************************************************
120 uint8_t
121 AES_CTR_EncryptData(uint8_t *plainText, uint16_t textLen,
122  uint8_t *aesKey, uint8_t *nonce,
123  uint8_t *initVector)
124 {
125  return aes_ctr_encrypt(plainText, textLen, aesKey, nonce, initVector);
126 }
127 
128 //*****************************************************************************
129 // AES_CTR_DecryptData
130 //*****************************************************************************
131 uint8_t
132 AES_CTR_DecryptData(uint8_t *cipherText, uint16_t textLen,
133  uint8_t *aesKey, uint8_t *nonce,
134  uint8_t *initVector)
135 {
136  return aes_ctr_decrypt(cipherText, textLen, aesKey, nonce, initVector);
137 }
138 
140 #ifdef ECC_PRIME_NIST256_CURVE
141 //#define TEST_NIST256
142 //#define PARAM_P NIST256_p;
143 #define PARAM_P 0x10018b0c;
144 
145 //#define PARAM_R NIST256_r;
146 #define PARAM_R 0x10018b30;
147 
148 //#define PARAM_A NIST256_a;
149 #define PARAM_A 0x10018b54;
150 
151 //#define PARAM_B NIST256_b;
152 #define PARAM_B 0x10018b78;
153 
154 //#define PARAM_GX NIST256_Gx;
155 #define PARAM_GX 0x10018b9c;
156 
157 //#define PARAM_GY NIST256_Gy;
158 #define PARAM_GY 0x10018bc0;
159 
160 #endif
161 
162 
163 //*****************************************************************************
164 // ECC_initialize
165 //*****************************************************************************
166 void
167 ECC_initialize(uint32_t *pWorkzone)
168 {
169  // Initialize curve parameters
170  //data_p = (uint32_t *)PARAM_P;
171  *((uint32_t **)0x20004f48) = (uint32_t *)PARAM_P;
172 
173  //data_r = (uint32_t *)PARAM_R;
174  *((uint32_t **)0x20004f4c) = (uint32_t *)PARAM_R;
175 
176  //data_a = (uint32_t *)PARAM_A;
177  *((uint32_t **)0x20004f50) = (uint32_t *)PARAM_A;
178 
179  //data_b = (uint32_t *)PARAM_B;
180  *((uint32_t **)0x20004fa8) = (uint32_t *)PARAM_B;
181 
182  //data_Gx = (uint32_t *)PARAM_GX;
183  *((uint32_t **)0x20004fa0) = (uint32_t *)PARAM_GX;
184 
185  //data_Gy = (uint32_t *)PARAM_GY;
186  *((uint32_t **)0x20004fa4) = (uint32_t *)PARAM_GY;
187 
188  // Initialize window size
189  //win = (uint8_t) ECC_WINDOW_SIZE;
190  *((uint8_t *)0x20004f40) = (uint8_t) ECC_WINDOW_SIZE;
191 
192  // Initialize work zone
193  //workzone = (uint32_t *) pWorkzone;
194  *((uint32_t **)0x20004f44) = (uint32_t *) pWorkzone;
195 }
196 
197 typedef uint8_t(*ecc_keygen_t)(uint32_t *, uint32_t *,uint32_t *, uint32_t *);
199 
200 typedef uint8_t(*ecdsa_sign_t)(uint32_t *, uint32_t *,uint32_t *, uint32_t *, uint32_t *);
202 
203 typedef uint8_t(*ecdsa_verify_t)(uint32_t *, uint32_t *,uint32_t *, uint32_t *, uint32_t *);
205 
206 typedef uint8_t(*ecdh_computeSharedSecret_t)(uint32_t *, uint32_t *,uint32_t *, uint32_t *, uint32_t *);
208 
209 //*****************************************************************************
210 // ECC_generateKey
211 //*****************************************************************************
212 uint8_t
213 ECC_generateKey(uint32_t *randString, uint32_t *privateKey,
214  uint32_t *publicKey_x, uint32_t *publicKey_y)
215 {
216  return (uint8_t)ecc_generatekey((uint32_t*)randString, (uint32_t*)privateKey,
217  (uint32_t*)publicKey_x, (uint32_t*)publicKey_y);
218 
219 }
220 
221 //*****************************************************************************
222 // ECC_ECDSA_sign
223 //*****************************************************************************
224 uint8_t
225 ECC_ECDSA_sign(uint32_t *secretKey, uint32_t *text, uint32_t *randString,
226  uint32_t *sign1, uint32_t *sign2)
227 {
228  return (uint8_t)ecc_ecdsa_sign((uint32_t*)secretKey, (uint32_t*)text, (uint32_t*)randString,
229  (uint32_t*)sign1, (uint32_t*)sign2);
230 }
231 
232 //*****************************************************************************
233 // ECC_ECDSA_verify
234 //*****************************************************************************
235 uint8_t
236 ECC_ECDSA_verify(uint32_t *publicKey_x, uint32_t *publicKey_y,
237  uint32_t *text, uint32_t *sign1, uint32_t *sign2)
238 {
239  return (uint8_t)ecc_ecdsa_verify((uint32_t*)publicKey_x, (uint32_t*)publicKey_y, (uint32_t*)text,
240  (uint32_t*)sign1, (uint32_t*)sign2);
241 }
242 
243 //*****************************************************************************
244 // ECC_ECDH_computeSharedSecret
245 //*****************************************************************************
246 uint8_t
247 ECC_ECDH_computeSharedSecret(uint32_t *privateKey, uint32_t *publicKey_x,
248  uint32_t *publicKey_y, uint32_t *sharedSecret_x,
249  uint32_t *sharedSecret_y)
250 {
251  return (uint8_t)ecdh_computeSharedSecret((uint32_t*)privateKey, (uint32_t*)publicKey_x,
252  (uint32_t*)publicKey_y, (uint32_t*)sharedSecret_x,
253  (uint32_t*)sharedSecret_y);
254 }
255 
256 
258 
259 typedef uint8_t(*sha256_full_t)(SHA256_memory_t *, uint8_t *, uint8_t *, uint32_t);
261 
262 typedef uint8_t(*sha256_init_t)(SHA256_memory_t *);
264 
265 typedef uint8_t(*sha256_process_t)(SHA256_memory_t *, uint8_t *, uint32_t);
267 
268 typedef uint8_t(*sha256_final_t)(SHA256_memory_t *, uint8_t *);
270 
271 //*****************************************************************************
272 // SHA256_runFullAlgorithm
273 //*****************************************************************************
274 uint8_t
275 SHA256_runFullAlgorithm(SHA256_memory_t *memory, uint8_t *pBufIn,
276  uint32_t bufLen, uint8_t *pBufOut)
277 {
278  return (uint8_t)sha256_runfullalg(memory, pBufOut, pBufIn, bufLen);
279 }
280 
281 //*****************************************************************************
282 // SHA256_initialize
283 //*****************************************************************************
284 uint8_t
285 SHA256_initialize(SHA256_memory_t *memory)
286 {
287  return (uint8_t)sha256_initialize(memory);
288 }
289 
290 //*****************************************************************************
291 // SHA256_execute
292 //*****************************************************************************
293 uint8_t
294 SHA256_execute(SHA256_memory_t *memory, uint8_t *pBufIn, uint32_t bufLen)
295 {
296  return (uint8_t)sha256_execute(memory,pBufIn, bufLen);
297 }
298 
299 //*****************************************************************************
300 // SHA256_output
301 //*****************************************************************************
302 uint8_t
303 SHA256_output(SHA256_memory_t *memory, uint8_t *pBufOut)
304 {
305  return (uint8_t)sha256_output(memory, pBufOut);
306 }
uint8_t SHA256_initialize(SHA256_memory_t *memory)
Intializes the SHA256 engine.
Definition: rom_crypto.c:285
uint8_t(* aes_ctr_decrypt_t)(uint8_t *, uint16_t, uint8_t *, uint8_t *, uint8_t *)
Definition: rom_crypto.c:113
aes_ecb_encrypt_t aes_ecb_encrypt
Definition: rom_crypto.c:47
aes_ccm_decrypt_t aes_ccm_decrypt
Definition: rom_crypto.c:79
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:197
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:132
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:71
uint8_t(* ecdsa_sign_t)(uint32_t *, uint32_t *, uint32_t *, uint32_t *, uint32_t *)
Definition: rom_crypto.c:200
ecdsa_verify_t ecc_ecdsa_verify
Definition: rom_crypto.c:204
uint8_t(* sha256_init_t)(SHA256_memory_t *)
Definition: rom_crypto.c:262
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:121
uint8_t(* sha256_final_t)(SHA256_memory_t *, uint8_t *)
Definition: rom_crypto.c:268
uint8_t(* ecdh_computeSharedSecret_t)(uint32_t *, uint32_t *, uint32_t *, uint32_t *, uint32_t *)
Definition: rom_crypto.c:206
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:98
sha256_process_t sha256_execute
Definition: rom_crypto.c:266
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:213
uint8_t SHA256_execute(SHA256_memory_t *memory, uint8_t *pBufIn, uint32_t bufLen)
Perform SHA256.
Definition: rom_crypto.c:294
uint8_t(* sha256_full_t)(SHA256_memory_t *, uint8_t *, uint8_t *, uint32_t)
Definition: rom_crypto.c:259
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:65
aes_ctr_encrypt_t aes_ctr_encrypt
Definition: rom_crypto.c:111
ecc_keygen_t ecc_generatekey
Definition: rom_crypto.c:198
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:85
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:225
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:56
sha256_init_t sha256_initialize
Definition: rom_crypto.c:263
aes_ccm_encrypt_t aes_ccm_encrypt
Definition: rom_crypto.c:74
aes_ctr_decrypt_t aes_ctr_decrypt
Definition: rom_crypto.c:115
aes_ecb_decrypt_t aes_ecb_decrypt
Definition: rom_crypto.c:50
ecdh_computeSharedSecret_t ecdh_computeSharedSecret
Definition: rom_crypto.c:207
uint8_t(* sha256_process_t)(SHA256_memory_t *, uint8_t *, uint32_t)
Definition: rom_crypto.c:265
sha256_full_t sha256_runfullalg
Definition: rom_crypto.c:260
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:247
ecdsa_sign_t ecc_ecdsa_sign
Definition: rom_crypto.c:201
uint8_t SHA256_output(SHA256_memory_t *memory, uint8_t *pBufOut)
Complete the process by passing the modified data back.
Definition: rom_crypto.c:303
void ECC_initialize(uint32_t *pWorkzone)
Pass pointer to ECC memory allocation to ECC engine.
Definition: rom_crypto.c:167
sha256_final_t sha256_output
Definition: rom_crypto.c:269
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:275
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:236
#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:109
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:76
uint8_t(* ecdsa_verify_t)(uint32_t *, uint32_t *, uint32_t *, uint32_t *, uint32_t *)
Definition: rom_crypto.c:203