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 static const uECC_word_t curve_b[uECC_WORDS] = uECC_CONCAT(Curve_B_, uECC_CURVE); 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 static bitcount_t vli_numBits(const uECC_word_t *vli, wordcount_t max_words); 379 static void vli_set(uECC_word_t *dest, const uECC_word_t *src); 380 static cmpresult_t vli_cmp(const uECC_word_t *left, const uECC_word_t *right); 381 static cmpresult_t vli_equal(const uECC_word_t *left, const uECC_word_t *right); 382 static void vli_rshift1(uECC_word_t *vli); 383 static uECC_word_t vli_add(uECC_word_t *result, 384 const uECC_word_t *left, 385 const uECC_word_t *right); 386 static uECC_word_t vli_sub(uECC_word_t *result, 387 const uECC_word_t *left, 388 const uECC_word_t *right); 389 static void vli_mult(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right); 390 static void vli_modAdd(uECC_word_t *result, 391 const uECC_word_t *left, 392 const uECC_word_t *right, 393 const uECC_word_t *mod); 394 static void vli_modSub(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_mmod_fast(uECC_word_t *RESTRICT result, uECC_word_t *RESTRICT product); 399 static void vli_modMult_fast(uECC_word_t *result, 400 const uECC_word_t *left, 401 const uECC_word_t *right); 402 static void vli_modInv(uECC_word_t *result, const uECC_word_t *input, const uECC_word_t *mod); 403 #if uECC_SQUARE_FUNC 404 static void vli_square(uECC_word_t *result, const uECC_word_t *left); 405 static void vli_modSquare_fast(uECC_word_t *result, const uECC_word_t *left); 406 #endif 407 408 #if (defined(_WIN32) || defined(_WIN64)) 409 /* Windows */ 410 411 #define WIN32_LEAN_AND_MEAN 412 #include <windows.h> 413 #include <wincrypt.h> 414 415 static int default_RNG(uint8_t *dest, unsigned size) { 416 HCRYPTPROV prov; 417 if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { 418 return 0; 419 } 420 421 CryptGenRandom(prov, size, (BYTE *)dest); 422 CryptReleaseContext(prov, 0); 423 return 1; 424 } 425 426 #elif defined(unix) || defined(__linux__) || defined(__unix__) || defined(__unix) || \ 427 (defined(__APPLE__) && defined(__MACH__)) || defined(uECC_POSIX) 428 429 /* Some POSIX-like system with /dev/urandom or /dev/random. */ 430 #include <sys/types.h> 431 #include <fcntl.h> 432 #include <unistd.h> 433 434 #ifndef O_CLOEXEC 435 #define O_CLOEXEC 0 436 #endif 437 438 static int default_RNG(uint8_t *dest, unsigned size) { 439 int fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC); 440 if (fd == -1) { 441 fd = open("/dev/random", O_RDONLY | O_CLOEXEC); 442 if (fd == -1) { 443 return 0; 444 } 445 } 446 447 char *ptr = (char *)dest; 448 size_t left = size; 449 while (left > 0) { 450 ssize_t bytes_read = read(fd, ptr, left); 451 if (bytes_read <= 0) { // read failed 452 close(fd); 453 return 0; 454 } 455 left -= bytes_read; 456 ptr += bytes_read; 457 } 458 459 close(fd); 460 return 1; 461 } 462 463 #else /* Some other platform */ 464 465 static int default_RNG(uint8_t *dest, unsigned size) { 466 return 0; 467 } 468 469 #endif 470 471 static uECC_RNG_Function g_rng_function = &default_RNG; 472 473 void uECC_set_rng(uECC_RNG_Function rng_function) { 474 g_rng_function = rng_function; 475 } 476 477 #ifdef __GNUC__ /* Only support GCC inline asm for now */ 478 #if (uECC_ASM && (uECC_PLATFORM == uECC_avr)) 479 #include "asm_avr.inc" 480 #endif 481 482 #if (uECC_ASM && (uECC_PLATFORM == uECC_arm || uECC_PLATFORM == uECC_arm_thumb || \ 483 uECC_PLATFORM == uECC_arm_thumb2)) 484 #include "asm_arm.inc" 485 #endif 486 #endif 487 488 #if !asm_clear 489 static void vli_clear(uECC_word_t *vli) { 490 wordcount_t i; 491 for (i = 0; i < uECC_WORDS; ++i) { 492 vli[i] = 0; 493 } 494 } 495 #endif 496 497 /* Returns 1 if vli == 0, 0 otherwise. */ 498 #if !asm_isZero 499 static uECC_word_t vli_isZero(const uECC_word_t *vli) { 500 wordcount_t i; 501 for (i = 0; i < uECC_WORDS; ++i) { 502 if (vli[i]) { 503 return 0; 504 } 505 } 506 return 1; 507 } 508 #endif 509 510 /* Returns nonzero if bit 'bit' of vli is set. */ 511 #if !asm_testBit 512 static uECC_word_t vli_testBit(const uECC_word_t *vli, bitcount_t bit) { 513 return (vli[bit >> uECC_WORD_BITS_SHIFT] & ((uECC_word_t)1 << (bit & uECC_WORD_BITS_MASK))); 514 } 515 #endif 516 517 /* Counts the number of words in vli. */ 518 #if !asm_numBits 519 static wordcount_t vli_numDigits(const uECC_word_t *vli, wordcount_t max_words) { 520 swordcount_t i; 521 /* Search from the end until we find a non-zero digit. 522 We do it in reverse because we expect that most digits will be nonzero. */ 523 for (i = max_words - 1; i >= 0 && vli[i] == 0; --i) { 524 } 525 526 return (i + 1); 527 } 528 529 /* Counts the number of bits required to represent vli. */ 530 static bitcount_t vli_numBits(const uECC_word_t *vli, wordcount_t max_words) { 531 uECC_word_t i; 532 uECC_word_t digit; 533 534 wordcount_t num_digits = vli_numDigits(vli, max_words); 535 if (num_digits == 0) { 536 return 0; 537 } 538 539 digit = vli[num_digits - 1]; 540 for (i = 0; digit; ++i) { 541 digit >>= 1; 542 } 543 544 return (((bitcount_t)(num_digits - 1) << uECC_WORD_BITS_SHIFT) + i); 545 } 546 #endif /* !asm_numBits */ 547 548 /* Sets dest = src. */ 549 #if !asm_set 550 static void vli_set(uECC_word_t *dest, const uECC_word_t *src) { 551 wordcount_t i; 552 for (i = 0; i < uECC_WORDS; ++i) { 553 dest[i] = src[i]; 554 } 555 } 556 #endif 557 558 /* Returns sign of left - right. */ 559 #if !asm_cmp 560 static cmpresult_t vli_cmp(const uECC_word_t *left, const uECC_word_t *right) { 561 swordcount_t i; 562 for (i = uECC_WORDS - 1; i >= 0; --i) { 563 if (left[i] > right[i]) { 564 return 1; 565 } else if (left[i] < right[i]) { 566 return -1; 567 } 568 } 569 return 0; 570 } 571 #endif 572 573 static cmpresult_t vli_equal(const uECC_word_t *left, const uECC_word_t *right) { 574 uECC_word_t result = 0; 575 swordcount_t i; 576 for (i = uECC_WORDS - 1; i >= 0; --i) { 577 result |= (left[i] ^ right[i]); 578 } 579 return (result == 0); 580 } 581 582 /* Computes vli = vli >> 1. */ 583 #if !asm_rshift1 584 static void vli_rshift1(uECC_word_t *vli) { 585 uECC_word_t *end = vli; 586 uECC_word_t carry = 0; 587 588 vli += uECC_WORDS; 589 while (vli-- > end) { 590 uECC_word_t temp = *vli; 591 *vli = (temp >> 1) | carry; 592 carry = temp << (uECC_WORD_BITS - 1); 593 } 594 } 595 #endif 596 597 /* Computes result = left + right, returning carry. Can modify in place. */ 598 #if !asm_add 599 static uECC_word_t vli_add(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { 600 uECC_word_t carry = 0; 601 wordcount_t i; 602 for (i = 0; i < uECC_WORDS; ++i) { 603 uECC_word_t sum = left[i] + right[i] + carry; 604 if (sum != left[i]) { 605 carry = (sum < left[i]); 606 } 607 result[i] = sum; 608 } 609 return carry; 610 } 611 #endif 612 613 /* Computes result = left - right, returning borrow. Can modify in place. */ 614 #if !asm_sub 615 static uECC_word_t vli_sub(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { 616 uECC_word_t borrow = 0; 617 wordcount_t i; 618 for (i = 0; i < uECC_WORDS; ++i) { 619 uECC_word_t diff = left[i] - right[i] - borrow; 620 if (diff != left[i]) { 621 borrow = (diff > left[i]); 622 } 623 result[i] = diff; 624 } 625 return borrow; 626 } 627 #endif 628 629 #if (!asm_mult || (uECC_SQUARE_FUNC && !asm_square) || uECC_CURVE == uECC_secp256k1) 630 static void muladd(uECC_word_t a, 631 uECC_word_t b, 632 uECC_word_t *r0, 633 uECC_word_t *r1, 634 uECC_word_t *r2) { 635 #if uECC_WORD_SIZE == 8 && !SUPPORTS_INT128 636 uint64_t a0 = a & 0xffffffffull; 637 uint64_t a1 = a >> 32; 638 uint64_t b0 = b & 0xffffffffull; 639 uint64_t b1 = b >> 32; 640 641 uint64_t i0 = a0 * b0; 642 uint64_t i1 = a0 * b1; 643 uint64_t i2 = a1 * b0; 644 uint64_t i3 = a1 * b1; 645 646 uint64_t p0, p1; 647 648 i2 += (i0 >> 32); 649 i2 += i1; 650 if (i2 < i1) { // overflow 651 i3 += 0x100000000ull; 652 } 653 654 p0 = (i0 & 0xffffffffull) | (i2 << 32); 655 p1 = i3 + (i2 >> 32); 656 657 *r0 += p0; 658 *r1 += (p1 + (*r0 < p0)); 659 *r2 += ((*r1 < p1) || (*r1 == p1 && *r0 < p0)); 660 #else 661 uECC_dword_t p = (uECC_dword_t)a * b; 662 uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0; 663 r01 += p; 664 *r2 += (r01 < p); 665 *r1 = r01 >> uECC_WORD_BITS; 666 *r0 = (uECC_word_t)r01; 667 #endif 668 } 669 #define muladd_exists 1 670 #endif 671 672 #if !asm_mult 673 static void vli_mult(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { 674 uECC_word_t r0 = 0; 675 uECC_word_t r1 = 0; 676 uECC_word_t r2 = 0; 677 wordcount_t i, k; 678 679 /* Compute each digit of result in sequence, maintaining the carries. */ 680 for (k = 0; k < uECC_WORDS; ++k) { 681 for (i = 0; i <= k; ++i) { 682 muladd(left[i], right[k - i], &r0, &r1, &r2); 683 } 684 result[k] = r0; 685 r0 = r1; 686 r1 = r2; 687 r2 = 0; 688 } 689 for (k = uECC_WORDS; k < uECC_WORDS * 2 - 1; ++k) { 690 for (i = (k + 1) - uECC_WORDS; i < uECC_WORDS; ++i) { 691 muladd(left[i], right[k - i], &r0, &r1, &r2); 692 } 693 result[k] = r0; 694 r0 = r1; 695 r1 = r2; 696 r2 = 0; 697 } 698 result[uECC_WORDS * 2 - 1] = r0; 699 } 700 #endif 701 702 #if uECC_SQUARE_FUNC 703 704 #if !asm_square 705 static void mul2add(uECC_word_t a, 706 uECC_word_t b, 707 uECC_word_t *r0, 708 uECC_word_t *r1, 709 uECC_word_t *r2) { 710 #if uECC_WORD_SIZE == 8 && !SUPPORTS_INT128 711 uint64_t a0 = a & 0xffffffffull; 712 uint64_t a1 = a >> 32; 713 uint64_t b0 = b & 0xffffffffull; 714 uint64_t b1 = b >> 32; 715 716 uint64_t i0 = a0 * b0; 717 uint64_t i1 = a0 * b1; 718 uint64_t i2 = a1 * b0; 719 uint64_t i3 = a1 * b1; 720 721 uint64_t p0, p1; 722 723 i2 += (i0 >> 32); 724 i2 += i1; 725 if (i2 < i1) 726 { // overflow 727 i3 += 0x100000000ull; 728 } 729 730 p0 = (i0 & 0xffffffffull) | (i2 << 32); 731 p1 = i3 + (i2 >> 32); 732 733 *r2 += (p1 >> 63); 734 p1 = (p1 << 1) | (p0 >> 63); 735 p0 <<= 1; 736 737 *r0 += p0; 738 *r1 += (p1 + (*r0 < p0)); 739 *r2 += ((*r1 < p1) || (*r1 == p1 && *r0 < p0)); 740 #else 741 uECC_dword_t p = (uECC_dword_t)a * b; 742 uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0; 743 *r2 += (p >> (uECC_WORD_BITS * 2 - 1)); 744 p *= 2; 745 r01 += p; 746 *r2 += (r01 < p); 747 *r1 = r01 >> uECC_WORD_BITS; 748 *r0 = (uECC_word_t)r01; 749 #endif 750 } 751 752 static void vli_square(uECC_word_t *result, const uECC_word_t *left) { 753 uECC_word_t r0 = 0; 754 uECC_word_t r1 = 0; 755 uECC_word_t r2 = 0; 756 757 wordcount_t i, k; 758 759 for (k = 0; k < uECC_WORDS * 2 - 1; ++k) { 760 uECC_word_t min = (k < uECC_WORDS ? 0 : (k + 1) - uECC_WORDS); 761 for (i = min; i <= k && i <= k - i; ++i) { 762 if (i < k-i) { 763 mul2add(left[i], left[k - i], &r0, &r1, &r2); 764 } else { 765 muladd(left[i], left[k - i], &r0, &r1, &r2); 766 } 767 } 768 result[k] = r0; 769 r0 = r1; 770 r1 = r2; 771 r2 = 0; 772 } 773 774 result[uECC_WORDS * 2 - 1] = r0; 775 } 776 #endif 777 778 #else /* uECC_SQUARE_FUNC */ 779 780 #define vli_square(result, left, size) vli_mult((result), (left), (left), (size)) 781 782 #endif /* uECC_SQUARE_FUNC */ 783 784 785 /* Computes result = (left + right) % mod. 786 Assumes that left < mod and right < mod, and that result does not overlap mod. */ 787 #if !asm_modAdd 788 static void vli_modAdd(uECC_word_t *result, 789 const uECC_word_t *left, 790 const uECC_word_t *right, 791 const uECC_word_t *mod) { 792 uECC_word_t carry = vli_add(result, left, right); 793 if (carry || vli_cmp(result, mod) >= 0) { 794 /* result > mod (result = mod + remainder), so subtract mod to get remainder. */ 795 vli_sub(result, result, mod); 796 } 797 } 798 #endif 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_modSub 803 static void vli_modSub(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 l_borrow = vli_sub(result, left, right); 808 if (l_borrow) { 809 /* In this case, result == -diff == (max int) - diff. Since -x % d == d - x, 810 we can get the correct result from result + mod (with overflow). */ 811 vli_add(result, result, mod); 812 } 813 } 814 #endif 815 816 #if !asm_modSub_fast 817 #define vli_modSub_fast(result, left, right) vli_modSub((result), (left), (right), curve_p) 818 #endif 819 820 #if !asm_mmod_fast 821 822 #if (uECC_CURVE == uECC_secp160r1 || uECC_CURVE == uECC_secp256k1) 823 /* omega_mult() is defined farther below for the different curves / word sizes */ 824 static void omega_mult(uECC_word_t * RESTRICT result, const uECC_word_t * RESTRICT right); 825 826 /* Computes result = product % curve_p 827 see http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf page 354 828 829 Note that this only works if log2(omega) < log2(p) / 2 */ 830 static void vli_mmod_fast(uECC_word_t *RESTRICT result, uECC_word_t *RESTRICT product) { 831 uECC_word_t tmp[2 * uECC_WORDS]; 832 uECC_word_t carry; 833 834 vli_clear(tmp); 835 vli_clear(tmp + uECC_WORDS); 836 837 omega_mult(tmp, product + uECC_WORDS); /* (Rq, q) = q * c */ 838 839 carry = vli_add(result, product, tmp); /* (C, r) = r + q */ 840 vli_clear(product); 841 omega_mult(product, tmp + uECC_WORDS); /* Rq*c */ 842 carry += vli_add(result, result, product); /* (C1, r) = r + Rq*c */ 843 844 while (carry > 0) { 845 --carry; 846 vli_sub(result, result, curve_p); 847 } 848 if (vli_cmp(result, curve_p) > 0) { 849 vli_sub(result, result, curve_p); 850 } 851 } 852 853 #endif 854 855 #if uECC_CURVE == uECC_secp160r1 856 857 #if uECC_WORD_SIZE == 1 858 static void omega_mult(uint8_t * RESTRICT result, const uint8_t * RESTRICT right) { 859 uint8_t carry; 860 uint8_t i; 861 862 /* Multiply by (2^31 + 1). */ 863 vli_set(result + 4, right); /* 2^32 */ 864 vli_rshift1(result + 4); /* 2^31 */ 865 result[3] = right[0] << 7; /* get last bit from shift */ 866 867 carry = vli_add(result, result, right); /* 2^31 + 1 */ 868 for (i = uECC_WORDS; carry; ++i) { 869 uint16_t sum = (uint16_t)result[i] + carry; 870 result[i] = (uint8_t)sum; 871 carry = sum >> 8; 872 } 873 } 874 #elif uECC_WORD_SIZE == 4 875 static void omega_mult(uint32_t * RESTRICT result, const uint32_t * RESTRICT right) { 876 uint32_t carry; 877 unsigned i; 878 879 /* Multiply by (2^31 + 1). */ 880 vli_set(result + 1, right); /* 2^32 */ 881 vli_rshift1(result + 1); /* 2^31 */ 882 result[0] = right[0] << 31; /* get last bit from shift */ 883 884 carry = vli_add(result, result, right); /* 2^31 + 1 */ 885 for (i = uECC_WORDS; carry; ++i) { 886 uint64_t sum = (uint64_t)result[i] + carry; 887 result[i] = (uint32_t)sum; 888 carry = sum >> 32; 889 } 890 } 891 #endif /* uECC_WORD_SIZE */ 892 893 #elif uECC_CURVE == uECC_secp192r1 894 895 /* Computes result = product % curve_p. 896 See algorithm 5 and 6 from http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf */ 897 #if uECC_WORD_SIZE == 1 898 static void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) { 899 uint8_t tmp[uECC_WORDS]; 900 uint8_t carry; 901 902 vli_set(result, product); 903 904 vli_set(tmp, &product[24]); 905 carry = vli_add(result, result, tmp); 906 907 tmp[0] = tmp[1] = tmp[2] = tmp[3] = tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0; 908 tmp[8] = product[24]; tmp[9] = product[25]; tmp[10] = product[26]; tmp[11] = product[27]; 909 tmp[12] = product[28]; tmp[13] = product[29]; tmp[14] = product[30]; tmp[15] = product[31]; 910 tmp[16] = product[32]; tmp[17] = product[33]; tmp[18] = product[34]; tmp[19] = product[35]; 911 tmp[20] = product[36]; tmp[21] = product[37]; tmp[22] = product[38]; tmp[23] = product[39]; 912 carry += vli_add(result, result, tmp); 913 914 tmp[0] = tmp[8] = product[40]; 915 tmp[1] = tmp[9] = product[41]; 916 tmp[2] = tmp[10] = product[42]; 917 tmp[3] = tmp[11] = product[43]; 918 tmp[4] = tmp[12] = product[44]; 919 tmp[5] = tmp[13] = product[45]; 920 tmp[6] = tmp[14] = product[46]; 921 tmp[7] = tmp[15] = product[47]; 922 tmp[16] = tmp[17] = tmp[18] = tmp[19] = tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0; 923 carry += vli_add(result, result, tmp); 924 925 while (carry || vli_cmp(curve_p, result) != 1) { 926 carry -= vli_sub(result, result, curve_p); 927 } 928 } 929 #elif uECC_WORD_SIZE == 4 930 static void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product) { 931 uint32_t tmp[uECC_WORDS]; 932 int carry; 933 934 vli_set(result, product); 935 936 vli_set(tmp, &product[6]); 937 carry = vli_add(result, result, tmp); 938 939 tmp[0] = tmp[1] = 0; 940 tmp[2] = product[6]; 941 tmp[3] = product[7]; 942 tmp[4] = product[8]; 943 tmp[5] = product[9]; 944 carry += vli_add(result, result, tmp); 945 946 tmp[0] = tmp[2] = product[10]; 947 tmp[1] = tmp[3] = product[11]; 948 tmp[4] = tmp[5] = 0; 949 carry += vli_add(result, result, tmp); 950 951 while (carry || vli_cmp(curve_p, result) != 1) { 952 carry -= vli_sub(result, result, curve_p); 953 } 954 } 955 #else 956 static void vli_mmod_fast(uint64_t *RESTRICT result, uint64_t *RESTRICT product) { 957 uint64_t tmp[uECC_WORDS]; 958 int carry; 959 960 vli_set(result, product); 961 962 vli_set(tmp, &product[3]); 963 carry = vli_add(result, result, tmp); 964 965 tmp[0] = 0; 966 tmp[1] = product[3]; 967 tmp[2] = product[4]; 968 carry += vli_add(result, result, tmp); 969 970 tmp[0] = tmp[1] = product[5]; 971 tmp[2] = 0; 972 carry += vli_add(result, result, tmp); 973 974 while (carry || vli_cmp(curve_p, result) != 1) { 975 carry -= vli_sub(result, result, curve_p); 976 } 977 } 978 #endif /* uECC_WORD_SIZE */ 979 980 #elif uECC_CURVE == uECC_secp256r1 981 982 /* Computes result = product % curve_p 983 from http://www.nsa.gov/ia/_files/nist-routines.pdf */ 984 #if uECC_WORD_SIZE == 1 985 static void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) { 986 uint8_t tmp[uECC_BYTES]; 987 int8_t carry; 988 989 /* t */ 990 vli_set(result, product); 991 992 /* s1 */ 993 tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0; 994 tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0; 995 tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0; 996 tmp[12] = product[44]; tmp[13] = product[45]; tmp[14] = product[46]; tmp[15] = product[47]; 997 tmp[16] = product[48]; tmp[17] = product[49]; tmp[18] = product[50]; tmp[19] = product[51]; 998 tmp[20] = product[52]; tmp[21] = product[53]; tmp[22] = product[54]; tmp[23] = product[55]; 999 tmp[24] = product[56]; tmp[25] = product[57]; tmp[26] = product[58]; tmp[27] = product[59]; 1000 tmp[28] = product[60]; tmp[29] = product[61]; tmp[30] = product[62]; tmp[31] = product[63]; 1001 carry = vli_add(tmp, tmp, tmp); 1002 carry += vli_add(result, result, tmp); 1003 1004 /* s2 */ 1005 tmp[12] = product[48]; tmp[13] = product[49]; tmp[14] = product[50]; tmp[15] = product[51]; 1006 tmp[16] = product[52]; tmp[17] = product[53]; tmp[18] = product[54]; tmp[19] = product[55]; 1007 tmp[20] = product[56]; tmp[21] = product[57]; tmp[22] = product[58]; tmp[23] = product[59]; 1008 tmp[24] = product[60]; tmp[25] = product[61]; tmp[26] = product[62]; tmp[27] = product[63]; 1009 tmp[28] = tmp[29] = tmp[30] = tmp[31] = 0; 1010 carry += vli_add(tmp, tmp, tmp); 1011 carry += vli_add(result, result, tmp); 1012 1013 /* s3 */ 1014 tmp[0] = product[32]; tmp[1] = product[33]; tmp[2] = product[34]; tmp[3] = product[35]; 1015 tmp[4] = product[36]; tmp[5] = product[37]; tmp[6] = product[38]; tmp[7] = product[39]; 1016 tmp[8] = product[40]; tmp[9] = product[41]; tmp[10] = product[42]; tmp[11] = product[43]; 1017 tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0; 1018 tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0; 1019 tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0; 1020 tmp[24] = product[56]; tmp[25] = product[57]; tmp[26] = product[58]; tmp[27] = product[59]; 1021 tmp[28] = product[60]; tmp[29] = product[61]; tmp[30] = product[62]; tmp[31] = product[63]; 1022 carry += vli_add(result, result, tmp); 1023 1024 /* s4 */ 1025 tmp[0] = product[36]; tmp[1] = product[37]; tmp[2] = product[38]; tmp[3] = product[39]; 1026 tmp[4] = product[40]; tmp[5] = product[41]; tmp[6] = product[42]; tmp[7] = product[43]; 1027 tmp[8] = product[44]; tmp[9] = product[45]; tmp[10] = product[46]; tmp[11] = product[47]; 1028 tmp[12] = product[52]; tmp[13] = product[53]; tmp[14] = product[54]; tmp[15] = product[55]; 1029 tmp[16] = product[56]; tmp[17] = product[57]; tmp[18] = product[58]; tmp[19] = product[59]; 1030 tmp[20] = product[60]; tmp[21] = product[61]; tmp[22] = product[62]; tmp[23] = product[63]; 1031 tmp[24] = product[52]; tmp[25] = product[53]; tmp[26] = product[54]; tmp[27] = product[55]; 1032 tmp[28] = product[32]; tmp[29] = product[33]; tmp[30] = product[34]; tmp[31] = product[35]; 1033 carry += vli_add(result, result, tmp); 1034 1035 /* d1 */ 1036 tmp[0] = product[44]; tmp[1] = product[45]; tmp[2] = product[46]; tmp[3] = product[47]; 1037 tmp[4] = product[48]; tmp[5] = product[49]; tmp[6] = product[50]; tmp[7] = product[51]; 1038 tmp[8] = product[52]; tmp[9] = product[53]; tmp[10] = product[54]; tmp[11] = product[55]; 1039 tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0; 1040 tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0; 1041 tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0; 1042 tmp[24] = product[32]; tmp[25] = product[33]; tmp[26] = product[34]; tmp[27] = product[35]; 1043 tmp[28] = product[40]; tmp[29] = product[41]; tmp[30] = product[42]; tmp[31] = product[43]; 1044 carry -= vli_sub(result, result, tmp); 1045 1046 /* d2 */ 1047 tmp[0] = product[48]; tmp[1] = product[49]; tmp[2] = product[50]; tmp[3] = product[51]; 1048 tmp[4] = product[52]; tmp[5] = product[53]; tmp[6] = product[54]; tmp[7] = product[55]; 1049 tmp[8] = product[56]; tmp[9] = product[57]; tmp[10] = product[58]; tmp[11] = product[59]; 1050 tmp[12] = product[60]; tmp[13] = product[61]; tmp[14] = product[62]; tmp[15] = product[63]; 1051 tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0; 1052 tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0; 1053 tmp[24] = product[36]; tmp[25] = product[37]; tmp[26] = product[38]; tmp[27] = product[39]; 1054 tmp[28] = product[44]; tmp[29] = product[45]; tmp[30] = product[46]; tmp[31] = product[47]; 1055 carry -= vli_sub(result, result, tmp); 1056 1057 /* d3 */ 1058 tmp[0] = product[52]; tmp[1] = product[53]; tmp[2] = product[54]; tmp[3] = product[55]; 1059 tmp[4] = product[56]; tmp[5] = product[57]; tmp[6] = product[58]; tmp[7] = product[59]; 1060 tmp[8] = product[60]; tmp[9] = product[61]; tmp[10] = product[62]; tmp[11] = product[63]; 1061 tmp[12] = product[32]; tmp[13] = product[33]; tmp[14] = product[34]; tmp[15] = product[35]; 1062 tmp[16] = product[36]; tmp[17] = product[37]; tmp[18] = product[38]; tmp[19] = product[39]; 1063 tmp[20] = product[40]; tmp[21] = product[41]; tmp[22] = product[42]; tmp[23] = product[43]; 1064 tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0; 1065 tmp[28] = product[48]; tmp[29] = product[49]; tmp[30] = product[50]; tmp[31] = product[51]; 1066 carry -= vli_sub(result, result, tmp); 1067 1068 /* d4 */ 1069 tmp[0] = product[56]; tmp[1] = product[57]; tmp[2] = product[58]; tmp[3] = product[59]; 1070 tmp[4] = product[60]; tmp[5] = product[61]; tmp[6] = product[62]; tmp[7] = product[63]; 1071 tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0; 1072 tmp[12] = product[36]; tmp[13] = product[37]; tmp[14] = product[38]; tmp[15] = product[39]; 1073 tmp[16] = product[40]; tmp[17] = product[41]; tmp[18] = product[42]; tmp[19] = product[43]; 1074 tmp[20] = product[44]; tmp[21] = product[45]; tmp[22] = product[46]; tmp[23] = product[47]; 1075 tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0; 1076 tmp[28] = product[52]; tmp[29] = product[53]; tmp[30] = product[54]; tmp[31] = product[55]; 1077 carry -= vli_sub(result, result, tmp); 1078 1079 if (carry < 0) { 1080 do { 1081 carry += vli_add(result, result, curve_p); 1082 } while (carry < 0); 1083 } else { 1084 while (carry || vli_cmp(curve_p, result) != 1) { 1085 carry -= vli_sub(result, result, curve_p); 1086 } 1087 } 1088 } 1089 #elif uECC_WORD_SIZE == 4 1090 static void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product) { 1091 uint32_t tmp[uECC_WORDS]; 1092 int carry; 1093 1094 /* t */ 1095 vli_set(result, product); 1096 1097 /* s1 */ 1098 tmp[0] = tmp[1] = tmp[2] = 0; 1099 tmp[3] = product[11]; 1100 tmp[4] = product[12]; 1101 tmp[5] = product[13]; 1102 tmp[6] = product[14]; 1103 tmp[7] = product[15]; 1104 carry = vli_add(tmp, tmp, tmp); 1105 carry += vli_add(result, result, tmp); 1106 1107 /* s2 */ 1108 tmp[3] = product[12]; 1109 tmp[4] = product[13]; 1110 tmp[5] = product[14]; 1111 tmp[6] = product[15]; 1112 tmp[7] = 0; 1113 carry += vli_add(tmp, tmp, tmp); 1114 carry += vli_add(result, result, tmp); 1115 1116 /* s3 */ 1117 tmp[0] = product[8]; 1118 tmp[1] = product[9]; 1119 tmp[2] = product[10]; 1120 tmp[3] = tmp[4] = tmp[5] = 0; 1121 tmp[6] = product[14]; 1122 tmp[7] = product[15]; 1123 carry += vli_add(result, result, tmp); 1124 1125 /* s4 */ 1126 tmp[0] = product[9]; 1127 tmp[1] = product[10]; 1128 tmp[2] = product[11]; 1129 tmp[3] = product[13]; 1130 tmp[4] = product[14]; 1131 tmp[5] = product[15]; 1132 tmp[6] = product[13]; 1133 tmp[7] = product[8]; 1134 carry += vli_add(result, result, tmp); 1135 1136 /* d1 */ 1137 tmp[0] = product[11]; 1138 tmp[1] = product[12]; 1139 tmp[2] = product[13]; 1140 tmp[3] = tmp[4] = tmp[5] = 0; 1141 tmp[6] = product[8]; 1142 tmp[7] = product[10]; 1143 carry -= vli_sub(result, result, tmp); 1144 1145 /* d2 */ 1146 tmp[0] = product[12]; 1147 tmp[1] = product[13]; 1148 tmp[2] = product[14]; 1149 tmp[3] = product[15]; 1150 tmp[4] = tmp[5] = 0; 1151 tmp[6] = product[9]; 1152 tmp[7] = product[11]; 1153 carry -= vli_sub(result, result, tmp); 1154 1155 /* d3 */ 1156 tmp[0] = product[13]; 1157 tmp[1] = product[14]; 1158 tmp[2] = product[15]; 1159 tmp[3] = product[8]; 1160 tmp[4] = product[9]; 1161 tmp[5] = product[10]; 1162 tmp[6] = 0; 1163 tmp[7] = product[12]; 1164 carry -= vli_sub(result, result, tmp); 1165 1166 /* d4 */ 1167 tmp[0] = product[14]; 1168 tmp[1] = product[15]; 1169 tmp[2] = 0; 1170 tmp[3] = product[9]; 1171 tmp[4] = product[10]; 1172 tmp[5] = product[11]; 1173 tmp[6] = 0; 1174 tmp[7] = product[13]; 1175 carry -= vli_sub(result, result, tmp); 1176 1177 if (carry < 0) { 1178 do { 1179 carry += vli_add(result, result, curve_p); 1180 } while (carry < 0); 1181 } else { 1182 while (carry || vli_cmp(curve_p, result) != 1) { 1183 carry -= vli_sub(result, result, curve_p); 1184 } 1185 } 1186 } 1187 #else 1188 static void vli_mmod_fast(uint64_t *RESTRICT result, uint64_t *RESTRICT product) { 1189 uint64_t tmp[uECC_WORDS]; 1190 int carry; 1191 1192 /* t */ 1193 vli_set(result, product); 1194 1195 /* s1 */ 1196 tmp[0] = 0; 1197 tmp[1] = product[5] & 0xffffffff00000000ull; 1198 tmp[2] = product[6]; 1199 tmp[3] = product[7]; 1200 carry = vli_add(tmp, tmp, tmp); 1201 carry += vli_add(result, result, tmp); 1202 1203 /* s2 */ 1204 tmp[1] = product[6] << 32; 1205 tmp[2] = (product[6] >> 32) | (product[7] << 32); 1206 tmp[3] = product[7] >> 32; 1207 carry += vli_add(tmp, tmp, tmp); 1208 carry += vli_add(result, result, tmp); 1209 1210 /* s3 */ 1211 tmp[0] = product[4]; 1212 tmp[1] = product[5] & 0xffffffff; 1213 tmp[2] = 0; 1214 tmp[3] = product[7]; 1215 carry += vli_add(result, result, tmp); 1216 1217 /* s4 */ 1218 tmp[0] = (product[4] >> 32) | (product[5] << 32); 1219 tmp[1] = (product[5] >> 32) | (product[6] & 0xffffffff00000000ull); 1220 tmp[2] = product[7]; 1221 tmp[3] = (product[6] >> 32) | (product[4] << 32); 1222 carry += vli_add(result, result, tmp); 1223 1224 /* d1 */ 1225 tmp[0] = (product[5] >> 32) | (product[6] << 32); 1226 tmp[1] = (product[6] >> 32); 1227 tmp[2] = 0; 1228 tmp[3] = (product[4] & 0xffffffff) | (product[5] << 32); 1229 carry -= vli_sub(result, result, tmp); 1230 1231 /* d2 */ 1232 tmp[0] = product[6]; 1233 tmp[1] = product[7]; 1234 tmp[2] = 0; 1235 tmp[3] = (product[4] >> 32) | (product[5] & 0xffffffff00000000ull); 1236 carry -= vli_sub(result, result, tmp); 1237 1238 /* d3 */ 1239 tmp[0] = (product[6] >> 32) | (product[7] << 32); 1240 tmp[1] = (product[7] >> 32) | (product[4] << 32); 1241 tmp[2] = (product[4] >> 32) | (product[5] << 32); 1242 tmp[3] = (product[6] << 32); 1243 carry -= vli_sub(result, result, tmp); 1244 1245 /* d4 */ 1246 tmp[0] = product[7]; 1247 tmp[1] = product[4] & 0xffffffff00000000ull; 1248 tmp[2] = product[5]; 1249 tmp[3] = product[6] & 0xffffffff00000000ull; 1250 carry -= vli_sub(result, result, tmp); 1251 1252 if (carry < 0) { 1253 do { 1254 carry += vli_add(result, result, curve_p); 1255 } while (carry < 0); 1256 } else { 1257 while (carry || vli_cmp(curve_p, result) != 1) { 1258 carry -= vli_sub(result, result, curve_p); 1259 } 1260 } 1261 } 1262 #endif /* uECC_WORD_SIZE */ 1263 1264 #elif uECC_CURVE == uECC_secp256k1 1265 1266 #if uECC_WORD_SIZE == 1 1267 static void omega_mult(uint8_t * RESTRICT result, const uint8_t * RESTRICT right) { 1268 /* Multiply by (2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */ 1269 uECC_word_t r0 = 0; 1270 uECC_word_t r1 = 0; 1271 uECC_word_t r2 = 0; 1272 wordcount_t k; 1273 1274 /* Multiply by (2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */ 1275 muladd(0xD1, right[0], &r0, &r1, &r2); 1276 result[0] = r0; 1277 r0 = r1; 1278 r1 = r2; 1279 /* r2 is still 0 */ 1280 1281 for (k = 1; k < uECC_WORDS; ++k) { 1282 muladd(0x03, right[k - 1], &r0, &r1, &r2); 1283 muladd(0xD1, right[k], &r0, &r1, &r2); 1284 result[k] = r0; 1285 r0 = r1; 1286 r1 = r2; 1287 r2 = 0; 1288 } 1289 muladd(0x03, right[uECC_WORDS - 1], &r0, &r1, &r2); 1290 result[uECC_WORDS] = r0; 1291 result[uECC_WORDS + 1] = r1; 1292 1293 result[4 + uECC_WORDS] = vli_add(result + 4, result + 4, right); /* add the 2^32 multiple */ 1294 } 1295 #elif uECC_WORD_SIZE == 4 1296 static void omega_mult(uint32_t * RESTRICT result, const uint32_t * RESTRICT right) { 1297 /* Multiply by (2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */ 1298 uint32_t carry = 0; 1299 wordcount_t k; 1300 1301 for (k = 0; k < uECC_WORDS; ++k) { 1302 uint64_t p = (uint64_t)0x3D1 * right[k] + carry; 1303 result[k] = (p & 0xffffffff); 1304 carry = p >> 32; 1305 } 1306 result[uECC_WORDS] = carry; 1307 1308 result[1 + uECC_WORDS] = vli_add(result + 1, result + 1, right); /* add the 2^32 multiple */ 1309 } 1310 #else 1311 static void omega_mult(uint64_t * RESTRICT result, const uint64_t * RESTRICT right) { 1312 uECC_word_t r0 = 0; 1313 uECC_word_t r1 = 0; 1314 uECC_word_t r2 = 0; 1315 wordcount_t k; 1316 1317 /* Multiply by (2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */ 1318 for (k = 0; k < uECC_WORDS; ++k) { 1319 muladd(0x1000003D1ull, right[k], &r0, &r1, &r2); 1320 result[k] = r0; 1321 r0 = r1; 1322 r1 = r2; 1323 r2 = 0; 1324 } 1325 result[uECC_WORDS] = r0; 1326 } 1327 #endif /* uECC_WORD_SIZE */ 1328 1329 #elif uECC_CURVE == uECC_secp224r1 1330 1331 /* Computes result = product % curve_p 1332 from http://www.nsa.gov/ia/_files/nist-routines.pdf */ 1333 #if uECC_WORD_SIZE == 1 1334 // TODO it may be faster to use the omega_mult method when fully asm optimized. 1335 void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) { 1336 uint8_t tmp[uECC_WORDS]; 1337 int8_t carry; 1338 1339 /* t */ 1340 vli_set(result, product); 1341 1342 /* s1 */ 1343 tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0; 1344 tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0; 1345 tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0; 1346 tmp[12] = product[28]; tmp[13] = product[29]; tmp[14] = product[30]; tmp[15] = product[31]; 1347 tmp[16] = product[32]; tmp[17] = product[33]; tmp[18] = product[34]; tmp[19] = product[35]; 1348 tmp[20] = product[36]; tmp[21] = product[37]; tmp[22] = product[38]; tmp[23] = product[39]; 1349 tmp[24] = product[40]; tmp[25] = product[41]; tmp[26] = product[42]; tmp[27] = product[43]; 1350 carry = vli_add(result, result, tmp); 1351 1352 /* s2 */ 1353 tmp[12] = product[44]; tmp[13] = product[45]; tmp[14] = product[46]; tmp[15] = product[47]; 1354 tmp[16] = product[48]; tmp[17] = product[49]; tmp[18] = product[50]; tmp[19] = product[51]; 1355 tmp[20] = product[52]; tmp[21] = product[53]; tmp[22] = product[54]; tmp[23] = product[55]; 1356 tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0; 1357 carry += vli_add(result, result, tmp); 1358 1359 /* d1 */ 1360 tmp[0] = product[28]; tmp[1] = product[29]; tmp[2] = product[30]; tmp[3] = product[31]; 1361 tmp[4] = product[32]; tmp[5] = product[33]; tmp[6] = product[34]; tmp[7] = product[35]; 1362 tmp[8] = product[36]; tmp[9] = product[37]; tmp[10] = product[38]; tmp[11] = product[39]; 1363 tmp[12] = product[40]; tmp[13] = product[41]; tmp[14] = product[42]; tmp[15] = product[43]; 1364 tmp[16] = product[44]; tmp[17] = product[45]; tmp[18] = product[46]; tmp[19] = product[47]; 1365 tmp[20] = product[48]; tmp[21] = product[49]; tmp[22] = product[50]; tmp[23] = product[51]; 1366 tmp[24] = product[52]; tmp[25] = product[53]; tmp[26] = product[54]; tmp[27] = product[55]; 1367 carry -= vli_sub(result, result, tmp); 1368 1369 /* d2 */ 1370 tmp[0] = product[44]; tmp[1] = product[45]; tmp[2] = product[46]; tmp[3] = product[47]; 1371 tmp[4] = product[48]; tmp[5] = product[49]; tmp[6] = product[50]; tmp[7] = product[51]; 1372 tmp[8] = product[52]; tmp[9] = product[53]; tmp[10] = product[54]; tmp[11] = product[55]; 1373 tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0; 1374 tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0; 1375 tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0; 1376 tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0; 1377 carry -= vli_sub(result, result, tmp); 1378 1379 if (carry < 0) { 1380 do { 1381 carry += vli_add(result, result, curve_p); 1382 } while (carry < 0); 1383 } else { 1384 while (carry || vli_cmp(curve_p, result) != 1) { 1385 carry -= vli_sub(result, result, curve_p); 1386 } 1387 } 1388 } 1389 #elif uECC_WORD_SIZE == 4 1390 void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product) 1391 { 1392 uint32_t tmp[uECC_WORDS]; 1393 int carry; 1394 1395 /* t */ 1396 vli_set(result, product); 1397 1398 /* s1 */ 1399 tmp[0] = tmp[1] = tmp[2] = 0; 1400 tmp[3] = product[7]; 1401 tmp[4] = product[8]; 1402 tmp[5] = product[9]; 1403 tmp[6] = product[10]; 1404 carry = vli_add(result, result, tmp); 1405 1406 /* s2 */ 1407 tmp[3] = product[11]; 1408 tmp[4] = product[12]; 1409 tmp[5] = product[13]; 1410 tmp[6] = 0; 1411 carry += vli_add(result, result, tmp); 1412 1413 /* d1 */ 1414 tmp[0] = product[7]; 1415 tmp[1] = product[8]; 1416 tmp[2] = product[9]; 1417 tmp[3] = product[10]; 1418 tmp[4] = product[11]; 1419 tmp[5] = product[12]; 1420 tmp[6] = product[13]; 1421 carry -= vli_sub(result, result, tmp); 1422 1423 /* d2 */ 1424 tmp[0] = product[11]; 1425 tmp[1] = product[12]; 1426 tmp[2] = product[13]; 1427 tmp[3] = tmp[4] = tmp[5] = tmp[6] = 0; 1428 carry -= vli_sub(result, result, tmp); 1429 1430 if (carry < 0) { 1431 do { 1432 carry += vli_add(result, result, curve_p); 1433 } while (carry < 0); 1434 } else { 1435 while (carry || vli_cmp(curve_p, result) != 1) { 1436 carry -= vli_sub(result, result, curve_p); 1437 } 1438 } 1439 } 1440 #endif /* uECC_WORD_SIZE */ 1441 1442 #endif /* uECC_CURVE */ 1443 #endif /* !asm_mmod_fast */ 1444 1445 /* Computes result = (left * right) % curve_p. */ 1446 static void vli_modMult_fast(uECC_word_t *result, 1447 const uECC_word_t *left, 1448 const uECC_word_t *right) { 1449 uECC_word_t product[2 * uECC_WORDS]; 1450 vli_mult(product, left, right); 1451 vli_mmod_fast(result, product); 1452 } 1453 1454 #if uECC_SQUARE_FUNC 1455 1456 /* Computes result = left^2 % curve_p. */ 1457 static void vli_modSquare_fast(uECC_word_t *result, const uECC_word_t *left) { 1458 uECC_word_t product[2 * uECC_WORDS]; 1459 vli_square(product, left); 1460 vli_mmod_fast(result, product); 1461 } 1462 1463 #else /* uECC_SQUARE_FUNC */ 1464 1465 #define vli_modSquare_fast(result, left) vli_modMult_fast((result), (left), (left)) 1466 1467 #endif /* uECC_SQUARE_FUNC */ 1468 1469 1470 #define EVEN(vli) (!(vli[0] & 1)) 1471 /* Computes result = (1 / input) % mod. All VLIs are the same size. 1472 See "From Euclid's GCD to Montgomery Multiplication to the Great Divide" 1473 https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf */ 1474 #if !asm_modInv 1475 static void vli_modInv(uECC_word_t *result, const uECC_word_t *input, const uECC_word_t *mod) { 1476 uECC_word_t a[uECC_WORDS], b[uECC_WORDS], u[uECC_WORDS], v[uECC_WORDS]; 1477 uECC_word_t carry; 1478 cmpresult_t cmpResult; 1479 1480 if (vli_isZero(input)) { 1481 vli_clear(result); 1482 return; 1483 } 1484 1485 vli_set(a, input); 1486 vli_set(b, mod); 1487 vli_clear(u); 1488 u[0] = 1; 1489 vli_clear(v); 1490 while ((cmpResult = vli_cmp(a, b)) != 0) { 1491 carry = 0; 1492 if (EVEN(a)) { 1493 vli_rshift1(a); 1494 if (!EVEN(u)) { 1495 carry = vli_add(u, u, mod); 1496 } 1497 vli_rshift1(u); 1498 if (carry) { 1499 u[uECC_WORDS - 1] |= HIGH_BIT_SET; 1500 } 1501 } else if (EVEN(b)) { 1502 vli_rshift1(b); 1503 if (!EVEN(v)) { 1504 carry = vli_add(v, v, mod); 1505 } 1506 vli_rshift1(v); 1507 if (carry) { 1508 v[uECC_WORDS - 1] |= HIGH_BIT_SET; 1509 } 1510 } else if (cmpResult > 0) { 1511 vli_sub(a, a, b); 1512 vli_rshift1(a); 1513 if (vli_cmp(u, v) < 0) { 1514 vli_add(u, u, mod); 1515 } 1516 vli_sub(u, u, v); 1517 if (!EVEN(u)) { 1518 carry = vli_add(u, u, mod); 1519 } 1520 vli_rshift1(u); 1521 if (carry) { 1522 u[uECC_WORDS - 1] |= HIGH_BIT_SET; 1523 } 1524 } else { 1525 vli_sub(b, b, a); 1526 vli_rshift1(b); 1527 if (vli_cmp(v, u) < 0) { 1528 vli_add(v, v, mod); 1529 } 1530 vli_sub(v, v, u); 1531 if (!EVEN(v)) { 1532 carry = vli_add(v, v, mod); 1533 } 1534 vli_rshift1(v); 1535 if (carry) { 1536 v[uECC_WORDS - 1] |= HIGH_BIT_SET; 1537 } 1538 } 1539 } 1540 vli_set(result, u); 1541 } 1542 #endif /* !asm_modInv */ 1543 1544 /* ------ Point operations ------ */ 1545 1546 /* Returns 1 if 'point' is the point at infinity, 0 otherwise. */ 1547 static cmpresult_t EccPoint_isZero(const EccPoint *point) { 1548 return (vli_isZero(point->x) && vli_isZero(point->y)); 1549 } 1550 1551 /* Point multiplication algorithm using Montgomery's ladder with co-Z coordinates. 1552 From http://eprint.iacr.org/2011/338.pdf 1553 */ 1554 1555 /* Double in place */ 1556 #if (uECC_CURVE == uECC_secp256k1) 1557 static void EccPoint_double_jacobian(uECC_word_t * RESTRICT X1, 1558 uECC_word_t * RESTRICT Y1, 1559 uECC_word_t * RESTRICT Z1) { 1560 /* t1 = X, t2 = Y, t3 = Z */ 1561 uECC_word_t t4[uECC_WORDS]; 1562 uECC_word_t t5[uECC_WORDS]; 1563 1564 if (vli_isZero(Z1)) { 1565 return; 1566 } 1567 1568 vli_modSquare_fast(t5, Y1); /* t5 = y1^2 */ 1569 vli_modMult_fast(t4, X1, t5); /* t4 = x1*y1^2 = A */ 1570 vli_modSquare_fast(X1, X1); /* t1 = x1^2 */ 1571 vli_modSquare_fast(t5, t5); /* t5 = y1^4 */ 1572 vli_modMult_fast(Z1, Y1, Z1); /* t3 = y1*z1 = z3 */ 1573 1574 vli_modAdd(Y1, X1, X1, curve_p); /* t2 = 2*x1^2 */ 1575 vli_modAdd(Y1, Y1, X1, curve_p); /* t2 = 3*x1^2 */ 1576 if (vli_testBit(Y1, 0)) { 1577 uECC_word_t carry = vli_add(Y1, Y1, curve_p); 1578 vli_rshift1(Y1); 1579 Y1[uECC_WORDS - 1] |= carry << (uECC_WORD_BITS - 1); 1580 } else { 1581 vli_rshift1(Y1); 1582 } 1583 /* t2 = 3/2*(x1^2) = B */ 1584 1585 vli_modSquare_fast(X1, Y1); /* t1 = B^2 */ 1586 vli_modSub(X1, X1, t4, curve_p); /* t1 = B^2 - A */ 1587 vli_modSub(X1, X1, t4, curve_p); /* t1 = B^2 - 2A = x3 */ 1588 1589 vli_modSub(t4, t4, X1, curve_p); /* t4 = A - x3 */ 1590 vli_modMult_fast(Y1, Y1, t4); /* t2 = B * (A - x3) */ 1591 vli_modSub(Y1, Y1, t5, curve_p); /* t2 = B * (A - x3) - y1^4 = y3 */ 1592 } 1593 #else 1594 static void EccPoint_double_jacobian(uECC_word_t * RESTRICT X1, 1595 uECC_word_t * RESTRICT Y1, 1596 uECC_word_t * RESTRICT Z1) { 1597 /* t1 = X, t2 = Y, t3 = Z */ 1598 uECC_word_t t4[uECC_WORDS]; 1599 uECC_word_t t5[uECC_WORDS]; 1600 1601 if (vli_isZero(Z1)) { 1602 return; 1603 } 1604 1605 vli_modSquare_fast(t4, Y1); /* t4 = y1^2 */ 1606 vli_modMult_fast(t5, X1, t4); /* t5 = x1*y1^2 = A */ 1607 vli_modSquare_fast(t4, t4); /* t4 = y1^4 */ 1608 vli_modMult_fast(Y1, Y1, Z1); /* t2 = y1*z1 = z3 */ 1609 vli_modSquare_fast(Z1, Z1); /* t3 = z1^2 */ 1610 1611 vli_modAdd(X1, X1, Z1, curve_p); /* t1 = x1 + z1^2 */ 1612 vli_modAdd(Z1, Z1, Z1, curve_p); /* t3 = 2*z1^2 */ 1613 vli_modSub_fast(Z1, X1, Z1); /* t3 = x1 - z1^2 */ 1614 vli_modMult_fast(X1, X1, Z1); /* t1 = x1^2 - z1^4 */ 1615 1616 vli_modAdd(Z1, X1, X1, curve_p); /* t3 = 2*(x1^2 - z1^4) */ 1617 vli_modAdd(X1, X1, Z1, curve_p); /* t1 = 3*(x1^2 - z1^4) */ 1618 if (vli_testBit(X1, 0)) { 1619 uECC_word_t l_carry = vli_add(X1, X1, curve_p); 1620 vli_rshift1(X1); 1621 X1[uECC_WORDS - 1] |= l_carry << (uECC_WORD_BITS - 1); 1622 } else { 1623 vli_rshift1(X1); 1624 } 1625 /* t1 = 3/2*(x1^2 - z1^4) = B */ 1626 1627 vli_modSquare_fast(Z1, X1); /* t3 = B^2 */ 1628 vli_modSub_fast(Z1, Z1, t5); /* t3 = B^2 - A */ 1629 vli_modSub_fast(Z1, Z1, t5); /* t3 = B^2 - 2A = x3 */ 1630 vli_modSub_fast(t5, t5, Z1); /* t5 = A - x3 */ 1631 vli_modMult_fast(X1, X1, t5); /* t1 = B * (A - x3) */ 1632 vli_modSub_fast(t4, X1, t4); /* t4 = B * (A - x3) - y1^4 = y3 */ 1633 1634 vli_set(X1, Z1); 1635 vli_set(Z1, Y1); 1636 vli_set(Y1, t4); 1637 } 1638 #endif 1639 1640 /* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */ 1641 static void apply_z(uECC_word_t * RESTRICT X1, 1642 uECC_word_t * RESTRICT Y1, 1643 const uECC_word_t * RESTRICT Z) { 1644 uECC_word_t t1[uECC_WORDS]; 1645 1646 vli_modSquare_fast(t1, Z); /* z^2 */ 1647 vli_modMult_fast(X1, X1, t1); /* x1 * z^2 */ 1648 vli_modMult_fast(t1, t1, Z); /* z^3 */ 1649 vli_modMult_fast(Y1, Y1, t1); /* y1 * z^3 */ 1650 } 1651 1652 /* P = (x1, y1) => 2P, (x2, y2) => P' */ 1653 static void XYcZ_initial_double(uECC_word_t * RESTRICT X1, 1654 uECC_word_t * RESTRICT Y1, 1655 uECC_word_t * RESTRICT X2, 1656 uECC_word_t * RESTRICT Y2, 1657 const uECC_word_t * RESTRICT initial_Z) { 1658 uECC_word_t z[uECC_WORDS]; 1659 if (initial_Z) { 1660 vli_set(z, initial_Z); 1661 } else { 1662 vli_clear(z); 1663 z[0] = 1; 1664 } 1665 1666 vli_set(X2, X1); 1667 vli_set(Y2, Y1); 1668 1669 apply_z(X1, Y1, z); 1670 EccPoint_double_jacobian(X1, Y1, z); 1671 apply_z(X2, Y2, z); 1672 } 1673 1674 /* Input P = (x1, y1, Z), Q = (x2, y2, Z) 1675 Output P' = (x1', y1', Z3), P + Q = (x3, y3, Z3) 1676 or P => P', Q => P + Q 1677 */ 1678 static void XYcZ_add(uECC_word_t * RESTRICT X1, 1679 uECC_word_t * RESTRICT Y1, 1680 uECC_word_t * RESTRICT X2, 1681 uECC_word_t * RESTRICT Y2) { 1682 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */ 1683 uECC_word_t t5[uECC_WORDS]; 1684 1685 vli_modSub_fast(t5, X2, X1); /* t5 = x2 - x1 */ 1686 vli_modSquare_fast(t5, t5); /* t5 = (x2 - x1)^2 = A */ 1687 vli_modMult_fast(X1, X1, t5); /* t1 = x1*A = B */ 1688 vli_modMult_fast(X2, X2, t5); /* t3 = x2*A = C */ 1689 vli_modSub_fast(Y2, Y2, Y1); /* t4 = y2 - y1 */ 1690 vli_modSquare_fast(t5, Y2); /* t5 = (y2 - y1)^2 = D */ 1691 1692 vli_modSub_fast(t5, t5, X1); /* t5 = D - B */ 1693 vli_modSub_fast(t5, t5, X2); /* t5 = D - B - C = x3 */ 1694 vli_modSub_fast(X2, X2, X1); /* t3 = C - B */ 1695 vli_modMult_fast(Y1, Y1, X2); /* t2 = y1*(C - B) */ 1696 vli_modSub_fast(X2, X1, t5); /* t3 = B - x3 */ 1697 vli_modMult_fast(Y2, Y2, X2); /* t4 = (y2 - y1)*(B - x3) */ 1698 vli_modSub_fast(Y2, Y2, Y1); /* t4 = y3 */ 1699 1700 vli_set(X2, t5); 1701 } 1702 1703 /* Input P = (x1, y1, Z), Q = (x2, y2, Z) 1704 Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3) 1705 or P => P - Q, Q => P + Q 1706 */ 1707 static void XYcZ_addC(uECC_word_t * RESTRICT X1, 1708 uECC_word_t * RESTRICT Y1, 1709 uECC_word_t * RESTRICT X2, 1710 uECC_word_t * RESTRICT Y2) { 1711 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */ 1712 uECC_word_t t5[uECC_WORDS]; 1713 uECC_word_t t6[uECC_WORDS]; 1714 uECC_word_t t7[uECC_WORDS]; 1715 1716 vli_modSub_fast(t5, X2, X1); /* t5 = x2 - x1 */ 1717 vli_modSquare_fast(t5, t5); /* t5 = (x2 - x1)^2 = A */ 1718 vli_modMult_fast(X1, X1, t5); /* t1 = x1*A = B */ 1719 vli_modMult_fast(X2, X2, t5); /* t3 = x2*A = C */ 1720 vli_modAdd(t5, Y2, Y1, curve_p); /* t5 = y2 + y1 */ 1721 vli_modSub_fast(Y2, Y2, Y1); /* t4 = y2 - y1 */ 1722 1723 vli_modSub_fast(t6, X2, X1); /* t6 = C - B */ 1724 vli_modMult_fast(Y1, Y1, t6); /* t2 = y1 * (C - B) = E */ 1725 vli_modAdd(t6, X1, X2, curve_p); /* t6 = B + C */ 1726 vli_modSquare_fast(X2, Y2); /* t3 = (y2 - y1)^2 = D */ 1727 vli_modSub_fast(X2, X2, t6); /* t3 = D - (B + C) = x3 */ 1728 1729 vli_modSub_fast(t7, X1, X2); /* t7 = B - x3 */ 1730 vli_modMult_fast(Y2, Y2, t7); /* t4 = (y2 - y1)*(B - x3) */ 1731 vli_modSub_fast(Y2, Y2, Y1); /* t4 = (y2 - y1)*(B - x3) - E = y3 */ 1732 1733 vli_modSquare_fast(t7, t5); /* t7 = (y2 + y1)^2 = F */ 1734 vli_modSub_fast(t7, t7, t6); /* t7 = F - (B + C) = x3' */ 1735 vli_modSub_fast(t6, t7, X1); /* t6 = x3' - B */ 1736 vli_modMult_fast(t6, t6, t5); /* t6 = (y2 + y1)*(x3' - B) */ 1737 vli_modSub_fast(Y1, t6, Y1); /* t2 = (y2 + y1)*(x3' - B) - E = y3' */ 1738 1739 vli_set(X1, t7); 1740 } 1741 1742 static void EccPoint_mult(EccPoint * RESTRICT result, 1743 const EccPoint * RESTRICT point, 1744 const uECC_word_t * RESTRICT scalar, 1745 const uECC_word_t * RESTRICT initialZ, 1746 bitcount_t numBits) { 1747 /* R0 and R1 */ 1748 uECC_word_t Rx[2][uECC_WORDS]; 1749 uECC_word_t Ry[2][uECC_WORDS]; 1750 uECC_word_t z[uECC_WORDS]; 1751 bitcount_t i; 1752 uECC_word_t nb; 1753 1754 vli_set(Rx[1], point->x); 1755 vli_set(Ry[1], point->y); 1756 1757 XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], initialZ); 1758 1759 for (i = numBits - 2; i > 0; --i) { 1760 nb = !vli_testBit(scalar, i); 1761 XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb]); 1762 XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb]); 1763 } 1764 1765 nb = !vli_testBit(scalar, 0); 1766 XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb]); 1767 1768 /* Find final 1/Z value. */ 1769 vli_modSub_fast(z, Rx[1], Rx[0]); /* X1 - X0 */ 1770 vli_modMult_fast(z, z, Ry[1 - nb]); /* Yb * (X1 - X0) */ 1771 vli_modMult_fast(z, z, point->x); /* xP * Yb * (X1 - X0) */ 1772 vli_modInv(z, z, curve_p); /* 1 / (xP * Yb * (X1 - X0)) */ 1773 vli_modMult_fast(z, z, point->y); /* yP / (xP * Yb * (X1 - X0)) */ 1774 vli_modMult_fast(z, z, Rx[1 - nb]); /* Xb * yP / (xP * Yb * (X1 - X0)) */ 1775 /* End 1/Z calculation */ 1776 1777 XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb]); 1778 apply_z(Rx[0], Ry[0], z); 1779 1780 vli_set(result->x, Rx[0]); 1781 vli_set(result->y, Ry[0]); 1782 } 1783 1784 static int EccPoint_compute_public_key(EccPoint *result, uECC_word_t *private) { 1785 uECC_word_t tmp1[uECC_WORDS]; 1786 uECC_word_t tmp2[uECC_WORDS]; 1787 uECC_word_t *p2[2] = {tmp1, tmp2}; 1788 uECC_word_t carry; 1789 1790 /* Make sure the private key is in the range [1, n-1]. */ 1791 if (vli_isZero(private)) { 1792 return 0; 1793 } 1794 1795 #if (uECC_CURVE == uECC_secp160r1) 1796 // Don't regularize the bitcount for secp160r1, since it would have a larger performance 1797 // impact (about 2% slower on average) and requires the vli_xxx_n functions, leading to 1798 // a significant increase in code size. 1799 1800 EccPoint_mult(result, &curve_G, private, NULL, vli_numBits(private, uECC_WORDS)); 1801 #else 1802 if (vli_cmp(curve_n, private) != 1) { 1803 return 0; 1804 } 1805 1806 // Regularize the bitcount for the private key so that attackers cannot use a side channel 1807 // attack to learn the number of leading zeros. 1808 carry = vli_add(tmp1, private, curve_n); 1809 vli_add(tmp2, tmp1, curve_n); 1810 EccPoint_mult(result, &curve_G, p2[!carry], NULL, (uECC_BYTES * 8) + 1); 1811 #endif 1812 1813 if (EccPoint_isZero(result)) { 1814 return 0; 1815 } 1816 return 1; 1817 } 1818 1819 #if uECC_CURVE == uECC_secp224r1 1820 1821 /* Routine 3.2.4 RS; from http://www.nsa.gov/ia/_files/nist-routines.pdf */ 1822 static void mod_sqrt_secp224r1_rs(uECC_word_t *d1, 1823 uECC_word_t *e1, 1824 uECC_word_t *f1, 1825 const uECC_word_t *d0, 1826 const uECC_word_t *e0, 1827 const uECC_word_t *f0) { 1828 uECC_word_t t[uECC_WORDS]; 1829 1830 vli_modSquare_fast(t, d0); /* t <-- d0 ^ 2 */ 1831 vli_modMult_fast(e1, d0, e0); /* e1 <-- d0 * e0 */ 1832 vli_modAdd(d1, t, f0, curve_p); /* d1 <-- t + f0 */ 1833 vli_modAdd(e1, e1, e1, curve_p); /* e1 <-- e1 + e1 */ 1834 vli_modMult_fast(f1, t, f0); /* f1 <-- t * f0 */ 1835 vli_modAdd(f1, f1, f1, curve_p); /* f1 <-- f1 + f1 */ 1836 vli_modAdd(f1, f1, f1, curve_p); /* f1 <-- f1 + f1 */ 1837 } 1838 1839 /* Routine 3.2.5 RSS; from http://www.nsa.gov/ia/_files/nist-routines.pdf */ 1840 static void mod_sqrt_secp224r1_rss(uECC_word_t *d1, 1841 uECC_word_t *e1, 1842 uECC_word_t *f1, 1843 const uECC_word_t *d0, 1844 const uECC_word_t *e0, 1845 const uECC_word_t *f0, 1846 const bitcount_t j) { 1847 bitcount_t i; 1848 1849 vli_set(d1, d0); /* d1 <-- d0 */ 1850 vli_set(e1, e0); /* e1 <-- e0 */ 1851 vli_set(f1, f0); /* f1 <-- f0 */ 1852 for (i = 1; i <= j; i++) { 1853 mod_sqrt_secp224r1_rs(d1, e1, f1, d1, e1, f1); /* RS (d1,e1,f1,d1,e1,f1) */ 1854 } 1855 } 1856 1857 /* Routine 3.2.6 RM; from http://www.nsa.gov/ia/_files/nist-routines.pdf */ 1858 static void mod_sqrt_secp224r1_rm(uECC_word_t *d2, 1859 uECC_word_t *e2, 1860 uECC_word_t *f2, 1861 const uECC_word_t *c, 1862 const uECC_word_t *d0, 1863 const uECC_word_t *e0, 1864 const uECC_word_t *d1, 1865 const uECC_word_t *e1) { 1866 uECC_word_t t1[uECC_WORDS]; 1867 uECC_word_t t2[uECC_WORDS]; 1868 1869 vli_modMult_fast(t1, e0, e1); /* t1 <-- e0 * e1 */ 1870 vli_modMult_fast(t1, t1, c); /* t1 <-- t1 * c */ 1871 vli_modSub_fast(t1, curve_p, t1); /* t1 <-- p - t1 */ 1872 vli_modMult_fast(t2, d0, d1); /* t2 <-- d0 * d1 */ 1873 vli_modAdd(t2, t2, t1, curve_p); /* t2 <-- t2 + t1 */ 1874 vli_modMult_fast(t1, d0, e1); /* t1 <-- d0 * e1 */ 1875 vli_modMult_fast(e2, d1, e0); /* e2 <-- d1 * e0 */ 1876 vli_modAdd(e2, e2, t1, curve_p); /* e2 <-- e2 + t1 */ 1877 vli_modSquare_fast(f2, e2); /* f2 <-- e2^2 */ 1878 vli_modMult_fast(f2, f2, c); /* f2 <-- f2 * c */ 1879 vli_modSub_fast(f2, curve_p, f2); /* f2 <-- p - f2 */ 1880 vli_set(d2, t2); /* d2 <-- t2 */ 1881 } 1882 1883 /* Routine 3.2.7 RP; from http://www.nsa.gov/ia/_files/nist-routines.pdf */ 1884 static void mod_sqrt_secp224r1_rp(uECC_word_t *d1, 1885 uECC_word_t *e1, 1886 uECC_word_t *f1, 1887 const uECC_word_t *c, 1888 const uECC_word_t *r) { 1889 wordcount_t i; 1890 wordcount_t pow2i = 1; 1891 uECC_word_t d0[uECC_WORDS]; 1892 uECC_word_t e0[uECC_WORDS] = {1}; /* e0 <-- 1 */ 1893 uECC_word_t f0[uECC_WORDS]; 1894 1895 vli_set(d0, r); /* d0 <-- r */ 1896 vli_modSub_fast(f0, curve_p, c); /* f0 <-- p - c */ 1897 for (i = 0; i <= 6; i++) { 1898 mod_sqrt_secp224r1_rss(d1, e1, f1, d0, e0, f0, pow2i); /* RSS (d1,e1,f1,d0,e0,f0,2^i) */ 1899 mod_sqrt_secp224r1_rm(d1, e1, f1, c, d1, e1, d0, e0); /* RM (d1,e1,f1,c,d1,e1,d0,e0) */ 1900 vli_set(d0, d1); /* d0 <-- d1 */ 1901 vli_set(e0, e1); /* e0 <-- e1 */ 1902 vli_set(f0, f1); /* f0 <-- f1 */ 1903 pow2i *= 2; 1904 } 1905 } 1906 1907 /* Compute a = sqrt(a) (mod curve_p). */ 1908 /* Routine 3.2.8 mp_mod_sqrt_224; from http://www.nsa.gov/ia/_files/nist-routines.pdf */ 1909 static void mod_sqrt(uECC_word_t *a) { 1910 bitcount_t i; 1911 uECC_word_t e1[uECC_WORDS]; 1912 uECC_word_t f1[uECC_WORDS]; 1913 uECC_word_t d0[uECC_WORDS]; 1914 uECC_word_t e0[uECC_WORDS]; 1915 uECC_word_t f0[uECC_WORDS]; 1916 uECC_word_t d1[uECC_WORDS]; 1917 1918 // s = a; using constant instead of random value 1919 mod_sqrt_secp224r1_rp(d0, e0, f0, a, a); /* RP (d0, e0, f0, c, s) */ 1920 mod_sqrt_secp224r1_rs(d1, e1, f1, d0, e0, f0); /* RS (d1, e1, f1, d0, e0, f0) */ 1921 for (i = 1; i <= 95; i++) { 1922 vli_set(d0, d1); /* d0 <-- d1 */ 1923 vli_set(e0, e1); /* e0 <-- e1 */ 1924 vli_set(f0, f1); /* f0 <-- f1 */ 1925 mod_sqrt_secp224r1_rs(d1, e1, f1, d0, e0, f0); /* RS (d1, e1, f1, d0, e0, f0) */ 1926 if (vli_isZero(d1)) { /* if d1 == 0 */ 1927 break; 1928 } 1929 } 1930 vli_modInv(f1, e0, curve_p); /* f1 <-- 1 / e0 */ 1931 vli_modMult_fast(a, d0, f1); /* a <-- d0 / e0 */ 1932 } 1933 1934 #else /* uECC_CURVE */ 1935 1936 /* Compute a = sqrt(a) (mod curve_p). */ 1937 static void mod_sqrt(uECC_word_t *a) { 1938 bitcount_t i; 1939 uECC_word_t p1[uECC_WORDS] = {1}; 1940 uECC_word_t l_result[uECC_WORDS] = {1}; 1941 1942 /* Since curve_p == 3 (mod 4) for all supported curves, we can 1943 compute sqrt(a) = a^((curve_p + 1) / 4) (mod curve_p). */ 1944 vli_add(p1, curve_p, p1); /* p1 = curve_p + 1 */ 1945 for (i = vli_numBits(p1, uECC_WORDS) - 1; i > 1; --i) { 1946 vli_modSquare_fast(l_result, l_result); 1947 if (vli_testBit(p1, i)) { 1948 vli_modMult_fast(l_result, l_result, a); 1949 } 1950 } 1951 vli_set(a, l_result); 1952 } 1953 #endif /* uECC_CURVE */ 1954 1955 #if uECC_WORD_SIZE == 1 1956 1957 static void vli_nativeToBytes(uint8_t * RESTRICT dest, const uint8_t * RESTRICT src) { 1958 uint8_t i; 1959 for (i = 0; i < uECC_BYTES; ++i) { 1960 dest[i] = src[(uECC_BYTES - 1) - i]; 1961 } 1962 } 1963 1964 #define vli_bytesToNative(dest, src) vli_nativeToBytes((dest), (src)) 1965 1966 #elif uECC_WORD_SIZE == 4 1967 1968 static void vli_nativeToBytes(uint8_t *bytes, const uint32_t *native) { 1969 unsigned i; 1970 for (i = 0; i < uECC_WORDS; ++i) { 1971 uint8_t *digit = bytes + 4 * (uECC_WORDS - 1 - i); 1972 digit[0] = native[i] >> 24; 1973 digit[1] = native[i] >> 16; 1974 digit[2] = native[i] >> 8; 1975 digit[3] = native[i]; 1976 } 1977 } 1978 1979 static void vli_bytesToNative(uint32_t *native, const uint8_t *bytes) { 1980 unsigned i; 1981 for (i = 0; i < uECC_WORDS; ++i) { 1982 const uint8_t *digit = bytes + 4 * (uECC_WORDS - 1 - i); 1983 native[i] = ((uint32_t)digit[0] << 24) | ((uint32_t)digit[1] << 16) | 1984 ((uint32_t)digit[2] << 8) | (uint32_t)digit[3]; 1985 } 1986 } 1987 1988 #else 1989 1990 static void vli_nativeToBytes(uint8_t *bytes, const uint64_t *native) { 1991 unsigned i; 1992 for (i = 0; i < uECC_WORDS; ++i) { 1993 uint8_t *digit = bytes + 8 * (uECC_WORDS - 1 - i); 1994 digit[0] = native[i] >> 56; 1995 digit[1] = native[i] >> 48; 1996 digit[2] = native[i] >> 40; 1997 digit[3] = native[i] >> 32; 1998 digit[4] = native[i] >> 24; 1999 digit[5] = native[i] >> 16; 2000 digit[6] = native[i] >> 8; 2001 digit[7] = native[i]; 2002 } 2003 } 2004 2005 static void vli_bytesToNative(uint64_t *native, const uint8_t *bytes) { 2006 unsigned i; 2007 for (i = 0; i < uECC_WORDS; ++i) { 2008 const uint8_t *digit = bytes + 8 * (uECC_WORDS - 1 - i); 2009 native[i] = ((uint64_t)digit[0] << 56) | ((uint64_t)digit[1] << 48) | 2010 ((uint64_t)digit[2] << 40) | ((uint64_t)digit[3] << 32) | 2011 ((uint64_t)digit[4] << 24) | ((uint64_t)digit[5] << 16) | 2012 ((uint64_t)digit[6] << 8) | (uint64_t)digit[7]; 2013 } 2014 } 2015 2016 #endif /* uECC_WORD_SIZE */ 2017 2018 int uECC_make_key(uint8_t public_key[uECC_BYTES*2], uint8_t private_key[uECC_BYTES]) { 2019 uECC_word_t private[uECC_WORDS]; 2020 EccPoint public; 2021 uECC_word_t tries; 2022 for (tries = 0; tries < MAX_TRIES; ++tries) { 2023 if (g_rng_function((uint8_t *)private, sizeof(private)) && 2024 EccPoint_compute_public_key(&public, private)) { 2025 vli_nativeToBytes(private_key, private); 2026 vli_nativeToBytes(public_key, public.x); 2027 vli_nativeToBytes(public_key + uECC_BYTES, public.y); 2028 return 1; 2029 } 2030 } 2031 return 0; 2032 } 2033 2034 int uECC_shared_secret(const uint8_t public_key[uECC_BYTES*2], 2035 const uint8_t private_key[uECC_BYTES], 2036 uint8_t secret[uECC_BYTES]) { 2037 EccPoint public; 2038 EccPoint product; 2039 uECC_word_t private[uECC_WORDS]; 2040 uECC_word_t tmp[uECC_WORDS]; 2041 uECC_word_t *p2[2] = {private, tmp}; 2042 uECC_word_t random[uECC_WORDS]; 2043 uECC_word_t *initial_Z = NULL; 2044 uECC_word_t tries; 2045 uECC_word_t carry; 2046 2047 // Try to get a random initial Z value to improve protection against side-channel 2048 // attacks. If the RNG fails every time (eg it was not defined), we continue so that 2049 // uECC_shared_secret() can still work without an RNG defined. 2050 for (tries = 0; tries < MAX_TRIES; ++tries) { 2051 if (g_rng_function((uint8_t *)random, sizeof(random)) && !vli_isZero(random)) { 2052 initial_Z = random; 2053 break; 2054 } 2055 } 2056 2057 vli_bytesToNative(private, private_key); 2058 vli_bytesToNative(public.x, public_key); 2059 vli_bytesToNative(public.y, public_key + uECC_BYTES); 2060 2061 #if (uECC_CURVE == uECC_secp160r1) 2062 // Don't regularize the bitcount for secp160r1. 2063 EccPoint_mult(&product, &public, private, initial_Z, vli_numBits(private, uECC_WORDS)); 2064 #else 2065 // Regularize the bitcount for the private key so that attackers cannot use a side channel 2066 // attack to learn the number of leading zeros. 2067 carry = vli_add(private, private, curve_n); 2068 vli_add(tmp, private, curve_n); 2069 EccPoint_mult(&product, &public, p2[!carry], initial_Z, (uECC_BYTES * 8) + 1); 2070 #endif 2071 2072 vli_nativeToBytes(secret, product.x); 2073 return !EccPoint_isZero(&product); 2074 } 2075 2076 void uECC_compress(const uint8_t public_key[uECC_BYTES*2], uint8_t compressed[uECC_BYTES+1]) { 2077 wordcount_t i; 2078 for (i = 0; i < uECC_BYTES; ++i) { 2079 compressed[i+1] = public_key[i]; 2080 } 2081 compressed[0] = 2 + (public_key[uECC_BYTES * 2 - 1] & 0x01); 2082 } 2083 2084 /* Computes result = x^3 + ax + b. result must not overlap x. */ 2085 static void curve_x_side(uECC_word_t * RESTRICT result, const uECC_word_t * RESTRICT x) { 2086 #if (uECC_CURVE == uECC_secp256k1) 2087 vli_modSquare_fast(result, x); /* r = x^2 */ 2088 vli_modMult_fast(result, result, x); /* r = x^3 */ 2089 vli_modAdd(result, result, curve_b, curve_p); /* r = x^3 + b */ 2090 #else 2091 uECC_word_t _3[uECC_WORDS] = {3}; /* -a = 3 */ 2092 2093 vli_modSquare_fast(result, x); /* r = x^2 */ 2094 vli_modSub_fast(result, result, _3); /* r = x^2 - 3 */ 2095 vli_modMult_fast(result, result, x); /* r = x^3 - 3x */ 2096 vli_modAdd(result, result, curve_b, curve_p); /* r = x^3 - 3x + b */ 2097 #endif 2098 } 2099 2100 void uECC_decompress(const uint8_t compressed[uECC_BYTES+1], uint8_t public_key[uECC_BYTES*2]) { 2101 EccPoint point; 2102 vli_bytesToNative(point.x, compressed + 1); 2103 curve_x_side(point.y, point.x); 2104 mod_sqrt(point.y); 2105 2106 if ((point.y[0] & 0x01) != (compressed[0] & 0x01)) { 2107 vli_sub(point.y, curve_p, point.y); 2108 } 2109 2110 vli_nativeToBytes(public_key, point.x); 2111 vli_nativeToBytes(public_key + uECC_BYTES, point.y); 2112 } 2113 2114 int uECC_valid_public_key(const uint8_t public_key[uECC_BYTES*2]) { 2115 uECC_word_t tmp1[uECC_WORDS]; 2116 uECC_word_t tmp2[uECC_WORDS]; 2117 EccPoint public; 2118 2119 vli_bytesToNative(public.x, public_key); 2120 vli_bytesToNative(public.y, public_key + uECC_BYTES); 2121 2122 // The point at infinity is invalid. 2123 if (EccPoint_isZero(&public)) { 2124 return 0; 2125 } 2126 2127 // x and y must be smaller than p. 2128 if (vli_cmp(curve_p, public.x) != 1 || vli_cmp(curve_p, public.y) != 1) { 2129 return 0; 2130 } 2131 2132 vli_modSquare_fast(tmp1, public.y); /* tmp1 = y^2 */ 2133 curve_x_side(tmp2, public.x); /* tmp2 = x^3 + ax + b */ 2134 2135 /* Make sure that y^2 == x^3 + ax + b */ 2136 return (vli_cmp(tmp1, tmp2) == 0); 2137 } 2138 2139 int uECC_compute_public_key(const uint8_t private_key[uECC_BYTES], 2140 uint8_t public_key[uECC_BYTES * 2]) { 2141 uECC_word_t private[uECC_WORDS]; 2142 EccPoint public; 2143 2144 vli_bytesToNative(private, private_key); 2145 2146 if (!EccPoint_compute_public_key(&public, private)) { 2147 return 0; 2148 } 2149 2150 vli_nativeToBytes(public_key, public.x); 2151 vli_nativeToBytes(public_key + uECC_BYTES, public.y); 2152 return 1; 2153 } 2154 2155 int uECC_bytes(void) { 2156 return uECC_BYTES; 2157 } 2158 2159 int uECC_curve(void) { 2160 return uECC_CURVE; 2161 } 2162 2163 /* -------- ECDSA code -------- */ 2164 2165 #if (uECC_CURVE == uECC_secp160r1) 2166 static void vli_clear_n(uECC_word_t *vli) { 2167 vli_clear(vli); 2168 vli[uECC_N_WORDS - 1] = 0; 2169 } 2170 2171 static uECC_word_t vli_isZero_n(const uECC_word_t *vli) { 2172 if (vli[uECC_N_WORDS - 1]) { 2173 return 0; 2174 } 2175 return vli_isZero(vli); 2176 } 2177 2178 static void vli_set_n(uECC_word_t *dest, const uECC_word_t *src) { 2179 vli_set(dest, src); 2180 dest[uECC_N_WORDS - 1] = src[uECC_N_WORDS - 1]; 2181 } 2182 2183 static cmpresult_t vli_cmp_n(const uECC_word_t *left, const uECC_word_t *right) { 2184 if (left[uECC_N_WORDS - 1] > right[uECC_N_WORDS - 1]) { 2185 return 1; 2186 } else if (left[uECC_N_WORDS - 1] < right[uECC_N_WORDS - 1]) { 2187 return -1; 2188 } 2189 return vli_cmp(left, right); 2190 } 2191 2192 static void vli_rshift1_n(uECC_word_t *vli) { 2193 vli_rshift1(vli); 2194 vli[uECC_N_WORDS - 2] |= vli[uECC_N_WORDS - 1] << (uECC_WORD_BITS - 1); 2195 vli[uECC_N_WORDS - 1] = vli[uECC_N_WORDS - 1] >> 1; 2196 } 2197 2198 static uECC_word_t vli_add_n(uECC_word_t *result, 2199 const uECC_word_t *left, 2200 const uECC_word_t *right) { 2201 uECC_word_t carry = vli_add(result, left, right); 2202 uECC_word_t sum = left[uECC_N_WORDS - 1] + right[uECC_N_WORDS - 1] + carry; 2203 if (sum != left[uECC_N_WORDS - 1]) { 2204 carry = (sum < left[uECC_N_WORDS - 1]); 2205 } 2206 result[uECC_N_WORDS - 1] = sum; 2207 return carry; 2208 } 2209 2210 static uECC_word_t vli_sub_n(uECC_word_t *result, 2211 const uECC_word_t *left, 2212 const uECC_word_t *right) { 2213 uECC_word_t borrow = vli_sub(result, left, right); 2214 uECC_word_t diff = left[uECC_N_WORDS - 1] - right[uECC_N_WORDS - 1] - borrow; 2215 if (diff != left[uECC_N_WORDS - 1]) { 2216 borrow = (diff > left[uECC_N_WORDS - 1]); 2217 } 2218 result[uECC_N_WORDS - 1] = diff; 2219 return borrow; 2220 } 2221 2222 #if !muladd_exists 2223 static void muladd(uECC_word_t a, 2224 uECC_word_t b, 2225 uECC_word_t *r0, 2226 uECC_word_t *r1, 2227 uECC_word_t *r2) { 2228 uECC_dword_t p = (uECC_dword_t)a * b; 2229 uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0; 2230 r01 += p; 2231 *r2 += (r01 < p); 2232 *r1 = r01 >> uECC_WORD_BITS; 2233 *r0 = (uECC_word_t)r01; 2234 } 2235 #define muladd_exists 1 2236 #endif 2237 2238 static void vli_mult_n(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { 2239 uECC_word_t r0 = 0; 2240 uECC_word_t r1 = 0; 2241 uECC_word_t r2 = 0; 2242 wordcount_t i, k; 2243 2244 for (k = 0; k < uECC_N_WORDS * 2 - 1; ++k) { 2245 wordcount_t min = (k < uECC_N_WORDS ? 0 : (k + 1) - uECC_N_WORDS); 2246 wordcount_t max = (k < uECC_N_WORDS ? k : uECC_N_WORDS - 1); 2247 for (i = min; i <= max; ++i) { 2248 muladd(left[i], right[k - i], &r0, &r1, &r2); 2249 } 2250 result[k] = r0; 2251 r0 = r1; 2252 r1 = r2; 2253 r2 = 0; 2254 } 2255 result[uECC_N_WORDS * 2 - 1] = r0; 2256 } 2257 2258 static void vli_modAdd_n(uECC_word_t *result, 2259 const uECC_word_t *left, 2260 const uECC_word_t *right, 2261 const uECC_word_t *mod) { 2262 uECC_word_t carry = vli_add_n(result, left, right); 2263 if (carry || vli_cmp_n(result, mod) >= 0) { 2264 vli_sub_n(result, result, mod); 2265 } 2266 } 2267 2268 static void vli_modInv_n(uECC_word_t *result, const uECC_word_t *input, const uECC_word_t *mod) { 2269 uECC_word_t a[uECC_N_WORDS], b[uECC_N_WORDS], u[uECC_N_WORDS], v[uECC_N_WORDS]; 2270 uECC_word_t carry; 2271 cmpresult_t cmpResult; 2272 2273 if (vli_isZero_n(input)) { 2274 vli_clear_n(result); 2275 return; 2276 } 2277 2278 vli_set_n(a, input); 2279 vli_set_n(b, mod); 2280 vli_clear_n(u); 2281 u[0] = 1; 2282 vli_clear_n(v); 2283 while ((cmpResult = vli_cmp_n(a, b)) != 0) { 2284 carry = 0; 2285 if (EVEN(a)) { 2286 vli_rshift1_n(a); 2287 if (!EVEN(u)) { 2288 carry = vli_add_n(u, u, mod); 2289 } 2290 vli_rshift1_n(u); 2291 if (carry) { 2292 u[uECC_N_WORDS - 1] |= HIGH_BIT_SET; 2293 } 2294 } else if (EVEN(b)) { 2295 vli_rshift1_n(b); 2296 if (!EVEN(v)) { 2297 carry = vli_add_n(v, v, mod); 2298 } 2299 vli_rshift1_n(v); 2300 if (carry) { 2301 v[uECC_N_WORDS - 1] |= HIGH_BIT_SET; 2302 } 2303 } else if (cmpResult > 0) { 2304 vli_sub_n(a, a, b); 2305 vli_rshift1_n(a); 2306 if (vli_cmp_n(u, v) < 0) { 2307 vli_add_n(u, u, mod); 2308 } 2309 vli_sub_n(u, u, v); 2310 if (!EVEN(u)) { 2311 carry = vli_add_n(u, u, mod); 2312 } 2313 vli_rshift1_n(u); 2314 if (carry) { 2315 u[uECC_N_WORDS - 1] |= HIGH_BIT_SET; 2316 } 2317 } else { 2318 vli_sub_n(b, b, a); 2319 vli_rshift1_n(b); 2320 if (vli_cmp_n(v, u) < 0) { 2321 vli_add_n(v, v, mod); 2322 } 2323 vli_sub_n(v, v, u); 2324 if (!EVEN(v)) { 2325 carry = vli_add_n(v, v, mod); 2326 } 2327 vli_rshift1_n(v); 2328 if (carry) { 2329 v[uECC_N_WORDS - 1] |= HIGH_BIT_SET; 2330 } 2331 } 2332 } 2333 vli_set_n(result, u); 2334 } 2335 2336 static void vli2_rshift1_n(uECC_word_t *vli) { 2337 vli_rshift1_n(vli); 2338 vli[uECC_N_WORDS - 1] |= vli[uECC_N_WORDS] << (uECC_WORD_BITS - 1); 2339 vli_rshift1_n(vli + uECC_N_WORDS); 2340 } 2341 2342 static uECC_word_t vli2_sub_n(uECC_word_t *result, 2343 const uECC_word_t *left, 2344 const uECC_word_t *right) { 2345 uECC_word_t borrow = 0; 2346 wordcount_t i; 2347 for (i = 0; i < uECC_N_WORDS * 2; ++i) { 2348 uECC_word_t diff = left[i] - right[i] - borrow; 2349 if (diff != left[i]) { 2350 borrow = (diff > left[i]); 2351 } 2352 result[i] = diff; 2353 } 2354 return borrow; 2355 } 2356 2357 /* Computes result = (left * right) % curve_n. */ 2358 static void vli_modMult_n(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { 2359 bitcount_t i; 2360 uECC_word_t product[2 * uECC_N_WORDS]; 2361 uECC_word_t modMultiple[2 * uECC_N_WORDS]; 2362 uECC_word_t tmp[2 * uECC_N_WORDS]; 2363 uECC_word_t *v[2] = {tmp, product}; 2364 uECC_word_t index = 1; 2365 2366 vli_mult_n(product, left, right); 2367 vli_clear_n(modMultiple); 2368 vli_set(modMultiple + uECC_N_WORDS + 1, curve_n); 2369 vli_rshift1(modMultiple + uECC_N_WORDS + 1); 2370 modMultiple[2 * uECC_N_WORDS - 1] |= HIGH_BIT_SET; 2371 modMultiple[uECC_N_WORDS] = HIGH_BIT_SET; 2372 2373 for (i = 0; 2374 i <= ((((bitcount_t)uECC_N_WORDS) << uECC_WORD_BITS_SHIFT) + (uECC_WORD_BITS - 1)); 2375 ++i) { 2376 uECC_word_t borrow = vli2_sub_n(v[1 - index], v[index], modMultiple); 2377 index = !(index ^ borrow); /* Swap the index if there was no borrow */ 2378 vli2_rshift1_n(modMultiple); 2379 } 2380 vli_set_n(result, v[index]); 2381 } 2382 2383 #else 2384 2385 #define vli_cmp_n vli_cmp 2386 #define vli_modInv_n vli_modInv 2387 #define vli_modAdd_n vli_modAdd 2388 2389 static void vli2_rshift1(uECC_word_t *vli) { 2390 vli_rshift1(vli); 2391 vli[uECC_WORDS - 1] |= vli[uECC_WORDS] << (uECC_WORD_BITS - 1); 2392 vli_rshift1(vli + uECC_WORDS); 2393 } 2394 2395 static uECC_word_t vli2_sub(uECC_word_t *result, 2396 const uECC_word_t *left, 2397 const uECC_word_t *right) { 2398 uECC_word_t borrow = 0; 2399 wordcount_t i; 2400 for (i = 0; i < uECC_WORDS * 2; ++i) { 2401 uECC_word_t diff = left[i] - right[i] - borrow; 2402 if (diff != left[i]) { 2403 borrow = (diff > left[i]); 2404 } 2405 result[i] = diff; 2406 } 2407 return borrow; 2408 } 2409 2410 /* Computes result = (left * right) % curve_n. */ 2411 static void vli_modMult_n(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { 2412 uECC_word_t product[2 * uECC_WORDS]; 2413 uECC_word_t modMultiple[2 * uECC_WORDS]; 2414 uECC_word_t tmp[2 * uECC_WORDS]; 2415 uECC_word_t *v[2] = {tmp, product}; 2416 bitcount_t i; 2417 uECC_word_t index = 1; 2418 2419 vli_mult(product, left, right); 2420 vli_set(modMultiple + uECC_WORDS, curve_n); /* works if curve_n has its highest bit set */ 2421 vli_clear(modMultiple); 2422 2423 for (i = 0; i <= uECC_BYTES * 8; ++i) { 2424 uECC_word_t borrow = vli2_sub(v[1 - index], v[index], modMultiple); 2425 index = !(index ^ borrow); /* Swap the index if there was no borrow */ 2426 vli2_rshift1(modMultiple); 2427 } 2428 vli_set(result, v[index]); 2429 } 2430 #endif /* (uECC_CURVE != uECC_secp160r1) */ 2431 2432 static int uECC_sign_with_k(const uint8_t private_key[uECC_BYTES], 2433 const uint8_t message_hash[uECC_BYTES], 2434 uECC_word_t k[uECC_N_WORDS], 2435 uint8_t signature[uECC_BYTES*2]) { 2436 uECC_word_t tmp[uECC_N_WORDS]; 2437 uECC_word_t s[uECC_N_WORDS]; 2438 uECC_word_t *k2[2] = {tmp, s}; 2439 EccPoint p; 2440 uECC_word_t carry; 2441 uECC_word_t tries; 2442 2443 /* Make sure 0 < k < curve_n */ 2444 if (vli_isZero(k) || vli_cmp_n(curve_n, k) != 1) { 2445 return 0; 2446 } 2447 2448 #if (uECC_CURVE == uECC_secp160r1) 2449 /* Make sure that we don't leak timing information about k. 2450 See http://eprint.iacr.org/2011/232.pdf */ 2451 vli_add_n(tmp, k, curve_n); 2452 carry = (tmp[uECC_WORDS] & 0x02); 2453 vli_add_n(s, tmp, curve_n); 2454 2455 /* p = k * G */ 2456 EccPoint_mult(&p, &curve_G, k2[!carry], NULL, (uECC_BYTES * 8) + 2); 2457 #else 2458 /* Make sure that we don't leak timing information about k. 2459 See http://eprint.iacr.org/2011/232.pdf */ 2460 carry = vli_add(tmp, k, curve_n); 2461 vli_add(s, tmp, curve_n); 2462 2463 /* p = k * G */ 2464 EccPoint_mult(&p, &curve_G, k2[!carry], NULL, (uECC_BYTES * 8) + 1); 2465 2466 /* r = x1 (mod n) */ 2467 if (vli_cmp(curve_n, p.x) != 1) { 2468 vli_sub(p.x, p.x, curve_n); 2469 } 2470 #endif 2471 if (vli_isZero(p.x)) { 2472 return 0; 2473 } 2474 2475 // Attempt to get a random number to prevent side channel analysis of k. 2476 // If the RNG fails every time (eg it was not defined), we continue so that 2477 // deterministic signing can still work (with reduced security) without 2478 // an RNG defined. 2479 carry = 0; // use to signal that the RNG succeeded at least once. 2480 for (tries = 0; tries < MAX_TRIES; ++tries) { 2481 if (!g_rng_function((uint8_t *)tmp, sizeof(tmp))) { 2482 continue; 2483 } 2484 carry = 1; 2485 if (!vli_isZero(tmp)) { 2486 break; 2487 } 2488 } 2489 if (!carry) { 2490 vli_clear(tmp); 2491 tmp[0] = 1; 2492 } 2493 2494 /* Prevent side channel analysis of vli_modInv() to determine 2495 bits of k / the private key by premultiplying by a random number */ 2496 vli_modMult_n(k, k, tmp); /* k' = rand * k */ 2497 vli_modInv_n(k, k, curve_n); /* k = 1 / k' */ 2498 vli_modMult_n(k, k, tmp); /* k = 1 / k */ 2499 2500 vli_nativeToBytes(signature, p.x); /* store r */ 2501 2502 tmp[uECC_N_WORDS - 1] = 0; 2503 vli_bytesToNative(tmp, private_key); /* tmp = d */ 2504 s[uECC_N_WORDS - 1] = 0; 2505 vli_set(s, p.x); 2506 vli_modMult_n(s, tmp, s); /* s = r*d */ 2507 2508 vli_bytesToNative(tmp, message_hash); 2509 vli_modAdd_n(s, tmp, s, curve_n); /* s = e + r*d */ 2510 vli_modMult_n(s, s, k); /* s = (e + r*d) / k */ 2511 #if (uECC_CURVE == uECC_secp160r1) 2512 if (s[uECC_N_WORDS - 1]) { 2513 return 0; 2514 } 2515 #endif 2516 vli_nativeToBytes(signature + uECC_BYTES, s); 2517 return 1; 2518 } 2519 2520 int uECC_sign(const uint8_t private_key[uECC_BYTES], 2521 const uint8_t message_hash[uECC_BYTES], 2522 uint8_t signature[uECC_BYTES*2]) { 2523 uECC_word_t k[uECC_N_WORDS]; 2524 uECC_word_t tries; 2525 2526 for (tries = 0; tries < MAX_TRIES; ++tries) { 2527 if(g_rng_function((uint8_t *)k, sizeof(k))) { 2528 #if (uECC_CURVE == uECC_secp160r1) 2529 k[uECC_WORDS] &= 0x01; 2530 #endif 2531 if (uECC_sign_with_k(private_key, message_hash, k, signature)) { 2532 return 1; 2533 } 2534 } 2535 } 2536 return 0; 2537 } 2538 2539 /* Compute an HMAC using K as a key (as in RFC 6979). Note that K is always 2540 the same size as the hash result size. */ 2541 static void HMAC_init(uECC_HashContext *hash_context, const uint8_t *K) { 2542 uint8_t *pad = hash_context->tmp + 2 * hash_context->result_size; 2543 unsigned i; 2544 for (i = 0; i < hash_context->result_size; ++i) 2545 pad[i] = K[i] ^ 0x36; 2546 for (; i < hash_context->block_size; ++i) 2547 pad[i] = 0x36; 2548 2549 hash_context->init_hash(hash_context); 2550 hash_context->update_hash(hash_context, pad, hash_context->block_size); 2551 } 2552 2553 static void HMAC_update(uECC_HashContext *hash_context, 2554 const uint8_t *message, 2555 unsigned message_size) { 2556 hash_context->update_hash(hash_context, message, message_size); 2557 } 2558 2559 static void HMAC_finish(uECC_HashContext *hash_context, const uint8_t *K, uint8_t *result) { 2560 uint8_t *pad = hash_context->tmp + 2 * hash_context->result_size; 2561 unsigned i; 2562 for (i = 0; i < hash_context->result_size; ++i) 2563 pad[i] = K[i] ^ 0x5c; 2564 for (; i < hash_context->block_size; ++i) 2565 pad[i] = 0x5c; 2566 2567 hash_context->finish_hash(hash_context, result); 2568 2569 hash_context->init_hash(hash_context); 2570 hash_context->update_hash(hash_context, pad, hash_context->block_size); 2571 hash_context->update_hash(hash_context, result, hash_context->result_size); 2572 hash_context->finish_hash(hash_context, result); 2573 } 2574 2575 /* V = HMAC_K(V) */ 2576 static void update_V(uECC_HashContext *hash_context, uint8_t *K, uint8_t *V) { 2577 HMAC_init(hash_context, K); 2578 HMAC_update(hash_context, V, hash_context->result_size); 2579 HMAC_finish(hash_context, K, V); 2580 } 2581 2582 /* Deterministic signing, similar to RFC 6979. Differences are: 2583 * We just use (truncated) H(m) directly rather than bits2octets(H(m)) 2584 (it is not reduced modulo curve_n). 2585 * We generate a value for k (aka T) directly rather than converting endianness. 2586 2587 Layout of hash_context->tmp: <K> | <V> | (1 byte overlapped 0x00 or 0x01) / <HMAC pad> */ 2588 int uECC_sign_deterministic(const uint8_t private_key[uECC_BYTES], 2589 const uint8_t message_hash[uECC_BYTES], 2590 uECC_HashContext *hash_context, 2591 uint8_t signature[uECC_BYTES*2]) { 2592 uint8_t *K = hash_context->tmp; 2593 uint8_t *V = K + hash_context->result_size; 2594 uECC_word_t tries; 2595 unsigned i; 2596 for (i = 0; i < hash_context->result_size; ++i) { 2597 V[i] = 0x01; 2598 K[i] = 0; 2599 } 2600 2601 // K = HMAC_K(V || 0x00 || int2octets(x) || h(m)) 2602 HMAC_init(hash_context, K); 2603 V[hash_context->result_size] = 0x00; 2604 HMAC_update(hash_context, V, hash_context->result_size + 1); 2605 HMAC_update(hash_context, private_key, uECC_BYTES); 2606 HMAC_update(hash_context, message_hash, uECC_BYTES); 2607 HMAC_finish(hash_context, K, K); 2608 2609 update_V(hash_context, K, V); 2610 2611 // K = HMAC_K(V || 0x01 || int2octets(x) || h(m)) 2612 HMAC_init(hash_context, K); 2613 V[hash_context->result_size] = 0x01; 2614 HMAC_update(hash_context, V, hash_context->result_size + 1); 2615 HMAC_update(hash_context, private_key, uECC_BYTES); 2616 HMAC_update(hash_context, message_hash, uECC_BYTES); 2617 HMAC_finish(hash_context, K, K); 2618 2619 update_V(hash_context, K, V); 2620 2621 for (tries = 0; tries < MAX_TRIES; ++tries) { 2622 uECC_word_t T[uECC_N_WORDS]; 2623 uint8_t *T_ptr = (uint8_t *)T; 2624 unsigned T_bytes = 0; 2625 while (T_bytes < sizeof(T)) { 2626 update_V(hash_context, K, V); 2627 for (i = 0; i < hash_context->result_size && T_bytes < sizeof(T); ++i, ++T_bytes) { 2628 T_ptr[T_bytes] = V[i]; 2629 } 2630 } 2631 #if (uECC_CURVE == uECC_secp160r1) 2632 T[uECC_WORDS] &= 0x01; 2633 #endif 2634 2635 if (uECC_sign_with_k(private_key, message_hash, T, signature)) { 2636 return 1; 2637 } 2638 2639 // K = HMAC_K(V || 0x00) 2640 HMAC_init(hash_context, K); 2641 V[hash_context->result_size] = 0x00; 2642 HMAC_update(hash_context, V, hash_context->result_size + 1); 2643 HMAC_finish(hash_context, K, K); 2644 2645 update_V(hash_context, K, V); 2646 } 2647 return 0; 2648 } 2649 2650 static bitcount_t smax(bitcount_t a, bitcount_t b) { 2651 return (a > b ? a : b); 2652 } 2653 2654 int uECC_verify(const uint8_t public_key[uECC_BYTES*2], 2655 const uint8_t hash[uECC_BYTES], 2656 const uint8_t signature[uECC_BYTES*2]) { 2657 uECC_word_t u1[uECC_N_WORDS], u2[uECC_N_WORDS]; 2658 uECC_word_t z[uECC_N_WORDS]; 2659 EccPoint public, sum; 2660 uECC_word_t rx[uECC_WORDS]; 2661 uECC_word_t ry[uECC_WORDS]; 2662 uECC_word_t tx[uECC_WORDS]; 2663 uECC_word_t ty[uECC_WORDS]; 2664 uECC_word_t tz[uECC_WORDS]; 2665 const EccPoint *points[4]; 2666 const EccPoint *point; 2667 bitcount_t numBits; 2668 bitcount_t i; 2669 uECC_word_t r[uECC_N_WORDS], s[uECC_N_WORDS]; 2670 r[uECC_N_WORDS - 1] = 0; 2671 s[uECC_N_WORDS - 1] = 0; 2672 2673 vli_bytesToNative(public.x, public_key); 2674 vli_bytesToNative(public.y, public_key + uECC_BYTES); 2675 vli_bytesToNative(r, signature); 2676 vli_bytesToNative(s, signature + uECC_BYTES); 2677 2678 if (vli_isZero(r) || vli_isZero(s)) { /* r, s must not be 0. */ 2679 return 0; 2680 } 2681 2682 #if (uECC_CURVE != uECC_secp160r1) 2683 if (vli_cmp(curve_n, r) != 1 || vli_cmp(curve_n, s) != 1) { /* r, s must be < n. */ 2684 return 0; 2685 } 2686 #endif 2687 2688 /* Calculate u1 and u2. */ 2689 vli_modInv_n(z, s, curve_n); /* Z = s^-1 */ 2690 u1[uECC_N_WORDS - 1] = 0; 2691 vli_bytesToNative(u1, hash); 2692 vli_modMult_n(u1, u1, z); /* u1 = e/s */ 2693 vli_modMult_n(u2, r, z); /* u2 = r/s */ 2694 2695 /* Calculate sum = G + Q. */ 2696 vli_set(sum.x, public.x); 2697 vli_set(sum.y, public.y); 2698 vli_set(tx, curve_G.x); 2699 vli_set(ty, curve_G.y); 2700 vli_modSub_fast(z, sum.x, tx); /* Z = x2 - x1 */ 2701 XYcZ_add(tx, ty, sum.x, sum.y); 2702 vli_modInv(z, z, curve_p); /* Z = 1/Z */ 2703 apply_z(sum.x, sum.y, z); 2704 2705 /* Use Shamir's trick to calculate u1*G + u2*Q */ 2706 points[0] = 0; 2707 points[1] = &curve_G; 2708 points[2] = &public; 2709 points[3] = ∑ 2710 numBits = smax(vli_numBits(u1, uECC_N_WORDS), vli_numBits(u2, uECC_N_WORDS)); 2711 2712 point = points[(!!vli_testBit(u1, numBits - 1)) | ((!!vli_testBit(u2, numBits - 1)) << 1)]; 2713 vli_set(rx, point->x); 2714 vli_set(ry, point->y); 2715 vli_clear(z); 2716 z[0] = 1; 2717 2718 for (i = numBits - 2; i >= 0; --i) { 2719 uECC_word_t index; 2720 EccPoint_double_jacobian(rx, ry, z); 2721 2722 index = (!!vli_testBit(u1, i)) | ((!!vli_testBit(u2, i)) << 1); 2723 point = points[index]; 2724 if (point) { 2725 vli_set(tx, point->x); 2726 vli_set(ty, point->y); 2727 apply_z(tx, ty, z); 2728 vli_modSub_fast(tz, rx, tx); /* Z = x2 - x1 */ 2729 XYcZ_add(tx, ty, rx, ry); 2730 vli_modMult_fast(z, z, tz); 2731 } 2732 } 2733 2734 vli_modInv(z, z, curve_p); /* Z = 1/Z */ 2735 apply_z(rx, ry, z); 2736 2737 /* v = x1 (mod n) */ 2738 #if (uECC_CURVE != uECC_secp160r1) 2739 if (vli_cmp(curve_n, rx) != 1) { 2740 vli_sub(rx, rx, curve_n); 2741 } 2742 #endif 2743 2744 /* Accept only if v == r. */ 2745 return vli_equal(rx, r); 2746 } 2747