sm2_blind.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <assert.h>
  13. #include <gmssl/sm2.h>
  14. #include <gmssl/mem.h>
  15. #include <gmssl/asn1.h>
  16. #include <gmssl/error.h>
  17. #include <gmssl/sm2_blind.h>
  18. extern SM2_BN SM2_N;
  19. extern SM2_BN SM2_ONE;
  20. int sm2_blind_sign_commit(SM2_Fn k, uint8_t *commit, size_t *commitlen)
  21. {
  22. SM2_POINT K;
  23. uint8_t k_bytes[32];
  24. sm2_fn_rand(k); // FIXME: check return
  25. sm2_bn_to_bytes(k, k_bytes);
  26. // commitment = k * G
  27. sm2_point_mul_generator(&K, k_bytes);
  28. sm2_point_to_compressed_octets(&K, commit);
  29. *commitlen = 33;
  30. gmssl_secure_clear(k_bytes, sizeof(k_bytes));
  31. return 1;
  32. }
  33. int sm2_blind_sign_init(SM2_BLIND_SIGN_CTX *ctx, const SM2_KEY *public_key, const char *id, size_t idlen)
  34. {
  35. ctx->public_key = *public_key;
  36. sm3_init(&ctx->sm3_ctx);
  37. if (id) {
  38. uint8_t z[SM3_DIGEST_SIZE];
  39. if (idlen <= 0 || idlen > SM2_MAX_ID_LENGTH) {
  40. error_print();
  41. return -1;
  42. }
  43. sm2_compute_z(z, &public_key->public_key, id, idlen);
  44. sm3_update(&ctx->sm3_ctx, z, sizeof(z));
  45. }
  46. return 1;
  47. }
  48. int sm2_blind_sign_update(SM2_BLIND_SIGN_CTX *ctx, const uint8_t *data, size_t datalen)
  49. {
  50. if (!ctx) {
  51. error_print();
  52. return -1;
  53. }
  54. if (data && datalen > 0) {
  55. sm3_update(&ctx->sm3_ctx, data, datalen);
  56. }
  57. return 1;
  58. }
  59. int sm2_blind_sign_finish(SM2_BLIND_SIGN_CTX *ctx,
  60. const uint8_t *commit, size_t commitlen,
  61. uint8_t blinded_sig_r[32])
  62. {
  63. int ret = -1;
  64. SM2_Fn a;
  65. SM2_Fn b;
  66. SM2_POINT K;
  67. SM2_Fn e;
  68. SM2_Fn r;
  69. uint8_t dgst[32];
  70. sm3_finish(&ctx->sm3_ctx, dgst);
  71. sm2_bn_from_bytes(e, dgst);
  72. if (sm2_bn_cmp(e, SM2_N) >= 0) {
  73. sm2_bn_sub(e, e, SM2_N);
  74. }
  75. //FIXME: return value of sm2_fn_rand()
  76. sm2_fn_rand(a);
  77. sm2_bn_to_bytes(a, ctx->blind_factor_a);
  78. sm2_fn_rand(b);
  79. sm2_bn_to_bytes(b, ctx->blind_factor_b);
  80. if (sm2_point_from_octets(&K, commit, commitlen) != 1) {
  81. error_print();
  82. goto end;
  83. }
  84. // K'(x1, y1) = a * K + b * G
  85. if (sm2_point_mul_sum(&K, ctx->blind_factor_a, &K, ctx->blind_factor_b) != 1) {
  86. error_print();
  87. goto end;
  88. }
  89. sm2_bn_from_bytes(r, K.x);
  90. if (sm2_bn_cmp(r, SM2_N) >= 0) {
  91. sm2_bn_sub(r, r, SM2_N);
  92. }
  93. // r = x1 + e (mod n)
  94. sm2_fn_add(r, r, e);
  95. sm2_bn_to_bytes(r, ctx->sig_r);
  96. // r' = a^-1 * (r + b)
  97. sm2_fn_add(r, r, b);
  98. sm2_fn_inv(a, a);
  99. sm2_fn_mul(r, r, a);
  100. sm2_bn_to_bytes(r, blinded_sig_r);
  101. ret = 1;
  102. end:
  103. gmssl_secure_clear(a, sizeof(a));
  104. gmssl_secure_clear(b, sizeof(b));
  105. return ret;
  106. }
  107. int sm2_blind_sign(const SM2_KEY *key, const SM2_Fn k, const uint8_t blinded_r[32], uint8_t blinded_s[32])
  108. {
  109. SM2_Fn x;
  110. SM2_Fn r;
  111. SM2_Fn s;
  112. sm2_bn_from_bytes(x, key->private_key);
  113. sm2_bn_from_bytes(r, blinded_r);
  114. // s = (1 + x)^-1 * (k - r * x) (mod n)
  115. sm2_fn_mul(r, r, x);
  116. sm2_fn_sub(s, k, r);
  117. sm2_fn_add(x, x, SM2_ONE);
  118. sm2_fn_inv(x, x);
  119. sm2_fn_mul(s, s, x);
  120. sm2_bn_to_bytes(s, blinded_s);
  121. gmssl_secure_clear(x, sizeof(x));
  122. gmssl_secure_clear(r, sizeof(r));
  123. gmssl_secure_clear(s, sizeof(s));
  124. return 1;
  125. }
  126. int sm2_blind_sign_unblind(SM2_BLIND_SIGN_CTX *ctx, const uint8_t blinded_sig_s[32], uint8_t *sig, size_t *siglen)
  127. {
  128. SM2_Fn a;
  129. SM2_Fn b;
  130. SM2_Fn s;
  131. SM2_SIGNATURE signature;
  132. sm2_bn_from_bytes(a, ctx->blind_factor_a);
  133. sm2_bn_from_bytes(b, ctx->blind_factor_b);
  134. sm2_bn_from_bytes(s, blinded_sig_s);
  135. // s = a * s' + b
  136. sm2_fn_mul(s, s, a);
  137. sm2_fn_add(s, s, b);
  138. memcpy(signature.r, ctx->sig_r, 32);
  139. sm2_bn_to_bytes(s, signature.s);
  140. *siglen = 0;
  141. sm2_signature_to_der(&signature, &sig, siglen);
  142. gmssl_secure_clear(a, sizeof(a));
  143. gmssl_secure_clear(b, sizeof(b));
  144. gmssl_secure_clear(ctx, sizeof(*ctx));
  145. return 1;
  146. }