rijndael-api-ref.c 17 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
/* rijndael-api-ref.c   v2.0   August '99
 * Reference ANSI C code
 * authors: Paulo Barreto
 *          Vincent Rijmen
 */
/*
                        ------------------------------
                        Rijndael ANSI C Reference Code
                        ------------------------------

                                October 24, 2000

                                  Disclaimer


This software package was submitted to the National Institute of Standards and
Technology (NIST) during the Advanced Encryption Standard (AES) development
effort by Joan Daemen and Vincent Rijmen, the developers of the Rijndael algorithm.

This software is distributed in compliance with export regulations (see below), and
it is intended for non-commercial use, only.   NIST does not support this software 
and does not provide any guarantees or warranties as to its performance, fitness 
for any particular application, or validation under the Cryptographic Module
Validation Program (CMVP) <http://csrc.nist.gov/cryptval/>.  NIST does not accept 
any liability associated with its use or misuse.  This software is provided as-is. 
By accepting this software the user agrees to the terms stated herein.

                            -----------------------

                              Export Restrictions


Implementations of cryptography are subject to United States Federal
Government export controls.  Export controls on commercial encryption products 
are administered by the Bureau of Export Administration (BXA) 
<http://www.bxa.doc.gov/Encryption/> in the U.S. Department of Commerce. 
Regulations governing exports of encryption are found in the Export 
Administration Regulations (EAR), 15 C.F.R. Parts 730-774.
*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "rijndael-alg-ref.h"
#include "rijndael-api-ref.h"


int makeKey(keyInstance *key, BYTE direction, int keyLen, int blockLen, unsigned char *keyMaterial)
{
    /*for debugging*/
  
    word8 k[4][MAXKC];
    int i, j, t;
    int x,y,z;

		
    if (key == NULL) {
        return BAD_KEY_INSTANCE;
    }

    if ((direction == DIR_ENCRYPT) || (direction == DIR_DECRYPT)) {
        key->direction = direction;
    } else {
        return BAD_KEY_DIR;
    }

    if ((keyLen == 128) || (keyLen == 192) || (keyLen == 256)) { 
        key->keyLen = keyLen;
    } else {
        return BAD_KEY_MAT;
    }

    /* initialize key schedule: */ 
    for(i = 0; i < key->keyLen/8; i++) {
        /*
          t = key->keyMaterial[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 BAD_KEY_MAT;
		
          t = key->keyMaterial[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 BAD_KEY_MAT;
		
          k[i % 4][i / 4] = (word8) j;
        */
        k[i % 4][i / 4] = keyMaterial[i];
    }	
	
	
    key->blockLen = blockLen;
	
    rijndaelKeySched (k, key->keyLen, key->blockLen, key->keySched);
    /*
	for (x=0; x < 11; x++){
	  for(y=0; y < 4; y++){
	    for(z=0; z < 4; z++){
	      printf(" %02x", key->keySched[x][z][y]);
	    }
	    printf("\n");
	  }
	}
	*/
    return TRUE;
}

int mult(int x, int y){
    int xt, r;
    r=0;
    xt=x;
    for(;y>0;y>>=1){
        if(y&1)
            r^=xt;
        if(xt & 0x80)
            xt = (xt<<1) ^ 0x1b;
        else
            xt <<= 1;
    }
    return r&0xff;
}

