aes_encode.c 3.25 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef unsigned char u8;

#include "aes_api.h"

#define BLOCK_SIZE 128

int asciiToBin(char *str,int bytes,u8 *buf)
{
    int i,j;
    char t;

    for(i=0;i<bytes;i++){
        t = str[2*i];
        if ((t >= '0') && (t <= '9')) 
            j = (t - '0') << 4;
        else if ((t >= 'a') && (t <= 'f')) 
            j = (t - 'a' + 10) << 4;
        else if ((t >= 'A') && (t <= 'F')) 
            j = (t - 'A' + 10) << 4;
        else return -1;
        
        t = str[2*i+1];
        if ((t >= '0') && (t <= '9')) 
            j ^= (t - '0');
        else if ((t >= 'a') && (t <= 'f')) 
            j ^= (t - 'a' + 10);
        else if ((t >= 'A') && (t <= 'F')) 
            j ^= (t - 'A' + 10);
        else return -1;

        buf[i]=j;
    }
    
    return 0;
}

int
main(int argc, char* argv[]) {
    FILE *inptr = stdin;
    FILE *outptr = stdout;
    AesKeyInstance key;
    int keyLen, error;
    u8 iv[BLOCK_SIZE/8] ={ 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
    u8 keyMaterial[BLOCK_SIZE/8] = {0xab,0xcd,0xef,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67,0x89};
    unsigned char buf[BLOCK_SIZE/8], obuf[BLOCK_SIZE/8];
    AesCipherInstance cipher;
    int decode, print = 0;
    const char *prog = argv[1];
    decode = argc > 1 && strstr(prog, "aes_decode") != 0;
    keyLen = 128;

    while(argc > 1 && argv[1][0] == '-') {
	switch(argv[1][1]) {
	case 'k':
	    if (argc <= 2) goto usage;
            asciiToBin(argv[2],16,keyMaterial);
	    //strcpy(keyMaterial, argv[2]);
	    argc--; argv++;
	    break;
	case 'i':
	    if (argc <= 2) goto usage;
            asciiToBin(argv[2],16,iv);
	    //strcpy(iv, argv[2]);
	    argc--; argv++;
	    break;
	case 'd':
	    decode = 1;
	    break;
	case 'D':
	    decode = 2;
	    break;
	case 'p':
	    print = 1;
	    break;
	default:
	usage:
	    fprintf(stderr, "Usage: %s [-d|-D] [-p] [-k key] [-i iv] [infile [outfile]]\n", prog);
	    exit(1);
	}
	argc--; argv++;
    }
    if (argc > 1) {
	if ((inptr = fopen(argv[1], "rb")) == NULL) {
	    perror(argv[1]);
	    exit(1);
	}
    }
    if (argc > 2) {
	if ((outptr = fopen(argv[2], "wb")) == NULL) {
	    perror(argv[2]);
	    exit(1);
	}
    }


    error = aesMakeKey(&key, decode ? AES_DIR_DECRYPT : AES_DIR_ENCRYPT,
                             keyLen, keyMaterial);

    if (error != AES_TRUE) {
	fprintf(stderr, "error in computing key %d\n", error);
	exit(1);
    }
    if (print) {
	int x, y, z;
	for (x = 0; x < 11; x++)
	    for(y = 0; y < 4; y++) {
		    fprintf(stderr, "%08x", key.rk[x*4+y]);
	    }
	fprintf(stderr, "\n");
    }
  
    if ((error = aesCipherInit (&cipher, AES_MODE_CBC, iv)) != AES_TRUE) {
	fprintf(stderr,"cipherInit error %d \n", error);
	exit(1);
    }
  
    memset(buf, 0, sizeof buf);
    while (fread(buf, 1, (sizeof buf), inptr) > 0) {

	if (decode == 1)
	    error = aesBlockDecrypt(&cipher, &key, buf, sizeof(buf)*8, obuf);
	else
	    error = aesBlockEncrypt(&cipher, &key, buf, sizeof(buf)*8, obuf);
	if (error <= 0) {
	    fprintf(stderr, "cipher encryption error %d\n", error);
	    exit(1);
	} else if (fwrite(obuf, (sizeof obuf), 1, outptr) != 1) {
	    perror("fwrite");
	    exit(1);
	}
	memset(buf, 0, sizeof buf);
    }
    return 0;
}