summaryrefslogtreecommitdiff
path: root/lib/lib2.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/lib2.c')
-rw-r--r--lib/lib2.c55
1 files changed, 46 insertions, 9 deletions
diff --git a/lib/lib2.c b/lib/lib2.c
index 49f7363..a28b7a5 100644
--- a/lib/lib2.c
+++ b/lib/lib2.c
@@ -59,7 +59,7 @@ int valid_pkcs7_padding(const char *in, int length_in, char *unpadded, int block
* @param string_key key with which the content in in has been decrypted
* @param iv initalization vector
*/
-int aes_cbc(char *in, int length_in, char *out, unsigned char *string_key, char *init_vector)
+int aes_cbc(char *in, int length_in, char *out, unsigned char *string_key, char *init_vector, int encrypt)
{
char iv[16];
AES_KEY key;
@@ -69,22 +69,59 @@ int aes_cbc(char *in, int length_in, char *out, unsigned char *string_key, char
unsigned char tmp_after_aes[128+1];
unsigned char cleartext[128+1];
// set the key and bits
- AES_set_decrypt_key(string_key, 128, &key);
+ if(encrypt)
+ AES_set_encrypt_key(string_key, 128, &key);
+ else
+ AES_set_decrypt_key(string_key, 128, &key);
memcpy(init_vector, iv, 16);
// implement cbc mode
for(i=0;i<number_blocks;i++) {
- //do aes decryption
- AES_decrypt(&in[i*16], tmp_after_aes, &key);
- // xor
- xor_string(iv, tmp_after_aes, &out[i*16], 16, 16);
- // this ciphertext block is the next iv
- for(j=0;j<16;j++) {
- iv[j] = in[i*16+j];
+ if (!encrypt) {
+ //do aes decryption
+ AES_decrypt(&in[i*16], tmp_after_aes, &key);
+ // xor
+ xor_string(iv, tmp_after_aes, &out[i*16], 16, 16);
+ // this ciphertext block is the next iv
+ for(j=0;j<16;j++) {
+ iv[j] = in[i*16+j];
+ }
+ }
+ else {
+ // first xor
+ xor_string(iv, &in[i*16], tmp_after_aes, 16, 16);
+ // aes encrypt
+ AES_encrypt(tmp_after_aes, &out[i*16], &key);
+ // ciphertext is next iv
+ for(j=0;j<16;j++) {
+ iv[j] = out[i*16+j];
+ }
}
}
return 0;
}
+
+int aes_ecb(char *in, int length_in, char *out, unsigned char *string_key,
+ int blocksize, int encrypt)
+{
+ AES_KEY key;
+ int number_blocks = length_in / blocksize;
+ int i;
+
+ if(encrypt)
+ AES_set_encrypt_key(string_key, blocksize, &key);
+ else
+ AES_set_decrypt_key(string_key, blocksize, &key);
+
+ for(i=0;i<number_blocks;i++) {
+ if(encrypt) {
+ AES_encrypt(&in[i*blocksize], &out[i*blocksize],&key);
+ }
+ else {
+ AES_decrypt(&in[i*blocksize], &out[i*blocksize],&key);
+ }
+ }
+}