Change EVP_MD_CTX_type so it is more logical and add EVP_MD_CTX_md for
[openssl.git] / doc / crypto / EVP_EncryptInit.pod
1 =pod
2
3 =head1 NAME
4
5 EVP_EncryptInit, EVP_EncryptUpdate, EVP_EncryptFinal - EVP cipher routines
6
7 =head1 SYNOPSIS
8
9  #include <openssl/evp.h>
10
11  void EVP_EncryptInit(EVP_CIPHER_CTX *ctx,const EVP_CIPHER *type, unsigned char *key, unsigned char *iv);
12  void EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, unsigned char *in, int inl);
13  void EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);
14
15  void EVP_DecryptInit(EVP_CIPHER_CTX *ctx,const EVP_CIPHER *type, unsigned char *key, unsigned char *iv);
16  void EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, unsigned char *in, int inl);
17  int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
18
19  void EVP_CipherInit(EVP_CIPHER_CTX *ctx,const EVP_CIPHER *type, unsigned char *key,unsigned char *iv,int enc);
20  void EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, unsigned char *in, int inl);
21  int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
22
23  void EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *a);
24
25  const EVP_CIPHER *EVP_get_cipherbyname(const char *name);
26  #define EVP_get_cipherbynid(a) EVP_get_cipherbyname(OBJ_nid2sn(a))
27  #define EVP_get_cipherbyobj(a) EVP_get_cipherbynid(OBJ_obj2nid(a))
28
29  #define EVP_CIPHER_nid(e)              ((e)->nid)
30  #define EVP_CIPHER_block_size(e)       ((e)->block_size)
31  #define EVP_CIPHER_key_length(e)       ((e)->key_len)
32  #define EVP_CIPHER_iv_length(e)                ((e)->iv_len)
33
34  int EVP_CIPHER_type(const EVP_CIPHER *ctx);
35  #define EVP_CIPHER_CTX_cipher(e)       ((e)->cipher)
36  #define EVP_CIPHER_CTX_nid(e)          ((e)->cipher->nid)
37  #define EVP_CIPHER_CTX_block_size(e)   ((e)->cipher->block_size)
38  #define EVP_CIPHER_CTX_key_length(e)   ((e)->cipher->key_len)
39  #define EVP_CIPHER_CTX_iv_length(e)    ((e)->cipher->iv_len)
40  #define EVP_CIPHER_CTX_type(c)         EVP_CIPHER_type(EVP_CIPHER_CTX_cipher(c))
41
42 =head1 DESCRIPTION
43
44 The EVP cipher routines are a high level interface to certain
45 symmetric ciphers.
46
47 EVP_EncryptInit() initialises a cipher context B<ctx> for encryption
48 with cipher B<type>. B<type> is normally supplied by a function such
49 as EVP_des_cbc() . B<key> is the symmetric key to use and B<iv> is the
50 IV to use (if necessary), the actual number of bytes used for the
51 key and IV depends on the cipher.
52
53 EVP_EncryptUpdate() encrypts B<inl> bytes from the buffer B<in> and
54 writes the encrypted version to B<out>. This function can be called
55 multiple times to encrypt successive blocks of data. The amount
56 of data written depends on the block alignment of the encrypted data:
57 as a result the amount of data written may be anything from zero bytes
58 to (inl + cipher_block_size - 1) so B<outl> should contain sufficient
59 room.  The actual number of bytes written is placed in B<outl>.
60
61 EVP_EncryptFinal() encrypts the "final" data, that is any data that
62 remains in a partial block. It uses standard block padding (aka PKCS
63 padding). The encrypted final data is written to B<out> which should
64 have sufficient space for one cipher block. The number of bytes written
65 is placed in B<outl>. After this function is called the encryption operation
66 is finished and no further calls to EVP_EncryptUpdate() should be made.
67
68 EVP_DecryptInit(), EVP_DecryptUpdate() and EVP_DecryptFinal() are the
69 corresponding decryption operations. EVP_DecryptFinal() will return an
70 error code if the final block is not correctly formatted. The parameters
71 and restrictions are identical to the encryption operations except that
72 the decrypted data buffer B<out> passed to EVP_DecryptUpdate() should
73 have sufficient room for (B<inl> + cipher_block_size) bytes unless the
74 cipher block size is 1 in which case B<inl> bytes is sufficient.
75
76 EVP_CipherInit(), EVP_CipherUpdate() and EVP_CipherFinal() are functions
77 that can be used for decryption or encryption. The operation performed
78 depends on the value of the B<enc> parameter. It should be set to 1 for
79 encryption and 0 for decryption.
80
81 EVP_CIPHER_CTX_cleanup() clears all information from a cipher context.
82 It should be called after all operations using a cipher are complete
83 so sensitive information does not remain in memory.
84
85 =head1 RETURN VALUES
86
87 EVP_EncryptInit(), EVP_EncryptUpdate() and EVP_EncryptFinal() do not return
88 values.
89
90 EVP_DecryptInit() and EVP_DecryptUpdate() do not return values.
91 EVP_DecryptFinal() returns 0 if the decrypt failed or 1 for success.
92
93 EVP_CipherInit() and EVP_CipherUpdate() do not return values.
94 EVP_CipherFinal() returns 1 for a decryption failure or 1 for success, if
95 the operation is encryption then it always returns 1.
96
97 =head1 NOTES
98
99 Where possible the B<EVP> interface to symmetric ciphers should be used in
100 preference to the low level interfaces. This is because the code then becomes
101 transparent to the cipher used and much more flexible.
102
103 PKCS padding works by adding B<n> padding bytes of value B<n> to make the total 
104 length of the encrypted data a multiple of the block size. Padding is always
105 added so if the data is already a multiple of the block size B<n> will equal
106 the block size. For example if the block size is 8 and 11 bytes are to be
107 encrypted then 5 padding bytes of value 5 will be added.
108
109 When decrypting the final block is checked to see if it has the correct form.
110
111 Although the decryption operation can produce an error, it is not a strong
112 test that the input data or key is correct. A random block has better than
113 1 in 256 chance of being of the correct format and problems with the
114 input data earlier on will not produce a final decrypt error.
115
116 =head1 BUGS
117
118 The current B<EVP> cipher interface is not as flexible as it should be. Only
119 certain "spot" encryption algorithms can be used for ciphers which have various
120 parameters associated with them (RC2, RC5 for example) this is inadequate.
121
122 Several of the functions do not return error codes because the software versions
123 can never fail. This is not true of hardware versions.
124
125 =head1 SEE ALSO
126
127 L<evp(3)|evp(3)>
128
129 =head1 HISTORY
130
131 =cut