ecdsa(openssl)

基于OpenSSL 1.1.0g版本的ECDSA实现。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <openssl/crypto.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/bn.h>
#include <openssl/ec.h>
#include <openssl/engine.h>
#include <openssl/err.h>

typedef enum
{
    EC_CURVE_NISTP256 = 0,
    EC_CURVE_BRAINPOOLP256R1 = 1,
    EC_CURVE_MAX = 2
} EC_CURVE_TYPE;

/*
 * the r and s value of the signature are stored in big endian in the buffer.
 * return 0 if success, others for failed.
 */
int sign_for_xxxx(const char *message_tbs, unsigned int message_tbs_len, EC_CURVE_TYPE curve_type,
                  const char *private_key, unsigned int private_key_len,
                  char **signature_r, unsigned int *signature_r_len,
                  char **signature_s, unsigned int *signature_s_len
                 )
{
    int ret = 1;
    int ec_type = 1;
    int nid;
    EC_KEY *ec_key = NULL;
    ECDSA_SIG *signature = NULL;
    unsigned char message_hashed[SHA256_DIGEST_LENGTH];
    BN_CTX *bn_ctx = NULL;
    BIGNUM *private_key_bn = NULL;
    BIGNUM *signature_r_bn = NULL, *signature_s_bn = NULL;

    if (message_tbs == NULL || message_tbs_len == 0 || curve_type >= EC_CURVE_MAX || curve_type < 0 ||
        private_key == NULL || private_key_len > 32 || signature_r == NULL || signature_r_len == NULL ||
        signature_s == NULL || signature_s_len == NULL) {
        return -1;
    }

    if (curve_type == EC_CURVE_NISTP256) {
        nid = NID_X9_62_prime256v1;
    } else if (curve_type == EC_CURVE_BRAINPOOLP256R1) {
        nid = NID_brainpoolP256r1;
    } else {
        return -1;
    }

    /* generate the hash of the message */
    if (!EVP_Digest(message_tbs, message_tbs_len, message_hashed, NULL, EVP_sha256(), NULL))
        goto err;

    /* generate an ecdsa key and set the private key*/
    if ((ec_key = EC_KEY_new_by_curve_name(nid)) == NULL)
        goto err;
    private_key_bn = BN_new();
    if (private_key_bn == NULL)
        goto err;
    BN_bin2bn(private_key, private_key_len, private_key_bn);
    EC_KEY_set_private_key(ec_key, private_key_bn);

    /* create signature */
    if ((signature = ECDSA_do_sign(message_hashed, SHA256_DIGEST_LENGTH, ec_key)) == NULL) {
        goto err;
    }

    /* get the r and s value of the signature */
    ECDSA_SIG_get0(signature, &signature_r_bn, &signature_s_bn);
    *signature_r_len = BN_num_bytes(signature_r_bn);
    *signature_s_len = BN_num_bytes(signature_s_bn);
    *signature_r = (char *)malloc(*signature_r_len);
    *signature_s = (char *)malloc(*signature_s_len);
    if (*signature_r == NULL || *signature_s == NULL) {
        goto err;
    }
    BN_bn2bin(signature_r_bn, *signature_r);
    BN_bn2bin(signature_s_bn, *signature_s);

    ret = 0;

err:
    EC_KEY_free(ec_key);
    ec_key = NULL;
    ECDSA_SIG_free(signature);
    signature = NULL;
    BN_free(private_key_bn);
    private_key_bn = NULL;
    BN_CTX_free(bn_ctx);
    bn_ctx = NULL;

    return ret;
}

/*
 * return 0 if verify success, others for failed.
 */
