12424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan/* Copyright (c) 2017, Google Inc. 22424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan * 32424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan * Permission to use, copy, modify, and/or distribute this software for any 42424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan * purpose with or without fee is hereby granted, provided that the above 52424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan * copyright notice and this permission notice appear in all copies. 62424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan * 72424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 82424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 92424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 102424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 112424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 122424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 132424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ 142424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan 152424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan// cavp_ecdsa2_pkv_test processes a NIST CAVP ECDSA2 PKV test vector request file 168ff035535f7cf2903f02bbe94d2fa10b7ab855f1Robert Sloan// and emits the corresponding response. 172424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan 182424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan#include <vector> 192424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan 202424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan#include <openssl/bn.h> 212424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan#include <openssl/crypto.h> 222424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan#include <openssl/ec_key.h> 232424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan#include <openssl/err.h> 242424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan#include <openssl/nid.h> 252424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan 262424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan#include "../crypto/test/file_test.h" 272424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan#include "cavp_test_util.h" 282424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan 292424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan 302424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloanstatic bool TestECDSA2PKV(FileTest *t, void *arg) { 312424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan int nid = GetECGroupNIDFromInstruction(t); 322424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan if (nid == NID_undef) { 332424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan return false; 342424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan } 352424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(nid)); 362424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan bssl::UniquePtr<BIGNUM> qx = GetBIGNUM(t, "Qx"); 372424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan bssl::UniquePtr<BIGNUM> qy = GetBIGNUM(t, "Qy"); 382424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan if (!key || !qx || !qy) { 392424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan return false; 402424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan } 412424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan 422424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan if (EC_KEY_set_public_key_affine_coordinates(key.get(), qx.get(), qy.get())) { 432424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan printf("%sResult = P\r\n\r\n", t->CurrentTestToString().c_str()); 442424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan } else { 452424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan char buf[256]; 462424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan ERR_error_string_n(ERR_get_error(), buf, sizeof(buf)); 472424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan printf("%sResult = F (%s)\r\n\r\n", t->CurrentTestToString().c_str(), buf); 482424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan } 492424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan ERR_clear_error(); 502424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan return true; 512424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan} 522424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan 538ff035535f7cf2903f02bbe94d2fa10b7ab855f1Robert Sloanint cavp_ecdsa2_pkv_test_main(int argc, char **argv) { 542424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan if (argc != 2) { 552424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan fprintf(stderr, "usage: %s <test file>\n", 562424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan argv[0]); 572424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan return 1; 582424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan } 592424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan 608ff035535f7cf2903f02bbe94d2fa10b7ab855f1Robert Sloan FileTest::Options opts; 618ff035535f7cf2903f02bbe94d2fa10b7ab855f1Robert Sloan opts.path = argv[1]; 628ff035535f7cf2903f02bbe94d2fa10b7ab855f1Robert Sloan opts.callback = TestECDSA2PKV; 638ff035535f7cf2903f02bbe94d2fa10b7ab855f1Robert Sloan opts.silent = true; 648ff035535f7cf2903f02bbe94d2fa10b7ab855f1Robert Sloan opts.comment_callback = EchoComment; 658ff035535f7cf2903f02bbe94d2fa10b7ab855f1Robert Sloan return FileTestMain(opts); 662424d84dd6dbdc0d32a4c80e6810d168f722ce0bRobert Sloan} 67