int makeKeyEqvtInv(keyInstance *key, BYTE direction, int keyLen, int blockLen, unsigned char *keyMaterial)
{
    /*for debugging*/
  
    word8 k[4][MAXKC];
    int i, j, t;
    int x,y,z;
    int a,b,c,d,m;

		
    if (key == NULL) {
        return BAD_KEY_INSTANCE;
    }

    if ((direction == DIR_ENCRYPT) || (direction == DIR_DECRYPT)) {
        key->direction = direction;
    } else {
        return BAD_KEY_DIR;
    }

    if ((keyLen == 128) || (keyLen == 192) || (keyLen == 256)) { 
        key->keyLen = keyLen;
    } else {
        return BAD_KEY_MAT;
    }

    /* initialize key schedule: */ 
    for(i = 0; i < key->keyLen/8; i++) {
        /*
          t = key->keyMaterial[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 BAD_KEY_MAT;
		
          t = key->keyMaterial[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 BAD_KEY_MAT;
		
          k[i % 4][i / 4] = (word8) j; 
        */
        k[i % 4][i / 4] = keyMaterial[i];            
    }	
    /*
	for (x=0; x< 4; x++){
	  for(y=0; y< 4;y++){
	    printf("key [%d][%d] = %02x\n", y,x, k[y][x]);
	  }
	}
	*/
    key->blockLen = blockLen;
	
    rijndaelKeySched (k, key->keyLen, key->blockLen, key->keySched);
	
	/* extra step for eqvt inv */

    for(m=1;m<10;m++)
        for(i=0;i<4;i++){
	    a=key->keySched[m][0][i]; 
	    b=key->keySched[m][1][i]; 
	    c=key->keySched[m][2][i]; 
	    d=key->keySched[m][3][i];
	    key->keySched[m][0][i]=mult(a,0xe)^mult(b,0xb)^mult(c,0xd)^mult(d,0x9);
	    key->keySched[m][1][i]=mult(a,0x9)^mult(b,0xe)^mult(c,0xb)^mult(d,0xd);
	    key->keySched[m][2][i]=mult(a,0xd)^mult(b,0x9)^mult(c,0xe)^mult(d,0xb);
	    key->keySched[m][3][i]=mult(a,0xb)^mult(b,0xd)^mult(c,0x9)^mult(d,0xe);
        }
    /*
	for (x=0; x < 11; x++){
	  for(y=0; y < 4; y++){
	    for(z=0; z < 4; z++){
	      printf(" %02x", key->keySched[x][z][y]);
	    }
	    printf("\n");
	  }
	}
	*/
    return TRUE;
}



int cipherInit(cipherInstance *cipher, BYTE mode, unsigned char *IV, int blockLen)
{
    int i, j, t;
	
    if ((mode == MODE_ECB) || (mode == MODE_CBC) || (mode == MODE_CFB1)) {
        cipher->mode = mode;
    } else {
        return BAD_CIPHER_MODE;
    }
    cipher->blockLen = blockLen;
	
    if (IV != NULL) {
        for(i = 0; i < cipher->blockLen/8; i++) {		
            /*
            t = IV[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 BAD_CIPHER_INSTANCE;
		
            t = IV[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 BAD_CIPHER_INSTANCE;
			
            cipher->IV[i] = (BYTE) j;
            */
            cipher->IV[i] = IV[i];
        } 
    }

    return TRUE;
}


int blockEncrypt(cipherInstance *cipher,
                 keyInstance *key, BYTE *input, int inputLen, BYTE *outBuffer)
{
    int i, j, t, numBlocks;
    word8 block[4][MAXBC];

	
    /* check parameter consistency: */
    if (key == NULL ||
        key->direction != DIR_ENCRYPT ||
        (key->keyLen != 128 && key->keyLen != 192 && key->keyLen != 256)) {
        return BAD_KEY_MAT;
    }
    if (cipher == NULL ||
        (cipher->mode != MODE_ECB && cipher->mode != MODE_CBC && cipher->mode != MODE_CFB1) ||
        (cipher->blockLen != 128 && cipher->blockLen != 192 && cipher->blockLen != 256)) {
        return BAD_CIPHER_STATE;
    }


    numBlocks = inputLen/cipher->blockLen;
	
    switch (cipher->mode) {
    case MODE_ECB: 
        for (i = 0; i < numBlocks; i++) {
            for (j = 0; j < cipher->blockLen/32; j++) {
                for(t = 0; t < 4; t++)
				/* parse input stream into rectangular array */
                    block[t][j] = input[4*j+t] & 0xFF;
				
            }
            rijndaelEncrypt (block, key->keyLen, cipher->blockLen, key->keySched);
            for (j = 0; j < cipher->blockLen/32; j++) {
				/* parse rectangular array into output ciphertext bytes */
                for(t = 0; t < 4; t++)
                    outBuffer[4*j+t] = (BYTE) block[t][j];
            }
        }
        break;
		
    case MODE_CBC:
        for (j = 0; j < cipher->blockLen/32; j++) {
            for(t = 0; t < 4; t++)
                /* parse initial value into rectangular array */
                block[t][j] = cipher->IV[t+4*j] & 0xFF;
        }
		
        for (i = 0; i < numBlocks; i++) {
            for (j = 0; j < cipher->blockLen/32; j++) {
                for(t = 0; t < 4; t++){
				/* parse input stream into rectangular array and exor with 
				   IV or the previous ciphertext */
                    /*block[t][j] ^= input[4*j+t] & 0xFF;*/
                    block[t][j] ^= input[4*j+t + (i*cipher->blockLen/8)] & 0xFF;
/*printf("block data = %d %d =%d from %d\n", t,j, block[t][j], 4*j+t +(i*cipher->blockLen/8));*/
                }
            }
						
            rijndaelEncrypt (block, key->keyLen, cipher->blockLen, key->keySched);
			
            for (j = 0; j < cipher->blockLen/32; j++) {
				/* parse rectangular array into output ciphertext bytes */
                for(t = 0; t < 4; t++){
                    /*outBuffer[4*j+t] = (BYTE) block[t][j];*/
                    outBuffer[4*j+t +i*cipher->blockLen/8] = (BYTE) block[t][j];
                }
            }
        }
        break;
	
    default: return BAD_CIPHER_STATE;
    }
	
    return numBlocks*cipher->blockLen;
}