int verify_for_xxxx(const char *message_tbv, unsigned int message_tbv_len, EC_CURVE_TYPE curve_type,
                    const char *public_key_x, unsigned int public_key_x_len,
                    const char *public_key_y, unsigned int public_key_y_len,
                    const char *signature_r, unsigned int signature_r_len,
                    const char *signature_s, unsigned int signature_s_len
                   )
{
    int ret = 1;
    int nid;

    EC_KEY *ec_key = NULL;
    ECDSA_SIG *signature = NULL;
    unsigned char message_hashed[SHA256_DIGEST_LENGTH];

    BN_CTX *bn_ctx = NULL;
    EC_POINT *public_key = NULL;
    BIGNUM *public_key_x_bn = NULL, *public_key_y_bn = NULL;
    BIGNUM *signature_r_bn = NULL, *signature_s_bn = NULL;

    if (message_tbv == NULL || message_tbv_len == 0 || curve_type >= EC_CURVE_MAX || curve_type < 0 ||
        public_key_x == NULL || public_key_x_len > 32 || public_key_y == NULL || public_key_y_len == 0 ||
        signature_r == NULL || signature_r_len == 0 || signature_s == NULL || signature_s_len == 0) {
        return -1;
    }

    if (curve_type == EC_CURVE_NISTP256) {
        nid = NID_X9_62_prime256v1;
    } else if (curve_type == EC_CURVE_BRAINPOOLP256R1) {
        nid = NID_brainpoolP256r1;
    } else {
        return -1;
    }

    /* generate the hash of the message */
    if (!EVP_Digest(message_tbv, message_tbv_len, message_hashed, NULL, EVP_sha256(), NULL)) {
        goto err;
    }

    /* create an ecdsa key and set the public key */
    if ((ec_key = EC_KEY_new_by_curve_name(nid)) == NULL) {
        goto err;
    }

    /* set the signature */
    signature = ECDSA_SIG_new();
    signature_r_bn = BN_new();
    signature_s_bn = BN_new();
    if (signature == NULL || signature_r_bn == NULL || signature_s_bn == NULL) {
        goto err;
    }
    BN_bin2bn(signature_r, signature_r_len, signature_r_bn);
    BN_bin2bn(signature_s, signature_s_len, signature_s_bn);
    ECDSA_SIG_set0(signature, signature_r_bn, signature_s_bn);

    /* set the public key */
    public_key_x_bn = BN_new();
    public_key_y_bn = BN_new();
    if (public_key_x_bn == NULL || public_key_y_bn == NULL) {
        goto err;
    }
    BN_bin2bn(public_key_x, public_key_x_len, public_key_x_bn);
    BN_bin2bn(public_key_y, public_key_y_len, public_key_y_bn);
    bn_ctx = BN_CTX_new();
    public_key = EC_POINT_new(EC_KEY_get0_group(ec_key));
    if (public_key == NULL) {
        goto err;
    }
    EC_POINT_set_affine_coordinates_GFp(EC_KEY_get0_group(ec_key), public_key, public_key_x_bn, public_key_y_bn, bn_ctx);
    EC_KEY_set_public_key(ec_key, public_key);

    /* verify signature */
    if (ECDSA_do_verify(message_hashed, SHA256_DIGEST_LENGTH, signature, ec_key) != 1) {
        goto err;
    }

    ret = 0;

 err:
    EC_KEY_free(ec_key);
    ec_key = NULL;
    ECDSA_SIG_free(signature);
    signature = NULL;
    EC_POINT_free(public_key);
    public_key = NULL;
    BN_free(public_key_x_bn);
    public_key_x_bn = NULL;
    BN_free(public_key_y_bn);
    public_key_y_bn = NULL;
    BN_CTX_free(bn_ctx);
    bn_ctx = NULL;

    return ret;
}

int main(void)
{
    int ret = 1;

    char message_tbs[] = "longshine is a good man!";
    char public_key_x[32] = {0x78, 0x3a, 0x96, 0x4e, 0x3c, 0x85, 0x99, 0xea, 0x51, 0xb9, 0xe9, 0x23, 0x63, 0x37, 0x2f, 0x8c,
                             0x24, 0x2c, 0x2d, 0xdc, 0xa7, 0xec, 0x0d, 0xab, 0x62, 0x4d, 0x23, 0xa6, 0xd4, 0xb5, 0x0c, 0xea};
    char public_key_y[32] = {0xa7, 0xde, 0x02, 0x38, 0xe3, 0xf0, 0x8e, 0xf5, 0xd6, 0xba, 0xe8, 0x06, 0x58, 0xd0, 0x5f, 0x57,
                             0x97, 0xe0, 0xee, 0x46, 0x28, 0x10, 0x38, 0x15, 0xa3, 0x66, 0xfd, 0x21, 0xc7, 0x4d, 0x83, 0x50};
    char private_key[32] = {0x57, 0x13, 0x5c, 0x39, 0x89, 0x78, 0x4c, 0x03, 0x7f, 0xa8, 0x8a, 0xc7, 0x60, 0xce, 0xa6, 0x97,
                            0x37, 0x5f, 0x55, 0x9a, 0x78, 0x88, 0x9a, 0x47, 0x21, 0x32, 0x0d, 0x61, 0x82, 0xf5, 0xdc, 0x56};
    unsigned int public_key_x_len = 32;
    unsigned int public_key_y_len = 32;
    unsigned int private_key_len = 32;

    /*
     * the buffer of the r and s value are allocated inside of the function sign_for_xxxx(),
     * they should be freed by someone who called the function sign_for_xxxx().
     */
    char *signature_r = NULL, *signature_s = NULL;
    unsigned int signature_r_len = 0, signature_s_len = 0;

    ret = sign_for_xxxx(message_tbs, 24, EC_CURVE_BRAINPOOLP256R1, private_key, 32, &signature_r, &signature_r_len, &signature_s, &signature_s_len);
    if (ret == 0) {
        printf("sign success\n");
        printf("the r value is:\n");
        BIO_dump_fp(stdout, signature_r, signature_r_len);
        printf("the s value is:\n");
        BIO_dump_fp(stdout, signature_s, signature_s_len);
    } else {
        printf("sign failed\n");
        goto err;
    }

    ret = verify_for_xxxx(message_tbs, 24, EC_CURVE_BRAINPOOLP256R1, public_key_x, 32, public_key_y, 32,
                          signature_r, signature_r_len, signature_s, signature_s_len);
    if (ret == 0) {
        printf("\nverify success\n");
    } else {
        printf("\nverify failed\n");
        goto err;
    }

err:
    free(signature_r);
    free(signature_s);

    return ret;
}