Add ctrl to EVP_MD and EVP_PKEY_CTX to EVP_MD_CTX. These will be used
[openssl.git] / crypto / evp / evp.h
index 7d089fe931365e91c7e4a0d753729bf77ec41e8c..e5af71669b4c20830b84a61f4141692f34c1ea42 100644 (file)
@@ -154,73 +154,6 @@ struct evp_pkey_st
 #define EVP_PKEY_MO_ENCRYPT    0x0004
 #define EVP_PKEY_MO_DECRYPT    0x0008
 
-#if 0
-/* This structure is required to tie the message digest and signing together.
- * The lookup can be done by md/pkey_method, oid, oid/pkey_method, or
- * oid, md and pkey.
- * This is required because for various smart-card perform the digest and
- * signing/verification on-board.  To handle this case, the specific
- * EVP_MD and EVP_PKEY_METHODs need to be closely associated.
- * When a PKEY is created, it will have a EVP_PKEY_METHOD associated with it.
- * This can either be software or a token to provide the required low level
- * routines.
- */
-typedef struct evp_pkey_md_st
-       {
-       int oid;
-       EVP_MD *md;
-       EVP_PKEY_METHOD *pkey;
-       } EVP_PKEY_MD;
-
-#define EVP_rsa_md2() \
-               EVP_PKEY_MD_add(NID_md2WithRSAEncryption,\
-                       EVP_rsa_pkcs1(),EVP_md2())
-#define EVP_rsa_md5() \
-               EVP_PKEY_MD_add(NID_md5WithRSAEncryption,\
-                       EVP_rsa_pkcs1(),EVP_md5())
-#define EVP_rsa_sha0() \
-               EVP_PKEY_MD_add(NID_shaWithRSAEncryption,\
-                       EVP_rsa_pkcs1(),EVP_sha())
-#define EVP_rsa_sha1() \
-               EVP_PKEY_MD_add(NID_sha1WithRSAEncryption,\
-                       EVP_rsa_pkcs1(),EVP_sha1())
-#define EVP_rsa_ripemd160() \
-               EVP_PKEY_MD_add(NID_ripemd160WithRSA,\
-                       EVP_rsa_pkcs1(),EVP_ripemd160())
-#define EVP_rsa_mdc2() \
-               EVP_PKEY_MD_add(NID_mdc2WithRSA,\
-                       EVP_rsa_octet_string(),EVP_mdc2())
-#define EVP_dsa_sha() \
-               EVP_PKEY_MD_add(NID_dsaWithSHA,\
-                       EVP_dsa(),EVP_sha())
-#define EVP_dsa_sha1() \
-               EVP_PKEY_MD_add(NID_dsaWithSHA1,\
-                       EVP_dsa(),EVP_sha1())
-
-typedef struct evp_pkey_method_st
-       {
-       char *name;
-       int flags;
-       int type;               /* RSA, DSA, an SSLeay specific constant */
-       int oid;                /* For the pub-key type */
-       int encrypt_oid;        /* pub/priv key encryption */
-
-       int (*sign)();
-       int (*verify)();
-       struct  {
-               int (*set)();   /* get and/or set the underlying type */
-               int (*get)();
-               int (*encrypt)();
-               int (*decrypt)();
-               int (*i2d)();
-               int (*d2i)();
-               int (*dup)();
-               } pub,priv;
-       int (*set_asn1_parameters)();
-       int (*get_asn1_parameters)();
-       } EVP_PKEY_METHOD;
-#endif
-
 #ifndef EVP_MD
 struct env_md_st
        {
@@ -243,6 +176,8 @@ struct env_md_st
        int required_pkey_type[5]; /*EVP_PKEY_xxx */
        int block_size;
        int ctx_size; /* how big does the ctx->md_data need to be */
+       /* control function */
+       int (*md_ctrl)(EVP_MD_CTX *ctx, int cmd, int p1, void *p2);
        } /* EVP_MD */;
 
 typedef int evp_sign_method(int type,const unsigned char *m,
@@ -255,6 +190,39 @@ typedef int evp_verify_method(int type,const unsigned char *m,
 #define EVP_MD_FLAG_ONESHOT    0x0001 /* digest can only handle a single
                                        * block */
 
+#define EVP_MD_FLAG_PKEY_DIGEST        0x0002 /* digest is a "clone" digest used
+                                       * which is a copy of an existing
+                                       * one for a specific public key type.
+                                       * EVP_dss1() etc */
+
+/* Digest uses EVP_PKEY_METHOD for signing instead of MD specific signing */
+
+#define EVP_MD_FLAG_PKEY_METHOD_SIGNATURE      0x0004
+
+/* DigestAlgorithmIdentifier flags... */
+
+#define EVP_MD_FLAG_DIGALGID_MASK              0x0018
+
+/* NULL or absent parameter accepted. Use NULL */
+
+#define EVP_MD_FLAG_DIGALGID_NULL              0x0000
+
+/* NULL or absent parameter accepted. Use NULL for PKCS#1 otherwise absent */
+
+#define EVP_MD_FLAG_DIGALGID_ABSENT            0x0008
+
+/* Custom handling via ctrl */
+
+#define EVP_MD_FLAG_DIGALGID_CUSTOM            0x0018
+
+/* Digest ctrls */
+
+#define        EVP_MD_CTRL_DIGALGID                    0x1
+
+/* Minimum Algorithm specific ctrl value */
+
+#define        EVP_MD_CTRL_ALG_CTRL                    0x1000
+
 #define EVP_PKEY_NULL_method   NULL,NULL,{0,0,0,0}
 
 #ifndef OPENSSL_NO_DSA
@@ -295,6 +263,8 @@ struct env_md_ctx_st
        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;
        } /* EVP_MD_CTX */;
 
 /* values for EVP_MD_CTX flags */
@@ -306,6 +276,14 @@ struct env_md_ctx_st
 #define EVP_MD_CTX_FLAG_REUSE          0x0004 /* Don't free up ctx->md_data
                                                * in EVP_MD_CTX_cleanup */
 
+/* MD operational flags */
+
+#define EVP_MD_CTX_FLAG_OP_MASK                0x00f0
+
+#define EVP_MD_CTX_FLAG_OP_DIGEST      0x0000
+#define EVP_MD_CTX_FLAG_OP_SIGN                0x0010
+#define EVP_MD_CTX_FLAG_OP_VERIFY      0x0020
+
 struct evp_cipher_st
        {
        int nid;
@@ -359,6 +337,7 @@ struct evp_cipher_st
 #define        EVP_CTRL_GET_RC5_ROUNDS         0x4
 #define        EVP_CTRL_SET_RC5_ROUNDS         0x5
 #define        EVP_CTRL_RAND_KEY               0x6
+#define        EVP_CTRL_PBE_PRF_NID            0x7
 
 typedef struct evp_cipher_info_st
        {
@@ -775,6 +754,8 @@ int         EVP_PKEY_encrypt_old(unsigned char *enc_key,
                        const unsigned char *key,int key_len,
                        EVP_PKEY *pub_key);
 int            EVP_PKEY_type(int type);
+int            EVP_PKEY_id(const EVP_PKEY *pkey);
+int            EVP_PKEY_base_id(const EVP_PKEY *pkey);
 int            EVP_PKEY_bits(EVP_PKEY *pkey);
 int            EVP_PKEY_size(EVP_PKEY *pkey);
 int            EVP_PKEY_assign(EVP_PKEY *pkey,int type,void *key);
@@ -828,6 +809,8 @@ int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
                                int indent, ASN1_PCTX *pctx);
 
+int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid);
+
 int EVP_CIPHER_type(const EVP_CIPHER *ctx);
 
 /* calls methods */
@@ -853,12 +836,29 @@ void PKCS5_PBE_add(void);
 
 int EVP_PBE_CipherInit (ASN1_OBJECT *pbe_obj, const char *pass, int passlen,
             ASN1_TYPE *param, EVP_CIPHER_CTX *ctx, int en_de);
+
+/* PBE type */
+
+/* Can appear as the outermost AlgorithmIdentifier */
+#define EVP_PBE_TYPE_OUTER     0x0
+/* Is an PRF type OID */
+#define EVP_PBE_TYPE_PRF       0x1
+
+int EVP_PBE_alg_add_type(int pbe_type, int pbe_nid, int cipher_nid, int md_nid,
+            EVP_PBE_KEYGEN *keygen);
 int EVP_PBE_alg_add(int nid, const EVP_CIPHER *cipher, const EVP_MD *md,
                    EVP_PBE_KEYGEN *keygen);
+int EVP_PBE_find(int type, int pbe_nid,
+                       int *pcnid, int *pmnid, EVP_PBE_KEYGEN **pkeygen);
 void EVP_PBE_cleanup(void);
 
 #define ASN1_PKEY_ALIAS                0x1
 #define ASN1_PKEY_DYNAMIC      0x2
+#define ASN1_PKEY_SIGPARAM_NULL        0x4
+
+#define ASN1_PKEY_CTRL_PKCS7_SIGN      0x1
+#define ASN1_PKEY_CTRL_PKCS7_ENCRYPT   0x2
+#define ASN1_PKEY_CTRL_DEFAULT_MD_NID  0x3
 
 int EVP_PKEY_asn1_get_count(void);
 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_get0(int idx);
@@ -870,7 +870,7 @@ int EVP_PKEY_asn1_get0_info(int *ppkey_id, int *pkey_base_id, int *ppkey_flags,
                                const char **pinfo, const char **ppem_str,
                                        const EVP_PKEY_ASN1_METHOD *ameth);
 
-EVP_PKEY_ASN1_METHOD* EVP_PKEY_asn1_new(int id,
+EVP_PKEY_ASN1_METHOD* EVP_PKEY_asn1_new(int id, int flags,
                                        const char *pem_str, const char *info);
 void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth);
 void EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD *ameth,
@@ -899,7 +899,7 @@ void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth,
 void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth,
                void (*pkey_free)(EVP_PKEY *pkey));
 void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth,
