1 /*******************************************************************************
2 *  Filename:       rom_crypto.c
3 *
4 *  Description:    This is the implementation for the API to the ECC functions
5 *                  built into ROM on the CC13x2/CC26x2.
6 *
7 *  Copyright (c) 2015 - 2022, Texas Instruments Incorporated
8 *  All rights reserved.
9 *
10 *  Redistribution and use in source and binary forms, with or without
11 *  modification, are permitted provided that the following conditions are met:
12 *
13 *  1) Redistributions of source code must retain the above copyright notice,
14 *     this list of conditions and the following disclaimer.
15 *
16 *  2) Redistributions in binary form must reproduce the above copyright notice,
17 *     this list of conditions and the following disclaimer in the documentation
18 *     and/or other materials provided with the distribution.
19 *
20 *  3) Neither the name of the ORGANIZATION nor the names of its contributors may
21 *     be used to endorse or promote products derived from this software without
22 *     specific prior written permission.
23 *
24 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
28 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 *  POSSIBILITY OF SUCH DAMAGE.
35 *
36 *******************************************************************************/
37 
38 #include <stdint.h>
39 #include "rom_crypto.h"
40 
41 
42 ////////////////////////////////////* ECC *////////////////////////////////////
43 
44 //*****************************************************************************
45 // ECC_initialize
46 //*****************************************************************************
47 void
ECC_initialize(uint32_t * pWorkzone)48 ECC_initialize(uint32_t *pWorkzone)
49 {
50   // Initialize curve parameters
51   //data_p  = ECC_NISTP256_prime;
52   *((const uint32_t **)0x20000138) = ECC_NISTP256_prime;
53 
54   //data_r  = ECC_NISTP256_order;
55   *((const uint32_t **)0x2000013c) = ECC_NISTP256_order;
56 
57   //data_a  = ECC_NISTP256_a;
58   *((const uint32_t **)0x20000140) = ECC_NISTP256_a;
59 
60   //data_b  = ECC_NISTP256_b;
61   *((const uint32_t **)0x20000144) = ECC_NISTP256_b;
62 
63   //data_Gx = ECC_NISTP256_generatorX;
64   *((const uint32_t **)0x2000012c) = ECC_NISTP256_generatorX;
65 
66   //data_Gy = ECC_NISTP256_generatorY;
67   *((const uint32_t **)0x20000130) = ECC_NISTP256_generatorY;
68 
69   // Initialize window size
70   //win = (uint8_t) ECC_WINDOW_SIZE;
71   *((uint8_t *)0x20000148) = (uint8_t) ECC_WINDOW_SIZE;
72 
73   // Initialize work zone
74   //workzone = (uint32_t *) pWorkzone;
75   *((uint32_t **)0x20000134) = (uint32_t *) pWorkzone;
76 }
77 
78 //*****************************************************************************
79 // ECC_init
80 //*****************************************************************************
81 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)82 ECC_init(uint32_t *workzone,
83          uint8_t   windowSize,
84          const uint32_t *prime,
85          const uint32_t *order,
86          const uint32_t *a,
87          const uint32_t *b,
88          const uint32_t *generatorX,
89          const uint32_t *generatorY)
90 {
91   // Initialize curve parameters
92   //data_p  = (uint32_t *)PARAM_P;
93   *((const uint32_t **)0x20000138) = prime;
94 
95   //data_r  = (uint32_t *)PARAM_R;
96   *((const uint32_t **)0x2000013c) = order;
97 
98   //data_a  = (uint32_t *)PARAM_A;
99   *((const uint32_t **)0x20000140) = a;
100 
101   //data_b  = (uint32_t *)PARAM_B;
102   *((const uint32_t **)0x20000144) = b;
103 
104   //data_Gx = (uint32_t *)PARAM_GX;
105   *((const uint32_t **)0x2000012c) = generatorX;
106 
107   //data_Gy = (uint32_t *)PARAM_GY;
108   *((const uint32_t **)0x20000130) = generatorY;
109 
110   // Initialize window size
111   //win = (uint8_t) windowSize;
112   *((uint8_t *)0x20000148) = windowSize;
113 
114   // Initialize work zone
115   //workzone = (uint32_t *) pWorkzone;
116   *((uint32_t **)0x20000134) = workzone;
117 }
118 
119 typedef uint8_t(*ecc_keygen_t)(uint32_t *, uint32_t *,uint32_t *, uint32_t *);
120 ecc_keygen_t ecc_generatekey = (ecc_keygen_t)(0x1001f94d);
121 
122 typedef uint8_t(*ecdsa_sign_t)(uint32_t *, uint32_t *,uint32_t *, uint32_t *, uint32_t *);
123 ecdsa_sign_t ecc_ecdsa_sign = (ecdsa_sign_t)(0x10010381);
124 
125 typedef uint8_t(*ecdsa_verify_t)(uint32_t *, uint32_t *,uint32_t *, uint32_t *, uint32_t *);
126 ecdsa_verify_t ecc_ecdsa_verify = (ecdsa_verify_t)(0x1000c805);
127 
128 typedef uint8_t(*ecdh_computeSharedSecret_t)(uint32_t *, uint32_t *,uint32_t *, uint32_t *, uint32_t *);
129 ecdh_computeSharedSecret_t ecdh_computeSharedSecret = (ecdh_computeSharedSecret_t)(0x10023485);
130 
131 //*****************************************************************************
132 // ECC_generateKey
133 //*****************************************************************************
134 uint8_t
ECC_generateKey(uint32_t * randString,uint32_t * privateKey,uint32_t * publicKey_x,uint32_t * publicKey_y)135 ECC_generateKey(uint32_t *randString, uint32_t *privateKey,
136                 uint32_t *publicKey_x, uint32_t *publicKey_y)
137 {
138   return (uint8_t)ecc_generatekey((uint32_t*)randString, (uint32_t*)privateKey,
139                                   (uint32_t*)publicKey_x, (uint32_t*)publicKey_y);
140 
141 }
142 
143 //*****************************************************************************
144 // ECC_ECDSA_sign
145 //*****************************************************************************
146 uint8_t
ECC_ECDSA_sign(uint32_t * secretKey,uint32_t * text,uint32_t * randString,uint32_t * sign1,uint32_t * sign2)147 ECC_ECDSA_sign(uint32_t *secretKey, uint32_t *text, uint32_t *randString,
148                uint32_t *sign1, uint32_t *sign2)
149 {
150   return (uint8_t)ecc_ecdsa_sign((uint32_t*)secretKey, (uint32_t*)text, (uint32_t*)randString,
151                              (uint32_t*)sign1, (uint32_t*)sign2);
152 }
153 
154 //*****************************************************************************
155 // ECC_ECDSA_verify
156 //*****************************************************************************
157 uint8_t
ECC_ECDSA_verify(uint32_t * publicKey_x,uint32_t * publicKey_y,uint32_t * text,uint32_t * sign1,uint32_t * sign2)158 ECC_ECDSA_verify(uint32_t *publicKey_x, uint32_t *publicKey_y,
159                  uint32_t *text, uint32_t *sign1, uint32_t *sign2)
160 {
161   return (uint8_t)ecc_ecdsa_verify((uint32_t*)publicKey_x, (uint32_t*)publicKey_y, (uint32_t*)text,
162                               (uint32_t*)sign1, (uint32_t*)sign2);
163 }
164 
165 //*****************************************************************************
166 // ECC_ECDH_computeSharedSecret
167 //*****************************************************************************
168 uint8_t
ECC_ECDH_computeSharedSecret(uint32_t * privateKey,uint32_t * publicKey_x,uint32_t * publicKey_y,uint32_t * sharedSecret_x,uint32_t * sharedSecret_y)169 ECC_ECDH_computeSharedSecret(uint32_t *privateKey, uint32_t *publicKey_x,
170                              uint32_t *publicKey_y, uint32_t *sharedSecret_x,
171                              uint32_t *sharedSecret_y)
172 {
173   return (uint8_t)ecdh_computeSharedSecret((uint32_t*)privateKey, (uint32_t*)publicKey_x,
174                                  (uint32_t*)publicKey_y, (uint32_t*)sharedSecret_x,
175                                  (uint32_t*)sharedSecret_y);
176 }
177