char rijndaelEncrypt () { /* Encryption of one block. */ char r; /* begin with a key addition */ KeyAddition ( 0 ); /* ROUNDS-1 ordinary rounds */ for ( r = 1; r < ROUNDS; r++ ) { SubstitutionS(); ShiftRow0(); MixColumn(); KeyAddition ( r ); } /* Last round is special: there is no MixColumn */ SubstitutionS(); ShiftRow0(); KeyAddition ( ROUNDS ); return 0; }
int rijndaelDecryptRound (word8 a[4][MAXBC], int keyBits, int blockBits, word8 rk[MAXROUNDS+1][4][MAXBC], int rounds) /* Decrypt only a certain number of rounds. * Only used in the Intermediate Value Known Answer Test. * Operations rearranged such that the intermediate values * of decryption correspond with the intermediate values * of encryption. */ { int r, BC, ROUNDS; switch (blockBits) { case 128: BC = 4; break; case 192: BC = 6; break; case 256: BC = 8; break; default : return (-2); } switch (keyBits >= blockBits ? keyBits : blockBits) { case 128: ROUNDS = 10; break; case 192: ROUNDS = 12; break; case 256: ROUNDS = 14; break; default : return (-3); /* this cannot happen */ } /* make number of rounds sane */ if (rounds > ROUNDS) rounds = ROUNDS; /* First the special round: * without InvMixColumn * with extra KeyAddition */ KeyAddition(a,rk[ROUNDS],BC); Substitution(a,Si,BC); ShiftRow(a,1,BC); /* ROUNDS-1 ordinary rounds */ for(r = ROUNDS-1; r > rounds; r--) { KeyAddition(a,rk[r],BC); InvMixColumn(a,BC); Substitution(a,Si,BC); ShiftRow(a,1,BC); } if (rounds == 0) { /* End with the extra key addition */ KeyAddition(a,rk[0],BC); } return 0; }
int _rijndaelDecrypt (word8 a[4][MAXBC], int keyBits, int blockBits, word8 rk[MAXROUNDS+1][4][MAXBC]) { int r, BC, ROUNDS; switch (blockBits) { case 128: BC = 4; break; case 192: BC = 6; break; case 256: BC = 8; break; default : return (-2); } switch (keyBits >= blockBits ? keyBits : blockBits) { case 128: ROUNDS = 10; break; case 192: ROUNDS = 12; break; case 256: ROUNDS = 14; break; default : return (-3); /* this cannot happen */ } /* To decrypt: apply the inverse operations of the encrypt routine, * in opposite order * * (KeyAddition is an involution: it 's equal to its inverse) * (the inverse of Substitution with table S is Substitution with the inverse table of S) * (the inverse of Shiftrow is Shiftrow over a suitable distance) */ /* First the special round: * without InvMixColumn * with extra KeyAddition */ KeyAddition(a,rk[ROUNDS],BC); Substitution(a,Si,BC); ShiftRow(a,1,BC); /* ROUNDS-1 ordinary rounds */ for(r = ROUNDS-1; r > 0; r--) { KeyAddition(a,rk[r],BC); InvMixColumn(a,BC); Substitution(a,Si,BC); ShiftRow(a,1,BC); } /* End with the extra key addition */ KeyAddition(a,rk[0],BC); return 0; }
int rijndaelEncryptRound (word8 a[4][MAXBC], int keyBits, int blockBits, word8 rk[MAXROUNDS+1][4][MAXBC], int rounds) /* Encrypt only a certain number of rounds. * Only used in the Intermediate Value Known Answer Test. */ { int r, BC, ROUNDS; switch (blockBits) { case 128: BC = 4; break; case 192: BC = 6; break; case 256: BC = 8; break; default : return (-2); } switch (keyBits >= blockBits ? keyBits : blockBits) { case 128: ROUNDS = 10; break; case 192: ROUNDS = 12; break; case 256: ROUNDS = 14; break; default : return (-3); /* this cannot happen */ } /* make number of rounds sane */ if (rounds > ROUNDS) rounds = ROUNDS; /* begin with a key addition */ KeyAddition(a,rk[0],BC); /* at most ROUNDS-1 ordinary rounds */ for(r = 1; (r <= rounds) && (r < ROUNDS); r++) { Substitution(a,S,BC); ShiftRow(a,0,BC); MixColumn(a,BC); KeyAddition(a,rk[r],BC); } /* if necessary, do the last, special, round: */ if (rounds == ROUNDS) { Substitution(a,S,BC); ShiftRow(a,0,BC); KeyAddition(a,rk[ROUNDS],BC); } return 0; }
int _rijndaelEncrypt (word8 a[4][MAXBC], int keyBits, int blockBits, word8 rk[MAXROUNDS+1][4][MAXBC]) { /* Encryption of one block. */ int r, BC, ROUNDS; switch (blockBits) { case 128: BC = 4; break; case 192: BC = 6; break; case 256: BC = 8; break; default : return (-2); } switch (keyBits >= blockBits ? keyBits : blockBits) { case 128: ROUNDS = 10; break; case 192: ROUNDS = 12; break; case 256: ROUNDS = 14; break; default : return (-3); /* this cannot happen */ } /* begin with a key addition */ KeyAddition(a,rk[0],BC); /* ROUNDS-1 ordinary rounds */ for(r = 1; r < ROUNDS; r++) { Substitution(a,S,BC); ShiftRow(a,0,BC); MixColumn(a,BC); KeyAddition(a,rk[r],BC); } /* Last round is special: there is no MixColumn */ Substitution(a,S,BC); ShiftRow(a,0,BC); KeyAddition(a,rk[ROUNDS],BC); return 0; }
int rijndaelDecryptRound (word8 a[4][4], word8 rk[MAXROUNDS+1][4][4], int rounds) /* Decrypt only a certain number of rounds. * Only used in the Intermediate Value Known Answer Test. * Operations rearranged such that the intermediate values * of decryption correspond with the intermediate values * of encryption. */ { int r; /* make number of rounds sane */ if (rounds > ROUNDS) rounds = ROUNDS; /* First the special round: * without InvMixColumn * with extra KeyAddition */ KeyAddition(a,rk[ROUNDS],4); Substitution(a,Si,4); ShiftRow(a,1,4); /* ROUNDS-1 ordinary rounds */ for(r = ROUNDS-1; r > rounds; r--) { KeyAddition(a,rk[r],4); InvMixColumn(a,4); Substitution(a,Si,4); ShiftRow(a,1,4); } if (rounds == 0) { /* End with the extra key addition */ KeyAddition(a,rk[0],4); } return 0; }
char rijndaelDecrypt () { char r; /* To decrypt: apply the inverse operations of the encrypt routine, * in opposite order * * (KeyAddition is an involution: it 's equal to its inverse) * (the inverse of Substitution with table S is Substitution with the inverse table of S) * (the inverse of Shiftrow is Shiftrow over a suitable distance) */ /* First the special round: * without InvMixColumn * with extra KeyAddition */ KeyAddition ( ROUNDS ); SubstitutionSi(); ShiftRow1(); /* ROUNDS-1 ordinary rounds */ for ( r = ROUNDS - 1; r > 0; r-- ) { KeyAddition ( r ); InvMixColumn(); SubstitutionSi(); ShiftRow1(); } /* End with the extra key addition */ KeyAddition ( 0 ); return 0; }