int blockDecrypt(cipherInstance *cipher,
                 keyInstance *key, BYTE *input, int inputLen, BYTE *outBuffer)
{
    int i, j, t, numBlocks;
    word8 block[4][MAXBC];

    if (cipher == NULL ||
        key == NULL ||
        key->direction == DIR_ENCRYPT /* ||
                                         cipher->blockLen != key->blockLen */) {
        if(cipher == NULL){
	    fprintf(stderr," bad state here: cipher null\n");
        }
        if(key  == NULL){
	    fprintf(stderr," key null\n");
        }
	  
        if(cipher->blockLen != key->blockLen){
	    fprintf(stderr," cipher->blockLen = %d\n", cipher->blockLen);
	    fprintf(stderr," key->blockLen = %d\n", key->blockLen);
	    fprintf(stderr,"lengths dont match\n");
        }
        return BAD_CIPHER_STATE;
    }

    /* check parameter consistency: */
    if (key == NULL ||
        key->direction != DIR_DECRYPT ||
        (key->keyLen != 128 && key->keyLen != 192 && key->keyLen != 256)) {
        fprintf(stderr,"key matrix bad\n");
        return BAD_KEY_MAT;
    }
    if (cipher == NULL ||
        (cipher->mode != MODE_ECB && cipher->mode != MODE_CBC && cipher->mode != MODE_CFB1) ||
        (cipher->blockLen != 128 && cipher->blockLen != 192 && cipher->blockLen != 256)) {
        fprintf(stderr,"mode or block length incorrect\n");
        return BAD_CIPHER_STATE;
    }
	

    numBlocks = inputLen/cipher->blockLen;
	
    switch (cipher->mode) {
    case MODE_ECB: 
        for (i = 0; i < numBlocks; i++) {
            for (j = 0; j < cipher->blockLen/32; j++) {
                for(t = 0; t < 4; t++){
				/* parse input stream into rectangular array */
                    block[t][j] = input[4*j+t] & 0xFF;
                }
            }
            rijndaelDecrypt (block, key->keyLen, cipher->blockLen, key->keySched);
            for (j = 0; j < cipher->blockLen/32; j++) {
				/* parse rectangular array into output ciphertext bytes */
                for(t = 0; t < 4; t++)
                    outBuffer[4*j+t] = (BYTE) block[t][j];
            }
        }
        break;
		
    case MODE_CBC:
        /* first block */
        for (j = 0; j < cipher->blockLen/32; j++) {
            for(t = 0; t < 4; t++){
                /* parse input stream into rectangular array */
                block[t][j] = input[4*j+t] & 0xFF;
			
            }
        }
		
        rijndaelDecrypt (block, key->keyLen, cipher->blockLen, key->keySched);
		
        for (j = 0; j < cipher->blockLen/32; j++) {
            /* exor the IV and parse rectangular array into output ciphertext bytes */
            for(t = 0; t < 4; t++){
                outBuffer[4*j+t] = (BYTE) (block[t][j] ^ cipher->IV[t+4*j]);
            }
        }
		
        /* next blocks */
        for (i = 1; i < numBlocks; i++) {
            for (j = 0; j < cipher->blockLen/32; j++) {
                for(t = 0; t < 4; t++){
				/* parse input stream into rectangular array */
                    /*block[t][j] = input[cipher->blockLen/8+4*j+t] & 0xFF;
                     */
                    block[t][j] = input[4*j+t + i*cipher->blockLen/8] & 0xFF;
                }
            }
            rijndaelDecrypt (block, key->keyLen, cipher->blockLen, key->keySched);
			
            for (j = 0; j < cipher->blockLen/32; j++) {
				/* exor previous ciphertext block and parse rectangular array 
                                   into output ciphertext bytes */
                for(t = 0; t < 4; t++){
                    /*outBuffer[cipher->blockLen/8+4*j+t] = (BYTE) (block[t][j] ^ 
                      input[4*j+t-4*cipher->blockLen/32]);*/
                    outBuffer[4*j+t +i*cipher->blockLen/8] = (BYTE) block[t][j] ^input[4*j+t +(i-1)*cipher->blockLen/8];
                }


            }
        }
        break;
	
    default: return BAD_CIPHER_STATE;
    }
	
    return numBlocks*cipher->blockLen;
}

