1 /*******************************************************************************
2 * Filename: rom_crypto.c
3 * Revised: 2020-09-17 15:26:49 +0200 (Thu, 17 Sep 2020)
4 * Revision: 58682
5 *
6 * Description: This is the implementation for the API to the ECC functions
7 * built into ROM on the CC13x2/CC26x2.
8 *
9 * Copyright (c) 2015 - 2020, 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 "rom_crypto.h"
42
43
44 ////////////////////////////////////* ECC *////////////////////////////////////
45
46 //*****************************************************************************
47 // ECC_initialize
48 //*****************************************************************************
49 void
ECC_initialize(uint32_t * pWorkzone)50 ECC_initialize(uint32_t *pWorkzone)
51 {
52 // Initialize curve parameters
53 //data_p = ECC_NISTP256_prime;
54 *((const uint32_t **)0x20000138) = ECC_NISTP256_prime;
55
56 //data_r = ECC_NISTP256_order;
57 *((const uint32_t **)0x2000013c) = ECC_NISTP256_order;
58
59 //data_a = ECC_NISTP256_a;
60 *((const uint32_t **)0x20000140) = ECC_NISTP256_a;
61
62 //data_b = ECC_NISTP256_b;
63 *((const uint32_t **)0x20000144) = ECC_NISTP256_b;
64
65 //data_Gx = ECC_NISTP256_generatorX;
66 *((const uint32_t **)0x2000012c) = ECC_NISTP256_generatorX;
67
68 //data_Gy = ECC_NISTP256_generatorY;
69 *((const uint32_t **)0x20000130) = ECC_NISTP256_generatorY;
70
71 // Initialize window size
72 //win = (uint8_t) ECC_WINDOW_SIZE;
73 *((uint8_t *)0x20000148) = (uint8_t) ECC_WINDOW_SIZE;
74
75 // Initialize work zone
76 //workzone = (uint32_t *) pWorkzone;
77 *((uint32_t **)0x20000134) = (uint32_t *) pWorkzone;
78 }
79
80 //*****************************************************************************
81 // ECC_init
82 //*****************************************************************************
83 void
ECC_init(uint32_t * workzone,uint8_t windowSize,const uint32_t * prime,const uint32_t * order,const uint32_t * a,const uint32_t * b,const uint32_t * generatorX,const uint32_t * generatorY)84 ECC_init(uint32_t *workzone,
85 uint8_t windowSize,
86 const uint32_t *prime,
87 const uint32_t *order,
88 const uint32_t *a,
89 const uint32_t *b,
90 const uint32_t *generatorX,
91 const uint32_t *generatorY)
92 {
93 // Initialize curve parameters
94 //data_p = (uint32_t *)PARAM_P;
95 *((const uint32_t **)0x20000138) = prime;
96
97 //data_r = (uint32_t *)PARAM_R;
98 *((const uint32_t **)0x2000013c) = order;
99
100 //data_a = (uint32_t *)PARAM_A;
101 *((const uint32_t **)0x20000140) = a;
102
103 //data_b = (uint32_t *)PARAM_B;
104 *((const uint32_t **)0x20000144) = b;
105
106 //data_Gx = (uint32_t *)PARAM_GX;
107 *((const uint32_t **)0x2000012c) = generatorX;
108
109 //data_Gy = (uint32_t *)PARAM_GY;
110 *((const uint32_t **)0x20000130) = generatorY;
111
112 // Initialize window size
113 //win = (uint8_t) windowSize;
114 *((uint8_t *)0x20000148) = windowSize;
115
116 // Initialize work zone
117 //workzone = (uint32_t *) pWorkzone;
118 *((uint32_t **)0x20000134) = workzone;
119 }
120
121 typedef uint8_t(*ecc_keygen_t)(uint32_t *, uint32_t *,uint32_t *, uint32_t *);
122 ecc_keygen_t ecc_generatekey = (ecc_keygen_t)(0x1001f94d);
123
124 typedef uint8_t(*ecdsa_sign_t)(uint32_t *, uint32_t *,uint32_t *, uint32_t *, uint32_t *);
125 ecdsa_sign_t ecc_ecdsa_sign = (ecdsa_sign_t)(0x10010381);
126
127 typedef uint8_t(*ecdsa_verify_t)(uint32_t *, uint32_t *,uint32_t *, uint32_t *, uint32_t *);
128 ecdsa_verify_t ecc_ecdsa_verify = (ecdsa_verify_t)(0x1000c805);
129
130 typedef uint8_t(*ecdh_computeSharedSecret_t)(uint32_t *, uint32_t *,uint32_t *, uint32_t *, uint32_t *);
131 ecdh_computeSharedSecret_t ecdh_computeSharedSecret = (ecdh_computeSharedSecret_t)(0x10023485);
132
133 //*****************************************************************************
134 // ECC_generateKey
135 //*****************************************************************************
136 uint8_t
ECC_generateKey(uint32_t * randString,uint32_t * privateKey,uint32_t * publicKey_x,uint32_t * publicKey_y)137 ECC_generateKey(uint32_t *randString, uint32_t *privateKey,
138 uint32_t *publicKey_x, uint32_t *publicKey_y)
139 {
140 return (uint8_t)ecc_generatekey((uint32_t*)randString, (uint32_t*)privateKey,
141 (uint32_t*)publicKey_x, (uint32_t*)publicKey_y);
142
143 }
144
145 //*****************************************************************************
146 // ECC_ECDSA_sign
147 //*****************************************************************************
148 uint8_t
ECC_ECDSA_sign(uint32_t * secretKey,uint32_t * text,uint32_t * randString,uint32_t * sign1,uint32_t * sign2)149 ECC_ECDSA_sign(uint32_t *secretKey, uint32_t *text, uint32_t *randString,
150 uint32_t *sign1, uint32_t *sign2)
151 {
152 return (uint8_t)ecc_ecdsa_sign((uint32_t*)secretKey, (uint32_t*)text, (uint32_t*)randString,
153 (uint32_t*)sign1, (uint32_t*)sign2);
154 }
155
156 //*****************************************************************************
157 // ECC_ECDSA_verify
158 //*****************************************************************************
159 uint8_t
ECC_ECDSA_verify(uint32_t * publicKey_x,uint32_t * publicKey_y,uint32_t * text,uint32_t * sign1,uint32_t * sign2)160 ECC_ECDSA_verify(uint32_t *publicKey_x, uint32_t *publicKey_y,
161 uint32_t *text, uint32_t *sign1, uint32_t *sign2)
162 {
163 return (uint8_t)ecc_ecdsa_verify((uint32_t*)publicKey_x, (uint32_t*)publicKey_y, (uint32_t*)text,
164 (uint32_t*)sign1, (uint32_t*)sign2);
165 }
166
167 //*****************************************************************************
168 // ECC_ECDH_computeSharedSecret
169 //*****************************************************************************
170 uint8_t
ECC_ECDH_computeSharedSecret(uint32_t * privateKey,uint32_t * publicKey_x,uint32_t * publicKey_y,uint32_t * sharedSecret_x,uint32_t * sharedSecret_y)171 ECC_ECDH_computeSharedSecret(uint32_t *privateKey, uint32_t *publicKey_x,
172 uint32_t *publicKey_y, uint32_t *sharedSecret_x,
173 uint32_t *sharedSecret_y)
174 {
175 return (uint8_t)ecdh_computeSharedSecret((uint32_t*)privateKey, (uint32_t*)publicKey_x,
176 (uint32_t*)publicKey_y, (uint32_t*)sharedSecret_x,
177 (uint32_t*)sharedSecret_y);
178 }
179