/*
 * Given two keys (presumably, in this test, one a raw key and 
 * one an equivalent ref key), calculate the key digest of both of them
 * and ensure they're the same. 
 */
static int compareKeyHashes(
	const CSSM_DATA *key1Hash,
	const char *key1Descr,
	const CSSM_DATA *key2Hash,
	const char *key2Descr,
	CSSM_BOOL verbose)
{
	if(appCompareCssmData(key1Hash, key2Hash)) {
		return 0;
	}
	printf("***Key Digest miscompare (%s,%s)***\n", key1Descr, key2Descr);
	if(!verbose) {
		printf("...%s hash:\n", key1Descr);
		dumpBuf(key1Hash->Data, key1Hash->Length);
		printf("...%s hash:\n", key2Descr);
		dumpBuf(key2Hash->Data, key2Hash->Length);
	}
	return 1;
}
/*
 * Given a KeyHashTest:
 *	-- cook up key pair, raw, specified formats
 *  -- NULL unwrap each raw to ref;
 *  -- obtain four key digests;
 *  -- ensure all digests match;
 */
static int doTest(
	CSSM_CSP_HANDLE		rawCspHand,		// generate keys here
	CSSM_CSP_HANDLE		refCspHand,		// null unwrap here
	KeyHashTest			*testParam,
	CSSM_BOOL			verbose,
	CSSM_BOOL			quiet)
{
	CSSM_RETURN crtn;
	CSSM_KEY pubKey;
	CSSM_KEY privKey;
	CSSM_KEY pubKeyRef;			
	CSSM_KEY privKeyRef;
	CSSM_DATA_PTR rawPubHash;
	CSSM_DATA_PTR rawPrivHash;
	CSSM_DATA_PTR refPubHash;
	CSSM_DATA_PTR refPrivHash;
	int rtn = 0;
	
	/* generate key pair, specified raw form */
	crtn = genKeyPair(rawCspHand,
		testParam->keyAlg,
		testParam->keySizeInBits,
		&pubKey,
		testParam->pubKeyForm,
		&privKey,
		testParam->privKeyForm,
		testParam->algParams);
	if(crtn) {
		return testError(quiet);
	}
	
	/* null unwrap both raw keys to ref form */
	crtn = cspRawKeyToRef(refCspHand, &pubKey, &pubKeyRef);
	if(crtn) {
		return testError(quiet);
	}
	crtn = cspRawKeyToRef(refCspHand, &privKey, &privKeyRef);
	if(crtn) {
		return testError(quiet);
	}
	
	/* calculate four key digests */
	crtn = cspKeyHash(rawCspHand, &pubKey, &rawPubHash);
	if(crtn) {
		return testError(quiet);
	}
	crtn = cspKeyHash(rawCspHand, &privKey, &rawPrivHash);
	if(crtn) {
		return testError(quiet);
	}
	crtn = cspKeyHash(refCspHand, &pubKeyRef, &refPubHash);
	if(crtn) {
		return testError(quiet);
	}
	crtn = cspKeyHash(refCspHand, &privKeyRef, &refPrivHash);
	if(crtn) {
		return testError(quiet);
	}

	if(verbose) {
		printf("...raw pub key hash:\n");
		dumpBuf(rawPubHash->Data, rawPubHash->Length);
		printf("...ref pub key hash:\n");
		dumpBuf(refPubHash->Data, refPubHash->Length);
		printf("...raw priv key hash:\n");
		dumpBuf(rawPrivHash->Data, rawPrivHash->Length);
		printf("...ref priv key hash:\n");
		dumpBuf(refPrivHash->Data, refPrivHash->Length);
	}

	/* compare */
	rtn += compareKeyHashes(rawPubHash, "Raw public",
		refPubHash, "Ref public", verbose);
	rtn += compareKeyHashes(rawPrivHash, "Raw private",
		refPrivHash, "Ref private", verbose);
	rtn += compareKeyHashes(refPubHash, "Ref public",
		refPrivHash, "Ref private", verbose);
	if(rtn) {
		rtn = testError(quiet);
	}
	cspFreeKey(rawCspHand, &pubKey);
	cspFreeKey(rawCspHand, &privKey);
	cspFreeKey(refCspHand, &pubKeyRef);
	cspFreeKey(refCspHand, &privKeyRef);
	appFreeCssmData(rawPubHash, CSSM_TRUE);
	appFreeCssmData(rawPrivHash, CSSM_TRUE);
	appFreeCssmData(refPubHash, CSSM_TRUE);
	appFreeCssmData(refPrivHash, CSSM_TRUE);
	return rtn;
}
Esempio n. 3
0
int main(int argc, char const *argv[])
{
    int fd; // ファイルディスクリプタ。サーバーとの接続
    uint32_t r_buf[DATA_NUM];

    const char *host = argv[1]; // 第一引数 ex) localhost
    const char *service = argv[2]; // 第二引数 ex) 1111

    /* サーバーとの接続 */
    fd = get_stream(host, service);
    if (fd == -1)
    {
        printf("get_stream error!!\n");
        exit(-1);
    }

    /* 一回目のデータ取得 */
    int len = -1;

    len = getData(fd, r_buf);
    printf("finish read\n");
    printf("len: %d\n", len);
    // dumpBuf(r_buf);

    /* ゲームに必要なデータの初期化 */
    struct Company companies[COMPANY_NUM];
    struct Tickets* tickets = InitTickets();
    uint32_t key, tmp_key, tmp_code;
    int money = 10000;
    int state = 0;

    key = InitCompanies(r_buf, companies);

    PrintCompanies(companies);

    /* メインルーチン。ターン数分実行される */
    for (int t = 0; t < TURNS; t++) {

        printf("\n\n------------------------%d th turn  money: %d------------------------\n", t, money);
        PrintCompanies(companies);

        /* strategy部分 */

        if (t == 0) {
            InitStrategy(fd, key, tickets, companies);
        } else if (t % 2 == 1) {
            SecondStrategy(fd, key, tickets, companies);
        } else if (t % 2 == 0) {
            ThirdBuyStrategy(fd, key, money, tickets, companies);
        } else {
            printf("bug!!!\n");
        }


        /* ターン内で投げたリクエストに対する反応を取得する */
        state = 0;
        // stateでゲームの進行状況を管理
        // state 0: 同一ターン内
        // state 1: 次のターンに進行
        // state 2: ゲーム終了。結果の出力へ
        while (state == 0) {

            getData(fd, r_buf);

            dumpBuf(r_buf);

            tmp_code = 0;
            tmp_key = 0;
            tmp_key = getKey(r_buf);
            if (key != tmp_key) {
                state = 1;
                printf("next turn\n");
            }
            tmp_code = getCode(r_buf);
            if (tmp_code == REQ_ACCEPT) {
                int accepted_idx = isContain(MakeTicketFromBuf(r_buf), tickets);
                printf("%d th ticket accepted!!\n", accepted_idx);
                if (accepted_idx != -1) {
                    money += ApplyTicket(accepted_idx, tickets, companies);
                }
            } else if (tmp_code == UNKOWN_CODE || tmp_code == INVALID_KEY ||
                       tmp_code == TOO_MUCH_REQ || tmp_code == ID_NOT_EXIST ||
                       tmp_code == TOO_MUCH_BUY || tmp_code == TOO_MUCH_SELL) {
                printf("code error type: %s\n", getCodeName(tmp_code));
                int rejected_idx = isContain(MakeTicketFromBuf(r_buf), tickets);
                printf("rejected ticket: %d\n", rejected_idx);
                if (rejected_idx != -1) {
                    DeleteTicket(rejected_idx, tickets);
                }
            } else if (tmp_code == TURN_START) {
                key = Parse(r_buf, companies);
                state = 1;
            } else if (tmp_code == GAME_END) {
                state = 2;
            } else {
                printf("something wrong in code\n");
            }
        }

        if (state == 2) {
            break;
        }

    }

    printf("\n\n\n");
    printf("##################################\n");
    printf("##############RESULT##############\n");
    printf("##################################\n\n");

    printf("your rank: %d\n", getID(0, r_buf));
    printf("your budget: %u\n", getValue(0, r_buf));

    close(fd);

    return 0;
}