Make EVP_CIPHER opaque and add creator/destructor/accessor/writer functions
[openssl.git] / crypto / evp / evp_locl.h
index f3414b9a7be581e7bb882d7926885c0f554be9e2..d93ea34fb3df60bb1a39e3486b085e8a76632020 100644 (file)
@@ -1,6 +1,7 @@
 /* evp_locl.h */
-/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
- * project 2000.
+/*
+ * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
+ * 2000.
  */
 /* ====================================================================
  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
@@ -10,7 +11,7 @@
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer. 
+ *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
  *
  */
 
-/* Macros to code block cipher wrappers */
+/* EVP_MD_CTX related stuff */
 
-/* Wrapper functions for each cipher mode */
+struct evp_md_ctx_st {
+    const EVP_MD *digest;
+    ENGINE *engine;             /* functional reference if 'digest' is
+                                 * ENGINE-provided */
+    unsigned long flags;
+    void *md_data;
+    /* Public key context for sign/verify */
+    EVP_PKEY_CTX *pctx;
+    /* Update function: usually copied from EVP_MD */
+    int (*update) (EVP_MD_CTX *ctx, const void *data, size_t count);
+} /* EVP_MD_CTX */ ;
 
-#define BLOCK_CIPHER_ecb_loop() \
-       unsigned int i; \
-       if(inl < 8) return 1;\
-       inl -= 8; \
-       for(i=0; i <= inl; i+=8) \
+struct evp_cipher_ctx_st {
+    const EVP_CIPHER *cipher;
+    ENGINE *engine;             /* functional reference if 'cipher' is
+                                 * ENGINE-provided */
+    int encrypt;                /* encrypt or decrypt */
+    int buf_len;                /* number we have left */
+    unsigned char oiv[EVP_MAX_IV_LENGTH]; /* original iv */
+    unsigned char iv[EVP_MAX_IV_LENGTH]; /* working iv */
+    unsigned char buf[EVP_MAX_BLOCK_LENGTH]; /* saved partial block */
+    int num;                    /* used by cfb/ofb/ctr mode */
+    /* FIXME: Should this even exist? It appears unused */
+    void *app_data;             /* application stuff */
+    int key_len;                /* May change for variable length cipher */
+    unsigned long flags;        /* Various flags */
+    void *cipher_data;          /* per EVP data */
+    int final_used;
+    int block_mask;
+    unsigned char final[EVP_MAX_BLOCK_LENGTH]; /* possible final block */
+} /* EVP_CIPHER_CTX */ ;
 
-#define BLOCK_CIPHER_func_ecb(cname, cprefix, kname) \
-static int cname##_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, unsigned int inl) \
-{\
-       BLOCK_CIPHER_ecb_loop() \
-               cprefix##_ecb_encrypt(in + i, out + i, &ctx->c.##kname, ctx->encrypt);\
-       return 1;\
-}
+int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
+                             int passlen, ASN1_TYPE *param,
+                             const EVP_CIPHER *c, const EVP_MD *md,
+                             int en_de);
 
