dsa_verify.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* dsa_verify.c */
  2. /*
  3. This file is part of the ARM-Crypto-Lib.
  4. Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de)
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stdint.h>
  17. #include <string.h>
  18. #include <crypto/bigint.h>
  19. #include <crypto/dsa.h>
  20. #include <crypto/hfal-basic.h>
  21. uint8_t dsa_verify_bigint(const dsa_signature_t* s, const bigint_t* m,
  22. const dsa_ctx_t* ctx){
  23. if(s->r.length_W==0 || s->s.length_W==0){
  24. return DSA_SIGNATURE_FAIL;
  25. }
  26. if(bigint_cmp_u(&(s->r), &(ctx->domain.q))>=0 || bigint_cmp_u(&(s->s), &(ctx->domain.q))>=0){
  27. return DSA_SIGNATURE_FAIL;
  28. }
  29. bigint_t w, u1, u2, v1, v2;
  30. bigint_word_t w_b[ctx->domain.q.length_W], u1_b[ctx->domain.q.length_W*2], u2_b[ctx->domain.q.length_W*2];
  31. bigint_word_t v1_b[ctx->domain.p.length_W*2], v2_b[ctx->domain.p.length_W];
  32. w.wordv = w_b;
  33. u1.wordv = u1_b;
  34. u2.wordv = u2_b;
  35. v1.wordv = v1_b;
  36. v2.wordv = v2_b;
  37. bigint_inverse(&w, &(s->s), &(ctx->domain.q));
  38. bigint_mul_u(&u1, &w, m);
  39. bigint_reduce(&u1, &(ctx->domain.q));
  40. bigint_mul_u(&u2, &w, &(s->r));
  41. bigint_reduce(&u2, &(ctx->domain.q));
  42. bigint_expmod_u(&v1, &(ctx->domain.g), &u1, &(ctx->domain.p));
  43. bigint_expmod_u(&v2, &(ctx->pub), &u2, &(ctx->domain.p));
  44. bigint_mul_u(&v1, &v1, &v2);
  45. bigint_reduce(&v1, &(ctx->domain.p));
  46. bigint_reduce(&v1, &(ctx->domain.q));
  47. if(bigint_cmp_u(&v1, &(s->r))==0){
  48. return DSA_SIGNATURE_OK;
  49. }
  50. return DSA_SIGNATURE_FAIL;
  51. }
  52. uint8_t dsa_verify_message(const dsa_signature_t* s, const void* m, uint16_t m_len_b,
  53. const hfdesc_t* hash_desc, const dsa_ctx_t* ctx){
  54. bigint_t z;
  55. uint16_t n_B = ctx->domain.q.length_W;
  56. unsigned hash_length = (hfal_hash_getHashsize(hash_desc)+sizeof(bigint_word_t)*8-1)/(sizeof(bigint_word_t)*8);
  57. bigint_word_t hash_value[hash_length];
  58. memset(hash_value, 0, hash_length*sizeof(bigint_word_t));
  59. hfal_hash_mem(hash_desc, hash_value, m, m_len_b);
  60. z.wordv = hash_value;
  61. z.length_W = n_B;
  62. bigint_changeendianess(&z);
  63. bigint_adjust(&z);
  64. return dsa_verify_bigint(s, &z, ctx);
  65. }