Example #1
0
void CCAESCmac(const void *key,
               const uint8_t *data,
               size_t dataLength,			/* length of data in bytes */
               void *macOut)				/* MAC written here */
{
    uint8_t       X[16],Y[16], M_last[16], padded[16];
    uint8_t       K1[16], K2[16];
    int         flag;
    size_t      n;
    const struct ccmode_ecb *aesmode = getCipherMode(kCCAlgorithmAES128, kCCModeECB, kCCEncrypt).ecb;
    ccecb_ctx_decl(aesmode->size, ctx);

    CC_DEBUG_LOG(ASL_LEVEL_ERR, "Entering\n");

	// CMacInit
    
    aesmode->init(aesmode, ctx, 16, key);
    aesmode->ecb(ctx, 1, Y, X);
    ccGenAESSubKey(aesmode, ctx, K1, K2);
    
    // CMacUpdates (all in this case)
    
    n = (dataLength+15) / 16;       /* n is number of rounds */
    
    if ( 0 == n ) {
        n = 1;
        flag = 0;
    } else {
        if ( (dataLength%16) == 0 ) flag = 1;
        else  flag = 0;
    }
    
    if ( flag ) { /* last block is complete block */
        xor_128(&data[16*(n-1)],K1,M_last);
    } else {
        ccAESCMacPadding(&data[16*(n-1)],padded,dataLength%16);
        xor_128(padded,K2,M_last);
    }
    
    memset(X, 0, 16);
    for (size_t i=0; i<n-1; i++ ) {
        xor_128(X,&data[16*i],Y); /* Y := Mi (+) X  */
        aesmode->ecb(ctx, 1, Y, X);
    }
    
    // CMacFinal
    
    xor_128(X,M_last,Y);
    aesmode->ecb(ctx, 1, Y, X);
    
    memcpy(macOut, X, 16);
}
Example #2
0
configuration_t *clparser(int argc, char ** argv) {
	configuration_t *conf;
	char *aOpt[] = {"aes128", "aes192", "aes256", "des"};
	char *mOpt[] = {"ecb", "cfb", "ofb", "cbc"};
	char *stegOpt[] = {"LSB1", "LSB4", "LSBE", "WANTED"};


	if((conf = calloc(sizeof(configuration_t),1)) == NULL){
		return NULL;
	}

	parserADT parser = newParser(argc, argv);

	setArg(parser, arg_in, "-in", 0, 1, ARG_TYPE1, 0, NULL);
	setArg(parser, arg_out, "-out", 0, 1, ARG_TYPE1, 0, NULL);
	setArg(parser, arg_porter, "-p", 0, 1, ARG_TYPE1, 0, NULL);
	setArg(parser, arg_embed, "-embed", 1, 1, ARG_TYPE0, 0, NULL);
	setArg(parser, arg_extract, "-extract", 1, 1, ARG_TYPE0, 0, NULL);
	setArg(parser, arg_pass, "-pass", 1, 1, ARG_TYPE1, 0, NULL);
	setArg(parser, arg_algorithm, "-a", 1, 1, ARG_TYPE1, SIZE_OF_ARRAY(aOpt), aOpt);
	setArg(parser, arg_ciphermode, "-m", 1, 1, ARG_TYPE1, SIZE_OF_ARRAY(mOpt) , mOpt);
	setArg(parser, arg_steg, "-steg", 1, 1, ARG_TYPE1, SIZE_OF_ARRAY(stegOpt), stegOpt);
	setArg(parser, arg_help, "-h", 1, 1, ARG_TYPE0, 0, NULL); //help

	int error = 0;
	while(!noMoreArgs(parser) && !error){
		switch(getArg(parser)){
		case arg_out:
			error = !processArg(&conf->targetFile, getValue(parser), "target file");
			break;
		case arg_in:
			error = !processArg(&conf->sourceFile, getValue(parser), "source file");
			break;
		case arg_porter:
			error = !processArg(&conf->carrierFile, getValue(parser), "carrier file");
			break;
		case arg_embed:
			if(conf->embed == steg_notSet){
				LOG("embed/extract is embed\n");
				conf->embed = steg_embed;

				if(conf->encriptation != NULL) {
					conf->encriptation->encrypOrDecrypt = encrypOrDecrypt_encrypt;
				}

			}
			else {
				printf("embed/extract already set\n");
				error = 1;
			}
			break;
		case arg_extract:
			if(conf->embed == steg_notSet){
				LOG("embed/extract is extract\n");
				conf->embed = steg_extract;

				if(conf->encriptation != NULL) {
					conf->encriptation->encrypOrDecrypt = encrypOrDecrypt_decrypt;
				}


			} else {
				printf("embed/extract already set\n");
				error = 1;
			}
			break;
		case arg_pass:
			if(!initEncrypt(conf)){
				error = 1;
			} else {
			error = !processArg((char**)&conf->encriptation->passKeyIv.password, getValue(parser), "password");
			}
			break;
		case arg_algorithm:
			if(!initEncrypt(conf)){
				error = 1;
			} else {
				algorithm_t alg = getCryptoAlgorithm(getValue(parser));
				if(alg != algorithm_none){
					setCryptoAlgorithm(conf->encriptation, alg);
				} else {
					printf("encryptation algorithm not reconized: %s\n", getValue(parser));
					error = 1;
				}
			}
			break;
		case arg_ciphermode:
			if(!initEncrypt(conf)){
				error = 1;
			} else {
				ciphermode_t cipherm = getCipherMode(getValue(parser));
				if(cipherm != ciphermode_none){
					setCryptoCiphermode(conf->encriptation, cipherm);
				} else {
					printf("ciphermode not reconized: %s\n", getValue(parser));
					error = 1;
				}
			}
			break;
		case arg_steg:
			if(conf->stegMode != stegMode_none){
				printf("steg already set\n");
				error = 1;
			} else {
				stegMode_t mode = getStegMode(getValue(parser));
				if(mode == stegMode_none){
					printf("steg mode not reconized: %s\n", getValue(parser));
					error = 1;
				} else {
					conf->stegMode = mode;
				}
			}
			break;
		case arg_help:
			showHelp();
			return NULL;
			break;
		default:
			error = 1;
			break;
		}

	}

	return conf;

}