int main (int argc, const char * argv[]) { if(argc < 2) { fprintf(stderr, "Usage: aes <e|d>\n"); exit(1); } switch(tolower(argv[1][0])) { case 'e': AES_Encrypt(); break; case 'd': AES_Decrypt(); break; default: fprintf(stderr, "Invalid mode. Expected e or d\n"); exit(1); } return 0; }
int NL6621_AES_Decrypt(unsigned char *plaintext, const unsigned char *ciphertext, unsigned int data_len) { AES_Decrypt(plaintext, ciphertext, data_len, IV); return 0; }
/* ** Perform AES key unwrap. ** "cx" the context ** "output" the output buffer to store the decrypted data. ** "outputLen" how much data is stored in "output". Set by the routine ** after some data is stored in output. ** "maxOutputLen" the maximum amount of data that can ever be ** stored in "output" ** "input" the input data ** "inputLen" the amount of input data */ extern SECStatus AESKeyWrap_Decrypt(AESKeyWrapContext *cx, unsigned char *output, unsigned int *pOutputLen, unsigned int maxOutputLen, const unsigned char *input, unsigned int inputLen) { PRUint64 * R = NULL; unsigned int nBlocks; unsigned int i, j; unsigned int aesLen = AES_BLOCK_SIZE; unsigned int outLen; SECStatus s = SECFailure; /* These PRUint64s are ALWAYS big endian, regardless of CPU orientation. */ PRUint64 t; PRUint64 B[2]; /* Check args */ if (inputLen < 3 * AES_KEY_WRAP_BLOCK_SIZE || 0 != inputLen % AES_KEY_WRAP_BLOCK_SIZE) { PORT_SetError(SEC_ERROR_INPUT_LEN); return s; } outLen = inputLen - AES_KEY_WRAP_BLOCK_SIZE; #ifdef maybe if (!output && pOutputLen) { /* caller is asking for output size */ *pOutputLen = outLen; return SECSuccess; } #endif if (maxOutputLen < outLen) { PORT_SetError(SEC_ERROR_OUTPUT_LEN); return s; } if (cx == NULL || output == NULL || input == NULL) { PORT_SetError(SEC_ERROR_INVALID_ARGS); return s; } nBlocks = inputLen / AES_KEY_WRAP_BLOCK_SIZE; R = PORT_NewArray(PRUint64, nBlocks); if (!R) return s; /* error is already set. */ nBlocks--; /* ** 1) Initialize variables. */ memcpy(&R[0], input, inputLen); B[0] = R[0]; #if BIG_ENDIAN_WITH_64_BIT_REGISTERS t = 6UL * nBlocks; #else set_t((unsigned char *)&t, 6UL * nBlocks); #endif /* ** 2) Calculate intermediate values. */ for (j = 0; j < 6; ++j) { for (i = nBlocks; i; --i) { /* here, XOR A with t (in big endian order) and decrement t; */ #if BIG_ENDIAN_WITH_64_BIT_REGISTERS B[0] ^= t--; #else xor_and_decrement(&B[0], &t); #endif B[1] = R[i]; s = AES_Decrypt(&cx->aescx, (unsigned char *)B, &aesLen, sizeof B, (unsigned char *)B, sizeof B); if (s != SECSuccess) break; R[i] = B[1]; } } /* ** 3) Output the results. */ if (s == SECSuccess) { int bad = memcmp(&B[0], cx->iv, AES_KEY_WRAP_IV_BYTES); if (!bad) { memcpy(output, &R[1], outLen); if (pOutputLen) *pOutputLen = outLen; } else { s = SECFailure; PORT_SetError(SEC_ERROR_BAD_DATA); if (pOutputLen) *pOutputLen = 0; } } else if (pOutputLen) { *pOutputLen = 0; } PORT_ZFree(R, inputLen); return s; }
int _tmain(int argc, _TCHAR* argv[]) { int i; // The array temp stores the key. // The array temp1 stores the plaintext. unsigned char Key[16] = {0x00 ,0x01 ,0x02 ,0x03 ,0x04 ,0x05 ,0x06 ,0x07 ,0x08 ,0x09 ,0x0a ,0x0b ,0x0c ,0x0d ,0x0e ,0x0f}; unsigned char temp1[16]= {0x00 ,0x11 ,0x22 ,0x33 ,0x44 ,0x55 ,0x66 ,0x77 ,0x88 ,0x99 ,0xaa ,0xbb ,0xcc ,0xdd ,0xee ,0xff}; unsigned char temp2[32]= {0x00 ,0x11 ,0x22 ,0x33 ,0x44 ,0x55 ,0x66 ,0x77 ,0x88 ,0x99 ,0xaa ,0xbb ,0xcc ,0xdd ,0xee ,0xff, 0x00 ,0x11 ,0x22 ,0x33 ,0x44 ,0x55 ,0x66 ,0x77 ,0x88 ,0x99 ,0xaa ,0xbb ,0xcc ,0xdd ,0xee ,0xff}; unsigned char temp3[32]; // The KeyExpansion routine must be called before encryption. AES_SetKey(Key); AES_SetIV(Key); // The next function call encrypts the PlainText with the Key using AES algorithm. AES_Encrypt(temp1, 16, temp1, 16); // Output the encrypted text. printf("\nText after encryption:\n"); for (i = 0; i < sizeof(temp1); i++) { printf("%02x ", temp1[i]); } printf("\n\n"); AES_Decrypt(temp1, temp1, 16); // Output the decrypted text. printf("\nText after decryption:\n"); for (i = 0; i < sizeof(temp1); i++) { printf("%02x ", temp1[i]); } printf("\n\n"); AES_Encrypt(temp2, 32, temp2, 32, CBC); printf("\nText after encryption:\n"); for (i = 0; i < sizeof(temp2); i++) { printf("%02x ", temp2[i]); } printf("\n\n"); AES_Decrypt(temp2, temp2, 32, CBC); printf("\nText after decryption:\n"); for (i = 0; i < sizeof(temp2); i++) { printf("%02x ", temp2[i]); } printf("\n\n"); AES_Encrypt(temp2, 16, temp3, 32, false, CFB); AES_Encrypt(temp2 + 16, 16, temp3 + 16, 16, true, CFB); printf("\nText after encryption:\n"); for (i = 0; i < sizeof(temp3); i++) { printf("%02x ", temp3[i]); } printf("\n\n"); AES_Decrypt(temp3, temp3, 32, false, CFB); printf("\nText after decryption:\n"); for (i = 0; i < sizeof(temp2); i++) { printf("%02x ", temp2[i]); } printf("\n\n"); AES_Encrypt(temp2, 24, temp3, 32, false, CFB); printf("\nText after encryption:\n"); for (i = 0; i < sizeof(temp3); i++) { printf("%02x ", temp3[i]); } printf("\n\n"); AES_Decrypt(temp3, temp2, 32, false, CFB); printf("\nText after decryption:\n"); for (i = 0; i < sizeof(temp2); i++) { printf("%02x ", temp2[i]); } printf("\n\n"); return 0; }