From: Pekka Riikonen Date: Mon, 18 Dec 2006 14:48:46 +0000 (+0000) Subject: Added encryptio boolena indicator to silc_cipher_set_key. X-Git-Tag: silc.client.1.1.beta1~98 X-Git-Url: http://git.silcnet.org/gitweb/?p=silc.git;a=commitdiff_plain;h=35cdce343e56b7bae588a4fe7ba0bf8615a48170 Added encryptio boolena indicator to silc_cipher_set_key. --- diff --git a/lib/silccrypt/Makefile.ad b/lib/silccrypt/Makefile.ad index 56de2636..7f369d09 100644 --- a/lib/silccrypt/Makefile.ad +++ b/lib/silccrypt/Makefile.ad @@ -24,7 +24,7 @@ if SILC_I486 SILC_AES_S = aes_x86.asm aes.c endif if SILC_X86_64 -SILC_AES_S = aes_x86_84.asm aes.c +SILC_AES_S = aes_x86_64.asm aes.c endif else SILC_AES_S = aes.c diff --git a/lib/silccrypt/aes.c b/lib/silccrypt/aes.c index 9fc9f1e6..d05bb768 100644 --- a/lib/silccrypt/aes.c +++ b/lib/silccrypt/aes.c @@ -45,8 +45,10 @@ SILC_CIPHER_API_SET_KEY(aes) { - aes_encrypt_key(key, keylen, &((AesContext *)context)->enc); - aes_decrypt_key(key, keylen, &((AesContext *)context)->dec); + if (encryption) + aes_encrypt_key(key, keylen, &((AesContext *)context)->u.enc); + else + aes_decrypt_key(key, keylen, &((AesContext *)context)->u.dec); return TRUE; } @@ -69,7 +71,7 @@ SILC_CIPHER_API_ENCRYPT_CBC(aes) lp32(iv)[1] ^= lp32(src)[1]; lp32(iv)[2] ^= lp32(src)[2]; lp32(iv)[3] ^= lp32(src)[3]; - aes_encrypt(iv, iv, &((AesContext *)context)->enc); + aes_encrypt(iv, iv, &((AesContext *)context)->u.enc); memcpy(dst, iv, 16); src += 16; dst += 16; @@ -88,7 +90,7 @@ SILC_CIPHER_API_DECRYPT_CBC(aes) while(nb--) { memcpy(tmp, src, 16); - aes_decrypt(src, dst, &((AesContext *)context)->dec); + aes_decrypt(src, dst, &((AesContext *)context)->u.dec); lp32(dst)[0] ^= lp32(iv)[0]; lp32(dst)[1] ^= lp32(iv)[1]; lp32(dst)[2] ^= lp32(iv)[2]; diff --git a/lib/silccrypt/rijndael_internal.h b/lib/silccrypt/rijndael_internal.h index bd6051fd..e6dcb1c3 100644 --- a/lib/silccrypt/rijndael_internal.h +++ b/lib/silccrypt/rijndael_internal.h @@ -52,8 +52,10 @@ typedef struct { } aes_decrypt_ctx; typedef struct { - aes_encrypt_ctx enc; - aes_decrypt_ctx dec; + union { + aes_encrypt_ctx enc; + aes_decrypt_ctx dec; + } u; } AesContext; #define AES_RETURN void diff --git a/lib/silccrypt/silccipher.c b/lib/silccrypt/silccipher.c index bf018c97..1832566f 100644 --- a/lib/silccrypt/silccipher.c +++ b/lib/silccrypt/silccipher.c @@ -333,9 +333,9 @@ SilcBool silc_cipher_decrypt(SilcCipher cipher, const unsigned char *src, /* Sets the key for the cipher */ SilcBool silc_cipher_set_key(SilcCipher cipher, const unsigned char *key, - SilcUInt32 keylen) + SilcUInt32 keylen, SilcBool encryption) { - return cipher->cipher->set_key(cipher->context, key, keylen); + return cipher->cipher->set_key(cipher->context, key, keylen, encryption); } /* Sets the IV (initial vector) for the cipher. */ diff --git a/lib/silccrypt/silccipher.h b/lib/silccrypt/silccipher.h index 51e389f7..c020d280 100644 --- a/lib/silccrypt/silccipher.h +++ b/lib/silccrypt/silccipher.h @@ -49,7 +49,7 @@ typedef struct SilcCipherStruct *SilcCipher; /* The default SILC Cipher object to represent any cipher in SILC. */ typedef struct { char *name; - SilcBool (*set_key)(void *, const unsigned char *, SilcUInt32); + SilcBool (*set_key)(void *, const unsigned char *, SilcUInt32, SilcBool); SilcBool (*encrypt)(void *, const unsigned char *, unsigned char *, SilcUInt32, unsigned char *); SilcBool (*decrypt)(void *, const unsigned char *, unsigned char *, @@ -72,7 +72,6 @@ extern DLLAPI const SilcCipherObject silc_default_ciphers[]; /* Default cipher in the SILC protocol */ #define SILC_DEFAULT_CIPHER "aes-256-cbc" - /* Macros */ /* Function names in SILC Crypto modules. The name of the cipher @@ -89,7 +88,8 @@ extern DLLAPI const SilcCipherObject silc_default_ciphers[]; #define SILC_CIPHER_API_SET_KEY(cipher) \ SilcBool silc_##cipher##_set_key(void *context, \ const unsigned char *key, \ - SilcUInt32 keylen) + SilcUInt32 keylen, \ + SilcBool encryption) #define SILC_CIPHER_API_ENCRYPT_CBC(cipher) \ SilcBool silc_##cipher##_encrypt_cbc(void *context, \ const unsigned char *src, \ @@ -102,12 +102,9 @@ SilcBool silc_##cipher##_decrypt_cbc(void *context, \ unsigned char *dst, \ SilcUInt32 len, \ unsigned char *iv) - - #define SILC_CIPHER_API_CONTEXT_LEN(cipher) \ SilcUInt32 silc_##cipher##_context_len() - /* Prototypes */ /****f* silccrypt/SilcCipherAPI/silc_cipher_register @@ -268,16 +265,17 @@ SilcBool silc_cipher_decrypt(SilcCipher cipher, const unsigned char *src, * SYNOPSIS * * SilcBool silc_cipher_set_key(SilcCipher cipher, const unsigned char *key, - * SilcUInt32 keylen); + * SilcUInt32 keylen, SilcBool encryption); * * DESCRIPTION * * Sets the key for the cipher. The `keylen' is the key length in - * bits. + * bits. If the `encryption' is TRUE the key is for encryption, if FALSE + * the key is for decryption. * ***/ SilcBool silc_cipher_set_key(SilcCipher cipher, const unsigned char *key, - SilcUInt32 keylen); + SilcUInt32 keylen, SilcBool encryption); /****f* silccrypt/SilcCipherAPI/silc_cipher_set_iv * @@ -359,4 +357,4 @@ SilcUInt32 silc_cipher_get_iv_len(SilcCipher cipher); ***/ const char *silc_cipher_get_name(SilcCipher cipher); -#endif +#endif /* SILCCIPHER_H */ diff --git a/lib/silccrypt/silcpk.c b/lib/silccrypt/silcpk.c index b0fd7283..7e7c234e 100644 --- a/lib/silccrypt/silcpk.c +++ b/lib/silccrypt/silcpk.c @@ -863,7 +863,7 @@ SilcBool silc_pkcs_silc_import_private_key_file(unsigned char *filedata, silc_hash_final(sha1, keymat + 16); /* Set the key to the cipher */ - silc_cipher_set_key(aes, keymat, 256); + silc_cipher_set_key(aes, keymat, 256, FALSE); /* First, verify the MAC of the private key data */ mac_len = silc_hmac_len(sha1hmac); @@ -1275,7 +1275,7 @@ silc_pkcs_silc_export_private_key_file(void *private_key, silc_hash_final(sha1, keymat + 16); /* Set the key to the cipher */ - silc_cipher_set_key(aes, keymat, 256); + silc_cipher_set_key(aes, keymat, 256, TRUE); /* Encode the buffer to be encrypted. Add padding to it too, at least block size of the cipher. */ diff --git a/lib/silcske/silcske.c b/lib/silcske/silcske.c index 6f9a568c..f554e3b0 100644 --- a/lib/silcske/silcske.c +++ b/lib/silcske/silcske.c @@ -2982,12 +2982,12 @@ SilcBool silc_ske_set_keys(SilcSKE ske, if (ske->responder) { if (ret_send_key) { silc_cipher_set_key(*ret_send_key, keymat->receive_enc_key, - keymat->enc_key_len); + keymat->enc_key_len, TRUE); silc_cipher_set_iv(*ret_send_key, keymat->receive_iv); } if (ret_receive_key) { silc_cipher_set_key(*ret_receive_key, keymat->send_enc_key, - keymat->enc_key_len); + keymat->enc_key_len, FALSE); silc_cipher_set_iv(*ret_receive_key, keymat->send_iv); } if (ret_hmac_send) @@ -2999,12 +2999,12 @@ SilcBool silc_ske_set_keys(SilcSKE ske, } else { if (ret_send_key) { silc_cipher_set_key(*ret_send_key, keymat->send_enc_key, - keymat->enc_key_len); + keymat->enc_key_len, TRUE); silc_cipher_set_iv(*ret_send_key, keymat->send_iv); } if (ret_receive_key) { silc_cipher_set_key(*ret_receive_key, keymat->receive_enc_key, - keymat->enc_key_len); + keymat->enc_key_len, FALSE); silc_cipher_set_iv(*ret_receive_key, keymat->receive_iv); } if (ret_hmac_send)