int cmac_test(const EVP_CIPHER *cipher, FILE *out, FILE *in, int mode, int Klen_counts_keys, int known_keylen) { char *linebuf, *olinebuf, *p, *q; char *keyword, *value; unsigned char **Keys = NULL, *Msg = NULL, *Mac = NULL; unsigned char *Key = NULL; int Count, Klen, Mlen, Tlen; long Keylen, Msglen, Maclen; int ret = 0; int lnum = 0; olinebuf = OPENSSL_malloc(CMAC_TEST_MAXLINELEN); linebuf = OPENSSL_malloc(CMAC_TEST_MAXLINELEN); if (!linebuf || !olinebuf) goto error; Count = -1; Klen = -1; Mlen = -1; Tlen = -1; while (fgets(olinebuf, CMAC_TEST_MAXLINELEN, in)) { lnum++; strcpy(linebuf, olinebuf); keyword = linebuf; /* Skip leading space */ while (isspace((unsigned char)*keyword)) keyword++; /* Skip comments */ if (keyword[0] == '#') { if (fputs(olinebuf, out) < 0) goto error; continue; } /* Look for = sign */ p = strchr(linebuf, '='); /* If no = or starts with [ (for [L=20] line) just copy */ if (!p) { if (fputs(olinebuf, out) < 0) goto error; continue; } q = p - 1; /* Remove trailing space */ while (isspace((unsigned char)*q)) *q-- = 0; *p = 0; value = p + 1; /* Remove leading space from value */ while (isspace((unsigned char)*value)) value++; /* Remove trailing space from value */ p = value + strlen(value) - 1; while (*p == '\n' || isspace((unsigned char)*p)) *p-- = 0; if (!strcmp(keyword, "Count")) { if (Count != -1) goto parse_error; Count = atoi(value); if (Count < 0) goto parse_error; } else if (!strcmp(keyword, "Klen")) { if (Klen != -1) goto parse_error; Klen = atoi(value); if (Klen < 0) goto parse_error; if (Klen_counts_keys) { Keys = OPENSSL_malloc(sizeof(*Keys) * Klen); memset(Keys, '\0', sizeof(*Keys) * Klen); } else { Keys = OPENSSL_malloc(sizeof(*Keys)); memset(Keys, '\0', sizeof(*Keys)); } } else if (!strcmp(keyword, "Mlen")) { if (Mlen != -1) goto parse_error; Mlen = atoi(value); if (Mlen < 0) goto parse_error; } else if (!strcmp(keyword, "Tlen")) { if (Tlen != -1) goto parse_error; Tlen = atoi(value); if (Tlen < 0) goto parse_error; } else if (!strcmp(keyword, "Key") && !Klen_counts_keys) { if (Keys[0]) goto parse_error; Keys[0] = hex2bin_m(value, &Keylen); if (!Keys[0]) goto parse_error; } else if (!strncmp(keyword, "Key", 3) && Klen_counts_keys) { int keynum = atoi(keyword + 3); if (!keynum || keynum > Klen || Keys[keynum-1]) goto parse_error; Keys[keynum-1] = hex2bin_m(value, &Keylen); if (!Keys[keynum-1]) goto parse_error; } else if (!strcmp(keyword, "Msg")) { if (Msg) goto parse_error; Msg = hex2bin_m(value, &Msglen); if (!Msg) goto parse_error; } else if (!strcmp(keyword, "Mac")) { if (mode == 0) continue; if (Mac) goto parse_error; Mac = hex2bin_m(value, &Maclen); if (!Mac) goto parse_error; } else if (!strcmp(keyword, "Result")) { if (mode == 1) continue; goto parse_error; } else goto parse_error; fputs(olinebuf, out); if (Keys && Msg && (!mode || Mac) && (Tlen > 0) && (Klen > 0)) { if (Klen_counts_keys) { int x; Key = OPENSSL_malloc(Klen * known_keylen); for (x = 0; x < Klen; x++) { memcpy(Key + x * known_keylen, Keys[x], known_keylen); OPENSSL_free(Keys[x]); } Klen *= known_keylen; } else { Key = OPENSSL_malloc(Klen); memcpy(Key, Keys[0], Klen); OPENSSL_free(Keys[0]); } OPENSSL_free(Keys); switch(mode) { case 0: if (!print_cmac_gen(cipher, out, Key, Klen, Msg, Mlen, Tlen)) goto error; break; case 1: if (!print_cmac_ver(cipher, out, Key, Klen, Msg, Mlen, Mac, Maclen, Tlen)) goto error; break; } OPENSSL_free(Key); Key = NULL; OPENSSL_free(Msg); Msg = NULL; OPENSSL_free(Mac); Mac = NULL; Klen = -1; Mlen = -1; Tlen = -1; Count = -1; } } ret = 1; error: if (olinebuf) OPENSSL_free(olinebuf); if (linebuf) OPENSSL_free(linebuf); if (Key) OPENSSL_free(Key); if (Msg) OPENSSL_free(Msg); if (Mac) OPENSSL_free(Mac); return ret; parse_error: fprintf(stderr, "FATAL parse error processing line %d\n", lnum); goto error; }
int hmac_test(const EVP_MD *md, FILE *out, FILE *in) { char *linebuf, *olinebuf, *p, *q; char *keyword, *value; unsigned char *Key = NULL, *Msg = NULL; int Count, Klen, Tlen; long Keylen, Msglen; int ret = 0; int lnum = 0; olinebuf = OPENSSL_malloc(HMAC_TEST_MAXLINELEN); linebuf = OPENSSL_malloc(HMAC_TEST_MAXLINELEN); if (!linebuf || !olinebuf) goto error; Count = -1; Klen = -1; Tlen = -1; while (fgets(olinebuf, HMAC_TEST_MAXLINELEN, in)) { lnum++; strcpy(linebuf, olinebuf); keyword = linebuf; /* Skip leading space */ while (isspace((unsigned char)*keyword)) keyword++; /* Look for = sign */ p = strchr(linebuf, '='); /* If no = or starts with [ (for [L=20] line) just copy */ if (!p) { if (fputs(olinebuf, out) < 0) goto error; continue; } q = p - 1; /* Remove trailing space */ while (isspace((unsigned char)*q)) *q-- = 0; *p = 0; value = p + 1; /* Remove leading space from value */ while (isspace((unsigned char)*value)) value++; /* Remove trailing space from value */ p = value + strlen(value) - 1; while (*p == '\n' || isspace((unsigned char)*p)) *p-- = 0; if (!strcmp(keyword,"[L") && *p==']') { switch (atoi(value)) { case 20: md=EVP_sha1(); break; case 28: md=EVP_sha224(); break; case 32: md=EVP_sha256(); break; case 48: md=EVP_sha384(); break; case 64: md=EVP_sha512(); break; default: goto parse_error; } } else if (!strcmp(keyword, "Count")) { if (Count != -1) goto parse_error; Count = atoi(value); if (Count < 0) goto parse_error; } else if (!strcmp(keyword, "Klen")) { if (Klen != -1) goto parse_error; Klen = atoi(value); if (Klen < 0) goto parse_error; } else if (!strcmp(keyword, "Tlen")) { if (Tlen != -1) goto parse_error; Tlen = atoi(value); if (Tlen < 0) goto parse_error; } else if (!strcmp(keyword, "Msg")) { if (Msg) goto parse_error; Msg = hex2bin_m(value, &Msglen); if (!Msg) goto parse_error; } else if (!strcmp(keyword, "Key")) { if (Key) goto parse_error; Key = hex2bin_m(value, &Keylen); if (!Key) goto parse_error; } else if (!strcmp(keyword, "Mac")) continue; else goto parse_error; fputs(olinebuf, out); if (Key && Msg && (Tlen > 0) && (Klen > 0)) { if (!print_hmac(md, out, Key, Klen, Msg, Msglen, Tlen)) goto error; OPENSSL_free(Key); Key = NULL; OPENSSL_free(Msg); Msg = NULL; Klen = -1; Tlen = -1; Count = -1; } } ret = 1; error: if (olinebuf) OPENSSL_free(olinebuf); if (linebuf) OPENSSL_free(linebuf); if (Key) OPENSSL_free(Key); if (Msg) OPENSSL_free(Msg); return ret; parse_error: fprintf(stderr, "FATAL parse error processing line %d\n", lnum); goto error; }
int main(int argc,char **argv) { FILE *in, *out; DRBG_CTX *dctx = NULL; TEST_ENT t; int r, nid = 0; int pr = 0; char buf[2048], lbuf[2048]; unsigned char randout[2048]; char *keyword = NULL, *value = NULL; unsigned char *ent = NULL, *nonce = NULL, *pers = NULL, *adin = NULL; long entlen, noncelen, perslen, adinlen; int df = 0; int randoutlen = 0; int gen = 0; fips_algtest_init(); if (argc == 3) { in = fopen(argv[1], "r"); if (!in) { fprintf(stderr, "Error opening input file\n"); exit(1); } out = fopen(argv[2], "w"); if (!out) { fprintf(stderr, "Error opening output file\n"); exit(1); } } else if (argc == 1) { in = stdin; out = stdout; } else { fprintf(stderr,"%s (infile outfile)\n",argv[0]); exit(1); } while (fgets(buf, sizeof(buf), in) != NULL) { fputs(buf, out); if (strlen(buf) > 4 && !strncmp(buf, "[SHA-", 5)) { nid = parse_md(buf); if (nid == NID_undef) exit(1); } if (strlen(buf) > 12 && !strncmp(buf, "[AES-", 5)) { nid = parse_aes(buf, &df); if (nid == NID_undef) exit(1); } if (!parse_line(&keyword, &value, lbuf, buf)) continue; if (!strcmp(keyword, "[PredictionResistance")) { if (!strcmp(value, "True]")) pr = 1; else if (!strcmp(value, "False]")) pr = 0; else exit(1); } if (!strcmp(keyword, "EntropyInput")) { ent = hex2bin_m(value, &entlen); t.ent = ent; t.entlen = entlen; } if (!strcmp(keyword, "Nonce")) { nonce = hex2bin_m(value, &noncelen); t.nonce = nonce; t.noncelen = noncelen; } if (!strcmp(keyword, "PersonalizationString")) { pers = hex2bin_m(value, &perslen); dctx = FIPS_drbg_new(nid, df | DRBG_FLAG_TEST); if (!dctx) exit (1); FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0, test_nonce, 0); FIPS_drbg_set_app_data(dctx, &t); randoutlen = (int)FIPS_drbg_get_blocklength(dctx); r = FIPS_drbg_instantiate(dctx, pers, perslen); if (!r) { fprintf(stderr, "Error instantiating DRBG\n"); exit(1); } OPENSSL_free(pers); OPENSSL_free(ent); OPENSSL_free(nonce); ent = nonce = pers = NULL; gen = 0; } if (!strcmp(keyword, "AdditionalInput")) { adin = hex2bin_m(value, &adinlen); if (pr) continue; r = FIPS_drbg_generate(dctx, randout, randoutlen, 0, 0, adin, adinlen); if (!r) { fprintf(stderr, "Error generating DRBG bits\n"); exit(1); } if (!r) exit(1); OPENSSL_free(adin); adin = NULL; gen++; } if (pr) { if (!strcmp(keyword, "EntropyInputPR")) { ent = hex2bin_m(value, &entlen); t.ent = ent; t.entlen = entlen; r = FIPS_drbg_generate(dctx, randout, randoutlen, 0, 1, adin, adinlen); if (!r) { fprintf(stderr, "Error generating DRBG bits\n"); exit(1); } OPENSSL_free(adin); OPENSSL_free(ent); adin = ent = NULL; gen++; } } if (!strcmp(keyword, "EntropyInputReseed")) { ent = hex2bin_m(value, &entlen); t.ent = ent; t.entlen = entlen; } if (!strcmp(keyword, "AdditionalInputReseed")) { adin = hex2bin_m(value, &adinlen); FIPS_drbg_reseed(dctx, adin, adinlen); OPENSSL_free(ent); OPENSSL_free(adin); ent = adin = NULL; } if (gen == 2) { OutputValue("ReturnedBits", randout, randoutlen, out, 0); FIPS_drbg_free(dctx); dctx = NULL; gen = 0; } } return 0; }
int main(int argc,char **argv) #endif { FILE *in, *out; DRBG_CTX *dctx = NULL; TEST_ENT t; int r, nid = 0; int pr = 0; char buf[2048], lbuf[2048]; unsigned char randout[2048]; char *keyword = NULL, *value = NULL; unsigned char *ent = NULL, *nonce = NULL, *pers = NULL, *adin = NULL; long entlen, noncelen, perslen, adinlen; int df = 0; enum dtype { DRBG_NONE, DRBG_CTR, DRBG_HASH, DRBG_HMAC, DRBG_DUAL_EC } drbg_type = DRBG_NONE; int randoutlen = 0; int gen = 0; fips_algtest_init(); if (argc == 3) { in = fopen(argv[1], "r"); if (!in) { fprintf(stderr, "Error opening input file\n"); exit(1); } out = fopen(argv[2], "w"); if (!out) { fprintf(stderr, "Error opening output file\n"); exit(1); } } else if (argc == 1) { in = stdin; out = stdout; } else { fprintf(stderr,"%s (infile outfile)\n",argv[0]); exit(1); } while (fgets(buf, sizeof(buf), in) != NULL) { fputs(buf, out); if (drbg_type == DRBG_NONE) { if (strstr(buf, "CTR_DRBG")) drbg_type = DRBG_CTR; else if (strstr(buf, "Hash_DRBG")) drbg_type = DRBG_HASH; else if (strstr(buf, "HMAC_DRBG")) drbg_type = DRBG_HMAC; else if (strstr(buf, "Dual_EC_DRBG")) drbg_type = DRBG_DUAL_EC; else continue; } if (strlen(buf) > 4 && !strncmp(buf, "[SHA-", 5)) { nid = dparse_md(buf); if (nid == NID_undef) exit(1); if (drbg_type == DRBG_HMAC) { switch (nid) { case NID_sha1: nid = NID_hmacWithSHA1; break; case NID_sha224: nid = NID_hmacWithSHA224; break; case NID_sha256: nid = NID_hmacWithSHA256; break; case NID_sha384: nid = NID_hmacWithSHA384; break; case NID_sha512: nid = NID_hmacWithSHA512; break; default: exit(1); } } } if (strlen(buf) > 12 && !strncmp(buf, "[AES-", 5)) { nid = parse_aes(buf, &df); if (nid == NID_undef) exit(1); } if (strlen(buf) > 12 && !strncmp(buf, "[P-", 3)) { nid = parse_ec(buf); if (nid == NID_undef) exit(1); } if (!parse_line(&keyword, &value, lbuf, buf)) continue; if (!strcmp(keyword, "[PredictionResistance")) { if (!strcmp(value, "True]")) pr = 1; else if (!strcmp(value, "False]")) pr = 0; else exit(1); } if (!strcmp(keyword, "EntropyInput")) { ent = hex2bin_m(value, &entlen); t.ent = ent; t.entlen = entlen; } if (!strcmp(keyword, "Nonce")) { nonce = hex2bin_m(value, &noncelen); t.nonce = nonce; t.noncelen = noncelen; } if (!strcmp(keyword, "PersonalizationString")) { pers = hex2bin_m(value, &perslen); if (nid == 0) { fprintf(stderr, "DRBG type not recognised!\n"); exit (1); } dctx = FIPS_drbg_new(nid, df | DRBG_FLAG_TEST); if (!dctx) exit (1); FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0, test_nonce, 0); FIPS_drbg_set_app_data(dctx, &t); randoutlen = (int)FIPS_drbg_get_blocklength(dctx); r = FIPS_drbg_instantiate(dctx, pers, perslen); if (!r) { fprintf(stderr, "Error instantiating DRBG\n"); exit(1); } OPENSSL_free(pers); OPENSSL_free(ent); OPENSSL_free(nonce); ent = nonce = pers = NULL; gen = 0; } if (!strcmp(keyword, "AdditionalInput")) { adin = hex2bin_m(value, &adinlen); if (pr) continue; r = FIPS_drbg_generate(dctx, randout, randoutlen, 0, adin, adinlen); if (!r) { fprintf(stderr, "Error generating DRBG bits\n"); exit(1); } if (!r) exit(1); OPENSSL_free(adin); adin = NULL; gen++; } if (pr) { if (!strcmp(keyword, "EntropyInputPR")) { ent = hex2bin_m(value, &entlen); t.ent = ent; t.entlen = entlen; r = FIPS_drbg_generate(dctx, randout, randoutlen, 1, adin, adinlen); if (!r) { fprintf(stderr, "Error generating DRBG bits\n"); exit(1); } OPENSSL_free(adin); OPENSSL_free(ent); adin = ent = NULL; gen++; } } if (!strcmp(keyword, "EntropyInputReseed")) { ent = hex2bin_m(value, &entlen); t.ent = ent; t.entlen = entlen; } if (!strcmp(keyword, "AdditionalInputReseed")) { adin = hex2bin_m(value, &adinlen); FIPS_drbg_reseed(dctx, adin, adinlen); OPENSSL_free(ent); OPENSSL_free(adin); ent = adin = NULL; } if (gen == 2) { OutputValue("ReturnedBits", randout, randoutlen, out, 0); FIPS_drbg_free(dctx); dctx = NULL; gen = 0; } } return 0; }
static void vst(FILE *in, FILE *out) { unsigned char *key = NULL; unsigned char *v = NULL; unsigned char *dt = NULL; unsigned char ret[16]; char buf[1024]; char lbuf[1024]; uint8_t seed[1024]; char *keyword, *value; long i, keylen; struct session_op session = { .rng = CRYPTO_ANSI_CPRNG, .key = seed }; struct crypt_op op = { .dst = ret, .len = 16 }; keylen = 0; while(fgets(buf,sizeof buf,in) != NULL) { fputs(buf,out); if(!strncmp(buf,"[AES 128-Key]", 13)) keylen = 16; else if(!strncmp(buf,"[AES 192-Key]", 13)) keylen = 24; else if(!strncmp(buf,"[AES 256-Key]", 13)) keylen = 32; if (!parse_line(&keyword, &value, lbuf, buf)) continue; if(!strcmp(keyword,"Key")) { key=hex2bin_m(value,&i); if (i != keylen) { fprintf(stderr, "Invalid key length, expecting %ld\n", keylen); return; } } else if(!strcmp(keyword,"DT")) { dt=hex2bin_m(value,&i); if (i != 16) { fprintf(stderr, "Invalid DT length\n"); return; } } else if(!strcmp(keyword,"V")) { v=hex2bin_m(value,&i); if (i != 16) { fprintf(stderr, "Invalid V length\n"); return; } if (!key || !dt) { fprintf(stderr, "Missing key or DT\n"); return; } session.keylen = build_seed(seed, v, key, dt, keylen); if (session.keylen > sizeof(seed)/sizeof(*seed)) { fprintf(stderr, "Seed buffer overrun\n"); return; } if (!cryptodev_op(session,op)) { fprintf(stderr, "Error getting PRNG value\n"); return; } OutputValue("R", ret, 16, out, 0); OPENSSL_free(key); key = NULL; OPENSSL_free(dt); dt = NULL; OPENSSL_free(v); v = NULL; } } } static void mct(FILE *in, FILE *out) { unsigned char *key = NULL; unsigned char *v = NULL; unsigned char *dt = NULL; unsigned char ret[16]; char buf[1024]; char lbuf[1024]; uint8_t seed[1024]; char *keyword, *value; long i, keylen; int cryptofd; struct session_op session = { .rng = CRYPTO_ANSI_CPRNG, .key = seed }; struct crypt_op op = { .dst = ret, .len = 16 }; keylen = 0; while(fgets(buf,sizeof buf,in) != NULL) { fputs(buf,out); if(!strncmp(buf,"[AES 128-Key]", 13)) keylen = 16; else if(!strncmp(buf,"[AES 192-Key]", 13)) keylen = 24; else if(!strncmp(buf,"[AES 256-Key]", 13)) keylen = 32; if (!parse_line(&keyword, &value, lbuf, buf)) continue; if(!strcmp(keyword,"Key")) { key=hex2bin_m(value,&i); if (i != keylen) { fprintf(stderr, "Invalid key length, expecting %ld\n", keylen); return; } } else if(!strcmp(keyword,"DT")) { dt=hex2bin_m(value,&i); if (i != 16) { fprintf(stderr, "Invalid DT length\n"); return; } } else if(!strcmp(keyword,"V")) { v=hex2bin_m(value,&i); if (i != 16) { fprintf(stderr, "Invalid V length\n"); return; } if (!key || !dt) { fprintf(stderr, "Missing key or DT\n"); return; } session.keylen = build_seed(seed, v, key, dt, keylen); if (session.keylen > sizeof(seed)/sizeof(*seed)) { fprintf(stderr, "Seed buffer overrun\n"); return; } cryptodev_start_session(&session, &cryptofd); for (i = 0; i < 10000; i++) { if (cryptodev_session_op(session, cryptofd, op) <= 0) { fprintf(stderr, "Error getting PRNG value\n"); return; } } cryptodev_end_session(session, cryptofd); OutputValue("R", ret, 16, out, 0); OPENSSL_free(key); key = NULL; OPENSSL_free(dt); dt = NULL; OPENSSL_free(v); v = NULL; } } } #ifdef FIPS_ALGVS int fips_rngvs_main(int argc, char **argv) #else int main(int argc, char **argv) #endif { FILE *in, *out; if (argc == 4) { in = fopen(argv[2], "r"); if (!in) { fprintf(stderr, "Error opening input file\n"); exit(1); } out = fopen(argv[3], "w"); if (!out) { fprintf(stderr, "Error opening output file\n"); exit(1); } } else if (argc == 2) { in = stdin; out = stdout; } else { fprintf(stderr,"%s [mct|vst]\n",argv[0]); exit(1); } if(!strcmp(argv[1],"mct")) mct(in, out); else if(!strcmp(argv[1],"vst")) vst(in, out); else { fprintf(stderr,"Don't know how to %s.\n",argv[1]); exit(1); } if (argc == 4) { fclose(in); fclose(out); } return 0; }
static int SigVer(FILE *in, FILE *out) { char buf[2048], lbuf[2048]; char *keyword, *value; unsigned char *msg = NULL; int curve_nid = NID_undef; long mlen; BIGNUM *Qx = NULL, *Qy = NULL; EC_KEY *key = NULL; ECDSA_SIG sg, *sig = &sg; const EVP_MD *digest = NULL; sig->r = NULL; sig->s = NULL; while (fgets(buf, sizeof buf, in) != NULL) { fputs(buf, out); if (*buf == '[') { curve_nid = elookup_curve(buf, lbuf, &digest); if (curve_nid == NID_undef) return 0; } if (!parse_line(&keyword, &value, lbuf, buf)) continue; if (!strcmp(keyword, "Msg")) { msg = hex2bin_m(value, &mlen); if (!msg) { fprintf(stderr, "Invalid Message\n"); return 0; } } if (!strcmp(keyword, "Qx")) { if (!do_hex2bn(&Qx, value)) { fprintf(stderr, "Invalid Qx value\n"); return 0; } } if (!strcmp(keyword, "Qy")) { if (!do_hex2bn(&Qy, value)) { fprintf(stderr, "Invalid Qy value\n"); return 0; } } if (!strcmp(keyword, "R")) { if (!do_hex2bn(&sig->r, value)) { fprintf(stderr, "Invalid R value\n"); return 0; } } if (!strcmp(keyword, "S")) { int rv; if (!do_hex2bn(&sig->s, value)) { fprintf(stderr, "Invalid S value\n"); return 0; } key = EC_KEY_new_by_curve_name(curve_nid); rv = EC_KEY_set_public_key_affine_coordinates(key, Qx, Qy); if (rv != 1) { fprintf(stderr, "Error setting public key\n"); return 0; } no_err = 1; rv = FIPS_ecdsa_verify(key, msg, mlen, digest, sig); EC_KEY_free(key); if (msg) OPENSSL_free(msg); no_err = 0; fprintf(out, "Result = %s" RESP_EOL, rv ? "P" : "F"); } } if (sig->r) BN_free(sig->r); if (sig->s) BN_free(sig->s); if (Qx) BN_free(Qx); if (Qy) BN_free(Qy); return 1; }
static int SigGen(FILE *in, FILE *out) { char buf[2048], lbuf[2048]; char *keyword, *value; unsigned char *msg; int curve_nid = NID_undef; long mlen; BIGNUM *Qx = NULL, *Qy = NULL; EC_KEY *key = NULL; ECDSA_SIG *sig = NULL; const EVP_MD *digest = NULL; Qx = BN_new(); Qy = BN_new(); while (fgets(buf, sizeof buf, in) != NULL) { fputs(buf, out); if (*buf == '[') { curve_nid = elookup_curve(buf, lbuf, &digest); if (curve_nid == NID_undef) return 0; } if (!parse_line(&keyword, &value, lbuf, buf)) continue; if (!strcmp(keyword, "Msg")) { msg = hex2bin_m(value, &mlen); if (!msg) { fprintf(stderr, "Invalid Message\n"); return 0; } key = EC_KEY_new_by_curve_name(curve_nid); if (!EC_KEY_generate_key(key)) { fprintf(stderr, "Error generating key\n"); return 0; } if (!ec_get_pubkey(key, Qx, Qy)) { fprintf(stderr, "Error getting public key\n"); return 0; } sig = FIPS_ecdsa_sign(key, msg, mlen, digest); if (!sig) { fprintf(stderr, "Error signing message\n"); return 0; } do_bn_print_name(out, "Qx", Qx); do_bn_print_name(out, "Qy", Qy); do_bn_print_name(out, "R", sig->r); do_bn_print_name(out, "S", sig->s); EC_KEY_free(key); OPENSSL_free(msg); FIPS_ecdsa_sig_free(sig); } } BN_free(Qx); BN_free(Qy); return 1; }
static void vst() { unsigned char *key = NULL; unsigned char *v = NULL; unsigned char *dt = NULL; unsigned char ret[16]; char buf[1024]; char lbuf[1024]; char *keyword, *value; long i, keylen; keylen = 0; while (fgets(buf, sizeof buf, stdin) != NULL) { fputs(buf, stdout); if (!strncmp(buf, "[AES 128-Key]", 13)) keylen = 16; else if (!strncmp(buf, "[AES 192-Key]", 13)) keylen = 24; else if (!strncmp(buf, "[AES 256-Key]", 13)) keylen = 32; if (!parse_line(&keyword, &value, lbuf, buf)) continue; if (!strcmp(keyword, "Key")) { key = hex2bin_m(value, &i); if (i != keylen) { fprintf(stderr, "Invalid key length, expecting %ld\n", keylen); return; } } else if (!strcmp(keyword, "DT")) { dt = hex2bin_m(value, &i); if (i != 16) { fprintf(stderr, "Invalid DT length\n"); return; } } else if (!strcmp(keyword, "V")) { v = hex2bin_m(value, &i); if (i != 16) { fprintf(stderr, "Invalid V length\n"); return; } if (!key || !dt) { fprintf(stderr, "Missing key or DT\n"); return; } FIPS_rand_set_key(key, keylen); FIPS_rand_seed(v, 16); FIPS_rand_set_dt(dt); if (FIPS_rand_bytes(ret, 16) <= 0) { fprintf(stderr, "Error getting PRNG value\n"); return; } pv("R", ret, 16); OPENSSL_free(key); key = NULL; OPENSSL_free(dt); dt = NULL; OPENSSL_free(v); v = NULL; } } }