-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgendilkey.c
224 lines (204 loc) · 6.15 KB
/
gendilkey.c
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
/* Copyright 2024 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "dilutils.h"
#include "mlca2.h"
#include "pqalgs.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* gAlgname = MLCA_ALGORITHM_SIG_DILITHIUM_87_R2;
#define BUF_SIZE 8000
int main(int argc, char** argv)
{
size_t sPrivKeyBytes = BUF_SIZE;
size_t sPubKeyBytes = BUF_SIZE;
size_t sWirePrivKeyBytes = BUF_SIZE;
size_t sWirePubKeyBytes = BUF_SIZE;
int sIdx = 0;
int sRc = 0;
bool sPrintHelp = false;
bool sRaw = false;
const char* sPubKeyFile = NULL;
const char* sPrivKeyFile = NULL;
for(sIdx = 1; sIdx < argc; sIdx++)
{
if(strcmp(argv[sIdx], "-h") == 0)
{
sPrintHelp = true;
}
else if (strcmp(argv[sIdx], "-raw") == 0)
{
sRaw = true;
}
else if(strcmp(argv[sIdx], "-pub") == 0)
{
sIdx++;
sPubKeyFile = argv[sIdx];
}
else if(strcmp(argv[sIdx], "-priv") == 0)
{
sIdx++;
sPrivKeyFile = argv[sIdx];
}
else if (strcmp(argv[sIdx], "-alg") == 0)
{
sIdx ++;
if (strcmp(argv[sIdx], "dilr2-87") == 0) {
gAlgname = MLCA_ALGORITHM_SIG_DILITHIUM_87_R2;
}
else if (strcmp(argv[sIdx], "mldsa-87") == 0) {
gAlgname = MLCA_ALGORITHM_SIG_MLDSA_87;
}
else
{
printf("**** ERROR : Unknown algoritym : %s\n", argv[sIdx]);
sPrintHelp = true;
}
}
else
{
printf("**** ERROR : Unknown parameter : %s\n", argv[sIdx]);
sPrintHelp = true;
}
}
if(!sPrintHelp && (NULL == sPubKeyFile || NULL == sPrivKeyFile))
{
printf("**** ERROR : Missing input parms\n");
sPrintHelp = true;
}
if(sPrintHelp)
{
printf("\ngendilkey -priv <private key file> -pub <public key file> [-alg <algorithm>]\n");
printf("\n");
printf("\t-alg <dilr2-87|mldsa-87>\tDefault: dilr2-87\n");
exit(0);
}
mlca_ctx_t sCtx;
MLCA_RC sMlRc = 0;
unsigned char* sPubKey = malloc(BUF_SIZE);
unsigned char* sPrivKey = malloc(BUF_SIZE);
unsigned char* sWirePubKey = malloc(BUF_SIZE);
unsigned char* sWirePrivKey = malloc(BUF_SIZE);
if(!sPubKey || !sPrivKey || !sWirePubKey || !sWirePrivKey)
{
printf("**** ERROR : Allocation Failure\n");
exit(1);
}
if(0 == sRc)
{
sMlRc = mlca_init(&sCtx, 1, 0);
if(sMlRc)
{
printf("**** ERROR : Failed mlca_init : %d\n", sMlRc);
sRc = 1;
}
}
if(0 == sRc)
{
sMlRc = mlca_set_alg(&sCtx, gAlgname, OPT_LEVEL_AUTO);
if(sMlRc)
{
printf("**** ERROR : Failed mlca_set_alg : %d\n", sMlRc);
sRc = 1;
}
}
if(0 == sRc)
{
sMlRc = mlca_set_encoding_by_idx(&sCtx, 0);
if(sMlRc)
{
printf("**** ERROR : Failed mlca_set_encoding_by_name_oid : %d\n", sMlRc);
sRc = 1;
}
}
if(0 == sRc)
{
printf("Generating %s key pair ...\n", gAlgname);
sPubKeyBytes = mlca_sig_crypto_publickeybytes(&sCtx);
sPrivKeyBytes = mlca_sig_crypto_secretkeybytes(&sCtx);
sMlRc = mlca_sig_keygen(&sCtx, sPubKey, sPrivKey);
if(sMlRc)
{
printf("**** ERROR : Failed mlca_sig_keygen : %d\n", sMlRc);
sRc = 1;
}
}
if (sRaw)
{
// Just copy over the key
memcpy(sWirePrivKey, sPrivKey, sPrivKeyBytes);
sWirePrivKeyBytes = sPrivKeyBytes;
memcpy(sWirePubKey, sPubKey, sPubKeyBytes);
sWirePubKeyBytes = sPubKeyBytes;
}
else
{
if(0 == sRc)
{
// Convert private key
sRc = mlca_key2wire(sWirePrivKey,
sWirePrivKeyBytes,
sPrivKey,
sPrivKeyBytes,
0,
sPubKey,
sPubKeyBytes,
NULL,
0);
if(sRc < 0)
{
printf("**** ERROR: Failure during private key conversion : %d\n", sRc);
}
else
{
sWirePrivKeyBytes = sRc;
sRc = 0;
}
}
if(0 == sRc)
{
// Convert public key
sRc = mlca_key2wire(
sWirePubKey, sWirePubKeyBytes, sPubKey, sPubKeyBytes, 0, NULL, 0, NULL, 0);
if(sRc < 0)
{
printf("**** ERROR: Failure during public key conversion : %d\n", sRc);
sRc = 1;
}
else
{
sWirePubKeyBytes = sRc;
sRc = 0;
}
}
}
if(0 == sRc)
{
writeFile(sWirePrivKey, sWirePrivKeyBytes, sPrivKeyFile);
writeFile(sWirePubKey, sWirePubKeyBytes, sPubKeyFile);
printf("Private Key Size : %lu\n", sPrivKeyBytes);
printf("Public Key Size : %lu\n", sPubKeyBytes);
printf("Private Key File : %s\n", sPrivKeyFile);
printf("Public Key File : %s\n", sPubKeyFile);
}
free(sPrivKey);
free(sPubKey);
free(sWirePrivKey);
free(sWirePubKey);
mlca_ctx_free(&sCtx);
exit(sRc);
}