File: builtin_curves.cpp

package info (click to toggle)
xca 2.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,328 kB
  • sloc: cpp: 30,584; sh: 341; xml: 74; makefile: 56; python: 34
file content (181 lines) | stat: -rw-r--r-- 3,705 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2014 - 2020 Christian Hohnstaedt.
 *
 * All rights reserved.
 */
#include <openssl/evp.h>
#include <QDebug>
#include <QList>

#include "builtin_curves.h"
#include "exception.h"
#include "func.h"

#ifndef OPENSSL_NO_EC
#include <openssl/ec.h>
#include "opensc-pkcs11.h"

static const QList<int> rfc5480_curve_nids()
{
	return QList<int> {
	NID_X9_62_prime192v1,
	NID_secp224r1,
	NID_X9_62_prime256v1,
	NID_secp384r1,
	NID_secp521r1,
	};
};

static const QList<int> x962_curve_nids()
{
	return QList<int> {
	NID_X9_62_prime192v1,
	NID_X9_62_prime192v2,
	NID_X9_62_prime192v3,
	NID_X9_62_prime239v1,
	NID_X9_62_prime239v2,
	NID_X9_62_prime239v3,
	NID_X9_62_prime256v1,

	NID_X9_62_c2pnb163v1,
	NID_X9_62_c2pnb163v2,
	NID_X9_62_c2pnb163v3,
	NID_X9_62_c2pnb176v1,
	NID_X9_62_c2tnb191v1,
	NID_X9_62_c2tnb191v2,
	NID_X9_62_c2tnb191v3,
	NID_X9_62_c2pnb208w1,
	NID_X9_62_c2tnb239v1,
	NID_X9_62_c2tnb239v2,
	NID_X9_62_c2tnb239v3,
	NID_X9_62_c2pnb272w1,
	NID_X9_62_c2pnb304w1,
	NID_X9_62_c2tnb359v1,
	NID_X9_62_c2pnb368w1,
	NID_X9_62_c2tnb431r1
	};
};

static const QList<int> other_curve_nids()
{
	return QList<int> {
	NID_sect113r1,
	NID_sect113r2,
	NID_sect131r1,
	NID_sect131r2,
	NID_sect163k1,
	NID_sect163r1,
	NID_sect163r2,
	NID_sect193r1,
	NID_sect193r2,
	NID_sect233k1,
	NID_sect233r1,
	NID_sect239k1,
	NID_sect283k1,
	NID_sect283r1,
	NID_sect409k1,
	NID_sect409r1,
	NID_sect571k1,
	NID_sect571r1,

	NID_secp112r1,
	NID_secp112r2,
	NID_secp128r1,
	NID_secp128r2,
	NID_secp160k1,
	NID_secp160r1,
	NID_secp160r2,
	NID_secp192k1,
	NID_secp224k1,
	NID_secp224r1,
	NID_secp256k1,
	NID_secp384r1,
	NID_secp521r1,

	NID_wap_wsg_idm_ecid_wtls1,
	NID_wap_wsg_idm_ecid_wtls3,
	NID_wap_wsg_idm_ecid_wtls4,
	NID_wap_wsg_idm_ecid_wtls5,
	NID_wap_wsg_idm_ecid_wtls6,
	NID_wap_wsg_idm_ecid_wtls7,
	NID_wap_wsg_idm_ecid_wtls8,
	NID_wap_wsg_idm_ecid_wtls9,
	NID_wap_wsg_idm_ecid_wtls10,
	NID_wap_wsg_idm_ecid_wtls11,
	NID_wap_wsg_idm_ecid_wtls12,

#ifdef NID_brainpoolP160r1
	NID_brainpoolP160r1,
	NID_brainpoolP160t1,
	NID_brainpoolP192r1,
	NID_brainpoolP192t1,
	NID_brainpoolP224r1,
	NID_brainpoolP224t1,
	NID_brainpoolP256r1,
	NID_brainpoolP256t1,
	NID_brainpoolP320r1,
	NID_brainpoolP320t1,
	NID_brainpoolP384r1,
	NID_brainpoolP384t1,
	NID_brainpoolP512r1,
	NID_brainpoolP512t1
#endif
	};
};

builtin_curves::builtin_curves()
{
	int i, num_curves = EC_get_builtin_curves(NULL, 0);
	EC_builtin_curve *curves = new EC_builtin_curve[num_curves];

	Q_CHECK_PTR(curves);

	BIGNUM *order = BN_new();
	Q_CHECK_PTR(order);

	EC_get_builtin_curves(curves, num_curves);

	for (i=0; i < num_curves; i++) {
		int flag = 0, nid = curves[i].nid;
		unsigned long type = 0;

		if (rfc5480_curve_nids().contains(nid))
			flag = CURVE_RFC5480;
		else if (x962_curve_nids().contains(nid))
			flag = CURVE_X962;
		else if (other_curve_nids().contains(nid))
			flag = CURVE_OTHER;
		else
			continue;

		EC_GROUP *group = EC_GROUP_new_by_curve_name(nid);
		EC_GROUP_get_order(group, order, NULL);

		switch (EC_METHOD_get_field_type(EC_GROUP_method_of(group))) {
		case NID_X9_62_prime_field:
			type = CKF_EC_F_P;
			break;
		case NID_X9_62_characteristic_two_field:
			type = CKF_EC_F_2M;
			break;
		default:
			continue;
		}
#undef PRINT_KNOWN_CURVES
#ifdef PRINT_KNOWN_CURVES
		qDebug() << QString(curves[i].comment).leftJustified(50)
			<< QString(OBJ_nid2sn(nid)).leftJustified(27)
			<< OBJ_obj2QString(OBJ_nid2obj(nid),1).leftJustified(20)
			<< (type == CKF_EC_F_P ? "Fp" : "F2m");
#endif
		append(builtin_curve(nid, QString(curves[i].comment),
			BN_num_bits(order), flag, type));
                EC_GROUP_free(group);
	}
	BN_free(order);
	delete[] curves;
}
#else
builtin_curves::builtin_curves() { }

#endif