-#define BLOCK_CIPHER_func_ofb(cname, cprefix, kname) \
-static int cname##_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, unsigned int inl) \
-{\
-       cprefix##_ofb64_encrypt(in, out, (long)inl, &ctx->c.##kname, ctx->iv, &ctx->num);\
-       return 1;\
-}
-
-#define BLOCK_CIPHER_func_cbc(cname, cprefix, kname) \
-static int cname##_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, unsigned int inl) \
-{\
-       cprefix##_cbc_encrypt(in, out, (long)inl, &ctx->c.##kname, ctx->iv, ctx->encrypt);\
-       return 1;\
-}
-
-#define BLOCK_CIPHER_func_cfb(cname, cprefix, kname) \
-static int cname##_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, unsigned int inl) \
-{\
-       cprefix##_cfb64_encrypt(in, out, (long)inl, &ctx->c.##kname, ctx->iv, &ctx->num, ctx->encrypt);\
-       return 1;\
-}
-
-#define BLOCK_CIPHER_all_funcs(cname, cprefix, kname) \
-       BLOCK_CIPHER_func_cbc(cname, cprefix, kname) \
-       BLOCK_CIPHER_func_cfb(cname, cprefix, kname) \
-       BLOCK_CIPHER_func_ecb(cname, cprefix, kname) \
-       BLOCK_CIPHER_func_ofb(cname, cprefix, kname)
-
-#define BLOCK_CIPHER_defs(cname, kstruct, \
-                               nid, block_size, key_len, iv_len, flags,\
-                                init_key, cleanup, set_asn1, get_asn1, ctrl)\
-static EVP_CIPHER cname##_cbc = {\
-       nid##_cbc, block_size, key_len, iv_len, \
-       flags | EVP_CIPH_CBC_MODE,\
-       init_key,\
-       cname##_cbc_cipher,\
-       cleanup,\
-       sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
-               sizeof((((EVP_CIPHER_CTX *)NULL)->c.##kstruct)),\
-       set_asn1, get_asn1,\
-       ctrl, \
-       NULL \
-};\
-EVP_CIPHER *EVP_##cname##_cbc(void) { return &cname##_cbc; }\
-static EVP_CIPHER cname##_cfb = {\
-       nid##_cfb64, 1, key_len, iv_len, \
-       flags | EVP_CIPH_CFB_MODE,\
-       init_key,\
-       cname##_cfb_cipher,\
-       cleanup,\
-       sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
-               sizeof((((EVP_CIPHER_CTX *)NULL)->c.##kstruct)),\
-       set_asn1, get_asn1,\
-       ctrl,\
-       NULL \
-};\
-EVP_CIPHER *EVP_##cname##_cfb(void) { return &cname##_cfb; }\
-static EVP_CIPHER cname##_ofb = {\
-       nid##_ofb64, 1, key_len, iv_len, \
-       flags | EVP_CIPH_OFB_MODE,\
-       init_key,\
-       cname##_ofb_cipher,\
-       cleanup,\
-       sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
-               sizeof((((EVP_CIPHER_CTX *)NULL)->c.##kstruct)),\
-       set_asn1, get_asn1,\
-       ctrl,\
-       NULL \
-};\
-EVP_CIPHER *EVP_##cname##_ofb(void) { return &cname##_ofb; }\
-static EVP_CIPHER cname##_ecb = {\
-       nid##_ecb, block_size, key_len, iv_len, \
-       flags | EVP_CIPH_ECB_MODE,\
-       init_key,\
-       cname##_ecb_cipher,\
-       cleanup,\
-       sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
-               sizeof((((EVP_CIPHER_CTX *)NULL)->c.##kstruct)),\
-       set_asn1, get_asn1,\
-       ctrl,\
-       NULL \
-};\
-EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; }
-
-
-
-#define IMPLEMENT_BLOCK_CIPHER(cname, kname, cprefix, kstruct, \
-                               nid, block_size, key_len, iv_len, flags, \
-                                init_key, cleanup, set_asn1, get_asn1, ctrl) \
-       BLOCK_CIPHER_all_funcs(cname, cprefix, kname) \
-       BLOCK_CIPHER_defs(cname, kstruct, nid, block_size, key_len, iv_len, flags,\
-                init_key, cleanup, set_asn1, get_asn1, ctrl) 
+struct evp_Encode_Ctx_st {
+    /* number saved in a partial encode/decode */
+    int num;
+    /*
+     * The length is either the output line length (in input bytes) or the
+     * shortest input line length that is ok.  Once decoding begins, the
+     * length is adjusted up each time a longer line is decoded
+     */
+    int length;
+    /* data to encode */
+    unsigned char enc_data[80];
+    /* number read on current line */
+    int line_num;
+    int expect_nl;
+};
 
+typedef struct evp_pbe_st EVP_PBE_CTL;
+DEFINE_STACK_OF(EVP_PBE_CTL)