Added encryptio boolena indicator to silc_cipher_set_key.
authorPekka Riikonen <priikone@silcnet.org>
Mon, 18 Dec 2006 14:48:46 +0000 (14:48 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Mon, 18 Dec 2006 14:48:46 +0000 (14:48 +0000)
lib/silccrypt/Makefile.ad
lib/silccrypt/aes.c
lib/silccrypt/rijndael_internal.h
lib/silccrypt/silccipher.c
lib/silccrypt/silccipher.h
lib/silccrypt/silcpk.c
lib/silcske/silcske.c

index 56de2636d99fdac10e1d7a6e40cdf1e49b7bb9b0..7f369d09d21c9047ec8c68ccb21cd68cbfa2e9d3 100644 (file)
@@ -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
index 9fc9f1e663ec0450e0f0523b35c59616e2b18a9a..d05bb7684a0bfb717d88ff1c7f0452c19e856256 100644 (file)
 
 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];
index bd6051fd3771bb5eb5141e13d9a7628412316867..e6dcb1c3a4a5fa9dca85a4d7bcbec681625581e6 100644 (file)
@@ -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
index bf018c9716f8fb1e05fc5064574c8b725d054972..1832566fec29c45286b3a926c316f63f779746c8 100644 (file)
@@ -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. */
index 51e389f7f7a8f152a3819f44960ebaffc2e4a43f..c020d280414688a7ad0579acb3ef0ac973d5636a 100644 (file)
@@ -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 */
index b0fd72835fcab0a660545a286e0b0117e8193d2e..7e7c234e72681cd6355c0e6b69aec9e51cf37215 100644 (file)
@@ -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. */
index 6f9a568c94ca1b379119501409c8311375bfeb3f..f554e3b0f6becccf430e63864f85b59d43d11df2 100644 (file)
@@ -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)