-               void (*pkey_ctrl)(EVP_PKEY *pkey, int op,
+               int (*pkey_ctrl)(EVP_PKEY *pkey, int op,
                                                        long arg1, void *arg2));
 
 
@@ -933,11 +933,21 @@ void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth,
                                        EVP_PKEY_CTRL_MD, 0, (void *)md)
 
 #define EVP_PKEY_CTRL_MD               1
+#define EVP_PKEY_CTRL_PEER_KEY         2       
+
+#define EVP_PKEY_CTRL_PKCS7_ENCRYPT    3       
+#define EVP_PKEY_CTRL_PKCS7_DECRYPT    4       
 
 #define EVP_PKEY_ALG_CTRL              0x1000
 
+#define EVP_PKEY_FLAG_AUTOARGLEN       2
+
 const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type, ENGINE *e);
-EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey);
+EVP_PKEY_METHOD* EVP_PKEY_meth_new(int id, int flags);
+int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth);
+
+EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e);
+EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e);
 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx);
 
 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
@@ -945,6 +955,12 @@ int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
                                                const char *value);
 
+void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data);
+void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx);
+EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx);
+
+void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data);
+void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx);
 
 int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx);
 int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
@@ -967,6 +983,79 @@ int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
                        unsigned char *out, int *outlen,
                        const unsigned char *in, int inlen);
 
