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
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <gmssl/ec.h>
15 #include <gmssl/error.h>
16
17
18 #define oid_sm_scheme 1,2,156,10197,1
19 static uint32_t oid_sm2[] = { oid_sm_scheme,301 };
20
21 #define oid_x9_62_curves oid_x9_62,3
22 #define oid_x9_62_prime_curves oid_x9_62_curves,1
23 static uint32_t oid_prime192v1[] = { oid_x9_62_prime_curves,1 };
24 static uint32_t oid_prime256v1[] = { oid_x9_62_prime_curves,7 }; // NIST P-256
25
26 #define oid_secg_curve 1,3,132,0
27 static uint32_t oid_secp256k1[] = { oid_secg_curve,10 };
28 static uint32_t oid_secp384r1[] = { oid_secg_curve,34 }; // NIST P-384
29 static uint32_t oid_secp521r1[] = { oid_secg_curve,35 }; // NIST P-521
30
31
32 static const ASN1_OID_INFO ec_named_curves[] = {
33 { OID_sm2, "sm2p256v1", oid_sm2, sizeof(oid_sm2)/sizeof(int), 0, "SM2" },
34 { OID_prime192v1, "prime192v1", oid_prime192v1, sizeof(oid_prime192v1)/sizeof(int), 0, },
35 { OID_prime256v1, "prime256v1", oid_prime256v1, sizeof(oid_prime256v1)/sizeof(int), 0, "NIST P-256" },
36 { OID_secp256k1, "secp256k1", oid_secp256k1, sizeof(oid_secp256k1)/sizeof(int) },
37 { OID_secp384r1, "secp384r1", oid_secp384r1, sizeof(oid_secp384r1)/sizeof(int), 0, "NIST P-384" },
38 { OID_secp521r1, "secp521r1", oid_secp521r1, sizeof(oid_secp521r1)/sizeof(int), 0, "NIST P-521" }
39 };
40
41 static const int ec_named_curves_count =
42 sizeof(ec_named_curves)/sizeof(ec_named_curves[0]);
43
ec_named_curve_name(int oid)44 const char *ec_named_curve_name(int oid)
45 {
46 const ASN1_OID_INFO *info;
47 if (!(info = asn1_oid_info_from_oid(ec_named_curves, ec_named_curves_count, oid))) {
48 error_print();
49 return NULL;
50 }
51 return info->name;
52 }
53
ec_named_curve_from_name(const char * name)54 int ec_named_curve_from_name(const char *name)
55 {
56 const ASN1_OID_INFO *info;
57 if (!(info = asn1_oid_info_from_name(ec_named_curves, ec_named_curves_count, name))) {
58 error_print();
59 return OID_undef;
60 }
61 return info->oid;
62 }
63
ec_named_curve_to_der(int oid,uint8_t ** out,size_t * outlen)64 int ec_named_curve_to_der(int oid, uint8_t **out, size_t *outlen)
65 {
66 const ASN1_OID_INFO *info;
67 if (!(info = asn1_oid_info_from_oid(ec_named_curves, ec_named_curves_count, oid))) {
68 error_print();
69 return -1;
70 }
71 if (asn1_object_identifier_to_der(info->nodes, info->nodes_cnt, out, outlen) != 1) {
72 error_print();
73 return -1;
74 }
75 return 1;
76 }
77
ec_named_curve_from_der(int * oid,const uint8_t ** in,size_t * inlen)78 int ec_named_curve_from_der(int *oid, const uint8_t **in, size_t *inlen)
79 {
80 int ret;
81 const ASN1_OID_INFO *info;
82 if ((ret = asn1_oid_info_from_der(&info, ec_named_curves, ec_named_curves_count, in, inlen)) != 1) {
83 if (ret < 0) error_print();
84 else *oid = -1;
85 return ret;
86 }
87 *oid = info->oid;
88 return 1;
89 }
90
ec_point_print(FILE * fp,int fmt,int ind,const char * label,const uint8_t * d,size_t dlen)91 int ec_point_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen)
92 {
93 const uint8_t *p;
94 size_t len;
95
96 if (asn1_octet_string_from_der(&p, &len, &d, &dlen) != 1) goto err;
97 format_bytes(fp, fmt, ind, label, p, len);
98 if (asn1_length_is_zero(dlen) != 1) goto err;
99 return 1;
100 err:
101 error_print();
102 return -1;
103 }
104
ec_private_key_print(FILE * fp,int fmt,int ind,const char * label,const uint8_t * d,size_t dlen)105 int ec_private_key_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen)
106 {
107 int ret;
108 const uint8_t *a;
109 size_t alen;
110 const uint8_t *p;
111 size_t len;
112 int val;
113
114 format_print(fp, fmt, ind, "%s\n", label);
115 ind += 4;
116
117 if (asn1_int_from_der(&val, &d, &dlen) != 1) goto err;
118 format_print(fp, fmt, ind, "version: %d\n", val);
119 if (asn1_octet_string_from_der(&p, &len, &d, &dlen) != 1) goto err;
120 format_bytes(fp, fmt, ind, "privateKey", p, len);
121 if ((ret = asn1_explicit_from_der(0, &a, &alen, &d, &dlen)) < 0) goto err;
122 else if (ret) {
123 if (ec_named_curve_from_der(&val, &a, &alen) != 1) goto err;
124 format_print(fp, fmt, ind, "parameters: %s\n", ec_named_curve_name(val));
125 if (asn1_length_is_zero(alen) != 1) goto err;
126 }
127 format_print(fp, fmt, ind, "publicKey\n");
128 ind += 4;
129 if ((ret = asn1_explicit_from_der(1, &a, &alen, &d, &dlen)) < 0) goto err;
130 else if (ret) {
131 if (asn1_bit_octets_from_der(&p, &len, &a, &alen) != 1) goto err;
132 format_bytes(fp, fmt, ind, "ECPoint", p, len);
133 if (asn1_length_is_zero(alen) != 1) goto err;
134 }
135 if (asn1_length_is_zero(dlen) != 1) goto err;
136 return 1;
137 err:
138 error_print();
139 return -1;
140 }
141