Exemplo n.º 1
0
/*
 PSEC-KEM key encapsulation mechanism

 Return: TRUE if succeed; otherwise FALSE
*/
u8 PSEC_KEM_KEM(
   PSEC_KEM_PUB_KEY    *publicKey,     /* PSEC_KEM public key */
   PSEC_KEM_KEY_ENCAPSULATION *keyEncapsulation,    /* returned keyEncapsulation */
   PSEC_KEM_KEY_MATERIAL *keyMaterial,    /* returned key material */
   EC_PARAM         *E,
   PSEC_KEM_EC_ENCODING_FORMAT format
)
{
mpz_t s, u, res, v;
u8 *s_raw, *u_raw, *t_raw, *mgf_arg_1, *mgf_arg_2, *res_raw, *v_raw, *PEH_raw; 
u8 *EG_raw;
s32 i;
u32 hoLen, uoLen, EGoLen, PEHoLen, qoLen;
EC_POINT h_tilde, g_tilde;

	mpz_init(s);
	mpz_init(u);
	mpz_init(res);
	mpz_init(v);

	hoLen = (publicKey->hLen) >> 3;
	uoLen = (u32)ceil(E->pLen/8.0) + 16;
	keyMaterial->KoLen = (publicKey->outputKeyLen) >> 3;
	qoLen = (u32)ceil(E->qLen/8.0);
	if (format == COMPRESSED)  {
		EGoLen = 1 + qoLen;
		PEHoLen = 1 + qoLen;
	}  else  {
		EGoLen = 1 + 2*qoLen;
		PEHoLen = 1 + 2*qoLen;
	}
	if ((publicKey->pk).inf_id == EC_O) 
		PEHoLen = 1;
	if ((E->P).inf_id == EC_O)
		EGoLen = 1;
#ifdef DEBUG
printf("hoLen = %u\n", hoLen);
printf("uoLen = %u\n", uoLen);
printf("keyMaterial->KoLen = %u\n", keyMaterial->KoLen);
printf("EGoLen = %u\n", EGoLen);
printf("PEHoLen = %u\n", PEHoLen);
#endif
	EC_initPoint ( &h_tilde );
	EC_initPoint ( &g_tilde);

	s_raw = (BYTE *) malloc (hoLen);
	u_raw = (BYTE *) malloc (uoLen);
	t_raw = (BYTE *) malloc (uoLen + keyMaterial->KoLen);
	mgf_arg_1 = (u8 *) malloc (4 + hoLen);
	mgf_arg_2 = (u8 *) malloc (4 + EGoLen + PEHoLen);
	res_raw = (u8 *) malloc (hoLen);
	v_raw = (u8 *) malloc (hoLen);
	PEH_raw = (u8 *) malloc (PEHoLen);
	EG_raw = (u8 *) malloc (EGoLen);

	/* generate s */
	GenerateBytes(s_raw, hoLen, global_prng);
	BYTE2WORD (s, s_raw, hoLen);
#ifdef DEBUG
printf("s = 0x%s\n", mpz_get_str(NULL, 16, s));
printf("\n"); fflush(stdout);
#endif

	/* compute t = MGF1(0 || s) */
	U32TO8_BIG(mgf_arg_1, 0L);
	memcpy(mgf_arg_1+4, s_raw, hoLen);
	MGF1( t_raw, 8 * (uoLen + keyMaterial->KoLen), mgf_arg_1, 4 + hoLen);
#ifdef DEBUG
printf("t = ");
printAsHex(t_raw, uoLen+keyMaterial->KoLen);
printf("\n"); fflush(stdout);
#endif

	/* parse t as t = u || K */
	memcpy( u_raw, t_raw, uoLen );
	BYTE2WORD( u, u_raw, uoLen );
	memcpy( keyMaterial->K_raw, t_raw+uoLen, keyMaterial->KoLen );
#ifdef DEBUG
printf("u = 0x%s\n", mpz_get_str(NULL, 16, u));
printf("keyMaterial = ");
printAsHex(keyMaterial->K_raw, keyMaterial->KoLen);
printf("\n"); fflush(stdout);
#endif

	/* compute h_tilde = u * publicKey->pk */
	EC_Mult( &h_tilde, u, &(publicKey->pk), E );
	ec2os (PEH_raw, &h_tilde, E, format);
#ifdef DEBUG
printf("h_tilde.x = 0x%s\n", mpz_get_str(NULL, 16, h_tilde.x));
printf("h_tilde.y = 0x%s\n", mpz_get_str(NULL, 16, h_tilde.y));
printf("PEH = ");
printAsHex(PEH_raw, PEHoLen);
printf("\n"); fflush(stdout);
#endif

	/* compute g_tilde = u * E->P */
	EC_Mult( &g_tilde, u, &(E->P), E );
#ifdef DEBUG
printf("g_tilde.x = 0x%s\n", mpz_get_str(NULL, 16, g_tilde.x));
printf("g_tilde.y = 0x%s\n", mpz_get_str(NULL, 16, g_tilde.y));
printf("g_tilde.inf_id = %u\n", g_tilde.inf_id);
#endif

    /* convert g_tilde's EC point to an octet string according to P1363 E.2.3.2 */
	ec2os( EG_raw, &g_tilde, E, format);
#ifdef DEBUG
printf("EG = ");
printAsHex(EG_raw, EGoLen);
printf("\n"); fflush(stdout);
#endif

	/* compute res = MGF1( 1 || EG || PEH ) */
	U32TO8_BIG ( mgf_arg_2, 1L );
	memcpy( mgf_arg_2 + 4, EG_raw, EGoLen );
	memcpy( mgf_arg_2 + 4 + EGoLen, PEH_raw, PEHoLen );
	MGF1( res_raw, publicKey->hLen, mgf_arg_2, 4 + EGoLen + PEHoLen);
	BYTE2WORD( res, res_raw, hoLen);
#ifdef DEBUG
printf("res = 0x%s\n", mpz_get_str(NULL, 16, res));
printf("\n"); fflush(stdout);
#endif

	/* compute v = s \xor res */
	mpz_xor(v, s, res);
	WORD2BYTE(v_raw, v, hoLen);
#ifdef DEBUG
printf("v = 0x%s\n", mpz_get_str(NULL, 16, v));
printf("\n"); fflush(stdout);
#endif

	/* output C0 = EG || v */
	memcpy( keyEncapsulation->C0, EG_raw, EGoLen);
	memcpy( keyEncapsulation->C0 + EGoLen, v_raw, hoLen);
#ifdef DEBUG
printf("C0 = ");
printAsHex(keyEncapsulation->C0, keyEncapsulation->C0oLen);
printf("\n"); fflush(stdout);
#endif
	
	/* clean up */
	mpz_clear(s);
	mpz_clear(u);
	mpz_clear(res);
	mpz_clear(v);

	memset(s_raw, 0, hoLen);
	memset(u_raw, 0, uoLen);
	memset(t_raw, 0, uoLen + keyMaterial->KoLen);
	memset(mgf_arg_1, 0, 4+hoLen);
	memset(mgf_arg_2, 0, 4 + EGoLen + PEHoLen);
	memset(res_raw, 0, hoLen);
	memset(v_raw, 0, hoLen);
	memset(PEH_raw, 0, PEHoLen);
	memset(EG_raw, 0, EGoLen);

	free(s_raw);
	free(u_raw);
	free(t_raw);
	free(mgf_arg_1);
	free(mgf_arg_2);
	free(res_raw);
	free(v_raw);
	free(PEH_raw);
	free(EG_raw);

	EC_clearPoint ( &h_tilde );
	EC_clearPoint ( &g_tilde );

	return TRUE;
}
Exemplo n.º 2
0
// THE MAIN
char* main(int argc, char *argv[])
// param options:
// decrypt some aes encrypted string:			enc_string key
// set a new flag d with id=a and token='b-.-c':	a 'b-.-c' d
// add a new fzn with value:				fzn value
{
    if (strcmp(argv[1], "-h") == 0) {	// decryption not really needed but management want me to leave it...
        char* encrypted_flag = argv[2];
        char* aeskey = argv[3];

        char tmpstr[8096];
        sprintf(tmpstr, "echo %s | openssl enc -d -aes-256-cbc -a -k %s", encrypted_flag, aeskey);

        FILE *fp;
        int status;
        char path[4096];
        char content[4096];

        fp = popen(tmpstr, "r");

        if (fp == NULL)
            /* Handle error */;

        fgets(path, 4096, fp);
        sprintf(content, "%s", path);
        if (strlen(content) < 5) {
            printf("gcry_cipher_decrypt failed:");
        } else {
            printf("%s\n", content);
        }
        // dec: echo U2FsdGVkX18eaVlEUPTR47GFaEoh3u9DMHgqvtZS1Ko= | openssl enc -d -aes-256-cbc -a -k mykey
    }
    else if (strcmp(argv[1], "-h") == 0) {	// decryption not really needed but management want me to leave it...
        char* encrypted_flag = argv[2];
        char* aes_key = argv[3];
        //printf("encrypted_flag: %s (%i)\n", encrypted_flag, (int)(strlen(encrypted_flag)));
        char* ascii_flag = hexToString(encrypted_flag);
        //printf("ascii_flag: %s (%i)\n", ascii_flag, (int)(strlen(ascii_flag)));
        decryptString2(ascii_flag, aes_key);
        //printf("passing ascii_flag: %s (%i)\n", ascii_flag, (int)(strlen(ascii_flag)));
        remPad(ascii_flag, strlen(ascii_flag));
        //printf("%s\n", ascii_flag);
        //printf("ascii_flag: %s (%i)\n", ascii_flag, (int)(strlen(ascii_flag)));
        printf("%s\n", ascii_flag);
        return ascii_flag;	// is decryped
    } else if (argc == 4)
    {
        char* flag_id = argv[1];        // FahrzeugnummerBeginnWortBeginn
        char* password = argv[2];       // ( komplette Fahrzeugnummer-.-komplettes Wort )
        char* content_arg = argv[3];        // the flag itself
        char content[4096];

        strcpy(content, content_arg);
        char delimiter[] = "-.-";
        char *ptr;
        ptr = strtok(password, delimiter);
        char* fzn = ptr;

        ptr = strtok(NULL, delimiter);
        char* bayWord = ptr;

        char* aeskey = randstring(16);
        printf("%s\n", aeskey);
        char hex_content[4096];

        char tmpstr[8096];
        sprintf(tmpstr, "echo %s | openssl enc -e -aes-256-cbc -a -k %s", argv[3], aeskey);

        FILE *fp;
        int status;
        char path[4096];

        fp = popen(tmpstr, "r");

        if (fp == NULL)
            /* Handle error */;

        fgets(path, 4096, fp);
        sprintf(hex_content, "%s", path);
        // dec: echo U2FsdGVkX18eaVlEUPTR47GFaEoh3u9DMHgqvtZS1Ko= | openssl enc -d -aes-256-cbc -a -k mykey
//		printf("hex: ||%s||", hex_content);
//		printf("aes: ||%s||", aeskey);
//		printf("-----------");

        printf("fzn: %s\n", fzn);
        printf("bayWord: %s\n", bayWord);
        printf("encrypted: %s\n", hex_content);
        printf("aeskey: %s\n", aeskey);

        // ADD TO FILES
        addFznAndEncContentToFznCsv(fzn, hex_content);
//                printf("Alles3: ||%s|| ; ||%s|| ; ||%s|| ; ||%s|| ; ||%s||\n", bayWord, aeskey, fzn, hex_content, aeskey);
        addBayWordAndKeyToBayCsv(bayWord, aeskey);
//                printf("Alles4: ||%s|| ; ||%s|| ; ||%s|| ; ./setflag -h %s %s\n", bayWord, aeskey, fzn, hex_content, aeskey);


    }
    else if (argc == 4) {	// for managers only!!11
        int done = 0;
        int tries = 0;

        char* flag_id = argv[1];        // FahrzeugnummerBeginnWortBeginn
        char* password = argv[2];       // ( komplette Fahrzeugnummer-.-komplettes Wort )
        char* content_arg = argv[3];        // the flag itself
        char content[4096];

        strcpy(content, content_arg);
        printf("content_arg: %s (%i)\n", content_arg, (int)(strlen(content_arg)));
        printf("Alles::content: %s (%i)\n", content, (int)(strlen(content)));
        printf("content1: %s\n", content);
        char delimiter[] = "-.-";
        char *ptr;
        ptr = strtok(password, delimiter);
        char* fzn = ptr;
        printf("fzn: %s\n", fzn);

        ptr = strtok(NULL, delimiter);
        char* bayWord = ptr;
        printf("Alles::bayWord: %s\n", bayWord);

        char aeskey[1024];
        char hex_content[4096];

        while (done == 0) {		// some weird workaround...
            tries++;
            strcpy(content, content_arg);

            printf("Alles0: || %i ||%s (%i)|| ; ||%s(%i)|| ; ||%s(%i)|| ; ||%s(%i)|| ; ||%s(%i)||\n", tries, bayWord, (int)(strlen(bayWord)), aeskey,(int)(strlen(aeskey)), fzn, (int)(strlen(fzn)), hex_content, (int)(strlen(hex_content)), content, (int)(strlen(content)));
//		char* aeskey = randstring(16);
            strcpy(aeskey, randstring(16));
            printf("aeskey: ||%s||\n", aeskey);
            printf("passing content: %s (%i)\n", content, (int)(strlen(content)));
            addPad(content, sizeof(content));
            printf("AllesX: || %i ||%s (%i)|| ; ||%s(%i)|| ; ||%s(%i)|| ; ||%s(%i)||; ||%s(%i)||\n", tries, bayWord, (int)(strlen(bayWord)), aeskey,(int)(strlen(aeskey)), fzn, (int)(strlen(fzn)), hex_content, (int)(strlen(hex_content)), content, (int)(strlen(content)));
            printf("after addpad: %s (%i)\n", content, (int)(strlen(content)));

            int index = 0;
            printf("AllesS: ");
            for (index = 0; index<strlen(content)+1; index++) {
                printf("%02X", (unsigned char)content[index]);
            }
            printf("\n");

            encryptString2(content, aeskey);
            printf("AllesY: || %i ||%s (%i)|| ; ||%s(%i)|| ; ||%s(%i)|| ; ||%s(%i)||; ||%s(%i)||\n", tries, bayWord, (int)(strlen(bayWord)), aeskey,(int)(strlen(aeskey)), fzn, (int)(strlen(fzn)), hex_content, (int)(strlen(hex_content)), content, (int)(strlen(content)));
            printf("after enc: %s (%i)\n", content, (int)(strlen(content)));
            if (strlen(content) < 32) {
                printf("strlen %i\n", (int)(strlen(content)));
                int rofl = 0;
                printf("As s: ||");
                for (rofl = 0; rofl < 32; rofl++)
                {
                    printf("%c", content[rofl]);
                }
                printf("||\nAs x02: ||");
                for (rofl = 0; rofl < 32; rofl = rofl+2) {
                    printf("%02X", content[rofl]);
                }
                printf("||\n");
            }
            printAsHex(content);
//		char* hex_content = stringToHex(content);
            strcpy(hex_content, stringToHex(content));
            printf("after hex: %s (%i)\n", content, (int)(sizeof(content)));
            printf("Alles1: || %i ||%s (%i)|| ; ||%s(%i)|| ; ||%s(%i)|| ; ||%s(%i)||; ||%s(%i)||\n", tries, bayWord, (int)(strlen(bayWord)), aeskey,(int)(strlen(aeskey)), fzn, (int)(strlen(fzn)), hex_content, (int)(strlen(hex_content)), content, (int)(strlen(content)));

            if ((( strlen(hex_content) > 0 && strlen(content) > 0 && (strlen(hex_content) % 16) == 0 && (strlen(content) % 16) == 0) ) || tries > 4 ) {
                done = 1;
                printf("Alles2: || %i ||%s|| ; ||%s|| ; ||%s|| ; ||%s|| ; ||%s||\n", tries, bayWord, aeskey, fzn, hex_content, aeskey);
                addFznAndEncContentToFznCsv(fzn, hex_content);
                printf("Alles3: || %i ||%s|| ; ||%s|| ; ||%s|| ; ||%s|| ; ||%s||\n", tries, bayWord, aeskey, fzn, hex_content, aeskey);
                addBayWordAndKeyToBayCsv(bayWord, aeskey);
                printf("Alles4: || %i ||%s|| ; ||%s|| ; ||%s|| ; ./setflag -h %s %s\n", tries, bayWord, aeskey, fzn, hex_content, aeskey);
            }
//		char tmpstr[8096];
//		sprintf(tmpstr, "echo 'tries: %i || done: %i || strlen content: (%i) || hex_content: %s (%i) || content_arg: %s (%i) || ' >> setflaglog.log", tries, done, (int)(strlen(content)), hex_content, (int)(strlen(hex_content)), content_arg, (int)(strlen(content_arg)));
//		popen(tmpstr, "r");

        }
    } else {	// official functionality
        char* fzn = argv[1];
        char* value = argv[2];
        if (fzn != NULL && value != NULL) {
            addFznAndEncContentToFznCsv(fzn, value);
            printf("FZN ADDED\n");
        } else {
            printf("you have to provide fzn and value\n");
        }
    }
}