1 /* 2 * Copyright (C) 2021 - This file is part of libecc project 3 * 4 * Authors: 5 * Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr> 6 * Ryad BENADJILA <ryadbenadjila@gmail.com> 7 * 8 * This software is licensed under a dual BSD and GPL v2 license. 9 * See LICENSE file at the root folder of the project. 10 */ 11 #include <libecc/lib_ecc_config.h> 12 #ifdef WITH_CURVE_SM2P256V1 13 14 #ifndef __EC_PARAMS_SM2P256V1_H__ 15 #define __EC_PARAMS_SM2P256V1_H__ 16 #include <libecc/curves/known/ec_params_external.h> 17 18 static const u8 sm2p256v1_p[] = { 19 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 20 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 21 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 22 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 23 }; 24 25 TO_EC_STR_PARAM(sm2p256v1_p); 26 27 #define CURVE_SM2P256V1_P_BITLEN 256 28 static const u8 sm2p256v1_p_bitlen[] = { 29 0x01, 0x00, 30 }; 31 32 TO_EC_STR_PARAM(sm2p256v1_p_bitlen); 33 34 #if (WORD_BYTES == 8) /* 64-bit words */ 35 static const u8 sm2p256v1_r[] = { 36 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 37 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 38 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 39 0x00, 0x00, 0x00, 0x00, 0x01, 40 }; 41 42 TO_EC_STR_PARAM(sm2p256v1_r); 43 44 static const u8 sm2p256v1_r_square[] = { 45 0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 46 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 47 0x02, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 48 0x02, 0x00, 0x00, 0x00, 0x03, 49 }; 50 51 TO_EC_STR_PARAM(sm2p256v1_r_square); 52 53 static const u8 sm2p256v1_mpinv[] = { 54 0x01, 55 }; 56 57 TO_EC_STR_PARAM(sm2p256v1_mpinv); 58 59 static const u8 sm2p256v1_p_shift[] = { 60 0x00, 61 }; 62 63 TO_EC_STR_PARAM(sm2p256v1_p_shift); 64 65 static const u8 sm2p256v1_p_normalized[] = { 66 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 67 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 68 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 69 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 70 }; 71 72 TO_EC_STR_PARAM(sm2p256v1_p_normalized); 73 74 static const u8 sm2p256v1_p_reciprocal[] = { 75 0x01, 0x00, 0x00, 0x00, 0x01, 76 }; 77 78 TO_EC_STR_PARAM(sm2p256v1_p_reciprocal); 79 80 #elif (WORD_BYTES == 4) /* 32-bit words */ 81 static const u8 sm2p256v1_r[] = { 82 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 83 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 84 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 85 0x00, 0x00, 0x00, 0x00, 0x01, 86 }; 87 88 TO_EC_STR_PARAM(sm2p256v1_r); 89 90 static const u8 sm2p256v1_r_square[] = { 91 0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 92 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 93 0x02, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 94 0x02, 0x00, 0x00, 0x00, 0x03, 95 }; 96 97 TO_EC_STR_PARAM(sm2p256v1_r_square); 98 99 static const u8 sm2p256v1_mpinv[] = { 100 0x01, 101 }; 102 103 TO_EC_STR_PARAM(sm2p256v1_mpinv); 104 105 static const u8 sm2p256v1_p_shift[] = { 106 0x00, 107 }; 108 109 TO_EC_STR_PARAM(sm2p256v1_p_shift); 110 111 static const u8 sm2p256v1_p_normalized[] = { 112 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 113 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 114 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 115 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 116 }; 117 118 TO_EC_STR_PARAM(sm2p256v1_p_normalized); 119 120 static const u8 sm2p256v1_p_reciprocal[] = { 121 0x01, 122 }; 123 124 TO_EC_STR_PARAM(sm2p256v1_p_reciprocal); 125 126 #elif (WORD_BYTES == 2) /* 16-bit words */ 127 static const u8 sm2p256v1_r[] = { 128 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 129 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 130 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 131 0x00, 0x00, 0x00, 0x00, 0x01, 132 }; 133 134 TO_EC_STR_PARAM(sm2p256v1_r); 135 136 static const u8 sm2p256v1_r_square[] = { 137 0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 138 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 139 0x02, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 140 0x02, 0x00, 0x00, 0x00, 0x03, 141 }; 142 143 TO_EC_STR_PARAM(sm2p256v1_r_square); 144 145 static const u8 sm2p256v1_mpinv[] = { 146 0x01, 147 }; 148 149 TO_EC_STR_PARAM(sm2p256v1_mpinv); 150 151 static const u8 sm2p256v1_p_shift[] = { 152 0x00, 153 }; 154 155 TO_EC_STR_PARAM(sm2p256v1_p_shift); 156 157 static const u8 sm2p256v1_p_normalized[] = { 158 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 159 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 160 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 161 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 162 163 }; 164 165 TO_EC_STR_PARAM(sm2p256v1_p_normalized); 166 167 static const u8 sm2p256v1_p_reciprocal[] = { 168 0x00, 169 }; 170 171 TO_EC_STR_PARAM(sm2p256v1_p_reciprocal); 172 173 #else /* unknown word size */ 174 #error "Unsupported word size" 175 #endif 176 177 static const u8 sm2p256v1_a[] = { 178 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 179 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 180 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 181 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 182 }; 183 184 TO_EC_STR_PARAM(sm2p256v1_a); 185 186 static const u8 sm2p256v1_b[] = { 187 0x28, 0xe9, 0xfa, 0x9e, 0x9d, 0x9f, 0x5e, 0x34, 188 0x4d, 0x5a, 0x9e, 0x4b, 0xcf, 0x65, 0x09, 0xa7, 189 0xf3, 0x97, 0x89, 0xf5, 0x15, 0xab, 0x8f, 0x92, 190 0xdd, 0xbc, 0xbd, 0x41, 0x4d, 0x94, 0x0e, 0x93, 191 }; 192 193 TO_EC_STR_PARAM(sm2p256v1_b); 194 195 #define CURVE_SM2P256V1_CURVE_ORDER_BITLEN 256 196 static const u8 sm2p256v1_curve_order[] = { 197 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 198 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 199 0x72, 0x03, 0xdf, 0x6b, 0x21, 0xc6, 0x05, 0x2b, 200 0x53, 0xbb, 0xf4, 0x09, 0x39, 0xd5, 0x41, 0x23, 201 }; 202 203 TO_EC_STR_PARAM(sm2p256v1_curve_order); 204 205 static const u8 sm2p256v1_gx[] = { 206 0x32, 0xc4, 0xae, 0x2c, 0x1f, 0x19, 0x81, 0x19, 207 0x5f, 0x99, 0x04, 0x46, 0x6a, 0x39, 0xc9, 0x94, 208 0x8f, 0xe3, 0x0b, 0xbf, 0xf2, 0x66, 0x0b, 0xe1, 209 0x71, 0x5a, 0x45, 0x89, 0x33, 0x4c, 0x74, 0xc7, 210 }; 211 212 TO_EC_STR_PARAM(sm2p256v1_gx); 213 214 static const u8 sm2p256v1_gy[] = { 215 0xbc, 0x37, 0x36, 0xa2, 0xf4, 0xf6, 0x77, 0x9c, 216 0x59, 0xbd, 0xce, 0xe3, 0x6b, 0x69, 0x21, 0x53, 217 0xd0, 0xa9, 0x87, 0x7c, 0xc6, 0x2a, 0x47, 0x40, 218 0x02, 0xdf, 0x32, 0xe5, 0x21, 0x39, 0xf0, 0xa0, 219 }; 220 221 TO_EC_STR_PARAM(sm2p256v1_gy); 222 223 static const u8 sm2p256v1_gz[] = { 224 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 225 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 226 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 227 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 228 }; 229 230 TO_EC_STR_PARAM(sm2p256v1_gz); 231 232 static const u8 sm2p256v1_gen_order[] = { 233 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 234 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 235 0x72, 0x03, 0xdf, 0x6b, 0x21, 0xc6, 0x05, 0x2b, 236 0x53, 0xbb, 0xf4, 0x09, 0x39, 0xd5, 0x41, 0x23, 237 }; 238 239 TO_EC_STR_PARAM(sm2p256v1_gen_order); 240 241 #define CURVE_SM2P256V1_Q_BITLEN 256 242 static const u8 sm2p256v1_gen_order_bitlen[] = { 243 0x01, 0x00, 244 }; 245 246 TO_EC_STR_PARAM(sm2p256v1_gen_order_bitlen); 247 248 static const u8 sm2p256v1_cofactor[] = { 249 0x01, 250 }; 251 252 TO_EC_STR_PARAM(sm2p256v1_cofactor); 253 254 255 static const u8 sm2p256v1_alpha_montgomery[] = { 256 0x00, 257 }; 258 259 TO_EC_STR_PARAM_FIXED_SIZE(sm2p256v1_alpha_montgomery, 0); 260 261 static const u8 sm2p256v1_gamma_montgomery[] = { 262 0x00, 263 }; 264 265 TO_EC_STR_PARAM_FIXED_SIZE(sm2p256v1_gamma_montgomery, 0); 266 267 static const u8 sm2p256v1_alpha_edwards[] = { 268 0x00, 269 }; 270 271 TO_EC_STR_PARAM_FIXED_SIZE(sm2p256v1_alpha_edwards, 0); 272 273 274 static const u8 sm2p256v1_name[] = "SM2P256V1"; 275 TO_EC_STR_PARAM(sm2p256v1_name); 276 277 static const u8 sm2p256v1_oid[] = "1.2.156.10197.1.301"; 278 TO_EC_STR_PARAM(sm2p256v1_oid); 279 280 static const ec_str_params sm2p256v1_str_params = { 281 .p = &sm2p256v1_p_str_param, 282 .p_bitlen = &sm2p256v1_p_bitlen_str_param, 283 .r = &sm2p256v1_r_str_param, 284 .r_square = &sm2p256v1_r_square_str_param, 285 .mpinv = &sm2p256v1_mpinv_str_param, 286 .p_shift = &sm2p256v1_p_shift_str_param, 287 .p_normalized = &sm2p256v1_p_normalized_str_param, 288 .p_reciprocal = &sm2p256v1_p_reciprocal_str_param, 289 .a = &sm2p256v1_a_str_param, 290 .b = &sm2p256v1_b_str_param, 291 .curve_order = &sm2p256v1_curve_order_str_param, 292 .gx = &sm2p256v1_gx_str_param, 293 .gy = &sm2p256v1_gy_str_param, 294 .gz = &sm2p256v1_gz_str_param, 295 .gen_order = &sm2p256v1_gen_order_str_param, 296 .gen_order_bitlen = &sm2p256v1_gen_order_bitlen_str_param, 297 .cofactor = &sm2p256v1_cofactor_str_param, 298 .alpha_montgomery = &sm2p256v1_alpha_montgomery_str_param, 299 .gamma_montgomery = &sm2p256v1_gamma_montgomery_str_param, 300 .alpha_edwards = &sm2p256v1_alpha_edwards_str_param, 301 .oid = &sm2p256v1_oid_str_param, 302 .name = &sm2p256v1_name_str_param, 303 }; 304 305 /* 306 * Compute max bit length of all curves for p and q 307 */ 308 #ifndef CURVES_MAX_P_BIT_LEN 309 #define CURVES_MAX_P_BIT_LEN 0 310 #endif 311 #if (CURVES_MAX_P_BIT_LEN < CURVE_SM2P256V1_P_BITLEN) 312 #undef CURVES_MAX_P_BIT_LEN 313 #define CURVES_MAX_P_BIT_LEN CURVE_SM2P256V1_P_BITLEN 314 #endif 315 #ifndef CURVES_MAX_Q_BIT_LEN 316 #define CURVES_MAX_Q_BIT_LEN 0 317 #endif 318 #if (CURVES_MAX_Q_BIT_LEN < CURVE_SM2P256V1_Q_BITLEN) 319 #undef CURVES_MAX_Q_BIT_LEN 320 #define CURVES_MAX_Q_BIT_LEN CURVE_SM2P256V1_Q_BITLEN 321 #endif 322 #ifndef CURVES_MAX_CURVE_ORDER_BIT_LEN 323 #define CURVES_MAX_CURVE_ORDER_BIT_LEN 0 324 #endif 325 #if (CURVES_MAX_CURVE_ORDER_BIT_LEN < CURVE_SM2P256V1_CURVE_ORDER_BITLEN) 326 #undef CURVES_MAX_CURVE_ORDER_BIT_LEN 327 #define CURVES_MAX_CURVE_ORDER_BIT_LEN CURVE_SM2P256V1_CURVE_ORDER_BITLEN 328 #endif 329 330 /* 331 * Compute and adapt max name and oid length 332 */ 333 #ifndef MAX_CURVE_OID_LEN 334 #define MAX_CURVE_OID_LEN 0 335 #endif 336 #ifndef MAX_CURVE_NAME_LEN 337 #define MAX_CURVE_NAME_LEN 0 338 #endif 339 #if (MAX_CURVE_OID_LEN < 21) 340 #undef MAX_CURVE_OID_LEN 341 #define MAX_CURVE_OID_LEN 21 342 #endif 343 #if (MAX_CURVE_NAME_LEN < 10) 344 #undef MAX_CURVE_NAME_LEN 345 #define MAX_CURVE_NAME_LEN 10 346 #endif 347 348 #endif /* __EC_PARAMS_SM2P256V1_H__ */ 349 350 #endif /* WITH_CURVE_SM2P256V1 */ 351