Major restructuring of the internals of SILC Cipher API
[crypto.git] / lib / silccrypt / silccipher_i.h
index a148d29113a60a305b0db272ea4b674db2eeb564..e52d8f00196716e48da8b1515a76c4b99ced701d 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2007 Pekka Riikonen
+  Copyright (C) 2007 - 2008 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
 #error "Do not include this header directly"
 #endif
 
+/* The SilcCipher context.  This is not visible to application programmer.
+   It is accessible from the algorithm implementations. */
+struct SilcCipherStruct {
+  SilcCipherObject *cipher;                    /* Cipher operations */
+  void *context;                               /* Algorithm context */
+  unsigned char iv[SILC_CIPHER_MAX_IV_SIZE];   /* IV */
+  unsigned char block[SILC_CIPHER_MAX_IV_SIZE];        /* Extra block for free use */
+};
+
 /* These macros can be used to implement the SILC Crypto API and to avoid
    errors in the API these macros should be used always. */
 #define SILC_CIPHER_API_SET_KEY(name)                                  \
-  SilcBool silc_##name##_set_key(struct SilcCipherObjectStruct *cipher,        \
-                                void *context,                         \
-                                const unsigned char *key,              \
+  SilcBool silc_##name##_set_key(SilcCipher cipher,                    \
+                                struct SilcCipherObjectStruct *ops,    \
+                                void *context, void *key,              \
                                 SilcUInt32 keylen,                     \
                                 SilcBool encryption)
 #define SILC_CIPHER_API_SET_IV(name)                                   \
-  void silc_##name##_set_iv(struct SilcCipherObjectStruct *cipher,     \
+  void silc_##name##_set_iv(SilcCipher cipher,                         \
+                           struct SilcCipherObjectStruct *ops,         \
                            void *context,                              \
                            unsigned char *iv)
 #define SILC_CIPHER_API_ENCRYPT(name)                                  \
-  SilcBool silc_##name##_encrypt(struct SilcCipherObjectStruct *cipher,        \
+  SilcBool silc_##name##_encrypt(SilcCipher cipher,                    \
+                                struct SilcCipherObjectStruct *ops,    \
                                 void *context,                         \
                                 const unsigned char *src,              \
                                 unsigned char *dst,                    \
                                 SilcUInt32 len,                        \
                                 unsigned char *iv)
 #define SILC_CIPHER_API_DECRYPT(name)                                  \
-  SilcBool silc_##name##_decrypt(struct SilcCipherObjectStruct *cipher,        \
+  SilcBool silc_##name##_decrypt(SilcCipher cipher,                    \
+                                struct SilcCipherObjectStruct *ops,    \
                                 void *context,                         \
                                 const unsigned char *src,              \
                                 unsigned char *dst,                    \
                                 SilcUInt32 len,                        \
                                 unsigned char *iv)
-#define SILC_CIPHER_API_CONTEXT_LEN(name)      \
-  SilcUInt32 silc_##name##_context_len()
+#define SILC_CIPHER_API_INIT(name)                                     \
+  void *silc_##name##_init(struct SilcCipherObjectStruct *ops)
+#define SILC_CIPHER_API_UNINIT(name)                                   \
+  void silc_##name##_uninit(struct SilcCipherObjectStruct *ops,                \
+                           void *context)
 
 /* Cipher object to represent a cipher algorithm. */
 struct SilcCipherObjectStruct {
   /* Cipher name */
   char *name;
+  char *alg_name;
 
   /* Set new key.  If `encryption' is TRUE the key is for encryption,
      FALSE for decryption.  The `keylen' is in bits. */
-  SilcBool (*set_key)(struct SilcCipherObjectStruct *cipher,
-                     void *context, const unsigned char *key,
-                     SilcUInt32 keylen, SilcBool encryption);
+  SilcBool (*set_key)(SilcCipher cipher, struct SilcCipherObjectStruct *ops,
+                     void *context, void *key, SilcUInt32 keylen,
+                     SilcBool encryption);
 
   /* Set IV.  The upper layer (SilcCipher) maintains the IV.  If the algorithm
      needs to set the IV itself, this should be implemented. */
-  void (*set_iv)(struct SilcCipherObjectStruct *cipher,
+  void (*set_iv)(SilcCipher cipher, struct SilcCipherObjectStruct *ops,
                 void *context, unsigned char *iv);
 
   /* Encrypt.  The `src' and `dst' may be same pointer.  The `iv' may be
      edited inside this function. */
-  SilcBool (*encrypt)(struct SilcCipherObjectStruct *cipher,
+  SilcBool (*encrypt)(SilcCipher cipher, struct SilcCipherObjectStruct *ops,
                      void *context, const unsigned char *src,
                      unsigned char *dst, SilcUInt32 len,
                      unsigned char *iv);
 
   /* Decrypt.  The `src' and `dst' may be same pointer.  The `iv' may be
      edited inside this function. */
-  SilcBool (*decrypt)(struct SilcCipherObjectStruct *cipher,
+  SilcBool (*decrypt)(SilcCipher cipher, struct SilcCipherObjectStruct *ops,
                      void *context, const unsigned char *src,
                      unsigned char *dst, SilcUInt32 len,
                      unsigned char *iv);
 
-  /* Returns the length of the internal cipher context */
-  SilcUInt32 (*context_len)(void);
+  /* Initializes the cipher.  Returns internal cipher context.  The uninit()
+     will be called in silc_cipher_free to uninitialize the cipher and free
+     the context. */
+  void *(*init)(struct SilcCipherObjectStruct *ops);
+
+  /* Uninitialize cipher. */
+  void (*uninit)(struct SilcCipherObjectStruct *ops, void *context);
 
   unsigned int key_len   : 10;            /* Key length in bits */
   unsigned int block_len : 8;             /* Block size in bytes */