📄 aes_api.c
字号:
/** * @file aes_api.c * * @brief interface for NIST AES implementation, OFB decryption only * * @date @(#) Sep 21 2004, 09:28:38 * * @author Reinhard Wobst */#include "aes_ref_encr.h"#include "aes_api.h"/* help function */static void encrypt_one_block(unsigned char *block);/* static data */static unsigned char *Iv;static int Counter;static ROUNDKEYS *roundkeys;/** * @brief initialize AES OFB decryption * * @param key the 128 bit key * @param iv the 128 bit initialization vector * @param rky pointer to workspace for round keys */void decrypt_aes_init(unsigned char key[], unsigned char *iv, ROUNDKEYS * rky){ unsigned char keytab[4][MAXBC]; int i; roundkeys = rky; for (i = 4 * 4; i--;) keytab[i % 4][i / 4] = key[i]; rijndaelKeySched(keytab, 128, 128, *roundkeys); Counter = 0; Iv = iv; encrypt_one_block(Iv);}/** * @brief decrypt piece of AES OFB enciphered text * * @param block [in,out] the ciphertext, will be overwritten by * plaintext * @param len number of bytes to decrypt */void decrypt_aes_ofb(unsigned char *block, int len){ register int i; register unsigned char *piv = Iv + Counter; if (Counter) { while (Counter < 16 && len > 0) { *block++ ^= *piv++; ++Counter; --len; } if (!len) return; encrypt_one_block(Iv); piv = Iv; Counter = 0; } /* now counter == 0 */ while (len >= 16) { for (i = 16; i--;) *block++ ^= *piv++; piv = Iv; encrypt_one_block(Iv); len -= 16; } /* still Counter == 0, len < 16 */ while (len) { *block++ ^= *piv++; --len; ++Counter; }}/** * @brief encrypt one AES block * * @param block the block */static void encrypt_one_block(unsigned char *block){ register int j, t; unsigned char table[4][MAXBC]; /* parse input stream into rectangular array */ for (j = 0; j < 4; j++) for (t = 0; t < 4; t++) table[t][j] = block[4 * j + t] & 0xFF; rijndaelEncrypt(table, 128, 128, *roundkeys); /* parse rectangular array into output ciphertext bytes */ for (j = 0; j < 4; j++) for (t = 0; t < 4; t++) block[4 * j + t] = table[t][j];}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -