1 /* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license. */ 2 3 #include "uECC.h" 4 5 // NULL 6 #include "stddef.h" 7 8 #ifndef uECC_PLATFORM 9 #if __AVR__ 10 #define uECC_PLATFORM uECC_avr 11 #elif defined(__thumb2__) || defined(_M_ARMT) /* I think MSVC only supports Thumb-2 targets */ 12 #define uECC_PLATFORM uECC_arm_thumb2 13 #elif defined(__thumb__) 14 #define uECC_PLATFORM uECC_arm_thumb 15 #elif defined(__arm__) || defined(_M_ARM) 16 #define uECC_PLATFORM uECC_arm 17 #elif defined(__i386__) || defined(_M_IX86) || defined(_X86_) || defined(__I86__) 18 #define uECC_PLATFORM uECC_x86 19 #elif defined(__amd64__) || defined(_M_X64) 20 #define uECC_PLATFORM uECC_x86_64 21 #else 22 #define uECC_PLATFORM uECC_arch_other 23 #endif 24 #endif 25 26 #ifndef uECC_WORD_SIZE 27 #if uECC_PLATFORM == uECC_avr 28 #define uECC_WORD_SIZE 1 29 #elif (uECC_PLATFORM == uECC_x86_64) 30 #define uECC_WORD_SIZE 8 31 #else 32 #define uECC_WORD_SIZE 4 33 #endif 34 #endif 35 36 #if (uECC_CURVE == uECC_secp160r1 || uECC_CURVE == uECC_secp224r1) && (uECC_WORD_SIZE == 8) 37 #undef uECC_WORD_SIZE 38 #define uECC_WORD_SIZE 4 39 #if (uECC_PLATFORM == uECC_x86_64) 40 #undef uECC_PLATFORM 41 #define uECC_PLATFORM uECC_x86 42 #endif 43 #endif 44 45 #if (uECC_WORD_SIZE != 1) && (uECC_WORD_SIZE != 4) && (uECC_WORD_SIZE != 8) 46 #error "Unsupported value for uECC_WORD_SIZE" 47 #endif 48 49 #if (uECC_ASM && (uECC_PLATFORM == uECC_avr) && (uECC_WORD_SIZE != 1)) 50 #pragma message ("uECC_WORD_SIZE must be 1 when using AVR asm") 51 #undef uECC_WORD_SIZE 52 #define uECC_WORD_SIZE 1 53 #endif 54 55 #if (uECC_ASM && \ 56 (uECC_PLATFORM == uECC_arm || uECC_PLATFORM == uECC_arm_thumb) && \ 57 (uECC_WORD_SIZE != 4)) 58 #pragma message ("uECC_WORD_SIZE must be 4 when using ARM asm") 59 #undef uECC_WORD_SIZE 60 #define uECC_WORD_SIZE 4 61 #endif 62 63 #if __STDC_VERSION__ >= 199901L 64 #define RESTRICT restrict 65 #else 66 #define RESTRICT 67 #endif 68 69 #if defined(__SIZEOF_INT128__) || ((__clang_major__ * 100 + __clang_minor__) >= 302) 70 #define SUPPORTS_INT128 1 71 #else 72 #define SUPPORTS_INT128 0 73 #endif 74 75 #define MAX_TRIES 64 76 77 #if (uECC_WORD_SIZE == 1) 78 79 typedef uint8_t uECC_word_t; 80 typedef uint16_t uECC_dword_t; 81 typedef uint8_t wordcount_t; 82 typedef int8_t swordcount_t; 83 typedef int16_t bitcount_t; 84 typedef int8_t cmpresult_t; 85 86 #define HIGH_BIT_SET 0x80 87 #define uECC_WORD_BITS 8 88 #define uECC_WORD_BITS_SHIFT 3 89 #define uECC_WORD_BITS_MASK 0x07 90 91 #define uECC_WORDS_1 20 92 #define uECC_WORDS_2 24 93 #define uECC_WORDS_3 32 94 #define uECC_WORDS_4 32 95 #define uECC_WORDS_5 28 96 97 #define uECC_N_WORDS_1 21 98 #define uECC_N_WORDS_2 24 99 #define uECC_N_WORDS_3 32 100 #define uECC_N_WORDS_4 32 101 #define uECC_N_WORDS_5 28 102 103 #define Curve_P_1 {0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, \ 104 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \ 105 0xFF, 0xFF, 0xFF, 0xFF} 106 #define Curve_P_2 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \ 107 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \ 108 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} 109 #define Curve_P_3 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \ 110 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, \ 111 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ 112 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF} 113 #define Curve_P_4 {0x2F, 0xFC, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, \ 114 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \ 115 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \ 116 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} 117 #define Curve_P_5 {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ 118 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, \ 119 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \ 120 0xFF, 0xFF, 0xFF, 0xFF} 121 122 #define Curve_B_1 {0x45, 0xFA, 0x65, 0xC5, 0xAD, 0xD4, 0xD4, 0x81, \ 123 0x9F, 0xF8, 0xAC, 0x65, 0x8B, 0x7A, 0xBD, 0x54, \ 124 0xFC, 0xBE, 0x97, 0x1C} 125 #define Curve_B_2 {0xB1, 0xB9, 0x46, 0xC1, 0xEC, 0xDE, 0xB8, 0xFE, \ 126 0x49, 0x30, 0x24, 0x72, 0xAB, 0xE9, 0xA7, 0x0F, \ 127 0xE7, 0x80, 0x9C, 0xE5, 0x19, 0x05, 0x21, 0x64} 128 #define Curve_B_3 {0x4B, 0x60, 0xD2, 0x27, 0x3E, 0x3C, 0xCE, 0x3B, \ 129 0xF6, 0xB0, 0x53, 0xCC, 0xB0, 0x06, 0x1D, 0x65, \ 130 0xBC, 0x86, 0x98, 0x76, 0x55, 0xBD, 0xEB, 0xB3, \ 131 0xE7, 0x93, 0x3A, 0xAA, 0xD8, 0x35, 0xC6, 0x5A} 132 #define Curve_B_4 {0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ 133 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ 134 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ 135 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} 136 #define Curve_B_5 {0xB4, 0xFF, 0x55, 0x23, 0x43, 0x39, 0x0B, 0x27, \ 137 0xBA, 0xD8, 0xBF, 0xD7, 0xB7, 0xB0, 0x44, 0x50, \ 138 0x56, 0x32, 0x41, 0xF5, 0xAB, 0xB3, 0x04, 0x0C, \ 139 0x85, 0x0A, 0x05, 0xB4} 140 141 #define Curve_G_1 { \ 142 {0x82, 0xFC, 0xCB, 0x13, 0xB9, 0x8B, 0xC3, 0x68, \ 143 0x89, 0x69, 0x64, 0x46, 0x28, 0x73, 0xF5, 0x8E, \ 144 0x68, 0xB5, 0x96, 0x4A}, \ 145 {0x32, 0xFB, 0xC5, 0x7A, 0x37, 0x51, 0x23, 0x04, \ 146 0x12, 0xC9, 0xDC, 0x59, 0x7D, 0x94, 0x68, 0x31, \ 147 0x55, 0x28, 0xA6, 0x23}} 148 149 #define Curve_G_2 { \ 150 {0x12, 0x10, 0xFF, 0x82, 0xFD, 0x0A, 0xFF, 0xF4, \ 151 0x00, 0x88, 0xA1, 0x43, 0xEB, 0x20, 0xBF, 0x7C, \ 152 0xF6, 0x90, 0x30, 0xB0, 0x0E, 0xA8, 0x8D, 0x18}, \ 153 {0x11, 0x48, 0x79, 0x1E, 0xA1, 0x77, 0xF9, 0x73, \ 154 0xD5, 0xCD, 0x24, 0x6B, 0xED, 0x11, 0x10, 0x63, \ 155 0x78, 0xDA, 0xC8, 0xFF, 0x95, 0x2B, 0x19, 0x07}} 156 157 #define Curve_G_3 { \ 158 {0x96, 0xC2, 0x98, 0xD8, 0x45, 0x39, 0xA1, 0xF4, \ 159 0xA0, 0x33, 0xEB, 0x2D, 0x81, 0x7D, 0x03, 0x77, \ 160 0xF2, 0x40, 0xA4, 0x63, 0xE5, 0xE6, 0xBC, 0xF8, \ 161 0x47, 0x42, 0x2C, 0xE1, 0xF2, 0xD1, 0x17, 0x6B}, \ 162 {0xF5, 0x51, 0xBF, 0x37, 0x68, 0x40, 0xB6, 0xCB, \ 163 0xCE, 0x5E, 0x31, 0x6B, 0x57, 0x33, 0xCE, 0x2B, \ 164 0x16, 0x9E, 0x0F, 0x7C, 0x4A, 0xEB, 0xE7, 0x8E, \ 165 0x9B, 0x7F, 0x1A, 0xFE, 0xE2, 0x42, 0xE3, 0x4F}} 166 167 #define Curve_G_4 { \ 168 {0x98, 0x17, 0xF8, 0x16, 0x5B, 0x81, 0xF2, 0x59, \ 169 0xD9, 0x28, 0xCE, 0x2D, 0xDB, 0xFC, 0x9B, 0x02, \ 170 0x07, 0x0B, 0x87, 0xCE, 0x95, 0x62, 0xA0, 0x55, \ 171 0xAC, 0xBB, 0xDC, 0xF9, 0x7E, 0x66, 0xBE, 0x79}, \ 172 {0xB8, 0xD4, 0x10, 0xFB, 0x8F, 0xD0, 0x47, 0x9C, \ 173 0x19, 0x54, 0x85, 0xA6, 0x48, 0xB4, 0x17, 0xFD, \ 174 0xA8, 0x08, 0x11, 0x0E, 0xFC, 0xFB, 0xA4, 0x5D, \ 175 0x65, 0xC4, 0xA3, 0x26, 0x77, 0xDA, 0x3A, 0x48}} 176 177 #define Curve_G_5 { \ 178 {0x21, 0x1D, 0x5C, 0x11, 0xD6, 0x80, 0x32, 0x34, \ 179 0x22, 0x11, 0xC2, 0x56, 0xD3, 0xC1, 0x03, 0x4A, \ 180 0xB9, 0x90, 0x13, 0x32, 0x7F, 0xBF, 0xB4, 0x6B, \ 181 0xBD, 0x0C, 0x0E, 0xB7}, \ 182 {0x34, 0x7E, 0x00, 0x85, 0x99, 0x81, 0xD5, 0x44, \ 183 0x64, 0x47, 0x07, 0x5A, 0xA0, 0x75, 0x43, 0xCD, \ 184 0xE6, 0xDF, 0x22, 0x4C, 0xFB, 0x23, 0xF7, 0xB5, \ 185 0x88, 0x63, 0x37, 0xBD}} 186 187 #define Curve_N_1 {0x57, 0x22, 0x75, 0xCA, 0xD3, 0xAE, 0x27, 0xF9, \ 188 0xC8, 0xF4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, \ 189 0x00, 0x00, 0x00, 0x00, 0x01} 190 #define Curve_N_2 {0x31, 0x28, 0xD2, 0xB4, 0xB1, 0xC9, 0x6B, 0x14, \ 191 0x36, 0xF8, 0xDE, 0x99, 0xFF, 0xFF, 0xFF, 0xFF, \ 192 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} 193 #define Curve_N_3 {0x51, 0x25, 0x63, 0xFC, 0xC2, 0xCA, 0xB9, 0xF3, \ 194 0x84, 0x9E, 0x17, 0xA7, 0xAD, 0xFA, 0xE6, 0xBC, \ 195 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \ 196 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF} 197 #define Curve_N_4 {0x41, 0x41, 0x36, 0xD0, 0x8C, 0x5E, 0xD2, 0xBF, \ 198 0x3B, 0xA0, 0x48, 0xAF, 0xE6, 0xDC, 0xAE, 0xBA, \ 199 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \ 200 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} 201 #define Curve_N_5 {0x3D, 0x2A, 0x5C, 0x5C, 0x45, 0x29, 0xDD, 0x13, \ 202 0x3E, 0xF0, 0xB8, 0xE0, 0xA2, 0x16, 0xFF, 0xFF, \ 203 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \ 204 0xFF, 0xFF, 0xFF, 0xFF} 205 206 #elif (uECC_WORD_SIZE == 4) 207 208 typedef uint32_t uECC_word_t; 209 typedef uint64_t uECC_dword_t; 210 typedef unsigned wordcount_t; 211 typedef int swordcount_t; 212 typedef int bitcount_t; 213 typedef int cmpresult_t; 214 215 #define HIGH_BIT_SET 0x80000000 216 #define uECC_WORD_BITS 32 217 #define uECC_WORD_BITS_SHIFT 5 218 #define uECC_WORD_BITS_MASK 0x01F 219 220 #define uECC_WORDS_1 5 221 #define uECC_WORDS_2 6 222 #define uECC_WORDS_3 8 223 #define uECC_WORDS_4 8 224 #define uECC_WORDS_5 7 225 226 #define uECC_N_WORDS_1 6 227 #define uECC_N_WORDS_2 6 228 #define uECC_N_WORDS_3 8 229 #define uECC_N_WORDS_4 8 230 #define uECC_N_WORDS_5 7 231 232 #define Curve_P_1 {0x7FFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF} 233 #define Curve_P_2 {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF} 234 #define Curve_P_3 {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, \ 235 0x00000000, 0x00000000, 0x00000001, 0xFFFFFFFF} 236 #define Curve_P_4 {0xFFFFFC2F, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, \ 237 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF} 238 #define Curve_P_5 {0x00000001, 0x00000000, 0x00000000, 0xFFFFFFFF, \ 239 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF} 240 241 #define Curve_B_1 {0xC565FA45, 0x81D4D4AD, 0x65ACF89F, 0x54BD7A8B, 0x1C97BEFC} 242 #define Curve_B_2 {0xC146B9B1, 0xFEB8DEEC, 0x72243049, 0x0FA7E9AB, 0xE59C80E7, 0x64210519} 243 #define Curve_B_3 {0x27D2604B, 0x3BCE3C3E, 0xCC53B0F6, 0x651D06B0, \ 244 0x769886BC, 0xB3EBBD55, 0xAA3A93E7, 0x5AC635D8} 245 #define Curve_B_4 {0x00000007, 0x00000000, 0x00000000, 0x00000000, \ 246 0x00000000, 0x00000000, 0x00000000, 0x00000000} 247 #define Curve_B_5 {0x2355FFB4, 0x270B3943, 0xD7BFD8BA, 0x5044B0B7, \ 248 0xF5413256, 0x0C04B3AB, 0xB4050A85} 249 250 #define Curve_G_1 { \ 251 {0x13CBFC82, 0x68C38BB9, 0x46646989, 0x8EF57328, 0x4A96B568}, \ 252 {0x7AC5FB32, 0x04235137, 0x59DCC912, 0x3168947D, 0x23A62855}} 253 254 #define Curve_G_2 { \ 255 {0x82FF1012, 0xF4FF0AFD, 0x43A18800, 0x7CBF20EB, 0xB03090F6, 0x188DA80E}, \ 256 {0x1E794811, 0x73F977A1, 0x6B24CDD5, 0x631011ED, 0xFFC8DA78, 0x07192B95}} 257 258 #define Curve_G_3 { \ 259 {0xD898C296, 0xF4A13945, 0x2DEB33A0, 0x77037D81, \ 260 0x63A440F2, 0xF8BCE6E5, 0xE12C4247, 0x6B17D1F2}, \ 261 {0x37BF51F5, 0xCBB64068, 0x6B315ECE, 0x2BCE3357, \ 262 0x7C0F9E16, 0x8EE7EB4A, 0xFE1A7F9B, 0x4FE342E2}} 263 264 #define Curve_G_4 { \ 265 {0x16F81798, 0x59F2815B, 0x2DCE28D9, 0x029BFCDB, \ 266 0xCE870B07, 0x55A06295, 0xF9DCBBAC, 0x79BE667E}, \ 267 {0xFB10D4B8, 0x9C47D08F, 0xA6855419, 0xFD17B448, \ 268 0x0E1108A8, 0x5DA4FBFC, 0x26A3C465, 0x483ADA77}} 269 270 #define Curve_G_5 { \ 271 {0x115C1D21, 0x343280D6, 0x56C21122, 0x4A03C1D3, \ 272 0x321390B9, 0x6BB4BF7F, 0xB70E0CBD}, \ 273 {0x85007E34, 0x44D58199, 0x5A074764, 0xCD4375A0, \ 274 0x4C22DFE6, 0xB5F723FB, 0xBD376388}} 275 276 #define Curve_N_1 {0xCA752257, 0xF927AED3, 0x0001F4C8, 0x00000000, 0x00000000, 0x00000001} 277 #define Curve_N_2 {0xB4D22831, 0x146BC9B1, 0x99DEF836, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF} 278 #define Curve_N_3 {0xFC632551, 0xF3B9CAC2, 0xA7179E84, 0xBCE6FAAD, \ 279 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0xFFFFFFFF} 280 #define Curve_N_4 {0xD0364141, 0xBFD25E8C, 0xAF48A03B, 0xBAAEDCE6, \ 281 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF} 282 #define Curve_N_5 {0x5C5C2A3D, 0x13DD2945, 0xE0B8F03E, 0xFFFF16A2, \ 283 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF} 284 285 #elif (uECC_WORD_SIZE == 8) 286 287 typedef uint64_t uECC_word_t; 288 #if SUPPORTS_INT128 289 typedef unsigned __int128 uECC_dword_t; 290 #endif 291 typedef unsigned wordcount_t; 292 typedef int swordcount_t; 293 typedef int bitcount_t; 294 typedef int cmpresult_t; 295 296 #define HIGH_BIT_SET 0x8000000000000000ull 297 #define uECC_WORD_BITS 64 298 #define uECC_WORD_BITS_SHIFT 6 299 #define uECC_WORD_BITS_MASK 0x03F 300 301 #define uECC_WORDS_1 3 302 #define uECC_WORDS_2 3 303 #define uECC_WORDS_3 4 304 #define uECC_WORDS_4 4 305 #define uECC_WORDS_5 4 306 307 #define uECC_N_WORDS_1 3 308 #define uECC_N_WORDS_2 3 309 #define uECC_N_WORDS_3 4 310 #define uECC_N_WORDS_4 4 311 #define uECC_N_WORDS_5 4 312 313 #define Curve_P_1 {0xFFFFFFFF7FFFFFFFull, 0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull} 314 #define Curve_P_2 {0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFFFFFFFFFEull, 0xFFFFFFFFFFFFFFFFull} 315 #define Curve_P_3 {0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull, \ 316 0x0000000000000000ull, 0xFFFFFFFF00000001ull} 317 #define Curve_P_4 {0xFFFFFFFEFFFFFC2Full, 0xFFFFFFFFFFFFFFFFull, \ 318 0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFFFFFFFFFFull} 319 #define Curve_P_5 {0x0000000000000001ull, 0xFFFFFFFF00000000ull, \ 320 0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull} 321 322 #define Curve_B_1 {0x81D4D4ADC565FA45ull, 0x54BD7A8B65ACF89Full, 0x000000001C97BEFCull} 323 #define Curve_B_2 {0xFEB8DEECC146B9B1ull, 0x0FA7E9AB72243049ull, 0x64210519E59C80E7ull} 324 #define Curve_B_3 {0x3BCE3C3E27D2604Bull, 0x651D06B0CC53B0F6ull, \ 325 0xB3EBBD55769886BCull, 0x5AC635D8AA3A93E7ull} 326 #define Curve_B_4 {0x0000000000000007ull, 0x0000000000000000ull, \ 327 0x0000000000000000ull, 0x0000000000000000ull} 328 #define Curve_B_5 {0x270B39432355FFB4ull, 0x5044B0B7D7BFD8BAull, \ 329 0x0C04B3ABF5413256ull, 0x00000000B4050A85ull} 330 331 #define Curve_G_1 { \ 332 {0x68C38BB913CBFC82ull, 0x8EF5732846646989ull, 0x000000004A96B568ull}, \ 333 {0x042351377AC5FB32ull, 0x3168947D59DCC912ull, 0x0000000023A62855ull}} 334 335 #define Curve_G_2 { \ 336 {0xF4FF0AFD82FF1012ull, 0x7CBF20EB43A18800ull, 0x188DA80EB03090F6ull}, \ 337 {0x73F977A11E794811ull, 0x631011ED6B24CDD5ull, 0x07192B95FFC8DA78ull}} 338 339 #define Curve_G_3 { \ 340 {0xF4A13945D898C296ull, 0x77037D812DEB33A0ull, 0xF8BCE6E563A440F2ull, 0x6B17D1F2E12C4247ull}, \ 341 {0xCBB6406837BF51F5ull, 0x2BCE33576B315ECEull, 0x8EE7EB4A7C0F9E16ull, 0x4FE342E2FE1A7F9Bull}} 342 343 #define Curve_G_4 { \ 344 {0x59F2815B16F81798ull, 0x029BFCDB2DCE28D9ull, 0x55A06295CE870B07ull, 0x79BE667EF9DCBBACull}, \ 345 {0x9C47D08FFB10D4B8ull, 0xFD17B448A6855419ull, 0x5DA4FBFC0E1108A8ull, 0x483ADA7726A3C465ull}} 346 347 #define Curve_G_5 { \ 348 {0x343280D6115C1D21ull, 0x4A03C1D356C21122ull, 0x6BB4BF7F321390B9ull, 0x00000000B70E0CBDull}, \ 349 {0x44D5819985007E34ull, 0xCD4375A05A074764ull, 0xB5F723FB4C22DFE6ull, 0x00000000BD376388ull}} 350 351 #define Curve_N_1 {0xF927AED3CA752257ull, 0x000000000001F4C8ull, 0x0000000100000000ull} 352 #define Curve_N_2 {0x146BC9B1B4D22831ull, 0xFFFFFFFF99DEF836ull, 0xFFFFFFFFFFFFFFFFull} 353 #define Curve_N_3 {0xF3B9CAC2FC632551ull, 0xBCE6FAADA7179E84ull, \ 354 0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFF00000000ull} 355 #define Curve_N_4 {0xBFD25E8CD0364141ull, 0xBAAEDCE6AF48A03Bull, \ 356 0xFFFFFFFFFFFFFFFEull, 0xFFFFFFFFFFFFFFFFull} 357 #define Curve_N_5 {0x13DD29455C5C2A3Dull, 0xFFFF16A2E0B8F03Eull, \ 358 0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull} 359 360 #endif /* (uECC_WORD_SIZE == 8) */ 361 362 #define uECC_WORDS uECC_CONCAT(uECC_WORDS_, uECC_CURVE) 363 #define uECC_N_WORDS uECC_CONCAT(uECC_N_WORDS_, uECC_CURVE) 364 365 typedef struct EccPoint { 366 uECC_word_t x[uECC_WORDS]; 367 uECC_word_t y[uECC_WORDS]; 368 } EccPoint; 369 370 static const uECC_word_t curve_p[uECC_WORDS] = uECC_CONCAT(Curve_P_, uECC_CURVE); 371 // Global object `curve_b' is only referenced from function `curve_x_side', it should be defined within that functions block scope 372 static const EccPoint curve_G = uECC_CONCAT(Curve_G_, uECC_CURVE); 373 static const uECC_word_t curve_n[uECC_N_WORDS] = uECC_CONCAT(Curve_N_, uECC_CURVE); 374 375 static void vli_clear(uECC_word_t *vli); 376 static uECC_word_t vli_isZero(const uECC_word_t *vli); 377 static uECC_word_t vli_testBit(const uECC_word_t *vli, bitcount_t bit); 378 #ifdef ENABLE_MICRO_ECC_ECDSA 379 static bitcount_t vli_numBits(const uECC_word_t *vli, wordcount_t max_words); 380 #endif 381 static void vli_set(uECC_word_t *dest, const uECC_word_t *src); 382 static cmpresult_t vli_cmp(const uECC_word_t *left, const uECC_word_t *right); 383 #ifdef ENABLE_MICRO_ECC_ECDSA 384 static cmpresult_t vli_equal(const uECC_word_t *left, const uECC_word_t *right); 385 #endif 386 static void vli_rshift1(uECC_word_t *vli); 387 static uECC_word_t vli_add(uECC_word_t *result, 388 const uECC_word_t *left, 389 const uECC_word_t *right); 390 static uECC_word_t vli_sub(uECC_word_t *result, 391 const uECC_word_t *left, 392 const uECC_word_t *right); 393 static void vli_mult(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right); 394 static void vli_modAdd(uECC_word_t *result, 395 const uECC_word_t *left, 396 const uECC_word_t *right, 397 const uECC_word_t *mod); 398 static void vli_modSub(uECC_word_t *result, 399 const uECC_word_t *left, 400 const uECC_word_t *right, 401 const uECC_word_t *mod); 402 static void vli_mmod_fast(uECC_word_t *RESTRICT result, uECC_word_t *RESTRICT product); 403 static void vli_modMult_fast(uECC_word_t *result, 404 const uECC_word_t *left, 405 const uECC_word_t *right); 406 static void vli_modInv(uECC_word_t *result, const uECC_word_t *input, const uECC_word_t *mod); 407 #if uECC_SQUARE_FUNC 408 static void vli_square(uECC_word_t *result, const uECC_word_t *left); 409 static void vli_modSquare_fast(uECC_word_t *result, const uECC_word_t *left); 410 #endif 411 412 #if ((defined(_WIN32) || defined(_WIN64)) && !defined(uECC_NO_DEFAULT_RNG)) 413 /* Windows */ 414 415 #define WIN32_LEAN_AND_MEAN 416 #include <windows.h> 417 #include <wincrypt.h> 418 419 static int default_RNG(uint8_t *dest, unsigned size) { 420 HCRYPTPROV prov; 421 if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { 422 return 0; 423 } 424 425 CryptGenRandom(prov, size, (BYTE *)dest); 426 CryptReleaseContext(prov, 0); 427 return 1; 428 } 429 430 #elif (defined(unix) || defined(__linux__) || defined(__unix__) || defined(__unix) || \ 431 (defined(__APPLE__) && defined(__MACH__)) || defined(uECC_POSIX)) && !defined(uECC_NO_DEFAULT_RNG) 432 433 /* Some POSIX-like system with /dev/urandom or /dev/random. */ 434 #include <sys/types.h> 435 #include <fcntl.h> 436 #include <unistd.h> 437 438 #ifndef O_CLOEXEC 439 #define O_CLOEXEC 0 440 #endif 441 442 static int default_RNG(uint8_t *dest, unsigned size) { 443 int fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC); 444 if (fd == -1) { 445 fd = open("/dev/random", O_RDONLY | O_CLOEXEC); 446 if (fd == -1) { 447 return 0; 448 } 449 } 450 451 char *ptr = (char *)dest; 452 size_t left = size; 453 while (left > 0) { 454 ssize_t bytes_read = read(fd, ptr, left); 455 if (bytes_read <= 0) { // read failed 456 close(fd); 457 return 0; 458 } 459 left -= bytes_read; 460 ptr += bytes_read; 461 } 462 463 close(fd); 464 return 1; 465 } 466 467 #else /* Some other platform */ 468 469 static int default_RNG(uint8_t *dest, unsigned size) { 470 (void) dest; 471 (void) size; 472 return 0; 473 } 474 475 #endif 476 477 static uECC_RNG_Function g_rng_function = &default_RNG; 478 479 void uECC_set_rng(uECC_RNG_Function rng_function) { 480 g_rng_function = rng_function; 481 } 482 483 #ifdef __GNUC__ /* Only support GCC inline asm for now */ 484 #if (uECC_ASM && (uECC_PLATFORM == uECC_avr)) 485 #include "asm_avr.inc" 486 #endif 487 488 #if (uECC_ASM && (uECC_PLATFORM == uECC_arm || uECC_PLATFORM == uECC_arm_thumb || \ 489 uECC_PLATFORM == uECC_arm_thumb2)) 490 #include "asm_arm.inc" 491 #endif 492 #endif 493 494 #if !asm_clear 495 static void vli_clear(uECC_word_t *vli) { 496 wordcount_t i; 497 for (i = 0; i < uECC_WORDS; ++i) { 498 vli[i] = 0; 499 } 500 } 501 #endif 502 503 /* Returns 1 if vli == 0, 0 otherwise. */ 504 #if !asm_isZero 505 static uECC_word_t vli_isZero(const uECC_word_t *vli) { 506 wordcount_t i; 507 for (i = 0; i < uECC_WORDS; ++i) { 508 if (vli[i]) { 509 return 0; 510 } 511 } 512 return 1; 513 } 514 #endif 515 516 /* Returns nonzero if bit 'bit' of vli is set. */ 517 #if !asm_testBit 518 static uECC_word_t vli_testBit(const uECC_word_t *vli, bitcount_t bit) { 519 return (vli[bit >> uECC_WORD_BITS_SHIFT] & ((uECC_word_t)1 << (bit & uECC_WORD_BITS_MASK))); 520 } 521 #endif 522 523 #ifdef ENABLE_MICO_ECC_ECDSA 524 525 /* Counts the number of words in vli. */ 526 #if !asm_numBits 527 static wordcount_t vli_numDigits(const uECC_word_t *vli, wordcount_t max_words) { 528 swordcount_t i; 529 /* Search from the end until we find a non-zero digit. 530 We do it in reverse because we expect that most digits will be nonzero. */ 531 for (i = max_words - 1; i >= 0 && vli[i] == 0; --i) { 532 } 533 534 return (i + 1); 535 } 536 537 /* Counts the number of bits required to represent vli. */ 538 static bitcount_t vli_numBits(const uECC_word_t *vli, wordcount_t max_words) { 539 uECC_word_t i; 540 uECC_word_t digit; 541 542 wordcount_t num_digits = vli_numDigits(vli, max_words); 543 if (num_digits == 0) { 544 return 0; 545 } 546 547 digit = vli[num_digits - 1]; 548 for (i = 0; digit; ++i) { 549 digit >>= 1; 550 } 551 552 return (((bitcount_t)(num_digits - 1) << uECC_WORD_BITS_SHIFT) + i); 553 } 554 555 #endif /* ENABLE_MICO_ECC_ECDSA */ 556 557 #endif /* !asm_numBits */ 558 559 /* Sets dest = src. */ 560 #if !asm_set 561 static void vli_set(uECC_word_t *dest, const uECC_word_t *src) { 562 wordcount_t i; 563 for (i = 0; i < uECC_WORDS; ++i) { 564 dest[i] = src[i]; 565 } 566 } 567 #endif 568 569 /* Returns sign of left - right. */ 570 #if !asm_cmp 571 static cmpresult_t vli_cmp(const uECC_word_t *left, const uECC_word_t *right) { 572 swordcount_t i; 573 for (i = uECC_WORDS - 1; i >= 0; --i) { 574 if (left[i] > right[i]) { 575 return 1; 576 } else if (left[i] < right[i]) { 577 return -1; 578 } 579 } 580 return 0; 581 } 582 #endif 583 584 #ifdef ENABLE_MICRO_ECC_ECDSA 585 586 static cmpresult_t vli_equal(const uECC_word_t *left, const uECC_word_t *right) { 587 uECC_word_t result = 0; 588 swordcount_t i; 589 for (i = uECC_WORDS - 1; i >= 0; --i) { 590 result |= (left[i] ^ right[i]); 591 } 592 return (result == 0); 593 } 594 595 #endif 596 597 /* Computes vli = vli >> 1. */ 598 #if !asm_rshift1 599 static void vli_rshift1(uECC_word_t *vli) { 600 uECC_word_t *end = vli; 601 uECC_word_t carry = 0; 602 603 vli += uECC_WORDS; 604 while (vli-- > end) { 605 uECC_word_t temp = *vli; 606 *vli = (temp >> 1) | carry; 607 carry = temp << (uECC_WORD_BITS - 1); 608 } 609 } 610 #endif 611 612 /* Computes result = left + right, returning carry. Can modify in place. */ 613 #if !asm_add 614 static uECC_word_t vli_add(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { 615 uECC_word_t carry = 0; 616 wordcount_t i; 617 for (i = 0; i < uECC_WORDS; ++i) { 618 uECC_word_t sum = left[i] + right[i] + carry; 619 if (sum != left[i]) { 620 carry = (sum < left[i]); 621 } 622 result[i] = sum; 623 } 624 return carry; 625 } 626 #endif 627 628 /* Computes result = left - right, returning borrow. Can modify in place. */ 629 #if !asm_sub 630 static uECC_word_t vli_sub(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { 631 uECC_word_t borrow = 0; 632 wordcount_t i; 633 for (i = 0; i < uECC_WORDS; ++i) { 634 uECC_word_t diff = left[i] - right[i] - borrow; 635 if (diff != left[i]) { 636 borrow = (diff > left[i]); 637 } 638 result[i] = diff; 639 } 640 return borrow; 641 } 642 #endif 643 644 #if (!asm_mult || (uECC_SQUARE_FUNC && !asm_square) || uECC_CURVE == uECC_secp256k1) 645 static void muladd(uECC_word_t a, 646 uECC_word_t b, 647 uECC_word_t *r0, 648 uECC_word_t *r1, 649 uECC_word_t *r2) { 650 #if uECC_WORD_SIZE == 8 && !SUPPORTS_INT128 651 uint64_t a0 = a & 0xffffffffull; 652 uint64_t a1 = a >> 32; 653 uint64_t b0 = b & 0xffffffffull; 654 uint64_t b1 = b >> 32; 655 656 uint64_t i0 = a0 * b0; 657 uint64_t i1 = a0 * b1; 658 uint64_t i2 = a1 * b0; 659 uint64_t i3 = a1 * b1; 660 661 uint64_t p0, p1; 662 663 i2 += (i0 >> 32); 664 i2 += i1; 665 if (i2 < i1) { // overflow 666 i3 += 0x100000000ull; 667 } 668 669 p0 = (i0 & 0xffffffffull) | (i2 << 32); 670 p1 = i3 + (i2 >> 32); 671 672 *r0 += p0; 673 *r1 += (p1 + (*r0 < p0)); 674 *r2 += ((*r1 < p1) || (*r1 == p1 && *r0 < p0)); 675 #else 676 uECC_dword_t p = (uECC_dword_t)a * b; 677 uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0; 678 r01 += p; 679 *r2 += (r01 < p); 680 *r1 = r01 >> uECC_WORD_BITS; 681 *r0 = (uECC_word_t)r01; 682 #endif 683 } 684 #define muladd_exists 1 685 #endif 686 687 #if !asm_mult 688 static void vli_mult(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { 689 uECC_word_t r0 = 0; 690 uECC_word_t r1 = 0; 691 uECC_word_t r2 = 0; 692 wordcount_t i, k; 693 694 /* Compute each digit of result in sequence, maintaining the carries. */ 695 for (k = 0; k < uECC_WORDS; ++k) { 696 for (i = 0; i <= k; ++i) { 697 muladd(left[i], right[k - i], &r0, &r1, &r2); 698 } 699 result[k] = r0; 700 r0 = r1; 701 r1 = r2; 702 r2 = 0; 703 } 704 for (k = uECC_WORDS; k < uECC_WORDS * 2 - 1; ++k) { 705 for (i = (k + 1) - uECC_WORDS; i < uECC_WORDS; ++i) { 706 muladd(left[i], right[k - i], &r0, &r1, &r2); 707 } 708 result[k] = r0; 709 r0 = r1; 710 r1 = r2; 711 r2 = 0; 712 } 713 result[uECC_WORDS * 2 - 1] = r0; 714 } 715 #endif 716 717 #if uECC_SQUARE_FUNC 718 719 #if !asm_square 720 static void mul2add(uECC_word_t a, 721 uECC_word_t b, 722 uECC_word_t *r0, 723 uECC_word_t *r1, 724 uECC_word_t *r2) { 725 #if uECC_WORD_SIZE == 8 && !SUPPORTS_INT128 726 uint64_t a0 = a & 0xffffffffull; 727 uint64_t a1 = a >> 32; 728 uint64_t b0 = b & 0xffffffffull; 729 uint64_t b1 = b >> 32; 730 731 uint64_t i0 = a0 * b0; 732 uint64_t i1 = a0 * b1; 733 uint64_t i2 = a1 * b0; 734 uint64_t i3 = a1 * b1; 735 736 uint64_t p0, p1; 737 738 i2 += (i0 >> 32); 739 i2 += i1; 740 if (i2 < i1) 741 { // overflow 742 i3 += 0x100000000ull; 743 } 744 745 p0 = (i0 & 0xffffffffull) | (i2 << 32); 746 p1 = i3 + (i2 >> 32); 747 748 *r2 += (p1 >> 63); 749 p1 = (p1 << 1) | (p0 >> 63); 750 p0 <<= 1; 751 752 *r0 += p0; 753 *r1 += (p1 + (*r0 < p0)); 754 *r2 += ((*r1 < p1) || (*r1 == p1 && *r0 < p0)); 755 #else 756 uECC_dword_t p = (uECC_dword_t)a * b; 757 uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0; 758 *r2 += (p >> (uECC_WORD_BITS * 2 - 1)); 759 p *= 2; 760 r01 += p; 761 *r2 += (r01 < p); 762 *r1 = r01 >> uECC_WORD_BITS; 763 *r0 = (uECC_word_t)r01; 764 #endif 765 } 766 767 static void vli_square(uECC_word_t *result, const uECC_word_t *left) { 768 uECC_word_t r0 = 0; 769 uECC_word_t r1 = 0; 770 uECC_word_t r2 = 0; 771 772 wordcount_t i, k; 773 774 for (k = 0; k < uECC_WORDS * 2 - 1; ++k) { 775 uECC_word_t min = (k < uECC_WORDS ? 0 : (k + 1) - uECC_WORDS); 776 for (i = min; i <= k && i <= k - i; ++i) { 777 if (i < k-i) { 778 mul2add(left[i], left[k - i], &r0, &r1, &r2); 779 } else { 780 muladd(left[i], left[k - i], &r0, &r1, &r2); 781 } 782 } 783 result[k] = r0; 784 r0 = r1; 785 r1 = r2; 786 r2 = 0; 787 } 788 789 result[uECC_WORDS * 2 - 1] = r0; 790 } 791 #endif 792 793 #else /* uECC_SQUARE_FUNC */ 794 795 #define vli_square(result, left, size) vli_mult((result), (left), (left), (size)) 796 797 #endif /* uECC_SQUARE_FUNC */ 798 799 800 /* Computes result = (left + right) % mod. 801 Assumes that left < mod and right < mod, and that result does not overlap mod. */ 802 #if !asm_modAdd 803 static void vli_modAdd(uECC_word_t *result, 804 const uECC_word_t *left, 805 const uECC_word_t *right, 806 const uECC_word_t *mod) { 807 uECC_word_t carry = vli_add(result, left, right); 808 if (carry || vli_cmp(result, mod) >= 0) { 809 /* result > mod (result = mod + remainder), so subtract mod to get remainder. */ 810 vli_sub(result, result, mod); 811 } 812 } 813 #endif 814 815 /* Computes result = (left - right) % mod. 816 Assumes that left < mod and right < mod, and that result does not overlap mod. */ 817 #if !asm_modSub 818 static void vli_modSub(uECC_word_t *result, 819 const uECC_word_t *left, 820 const uECC_word_t *right, 821 const uECC_word_t *mod) { 822 uECC_word_t l_borrow = vli_sub(result, left, right); 823 if (l_borrow) { 824 /* In this case, result == -diff == (max int) - diff. Since -x % d == d - x, 825 we can get the correct result from result + mod (with overflow). */ 826 vli_add(result, result, mod); 827 } 828 } 829 #endif 830 831 #if !asm_modSub_fast 832 #define vli_modSub_fast(result, left, right) vli_modSub((result), (left), (right), curve_p) 833 #endif 834 835 #if !asm_mmod_fast 836 837 #if (uECC_CURVE == uECC_secp160r1 || uECC_CURVE == uECC_secp256k1) 838 /* omega_mult() is defined farther below for the different curves / word sizes */ 839 static void omega_mult(uECC_word_t * RESTRICT result, const uECC_word_t * RESTRICT right); 840 841 /* Computes result = product % curve_p 842 see http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf page 354 843 844 Note that this only works if log2(omega) < log2(p) / 2 */ 845 static void vli_mmod_fast(uECC_word_t *RESTRICT result, uECC_word_t *RESTRICT product) { 846 uECC_word_t tmp[2 * uECC_WORDS]; 847 uECC_word_t carry; 848 849 vli_clear(tmp); 850 vli_clear(tmp + uECC_WORDS); 851 852 omega_mult(tmp, product + uECC_WORDS); /* (Rq, q) = q * c */ 853 854 carry = vli_add(result, product, tmp); /* (C, r) = r + q */ 855 vli_clear(product); 856 omega_mult(product, tmp + uECC_WORDS); /* Rq*c */ 857 carry += vli_add(result, result, product); /* (C1, r) = r + Rq*c */ 858 859 while (carry > 0) { 860 --carry; 861 vli_sub(result, result, curve_p); 862 } 863 if (vli_cmp(result, curve_p) > 0) { 864 vli_sub(result, result, curve_p); 865 } 866 } 867 868 #endif 869 870 #if uECC_CURVE == uECC_secp160r1 871 872 #if uECC_WORD_SIZE == 1 873 static void omega_mult(uint8_t * RESTRICT result, const uint8_t * RESTRICT right) { 874 uint8_t carry; 875 uint8_t i; 876 877 /* Multiply by (2^31 + 1). */ 878 vli_set(result + 4, right); /* 2^32 */ 879 vli_rshift1(result + 4); /* 2^31 */ 880 result[3] = right[0] << 7; /* get last bit from shift */ 881 882 carry = vli_add(result, result, right); /* 2^31 + 1 */ 883 for (i = uECC_WORDS; carry; ++i) { 884 uint16_t sum = (uint16_t)result[i] + carry; 885 result[i] = (uint8_t)sum; 886 carry = sum >> 8; 887 } 888 } 889 #elif uECC_WORD_SIZE == 4 890 static void omega_mult(uint32_t * RESTRICT result, const uint32_t * RESTRICT right) { 891 uint32_t carry; 892 unsigned i; 893 894 /* Multiply by (2^31 + 1). */ 895 vli_set(result + 1, right); /* 2^32 */ 896 vli_rshift1(result + 1); /* 2^31 */ 897 result[0] = right[0] << 31; /* get last bit from shift */ 898 899 carry = vli_add(result, result, right); /* 2^31 + 1 */ 900 for (i = uECC_WORDS; carry; ++i) { 901 uint64_t sum = (uint64_t)result[i] + carry; 902 result[i] = (uint32_t)sum; 903 carry = sum >> 32; 904 } 905 } 906 #endif /* uECC_WORD_SIZE */ 907 908 #elif uECC_CURVE == uECC_secp192r1 909 910 /* Computes result = product % curve_p. 911 See algorithm 5 and 6 from http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf */ 912 #if uECC_WORD_SIZE == 1 913 static void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) { 914 uint8_t tmp[uECC_WORDS]; 915 uint8_t carry; 916 917 vli_set(result, product); 918 919 vli_set(tmp, &product[24]); 920 carry = vli_add(result, result, tmp); 921 922 tmp[0] = tmp[1] = tmp[2] = tmp[3] = tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0; 923 tmp[8] = product[24]; tmp[9] = product[25]; tmp[10] = product[26]; tmp[11] = product[27]; 924 tmp[12] = product[28]; tmp[13] = product[29]; tmp[14] = product[30]; tmp[15] = product[31]; 925 tmp[16] = product[32]; tmp[17] = product[33]; tmp[18] = product[34]; tmp[19] = product[35]; 926 tmp[20] = product[36]; tmp[21] = product[37]; tmp[22] = product[38]; tmp[23] = product[39]; 927 carry += vli_add(result, result, tmp); 928 929 tmp[0] = tmp[8] = product[40]; 930 tmp[1] = tmp[9] = product[41]; 931 tmp[2] = tmp[10] = product[42]; 932 tmp[3] = tmp[11] = product[43]; 933 tmp[4] = tmp[12] = product[44]; 934 tmp[5] = tmp[13] = product[45]; 935 tmp[6] = tmp[14] = product[46]; 936 tmp[7] = tmp[15] = product[47]; 937 tmp[16] = tmp[17] = tmp[18] = tmp[19] = tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0; 938 carry += vli_add(result, result, tmp); 939 940 while (carry || vli_cmp(curve_p, result) != 1) { 941 carry -= vli_sub(result, result, curve_p); 942 } 943 } 944 #elif uECC_WORD_SIZE == 4 945 static void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product) { 946 uint32_t tmp[uECC_WORDS]; 947 int carry; 948 949 vli_set(result, product); 950 951 vli_set(tmp, &product[6]); 952 carry = vli_add(result, result, tmp); 953 954 tmp[0] = tmp[1] = 0; 955 tmp[2] = product[6]; 956 tmp[3] = product[7]; 957 tmp[4] = product[8]; 958 tmp[5] = product[9]; 959 carry += vli_add(result, result, tmp); 960 961 tmp[0] = tmp[2] = product[10]; 962 tmp[1] = tmp[3] = product[11]; 963 tmp[4] = tmp[5] = 0; 964 carry += vli_add(result, result, tmp); 965 966 while (carry || vli_cmp(curve_p, result) != 1) { 967 carry -= vli_sub(result, result, curve_p); 968 } 969 } 970 #else 971 static void vli_mmod_fast(uint64_t *RESTRICT result, uint64_t *RESTRICT product) { 972 uint64_t tmp[uECC_WORDS]; 973 int carry; 974 975 vli_set(result, product); 976 977 vli_set(tmp, &product[3]); 978 carry = vli_add(result, result, tmp); 979 980 tmp[0] = 0; 981 tmp[1] = product[3]; 982 tmp[2] = product[4]; 983 carry += vli_add(result, result, tmp); 984 985 tmp[0] = tmp[1] = product[5]; 986 tmp[2] = 0; 987 carry += vli_add(result, result, tmp); 988 989 while (carry || vli_cmp(curve_p, result) != 1) { 990 carry -= vli_sub(result, result, curve_p); 991 } 992 } 993 #endif /* uECC_WORD_SIZE */ 994 995 #elif uECC_CURVE == uECC_secp256r1 996 997 /* Computes result = product % curve_p 998 from http://www.nsa.gov/ia/_files/nist-routines.pdf */ 999 #if uECC_WORD_SIZE == 1 1000 static void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) { 1001 uint8_t tmp[uECC_BYTES]; 1002 int8_t carry; 1003 1004 /* t */ 1005 vli_set(result, product); 1006 1007 /* s1 */ 1008 tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0; 1009 tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0; 1010 tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0; 1011 tmp[12] = product[44]; tmp[13] = product[45]; tmp[14] = product[46]; tmp[15] = product[47]; 1012 tmp[16] = product[48]; tmp[17] = product[49]; tmp[18] = product[50]; tmp[19] = product[51]; 1013 tmp[20] = product[52]; tmp[21] = product[53]; tmp[22] = product[54]; tmp[23] = product[55]; 1014 tmp[24] = product[56]; tmp[25] = product[57]; tmp[26] = product[58]; tmp[27] = product[59]; 1015 tmp[28] = product[60]; tmp[29] = product[61]; tmp[30] = product[62]; tmp[31] = product[63]; 1016 carry = vli_add(tmp, tmp, tmp); 1017 carry += vli_add(result, result, tmp); 1018 1019 /* s2 */ 1020 tmp[12] = product[48]; tmp[13] = product[49]; tmp[14] = product[50]; tmp[15] = product[51]; 1021 tmp[16] = product[52]; tmp[17] = product[53]; tmp[18] = product[54]; tmp[19] = product[55]; 1022 tmp[20] = product[56]; tmp[21] = product[57]; tmp[22] = product[58]; tmp[23] = product[59]; 1023 tmp[24] = product[60]; tmp[25] = product[61]; tmp[26] = product[62]; tmp[27] = product[63]; 1024 tmp[28] = tmp[29] = tmp[30] = tmp[31] = 0; 1025 carry += vli_add(tmp, tmp, tmp); 1026 carry += vli_add(result, result, tmp); 1027 1028 /* s3 */ 1029 tmp[0] = product[32]; tmp[1] = product[33]; tmp[2] = product[34]; tmp[3] = product[35]; 1030 tmp[4] = product[36]; tmp[5] = product[37]; tmp[6] = product[38]; tmp[7] = product[39]; 1031 tmp[8] = product[40]; tmp[9] = product[41]; tmp[10] = product[42]; tmp[11] = product[43]; 1032 tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0; 1033 tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0; 1034 tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0; 1035 tmp[24] = product[56]; tmp[25] = product[57]; tmp[26] = product[58]; tmp[27] = product[59]; 1036 tmp[28] = product[60]; tmp[29] = product[61]; tmp[30] = product[62]; tmp[31] = product[63]; 1037 carry += vli_add(result, result, tmp); 1038 1039 /* s4 */ 1040 tmp[0] = product[36]; tmp[1] = product[37]; tmp[2] = product[38]; tmp[3] = product[39]; 1041 tmp[4] = product[40]; tmp[5] = product[41]; tmp[6] = product[42]; tmp[7] = product[43]; 1042 tmp[8] = product[44]; tmp[9] = product[45]; tmp[10] = product[46]; tmp[11] = product[47]; 1043 tmp[12] = product[52]; tmp[13] = product[53]; tmp[14] = product[54]; tmp[15] = product[55]; 1044 tmp[16] = product[56]; tmp[17] = product[57]; tmp[18] = product[58]; tmp[19] = product[59]; 1045 tmp[20] = product[60]; tmp[21] = product[61]; tmp[22] = product[62]; tmp[23] = product[63]; 1046 tmp[24] = product[52]; tmp[25] = product[53]; tmp[26] = product[54]; tmp[27] = product[55]; 1047 tmp[28] = product[32]; tmp[29] = product[33]; tmp[30] = product[34]; tmp[31] = product[35]; 1048 carry += vli_add(result, result, tmp); 1049 1050 /* d1 */ 1051 tmp[0] = product[44]; tmp[1] = product[45]; tmp[2] = product[46]; tmp[3] = product[47]; 1052 tmp[4] = product[48]; tmp[5] = product[49]; tmp[6] = product[50]; tmp[7] = product[51]; 1053 tmp[8] = product[52]; tmp[9] = product[53]; tmp[10] = product[54]; tmp[11] = product[55]; 1054 tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0; 1055 tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0; 1056 tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0; 1057 tmp[24] = product[32]; tmp[25] = product[33]; tmp[26] = product[34]; tmp[27] = product[35]; 1058 tmp[28] = product[40]; tmp[29] = product[41]; tmp[30] = product[42]; tmp[31] = product[43]; 1059 carry -= vli_sub(result, result, tmp); 1060 1061 /* d2 */ 1062 tmp[0] = product[48]; tmp[1] = product[49]; tmp[2] = product[50]; tmp[3] = product[51]; 1063 tmp[4] = product[52]; tmp[5] = product[53]; tmp[6] = product[54]; tmp[7] = product[55]; 1064 tmp[8] = product[56]; tmp[9] = product[57]; tmp[10] = product[58]; tmp[11] = product[59]; 1065 tmp[12] = product[60]; tmp[13] = product[61]; tmp[14] = product[62]; tmp[15] = product[63]; 1066 tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0; 1067 tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0; 1068 tmp[24] = product[36]; tmp[25] = product[37]; tmp[26] = product[38]; tmp[27] = product[39]; 1069 tmp[28] = product[44]; tmp[29] = product[45]; tmp[30] = product[46]; tmp[31] = product[47]; 1070 carry -= vli_sub(result, result, tmp); 1071 1072 /* d3 */ 1073 tmp[0] = product[52]; tmp[1] = product[53]; tmp[2] = product[54]; tmp[3] = product[55]; 1074 tmp[4] = product[56]; tmp[5] = product[57]; tmp[6] = product[58]; tmp[7] = product[59]; 1075 tmp[8] = product[60]; tmp[9] = product[61]; tmp[10] = product[62]; tmp[11] = product[63]; 1076 tmp[12] = product[32]; tmp[13] = product[33]; tmp[14] = product[34]; tmp[15] = product[35]; 1077 tmp[16] = product[36]; tmp[17] = product[37]; tmp[18] = product[38]; tmp[19] = product[39]; 1078 tmp[20] = product[40]; tmp[21] = product[41]; tmp[22] = product[42]; tmp[23] = product[43]; 1079 tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0; 1080 tmp[28] = product[48]; tmp[29] = product[49]; tmp[30] = product[50]; tmp[31] = product[51]; 1081 carry -= vli_sub(result, result, tmp); 1082 1083 /* d4 */ 1084 tmp[0] = product[56]; tmp[1] = product[57]; tmp[2] = product[58]; tmp[3] = product[59]; 1085 tmp[4] = product[60]; tmp[5] = product[61]; tmp[6] = product[62]; tmp[7] = product[63]; 1086 tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0; 1087 tmp[12] = product[36]; tmp[13] = product[37]; tmp[14] = product[38]; tmp[15] = product[39]; 1088 tmp[16] = product[40]; tmp[17] = product[41]; tmp[18] = product[42]; tmp[19] = product[43]; 1089 tmp[20] = product[44]; tmp[21] = product[45]; tmp[22] = product[46]; tmp[23] = product[47]; 1090 tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0; 1091 tmp[28] = product[52]; tmp[29] = product[53]; tmp[30] = product[54]; tmp[31] = product[55]; 1092 carry -= vli_sub(result, result, tmp); 1093 1094 if (carry < 0) { 1095 do { 1096 carry += vli_add(result, result, curve_p); 1097 } while (carry < 0); 1098 } else { 1099 while (carry || vli_cmp(curve_p, result) != 1) { 1100 carry -= vli_sub(result, result, curve_p); 1101 } 1102 } 1103 } 1104 #elif uECC_WORD_SIZE == 4 1105 static void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product) { 1106 uint32_t tmp[uECC_WORDS]; 1107 int carry; 1108 1109 /* t */ 1110 vli_set(result, product); 1111 1112 /* s1 */ 1113 tmp[0] = tmp[1] = tmp[2] = 0; 1114 tmp[3] = product[11]; 1115 tmp[4] = product[12]; 1116 tmp[5] = product[13]; 1117 tmp[6] = product[14]; 1118 tmp[7] = product[15]; 1119 carry = vli_add(tmp, tmp, tmp); 1120 carry += vli_add(result, result, tmp); 1121 1122 /* s2 */ 1123 tmp[3] = product[12]; 1124 tmp[4] = product[13]; 1125 tmp[5] = product[14]; 1126 tmp[6] = product[15]; 1127 tmp[7] = 0; 1128 carry += vli_add(tmp, tmp, tmp); 1129 carry += vli_add(result, result, tmp); 1130 1131 /* s3 */ 1132 tmp[0] = product[8]; 1133 tmp[1] = product[9]; 1134 tmp[2] = product[10]; 1135 tmp[3] = tmp[4] = tmp[5] = 0; 1136 tmp[6] = product[14]; 1137 tmp[7] = product[15]; 1138 carry += vli_add(result, result, tmp); 1139 1140 /* s4 */ 1141 tmp[0] = product[9]; 1142 tmp[1] = product[10]; 1143 tmp[2] = product[11]; 1144 tmp[3] = product[13]; 1145 tmp[4] = product[14]; 1146 tmp[5] = product[15]; 1147 tmp[6] = product[13]; 1148 tmp[7] = product[8]; 1149 carry += vli_add(result, result, tmp); 1150 1151 /* d1 */ 1152 tmp[0] = product[11]; 1153 tmp[1] = product[12]; 1154 tmp[2] = product[13]; 1155 tmp[3] = tmp[4] = tmp[5] = 0; 1156 tmp[6] = product[8]; 1157 tmp[7] = product[10]; 1158 carry -= vli_sub(result, result, tmp); 1159 1160 /* d2 */ 1161 tmp[0] = product[12]; 1162 tmp[1] = product[13]; 1163 tmp[2] = product[14]; 1164 tmp[3] = product[15]; 1165 tmp[4] = tmp[5] = 0; 1166 tmp[6] = product[9]; 1167 tmp[7] = product[11]; 1168 carry -= vli_sub(result, result, tmp); 1169 1170 /* d3 */ 1171 tmp[0] = product[13]; 1172 tmp[1] = product[14]; 1173 tmp[2] = product[15]; 1174 tmp[3] = product[8]; 1175 tmp[4] = product[9]; 1176 tmp[5] = product[10]; 1177 tmp[6] = 0; 1178 tmp[7] = product[12]; 1179 carry -= vli_sub(result, result, tmp); 1180 1181 /* d4 */ 1182 tmp[0] = product[14]; 1183 tmp[1] = product[15]; 1184 tmp[2] = 0; 1185 tmp[3] = product[9]; 1186 tmp[4] = product[10]; 1187 tmp[5] = product[11]; 1188 tmp[6] = 0; 1189 tmp[7] = product[13]; 1190 carry -= vli_sub(result, result, tmp); 1191 1192 if (carry < 0) { 1193 do { 1194 carry += vli_add(result, result, curve_p); 1195 } while (carry < 0); 1196 } else { 1197 while (carry || vli_cmp(curve_p, result) != 1) { 1198 carry -= vli_sub(result, result, curve_p); 1199 } 1200 } 1201 } 1202 #else 1203 static void vli_mmod_fast(uint64_t *RESTRICT result, uint64_t *RESTRICT product) { 1204 uint64_t tmp[uECC_WORDS]; 1205 int carry; 1206 1207 /* t */ 1208 vli_set(result, product); 1209 1210 /* s1 */ 1211 tmp[0] = 0; 1212 tmp[1] = product[5] & 0xffffffff00000000ull; 1213 tmp[2] = product[6]; 1214 tmp[3] = product[7]; 1215 carry = vli_add(tmp, tmp, tmp); 1216 carry += vli_add(result, result, tmp); 1217 1218 /* s2 */ 1219 tmp[1] = product[6] << 32; 1220 tmp[2] = (product[6] >> 32) | (product[7] << 32); 1221 tmp[3] = product[7] >> 32; 1222 carry += vli_add(tmp, tmp, tmp); 1223 carry += vli_add(result, result, tmp); 1224 1225 /* s3 */ 1226 tmp[0] = product[4]; 1227 tmp[1] = product[5] & 0xffffffff; 1228 tmp[2] = 0; 1229 tmp[3] = product[7]; 1230 carry += vli_add(result, result, tmp); 1231 1232 /* s4 */ 1233 tmp[0] = (product[4] >> 32) | (product[5] << 32); 1234 tmp[1] = (product[5] >> 32) | (product[6] & 0xffffffff00000000ull); 1235 tmp[2] = product[7]; 1236 tmp[3] = (product[6] >> 32) | (product[4] << 32); 1237 carry += vli_add(result, result, tmp); 1238 1239 /* d1 */ 1240 tmp[0] = (product[5] >> 32) | (product[6] << 32); 1241 tmp[1] = (product[6] >> 32); 1242 tmp[2] = 0; 1243 tmp[3] = (product[4] & 0xffffffff) | (product[5] << 32); 1244 carry -= vli_sub(result, result, tmp); 1245 1246 /* d2 */ 1247 tmp[0] = product[6]; 1248 tmp[1] = product[7]; 1249 tmp[2] = 0; 1250 tmp[3] = (product[4] >> 32) | (product[5] & 0xffffffff00000000ull); 1251 carry -= vli_sub(result, result, tmp); 1252 1253 /* d3 */ 1254 tmp[0] = (product[6] >> 32) | (product[7] << 32); 1255 tmp[1] = (product[7] >> 32) | (product[4] << 32); 1256 tmp[2] = (product[4] >> 32) | (product[5] << 32); 1257 tmp[3] = (product[6] << 32); 1258 carry -= vli_sub(result, result, tmp); 1259 1260 /* d4 */ 1261 tmp[0] = product[7]; 1262 tmp[1] = product[4] & 0xffffffff00000000ull; 1263 tmp[2] = product[5]; 1264 tmp[3] = product[6] & 0xffffffff00000000ull; 1265 carry -= vli_sub(result, result, tmp); 1266 1267 if (carry < 0) { 1268 do { 1269 carry += vli_add(result, result, curve_p); 1270 } while (carry < 0); 1271 } else { 1272 while (carry || vli_cmp(curve_p, result) != 1) { 1273 carry -= vli_sub(result, result, curve_p); 1274 } 1275 } 1276 } 1277 #endif /* uECC_WORD_SIZE */ 1278 1279 #elif uECC_CURVE == uECC_secp256k1 1280 1281 #if uECC_WORD_SIZE == 1 1282 static void omega_mult(uint8_t * RESTRICT result, const uint8_t * RESTRICT right) { 1283 /* Multiply by (2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */ 1284 uECC_word_t r0 = 0; 1285 uECC_word_t r1 = 0; 1286 uECC_word_t r2 = 0; 1287 wordcount_t k; 1288 1289 /* Multiply by (2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */ 1290 muladd(0xD1, right[0], &r0, &r1, &r2); 1291 result[0] = r0; 1292 r0 = r1; 1293 r1 = r2; 1294 /* r2 is still 0 */ 1295 1296 for (k = 1; k < uECC_WORDS; ++k) { 1297 muladd(0x03, right[k - 1], &r0, &r1, &r2); 1298 muladd(0xD1, right[k], &r0, &r1, &r2); 1299 result[k] = r0; 1300 r0 = r1; 1301 r1 = r2; 1302 r2 = 0; 1303 } 1304 muladd(0x03, right[uECC_WORDS - 1], &r0, &r1, &r2); 1305 result[uECC_WORDS] = r0; 1306 result[uECC_WORDS + 1] = r1; 1307 1308 result[4 + uECC_WORDS] = vli_add(result + 4, result + 4, right); /* add the 2^32 multiple */ 1309 } 1310 #elif uECC_WORD_SIZE == 4 1311 static void omega_mult(uint32_t * RESTRICT result, const uint32_t * RESTRICT right) { 1312 /* Multiply by (2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */ 1313 uint32_t carry = 0; 1314 wordcount_t k; 1315 1316 for (k = 0; k < uECC_WORDS; ++k) { 1317 uint64_t p = (uint64_t)0x3D1 * right[k] + carry; 1318 result[k] = (p & 0xffffffff); 1319 carry = p >> 32; 1320 } 1321 result[uECC_WORDS] = carry; 1322 1323 result[1 + uECC_WORDS] = vli_add(result + 1, result + 1, right); /* add the 2^32 multiple */ 1324 } 1325 #else 1326 static void omega_mult(uint64_t * RESTRICT result, const uint64_t * RESTRICT right) { 1327 uECC_word_t r0 = 0; 1328 uECC_word_t r1 = 0; 1329 uECC_word_t r2 = 0; 1330 wordcount_t k; 1331 1332 /* Multiply by (2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */ 1333 for (k = 0; k < uECC_WORDS; ++k) { 1334 muladd(0x1000003D1ull, right[k], &r0, &r1, &r2); 1335 result[k] = r0; 1336 r0 = r1; 1337 r1 = r2; 1338 r2 = 0; 1339 } 1340 result[uECC_WORDS] = r0; 1341 } 1342 #endif /* uECC_WORD_SIZE */ 1343 1344 #elif uECC_CURVE == uECC_secp224r1 1345 1346 /* Computes result = product % curve_p 1347 from http://www.nsa.gov/ia/_files/nist-routines.pdf */ 1348 #if uECC_WORD_SIZE == 1 1349 // TODO it may be faster to use the omega_mult method when fully asm optimized. 1350 void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) { 1351 uint8_t tmp[uECC_WORDS]; 1352 int8_t carry; 1353 1354 /* t */ 1355 vli_set(result, product); 1356 1357 /* s1 */ 1358 tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0; 1359 tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0; 1360 tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0; 1361 tmp[12] = product[28]; tmp[13] = product[29]; tmp[14] = product[30]; tmp[15] = product[31]; 1362 tmp[16] = product[32]; tmp[17] = product[33]; tmp[18] = product[34]; tmp[19] = product[35]; 1363 tmp[20] = product[36]; tmp[21] = product[37]; tmp[22] = product[38]; tmp[23] = product[39]; 1364 tmp[24] = product[40]; tmp[25] = product[41]; tmp[26] = product[42]; tmp[27] = product[43]; 1365 carry = vli_add(result, result, tmp); 1366 1367 /* s2 */ 1368 tmp[12] = product[44]; tmp[13] = product[45]; tmp[14] = product[46]; tmp[15] = product[47]; 1369 tmp[16] = product[48]; tmp[17] = product[49]; tmp[18] = product[50]; tmp[19] = product[51]; 1370 tmp[20] = product[52]; tmp[21] = product[53]; tmp[22] = product[54]; tmp[23] = product[55]; 1371 tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0; 1372 carry += vli_add(result, result, tmp); 1373 1374 /* d1 */ 1375 tmp[0] = product[28]; tmp[1] = product[29]; tmp[2] = product[30]; tmp[3] = product[31]; 1376 tmp[4] = product[32]; tmp[5] = product[33]; tmp[6] = product[34]; tmp[7] = product[35]; 1377 tmp[8] = product[36]; tmp[9] = product[37]; tmp[10] = product[38]; tmp[11] = product[39]; 1378 tmp[12] = product[40]; tmp[13] = product[41]; tmp[14] = product[42]; tmp[15] = product[43]; 1379 tmp[16] = product[44]; tmp[17] = product[45]; tmp[18] = product[46]; tmp[19] = product[47]; 1380 tmp[20] = product[48]; tmp[21] = product[49]; tmp[22] = product[50]; tmp[23] = product[51]; 1381 tmp[24] = product[52]; tmp[25] = product[53]; tmp[26] = product[54]; tmp[27] = product[55]; 1382 carry -= vli_sub(result, result, tmp); 1383 1384 /* d2 */ 1385 tmp[0] = product[44]; tmp[1] = product[45]; tmp[2] = product[46]; tmp[3] = product[47]; 1386 tmp[4] = product[48]; tmp[5] = product[49]; tmp[6] = product[50]; tmp[7] = product[51]; 1387 tmp[8] = product[52]; tmp[9] = product[53]; tmp[10] = product[54]; tmp[11] = product[55]; 1388 tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0; 1389 tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0; 1390 tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0; 1391 tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0; 1392 carry -= vli_sub(result, result, tmp); 1393 1394 if (carry < 0) { 1395 do { 1396 carry += vli_add(result, result, curve_p); 1397 } while (carry < 0); 1398 } else { 1399 while (carry || vli_cmp(curve_p, result) != 1) { 1400 carry -= vli_sub(result, result, curve_p); 1401 } 1402 } 1403 } 1404 #elif uECC_WORD_SIZE == 4 1405 void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product) 1406 { 1407 uint32_t tmp[uECC_WORDS]; 1408 int carry; 1409 1410 /* t */ 1411 vli_set(result, product); 1412 1413 /* s1 */ 1414 tmp[0] = tmp[1] = tmp[2] = 0; 1415 tmp[3] = product[7]; 1416 tmp[4] = product[8]; 1417 tmp[5] = product[9]; 1418 tmp[6] = product[10]; 1419 carry = vli_add(result, result, tmp); 1420 1421 /* s2 */ 1422 tmp[3] = product[11]; 1423 tmp[4] = product[12]; 1424 tmp[5] = product[13]; 1425 tmp[6] = 0; 1426 carry += vli_add(result, result, tmp); 1427 1428 /* d1 */ 1429 tmp[0] = product[7]; 1430 tmp[1] = product[8]; 1431 tmp[2] = product[9]; 1432 tmp[3] = product[10]; 1433 tmp[4] = product[11]; 1434 tmp[5] = product[12]; 1435 tmp[6] = product[13]; 1436 carry -= vli_sub(result, result, tmp); 1437 1438 /* d2 */ 1439 tmp[0] = product[11]; 1440 tmp[1] = product[12]; 1441 tmp[2] = product[13]; 1442 tmp[3] = tmp[4] = tmp[5] = tmp[6] = 0; 1443 carry -= vli_sub(result, result, tmp); 1444 1445 if (carry < 0) { 1446 do { 1447 carry += vli_add(result, result, curve_p); 1448 } while (carry < 0); 1449 } else { 1450 while (carry || vli_cmp(curve_p, result) != 1) { 1451 carry -= vli_sub(result, result, curve_p); 1452 } 1453 } 1454 } 1455 #endif /* uECC_WORD_SIZE */ 1456 1457 #endif /* uECC_CURVE */ 1458 #endif /* !asm_mmod_fast */ 1459 1460 /* Computes result = (left * right) % curve_p. */ 1461 static void vli_modMult_fast(uECC_word_t *result, 1462 const uECC_word_t *left, 1463 const uECC_word_t *right) { 1464 uECC_word_t product[2 * uECC_WORDS]; 1465 vli_mult(product, left, right); 1466 vli_mmod_fast(result, product); 1467 } 1468 1469 #if uECC_SQUARE_FUNC 1470 1471 /* Computes result = left^2 % curve_p. */ 1472 static void vli_modSquare_fast(uECC_word_t *result, const uECC_word_t *left) { 1473 uECC_word_t product[2 * uECC_WORDS]; 1474 vli_square(product, left); 1475 vli_mmod_fast(result, product); 1476 } 1477 1478 #else /* uECC_SQUARE_FUNC */ 1479 1480 #define vli_modSquare_fast(result, left) vli_modMult_fast((result), (left), (left)) 1481 1482 #endif /* uECC_SQUARE_FUNC */ 1483 1484 1485 #define EVEN(vli) (!(vli[0] & 1)) 1486 /* Computes result = (1 / input) % mod. All VLIs are the same size. 1487 See "From Euclid's GCD to Montgomery Multiplication to the Great Divide" 1488 https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf */ 1489 #if !asm_modInv 1490 static void vli_modInv(uECC_word_t *result, const uECC_word_t *input, const uECC_word_t *mod) { 1491 uECC_word_t a[uECC_WORDS], b[uECC_WORDS], u[uECC_WORDS], v[uECC_WORDS]; 1492 uECC_word_t carry; 1493 cmpresult_t cmpResult; 1494 1495 if (vli_isZero(input)) { 1496 vli_clear(result); 1497 return; 1498 } 1499 1500 vli_set(a, input); 1501 vli_set(b, mod); 1502 vli_clear(u); 1503 u[0] = 1; 1504 vli_clear(v); 1505 while ((cmpResult = vli_cmp(a, b)) != 0) { 1506 carry = 0; 1507 if (EVEN(a)) { 1508 vli_rshift1(a); 1509 if (!EVEN(u)) { 1510 carry = vli_add(u, u, mod); 1511 } 1512 vli_rshift1(u); 1513 if (carry) { 1514 u[uECC_WORDS - 1] |= HIGH_BIT_SET; 1515 } 1516 } else if (EVEN(b)) { 1517 vli_rshift1(b); 1518 if (!EVEN(v)) { 1519 carry = vli_add(v, v, mod); 1520 } 1521 vli_rshift1(v); 1522 if (carry) { 1523 v[uECC_WORDS - 1] |= HIGH_BIT_SET; 1524 } 1525 } else if (cmpResult > 0) { 1526 vli_sub(a, a, b); 1527 vli_rshift1(a); 1528 if (vli_cmp(u, v) < 0) { 1529 vli_add(u, u, mod); 1530 } 1531 vli_sub(u, u, v); 1532 if (!EVEN(u)) { 1533 carry = vli_add(u, u, mod); 1534 } 1535 vli_rshift1(u); 1536 if (carry) { 1537 u[uECC_WORDS - 1] |= HIGH_BIT_SET; 1538 } 1539 } else { 1540 vli_sub(b, b, a); 1541 vli_rshift1(b); 1542 if (vli_cmp(v, u) < 0) { 1543 vli_add(v, v, mod); 1544 } 1545 vli_sub(v, v, u); 1546 if (!EVEN(v)) { 1547 carry = vli_add(v, v, mod); 1548 } 1549 vli_rshift1(v); 1550 if (carry) { 1551 v[uECC_WORDS - 1] |= HIGH_BIT_SET; 1552 } 1553 } 1554 } 1555 vli_set(result, u); 1556 } 1557 #endif /* !asm_modInv */ 1558 1559 /* ------ Point operations ------ */ 1560 1561 /* Returns 1 if 'point' is the point at infinity, 0 otherwise. */ 1562 static cmpresult_t EccPoint_isZero(const EccPoint *point) { 1563 return (vli_isZero(point->x) && vli_isZero(point->y)); 1564 } 1565 1566 /* Point multiplication algorithm using Montgomery's ladder with co-Z coordinates. 1567 From http://eprint.iacr.org/2011/338.pdf 1568 */ 1569 1570 /* Double in place */ 1571 #if (uECC_CURVE == uECC_secp256k1) 1572 static void EccPoint_double_jacobian(uECC_word_t * RESTRICT X1, 1573 uECC_word_t * RESTRICT Y1, 1574 uECC_word_t * RESTRICT Z1) { 1575 /* t1 = X, t2 = Y, t3 = Z */ 1576 uECC_word_t t4[uECC_WORDS]; 1577 uECC_word_t t5[uECC_WORDS]; 1578 1579 if (vli_isZero(Z1)) { 1580 return; 1581 } 1582 1583 vli_modSquare_fast(t5, Y1); /* t5 = y1^2 */ 1584 vli_modMult_fast(t4, X1, t5); /* t4 = x1*y1^2 = A */ 1585 vli_modSquare_fast(X1, X1); /* t1 = x1^2 */ 1586 vli_modSquare_fast(t5, t5); /* t5 = y1^4 */ 1587 vli_modMult_fast(Z1, Y1, Z1); /* t3 = y1*z1 = z3 */ 1588 1589 vli_modAdd(Y1, X1, X1, curve_p); /* t2 = 2*x1^2 */ 1590 vli_modAdd(Y1, Y1, X1, curve_p); /* t2 = 3*x1^2 */ 1591 if (vli_testBit(Y1, 0)) { 1592 uECC_word_t carry = vli_add(Y1, Y1, curve_p); 1593 vli_rshift1(Y1); 1594 Y1[uECC_WORDS - 1] |= carry << (uECC_WORD_BITS - 1); 1595 } else { 1596 vli_rshift1(Y1); 1597 } 1598 /* t2 = 3/2*(x1^2) = B */ 1599 1600 vli_modSquare_fast(X1, Y1); /* t1 = B^2 */ 1601 vli_modSub(X1, X1, t4, curve_p); /* t1 = B^2 - A */ 1602 vli_modSub(X1, X1, t4, curve_p); /* t1 = B^2 - 2A = x3 */ 1603 1604 vli_modSub(t4, t4, X1, curve_p); /* t4 = A - x3 */ 1605 vli_modMult_fast(Y1, Y1, t4); /* t2 = B * (A - x3) */ 1606 vli_modSub(Y1, Y1, t5, curve_p); /* t2 = B * (A - x3) - y1^4 = y3 */ 1607 } 1608 #else 1609 static void EccPoint_double_jacobian(uECC_word_t * RESTRICT X1, 1610 uECC_word_t * RESTRICT Y1, 1611 uECC_word_t * RESTRICT Z1) { 1612 /* t1 = X, t2 = Y, t3 = Z */ 1613 uECC_word_t t4[uECC_WORDS]; 1614 uECC_word_t t5[uECC_WORDS]; 1615 1616 if (vli_isZero(Z1)) { 1617 return; 1618 } 1619 1620 vli_modSquare_fast(t4, Y1); /* t4 = y1^2 */ 1621 vli_modMult_fast(t5, X1, t4); /* t5 = x1*y1^2 = A */ 1622 vli_modSquare_fast(t4, t4); /* t4 = y1^4 */ 1623 vli_modMult_fast(Y1, Y1, Z1); /* t2 = y1*z1 = z3 */ 1624 vli_modSquare_fast(Z1, Z1); /* t3 = z1^2 */ 1625 1626 vli_modAdd(X1, X1, Z1, curve_p); /* t1 = x1 + z1^2 */ 1627 vli_modAdd(Z1, Z1, Z1, curve_p); /* t3 = 2*z1^2 */ 1628 vli_modSub_fast(Z1, X1, Z1); /* t3 = x1 - z1^2 */ 1629 vli_modMult_fast(X1, X1, Z1); /* t1 = x1^2 - z1^4 */ 1630 1631 vli_modAdd(Z1, X1, X1, curve_p); /* t3 = 2*(x1^2 - z1^4) */ 1632 vli_modAdd(X1, X1, Z1, curve_p); /* t1 = 3*(x1^2 - z1^4) */ 1633 if (vli_testBit(X1, 0)) { 1634 uECC_word_t l_carry = vli_add(X1, X1, curve_p); 1635 vli_rshift1(X1); 1636 X1[uECC_WORDS - 1] |= l_carry << (uECC_WORD_BITS - 1); 1637 } else { 1638 vli_rshift1(X1); 1639 } 1640 /* t1 = 3/2*(x1^2 - z1^4) = B */ 1641 1642 vli_modSquare_fast(Z1, X1); /* t3 = B^2 */ 1643 vli_modSub_fast(Z1, Z1, t5); /* t3 = B^2 - A */ 1644 vli_modSub_fast(Z1, Z1, t5); /* t3 = B^2 - 2A = x3 */ 1645 vli_modSub_fast(t5, t5, Z1); /* t5 = A - x3 */ 1646 vli_modMult_fast(X1, X1, t5); /* t1 = B * (A - x3) */ 1647 vli_modSub_fast(t4, X1, t4); /* t4 = B * (A - x3) - y1^4 = y3 */ 1648 1649 vli_set(X1, Z1); 1650 vli_set(Z1, Y1); 1651 vli_set(Y1, t4); 1652 } 1653 #endif 1654 1655 /* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */ 1656 static void apply_z(uECC_word_t * RESTRICT X1, 1657 uECC_word_t * RESTRICT Y1, 1658 const uECC_word_t * RESTRICT Z) { 1659 uECC_word_t t1[uECC_WORDS]; 1660 1661 vli_modSquare_fast(t1, Z); /* z^2 */ 1662 vli_modMult_fast(X1, X1, t1); /* x1 * z^2 */ 1663 vli_modMult_fast(t1, t1, Z); /* z^3 */ 1664 vli_modMult_fast(Y1, Y1, t1); /* y1 * z^3 */ 1665 } 1666 1667 /* P = (x1, y1) => 2P, (x2, y2) => P' */ 1668 static void XYcZ_initial_double(uECC_word_t * RESTRICT X1, 1669 uECC_word_t * RESTRICT Y1, 1670 uECC_word_t * RESTRICT X2, 1671 uECC_word_t * RESTRICT Y2, 1672 const uECC_word_t * RESTRICT initial_Z) { 1673 uECC_word_t z[uECC_WORDS]; 1674 if (initial_Z) { 1675 vli_set(z, initial_Z); 1676 } else { 1677 vli_clear(z); 1678 z[0] = 1; 1679 } 1680 1681 vli_set(X2, X1); 1682 vli_set(Y2, Y1); 1683 1684 apply_z(X1, Y1, z); 1685 EccPoint_double_jacobian(X1, Y1, z); 1686 apply_z(X2, Y2, z); 1687 } 1688 1689 /* Input P = (x1, y1, Z), Q = (x2, y2, Z) 1690 Output P' = (x1', y1', Z3), P + Q = (x3, y3, Z3) 1691 or P => P', Q => P + Q 1692 */ 1693 static void XYcZ_add(uECC_word_t * RESTRICT X1, 1694 uECC_word_t * RESTRICT Y1, 1695 uECC_word_t * RESTRICT X2, 1696 uECC_word_t * RESTRICT Y2) { 1697 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */ 1698 uECC_word_t t5[uECC_WORDS]; 1699 1700 vli_modSub_fast(t5, X2, X1); /* t5 = x2 - x1 */ 1701 vli_modSquare_fast(t5, t5); /* t5 = (x2 - x1)^2 = A */ 1702 vli_modMult_fast(X1, X1, t5); /* t1 = x1*A = B */ 1703 vli_modMult_fast(X2, X2, t5); /* t3 = x2*A = C */ 1704 vli_modSub_fast(Y2, Y2, Y1); /* t4 = y2 - y1 */ 1705 vli_modSquare_fast(t5, Y2); /* t5 = (y2 - y1)^2 = D */ 1706 1707 vli_modSub_fast(t5, t5, X1); /* t5 = D - B */ 1708 vli_modSub_fast(t5, t5, X2); /* t5 = D - B - C = x3 */ 1709 vli_modSub_fast(X2, X2, X1); /* t3 = C - B */ 1710 vli_modMult_fast(Y1, Y1, X2); /* t2 = y1*(C - B) */ 1711 vli_modSub_fast(X2, X1, t5); /* t3 = B - x3 */ 1712 vli_modMult_fast(Y2, Y2, X2); /* t4 = (y2 - y1)*(B - x3) */ 1713 vli_modSub_fast(Y2, Y2, Y1); /* t4 = y3 */ 1714 1715 vli_set(X2, t5); 1716 } 1717 1718 /* Input P = (x1, y1, Z), Q = (x2, y2, Z) 1719 Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3) 1720 or P => P - Q, Q => P + Q 1721 */ 1722 static void XYcZ_addC(uECC_word_t * RESTRICT X1, 1723 uECC_word_t * RESTRICT Y1, 1724 uECC_word_t * RESTRICT X2, 1725 uECC_word_t * RESTRICT Y2) { 1726 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */ 1727 uECC_word_t t5[uECC_WORDS]; 1728 uECC_word_t t6[uECC_WORDS]; 1729 uECC_word_t t7[uECC_WORDS]; 1730 1731 vli_modSub_fast(t5, X2, X1); /* t5 = x2 - x1 */ 1732 vli_modSquare_fast(t5, t5); /* t5 = (x2 - x1)^2 = A */ 1733 vli_modMult_fast(X1, X1, t5); /* t1 = x1*A = B */ 1734 vli_modMult_fast(X2, X2, t5); /* t3 = x2*A = C */ 1735 vli_modAdd(t5, Y2, Y1, curve_p); /* t5 = y2 + y1 */ 1736 vli_modSub_fast(Y2, Y2, Y1); /* t4 = y2 - y1 */ 1737 1738 vli_modSub_fast(t6, X2, X1); /* t6 = C - B */ 1739 vli_modMult_fast(Y1, Y1, t6); /* t2 = y1 * (C - B) = E */ 1740 vli_modAdd(t6, X1, X2, curve_p); /* t6 = B + C */ 1741 vli_modSquare_fast(X2, Y2); /* t3 = (y2 - y1)^2 = D */ 1742 vli_modSub_fast(X2, X2, t6); /* t3 = D - (B + C) = x3 */ 1743 1744 vli_modSub_fast(t7, X1, X2); /* t7 = B - x3 */ 1745 vli_modMult_fast(Y2, Y2, t7); /* t4 = (y2 - y1)*(B - x3) */ 1746 vli_modSub_fast(Y2, Y2, Y1); /* t4 = (y2 - y1)*(B - x3) - E = y3 */ 1747 1748 vli_modSquare_fast(t7, t5); /* t7 = (y2 + y1)^2 = F */ 1749 vli_modSub_fast(t7, t7, t6); /* t7 = F - (B + C) = x3' */ 1750 vli_modSub_fast(t6, t7, X1); /* t6 = x3' - B */ 1751 vli_modMult_fast(t6, t6, t5); /* t6 = (y2 + y1)*(x3' - B) */ 1752 vli_modSub_fast(Y1, t6, Y1); /* t2 = (y2 + y1)*(x3' - B) - E = y3' */ 1753 1754 vli_set(X1, t7); 1755 } 1756 1757 static void EccPoint_mult(EccPoint * RESTRICT result, 1758 const EccPoint * RESTRICT point, 1759 const uECC_word_t * RESTRICT scalar, 1760 const uECC_word_t * RESTRICT initialZ, 1761 bitcount_t numBits) { 1762 /* R0 and R1 */ 1763 uECC_word_t Rx[2][uECC_WORDS]; 1764 uECC_word_t Ry[2][uECC_WORDS]; 1765 uECC_word_t z[uECC_WORDS]; 1766 bitcount_t i; 1767 uECC_word_t nb; 1768 1769 vli_set(Rx[1], point->x); 1770 vli_set(Ry[1], point->y); 1771 1772 XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], initialZ); 1773 1774 for (i = numBits - 2; i > 0; --i) { 1775 nb = !vli_testBit(scalar, i); 1776 XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb]); 1777 XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb]); 1778 } 1779 1780 nb = !vli_testBit(scalar, 0); 1781 XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb]); 1782 1783 /* Find final 1/Z value. */ 1784 vli_modSub_fast(z, Rx[1], Rx[0]); /* X1 - X0 */ 1785 vli_modMult_fast(z, z, Ry[1 - nb]); /* Yb * (X1 - X0) */ 1786 vli_modMult_fast(z, z, point->x); /* xP * Yb * (X1 - X0) */ 1787 vli_modInv(z, z, curve_p); /* 1 / (xP * Yb * (X1 - X0)) */ 1788 vli_modMult_fast(z, z, point->y); /* yP / (xP * Yb * (X1 - X0)) */ 1789 vli_modMult_fast(z, z, Rx[1 - nb]); /* Xb * yP / (xP * Yb * (X1 - X0)) */ 1790 /* End 1/Z calculation */ 1791 1792 XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb]); 1793 apply_z(Rx[0], Ry[0], z); 1794 1795 vli_set(result->x, Rx[0]); 1796 vli_set(result->y, Ry[0]); 1797 } 1798 1799 static int EccPoint_compute_public_key(EccPoint *result, uECC_word_t *private) { 1800 uECC_word_t tmp1[uECC_WORDS]; 1801 uECC_word_t tmp2[uECC_WORDS]; 1802 uECC_word_t *p2[2] = {tmp1, tmp2}; 1803 uECC_word_t carry; 1804 1805 /* Make sure the private key is in the range [1, n-1]. */ 1806 if (vli_isZero(private)) { 1807 return 0; 1808 } 1809 1810 #if (uECC_CURVE == uECC_secp160r1) 1811 // Don't regularize the bitcount for secp160r1, since it would have a larger performance 1812 // impact (about 2% slower on average) and requires the vli_xxx_n functions, leading to 1813 // a significant increase in code size. 1814 1815 EccPoint_mult(result, &curve_G, private, NULL, vli_numBits(private, uECC_WORDS)); 1816 #else 1817 if (vli_cmp(curve_n, private) != 1) { 1818 return 0; 1819 } 1820 1821 // Regularize the bitcount for the private key so that attackers cannot use a side channel 1822 // attack to learn the number of leading zeros. 1823 carry = vli_add(tmp1, private, curve_n); 1824 vli_add(tmp2, tmp1, curve_n); 1825 EccPoint_mult(result, &curve_G, p2[!carry], NULL, (uECC_BYTES * 8) + 1); 1826 #endif 1827 1828 if (EccPoint_isZero(result)) { 1829 return 0; 1830 } 1831 return 1; 1832 } 1833 1834 #ifdef ENABLE_MICRO_ECC_COMPRESSION 1835 1836 #if uECC_CURVE == uECC_secp224r1 1837 1838 /* Routine 3.2.4 RS; from http://www.nsa.gov/ia/_files/nist-routines.pdf */ 1839 static void mod_sqrt_secp224r1_rs(uECC_word_t *d1, 1840 uECC_word_t *e1, 1841 uECC_word_t *f1, 1842 const uECC_word_t *d0, 1843 const uECC_word_t *e0, 1844 const uECC_word_t *f0) { 1845 uECC_word_t t[uECC_WORDS]; 1846 1847 vli_modSquare_fast(t, d0); /* t <-- d0 ^ 2 */ 1848 vli_modMult_fast(e1, d0, e0); /* e1 <-- d0 * e0 */ 1849 vli_modAdd(d1, t, f0, curve_p); /* d1 <-- t + f0 */ 1850 vli_modAdd(e1, e1, e1, curve_p); /* e1 <-- e1 + e1 */ 1851 vli_modMult_fast(f1, t, f0); /* f1 <-- t * f0 */ 1852 vli_modAdd(f1, f1, f1, curve_p); /* f1 <-- f1 + f1 */ 1853 vli_modAdd(f1, f1, f1, curve_p); /* f1 <-- f1 + f1 */ 1854 } 1855 1856 /* Routine 3.2.5 RSS; from http://www.nsa.gov/ia/_files/nist-routines.pdf */ 1857 static void mod_sqrt_secp224r1_rss(uECC_word_t *d1, 1858 uECC_word_t *e1, 1859 uECC_word_t *f1, 1860 const uECC_word_t *d0, 1861 const uECC_word_t *e0, 1862 const uECC_word_t *f0, 1863 const bitcount_t j) { 1864 bitcount_t i; 1865 1866 vli_set(d1, d0); /* d1 <-- d0 */ 1867 vli_set(e1, e0); /* e1 <-- e0 */ 1868 vli_set(f1, f0); /* f1 <-- f0 */ 1869 for (i = 1; i <= j; i++) { 1870 mod_sqrt_secp224r1_rs(d1, e1, f1, d1, e1, f1); /* RS (d1,e1,f1,d1,e1,f1) */ 1871 } 1872 } 1873 1874 /* Routine 3.2.6 RM; from http://www.nsa.gov/ia/_files/nist-routines.pdf */ 1875 static void mod_sqrt_secp224r1_rm(uECC_word_t *d2, 1876 uECC_word_t *e2, 1877 uECC_word_t *f2, 1878 const uECC_word_t *c, 1879 const uECC_word_t *d0, 1880 const uECC_word_t *e0, 1881 const uECC_word_t *d1, 1882 const uECC_word_t *e1) { 1883 uECC_word_t t1[uECC_WORDS]; 1884 uECC_word_t t2[uECC_WORDS]; 1885 1886 vli_modMult_fast(t1, e0, e1); /* t1 <-- e0 * e1 */ 1887 vli_modMult_fast(t1, t1, c); /* t1 <-- t1 * c */ 1888 vli_modSub_fast(t1, curve_p, t1); /* t1 <-- p - t1 */ 1889 vli_modMult_fast(t2, d0, d1); /* t2 <-- d0 * d1 */ 1890 vli_modAdd(t2, t2, t1, curve_p); /* t2 <-- t2 + t1 */ 1891 vli_modMult_fast(t1, d0, e1); /* t1 <-- d0 * e1 */ 1892 vli_modMult_fast(e2, d1, e0); /* e2 <-- d1 * e0 */ 1893 vli_modAdd(e2, e2, t1, curve_p); /* e2 <-- e2 + t1 */ 1894 vli_modSquare_fast(f2, e2); /* f2 <-- e2^2 */ 1895 vli_modMult_fast(f2, f2, c); /* f2 <-- f2 * c */ 1896 vli_modSub_fast(f2, curve_p, f2); /* f2 <-- p - f2 */ 1897 vli_set(d2, t2); /* d2 <-- t2 */ 1898 } 1899 1900 /* Routine 3.2.7 RP; from http://www.nsa.gov/ia/_files/nist-routines.pdf */ 1901 static void mod_sqrt_secp224r1_rp(uECC_word_t *d1, 1902 uECC_word_t *e1, 1903 uECC_word_t *f1, 1904 const uECC_word_t *c, 1905 const uECC_word_t *r) { 1906 wordcount_t i; 1907 wordcount_t pow2i = 1; 1908 uECC_word_t d0[uECC_WORDS]; 1909 uECC_word_t e0[uECC_WORDS] = {1}; /* e0 <-- 1 */ 1910 uECC_word_t f0[uECC_WORDS]; 1911 1912 vli_set(d0, r); /* d0 <-- r */ 1913 vli_modSub_fast(f0, curve_p, c); /* f0 <-- p - c */ 1914 for (i = 0; i <= 6; i++) { 1915 mod_sqrt_secp224r1_rss(d1, e1, f1, d0, e0, f0, pow2i); /* RSS (d1,e1,f1,d0,e0,f0,2^i) */ 1916 mod_sqrt_secp224r1_rm(d1, e1, f1, c, d1, e1, d0, e0); /* RM (d1,e1,f1,c,d1,e1,d0,e0) */ 1917 vli_set(d0, d1); /* d0 <-- d1 */ 1918 vli_set(e0, e1); /* e0 <-- e1 */ 1919 vli_set(f0, f1); /* f0 <-- f1 */ 1920 pow2i *= 2; 1921 } 1922 } 1923 1924 /* Compute a = sqrt(a) (mod curve_p). */ 1925 /* Routine 3.2.8 mp_mod_sqrt_224; from http://www.nsa.gov/ia/_files/nist-routines.pdf */ 1926 static void mod_sqrt(uECC_word_t *a) { 1927 bitcount_t i; 1928 uECC_word_t e1[uECC_WORDS]; 1929 uECC_word_t f1[uECC_WORDS]; 1930 uECC_word_t d0[uECC_WORDS]; 1931 uECC_word_t e0[uECC_WORDS]; 1932 uECC_word_t f0[uECC_WORDS]; 1933 uECC_word_t d1[uECC_WORDS]; 1934 1935 // s = a; using constant instead of random value 1936 mod_sqrt_secp224r1_rp(d0, e0, f0, a, a); /* RP (d0, e0, f0, c, s) */ 1937 mod_sqrt_secp224r1_rs(d1, e1, f1, d0, e0, f0); /* RS (d1, e1, f1, d0, e0, f0) */ 1938 for (i = 1; i <= 95; i++) { 1939 vli_set(d0, d1); /* d0 <-- d1 */ 1940 vli_set(e0, e1); /* e0 <-- e1 */ 1941 vli_set(f0, f1); /* f0 <-- f1 */ 1942 mod_sqrt_secp224r1_rs(d1, e1, f1, d0, e0, f0); /* RS (d1, e1, f1, d0, e0, f0) */ 1943 if (vli_isZero(d1)) { /* if d1 == 0 */ 1944 break; 1945 } 1946 } 1947 vli_modInv(f1, e0, curve_p); /* f1 <-- 1 / e0 */ 1948 vli_modMult_fast(a, d0, f1); /* a <-- d0 / e0 */ 1949 } 1950 1951 #else /* uECC_CURVE */ 1952 1953 /* Compute a = sqrt(a) (mod curve_p). */ 1954 static void mod_sqrt(uECC_word_t *a) { 1955 bitcount_t i; 1956 uECC_word_t p1[uECC_WORDS] = {1}; 1957 uECC_word_t l_result[uECC_WORDS] = {1}; 1958 1959 /* Since curve_p == 3 (mod 4) for all supported curves, we can 1960 compute sqrt(a) = a^((curve_p + 1) / 4) (mod curve_p). */ 1961 vli_add(p1, curve_p, p1); /* p1 = curve_p + 1 */ 1962 for (i = vli_numBits(p1, uECC_WORDS) - 1; i > 1; --i) { 1963 vli_modSquare_fast(l_result, l_result); 1964 if (vli_testBit(p1, i)) { 1965 vli_modMult_fast(l_result, l_result, a); 1966 } 1967 } 1968 vli_set(a, l_result); 1969 } 1970 #endif /* uECC_CURVE */ 1971 1972 #endif /* ENABLE_MICRO_ECC_COMPRESSION */ 1973 1974 1975 #if uECC_WORD_SIZE == 1 1976 1977 static void vli_nativeToBytes(uint8_t * RESTRICT dest, const uint8_t * RESTRICT src) { 1978 uint8_t i; 1979 for (i = 0; i < uECC_BYTES; ++i) { 1980 dest[i] = src[(uECC_BYTES - 1) - i]; 1981 } 1982 } 1983 1984 #define vli_bytesToNative(dest, src) vli_nativeToBytes((dest), (src)) 1985 1986 #elif uECC_WORD_SIZE == 4 1987 1988 static void vli_nativeToBytes(uint8_t *bytes, const uint32_t *native) { 1989 unsigned i; 1990 for (i = 0; i < uECC_WORDS; ++i) { 1991 uint8_t *digit = bytes + 4 * (uECC_WORDS - 1 - i); 1992 digit[0] = native[i] >> 24; 1993 digit[1] = native[i] >> 16; 1994 digit[2] = native[i] >> 8; 1995 digit[3] = native[i]; 1996 } 1997 } 1998 1999 static void vli_bytesToNative(uint32_t *native, const uint8_t *bytes) { 2000 unsigned i; 2001 for (i = 0; i < uECC_WORDS; ++i) { 2002 const uint8_t *digit = bytes + 4 * (uECC_WORDS - 1 - i); 2003 native[i] = ((uint32_t)digit[0] << 24) | ((uint32_t)digit[1] << 16) | 2004 ((uint32_t)digit[2] << 8) | (uint32_t)digit[3]; 2005 } 2006 } 2007 2008 #else 2009 2010 static void vli_nativeToBytes(uint8_t *bytes, const uint64_t *native) { 2011 unsigned i; 2012 for (i = 0; i < uECC_WORDS; ++i) { 2013 uint8_t *digit = bytes + 8 * (uECC_WORDS - 1 - i); 2014 digit[0] = native[i] >> 56; 2015 digit[1] = native[i] >> 48; 2016 digit[2] = native[i] >> 40; 2017 digit[3] = native[i] >> 32; 2018 digit[4] = native[i] >> 24; 2019 digit[5] = native[i] >> 16; 2020 digit[6] = native[i] >> 8; 2021 digit[7] = native[i]; 2022 } 2023 } 2024 2025 static void vli_bytesToNative(uint64_t *native, const uint8_t *bytes) { 2026 unsigned i; 2027 for (i = 0; i < uECC_WORDS; ++i) { 2028 const uint8_t *digit = bytes + 8 * (uECC_WORDS - 1 - i); 2029 native[i] = ((uint64_t)digit[0] << 56) | ((uint64_t)digit[1] << 48) | 2030 ((uint64_t)digit[2] << 40) | ((uint64_t)digit[3] << 32) | 2031 ((uint64_t)digit[4] << 24) | ((uint64_t)digit[5] << 16) | 2032 ((uint64_t)digit[6] << 8) | (uint64_t)digit[7]; 2033 } 2034 } 2035 2036 #endif /* uECC_WORD_SIZE */ 2037 2038 int uECC_make_key(uint8_t public_key[uECC_BYTES*2], uint8_t private_key[uECC_BYTES]) { 2039 uECC_word_t private[uECC_WORDS]; 2040 EccPoint public; 2041 uECC_word_t tries; 2042 for (tries = 0; tries < MAX_TRIES; ++tries) { 2043 if (g_rng_function((uint8_t *)private, sizeof(private)) && 2044 EccPoint_compute_public_key(&public, private)) { 2045 vli_nativeToBytes(private_key, private); 2046 vli_nativeToBytes(public_key, public.x); 2047 vli_nativeToBytes(public_key + uECC_BYTES, public.y); 2048 return 1; 2049 } 2050 } 2051 return 0; 2052 } 2053 2054 int uECC_shared_secret(const uint8_t public_key[uECC_BYTES*2], 2055 const uint8_t private_key[uECC_BYTES], 2056 uint8_t secret[uECC_BYTES]) { 2057 EccPoint public; 2058 EccPoint product; 2059 uECC_word_t private[uECC_WORDS]; 2060 uECC_word_t tmp[uECC_WORDS]; 2061 uECC_word_t *p2[2] = {private, tmp}; 2062 uECC_word_t random[uECC_WORDS]; 2063 uECC_word_t *initial_Z = NULL; 2064 uECC_word_t tries; 2065 uECC_word_t carry; 2066 2067 // Try to get a random initial Z value to improve protection against side-channel 2068 // attacks. If the RNG fails every time (eg it was not defined), we continue so that 2069 // uECC_shared_secret() can still work without an RNG defined. 2070 for (tries = 0; tries < MAX_TRIES; ++tries) { 2071 if (g_rng_function((uint8_t *)random, sizeof(random)) && !vli_isZero(random)) { 2072 initial_Z = random; 2073 break; 2074 } 2075 } 2076 2077 vli_bytesToNative(private, private_key); 2078 vli_bytesToNative(public.x, public_key); 2079 vli_bytesToNative(public.y, public_key + uECC_BYTES); 2080 2081 #if (uECC_CURVE == uECC_secp160r1) 2082 // Don't regularize the bitcount for secp160r1. 2083 EccPoint_mult(&product, &public, private, initial_Z, vli_numBits(private, uECC_WORDS)); 2084 #else 2085 // Regularize the bitcount for the private key so that attackers cannot use a side channel 2086 // attack to learn the number of leading zeros. 2087 carry = vli_add(private, private, curve_n); 2088 vli_add(tmp, private, curve_n); 2089 EccPoint_mult(&product, &public, p2[!carry], initial_Z, (uECC_BYTES * 8) + 1); 2090 #endif 2091 2092 vli_nativeToBytes(secret, product.x); 2093 return !EccPoint_isZero(&product); 2094 } 2095 2096 #ifdef ENABLE_MICRO_ECC_COMPRESSION 2097 2098 void uECC_compress(const uint8_t public_key[uECC_BYTES*2], uint8_t compressed[uECC_BYTES+1]) { 2099 wordcount_t i; 2100 for (i = 0; i < uECC_BYTES; ++i) { 2101 compressed[i+1] = public_key[i]; 2102 } 2103 compressed[0] = 2 + (public_key[uECC_BYTES * 2 - 1] & 0x01); 2104 } 2105 2106 #endif 2107 2108 /* Computes result = x^3 + ax + b. result must not overlap x. */ 2109 static void curve_x_side(uECC_word_t * RESTRICT result, const uECC_word_t * RESTRICT x) { 2110 static const uECC_word_t curve_b[uECC_WORDS] = uECC_CONCAT(Curve_B_, uECC_CURVE); 2111 #if (uECC_CURVE == uECC_secp256k1) 2112 vli_modSquare_fast(result, x); /* r = x^2 */ 2113 vli_modMult_fast(result, result, x); /* r = x^3 */ 2114 vli_modAdd(result, result, curve_b, curve_p); /* r = x^3 + b */ 2115 #else 2116 uECC_word_t _3[uECC_WORDS] = {3}; /* -a = 3 */ 2117 2118 vli_modSquare_fast(result, x); /* r = x^2 */ 2119 vli_modSub_fast(result, result, _3); /* r = x^2 - 3 */ 2120 vli_modMult_fast(result, result, x); /* r = x^3 - 3x */ 2121 vli_modAdd(result, result, curve_b, curve_p); /* r = x^3 - 3x + b */ 2122 #endif 2123 } 2124 2125 #ifdef ENABLE_MICRO_ECC_COMPRESSION 2126 2127 void uECC_decompress(const uint8_t compressed[uECC_BYTES+1], uint8_t public_key[uECC_BYTES*2]) { 2128 EccPoint point; 2129 vli_bytesToNative(point.x, compressed + 1); 2130 curve_x_side(point.y, point.x); 2131 mod_sqrt(point.y); 2132 2133 if ((point.y[0] & 0x01) != (compressed[0] & 0x01)) { 2134 vli_sub(point.y, curve_p, point.y); 2135 } 2136 2137 vli_nativeToBytes(public_key, point.x); 2138 vli_nativeToBytes(public_key + uECC_BYTES, point.y); 2139 } 2140 2141 #endif /* ENABLE_MICRO_ECC_COMPRESSION */ 2142 2143 int uECC_valid_public_key(const uint8_t public_key[uECC_BYTES*2]) { 2144 uECC_word_t tmp1[uECC_WORDS]; 2145 uECC_word_t tmp2[uECC_WORDS]; 2146 EccPoint public; 2147 2148 vli_bytesToNative(public.x, public_key); 2149 vli_bytesToNative(public.y, public_key + uECC_BYTES); 2150 2151 // The point at infinity is invalid. 2152 if (EccPoint_isZero(&public)) { 2153 return 0; 2154 } 2155 2156 // x and y must be smaller than p. 2157 if (vli_cmp(curve_p, public.x) != 1 || vli_cmp(curve_p, public.y) != 1) { 2158 return 0; 2159 } 2160 2161 vli_modSquare_fast(tmp1, public.y); /* tmp1 = y^2 */ 2162 curve_x_side(tmp2, public.x); /* tmp2 = x^3 + ax + b */ 2163 2164 /* Make sure that y^2 == x^3 + ax + b */ 2165 return (vli_cmp(tmp1, tmp2) == 0); 2166 } 2167 2168 int uECC_compute_public_key(const uint8_t private_key[uECC_BYTES], 2169 uint8_t public_key[uECC_BYTES * 2]) { 2170 uECC_word_t private[uECC_WORDS]; 2171 EccPoint public; 2172 2173 vli_bytesToNative(private, private_key); 2174 2175 if (!EccPoint_compute_public_key(&public, private)) { 2176 return 0; 2177 } 2178 2179 vli_nativeToBytes(public_key, public.x); 2180 vli_nativeToBytes(public_key + uECC_BYTES, public.y); 2181 return 1; 2182 } 2183 2184 int uECC_bytes(void) { 2185 return uECC_BYTES; 2186 } 2187 2188 int uECC_curve(void) { 2189 return uECC_CURVE; 2190 } 2191 2192 /* -------- ECDSA code -------- */ 2193 2194 #ifdef ENABLE_MICRO_ECC_ECDSA 2195 2196 #if (uECC_CURVE == uECC_secp160r1) 2197 static void vli_clear_n(uECC_word_t *vli) { 2198 vli_clear(vli); 2199 vli[uECC_N_WORDS - 1] = 0; 2200 } 2201 2202 static uECC_word_t vli_isZero_n(const uECC_word_t *vli) { 2203 if (vli[uECC_N_WORDS - 1]) { 2204 return 0; 2205 } 2206 return vli_isZero(vli); 2207 } 2208 2209 static void vli_set_n(uECC_word_t *dest, const uECC_word_t *src) { 2210 vli_set(dest, src); 2211 dest[uECC_N_WORDS - 1] = src[uECC_N_WORDS - 1]; 2212 } 2213 2214 static cmpresult_t vli_cmp_n(const uECC_word_t *left, const uECC_word_t *right) { 2215 if (left[uECC_N_WORDS - 1] > right[uECC_N_WORDS - 1]) { 2216 return 1; 2217 } else if (left[uECC_N_WORDS - 1] < right[uECC_N_WORDS - 1]) { 2218 return -1; 2219 } 2220 return vli_cmp(left, right); 2221 } 2222 2223 static void vli_rshift1_n(uECC_word_t *vli) { 2224 vli_rshift1(vli); 2225 vli[uECC_N_WORDS - 2] |= vli[uECC_N_WORDS - 1] << (uECC_WORD_BITS - 1); 2226 vli[uECC_N_WORDS - 1] = vli[uECC_N_WORDS - 1] >> 1; 2227 } 2228 2229 static uECC_word_t vli_add_n(uECC_word_t *result, 2230 const uECC_word_t *left, 2231 const uECC_word_t *right) { 2232 uECC_word_t carry = vli_add(result, left, right); 2233 uECC_word_t sum = left[uECC_N_WORDS - 1] + right[uECC_N_WORDS - 1] + carry; 2234 if (sum != left[uECC_N_WORDS - 1]) { 2235 carry = (sum < left[uECC_N_WORDS - 1]); 2236 } 2237 result[uECC_N_WORDS - 1] = sum; 2238 return carry; 2239 } 2240 2241 static uECC_word_t vli_sub_n(uECC_word_t *result, 2242 const uECC_word_t *left, 2243 const uECC_word_t *right) { 2244 uECC_word_t borrow = vli_sub(result, left, right); 2245 uECC_word_t diff = left[uECC_N_WORDS - 1] - right[uECC_N_WORDS - 1] - borrow; 2246 if (diff != left[uECC_N_WORDS - 1]) { 2247 borrow = (diff > left[uECC_N_WORDS - 1]); 2248 } 2249 result[uECC_N_WORDS - 1] = diff; 2250 return borrow; 2251 } 2252 2253 #if !muladd_exists 2254 static void muladd(uECC_word_t a, 2255 uECC_word_t b, 2256 uECC_word_t *r0, 2257 uECC_word_t *r1, 2258 uECC_word_t *r2) { 2259 uECC_dword_t p = (uECC_dword_t)a * b; 2260 uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0; 2261 r01 += p; 2262 *r2 += (r01 < p); 2263 *r1 = r01 >> uECC_WORD_BITS; 2264 *r0 = (uECC_word_t)r01; 2265 } 2266 #define muladd_exists 1 2267 #endif 2268 2269 static void vli_mult_n(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { 2270 uECC_word_t r0 = 0; 2271 uECC_word_t r1 = 0; 2272 uECC_word_t r2 = 0; 2273 wordcount_t i, k; 2274 2275 for (k = 0; k < uECC_N_WORDS * 2 - 1; ++k) { 2276 wordcount_t min = (k < uECC_N_WORDS ? 0 : (k + 1) - uECC_N_WORDS); 2277 wordcount_t max = (k < uECC_N_WORDS ? k : uECC_N_WORDS - 1); 2278 for (i = min; i <= max; ++i) { 2279 muladd(left[i], right[k - i], &r0, &r1, &r2); 2280 } 2281 result[k] = r0; 2282 r0 = r1; 2283 r1 = r2; 2284 r2 = 0; 2285 } 2286 result[uECC_N_WORDS * 2 - 1] = r0; 2287 } 2288 2289 static void vli_modAdd_n(uECC_word_t *result, 2290 const uECC_word_t *left, 2291 const uECC_word_t *right, 2292 const uECC_word_t *mod) { 2293 uECC_word_t carry = vli_add_n(result, left, right); 2294 if (carry || vli_cmp_n(result, mod) >= 0) { 2295 vli_sub_n(result, result, mod); 2296 } 2297 } 2298 2299 static void vli_modInv_n(uECC_word_t *result, const uECC_word_t *input, const uECC_word_t *mod) { 2300 uECC_word_t a[uECC_N_WORDS], b[uECC_N_WORDS], u[uECC_N_WORDS], v[uECC_N_WORDS]; 2301 uECC_word_t carry; 2302 cmpresult_t cmpResult; 2303 2304 if (vli_isZero_n(input)) { 2305 vli_clear_n(result); 2306 return; 2307 } 2308 2309 vli_set_n(a, input); 2310 vli_set_n(b, mod); 2311 vli_clear_n(u); 2312 u[0] = 1; 2313 vli_clear_n(v); 2314 while ((cmpResult = vli_cmp_n(a, b)) != 0) { 2315 carry = 0; 2316 if (EVEN(a)) { 2317 vli_rshift1_n(a); 2318 if (!EVEN(u)) { 2319 carry = vli_add_n(u, u, mod); 2320 } 2321 vli_rshift1_n(u); 2322 if (carry) { 2323 u[uECC_N_WORDS - 1] |= HIGH_BIT_SET; 2324 } 2325 } else if (EVEN(b)) { 2326 vli_rshift1_n(b); 2327 if (!EVEN(v)) { 2328 carry = vli_add_n(v, v, mod); 2329 } 2330 vli_rshift1_n(v); 2331 if (carry) { 2332 v[uECC_N_WORDS - 1] |= HIGH_BIT_SET; 2333 } 2334 } else if (cmpResult > 0) { 2335 vli_sub_n(a, a, b); 2336 vli_rshift1_n(a); 2337 if (vli_cmp_n(u, v) < 0) { 2338 vli_add_n(u, u, mod); 2339 } 2340 vli_sub_n(u, u, v); 2341 if (!EVEN(u)) { 2342 carry = vli_add_n(u, u, mod); 2343 } 2344 vli_rshift1_n(u); 2345 if (carry) { 2346 u[uECC_N_WORDS - 1] |= HIGH_BIT_SET; 2347 } 2348 } else { 2349 vli_sub_n(b, b, a); 2350 vli_rshift1_n(b); 2351 if (vli_cmp_n(v, u) < 0) { 2352 vli_add_n(v, v, mod); 2353 } 2354 vli_sub_n(v, v, u); 2355 if (!EVEN(v)) { 2356 carry = vli_add_n(v, v, mod); 2357 } 2358 vli_rshift1_n(v); 2359 if (carry) { 2360 v[uECC_N_WORDS - 1] |= HIGH_BIT_SET; 2361 } 2362 } 2363 } 2364 vli_set_n(result, u); 2365 } 2366 2367 static void vli2_rshift1_n(uECC_word_t *vli) { 2368 vli_rshift1_n(vli); 2369 vli[uECC_N_WORDS - 1] |= vli[uECC_N_WORDS] << (uECC_WORD_BITS - 1); 2370 vli_rshift1_n(vli + uECC_N_WORDS); 2371 } 2372 2373 static uECC_word_t vli2_sub_n(uECC_word_t *result, 2374 const uECC_word_t *left, 2375 const uECC_word_t *right) { 2376 uECC_word_t borrow = 0; 2377 wordcount_t i; 2378 for (i = 0; i < uECC_N_WORDS * 2; ++i) { 2379 uECC_word_t diff = left[i] - right[i] - borrow; 2380 if (diff != left[i]) { 2381 borrow = (diff > left[i]); 2382 } 2383 result[i] = diff; 2384 } 2385 return borrow; 2386 } 2387 2388 /* Computes result = (left * right) % curve_n. */ 2389 static void vli_modMult_n(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { 2390 bitcount_t i; 2391 uECC_word_t product[2 * uECC_N_WORDS]; 2392 uECC_word_t modMultiple[2 * uECC_N_WORDS]; 2393 uECC_word_t tmp[2 * uECC_N_WORDS]; 2394 uECC_word_t *v[2] = {tmp, product}; 2395 uECC_word_t index = 1; 2396 2397 vli_mult_n(product, left, right); 2398 vli_clear_n(modMultiple); 2399 vli_set(modMultiple + uECC_N_WORDS + 1, curve_n); 2400 vli_rshift1(modMultiple + uECC_N_WORDS + 1); 2401 modMultiple[2 * uECC_N_WORDS - 1] |= HIGH_BIT_SET; 2402 modMultiple[uECC_N_WORDS] = HIGH_BIT_SET; 2403 2404 for (i = 0; 2405 i <= ((((bitcount_t)uECC_N_WORDS) << uECC_WORD_BITS_SHIFT) + (uECC_WORD_BITS - 1)); 2406 ++i) { 2407 uECC_word_t borrow = vli2_sub_n(v[1 - index], v[index], modMultiple); 2408 index = !(index ^ borrow); /* Swap the index if there was no borrow */ 2409 vli2_rshift1_n(modMultiple); 2410 } 2411 vli_set_n(result, v[index]); 2412 } 2413 2414 #else 2415 2416 #define vli_cmp_n vli_cmp 2417 #define vli_modInv_n vli_modInv 2418 #define vli_modAdd_n vli_modAdd 2419 2420 static void vli2_rshift1(uECC_word_t *vli) { 2421 vli_rshift1(vli); 2422 vli[uECC_WORDS - 1] |= vli[uECC_WORDS] << (uECC_WORD_BITS - 1); 2423 vli_rshift1(vli + uECC_WORDS); 2424 } 2425 2426 static uECC_word_t vli2_sub(uECC_word_t *result, 2427 const uECC_word_t *left, 2428 const uECC_word_t *right) { 2429 uECC_word_t borrow = 0; 2430 wordcount_t i; 2431 for (i = 0; i < uECC_WORDS * 2; ++i) { 2432 uECC_word_t diff = left[i] - right[i] - borrow; 2433 if (diff != left[i]) { 2434 borrow = (diff > left[i]); 2435 } 2436 result[i] = diff; 2437 } 2438 return borrow; 2439 } 2440 2441 /* Computes result = (left * right) % curve_n. */ 2442 static void vli_modMult_n(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { 2443 uECC_word_t product[2 * uECC_WORDS]; 2444 uECC_word_t modMultiple[2 * uECC_WORDS]; 2445 uECC_word_t tmp[2 * uECC_WORDS]; 2446 uECC_word_t *v[2] = {tmp, product}; 2447 bitcount_t i; 2448 uECC_word_t index = 1; 2449 2450 vli_mult(product, left, right); 2451 vli_set(modMultiple + uECC_WORDS, curve_n); /* works if curve_n has its highest bit set */ 2452 vli_clear(modMultiple); 2453 2454 for (i = 0; i <= uECC_BYTES * 8; ++i) { 2455 uECC_word_t borrow = vli2_sub(v[1 - index], v[index], modMultiple); 2456 index = !(index ^ borrow); /* Swap the index if there was no borrow */ 2457 vli2_rshift1(modMultiple); 2458 } 2459 vli_set(result, v[index]); 2460 } 2461 #endif /* (uECC_CURVE != uECC_secp160r1) */ 2462 2463 static int uECC_sign_with_k(const uint8_t private_key[uECC_BYTES], 2464 const uint8_t message_hash[uECC_BYTES], 2465 uECC_word_t k[uECC_N_WORDS], 2466 uint8_t signature[uECC_BYTES*2]) { 2467 uECC_word_t tmp[uECC_N_WORDS]; 2468 uECC_word_t s[uECC_N_WORDS]; 2469 uECC_word_t *k2[2] = {tmp, s}; 2470 EccPoint p; 2471 uECC_word_t carry; 2472 uECC_word_t tries; 2473 2474 /* Make sure 0 < k < curve_n */ 2475 if (vli_isZero(k) || vli_cmp_n(curve_n, k) != 1) { 2476 return 0; 2477 } 2478 2479 #if (uECC_CURVE == uECC_secp160r1) 2480 /* Make sure that we don't leak timing information about k. 2481 See http://eprint.iacr.org/2011/232.pdf */ 2482 vli_add_n(tmp, k, curve_n); 2483 carry = (tmp[uECC_WORDS] & 0x02); 2484 vli_add_n(s, tmp, curve_n); 2485 2486 /* p = k * G */ 2487 EccPoint_mult(&p, &curve_G, k2[!carry], NULL, (uECC_BYTES * 8) + 2); 2488 #else 2489 /* Make sure that we don't leak timing information about k. 2490 See http://eprint.iacr.org/2011/232.pdf */ 2491 carry = vli_add(tmp, k, curve_n); 2492 vli_add(s, tmp, curve_n); 2493 2494 /* p = k * G */ 2495 EccPoint_mult(&p, &curve_G, k2[!carry], NULL, (uECC_BYTES * 8) + 1); 2496 2497 /* r = x1 (mod n) */ 2498 if (vli_cmp(curve_n, p.x) != 1) { 2499 vli_sub(p.x, p.x, curve_n); 2500 } 2501 #endif 2502 if (vli_isZero(p.x)) { 2503 return 0; 2504 } 2505 2506 // Attempt to get a random number to prevent side channel analysis of k. 2507 // If the RNG fails every time (eg it was not defined), we continue so that 2508 // deterministic signing can still work (with reduced security) without 2509 // an RNG defined. 2510 carry = 0; // use to signal that the RNG succeeded at least once. 2511 for (tries = 0; tries < MAX_TRIES; ++tries) { 2512 if (!g_rng_function((uint8_t *)tmp, sizeof(tmp))) { 2513 continue; 2514 } 2515 carry = 1; 2516 if (!vli_isZero(tmp)) { 2517 break; 2518 } 2519 } 2520 if (!carry) { 2521 vli_clear(tmp); 2522 tmp[0] = 1; 2523 } 2524 2525 /* Prevent side channel analysis of vli_modInv() to determine 2526 bits of k / the private key by premultiplying by a random number */ 2527 vli_modMult_n(k, k, tmp); /* k' = rand * k */ 2528 vli_modInv_n(k, k, curve_n); /* k = 1 / k' */ 2529 vli_modMult_n(k, k, tmp); /* k = 1 / k */ 2530 2531 vli_nativeToBytes(signature, p.x); /* store r */ 2532 2533 tmp[uECC_N_WORDS - 1] = 0; 2534 vli_bytesToNative(tmp, private_key); /* tmp = d */ 2535 s[uECC_N_WORDS - 1] = 0; 2536 vli_set(s, p.x); 2537 vli_modMult_n(s, tmp, s); /* s = r*d */ 2538 2539 vli_bytesToNative(tmp, message_hash); 2540 vli_modAdd_n(s, tmp, s, curve_n); /* s = e + r*d */ 2541 vli_modMult_n(s, s, k); /* s = (e + r*d) / k */ 2542 #if (uECC_CURVE == uECC_secp160r1) 2543 if (s[uECC_N_WORDS - 1]) { 2544 return 0; 2545 } 2546 #endif 2547 vli_nativeToBytes(signature + uECC_BYTES, s); 2548 return 1; 2549 } 2550 2551 int uECC_sign(const uint8_t private_key[uECC_BYTES], 2552 const uint8_t message_hash[uECC_BYTES], 2553 uint8_t signature[uECC_BYTES*2]) { 2554 uECC_word_t k[uECC_N_WORDS]; 2555 uECC_word_t tries; 2556 2557 for (tries = 0; tries < MAX_TRIES; ++tries) { 2558 if(g_rng_function((uint8_t *)k, sizeof(k))) { 2559 #if (uECC_CURVE == uECC_secp160r1) 2560 k[uECC_WORDS] &= 0x01; 2561 #endif 2562 if (uECC_sign_with_k(private_key, message_hash, k, signature)) { 2563 return 1; 2564 } 2565 } 2566 } 2567 return 0; 2568 } 2569 2570 /* Compute an HMAC using K as a key (as in RFC 6979). Note that K is always 2571 the same size as the hash result size. */ 2572 static void HMAC_init(uECC_HashContext *hash_context, const uint8_t *K) { 2573 uint8_t *pad = hash_context->tmp + 2 * hash_context->result_size; 2574 unsigned i; 2575 for (i = 0; i < hash_context->result_size; ++i) 2576 pad[i] = K[i] ^ 0x36; 2577 for (; i < hash_context->block_size; ++i) 2578 pad[i] = 0x36; 2579 2580 hash_context->init_hash(hash_context); 2581 hash_context->update_hash(hash_context, pad, hash_context->block_size); 2582 } 2583 2584 static void HMAC_update(uECC_HashContext *hash_context, 2585 const uint8_t *message, 2586 unsigned message_size) { 2587 hash_context->update_hash(hash_context, message, message_size); 2588 } 2589 2590 static void HMAC_finish(uECC_HashContext *hash_context, const uint8_t *K, uint8_t *result) { 2591 uint8_t *pad = hash_context->tmp + 2 * hash_context->result_size; 2592 unsigned i; 2593 for (i = 0; i < hash_context->result_size; ++i) 2594 pad[i] = K[i] ^ 0x5c; 2595 for (; i < hash_context->block_size; ++i) 2596 pad[i] = 0x5c; 2597 2598 hash_context->finish_hash(hash_context, result); 2599 2600 hash_context->init_hash(hash_context); 2601 hash_context->update_hash(hash_context, pad, hash_context->block_size); 2602 hash_context->update_hash(hash_context, result, hash_context->result_size); 2603 hash_context->finish_hash(hash_context, result); 2604 } 2605 2606 /* V = HMAC_K(V) */ 2607 static void update_V(uECC_HashContext *hash_context, uint8_t *K, uint8_t *V) { 2608 HMAC_init(hash_context, K); 2609 HMAC_update(hash_context, V, hash_context->result_size); 2610 HMAC_finish(hash_context, K, V); 2611 } 2612 2613 /* Deterministic signing, similar to RFC 6979. Differences are: 2614 * We just use (truncated) H(m) directly rather than bits2octets(H(m)) 2615 (it is not reduced modulo curve_n). 2616 * We generate a value for k (aka T) directly rather than converting endianness. 2617 2618 Layout of hash_context->tmp: <K> | <V> | (1 byte overlapped 0x00 or 0x01) / <HMAC pad> */ 2619 int uECC_sign_deterministic(const uint8_t private_key[uECC_BYTES], 2620 const uint8_t message_hash[uECC_BYTES], 2621 uECC_HashContext *hash_context, 2622 uint8_t signature[uECC_BYTES*2]) { 2623 uint8_t *K = hash_context->tmp; 2624 uint8_t *V = K + hash_context->result_size; 2625 uECC_word_t tries; 2626 unsigned i; 2627 for (i = 0; i < hash_context->result_size; ++i) { 2628 V[i] = 0x01; 2629 K[i] = 0; 2630 } 2631 2632 // K = HMAC_K(V || 0x00 || int2octets(x) || h(m)) 2633 HMAC_init(hash_context, K); 2634 V[hash_context->result_size] = 0x00; 2635 HMAC_update(hash_context, V, hash_context->result_size + 1); 2636 HMAC_update(hash_context, private_key, uECC_BYTES); 2637 HMAC_update(hash_context, message_hash, uECC_BYTES); 2638 HMAC_finish(hash_context, K, K); 2639 2640 update_V(hash_context, K, V); 2641 2642 // K = HMAC_K(V || 0x01 || int2octets(x) || h(m)) 2643 HMAC_init(hash_context, K); 2644 V[hash_context->result_size] = 0x01; 2645 HMAC_update(hash_context, V, hash_context->result_size + 1); 2646 HMAC_update(hash_context, private_key, uECC_BYTES); 2647 HMAC_update(hash_context, message_hash, uECC_BYTES); 2648 HMAC_finish(hash_context, K, K); 2649 2650 update_V(hash_context, K, V); 2651 2652 for (tries = 0; tries < MAX_TRIES; ++tries) { 2653 uECC_word_t T[uECC_N_WORDS]; 2654 uint8_t *T_ptr = (uint8_t *)T; 2655 unsigned T_bytes = 0; 2656 while (T_bytes < sizeof(T)) { 2657 update_V(hash_context, K, V); 2658 for (i = 0; i < hash_context->result_size && T_bytes < sizeof(T); ++i, ++T_bytes) { 2659 T_ptr[T_bytes] = V[i]; 2660 } 2661 } 2662 #if (uECC_CURVE == uECC_secp160r1) 2663 T[uECC_WORDS] &= 0x01; 2664 #endif 2665 2666 if (uECC_sign_with_k(private_key, message_hash, T, signature)) { 2667 return 1; 2668 } 2669 2670 // K = HMAC_K(V || 0x00) 2671 HMAC_init(hash_context, K); 2672 V[hash_context->result_size] = 0x00; 2673 HMAC_update(hash_context, V, hash_context->result_size + 1); 2674 HMAC_finish(hash_context, K, K); 2675 2676 update_V(hash_context, K, V); 2677 } 2678 return 0; 2679 } 2680 2681 static bitcount_t smax(bitcount_t a, bitcount_t b) { 2682 return (a > b ? a : b); 2683 } 2684 2685 int uECC_verify(const uint8_t public_key[uECC_BYTES*2], 2686 const uint8_t hash[uECC_BYTES], 2687 const uint8_t signature[uECC_BYTES*2]) { 2688 uECC_word_t u1[uECC_N_WORDS], u2[uECC_N_WORDS]; 2689 uECC_word_t z[uECC_N_WORDS]; 2690 EccPoint public, sum; 2691 uECC_word_t rx[uECC_WORDS]; 2692 uECC_word_t ry[uECC_WORDS]; 2693 uECC_word_t tx[uECC_WORDS]; 2694 uECC_word_t ty[uECC_WORDS]; 2695 uECC_word_t tz[uECC_WORDS]; 2696 const EccPoint *points[4]; 2697 const EccPoint *point; 2698 bitcount_t numBits; 2699 bitcount_t i; 2700 uECC_word_t r[uECC_N_WORDS], s[uECC_N_WORDS]; 2701 r[uECC_N_WORDS - 1] = 0; 2702 s[uECC_N_WORDS - 1] = 0; 2703 2704 vli_bytesToNative(public.x, public_key); 2705 vli_bytesToNative(public.y, public_key + uECC_BYTES); 2706 vli_bytesToNative(r, signature); 2707 vli_bytesToNative(s, signature + uECC_BYTES); 2708 2709 if (vli_isZero(r) || vli_isZero(s)) { /* r, s must not be 0. */ 2710 return 0; 2711 } 2712 2713 #if (uECC_CURVE != uECC_secp160r1) 2714 if (vli_cmp(curve_n, r) != 1 || vli_cmp(curve_n, s) != 1) { /* r, s must be < n. */ 2715 return 0; 2716 } 2717 #endif 2718 2719 /* Calculate u1 and u2. */ 2720 vli_modInv_n(z, s, curve_n); /* Z = s^-1 */ 2721 u1[uECC_N_WORDS - 1] = 0; 2722 vli_bytesToNative(u1, hash); 2723 vli_modMult_n(u1, u1, z); /* u1 = e/s */ 2724 vli_modMult_n(u2, r, z); /* u2 = r/s */ 2725 2726 /* Calculate sum = G + Q. */ 2727 vli_set(sum.x, public.x); 2728 vli_set(sum.y, public.y); 2729 vli_set(tx, curve_G.x); 2730 vli_set(ty, curve_G.y); 2731 vli_modSub_fast(z, sum.x, tx); /* Z = x2 - x1 */ 2732 XYcZ_add(tx, ty, sum.x, sum.y); 2733 vli_modInv(z, z, curve_p); /* Z = 1/Z */ 2734 apply_z(sum.x, sum.y, z); 2735 2736 /* Use Shamir's trick to calculate u1*G + u2*Q */ 2737 points[0] = 0; 2738 points[1] = &curve_G; 2739 points[2] = &public; 2740 points[3] = ∑ 2741 numBits = smax(vli_numBits(u1, uECC_N_WORDS), vli_numBits(u2, uECC_N_WORDS)); 2742 2743 point = points[(!!vli_testBit(u1, numBits - 1)) | ((!!vli_testBit(u2, numBits - 1)) << 1)]; 2744 vli_set(rx, point->x); 2745 vli_set(ry, point->y); 2746 vli_clear(z); 2747 z[0] = 1; 2748 2749 for (i = numBits - 2; i >= 0; --i) { 2750 uECC_word_t index; 2751 EccPoint_double_jacobian(rx, ry, z); 2752 2753 index = (!!vli_testBit(u1, i)) | ((!!vli_testBit(u2, i)) << 1); 2754 point = points[index]; 2755 if (point) { 2756 vli_set(tx, point->x); 2757 vli_set(ty, point->y); 2758 apply_z(tx, ty, z); 2759 vli_modSub_fast(tz, rx, tx); /* Z = x2 - x1 */ 2760 XYcZ_add(tx, ty, rx, ry); 2761 vli_modMult_fast(z, z, tz); 2762 } 2763 } 2764 2765 vli_modInv(z, z, curve_p); /* Z = 1/Z */ 2766 apply_z(rx, ry, z); 2767 2768 /* v = x1 (mod n) */ 2769 #if (uECC_CURVE != uECC_secp160r1) 2770 if (vli_cmp(curve_n, rx) != 1) { 2771 vli_sub(rx, rx, curve_n); 2772 } 2773 #endif 2774 2775 /* Accept only if v == r. */ 2776 return vli_equal(rx, r); 2777 } 2778 2779 #endif 2780