Esempio n. 1
0
aes_rval aes_set_block_size(unsigned int blen, aes_ctx cx[1])
{
#if !defined(FIXED_TABLES)
#ifdef GLOBALS
    if(!t_use(in,it)) gen_tabs();
#else
    if(!cx->t_ptr || !t_use(in,it)) gen_tabs(cx);
#endif
#endif
    if(((blen & 7) || blen < 16 || blen > 32) && ((blen & 63) || blen < 128 || blen > 256))
    {
        cx->n_blk = 0; return aes_bad;
    }
    else
    {
        cx->n_blk = blen >> (blen < 128 ? 0 : 3); return aes_good;
    }
}
Esempio n. 2
0
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
	WindowsInitialization();

	gen_tabs();								// initialize AES tables
	CheckSpecialArgs();						// handle commandline

	std::auto_ptr<document::Document>	doc(new document::Document);

	if(g_documentFilePath.empty())
	{
		// haven't got a filename, check for embedded data. If we've got embedded data,
		// make a temp copy of our exe, and launch that with "-edit". If no embedded data,
		// we'll end up with a blank note instead.

		FileEndData::FileEndData fed( xstring(g_moduleName), true );
		if(fed.HasData())
			return CopyAndSpawn() ? 0 : -1;
	} else
	{
		// We've got a filename from commandline, work with that file.
		if(!CheckAndLoadEmbeddedDocument(g_documentFilePath, doc))
		{
			util::error_box(strErrorLoad);
			return -1;
		}
	}

	// Now doc is either loaded from our own executable, or blank - start the editor.
	editor::Editor edit(doc);

	if(!g_documentFilePath.empty())
		edit.UpdateTitle(g_documentFilePath.c_str());

	MessageLoop(edit.GetHWND());

	if(g_editorMode)
	{
		// We were originally launched with "-edit" argument, which means we're running
		// from a temp file that should be erased. Launch original process to delete temp.
		//TODO: what if a nosy user is messing with commandline args?
		xstring args(_T("-erase:\""));
		args.append(g_moduleName);
		args.append(_T("\""));

		util::launch(g_documentFilePath, args, false);
	}

	return 0;
}
Esempio n. 3
0
void rijndael::set_key(const u1byte in_key[], const u4byte key_len)
{
	u4byte  i, t, u, v, w;

	if(!tab_gen)

		gen_tabs();

	k_len = (key_len + 31) / 32;

	e_key[0] = u4byte_in(in_key     ); 
	e_key[1] = u4byte_in(in_key +  4);
	e_key[2] = u4byte_in(in_key +  8); 
	e_key[3] = u4byte_in(in_key + 12);

	switch(k_len)
	{
	case 4: t = e_key[3];
		for(i = 0; i < 10; ++i) 
			loop4(i);
		break;

	case 6: e_key[4] = u4byte_in(in_key + 16); t = e_key[5] = u4byte_in(in_key + 20);
		for(i = 0; i < 8; ++i) 
			loop6(i);
		break;

	case 8: e_key[4] = u4byte_in(in_key + 16); e_key[5] = u4byte_in(in_key + 20);
		e_key[6] = u4byte_in(in_key + 24); t = e_key[7] = u4byte_in(in_key + 28);
		for(i = 0; i < 7; ++i) 
			loop8(i);
		break;
	}

	d_key[0] = e_key[0]; d_key[1] = e_key[1];
	d_key[2] = e_key[2]; d_key[3] = e_key[3];

	for(i = 4; i < 4 * k_len + 24; ++i)
	{
		imix_col(d_key[i], e_key[i]);
	}

	return;
}
Esempio n. 4
0
int main(int argc, char *argv[])
{   FILE            *fin = 0, *fout = 0;
    char            *cp, ch, key[32];
    int             i, by = 0, key_len, err = 0;

    if(argc != 5 || toupper(*argv[3]) != 'D' && toupper(*argv[3]) != 'E')
    {
        printf("usage: aesxam in_filename out_filename [d/e] key_in_hex\n");
        err = -1; goto exit;
    }

    gen_tabs();     // in case dynamic AES tables are being used

    cp = argv[4];   // this is a pointer to the hexadecimal key digits
    i = 0;          // this is a count for the input digits processed

    while(i < 64 && *cp)        // the maximum key length is 32 bytes and
    {                           // hence at most 64 hexadecimal digits
        ch = toupper(*cp++);    // process a hexadecimal digit
        if(ch >= '0' && ch <= '9')
            by = (by << 4) + ch - '0';
        else if(ch >= 'A' && ch <= 'F')
            by = (by << 4) + ch - 'A' + 10;
        else                    // error if not hexadecimal
        {
            printf("key must be in hexadecimal notation\n");
            err = -2; goto exit;
        }

        // store a key byte for each pair of hexadecimal digits
        if(i++ & 1)
            key[i / 2 - 1] = by & 0xff;
    }

    if(*cp)
    {
        printf("The key value is too long\n");
        err = -3; goto exit;
    }
    else if(i < 32 || (i & 15))
    {
        printf("The key length must be 32, 48 or 64 hexadecimal digits\n");
        err = -4; goto exit;
    }

    key_len = i / 2;

    if(!(fin = fopen(argv[1], "rb")))   // try to open the input file
    {
        printf("The input file: %s could not be opened\n", argv[1]);
        err = -5; goto exit;
    }

    if(!(fout = fopen(argv[2], "wb")))  // try to open the output file
    {
        printf("The output file: %s could not be opened\n", argv[2]);
        err = -6; goto exit;
    }

    if(toupper(*argv[3]) == 'E') // encryption in Cipher Block Chaining mode
    {   aes_encrypt_ctx ctx[1];

        aes_encrypt_key((unsigned char*)key, key_len, ctx);

        err = encfile(fin, fout, ctx, argv[1], argv[2]);
    }
    else                         // decryption in Cipher Block Chaining mode
    {   aes_decrypt_ctx ctx[1];

        aes_decrypt_key((unsigned char*)key, key_len, ctx);

        err = decfile(fin, fout, ctx, argv[1], argv[2]);
    }
exit:
    if(err == READ_ERROR)
        printf("Error reading from input file: %s\n", argv[1]);

    if(err == WRITE_ERROR)
        printf("Error writing to output file: %s\n", argv[2]);

    if(fout)
        fclose(fout);

    if(fin)
        fclose(fin);

    return err;
}
Esempio n. 5
0
aes_rval aes_set_decrypt_key(const unsigned char in_key[], unsigned int klen, aes_ctx cx[1])
{   aes_32t    ss[8];
    d_vars

#if !defined(FIXED_TABLES)
#ifdef GLOBALS
    if(!t_use(in,it)) gen_tabs();
#else
    if(!cx->t_ptr || !t_use(in,it)) gen_tabs(cx);
#endif
#endif

#if !defined(BLOCK_SIZE)
    if(!cx->n_blk) cx->n_blk = 16;
#else
    cx->n_blk = BLOCK_SIZE;
#endif

    if(((klen & 7) || klen < 16 || klen > 32) && ((klen & 63) || klen < 128 || klen > 256))
    {
        cx->n_rnd = 0; return aes_bad;
    }

    klen >>= (klen < 128 ? 2 : 5);
    cx->n_blk = (cx->n_blk & ~3) | 2;

    cx->k_sch[0] = ss[0] = word_in(in_key     );
    cx->k_sch[1] = ss[1] = word_in(in_key +  4);
    cx->k_sch[2] = ss[2] = word_in(in_key +  8);
    cx->k_sch[3] = ss[3] = word_in(in_key + 12);

#if (BLOCK_SIZE == 16) && (DEC_UNROLL != NONE)

    switch(klen)
    {
    case 4:
        kdf4(cx->k_sch, 0); kd4(cx->k_sch, 1);
        kd4(cx->k_sch, 2); kd4(cx->k_sch, 3);
        kd4(cx->k_sch, 4); kd4(cx->k_sch, 5);
        kd4(cx->k_sch, 6); kd4(cx->k_sch, 7);
        kd4(cx->k_sch, 8); kdl4(cx->k_sch, 9);
        cx->n_rnd = 10; break;
    case 6:
        cx->k_sch[4] = ff(ss[4] = word_in(in_key + 16));
        cx->k_sch[5] = ff(ss[5] = word_in(in_key + 20));
        kdf6(cx->k_sch, 0); kd6(cx->k_sch, 1);
        kd6(cx->k_sch, 2); kd6(cx->k_sch, 3);
        kd6(cx->k_sch, 4); kd6(cx->k_sch, 5);
        kd6(cx->k_sch, 6); kdl6(cx->k_sch, 7);
        cx->n_rnd = 12; break;
    case 8:
        cx->k_sch[4] = ff(ss[4] = word_in(in_key + 16));
        cx->k_sch[5] = ff(ss[5] = word_in(in_key + 20));
        cx->k_sch[6] = ff(ss[6] = word_in(in_key + 24));
        cx->k_sch[7] = ff(ss[7] = word_in(in_key + 28));
        kdf8(cx->k_sch, 0); kd8(cx->k_sch, 1);
        kd8(cx->k_sch, 2); kd8(cx->k_sch, 3);
        kd8(cx->k_sch, 4); kd8(cx->k_sch, 5);
        kdl8(cx->k_sch, 6);
        cx->n_rnd = 14; break;
    default:
        ;
    }
#else
    cx->n_rnd = (klen > nc ? klen : nc) + 6;
    {   aes_32t i, l;
        l = (nc * cx->n_rnd + nc - 1) / klen;

        switch(klen)
        {
        case 4:
            for(i = 0; i < l; ++i)
                ke4(cx->k_sch, i);
            break;
        case 6:
            cx->k_sch[4] = ss[4] = word_in(in_key + 16);
            cx->k_sch[5] = ss[5] = word_in(in_key + 20);
            for(i = 0; i < l; ++i)
                ke6(cx->k_sch, i);
            break;
        case 8:
            cx->k_sch[4] = ss[4] = word_in(in_key + 16);
            cx->k_sch[5] = ss[5] = word_in(in_key + 20);
            cx->k_sch[6] = ss[6] = word_in(in_key + 24);
            cx->k_sch[7] = ss[7] = word_in(in_key + 28);
            for(i = 0; i < l; ++i)
                ke8(cx->k_sch,  i);
            break;
        default:
            ;
        }
#if (DEC_ROUND != NO_TABLES)
        for(i = nc; i < nc * cx->n_rnd; ++i)
            cx->k_sch[i] = inv_mcol(cx->k_sch[i]);
#endif
    }
#endif

    return aes_good;
}