]> git.cryptolib.org Git - arm-crypto-lib.git/blob - dsa/dsa_sign.c
e8c826ab308057cdd7f00a67f42368518edb981c
[arm-crypto-lib.git] / dsa / dsa_sign.c
1 /* dsa_sign.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 "hashfunction_descriptor.h"
25 #include "hfal-basic.h"
26
27 #define MAX(a,b) (((a)>(b))?(a):(b))
28
29 uint8_t dsa_sign_bigint(dsa_signature_t* s, const bigint_t* m,
30                                 const dsa_ctx_t* ctx, const bigint_t* k){
31         bigint_t tmp, tmp2;
32         bigint_word_t tmp_b[ctx->domain.p.length_W+5], tmp2_b[ctx->domain.q.length_W+5];
33         tmp.wordv= tmp_b;
34         tmp2.wordv = tmp2_b;
35         bigint_expmod_u(&tmp, &(ctx->domain.g), k, &(ctx->domain.p));
36         bigint_reduce(&tmp, &(ctx->domain.q));
37         bigint_copy(&(s->r), &tmp);
38         bigint_mul_u(&tmp, &tmp, &(ctx->priv));
39         bigint_add_u(&tmp, &tmp, m);
40         bigint_inverse(&tmp2, k, &(ctx->domain.q));
41         bigint_mul_u(&tmp, &tmp, &tmp2);
42         bigint_reduce(&tmp, &(ctx->domain.q));
43         bigint_copy(&(s->s), &tmp);
44
45         if(s->s.length_W==0 || s->r.length_W==0){
46                 return 1;
47         }
48
49         return 0;
50 }
51
52 uint8_t dsa_sign_message(dsa_signature_t* s, const void* m, uint16_t m_len_b,
53                                 const hfdesc_t* hash_desc, const dsa_ctx_t* ctx,
54                                 uint8_t(*rand_in)(void)){
55         bigint_t z, k;
56         uint16_t i, n_B = ctx->domain.q.length_W;
57         unsigned hash_length = MAX(n_B,(hfal_hash_getHashsize(hash_desc)+sizeof(bigint_word_t)*8-1)/(sizeof(bigint_word_t)*8));
58         bigint_word_t hash_value[hash_length];
59         bigint_word_t k_b[n_B];
60         memset(hash_value, 0, hash_length*sizeof(bigint_word_t));
61         hfal_hash_mem(hash_desc, hash_value, m, m_len_b);
62         z.wordv = hash_value;
63         z.length_W = n_B;
64         bigint_changeendianess(&z);
65         k.wordv = k_b;
66         k.length_W = n_B;
67         do{
68                 for(i=0; i<n_B*sizeof(bigint_word_t); ++i){
69                         ((uint8_t*)k_b)[i] = rand_in();
70                 }
71                 k.length_W = n_B;
72                 bigint_adjust(&k);
73         }while(dsa_sign_bigint(s, &z, ctx, &k));
74         return 0;
75 }
76