int blockDecryptEqvtInv(cipherInstance *cipher,
                        keyInstance *key, BYTE *input, int inputLen, BYTE *outBuffer)
{
    int i, j, t, numBlocks;
    word8 block[4][MAXBC];

    if (cipher == NULL ||
        key == NULL ||
        key->direction == DIR_ENCRYPT /* ||
                                         cipher->blockLen != key->blockLen */) {
        if(cipher == NULL){
	    fprintf(stderr," bad state here: cipher null\n");
        }
        if(key  == NULL){
	    fprintf(stderr," key null\n");
        }
	  
        if(cipher->blockLen != key->blockLen){
	    fprintf(stderr," cipher->blockLen = %d\n", cipher->blockLen);
	    fprintf(stderr," key->blockLen = %d\n", key->blockLen);
	    fprintf(stderr,"lengths dont match\n");
        }
        return BAD_CIPHER_STATE;
    }

    /* check parameter consistency: */
    if (key == NULL ||
        key->direction != DIR_DECRYPT ||
        (key->keyLen != 128 && key->keyLen != 192 && key->keyLen != 256)) {
        fprintf(stderr,"key matrix bad\n");
        return BAD_KEY_MAT;
    }
    if (cipher == NULL ||
        (cipher->mode != MODE_ECB && cipher->mode != MODE_CBC && cipher->mode != MODE_CFB1) ||
        (cipher->blockLen != 128 && cipher->blockLen != 192 && cipher->blockLen != 256)) {
        fprintf(stderr,"mode or block length incorrect\n");
        return BAD_CIPHER_STATE;
    }
	

    numBlocks = inputLen/cipher->blockLen;
	
    switch (cipher->mode) {
    case MODE_ECB: 
        for (i = 0; i < numBlocks; i++) {
            for (j = 0; j < cipher->blockLen/32; j++) {
                for(t = 0; t < 4; t++){
				/* parse input stream into rectangular array */
                    block[t][j] = input[4*j+t] & 0xFF;
                }
            }
            rijndaelDecryptEqvtInv (block, key->keyLen, cipher->blockLen, key->keySched);
            for (j = 0; j < cipher->blockLen/32; j++) {
				/* parse rectangular array into output ciphertext bytes */
                for(t = 0; t < 4; t++)
                    outBuffer[4*j+t] = (BYTE) block[t][j];
            }
        }
        break;
		
    case MODE_CBC:
        /* first block */
        for (j = 0; j < cipher->blockLen/32; j++) {
            for(t = 0; t < 4; t++){
                /* parse input stream into rectangular array */
                block[t][j] = input[4*j+t] & 0xFF;
			
            }
        }
		
        rijndaelDecryptEqvtInv (block, key->keyLen, cipher->blockLen, key->keySched);
		
        for (j = 0; j < cipher->blockLen/32; j++) {
            /* exor the IV and parse rectangular array into output ciphertext bytes */
            for(t = 0; t < 4; t++){
                outBuffer[4*j+t] = (BYTE) (block[t][j] ^ cipher->IV[t+4*j]);
            }
        }
		
        /* next blocks */
        for (i = 1; i < numBlocks; i++) {
		  
            for (j = 0; j < cipher->blockLen/32; j++) {
                for(t = 0; t < 4; t++){
				/* parse input stream into rectangular array */
                    /*block[t][j] = input[cipher->blockLen/8+4*j+t] & 0xFF;
                     */
                    block[t][j] = input[4*j+t + i*cipher->blockLen/8] & 0xFF;
                }
            }
			
            rijndaelDecryptEqvtInv (block, key->keyLen, cipher->blockLen, key->keySched);
			
            for (j = 0; j < cipher->blockLen/32; j++) {
				/* exor previous ciphertext block and parse rectangular array 
                                   into output ciphertext bytes */
                for(t = 0; t < 4; t++){
                    /*outBuffer[cipher->blockLen/8+4*j+t] = (BYTE) (block[t][j] ^ 
                      input[4*j+t-4*cipher->blockLen/32]);*/
                    outBuffer[4*j+t +i*cipher->blockLen/8] = (BYTE) block[t][j] ^input[4*j+t +(i-1)*cipher->blockLen/8];
                }


            }
        }
        break;
	
    default: return BAD_CIPHER_STATE;
    }
	
    return numBlocks*cipher->blockLen;
}