| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/trust_token.h> |
| 16 |
| 17 | #include <openssl/bn.h> |
| 18 | #include <openssl/bytestring.h> |
| 19 | #include <openssl/ec.h> |
| 20 | #include <openssl/err.h> |
| 21 | #include <openssl/mem.h> |
| 22 | #include <openssl/nid.h> |
| 23 | #include <openssl/rand.h> |
| 24 | #include <openssl/sha.h> |
| 25 |
| 26 | #include "../ec/internal.h" |
| 27 | #include "../fipsmodule/bn/internal.h" |
| 28 | #include "../fipsmodule/ec/internal.h" |
| 29 |
| 30 | #include "internal.h" |
| 31 |
| 32 |
| 33 | typedef int (*hash_t_func_t)(const EC_GROUP *group, EC_JACOBIAN *out, |
| 34 | const uint8_t t[TRUST_TOKEN_NONCE_SIZE]); |
| 35 | typedef int (*hash_s_func_t)(const EC_GROUP *group, EC_JACOBIAN *out, |
| 36 | const EC_AFFINE *t, |
| 37 | const uint8_t s[TRUST_TOKEN_NONCE_SIZE]); |
| 38 | typedef int (*hash_c_func_t)(const EC_GROUP *group, EC_SCALAR *out, |
| 39 | uint8_t *buf, size_t len); |
| 40 | typedef int (*hash_to_scalar_func_t)(const EC_GROUP *group, EC_SCALAR *out, |
| 41 | uint8_t *buf, size_t len); |
| 42 |
| 43 | typedef struct { |
| 44 | const EC_GROUP *group; |
| 45 | EC_PRECOMP g_precomp; |
| 46 | EC_PRECOMP h_precomp; |
| 47 | EC_JACOBIAN h; |
| 48 | // hash_t implements the H_t operation in PMBTokens. It returns one on success |
| 49 | // and zero on error. |
| 50 | hash_t_func_t hash_t; |
| 51 | // hash_s implements the H_s operation in PMBTokens. It returns one on success |
| 52 | // and zero on error. |
| 53 | hash_s_func_t hash_s; |
| 54 | // hash_c implements the H_c operation in PMBTokens. It returns one on success |
| 55 | // and zero on error. |
| 56 | hash_c_func_t hash_c; |
| 57 | // hash_to_scalar implements the HashToScalar operation for PMBTokens. It |
| 58 | // returns one on success and zero on error. |
| 59 | hash_to_scalar_func_t hash_to_scalar; |
| 60 | int prefix_point : 1; |
| 61 | } PMBTOKEN_METHOD; |
| 62 |
| 63 | static const uint8_t kDefaultAdditionalData[32] = {0}; |
| 64 |
| 65 | static int pmbtoken_init_method(PMBTOKEN_METHOD *method, const EC_GROUP *group, |
| 66 | const uint8_t *h_bytes, size_t h_len, |
| 67 | hash_t_func_t hash_t, hash_s_func_t hash_s, |
| 68 | hash_c_func_t hash_c, |
| 69 | hash_to_scalar_func_t hash_to_scalar, |
| 70 | int prefix_point) { |
| 71 | method->group = group; |
| 72 | method->hash_t = hash_t; |
| 73 | method->hash_s = hash_s; |
| 74 | method->hash_c = hash_c; |
| 75 | method->hash_to_scalar = hash_to_scalar; |
| 76 | method->prefix_point = prefix_point; |
| 77 |
| 78 | EC_AFFINE h; |
| 79 | if (!ec_point_from_uncompressed(method->group, &h, h_bytes, h_len)) { |
| 80 | return 0; |
| 81 | } |
| 82 | ec_affine_to_jacobian(method->group, &method->h, &h); |
| 83 |
| 84 | if (!ec_init_precomp(method->group, &method->g_precomp, |
| 85 | &method->group->generator.raw) || |
| 86 | !ec_init_precomp(method->group, &method->h_precomp, &method->h)) { |
| 87 | return 0; |
| 88 | } |
| 89 | return 1; |
| 90 | } |
| 91 |
| 92 | static int derive_scalar_from_secret(const PMBTOKEN_METHOD *method, |
| 93 | EC_SCALAR *out, const uint8_t *secret, |
| 94 | size_t secret_len, uint8_t scalar_id) { |
| 95 | static const uint8_t kKeygenLabel[] = "TrustTokenPMBTokenKeyGen"; |
| 96 |
| 97 | int ok = 0; |
| 98 | CBB cbb; |
| 99 | CBB_zero(&cbb); |
| 100 | uint8_t *buf = NULL; |
| 101 | size_t len; |
| 102 | if (!CBB_init(&cbb, 0) || |
| 103 | !CBB_add_bytes(&cbb, kKeygenLabel, sizeof(kKeygenLabel)) || |
| 104 | !CBB_add_u8(&cbb, scalar_id) || |
| 105 | !CBB_add_bytes(&cbb, secret, secret_len) || |
| 106 | !CBB_finish(&cbb, &buf, &len) || |
| 107 | !method->hash_to_scalar(method->group, out, buf, len)) { |
| 108 | OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_KEYGEN_FAILURE); |
| 109 | goto err; |
| 110 | } |
| 111 |
| 112 | ok = 1; |
| 113 |
| 114 | err: |
| 115 | CBB_cleanup(&cbb); |
| 116 | OPENSSL_free(buf); |
| 117 | return ok; |
| 118 | } |
| 119 |
| 120 | static int point_to_cbb(CBB *out, const EC_GROUP *group, |
| 121 | const EC_AFFINE *point) { |
| 122 | size_t len = ec_point_byte_len(group, POINT_CONVERSION_UNCOMPRESSED); |
| 123 | if (len == 0) { |
| 124 | return 0; |
| 125 | } |
| 126 | uint8_t *p; |
| 127 | return CBB_add_space(out, &p, len) && |
| 128 | ec_point_to_bytes(group, point, POINT_CONVERSION_UNCOMPRESSED, p, |
| 129 | len) == len; |
| 130 | } |
| 131 |
| 132 | static int cbb_add_prefixed_point(CBB *out, const EC_GROUP *group, |
| 133 | const EC_AFFINE *point, int prefix_point) { |
| 134 | if (prefix_point) { |
| 135 | CBB child; |
| 136 | if (!CBB_add_u16_length_prefixed(out, &child) || |
| 137 | !point_to_cbb(&child, group, point) || !CBB_flush(out)) { |
| 138 | return 0; |
| 139 | } |
| 140 | } else { |
| 141 | if (!point_to_cbb(out, group, point) || !CBB_flush(out)) { |
| 142 | return 0; |
| 143 | } |
| 144 | } |
| 145 |
| 146 | return 1; |
| 147 | } |
| 148 |
| 149 | static int cbs_get_prefixed_point(CBS *cbs, const EC_GROUP *group, |
| 150 | EC_AFFINE *out, int prefix_point) { |
| 151 | CBS child; |
| 152 | if (prefix_point) { |
| 153 | if (!CBS_get_u16_length_prefixed(cbs, &child)) { |
| 154 | return 0; |
| 155 | } |
| 156 | } else { |
| 157 | size_t plen = ec_point_byte_len(group, POINT_CONVERSION_UNCOMPRESSED); |
| 158 | if (!CBS_get_bytes(cbs, &child, plen)) { |
| 159 | return 0; |
| 160 | } |
| 161 | } |
| 162 |
| 163 | if (!ec_point_from_uncompressed(group, out, CBS_data(&child), |
| 164 | CBS_len(&child))) { |
| 165 | return 0; |
| 166 | } |
| 167 | return 1; |
| 168 | } |
| 169 |
| 170 | static int mul_public_3(const EC_GROUP *group, EC_JACOBIAN *out, |
| 171 | const EC_JACOBIAN *p0, const EC_SCALAR *scalar0, |
| 172 | const EC_JACOBIAN *p1, const EC_SCALAR *scalar1, |
| 173 | const EC_JACOBIAN *p2, const EC_SCALAR *scalar2) { |
| 174 | EC_JACOBIAN points[3] = {*p0, *p1, *p2}; |
| 175 | EC_SCALAR scalars[3] = {*scalar0, *scalar1, *scalar2}; |
| 176 | return ec_point_mul_scalar_public_batch(group, out, /*g_scalar=*/NULL, points, |
| 177 | scalars, 3); |
| 178 | } |
| 179 |
| 180 | static int pmbtoken_compute_keys(const PMBTOKEN_METHOD *method, |
| 181 | CBB *out_private, CBB *out_public, |
| 182 | const EC_SCALAR *x0, const EC_SCALAR *y0, |
| 183 | const EC_SCALAR *x1, const EC_SCALAR *y1, |
| 184 | const EC_SCALAR *xs, const EC_SCALAR *ys) { |
| 185 | const EC_GROUP *group = method->group; |
| 186 | EC_JACOBIAN pub[3]; |
| 187 | if (!ec_point_mul_scalar_precomp(group, &pub[0], &method->g_precomp, x0, |
| 188 | &method->h_precomp, y0, NULL, NULL) || |
| 189 | !ec_point_mul_scalar_precomp(group, &pub[1], &method->g_precomp, x1, |
| 190 | &method->h_precomp, y1, NULL, NULL) || |
| 191 | !ec_point_mul_scalar_precomp(method->group, &pub[2], &method->g_precomp, |
| 192 | xs, &method->h_precomp, ys, NULL, NULL)) { |
| 193 | OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_KEYGEN_FAILURE); |
| 194 | return 0; |
| 195 | } |
| 196 |
| 197 | const EC_SCALAR *scalars[] = {x0, y0, x1, y1, xs, ys}; |
| 198 | size_t scalar_len = BN_num_bytes(EC_GROUP_get0_order(group)); |
| 199 | for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(scalars); i++) { |
| 200 | uint8_t *buf; |
| 201 | if (!CBB_add_space(out_private, &buf, scalar_len)) { |
| 202 | OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_BUFFER_TOO_SMALL); |
| 203 | return 0; |
| 204 | } |
| 205 | ec_scalar_to_bytes(group, buf, &scalar_len, scalars[i]); |
| 206 | } |
| 207 |
| 208 | EC_AFFINE pub_affine[3]; |
| 209 | if (!ec_jacobian_to_affine_batch(group, pub_affine, pub, 3)) { |
| 210 | return 0; |
| 211 | } |
| 212 |
| 213 | if (!cbb_add_prefixed_point(out_public, group, &pub_affine[0], |
| 214 | method->prefix_point) || |
| 215 | !cbb_add_prefixed_point(out_public, group, &pub_affine[1], |
| 216 | method->prefix_point) || |
| 217 | !cbb_add_prefixed_point(out_public, group, &pub_affine[2], |
| 218 | method->prefix_point)) { |
| 219 | OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_BUFFER_TOO_SMALL); |
| 220 | return 0; |
| 221 | } |
| 222 |
| 223 | return 1; |
| 224 | } |
| 225 |
| 226 | static int pmbtoken_generate_key(const PMBTOKEN_METHOD *method, |
| 227 | CBB *out_private, CBB *out_public) { |
| 228 | EC_SCALAR x0, y0, x1, y1, xs, ys; |
| 229 | if (!ec_random_nonzero_scalar(method->group, &x0, kDefaultAdditionalData) || |
| 230 | !ec_random_nonzero_scalar(method->group, &y0, kDefaultAdditionalData) || |
| 231 | !ec_random_nonzero_scalar(method->group, &x1, kDefaultAdditionalData) || |
| 232 | !ec_random_nonzero_scalar(method->group, &y1, kDefaultAdditionalData) || |
| 233 | !ec_random_nonzero_scalar(method->group, &xs, kDefaultAdditionalData) || |
| 234 | !ec_random_nonzero_scalar(method->group, &ys, kDefaultAdditionalData)) { |
| 235 | OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_KEYGEN_FAILURE); |
| 236 | return 0; |
| 237 | } |
| 238 |
| 239 | return pmbtoken_compute_keys(method, out_private, out_public, &x0, &y0, &x1, |
| 240 | &y1, &xs, &ys); |
| 241 | } |
| 242 |
| 243 | static int pmbtoken_derive_key_from_secret(const PMBTOKEN_METHOD *method, |
| 244 | CBB *out_private, CBB *out_public, |
| 245 | const uint8_t *secret, |
| 246 | size_t secret_len) { |
| 247 | EC_SCALAR x0, y0, x1, y1, xs, ys; |
| 248 | if (!derive_scalar_from_secret(method, &x0, secret, secret_len, 0) || |
| 249 | !derive_scalar_from_secret(method, &y0, secret, secret_len, 1) || |
| 250 | !derive_scalar_from_secret(method, &x1, secret, secret_len, 2) || |
| 251 | !derive_scalar_from_secret(method, &y1, secret, secret_len, 3) || |
| 252 | !derive_scalar_from_secret(method, &xs, secret, secret_len, 4) || |
| 253 | !derive_scalar_from_secret(method, &ys, secret, secret_len, 5)) { |
| 254 | OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_KEYGEN_FAILURE); |
| 255 | return 0; |
| 256 | } |
| 257 |
| 258 | return pmbtoken_compute_keys(method, out_private, out_public, &x0, &y0, &x1, |
| 259 | &y1, &xs, &ys); |
| 260 | } |
| 261 |
| 262 | static int pmbtoken_client_key_from_bytes(const PMBTOKEN_METHOD *method, |
| 263 | TRUST_TOKEN_CLIENT_KEY *key, |
| 264 | const uint8_t *in, size_t len) { |
| 265 | CBS cbs; |
| 266 | CBS_init(&cbs, in, len); |
| 267 | if (!cbs_get_prefixed_point(&cbs, method->group, &key->pub0, |
| 268 | method->prefix_point) || |
| 269 | !cbs_get_prefixed_point(&cbs, method->group, &key->pub1, |
| 270 | method->prefix_point) || |
| 271 | !cbs_get_prefixed_point(&cbs, method->group, &key->pubs, |
| 272 | method->prefix_point) || |
| 273 | CBS_len(&cbs) != 0) { |
| 274 | OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE); |
| 275 | return 0; |
| 276 | } |
| 277 |
| 278 | return 1; |
| 279 | } |
| 280 |
| 281 | static int pmbtoken_issuer_key_from_bytes(const PMBTOKEN_METHOD *method, |
| 282 | TRUST_TOKEN_ISSUER_KEY *key, |
| 283 | const uint8_t *in, size_t len) { |
| 284 | const EC_GROUP *group = method->group; |
| 285 | CBS cbs, tmp; |
| 286 | CBS_init(&cbs, in, len); |
| 287 | size_t scalar_len = BN_num_bytes(EC_GROUP_get0_order(group)); |
| 288 | EC_SCALAR *scalars[] = {&key->x0, &key->y0, &key->x1, |
| 289 | &key->y1, &key->xs, &key->ys}; |
| 290 | for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(scalars); i++) { |
| 291 | if (!CBS_get_bytes(&cbs, &tmp, scalar_len) || |
| 292 | !ec_scalar_from_bytes(group, scalars[i], CBS_data(&tmp), |
| 293 | CBS_len(&tmp))) { |
| 294 | OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE); |
| 295 | return 0; |
| 296 | } |
| 297 | } |
| 298 |
| 299 | // Recompute the public key. |
| 300 | EC_JACOBIAN pub[3]; |
| 301 | EC_AFFINE pub_affine[3]; |
| 302 | if (!ec_point_mul_scalar_precomp(group, &pub[0], &method->g_precomp, &key->x0, |
| 303 | &method->h_precomp, &key->y0, NULL, NULL) || |
| 304 | !ec_init_precomp(group, &key->pub0_precomp, &pub[0]) || |
| 305 | !ec_point_mul_scalar_precomp(group, &pub[1], &method->g_precomp, &key->x1, |
| 306 | &method->h_precomp, &key->y1, NULL, NULL) || |
| 307 | !ec_init_precomp(group, &key->pub1_precomp, &pub[1]) || |
| 308 | !ec_point_mul_scalar_precomp(group, &pub[2], &method->g_precomp, &key->xs, |
| 309 | &method->h_precomp, &key->ys, NULL, NULL) || |
| 310 | !ec_init_precomp(group, &key->pubs_precomp, &pub[2]) || |
| 311 | !ec_jacobian_to_affine_batch(group, pub_affine, pub, 3)) { |
| 312 | return 0; |
| 313 | } |
| 314 |
| 315 | key->pub0 = pub_affine[0]; |
| 316 | key->pub1 = pub_affine[1]; |
| 317 | key->pubs = pub_affine[2]; |
| 318 | return 1; |
| 319 | } |
| 320 |
| 321 | static STACK_OF(TRUST_TOKEN_PRETOKEN) *pmbtoken_blind( |
| 322 | const PMBTOKEN_METHOD *method, CBB *cbb, size_t count, int include_message, |
| 323 | const uint8_t *msg, size_t msg_len) { |
| 324 | SHA512_CTX hash_ctx; |
| 325 |
| 326 | const EC_GROUP *group = method->group; |
| 327 | STACK_OF(TRUST_TOKEN_PRETOKEN) *pretokens = |
| 328 | sk_TRUST_TOKEN_PRETOKEN_new_null(); |
| 329 | if (pretokens == NULL) { |
| 330 | goto err; |
| 331 | } |
| 332 |
| 333 | for (size_t i = 0; i < count; i++) { |
| 334 | // Insert |pretoken| into |pretokens| early to simplify error-handling. |
| 335 | TRUST_TOKEN_PRETOKEN *pretoken = reinterpret_cast<TRUST_TOKEN_PRETOKEN *>( |
| 336 | OPENSSL_malloc(sizeof(TRUST_TOKEN_PRETOKEN))); |
| 337 | if (pretoken == NULL || |
| 338 | !sk_TRUST_TOKEN_PRETOKEN_push(pretokens, pretoken)) { |
| 339 | TRUST_TOKEN_PRETOKEN_free(pretoken); |
| 340 | goto err; |
| 341 | } |
| 342 |
| 343 | RAND_bytes(pretoken->salt, sizeof(pretoken->salt)); |
| 344 | if (include_message) { |
| 345 | assert(SHA512_DIGEST_LENGTH == TRUST_TOKEN_NONCE_SIZE); |
| 346 | SHA512_Init(&hash_ctx); |
| 347 | SHA512_Update(&hash_ctx, pretoken->salt, sizeof(pretoken->salt)); |
| 348 | SHA512_Update(&hash_ctx, msg, msg_len); |
| 349 | SHA512_Final(pretoken->t, &hash_ctx); |
| 350 | } else { |
| 351 | OPENSSL_memcpy(pretoken->t, pretoken->salt, TRUST_TOKEN_NONCE_SIZE); |
| 352 | } |
| 353 |
| 354 | // We sample |pretoken->r| in Montgomery form to simplify inverting. |
| 355 | if (!ec_random_nonzero_scalar(group, &pretoken->r, |
| 356 | kDefaultAdditionalData)) { |
| 357 | goto err; |
| 358 | } |
| 359 |
| 360 | EC_SCALAR rinv; |
| 361 | ec_scalar_inv0_montgomery(group, &rinv, &pretoken->r); |
| 362 | // Convert both out of Montgomery form. |
| 363 | ec_scalar_from_montgomery(group, &pretoken->r, &pretoken->r); |
| 364 | ec_scalar_from_montgomery(group, &rinv, &rinv); |
| 365 |
| 366 | EC_JACOBIAN T, Tp; |
| 367 | if (!method->hash_t(group, &T, pretoken->t) || |
| 368 | !ec_point_mul_scalar(group, &Tp, &T, &rinv) || |
| 369 | !ec_jacobian_to_affine(group, &pretoken->Tp, &Tp)) { |
| 370 | goto err; |
| 371 | } |
| 372 |
| 373 | if (!cbb_add_prefixed_point(cbb, group, &pretoken->Tp, |
| 374 | method->prefix_point)) { |
| 375 | goto err; |
| 376 | } |
| 377 | } |
| 378 |
| 379 | return pretokens; |
| 380 |
| 381 | err: |
| 382 | sk_TRUST_TOKEN_PRETOKEN_pop_free(pretokens, TRUST_TOKEN_PRETOKEN_free); |
| 383 | return NULL; |
| 384 | } |
| 385 |
| 386 | static int scalar_to_cbb(CBB *out, const EC_GROUP *group, |
| 387 | const EC_SCALAR *scalar) { |
| 388 | uint8_t *buf; |
| 389 | size_t scalar_len = BN_num_bytes(EC_GROUP_get0_order(group)); |
| 390 | if (!CBB_add_space(out, &buf, scalar_len)) { |
| 391 | return 0; |
| 392 | } |
| 393 | ec_scalar_to_bytes(group, buf, &scalar_len, scalar); |
| 394 | return 1; |
| 395 | } |
| 396 |
| 397 | static int scalar_from_cbs(CBS *cbs, const EC_GROUP *group, EC_SCALAR *out) { |
| 398 | size_t scalar_len = BN_num_bytes(EC_GROUP_get0_order(group)); |
| 399 | CBS tmp; |
| 400 | if (!CBS_get_bytes(cbs, &tmp, scalar_len)) { |
| 401 | OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE); |
| 402 | return 0; |
| 403 | } |
| 404 |
| 405 | ec_scalar_from_bytes(group, out, CBS_data(&tmp), CBS_len(&tmp)); |
| 406 | return 1; |
| 407 | } |
| 408 |
| 409 | static int hash_c_dleq(const PMBTOKEN_METHOD *method, EC_SCALAR *out, |
| 410 | const EC_AFFINE *X, const EC_AFFINE *T, |
| 411 | const EC_AFFINE *S, const EC_AFFINE *W, |
| 412 | const EC_AFFINE *K0, const EC_AFFINE *K1) { |
| 413 | static const uint8_t kDLEQ2Label[] = "DLEQ2"; |
| 414 |
| 415 | int ok = 0; |
| 416 | CBB cbb; |
| 417 | CBB_zero(&cbb); |
| 418 | uint8_t *buf = NULL; |
| 419 | size_t len; |
| 420 | if (!CBB_init(&cbb, 0) || |
| 421 | !CBB_add_bytes(&cbb, kDLEQ2Label, sizeof(kDLEQ2Label)) || |
| 422 | !point_to_cbb(&cbb, method->group, X) || |
| 423 | !point_to_cbb(&cbb, method->group, T) || |
| 424 | !point_to_cbb(&cbb, method->group, S) || |
| 425 | !point_to_cbb(&cbb, method->group, W) || |
| 426 | !point_to_cbb(&cbb, method->group, K0) || |
| 427 | !point_to_cbb(&cbb, method->group, K1) || !CBB_finish(&cbb, &buf, &len) || |
| 428 | !method->hash_c(method->group, out, buf, len)) { |
| 429 | goto err; |
| 430 | } |
| 431 |
| 432 | ok = 1; |
| 433 |
| 434 | err: |
| 435 | CBB_cleanup(&cbb); |
| 436 | OPENSSL_free(buf); |
| 437 | return ok; |
| 438 | } |
| 439 |
| 440 | static int hash_c_dleqor(const PMBTOKEN_METHOD *method, EC_SCALAR *out, |
| 441 | const EC_AFFINE *X0, const EC_AFFINE *X1, |
| 442 | const EC_AFFINE *T, const EC_AFFINE *S, |
| 443 | const EC_AFFINE *W, const EC_AFFINE *K00, |
| 444 | const EC_AFFINE *K01, const EC_AFFINE *K10, |
| 445 | const EC_AFFINE *K11) { |
| 446 | static const uint8_t kDLEQOR2Label[] = "DLEQOR2"; |
| 447 |
| 448 | int ok = 0; |
| 449 | CBB cbb; |
| 450 | CBB_zero(&cbb); |
| 451 | uint8_t *buf = NULL; |
| 452 | size_t len; |
| 453 | if (!CBB_init(&cbb, 0) || |
| 454 | !CBB_add_bytes(&cbb, kDLEQOR2Label, sizeof(kDLEQOR2Label)) || |
| 455 | !point_to_cbb(&cbb, method->group, X0) || |
| 456 | !point_to_cbb(&cbb, method->group, X1) || |
| 457 | !point_to_cbb(&cbb, method->group, T) || |
| 458 | !point_to_cbb(&cbb, method->group, S) || |
| 459 | !point_to_cbb(&cbb, method->group, W) || |
| 460 | !point_to_cbb(&cbb, method->group, K00) || |
| 461 | !point_to_cbb(&cbb, method->group, K01) || |
| 462 | !point_to_cbb(&cbb, method->group, K10) || |
| 463 | !point_to_cbb(&cbb, method->group, K11) || |
| 464 | !CBB_finish(&cbb, &buf, &len) || |
| 465 | !method->hash_c(method->group, out, buf, len)) { |
| 466 | goto err; |
| 467 | } |
| 468 |
| 469 | ok = 1; |
| 470 |
| 471 | err: |
| 472 | CBB_cleanup(&cbb); |
| 473 | OPENSSL_free(buf); |
| 474 | return ok; |
| 475 | } |
| 476 |
| 477 | static int hash_c_batch(const PMBTOKEN_METHOD *method, EC_SCALAR *out, |
| 478 | const CBB *points, size_t index) { |
| 479 | static const uint8_t kDLEQBatchLabel[] = "DLEQ BATCH"; |
| 480 | if (index > 0xffff) { |
| 481 | // The protocol supports only two-byte batches. |
| 482 | OPENSSL_PUT_ERROR(TRUST_TOKEN, ERR_R_OVERFLOW); |
| 483 | return 0; |
| 484 | } |
| 485 |
| 486 | int ok = 0; |
| 487 | CBB cbb; |
| 488 | CBB_zero(&cbb); |
| 489 | uint8_t *buf = NULL; |
| 490 | size_t len; |
| 491 | if (!CBB_init(&cbb, 0) || |
| 492 | !CBB_add_bytes(&cbb, kDLEQBatchLabel, sizeof(kDLEQBatchLabel)) || |
| 493 | !CBB_add_bytes(&cbb, CBB_data(points), CBB_len(points)) || |
| 494 | !CBB_add_u16(&cbb, (uint16_t)index) || !CBB_finish(&cbb, &buf, &len) || |
| 495 | !method->hash_c(method->group, out, buf, len)) { |
| 496 | goto err; |
| 497 | } |
| 498 |
| 499 | ok = 1; |
| 500 |
| 501 | err: |
| 502 | CBB_cleanup(&cbb); |
| 503 | OPENSSL_free(buf); |
| 504 | return ok; |
| 505 | } |
| 506 |
| 507 | // The DLEQ2 and DLEQOR2 constructions are described in appendix B of |
| 508 | // https://eprint.iacr.org/2020/072/20200324:214215. DLEQ2 is an instance of |
| 509 | // DLEQOR2 with only one value (n=1). |
| 510 |
| 511 | static int dleq_generate(const PMBTOKEN_METHOD *method, CBB *cbb, |
| 512 | const TRUST_TOKEN_ISSUER_KEY *priv, |
| 513 | const EC_JACOBIAN *T, const EC_JACOBIAN *S, |
| 514 | const EC_JACOBIAN *W, const EC_JACOBIAN *Ws, |
| 515 | uint8_t private_metadata) { |
| 516 | const EC_GROUP *group = method->group; |
| 517 |
| 518 | // We generate a DLEQ proof for the validity token and a DLEQOR2 proof for the |
| 519 | // private metadata token. To allow amortizing Jacobian-to-affine conversions, |
| 520 | // we compute Ki for both proofs first. |
| 521 | enum { |
| 522 | idx_T, |
| 523 | idx_S, |
| 524 | idx_W, |
| 525 | idx_Ws, |
| 526 | idx_Ks0, |
| 527 | idx_Ks1, |
| 528 | idx_Kb0, |
| 529 | idx_Kb1, |
| 530 | idx_Ko0, |
| 531 | idx_Ko1, |
| 532 | num_idx, |
| 533 | }; |
| 534 | EC_JACOBIAN jacobians[num_idx]; |
| 535 |
| 536 | // Setup the DLEQ proof. |
| 537 | EC_SCALAR ks0, ks1; |
| 538 | if ( // ks0, ks1 <- Zp |
| 539 | !ec_random_nonzero_scalar(group, &ks0, kDefaultAdditionalData) || |
| 540 | !ec_random_nonzero_scalar(group, &ks1, kDefaultAdditionalData) || |
| 541 | // Ks = ks0*(G;T) + ks1*(H;S) |
| 542 | !ec_point_mul_scalar_precomp(group, &jacobians[idx_Ks0], |
| 543 | &method->g_precomp, &ks0, &method->h_precomp, |
| 544 | &ks1, NULL, NULL) || |
| 545 | !ec_point_mul_scalar_batch(group, &jacobians[idx_Ks1], T, &ks0, S, &ks1, |
| 546 | NULL, NULL)) { |
| 547 | return 0; |
| 548 | } |
| 549 |
| 550 | // Setup the DLEQOR proof. First, select values of xb, yb (keys corresponding |
| 551 | // to the private metadata value) and pubo (public key corresponding to the |
| 552 | // other value) in constant time. |
| 553 | BN_ULONG mask = ((BN_ULONG)0) - (private_metadata & 1); |
| 554 | EC_PRECOMP pubo_precomp; |
| 555 | EC_SCALAR xb, yb; |
| 556 | ec_scalar_select(group, &xb, mask, &priv->x1, &priv->x0); |
| 557 | ec_scalar_select(group, &yb, mask, &priv->y1, &priv->y0); |
| 558 | ec_precomp_select(group, &pubo_precomp, mask, &priv->pub0_precomp, |
| 559 | &priv->pub1_precomp); |
| 560 |
| 561 | EC_SCALAR k0, k1, minus_co, uo, vo; |
| 562 | if ( // k0, k1 <- Zp |
| 563 | !ec_random_nonzero_scalar(group, &k0, kDefaultAdditionalData) || |
| 564 | !ec_random_nonzero_scalar(group, &k1, kDefaultAdditionalData) || |
| 565 | // Kb = k0*(G;T) + k1*(H;S) |
| 566 | !ec_point_mul_scalar_precomp(group, &jacobians[idx_Kb0], |
| 567 | &method->g_precomp, &k0, &method->h_precomp, |
| 568 | &k1, NULL, NULL) || |
| 569 | !ec_point_mul_scalar_batch(group, &jacobians[idx_Kb1], T, &k0, S, &k1, |
| 570 | NULL, NULL) || |
| 571 | // co, uo, vo <- Zp |
| 572 | !ec_random_nonzero_scalar(group, &minus_co, kDefaultAdditionalData) || |
| 573 | !ec_random_nonzero_scalar(group, &uo, kDefaultAdditionalData) || |
| 574 | !ec_random_nonzero_scalar(group, &vo, kDefaultAdditionalData) || |
| 575 | // Ko = uo*(G;T) + vo*(H;S) - co*(pubo;W) |
| 576 | !ec_point_mul_scalar_precomp(group, &jacobians[idx_Ko0], |
| 577 | &method->g_precomp, &uo, &method->h_precomp, |
| 578 | &vo, &pubo_precomp, &minus_co) || |
| 579 | !ec_point_mul_scalar_batch(group, &jacobians[idx_Ko1], T, &uo, S, &vo, W, |
| 580 | &minus_co)) { |
| 581 | return 0; |
| 582 | } |
| 583 |
| 584 | EC_AFFINE affines[num_idx]; |
| 585 | jacobians[idx_T] = *T; |
| 586 | jacobians[idx_S] = *S; |
| 587 | jacobians[idx_W] = *W; |
| 588 | jacobians[idx_Ws] = *Ws; |
| 589 | if (!ec_jacobian_to_affine_batch(group, affines, jacobians, num_idx)) { |
| 590 | return 0; |
| 591 | } |
| 592 |
| 593 | // Select the K corresponding to K0 and K1 in constant-time. |
| 594 | EC_AFFINE K00, K01, K10, K11; |
| 595 | ec_affine_select(group, &K00, mask, &affines[idx_Ko0], &affines[idx_Kb0]); |
| 596 | ec_affine_select(group, &K01, mask, &affines[idx_Ko1], &affines[idx_Kb1]); |
| 597 | ec_affine_select(group, &K10, mask, &affines[idx_Kb0], &affines[idx_Ko0]); |
| 598 | ec_affine_select(group, &K11, mask, &affines[idx_Kb1], &affines[idx_Ko1]); |
| 599 |
| 600 | // Compute c = Hc(...) for the two proofs. |
| 601 | EC_SCALAR cs, c; |
| 602 | if (!hash_c_dleq(method, &cs, &priv->pubs, &affines[idx_T], &affines[idx_S], |
| 603 | &affines[idx_Ws], &affines[idx_Ks0], &affines[idx_Ks1]) || |
| 604 | !hash_c_dleqor(method, &c, &priv->pub0, &priv->pub1, &affines[idx_T], |
| 605 | &affines[idx_S], &affines[idx_W], &K00, &K01, &K10, |
| 606 | &K11)) { |
| 607 | return 0; |
| 608 | } |
| 609 |
| 610 | // Compute cb, ub, and ub for the two proofs. In each of these products, only |
| 611 | // one operand is in Montgomery form, so the product does not need to be |
| 612 | // converted. |
| 613 |
| 614 | EC_SCALAR cs_mont; |
| 615 | ec_scalar_to_montgomery(group, &cs_mont, &cs); |
| 616 |
| 617 | // us = ks0 + cs*xs |
| 618 | EC_SCALAR us, vs; |
| 619 | ec_scalar_mul_montgomery(group, &us, &priv->xs, &cs_mont); |
| 620 | ec_scalar_add(group, &us, &ks0, &us); |
| 621 |
| 622 | // vs = ks1 + cs*ys |
| 623 | ec_scalar_mul_montgomery(group, &vs, &priv->ys, &cs_mont); |
| 624 | ec_scalar_add(group, &vs, &ks1, &vs); |
| 625 |
| 626 | // Store DLEQ2 proof in transcript. |
| 627 | if (!scalar_to_cbb(cbb, group, &cs) || !scalar_to_cbb(cbb, group, &us) || |
| 628 | !scalar_to_cbb(cbb, group, &vs)) { |
| 629 | return 0; |
| 630 | } |
| 631 |
| 632 | // cb = c - co |
| 633 | EC_SCALAR cb, ub, vb; |
| 634 | ec_scalar_add(group, &cb, &c, &minus_co); |
| 635 |
| 636 | EC_SCALAR cb_mont; |
| 637 | ec_scalar_to_montgomery(group, &cb_mont, &cb); |
| 638 |
| 639 | // ub = k0 + cb*xb |
| 640 | ec_scalar_mul_montgomery(group, &ub, &xb, &cb_mont); |
| 641 | ec_scalar_add(group, &ub, &k0, &ub); |
| 642 |
| 643 | // vb = k1 + cb*yb |
| 644 | ec_scalar_mul_montgomery(group, &vb, &yb, &cb_mont); |
| 645 | ec_scalar_add(group, &vb, &k1, &vb); |
| 646 |
| 647 | // Select c, u, v in constant-time. |
| 648 | EC_SCALAR co, c0, c1, u0, u1, v0, v1; |
| 649 | ec_scalar_neg(group, &co, &minus_co); |
| 650 | ec_scalar_select(group, &c0, mask, &co, &cb); |
| 651 | ec_scalar_select(group, &u0, mask, &uo, &ub); |
| 652 | ec_scalar_select(group, &v0, mask, &vo, &vb); |
| 653 | ec_scalar_select(group, &c1, mask, &cb, &co); |
| 654 | ec_scalar_select(group, &u1, mask, &ub, &uo); |
| 655 | ec_scalar_select(group, &v1, mask, &vb, &vo); |
| 656 |
| 657 | // Store DLEQOR2 proof in transcript. |
| 658 | if (!scalar_to_cbb(cbb, group, &c0) || !scalar_to_cbb(cbb, group, &c1) || |
| 659 | !scalar_to_cbb(cbb, group, &u0) || !scalar_to_cbb(cbb, group, &u1) || |
| 660 | !scalar_to_cbb(cbb, group, &v0) || !scalar_to_cbb(cbb, group, &v1)) { |
| 661 | return 0; |
| 662 | } |
| 663 |
| 664 | return 1; |
| 665 | } |
| 666 |
| 667 | static int dleq_verify(const PMBTOKEN_METHOD *method, CBS *cbs, |
| 668 | const TRUST_TOKEN_CLIENT_KEY *pub, const EC_JACOBIAN *T, |
| 669 | const EC_JACOBIAN *S, const EC_JACOBIAN *W, |
| 670 | const EC_JACOBIAN *Ws) { |
| 671 | const EC_GROUP *group = method->group; |
| 672 | const EC_JACOBIAN *g = &group->generator.raw; |
| 673 |
| 674 | // We verify a DLEQ proof for the validity token and a DLEQOR2 proof for the |
| 675 | // private metadata token. To allow amortizing Jacobian-to-affine conversions, |
| 676 | // we compute Ki for both proofs first. Additionally, all inputs to this |
| 677 | // function are public, so we can use the faster variable-time |
| 678 | // multiplications. |
| 679 | enum { |
| 680 | idx_T, |
| 681 | idx_S, |
| 682 | idx_W, |
| 683 | idx_Ws, |
| 684 | idx_Ks0, |
| 685 | idx_Ks1, |
| 686 | idx_K00, |
| 687 | idx_K01, |
| 688 | idx_K10, |
| 689 | idx_K11, |
| 690 | num_idx, |
| 691 | }; |
| 692 | EC_JACOBIAN jacobians[num_idx]; |
| 693 |
| 694 | // Decode the DLEQ proof. |
| 695 | EC_SCALAR cs, us, vs; |
| 696 | if (!scalar_from_cbs(cbs, group, &cs) || !scalar_from_cbs(cbs, group, &us) || |
| 697 | !scalar_from_cbs(cbs, group, &vs)) { |
| 698 | OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE); |
| 699 | return 0; |
| 700 | } |
| 701 |
| 702 | // Ks = us*(G;T) + vs*(H;S) - cs*(pubs;Ws) |
| 703 | EC_JACOBIAN pubs; |
| 704 | ec_affine_to_jacobian(group, &pubs, &pub->pubs); |
| 705 | EC_SCALAR minus_cs; |
| 706 | ec_scalar_neg(group, &minus_cs, &cs); |
| 707 | if (!mul_public_3(group, &jacobians[idx_Ks0], g, &us, &method->h, &vs, &pubs, |
| 708 | &minus_cs) || |
| 709 | !mul_public_3(group, &jacobians[idx_Ks1], T, &us, S, &vs, Ws, |
| 710 | &minus_cs)) { |
| 711 | return 0; |
| 712 | } |
| 713 |
| 714 | // Decode the DLEQOR proof. |
| 715 | EC_SCALAR c0, c1, u0, u1, v0, v1; |
| 716 | if (!scalar_from_cbs(cbs, group, &c0) || !scalar_from_cbs(cbs, group, &c1) || |
| 717 | !scalar_from_cbs(cbs, group, &u0) || !scalar_from_cbs(cbs, group, &u1) || |
| 718 | !scalar_from_cbs(cbs, group, &v0) || !scalar_from_cbs(cbs, group, &v1)) { |
| 719 | OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE); |
| 720 | return 0; |
| 721 | } |
| 722 |
| 723 | EC_JACOBIAN pub0, pub1; |
| 724 | ec_affine_to_jacobian(group, &pub0, &pub->pub0); |
| 725 | ec_affine_to_jacobian(group, &pub1, &pub->pub1); |
| 726 | EC_SCALAR minus_c0, minus_c1; |
| 727 | ec_scalar_neg(group, &minus_c0, &c0); |
| 728 | ec_scalar_neg(group, &minus_c1, &c1); |
| 729 | if ( // K0 = u0*(G;T) + v0*(H;S) - c0*(pub0;W) |
| 730 | !mul_public_3(group, &jacobians[idx_K00], g, &u0, &method->h, &v0, &pub0, |
| 731 | &minus_c0) || |
| 732 | !mul_public_3(group, &jacobians[idx_K01], T, &u0, S, &v0, W, &minus_c0) || |
| 733 | // K1 = u1*(G;T) + v1*(H;S) - c1*(pub1;W) |
| 734 | !mul_public_3(group, &jacobians[idx_K10], g, &u1, &method->h, &v1, &pub1, |
| 735 | &minus_c1) || |
| 736 | !mul_public_3(group, &jacobians[idx_K11], T, &u1, S, &v1, W, &minus_c1)) { |
| 737 | return 0; |
| 738 | } |
| 739 |
| 740 | EC_AFFINE affines[num_idx]; |
| 741 | jacobians[idx_T] = *T; |
| 742 | jacobians[idx_S] = *S; |
| 743 | jacobians[idx_W] = *W; |
| 744 | jacobians[idx_Ws] = *Ws; |
| 745 | if (!ec_jacobian_to_affine_batch(group, affines, jacobians, num_idx)) { |
| 746 | return 0; |
| 747 | } |
| 748 |
| 749 | // Check the DLEQ proof. |
| 750 | EC_SCALAR calculated; |
| 751 | if (!hash_c_dleq(method, &calculated, &pub->pubs, &affines[idx_T], |
| 752 | &affines[idx_S], &affines[idx_Ws], &affines[idx_Ks0], |
| 753 | &affines[idx_Ks1])) { |
| 754 | return 0; |
| 755 | } |
| 756 |
| 757 | // cs == calculated |
| 758 | if (!ec_scalar_equal_vartime(group, &cs, &calculated)) { |
| 759 | OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_INVALID_PROOF); |
| 760 | return 0; |
| 761 | } |
| 762 |
| 763 | // Check the DLEQOR proof. |
| 764 | if (!hash_c_dleqor(method, &calculated, &pub->pub0, &pub->pub1, |
| 765 | &affines[idx_T], &affines[idx_S], &affines[idx_W], |
| 766 | &affines[idx_K00], &affines[idx_K01], &affines[idx_K10], |
| 767 | &affines[idx_K11])) { |
| 768 | return 0; |
| 769 | } |
| 770 |
| 771 | // c0 + c1 == calculated |
| 772 | EC_SCALAR c; |
| 773 | ec_scalar_add(group, &c, &c0, &c1); |
| 774 | if (!ec_scalar_equal_vartime(group, &c, &calculated)) { |
| 775 | OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_INVALID_PROOF); |
| 776 | return 0; |
| 777 | } |
| 778 |
| 779 | return 1; |
| 780 | } |
| 781 |
| 782 | static int pmbtoken_sign(const PMBTOKEN_METHOD *method, |
| 783 | const TRUST_TOKEN_ISSUER_KEY *key, CBB *cbb, CBS *cbs, |
| 784 | size_t num_requested, size_t num_to_issue, |
| 785 | uint8_t private_metadata) { |
| 786 | const EC_GROUP *group = method->group; |
| 787 | if (num_requested < num_to_issue) { |
| 788 | OPENSSL_PUT_ERROR(TRUST_TOKEN, ERR_R_INTERNAL_ERROR); |
| 789 | return 0; |
| 790 | } |
| 791 |
| 792 | int ret = 0; |
| 793 | EC_JACOBIAN *Tps = reinterpret_cast<EC_JACOBIAN *>( |
| 794 | OPENSSL_calloc(num_to_issue, sizeof(EC_JACOBIAN))); |
| 795 | EC_JACOBIAN *Sps = reinterpret_cast<EC_JACOBIAN *>( |
| 796 | OPENSSL_calloc(num_to_issue, sizeof(EC_JACOBIAN))); |
| 797 | EC_JACOBIAN *Wps = reinterpret_cast<EC_JACOBIAN *>( |
| 798 | OPENSSL_calloc(num_to_issue, sizeof(EC_JACOBIAN))); |
| 799 | EC_JACOBIAN *Wsps = reinterpret_cast<EC_JACOBIAN *>( |
| 800 | OPENSSL_calloc(num_to_issue, sizeof(EC_JACOBIAN))); |
| 801 | EC_SCALAR *es = reinterpret_cast<EC_SCALAR *>( |
| 802 | OPENSSL_calloc(num_to_issue, sizeof(EC_SCALAR))); |
| 803 | CBB batch_cbb; |
| 804 | CBB_zero(&batch_cbb); |
| 805 |
| 806 | { |
| 807 | if (!Tps || !Sps || !Wps || !Wsps || !es || !CBB_init(&batch_cbb, 0) || |
| 808 | !point_to_cbb(&batch_cbb, method->group, &key->pubs) || |
| 809 | !point_to_cbb(&batch_cbb, method->group, &key->pub0) || |
| 810 | !point_to_cbb(&batch_cbb, method->group, &key->pub1)) { |
| 811 | goto err; |
| 812 | } |
| 813 |
| 814 | for (size_t i = 0; i < num_to_issue; i++) { |
| 815 | EC_AFFINE Tp_affine; |
| 816 | EC_JACOBIAN Tp; |
| 817 | if (!cbs_get_prefixed_point(cbs, group, &Tp_affine, |
| 818 | method->prefix_point)) { |
| 819 | OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE); |
| 820 | goto err; |
| 821 | } |
| 822 | ec_affine_to_jacobian(group, &Tp, &Tp_affine); |
| 823 |
| 824 | EC_SCALAR xb, yb; |
| 825 | BN_ULONG mask = ((BN_ULONG)0) - (private_metadata & 1); |
| 826 | ec_scalar_select(group, &xb, mask, &key->x1, &key->x0); |
| 827 | ec_scalar_select(group, &yb, mask, &key->y1, &key->y0); |
| 828 |
| 829 | uint8_t s[TRUST_TOKEN_NONCE_SIZE]; |
| 830 | RAND_bytes(s, TRUST_TOKEN_NONCE_SIZE); |
| 831 | // The |jacobians| and |affines| contain Sp, Wp, and Wsp. |
| 832 | EC_JACOBIAN jacobians[3]; |
| 833 | EC_AFFINE affines[3]; |
| 834 | if (!method->hash_s(group, &jacobians[0], &Tp_affine, s) || |
| 835 | !ec_point_mul_scalar_batch(group, &jacobians[1], &Tp, &xb, |
| 836 | &jacobians[0], &yb, NULL, NULL) || |
| 837 | !ec_point_mul_scalar_batch(group, &jacobians[2], &Tp, &key->xs, |
| 838 | &jacobians[0], &key->ys, NULL, NULL) || |
| 839 | !ec_jacobian_to_affine_batch(group, affines, jacobians, 3) || |
| 840 | !CBB_add_bytes(cbb, s, TRUST_TOKEN_NONCE_SIZE) || |
| 841 | !cbb_add_prefixed_point(cbb, group, &affines[1], |
| 842 | method->prefix_point) || |
| 843 | !cbb_add_prefixed_point(cbb, group, &affines[2], |
| 844 | method->prefix_point)) { |
| 845 | goto err; |
| 846 | } |
| 847 |
| 848 | if (!point_to_cbb(&batch_cbb, group, &Tp_affine) || |
| 849 | !point_to_cbb(&batch_cbb, group, &affines[0]) || |
| 850 | !point_to_cbb(&batch_cbb, group, &affines[1]) || |
| 851 | !point_to_cbb(&batch_cbb, group, &affines[2])) { |
| 852 | goto err; |
| 853 | } |
| 854 | Tps[i] = Tp; |
| 855 | Sps[i] = jacobians[0]; |
| 856 | Wps[i] = jacobians[1]; |
| 857 | Wsps[i] = jacobians[2]; |
| 858 |
| 859 | if (!CBB_flush(cbb)) { |
| 860 | goto err; |
| 861 | } |
| 862 | } |
| 863 |
| 864 | // The DLEQ batching construction is described in appendix B of |
| 865 | // https://eprint.iacr.org/2020/072/20200324:214215. Note the additional |
| 866 | // computations all act on public inputs. |
| 867 | for (size_t i = 0; i < num_to_issue; i++) { |
| 868 | if (!hash_c_batch(method, &es[i], &batch_cbb, i)) { |
| 869 | goto err; |
| 870 | } |
| 871 | } |
| 872 |
| 873 | EC_JACOBIAN Tp_batch, Sp_batch, Wp_batch, Wsp_batch; |
| 874 | if (!ec_point_mul_scalar_public_batch(group, &Tp_batch, |
| 875 | /*g_scalar=*/NULL, Tps, es, |
| 876 | num_to_issue) || |
| 877 | !ec_point_mul_scalar_public_batch(group, &Sp_batch, |
| 878 | /*g_scalar=*/NULL, Sps, es, |
| 879 | num_to_issue) || |
| 880 | !ec_point_mul_scalar_public_batch(group, &Wp_batch, |
| 881 | /*g_scalar=*/NULL, Wps, es, |
| 882 | num_to_issue) || |
| 883 | !ec_point_mul_scalar_public_batch(group, &Wsp_batch, |
| 884 | /*g_scalar=*/NULL, Wsps, es, |
| 885 | num_to_issue)) { |
| 886 | goto err; |
| 887 | } |
| 888 |
| 889 | CBB proof; |
| 890 | if (!CBB_add_u16_length_prefixed(cbb, &proof) || |
| 891 | !dleq_generate(method, &proof, key, &Tp_batch, &Sp_batch, &Wp_batch, |
| 892 | &Wsp_batch, private_metadata) || |
| 893 | !CBB_flush(cbb)) { |
| 894 | goto err; |
| 895 | } |
| 896 |
| 897 | // Skip over any unused requests. |
| 898 | size_t point_len = ec_point_byte_len(group, POINT_CONVERSION_UNCOMPRESSED); |
| 899 | size_t token_len = point_len; |
| 900 | if (method->prefix_point) { |
| 901 | token_len += 2; |
| 902 | } |
| 903 | if (!CBS_skip(cbs, token_len * (num_requested - num_to_issue))) { |
| 904 | OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE); |
| 905 | goto err; |
| 906 | } |
| 907 |
| 908 | ret = 1; |
| 909 | } |
| 910 |
| 911 | err: |
| 912 | OPENSSL_free(Tps); |
| 913 | OPENSSL_free(Sps); |
| 914 | OPENSSL_free(Wps); |
| 915 | OPENSSL_free(Wsps); |
| 916 | OPENSSL_free(es); |
| 917 | CBB_cleanup(&batch_cbb); |
| 918 | return ret; |
| 919 | } |
| 920 |
| 921 | static STACK_OF(TRUST_TOKEN) *pmbtoken_unblind( |
| 922 | const PMBTOKEN_METHOD *method, const TRUST_TOKEN_CLIENT_KEY *key, |
| 923 | const STACK_OF(TRUST_TOKEN_PRETOKEN) *pretokens, CBS *cbs, size_t count, |
| 924 | uint32_t key_id) { |
| 925 | const EC_GROUP *group = method->group; |
| 926 | if (count > sk_TRUST_TOKEN_PRETOKEN_num(pretokens)) { |
| 927 | OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE); |
| 928 | return NULL; |
| 929 | } |
| 930 |
| 931 | int ok = 0; |
| 932 | STACK_OF(TRUST_TOKEN) *ret = sk_TRUST_TOKEN_new_null(); |
| 933 | EC_JACOBIAN *Tps = reinterpret_cast<EC_JACOBIAN *>( |
| 934 | OPENSSL_calloc(count, sizeof(EC_JACOBIAN))); |
| 935 | EC_JACOBIAN *Sps = reinterpret_cast<EC_JACOBIAN *>( |
| 936 | OPENSSL_calloc(count, sizeof(EC_JACOBIAN))); |
| 937 | EC_JACOBIAN *Wps = reinterpret_cast<EC_JACOBIAN *>( |
| 938 | OPENSSL_calloc(count, sizeof(EC_JACOBIAN))); |
| 939 | EC_JACOBIAN *Wsps = reinterpret_cast<EC_JACOBIAN *>( |
| 940 | OPENSSL_calloc(count, sizeof(EC_JACOBIAN))); |
| 941 | EC_SCALAR *es = |
| 942 | reinterpret_cast<EC_SCALAR *>(OPENSSL_calloc(count, sizeof(EC_SCALAR))); |
| 943 | CBB batch_cbb; |
| 944 | CBB_zero(&batch_cbb); |
| 945 | if (ret == NULL || Tps == NULL || Sps == NULL || Wps == NULL || |
| 946 | Wsps == NULL || es == NULL || !CBB_init(&batch_cbb, 0) || |
| 947 | !point_to_cbb(&batch_cbb, method->group, &key->pubs) || |
| 948 | !point_to_cbb(&batch_cbb, method->group, &key->pub0) || |
| 949 | !point_to_cbb(&batch_cbb, method->group, &key->pub1)) { |
| 950 | goto err; |
| 951 | } |
| 952 |
| 953 | for (size_t i = 0; i < count; i++) { |
| 954 | const TRUST_TOKEN_PRETOKEN *pretoken = |
| 955 | sk_TRUST_TOKEN_PRETOKEN_value(pretokens, i); |
| 956 |
| 957 | uint8_t s[TRUST_TOKEN_NONCE_SIZE]; |
| 958 | EC_AFFINE Wp_affine, Wsp_affine; |
| 959 | if (!CBS_copy_bytes(cbs, s, TRUST_TOKEN_NONCE_SIZE) || |
| 960 | !cbs_get_prefixed_point(cbs, group, &Wp_affine, method->prefix_point) || |
| 961 | !cbs_get_prefixed_point(cbs, group, &Wsp_affine, |
| 962 | method->prefix_point)) { |
| 963 | OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE); |
| 964 | goto err; |
| 965 | } |
| 966 |
| 967 | ec_affine_to_jacobian(group, &Tps[i], &pretoken->Tp); |
| 968 | ec_affine_to_jacobian(group, &Wps[i], &Wp_affine); |
| 969 | ec_affine_to_jacobian(group, &Wsps[i], &Wsp_affine); |
| 970 | if (!method->hash_s(group, &Sps[i], &pretoken->Tp, s)) { |
| 971 | goto err; |
| 972 | } |
| 973 |
| 974 | EC_AFFINE Sp_affine; |
| 975 | if (!point_to_cbb(&batch_cbb, group, &pretoken->Tp) || |
| 976 | !ec_jacobian_to_affine(group, &Sp_affine, &Sps[i]) || |
| 977 | !point_to_cbb(&batch_cbb, group, &Sp_affine) || |
| 978 | !point_to_cbb(&batch_cbb, group, &Wp_affine) || |
| 979 | !point_to_cbb(&batch_cbb, group, &Wsp_affine)) { |
| 980 | goto err; |
| 981 | } |
| 982 |
| 983 | // Unblind the token. |
| 984 | EC_JACOBIAN jacobians[3]; |
| 985 | EC_AFFINE affines[3]; |
| 986 | if (!ec_point_mul_scalar(group, &jacobians[0], &Sps[i], &pretoken->r) || |
| 987 | !ec_point_mul_scalar(group, &jacobians[1], &Wps[i], &pretoken->r) || |
| 988 | !ec_point_mul_scalar(group, &jacobians[2], &Wsps[i], &pretoken->r) || |
| 989 | !ec_jacobian_to_affine_batch(group, affines, jacobians, 3)) { |
| 990 | goto err; |
| 991 | } |
| 992 |
| 993 | // Serialize the token. Include |key_id| to avoid an extra copy in the layer |
| 994 | // above. |
| 995 | CBB token_cbb; |
| 996 | size_t point_len = ec_point_byte_len(group, POINT_CONVERSION_UNCOMPRESSED); |
| 997 | if (!CBB_init(&token_cbb, |
| 998 | 4 + TRUST_TOKEN_NONCE_SIZE + 3 * (2 + point_len)) || |
| 999 | !CBB_add_u32(&token_cbb, key_id) || |
| 1000 | !CBB_add_bytes(&token_cbb, pretoken->salt, TRUST_TOKEN_NONCE_SIZE) || |
| 1001 | !cbb_add_prefixed_point(&token_cbb, group, &affines[0], |
| 1002 | method->prefix_point) || |
| 1003 | !cbb_add_prefixed_point(&token_cbb, group, &affines[1], |
| 1004 | method->prefix_point) || |
| 1005 | !cbb_add_prefixed_point(&token_cbb, group, &affines[2], |
| 1006 | method->prefix_point) || |
| 1007 | !CBB_flush(&token_cbb)) { |
| 1008 | CBB_cleanup(&token_cbb); |
| 1009 | goto err; |
| 1010 | } |
| 1011 |
| 1012 | TRUST_TOKEN *token = |
| 1013 | TRUST_TOKEN_new(CBB_data(&token_cbb), CBB_len(&token_cbb)); |
| 1014 | CBB_cleanup(&token_cbb); |
| 1015 | if (token == NULL || !sk_TRUST_TOKEN_push(ret, token)) { |
| 1016 | TRUST_TOKEN_free(token); |
| 1017 | goto err; |
| 1018 | } |
| 1019 | } |
| 1020 |
| 1021 | // The DLEQ batching construction is described in appendix B of |
| 1022 | // https://eprint.iacr.org/2020/072/20200324:214215. Note the additional |
| 1023 | // computations all act on public inputs. |
| 1024 | for (size_t i = 0; i < count; i++) { |
| 1025 | if (!hash_c_batch(method, &es[i], &batch_cbb, i)) { |
| 1026 | goto err; |
| 1027 | } |
| 1028 | } |
| 1029 |
| 1030 | EC_JACOBIAN Tp_batch, Sp_batch, Wp_batch, Wsp_batch; |
| 1031 | if (!ec_point_mul_scalar_public_batch(group, &Tp_batch, |
| 1032 | /*g_scalar=*/NULL, Tps, es, count) || |
| 1033 | !ec_point_mul_scalar_public_batch(group, &Sp_batch, |
| 1034 | /*g_scalar=*/NULL, Sps, es, count) || |
| 1035 | !ec_point_mul_scalar_public_batch(group, &Wp_batch, |
| 1036 | /*g_scalar=*/NULL, Wps, es, count) || |
| 1037 | !ec_point_mul_scalar_public_batch(group, &Wsp_batch, |
| 1038 | /*g_scalar=*/NULL, Wsps, es, count)) { |
| 1039 | goto err; |
| 1040 | } |
| 1041 |
| 1042 | CBS proof; |
| 1043 | if (!CBS_get_u16_length_prefixed(cbs, &proof) || |
| 1044 | !dleq_verify(method, &proof, key, &Tp_batch, &Sp_batch, &Wp_batch, |
| 1045 | &Wsp_batch) || |
| 1046 | CBS_len(&proof) != 0) { |
| 1047 | goto err; |
| 1048 | } |
| 1049 |
| 1050 | ok = 1; |
| 1051 |
| 1052 | err: |
| 1053 | OPENSSL_free(Tps); |
| 1054 | OPENSSL_free(Sps); |
| 1055 | OPENSSL_free(Wps); |
| 1056 | OPENSSL_free(Wsps); |
| 1057 | OPENSSL_free(es); |
| 1058 | CBB_cleanup(&batch_cbb); |
| 1059 | if (!ok) { |
| 1060 | sk_TRUST_TOKEN_pop_free(ret, TRUST_TOKEN_free); |
| 1061 | ret = NULL; |
| 1062 | } |
| 1063 | return ret; |
| 1064 | } |
| 1065 |
| 1066 | static int pmbtoken_read(const PMBTOKEN_METHOD *method, |
| 1067 | const TRUST_TOKEN_ISSUER_KEY *key, |
| 1068 | uint8_t out_nonce[TRUST_TOKEN_NONCE_SIZE], |
| 1069 | uint8_t *out_private_metadata, const uint8_t *token, |
| 1070 | size_t token_len, int include_message, |
| 1071 | const uint8_t *msg, size_t msg_len) { |
| 1072 | const EC_GROUP *group = method->group; |
| 1073 | CBS cbs, salt; |
| 1074 | CBS_init(&cbs, token, token_len); |
| 1075 | EC_AFFINE S, W, Ws; |
| 1076 | if (!CBS_get_bytes(&cbs, &salt, TRUST_TOKEN_NONCE_SIZE) || |
| 1077 | !cbs_get_prefixed_point(&cbs, group, &S, method->prefix_point) || |
| 1078 | !cbs_get_prefixed_point(&cbs, group, &W, method->prefix_point) || |
| 1079 | !cbs_get_prefixed_point(&cbs, group, &Ws, method->prefix_point) || |
| 1080 | CBS_len(&cbs) != 0) { |
| 1081 | OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_INVALID_TOKEN); |
| 1082 | return 0; |
| 1083 | } |
| 1084 |
| 1085 | if (include_message) { |
| 1086 | SHA512_CTX hash_ctx; |
| 1087 | assert(SHA512_DIGEST_LENGTH == TRUST_TOKEN_NONCE_SIZE); |
| 1088 | SHA512_Init(&hash_ctx); |
| 1089 | SHA512_Update(&hash_ctx, CBS_data(&salt), CBS_len(&salt)); |
| 1090 | SHA512_Update(&hash_ctx, msg, msg_len); |
| 1091 | SHA512_Final(out_nonce, &hash_ctx); |
| 1092 | } else { |
| 1093 | OPENSSL_memcpy(out_nonce, CBS_data(&salt), CBS_len(&salt)); |
| 1094 | } |
| 1095 |
| 1096 | EC_JACOBIAN T; |
| 1097 | if (!method->hash_t(group, &T, out_nonce)) { |
| 1098 | return 0; |
| 1099 | } |
| 1100 |
| 1101 | // We perform three multiplications with S and T. This is enough that it is |
| 1102 | // worth using |ec_point_mul_scalar_precomp|. |
| 1103 | EC_JACOBIAN S_jacobian; |
| 1104 | EC_PRECOMP S_precomp, T_precomp; |
| 1105 | ec_affine_to_jacobian(group, &S_jacobian, &S); |
| 1106 | if (!ec_init_precomp(group, &S_precomp, &S_jacobian) || |
| 1107 | !ec_init_precomp(group, &T_precomp, &T)) { |
| 1108 | return 0; |
| 1109 | } |
| 1110 |
| 1111 | EC_JACOBIAN Ws_calculated; |
| 1112 | // Check the validity of the token. |
| 1113 | if (!ec_point_mul_scalar_precomp(group, &Ws_calculated, &T_precomp, &key->xs, |
| 1114 | &S_precomp, &key->ys, NULL, NULL) || |
| 1115 | !ec_affine_jacobian_equal(group, &Ws, &Ws_calculated)) { |
| 1116 | OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_BAD_VALIDITY_CHECK); |
| 1117 | return 0; |
| 1118 | } |
| 1119 |
| 1120 | EC_JACOBIAN W0, W1; |
| 1121 | if (!ec_point_mul_scalar_precomp(group, &W0, &T_precomp, &key->x0, &S_precomp, |
| 1122 | &key->y0, NULL, NULL) || |
| 1123 | !ec_point_mul_scalar_precomp(group, &W1, &T_precomp, &key->x1, &S_precomp, |
| 1124 | &key->y1, NULL, NULL)) { |
| 1125 | return 0; |
| 1126 | } |
| 1127 |
| 1128 | const int is_W0 = ec_affine_jacobian_equal(group, &W, &W0); |
| 1129 | const int is_W1 = ec_affine_jacobian_equal(group, &W, &W1); |
| 1130 | const int is_valid = is_W0 ^ is_W1; |
| 1131 | if (!is_valid) { |
| 1132 | // Invalid tokens will fail the validity check above. |
| 1133 | OPENSSL_PUT_ERROR(TRUST_TOKEN, ERR_R_INTERNAL_ERROR); |
| 1134 | return 0; |
| 1135 | } |
| 1136 |
| 1137 | *out_private_metadata = is_W1; |
| 1138 | return 1; |
| 1139 | } |
| 1140 |
| 1141 |
| 1142 | // PMBTokens experiment v1. |
| 1143 |
| 1144 | static int pmbtoken_exp1_hash_t(const EC_GROUP *group, EC_JACOBIAN *out, |
| 1145 | const uint8_t t[TRUST_TOKEN_NONCE_SIZE]) { |
| 1146 | const uint8_t kHashTLabel[] = "PMBTokens Experiment V1 HashT"; |
| 1147 | return ec_hash_to_curve_p384_xmd_sha512_sswu_draft07( |
| 1148 | group, out, kHashTLabel, sizeof(kHashTLabel), t, TRUST_TOKEN_NONCE_SIZE); |
| 1149 | } |
| 1150 |
| 1151 | static int pmbtoken_exp1_hash_s(const EC_GROUP *group, EC_JACOBIAN *out, |
| 1152 | const EC_AFFINE *t, |
| 1153 | const uint8_t s[TRUST_TOKEN_NONCE_SIZE]) { |
| 1154 | const uint8_t kHashSLabel[] = "PMBTokens Experiment V1 HashS"; |
| 1155 | int ret = 0; |
| 1156 | CBB cbb; |
| 1157 | uint8_t *buf = NULL; |
| 1158 | size_t len; |
| 1159 | if (!CBB_init(&cbb, 0) || !point_to_cbb(&cbb, group, t) || |
| 1160 | !CBB_add_bytes(&cbb, s, TRUST_TOKEN_NONCE_SIZE) || |
| 1161 | !CBB_finish(&cbb, &buf, &len) || |
| 1162 | !ec_hash_to_curve_p384_xmd_sha512_sswu_draft07( |
| 1163 | group, out, kHashSLabel, sizeof(kHashSLabel), buf, len)) { |
| 1164 | goto err; |
| 1165 | } |
| 1166 |
| 1167 | ret = 1; |
| 1168 |
| 1169 | err: |
| 1170 | OPENSSL_free(buf); |
| 1171 | CBB_cleanup(&cbb); |
| 1172 | return ret; |
| 1173 | } |
| 1174 |
| 1175 | static int pmbtoken_exp1_hash_c(const EC_GROUP *group, EC_SCALAR *out, |
| 1176 | uint8_t *buf, size_t len) { |
| 1177 | const uint8_t kHashCLabel[] = "PMBTokens Experiment V1 HashC"; |
| 1178 | return ec_hash_to_scalar_p384_xmd_sha512_draft07( |
| 1179 | group, out, kHashCLabel, sizeof(kHashCLabel), buf, len); |
| 1180 | } |
| 1181 |
| 1182 | static int pmbtoken_exp1_hash_to_scalar(const EC_GROUP *group, EC_SCALAR *out, |
| 1183 | uint8_t *buf, size_t len) { |
| 1184 | const uint8_t kHashLabel[] = "PMBTokens Experiment V1 HashToScalar"; |
| 1185 | return ec_hash_to_scalar_p384_xmd_sha512_draft07( |
| 1186 | group, out, kHashLabel, sizeof(kHashLabel), buf, len); |
| 1187 | } |
| 1188 |
| 1189 | static int pmbtoken_exp1_ok = 0; |
| 1190 | static PMBTOKEN_METHOD pmbtoken_exp1_method; |
| 1191 | static CRYPTO_once_t pmbtoken_exp1_method_once = CRYPTO_ONCE_INIT; |
| 1192 |
| 1193 | static void pmbtoken_exp1_init_method_impl(void) { |
| 1194 | // This is the output of |ec_hash_to_scalar_p384_xmd_sha512_draft07| with DST |
| 1195 | // "PMBTokens Experiment V1 HashH" and message "generator". |
| 1196 | static const uint8_t kH[] = { |
| 1197 | 0x04, 0x82, 0xd5, 0x68, 0xf5, 0x39, 0xf6, 0x08, 0x19, 0xa1, 0x75, |
| 1198 | 0x9f, 0x98, 0xb5, 0x10, 0xf5, 0x0b, 0x9d, 0x2b, 0xe1, 0x64, 0x4d, |
| 1199 | 0x02, 0x76, 0x18, 0x11, 0xf8, 0x2f, 0xd3, 0x33, 0x25, 0x1f, 0x2c, |
| 1200 | 0xb8, 0xf6, 0xf1, 0x9e, 0x93, 0x85, 0x79, 0xb3, 0xb7, 0x81, 0xa3, |
| 1201 | 0xe6, 0x23, 0xc3, 0x1c, 0xff, 0x03, 0xd9, 0x40, 0x6c, 0xec, 0xe0, |
| 1202 | 0x4d, 0xea, 0xdf, 0x9d, 0x94, 0xd1, 0x87, 0xab, 0x27, 0xf7, 0x4f, |
| 1203 | 0x53, 0xea, 0xa3, 0x18, 0x72, 0xb9, 0xd1, 0x56, 0xa0, 0x4e, 0x81, |
| 1204 | 0xaa, 0xeb, 0x1c, 0x22, 0x6d, 0x39, 0x1c, 0x5e, 0xb1, 0x27, 0xfc, |
| 1205 | 0x87, 0xc3, 0x95, 0xd0, 0x13, 0xb7, 0x0b, 0x5c, 0xc7, |
| 1206 | }; |
| 1207 |
| 1208 | pmbtoken_exp1_ok = pmbtoken_init_method( |
| 1209 | &pmbtoken_exp1_method, EC_group_p384(), kH, sizeof(kH), |
| 1210 | pmbtoken_exp1_hash_t, pmbtoken_exp1_hash_s, pmbtoken_exp1_hash_c, |
| 1211 | pmbtoken_exp1_hash_to_scalar, 1); |
| 1212 | } |
| 1213 |
| 1214 | static int pmbtoken_exp1_init_method(void) { |
| 1215 | CRYPTO_once(&pmbtoken_exp1_method_once, pmbtoken_exp1_init_method_impl); |
| 1216 | if (!pmbtoken_exp1_ok) { |
| 1217 | OPENSSL_PUT_ERROR(TRUST_TOKEN, ERR_R_INTERNAL_ERROR); |
| 1218 | return 0; |
| 1219 | } |
| 1220 | return 1; |
| 1221 | } |
| 1222 |
| 1223 | int pmbtoken_exp1_generate_key(CBB *out_private, CBB *out_public) { |
| 1224 | if (!pmbtoken_exp1_init_method()) { |
| 1225 | return 0; |
| 1226 | } |
| 1227 |
| 1228 | return pmbtoken_generate_key(&pmbtoken_exp1_method, out_private, out_public); |
| 1229 | } |
| 1230 |
| 1231 | int pmbtoken_exp1_derive_key_from_secret(CBB *out_private, CBB *out_public, |
| 1232 | const uint8_t *secret, |
| 1233 | size_t secret_len) { |
| 1234 | if (!pmbtoken_exp1_init_method()) { |
| 1235 | return 0; |
| 1236 | } |
| 1237 |
| 1238 | return pmbtoken_derive_key_from_secret(&pmbtoken_exp1_method, out_private, |
| 1239 | out_public, secret, secret_len); |
| 1240 | } |
| 1241 |
| 1242 | int pmbtoken_exp1_client_key_from_bytes(TRUST_TOKEN_CLIENT_KEY *key, |
| 1243 | const uint8_t *in, size_t len) { |
| 1244 | if (!pmbtoken_exp1_init_method()) { |
| 1245 | return 0; |
| 1246 | } |
| 1247 | return pmbtoken_client_key_from_bytes(&pmbtoken_exp1_method, key, in, len); |
| 1248 | } |
| 1249 |
| 1250 | int pmbtoken_exp1_issuer_key_from_bytes(TRUST_TOKEN_ISSUER_KEY *key, |
| 1251 | const uint8_t *in, size_t len) { |
| 1252 | if (!pmbtoken_exp1_init_method()) { |
| 1253 | return 0; |
| 1254 | } |
| 1255 | return pmbtoken_issuer_key_from_bytes(&pmbtoken_exp1_method, key, in, len); |
| 1256 | } |
| 1257 |
| 1258 | STACK_OF(TRUST_TOKEN_PRETOKEN) *pmbtoken_exp1_blind(CBB *cbb, size_t count, |
| 1259 | int include_message, |
| 1260 | const uint8_t *msg, |
| 1261 | size_t msg_len) { |
| 1262 | if (!pmbtoken_exp1_init_method()) { |
| 1263 | return NULL; |
| 1264 | } |
| 1265 | return pmbtoken_blind(&pmbtoken_exp1_method, cbb, count, include_message, msg, |
| 1266 | msg_len); |
| 1267 | } |
| 1268 |
| 1269 | int pmbtoken_exp1_sign(const TRUST_TOKEN_ISSUER_KEY *key, CBB *cbb, CBS *cbs, |
| 1270 | size_t num_requested, size_t num_to_issue, |
| 1271 | uint8_t private_metadata) { |
| 1272 | if (!pmbtoken_exp1_init_method()) { |
| 1273 | return 0; |
| 1274 | } |
| 1275 | return pmbtoken_sign(&pmbtoken_exp1_method, key, cbb, cbs, num_requested, |
| 1276 | num_to_issue, private_metadata); |
| 1277 | } |
| 1278 |
| 1279 | STACK_OF(TRUST_TOKEN) *pmbtoken_exp1_unblind( |
| 1280 | const TRUST_TOKEN_CLIENT_KEY *key, |
| 1281 | const STACK_OF(TRUST_TOKEN_PRETOKEN) *pretokens, CBS *cbs, size_t count, |
| 1282 | uint32_t key_id) { |
| 1283 | if (!pmbtoken_exp1_init_method()) { |
| 1284 | return NULL; |
| 1285 | } |
| 1286 | return pmbtoken_unblind(&pmbtoken_exp1_method, key, pretokens, cbs, count, |
| 1287 | key_id); |
| 1288 | } |
| 1289 |
| 1290 | int pmbtoken_exp1_read(const TRUST_TOKEN_ISSUER_KEY *key, |
| 1291 | uint8_t out_nonce[TRUST_TOKEN_NONCE_SIZE], |
| 1292 | uint8_t *out_private_metadata, const uint8_t *token, |
| 1293 | size_t token_len, int include_message, |
| 1294 | const uint8_t *msg, size_t msg_len) { |
| 1295 | if (!pmbtoken_exp1_init_method()) { |
| 1296 | return 0; |
| 1297 | } |
| 1298 | return pmbtoken_read(&pmbtoken_exp1_method, key, out_nonce, |
| 1299 | out_private_metadata, token, token_len, include_message, |
| 1300 | msg, msg_len); |
| 1301 | } |
| 1302 |
| 1303 | int pmbtoken_exp1_get_h_for_testing(uint8_t out[97]) { |
| 1304 | if (!pmbtoken_exp1_init_method()) { |
| 1305 | return 0; |
| 1306 | } |
| 1307 | EC_AFFINE h; |
| 1308 | return ec_jacobian_to_affine(pmbtoken_exp1_method.group, &h, |
| 1309 | &pmbtoken_exp1_method.h) && |
| 1310 | ec_point_to_bytes(pmbtoken_exp1_method.group, &h, |
| 1311 | POINT_CONVERSION_UNCOMPRESSED, out, 97) == 97; |
| 1312 | } |
| 1313 |
| 1314 | // PMBTokens experiment v2. |
| 1315 |
| 1316 | static int pmbtoken_exp2_hash_t(const EC_GROUP *group, EC_JACOBIAN *out, |
| 1317 | const uint8_t t[TRUST_TOKEN_NONCE_SIZE]) { |
| 1318 | const uint8_t kHashTLabel[] = "PMBTokens Experiment V2 HashT"; |
| 1319 | return ec_hash_to_curve_p384_xmd_sha512_sswu_draft07( |
| 1320 | group, out, kHashTLabel, sizeof(kHashTLabel), t, TRUST_TOKEN_NONCE_SIZE); |
| 1321 | } |
| 1322 |
| 1323 | static int pmbtoken_exp2_hash_s(const EC_GROUP *group, EC_JACOBIAN *out, |
| 1324 | const EC_AFFINE *t, |
| 1325 | const uint8_t s[TRUST_TOKEN_NONCE_SIZE]) { |
| 1326 | const uint8_t kHashSLabel[] = "PMBTokens Experiment V2 HashS"; |
| 1327 | int ret = 0; |
| 1328 | CBB cbb; |
| 1329 | uint8_t *buf = NULL; |
| 1330 | size_t len; |
| 1331 | if (!CBB_init(&cbb, 0) || !point_to_cbb(&cbb, group, t) || |
| 1332 | !CBB_add_bytes(&cbb, s, TRUST_TOKEN_NONCE_SIZE) || |
| 1333 | !CBB_finish(&cbb, &buf, &len) || |
| 1334 | !ec_hash_to_curve_p384_xmd_sha512_sswu_draft07( |
| 1335 | group, out, kHashSLabel, sizeof(kHashSLabel), buf, len)) { |
| 1336 | goto err; |
| 1337 | } |
| 1338 |
| 1339 | ret = 1; |
| 1340 |
| 1341 | err: |
| 1342 | OPENSSL_free(buf); |
| 1343 | CBB_cleanup(&cbb); |
| 1344 | return ret; |
| 1345 | } |
| 1346 |
| 1347 | static int pmbtoken_exp2_hash_c(const EC_GROUP *group, EC_SCALAR *out, |
| 1348 | uint8_t *buf, size_t len) { |
| 1349 | const uint8_t kHashCLabel[] = "PMBTokens Experiment V2 HashC"; |
| 1350 | return ec_hash_to_scalar_p384_xmd_sha512_draft07( |
| 1351 | group, out, kHashCLabel, sizeof(kHashCLabel), buf, len); |
| 1352 | } |
| 1353 |
| 1354 | static int pmbtoken_exp2_hash_to_scalar(const EC_GROUP *group, EC_SCALAR *out, |
| 1355 | uint8_t *buf, size_t len) { |
| 1356 | const uint8_t kHashLabel[] = "PMBTokens Experiment V2 HashToScalar"; |
| 1357 | return ec_hash_to_scalar_p384_xmd_sha512_draft07( |
| 1358 | group, out, kHashLabel, sizeof(kHashLabel), buf, len); |
| 1359 | } |
| 1360 |
| 1361 | static int pmbtoken_exp2_ok = 0; |
| 1362 | static PMBTOKEN_METHOD pmbtoken_exp2_method; |
| 1363 | static CRYPTO_once_t pmbtoken_exp2_method_once = CRYPTO_ONCE_INIT; |
| 1364 |
| 1365 | static void pmbtoken_exp2_init_method_impl(void) { |
| 1366 | // This is the output of |ec_hash_to_scalar_p384_xmd_sha512_draft07| with DST |
| 1367 | // "PMBTokens Experiment V2 HashH" and message "generator". |
| 1368 | static const uint8_t kH[] = { |
| 1369 | 0x04, 0xbc, 0x27, 0x24, 0x99, 0xfa, 0xc9, 0xa4, 0x74, 0x6f, 0xf9, |
| 1370 | 0x07, 0x81, 0x55, 0xf8, 0x1f, 0x6f, 0xda, 0x09, 0xe7, 0x8c, 0x5d, |
| 1371 | 0x9e, 0x4e, 0x14, 0x7c, 0x53, 0x14, 0xbc, 0x7e, 0x29, 0x57, 0x92, |
| 1372 | 0x17, 0x94, 0x6e, 0xd2, 0xdf, 0xa5, 0x31, 0x1b, 0x4e, 0xb7, 0xfc, |
| 1373 | 0x93, 0xe3, 0x6e, 0x14, 0x1f, 0x4f, 0x14, 0xf3, 0xe5, 0x47, 0x61, |
| 1374 | 0x1c, 0x2c, 0x72, 0x25, 0xf0, 0x4a, 0x45, 0x23, 0x2d, 0x57, 0x93, |
| 1375 | 0x0e, 0xb2, 0x55, 0xb8, 0x57, 0x25, 0x4c, 0x1e, 0xdb, 0xfd, 0x58, |
| 1376 | 0x70, 0x17, 0x9a, 0xbb, 0x9e, 0x5e, 0x93, 0x9e, 0x92, 0xd3, 0xe8, |
| 1377 | 0x25, 0x62, 0xbf, 0x59, 0xb2, 0xd2, 0x3d, 0x71, 0xff}; |
| 1378 |
| 1379 | pmbtoken_exp2_ok = pmbtoken_init_method( |
| 1380 | &pmbtoken_exp2_method, EC_group_p384(), kH, sizeof(kH), |
| 1381 | pmbtoken_exp2_hash_t, pmbtoken_exp2_hash_s, pmbtoken_exp2_hash_c, |
| 1382 | pmbtoken_exp2_hash_to_scalar, 0); |
| 1383 | } |
| 1384 |
| 1385 | static int pmbtoken_exp2_init_method(void) { |
| 1386 | CRYPTO_once(&pmbtoken_exp2_method_once, pmbtoken_exp2_init_method_impl); |
| 1387 | if (!pmbtoken_exp2_ok) { |
| 1388 | OPENSSL_PUT_ERROR(TRUST_TOKEN, ERR_R_INTERNAL_ERROR); |
| 1389 | return 0; |
| 1390 | } |
| 1391 | return 1; |
| 1392 | } |
| 1393 |
| 1394 | int pmbtoken_exp2_generate_key(CBB *out_private, CBB *out_public) { |
| 1395 | if (!pmbtoken_exp2_init_method()) { |
| 1396 | return 0; |
| 1397 | } |
| 1398 |
| 1399 | return pmbtoken_generate_key(&pmbtoken_exp2_method, out_private, out_public); |
| 1400 | } |
| 1401 |
| 1402 |
| 1403 | int pmbtoken_exp2_derive_key_from_secret(CBB *out_private, CBB *out_public, |
| 1404 | const uint8_t *secret, |
| 1405 | size_t secret_len) { |
| 1406 | if (!pmbtoken_exp2_init_method()) { |
| 1407 | return 0; |
| 1408 | } |
| 1409 |
| 1410 | return pmbtoken_derive_key_from_secret(&pmbtoken_exp2_method, out_private, |
| 1411 | out_public, secret, secret_len); |
| 1412 | } |
| 1413 |
| 1414 | int pmbtoken_exp2_client_key_from_bytes(TRUST_TOKEN_CLIENT_KEY *key, |
| 1415 | const uint8_t *in, size_t len) { |
| 1416 | if (!pmbtoken_exp2_init_method()) { |
| 1417 | return 0; |
| 1418 | } |
| 1419 | return pmbtoken_client_key_from_bytes(&pmbtoken_exp2_method, key, in, len); |
| 1420 | } |
| 1421 |
| 1422 | int pmbtoken_exp2_issuer_key_from_bytes(TRUST_TOKEN_ISSUER_KEY *key, |
| 1423 | const uint8_t *in, size_t len) { |
| 1424 | if (!pmbtoken_exp2_init_method()) { |
| 1425 | return 0; |
| 1426 | } |
| 1427 | return pmbtoken_issuer_key_from_bytes(&pmbtoken_exp2_method, key, in, len); |
| 1428 | } |
| 1429 |
| 1430 | STACK_OF(TRUST_TOKEN_PRETOKEN) *pmbtoken_exp2_blind(CBB *cbb, size_t count, |
| 1431 | int include_message, |
| 1432 | const uint8_t *msg, |
| 1433 | size_t msg_len) { |
| 1434 | if (!pmbtoken_exp2_init_method()) { |
| 1435 | return NULL; |
| 1436 | } |
| 1437 | return pmbtoken_blind(&pmbtoken_exp2_method, cbb, count, include_message, msg, |
| 1438 | msg_len); |
| 1439 | } |
| 1440 |
| 1441 | int pmbtoken_exp2_sign(const TRUST_TOKEN_ISSUER_KEY *key, CBB *cbb, CBS *cbs, |
| 1442 | size_t num_requested, size_t num_to_issue, |
| 1443 | uint8_t private_metadata) { |
| 1444 | if (!pmbtoken_exp2_init_method()) { |
| 1445 | return 0; |
| 1446 | } |
| 1447 | return pmbtoken_sign(&pmbtoken_exp2_method, key, cbb, cbs, num_requested, |
| 1448 | num_to_issue, private_metadata); |
| 1449 | } |
| 1450 |
| 1451 | STACK_OF(TRUST_TOKEN) *pmbtoken_exp2_unblind( |
| 1452 | const TRUST_TOKEN_CLIENT_KEY *key, |
| 1453 | const STACK_OF(TRUST_TOKEN_PRETOKEN) *pretokens, CBS *cbs, size_t count, |
| 1454 | uint32_t key_id) { |
| 1455 | if (!pmbtoken_exp2_init_method()) { |
| 1456 | return NULL; |
| 1457 | } |
| 1458 | return pmbtoken_unblind(&pmbtoken_exp2_method, key, pretokens, cbs, count, |
| 1459 | key_id); |
| 1460 | } |
| 1461 |
| 1462 | int pmbtoken_exp2_read(const TRUST_TOKEN_ISSUER_KEY *key, |
| 1463 | uint8_t out_nonce[TRUST_TOKEN_NONCE_SIZE], |
| 1464 | uint8_t *out_private_metadata, const uint8_t *token, |
| 1465 | size_t token_len, int include_message, |
| 1466 | const uint8_t *msg, size_t msg_len) { |
| 1467 | if (!pmbtoken_exp2_init_method()) { |
| 1468 | return 0; |
| 1469 | } |
| 1470 | return pmbtoken_read(&pmbtoken_exp2_method, key, out_nonce, |
| 1471 | out_private_metadata, token, token_len, include_message, |
| 1472 | msg, msg_len); |
| 1473 | } |
| 1474 |
| 1475 | int pmbtoken_exp2_get_h_for_testing(uint8_t out[97]) { |
| 1476 | if (!pmbtoken_exp2_init_method()) { |
| 1477 | return 0; |
| 1478 | } |
| 1479 | EC_AFFINE h; |
| 1480 | return ec_jacobian_to_affine(pmbtoken_exp2_method.group, &h, |
| 1481 | &pmbtoken_exp2_method.h) && |
| 1482 | ec_point_to_bytes(pmbtoken_exp2_method.group, &h, |
| 1483 | POINT_CONVERSION_UNCOMPRESSED, out, 97) == 97; |
| 1484 | } |
| 1485 |
| 1486 | // PMBTokens PST v1. |
| 1487 |
| 1488 | static int pmbtoken_pst1_hash_t(const EC_GROUP *group, EC_JACOBIAN *out, |
| 1489 | const uint8_t t[TRUST_TOKEN_NONCE_SIZE]) { |
| 1490 | const uint8_t kHashTLabel[] = "PMBTokens PST V1 HashT"; |
| 1491 | return ec_hash_to_curve_p384_xmd_sha384_sswu( |
| 1492 | group, out, kHashTLabel, sizeof(kHashTLabel), t, TRUST_TOKEN_NONCE_SIZE); |
| 1493 | } |
| 1494 |
| 1495 | static int pmbtoken_pst1_hash_s(const EC_GROUP *group, EC_JACOBIAN *out, |
| 1496 | const EC_AFFINE *t, |
| 1497 | const uint8_t s[TRUST_TOKEN_NONCE_SIZE]) { |
| 1498 | const uint8_t kHashSLabel[] = "PMBTokens PST V1 HashS"; |
| 1499 | int ret = 0; |
| 1500 | CBB cbb; |
| 1501 | uint8_t *buf = NULL; |
| 1502 | size_t len; |
| 1503 | if (!CBB_init(&cbb, 0) || !point_to_cbb(&cbb, group, t) || |
| 1504 | !CBB_add_bytes(&cbb, s, TRUST_TOKEN_NONCE_SIZE) || |
| 1505 | !CBB_finish(&cbb, &buf, &len) || |
| 1506 | !ec_hash_to_curve_p384_xmd_sha384_sswu(group, out, kHashSLabel, |
| 1507 | sizeof(kHashSLabel), buf, len)) { |
| 1508 | goto err; |
| 1509 | } |
| 1510 |
| 1511 | ret = 1; |
| 1512 |
| 1513 | err: |
| 1514 | OPENSSL_free(buf); |
| 1515 | CBB_cleanup(&cbb); |
| 1516 | return ret; |
| 1517 | } |
| 1518 |
| 1519 | static int pmbtoken_pst1_hash_c(const EC_GROUP *group, EC_SCALAR *out, |
| 1520 | uint8_t *buf, size_t len) { |
| 1521 | const uint8_t kHashCLabel[] = "PMBTokens PST V1 HashC"; |
| 1522 | return ec_hash_to_scalar_p384_xmd_sha384(group, out, kHashCLabel, |
| 1523 | sizeof(kHashCLabel), buf, len); |
| 1524 | } |
| 1525 |
| 1526 | static int pmbtoken_pst1_hash_to_scalar(const EC_GROUP *group, EC_SCALAR *out, |
| 1527 | uint8_t *buf, size_t len) { |
| 1528 | const uint8_t kHashLabel[] = "PMBTokens PST V1 HashToScalar"; |
| 1529 | return ec_hash_to_scalar_p384_xmd_sha384(group, out, kHashLabel, |
| 1530 | sizeof(kHashLabel), buf, len); |
| 1531 | } |
| 1532 |
| 1533 | static int pmbtoken_pst1_ok = 0; |
| 1534 | static PMBTOKEN_METHOD pmbtoken_pst1_method; |
| 1535 | static CRYPTO_once_t pmbtoken_pst1_method_once = CRYPTO_ONCE_INIT; |
| 1536 |
| 1537 | static void pmbtoken_pst1_init_method_impl(void) { |
| 1538 | // This is the output of |ec_hash_to_scalar_p384_xmd_sha384| with DST |
| 1539 | // "PMBTokens PST V1 HashH" and message "generator". |
| 1540 | static const uint8_t kH[] = { |
| 1541 | 0x04, 0x4c, 0xfa, 0xd4, 0x33, 0x6d, 0x8c, 0x4e, 0x18, 0xce, 0x1a, |
| 1542 | 0x82, 0x7b, 0x53, 0x8c, 0xf8, 0x63, 0x18, 0xe5, 0xa3, 0x96, 0x0d, |
| 1543 | 0x05, 0xde, 0xf4, 0x83, 0xa7, 0xd8, 0xde, 0x9c, 0x50, 0x81, 0x38, |
| 1544 | 0xc9, 0x38, 0x25, 0xa3, 0x70, 0x97, 0xc1, 0x1c, 0x33, 0x2e, 0x83, |
| 1545 | 0x68, 0x64, 0x9c, 0x53, 0x73, 0xc3, 0x03, 0xc1, 0xa9, 0xd8, 0x92, |
| 1546 | 0xa2, 0x32, 0xf4, 0x22, 0x40, 0x07, 0x2d, 0x9b, 0x6f, 0xab, 0xff, |
| 1547 | 0x2a, 0x92, 0x03, 0xb1, 0x73, 0x09, 0x1a, 0x6a, 0x4a, 0xc2, 0x4c, |
| 1548 | 0xac, 0x13, 0x59, 0xf4, 0x28, 0x0e, 0x78, 0x69, 0xa5, 0xdf, 0x0d, |
| 1549 | 0x74, 0xeb, 0x14, 0xca, 0x8a, 0x32, 0xbb, 0xd3, 0x91}; |
| 1550 |
| 1551 | pmbtoken_pst1_ok = pmbtoken_init_method( |
| 1552 | &pmbtoken_pst1_method, EC_group_p384(), kH, sizeof(kH), |
| 1553 | pmbtoken_pst1_hash_t, pmbtoken_pst1_hash_s, pmbtoken_pst1_hash_c, |
| 1554 | pmbtoken_pst1_hash_to_scalar, 0); |
| 1555 | } |
| 1556 |
| 1557 | static int pmbtoken_pst1_init_method(void) { |
| 1558 | CRYPTO_once(&pmbtoken_pst1_method_once, pmbtoken_pst1_init_method_impl); |
| 1559 | if (!pmbtoken_pst1_ok) { |
| 1560 | OPENSSL_PUT_ERROR(TRUST_TOKEN, ERR_R_INTERNAL_ERROR); |
| 1561 | return 0; |
| 1562 | } |
| 1563 | return 1; |
| 1564 | } |
| 1565 |
| 1566 | int pmbtoken_pst1_generate_key(CBB *out_private, CBB *out_public) { |
| 1567 | if (!pmbtoken_pst1_init_method()) { |
| 1568 | return 0; |
| 1569 | } |
| 1570 |
| 1571 | return pmbtoken_generate_key(&pmbtoken_pst1_method, out_private, out_public); |
| 1572 | } |
| 1573 |
| 1574 |
| 1575 | int pmbtoken_pst1_derive_key_from_secret(CBB *out_private, CBB *out_public, |
| 1576 | const uint8_t *secret, |
| 1577 | size_t secret_len) { |
| 1578 | if (!pmbtoken_pst1_init_method()) { |
| 1579 | return 0; |
| 1580 | } |
| 1581 |
| 1582 | return pmbtoken_derive_key_from_secret(&pmbtoken_pst1_method, out_private, |
| 1583 | out_public, secret, secret_len); |
| 1584 | } |
| 1585 |
| 1586 | int pmbtoken_pst1_client_key_from_bytes(TRUST_TOKEN_CLIENT_KEY *key, |
| 1587 | const uint8_t *in, size_t len) { |
| 1588 | if (!pmbtoken_pst1_init_method()) { |
| 1589 | return 0; |
| 1590 | } |
| 1591 | return pmbtoken_client_key_from_bytes(&pmbtoken_pst1_method, key, in, len); |
| 1592 | } |
| 1593 |
| 1594 | int pmbtoken_pst1_issuer_key_from_bytes(TRUST_TOKEN_ISSUER_KEY *key, |
| 1595 | const uint8_t *in, size_t len) { |
| 1596 | if (!pmbtoken_pst1_init_method()) { |
| 1597 | return 0; |
| 1598 | } |
| 1599 | return pmbtoken_issuer_key_from_bytes(&pmbtoken_pst1_method, key, in, len); |
| 1600 | } |
| 1601 |
| 1602 | STACK_OF(TRUST_TOKEN_PRETOKEN) *pmbtoken_pst1_blind(CBB *cbb, size_t count, |
| 1603 | int include_message, |
| 1604 | const uint8_t *msg, |
| 1605 | size_t msg_len) { |
| 1606 | if (!pmbtoken_pst1_init_method()) { |
| 1607 | return NULL; |
| 1608 | } |
| 1609 | return pmbtoken_blind(&pmbtoken_pst1_method, cbb, count, include_message, msg, |
| 1610 | msg_len); |
| 1611 | } |
| 1612 |
| 1613 | int pmbtoken_pst1_sign(const TRUST_TOKEN_ISSUER_KEY *key, CBB *cbb, CBS *cbs, |
| 1614 | size_t num_requested, size_t num_to_issue, |
| 1615 | uint8_t private_metadata) { |
| 1616 | if (!pmbtoken_pst1_init_method()) { |
| 1617 | return 0; |
| 1618 | } |
| 1619 | return pmbtoken_sign(&pmbtoken_pst1_method, key, cbb, cbs, num_requested, |
| 1620 | num_to_issue, private_metadata); |
| 1621 | } |
| 1622 |
| 1623 | STACK_OF(TRUST_TOKEN) *pmbtoken_pst1_unblind( |
| 1624 | const TRUST_TOKEN_CLIENT_KEY *key, |
| 1625 | const STACK_OF(TRUST_TOKEN_PRETOKEN) *pretokens, CBS *cbs, size_t count, |
| 1626 | uint32_t key_id) { |
| 1627 | if (!pmbtoken_pst1_init_method()) { |
| 1628 | return NULL; |
| 1629 | } |
| 1630 | return pmbtoken_unblind(&pmbtoken_pst1_method, key, pretokens, cbs, count, |
| 1631 | key_id); |
| 1632 | } |
| 1633 |
| 1634 | int pmbtoken_pst1_read(const TRUST_TOKEN_ISSUER_KEY *key, |
| 1635 | uint8_t out_nonce[TRUST_TOKEN_NONCE_SIZE], |
| 1636 | uint8_t *out_private_metadata, const uint8_t *token, |
| 1637 | size_t token_len, int include_message, |
| 1638 | const uint8_t *msg, size_t msg_len) { |
| 1639 | if (!pmbtoken_pst1_init_method()) { |
| 1640 | return 0; |
| 1641 | } |
| 1642 | return pmbtoken_read(&pmbtoken_pst1_method, key, out_nonce, |
| 1643 | out_private_metadata, token, token_len, include_message, |
| 1644 | msg, msg_len); |
| 1645 | } |
| 1646 |
| 1647 | int pmbtoken_pst1_get_h_for_testing(uint8_t out[97]) { |
| 1648 | if (!pmbtoken_pst1_init_method()) { |
| 1649 | return 0; |
| 1650 | } |
| 1651 | EC_AFFINE h; |
| 1652 | return ec_jacobian_to_affine(pmbtoken_pst1_method.group, &h, |
| 1653 | &pmbtoken_pst1_method.h) && |
| 1654 | ec_point_to_bytes(pmbtoken_pst1_method.group, &h, |
| 1655 | POINT_CONVERSION_UNCOMPRESSED, out, 97) == 97; |
| 1656 | } |
| 1657 |