+int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx);
+int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer);
+int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, int *keylen);
+
+typedef int EVP_PKEY_gen_cb(EVP_PKEY_CTX *ctx);
+
+int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx);
+int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
+int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx);
+int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
+
+void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb);
+int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx);
+
+void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
+       int (*init)(EVP_PKEY_CTX *ctx));
+
+void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
+       void (*cleanup)(EVP_PKEY_CTX *ctx));
+
+void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
+       int (*paramgen_init)(EVP_PKEY_CTX *ctx),
+       int (*paramgen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey));
+
+void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
+       int (*keygen_init)(EVP_PKEY_CTX *ctx),
+       int (*keygen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey));
+
+void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
+       int (*sign_init)(EVP_PKEY_CTX *ctx),
+       int (*sign)(EVP_PKEY_CTX *ctx, unsigned char *sig, int *siglen,
+                                       const unsigned char *tbs, int tbslen));
+
+void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
+       int (*verify_init)(EVP_PKEY_CTX *ctx),
+       int (*verify)(EVP_PKEY_CTX *ctx, const unsigned char *sig, int siglen,
+                                       const unsigned char *tbs, int tbslen));
+
+void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
+       int (*verify_recover_init)(EVP_PKEY_CTX *ctx),
+       int (*verify_recover)(EVP_PKEY_CTX *ctx,
+                                       unsigned char *sig, int *siglen,
+                                       const unsigned char *tbs, int tbslen));
+
+void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
+       int (*signctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx),
+       int (*signctx)(EVP_PKEY_CTX *ctx, unsigned char *sig, int *siglen,
+                                       EVP_MD_CTX *mctx));
+
+void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
+       int (*verifyctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx),
+       int (*verifyctx)(EVP_PKEY_CTX *ctx, const unsigned char *sig,int siglen,
+                                       EVP_MD_CTX *mctx));
+
+void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
+       int (*encrypt_init)(EVP_PKEY_CTX *ctx),
+       int (*encrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, int *outlen,
+                                       const unsigned char *in, int inlen));
+
+void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
+       int (*decrypt_init)(EVP_PKEY_CTX *ctx),
+       int (*decrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, int *outlen,
+                                       const unsigned char *in, int inlen));
+
+void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
+       int (*derive_init)(EVP_PKEY_CTX *ctx),
+       int (*derive)(EVP_PKEY_CTX *ctx, unsigned char *key, int *keylen));
+
+void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
+       int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2),
+       int (*ctrl_str)(EVP_PKEY_CTX *ctx,
+                                       const char *type, const char *value));
+
 /* BEGIN ERROR CODES */
 /* The following lines are auto generated by the script mkerr.pl. Any changes
  * made after this point may be overwritten when the script is next run.
@@ -997,16 +1086,26 @@ void ERR_load_EVP_strings(void);
 #define EVP_F_EVP_PKEY2PKCS8_BROKEN                     113
 #define EVP_F_EVP_PKEY_COPY_PARAMETERS                  103
 #define EVP_F_EVP_PKEY_CTX_CTRL                                 137
+#define EVP_F_EVP_PKEY_CTX_CTRL_STR                     150
 #define EVP_F_EVP_PKEY_DECRYPT                          104
 #define EVP_F_EVP_PKEY_DECRYPT_INIT                     138
+#define EVP_F_EVP_PKEY_DECRYPT_OLD                      151
+#define EVP_F_EVP_PKEY_DERIVE                           153
+#define EVP_F_EVP_PKEY_DERIVE_INIT                      154
+#define EVP_F_EVP_PKEY_DERIVE_SET_PEER                  155
 #define EVP_F_EVP_PKEY_ENCRYPT                          105
 #define EVP_F_EVP_PKEY_ENCRYPT_INIT                     139
+#define EVP_F_EVP_PKEY_ENCRYPT_OLD                      152
 #define EVP_F_EVP_PKEY_GET1_DH                          119
 #define EVP_F_EVP_PKEY_GET1_DSA                                 120
 #define EVP_F_EVP_PKEY_GET1_ECDSA                       130
 #define EVP_F_EVP_PKEY_GET1_EC_KEY                      131
 #define EVP_F_EVP_PKEY_GET1_RSA                                 121
+#define EVP_F_EVP_PKEY_KEYGEN                           146
+#define EVP_F_EVP_PKEY_KEYGEN_INIT                      147
 #define EVP_F_EVP_PKEY_NEW                              106
+#define EVP_F_EVP_PKEY_PARAMGEN                                 148
+#define EVP_F_EVP_PKEY_PARAMGEN_INIT                    149
 #define EVP_F_EVP_PKEY_SIGN                             140
 #define EVP_F_EVP_PKEY_SIGN_INIT                        141
 #define EVP_F_EVP_PKEY_VERIFY                           142
@@ -1030,6 +1129,7 @@ void ERR_load_EVP_strings(void);
 #define EVP_R_BAD_KEY_LENGTH                            137
 #define EVP_R_BN_DECODE_ERROR                           112
 #define EVP_R_BN_PUBKEY_ERROR                           113
+#define EVP_R_BUFFER_TOO_SMALL                          155
 #define EVP_R_CIPHER_PARAMETER_ERROR                    122
 #define EVP_R_COMMAND_NOT_SUPPORTED                     147
 #define EVP_R_CTRL_NOT_IMPLEMENTED                      132
@@ -1037,6 +1137,7 @@ void ERR_load_EVP_strings(void);
 #define EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH                 138
 #define EVP_R_DECODE_ERROR                              114
 #define EVP_R_DIFFERENT_KEY_TYPES                       101
+#define EVP_R_DIFFERENT_PARAMETERS                      153
 #define EVP_R_ENCODE_ERROR                              115
 #define EVP_R_EVP_PBE_CIPHERINIT_ERROR                  119
 #define EVP_R_EXPECTING_AN_RSA_KEY                      127
@@ -1056,6 +1157,7 @@ void ERR_load_EVP_strings(void);
 #define EVP_R_NO_CIPHER_SET                             131
 #define EVP_R_NO_DIGEST_SET                             139
 #define EVP_R_NO_DSA_PARAMETERS                                 116
+#define EVP_R_NO_KEY_SET                                154
 #define EVP_R_NO_OPERATION_SET                          149
 #define EVP_R_NO_SIGN_FUNCTION_CONFIGURED               104
 #define EVP_R_NO_VERIFY_FUNCTION_CONFIGURED             105