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