1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/cmd/makepqg/makepqg.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,355 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "prtypes.h" 1.9 +#include "prtime.h" 1.10 +#include "prlong.h" 1.11 + 1.12 +#include "nss.h" 1.13 +#include "secutil.h" 1.14 +#include "secitem.h" 1.15 +#include "pk11func.h" 1.16 +#include "pk11pqg.h" 1.17 + 1.18 +#if defined(XP_UNIX) 1.19 +#include <unistd.h> 1.20 +#endif 1.21 + 1.22 +#include "plgetopt.h" 1.23 + 1.24 +#define BPB 8 /* bits per byte. */ 1.25 + 1.26 +char *progName; 1.27 + 1.28 + 1.29 +const SEC_ASN1Template seckey_PQGParamsTemplate[] = { 1.30 + { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(SECKEYPQGParams) }, 1.31 + { SEC_ASN1_INTEGER, offsetof(SECKEYPQGParams,prime) }, 1.32 + { SEC_ASN1_INTEGER, offsetof(SECKEYPQGParams,subPrime) }, 1.33 + { SEC_ASN1_INTEGER, offsetof(SECKEYPQGParams,base) }, 1.34 + { 0, } 1.35 +}; 1.36 + 1.37 + 1.38 + 1.39 +void 1.40 +Usage(void) 1.41 +{ 1.42 + fprintf(stderr, "Usage: %s\n", progName); 1.43 + fprintf(stderr, 1.44 +"-a Output DER-encoded PQG params, BTOA encoded.\n" 1.45 +"-b Output DER-encoded PQG params in binary\n" 1.46 +"-r Output P, Q and G in ASCII hexadecimal. \n" 1.47 +" -l prime-length Length of prime in bits (1024 is default)\n" 1.48 +" -n subprime-length Length of subprime in bits\n" 1.49 +" -o file Output to this file (default is stdout)\n" 1.50 +" -g bits Generate SEED this many bits long.\n" 1.51 +); 1.52 + exit(-1); 1.53 + 1.54 +} 1.55 + 1.56 +SECStatus 1.57 +outputPQGParams(PQGParams * pqgParams, PRBool output_binary, PRBool output_raw, 1.58 + FILE * outFile) 1.59 +{ 1.60 + PLArenaPool * arena = NULL; 1.61 + char * PQG; 1.62 + SECItem * pItem; 1.63 + int cc; 1.64 + SECStatus rv; 1.65 + SECItem encodedParams; 1.66 + 1.67 + if (output_raw) { 1.68 + SECItem item; 1.69 + 1.70 + rv = PK11_PQG_GetPrimeFromParams(pqgParams, &item); 1.71 + if (rv) { 1.72 + SECU_PrintError(progName, "PK11_PQG_GetPrimeFromParams"); 1.73 + return rv; 1.74 + } 1.75 + SECU_PrintInteger(outFile, &item, "Prime", 1); 1.76 + SECITEM_FreeItem(&item, PR_FALSE); 1.77 + 1.78 + rv = PK11_PQG_GetSubPrimeFromParams(pqgParams, &item); 1.79 + if (rv) { 1.80 + SECU_PrintError(progName, "PK11_PQG_GetPrimeFromParams"); 1.81 + return rv; 1.82 + } 1.83 + SECU_PrintInteger(outFile, &item, "Subprime", 1); 1.84 + SECITEM_FreeItem(&item, PR_FALSE); 1.85 + 1.86 + rv = PK11_PQG_GetBaseFromParams(pqgParams, &item); 1.87 + if (rv) { 1.88 + SECU_PrintError(progName, "PK11_PQG_GetPrimeFromParams"); 1.89 + return rv; 1.90 + } 1.91 + SECU_PrintInteger(outFile, &item, "Base", 1); 1.92 + SECITEM_FreeItem(&item, PR_FALSE); 1.93 + 1.94 + fprintf(outFile, "\n"); 1.95 + return SECSuccess; 1.96 + } 1.97 + 1.98 + encodedParams.data = NULL; 1.99 + encodedParams.len = 0; 1.100 + arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); 1.101 + if (!arena) { 1.102 + SECU_PrintError(progName, "PORT_NewArena"); 1.103 + return SECFailure; 1.104 + } 1.105 + pItem = SEC_ASN1EncodeItem(arena, &encodedParams, pqgParams, 1.106 + seckey_PQGParamsTemplate); 1.107 + if (!pItem) { 1.108 + SECU_PrintError(progName, "SEC_ASN1EncodeItem"); 1.109 + PORT_FreeArena(arena, PR_FALSE); 1.110 + return SECFailure; 1.111 + } 1.112 + if (output_binary) { 1.113 + size_t len; 1.114 + len = fwrite(encodedParams.data, 1, encodedParams.len, outFile); 1.115 + PORT_FreeArena(arena, PR_FALSE); 1.116 + if (len != encodedParams.len) { 1.117 + fprintf(stderr, "%s: fwrite failed\n", progName); 1.118 + return SECFailure; 1.119 + } 1.120 + return SECSuccess; 1.121 + } 1.122 + 1.123 + /* must be output ASCII */ 1.124 + PQG = BTOA_DataToAscii(encodedParams.data, encodedParams.len); 1.125 + PORT_FreeArena(arena, PR_FALSE); 1.126 + if (!PQG) { 1.127 + SECU_PrintError(progName, "BTOA_DataToAscii"); 1.128 + return SECFailure; 1.129 + } 1.130 + 1.131 + cc = fprintf(outFile,"%s\n",PQG); 1.132 + PORT_Free(PQG); 1.133 + if (cc <= 0) { 1.134 + fprintf(stderr, "%s: fprintf failed\n", progName); 1.135 + return SECFailure; 1.136 + } 1.137 + return SECSuccess; 1.138 +} 1.139 + 1.140 +SECStatus 1.141 +outputPQGVerify(PQGVerify * pqgVerify, PRBool output_binary, PRBool output_raw, 1.142 + FILE * outFile) 1.143 +{ 1.144 + SECStatus rv = SECSuccess; 1.145 + if (output_raw) { 1.146 + SECItem item; 1.147 + unsigned int counter; 1.148 + 1.149 + rv = PK11_PQG_GetHFromVerify(pqgVerify, &item); 1.150 + if (rv) { 1.151 + SECU_PrintError(progName, "PK11_PQG_GetHFromVerify"); 1.152 + return rv; 1.153 + } 1.154 + SECU_PrintInteger(outFile, &item, "h", 1); 1.155 + SECITEM_FreeItem(&item, PR_FALSE); 1.156 + 1.157 + rv = PK11_PQG_GetSeedFromVerify(pqgVerify, &item); 1.158 + if (rv) { 1.159 + SECU_PrintError(progName, "PK11_PQG_GetSeedFromVerify"); 1.160 + return rv; 1.161 + } 1.162 + SECU_PrintInteger(outFile, &item, "SEED", 1); 1.163 + fprintf(outFile, " g: %d\n", item.len * BPB); 1.164 + SECITEM_FreeItem(&item, PR_FALSE); 1.165 + 1.166 + counter = PK11_PQG_GetCounterFromVerify(pqgVerify); 1.167 + fprintf(outFile, " counter: %d\n", counter); 1.168 + fprintf(outFile, "\n"); 1.169 + } 1.170 + return rv; 1.171 +} 1.172 + 1.173 +int 1.174 +main(int argc, char **argv) 1.175 +{ 1.176 + FILE * outFile = NULL; 1.177 + char * outFileName = NULL; 1.178 + PQGParams * pqgParams = NULL; 1.179 + PQGVerify * pqgVerify = NULL; 1.180 + int keySizeInBits = 1024; 1.181 + int j = 8; 1.182 + int g = 0; 1.183 + int gMax = 0; 1.184 + int qSizeInBits = 0; 1.185 + SECStatus rv = 0; 1.186 + SECStatus passed = 0; 1.187 + PRBool output_ascii = PR_FALSE; 1.188 + PRBool output_binary = PR_FALSE; 1.189 + PRBool output_raw = PR_FALSE; 1.190 + PLOptState *optstate; 1.191 + PLOptStatus status; 1.192 + 1.193 + 1.194 + progName = strrchr(argv[0], '/'); 1.195 + if (!progName) 1.196 + progName = strrchr(argv[0], '\\'); 1.197 + progName = progName ? progName+1 : argv[0]; 1.198 + 1.199 + /* Parse command line arguments */ 1.200 + optstate = PL_CreateOptState(argc, argv, "?abg:l:n:o:r" ); 1.201 + while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) { 1.202 + switch (optstate->option) { 1.203 + 1.204 + case 'l': 1.205 + keySizeInBits = atoi(optstate->value); 1.206 + break; 1.207 + 1.208 + case 'n': 1.209 + qSizeInBits = atoi(optstate->value); 1.210 + break; 1.211 + 1.212 + case 'a': 1.213 + output_ascii = PR_TRUE; 1.214 + break; 1.215 + 1.216 + case 'b': 1.217 + output_binary = PR_TRUE; 1.218 + break; 1.219 + 1.220 + case 'r': 1.221 + output_raw = PR_TRUE; 1.222 + break; 1.223 + 1.224 + case 'o': 1.225 + if (outFileName) { 1.226 + PORT_Free(outFileName); 1.227 + } 1.228 + outFileName = PORT_Strdup(optstate->value); 1.229 + if (!outFileName) { 1.230 + rv = -1; 1.231 + } 1.232 + break; 1.233 + 1.234 + case 'g': 1.235 + g = atoi(optstate->value); 1.236 + break; 1.237 + 1.238 + 1.239 + default: 1.240 + case '?': 1.241 + Usage(); 1.242 + break; 1.243 + 1.244 + } 1.245 + } 1.246 + PL_DestroyOptState(optstate); 1.247 + 1.248 + if (status == PL_OPT_BAD) { 1.249 + Usage(); 1.250 + } 1.251 + 1.252 + /* exactly 1 of these options must be set. */ 1.253 + if (1 != ((output_ascii != PR_FALSE) + 1.254 + (output_binary != PR_FALSE) + 1.255 + (output_raw != PR_FALSE))) { 1.256 + Usage(); 1.257 + } 1.258 + 1.259 + gMax = 2*keySizeInBits; 1.260 + if (keySizeInBits < 1024) { 1.261 + j = PQG_PBITS_TO_INDEX(keySizeInBits); 1.262 + if (j < 0) { 1.263 + fprintf(stderr, "%s: Illegal prime length, \n" 1.264 + "\tacceptable values are between 512 and 1024,\n" 1.265 + "\tand divisible by 64, or 2048 or 3072\n", 1.266 + progName); 1.267 + return 2; 1.268 + } 1.269 + gMax =2048; 1.270 + if ((qSizeInBits != 0) && (qSizeInBits != 160)) { 1.271 + fprintf(stderr, "%s: Illegal subprime length, \n" 1.272 + "\tonly 160 is acceptible for primes <= 1024\n", 1.273 + progName); 1.274 + return 2; 1.275 + } 1.276 + /* this forces keysizes less than 1024 into the DSA1 generation 1.277 + * code. Whether 1024 uses DSA2 or not is triggered by qSizeInBits 1.278 + * being non-zero. All larger keysizes will use DSA2. 1.279 + */ 1.280 + qSizeInBits = 0; 1.281 + } 1.282 + if (g != 0 && (g < 160 || g >= gMax || g % 8 != 0)) { 1.283 + fprintf(stderr, "%s: Illegal g bits, \n" 1.284 + "\tacceptable values are between 160 and %d,\n" 1.285 + "\tand divisible by 8\n", progName, gMax); 1.286 + return 3; 1.287 + } 1.288 + 1.289 + if (!rv && outFileName) { 1.290 + outFile = fopen(outFileName, output_binary ? "wb" : "w"); 1.291 + if (!outFile) { 1.292 + fprintf(stderr, "%s: unable to open \"%s\" for writing\n", 1.293 + progName, outFileName); 1.294 + rv = -1; 1.295 + } 1.296 + } 1.297 + if (outFileName) { 1.298 + PORT_Free(outFileName); 1.299 + } 1.300 + if (rv != 0) { 1.301 + return 1; 1.302 + } 1.303 + 1.304 + if (outFile == NULL) { 1.305 + outFile = stdout; 1.306 + } 1.307 + 1.308 + 1.309 + NSS_NoDB_Init(NULL); 1.310 + 1.311 + if (keySizeInBits > 1024 || qSizeInBits != 0) { 1.312 + rv = PK11_PQG_ParamGenV2((unsigned)keySizeInBits, 1.313 + (unsigned) qSizeInBits, (unsigned)(g/8), &pqgParams, &pqgVerify); 1.314 + } else if (g) { 1.315 + rv = PK11_PQG_ParamGenSeedLen((unsigned)j, (unsigned)(g/8), 1.316 + &pqgParams, &pqgVerify); 1.317 + } else { 1.318 + rv = PK11_PQG_ParamGen((unsigned)j, &pqgParams, &pqgVerify); 1.319 + } 1.320 + /* below here, must go to loser */ 1.321 + 1.322 + if (rv != SECSuccess || pqgParams == NULL || pqgVerify == NULL) { 1.323 + SECU_PrintError(progName, "PQG parameter generation failed.\n"); 1.324 + goto loser; 1.325 + } 1.326 + fprintf(stderr, "%s: PQG parameter generation completed.\n", progName); 1.327 + 1.328 + rv = outputPQGParams(pqgParams, output_binary, output_raw, outFile); 1.329 + if (rv) { 1.330 + fprintf(stderr, "%s: failed to output PQG params.\n", progName); 1.331 + goto loser; 1.332 + } 1.333 + rv = outputPQGVerify(pqgVerify, output_binary, output_raw, outFile); 1.334 + if (rv) { 1.335 + fprintf(stderr, "%s: failed to output PQG Verify.\n", progName); 1.336 + goto loser; 1.337 + } 1.338 + 1.339 + rv = PK11_PQG_VerifyParams(pqgParams, pqgVerify, &passed); 1.340 + if (rv != SECSuccess) { 1.341 + fprintf(stderr, "%s: PQG parameter verification aborted.\n", progName); 1.342 + goto loser; 1.343 + } 1.344 + if (passed != SECSuccess) { 1.345 + fprintf(stderr, "%s: PQG parameters failed verification.\n", progName); 1.346 + goto loser; 1.347 + } 1.348 + fprintf(stderr, "%s: PQG parameters passed verification.\n", progName); 1.349 + 1.350 + PK11_PQG_DestroyParams(pqgParams); 1.351 + PK11_PQG_DestroyVerify(pqgVerify); 1.352 + return 0; 1.353 + 1.354 +loser: 1.355 + PK11_PQG_DestroyParams(pqgParams); 1.356 + PK11_PQG_DestroyVerify(pqgVerify); 1.357 + return 1; 1.358 +}