This commit was generated by cvs2svn to track changes on a CVS vendor
[openssl.git] / doc / cipher.m
1 From ssl-lists-owner@mincom.com Tue Oct 15 18:16:14 1996
2 Received: from cygnus.mincom.oz.au by orb.mincom.oz.au with SMTP id AA11550
3   (5.65c/IDA-1.4.4 for eay); Tue, 15 Oct 1996 08:17:41 +1000
4 Received: (from daemon@localhost) by cygnus.mincom.oz.au (8.7.5/8.7.3) id IAA12472 for ssl-users-outgoing; Tue, 15 Oct 1996 08:16:35 +1000 (EST)
5 Received: from orb.mincom.oz.au (eay@orb.mincom.oz.au [192.55.197.1]) by cygnus.mincom.oz.au (8.7.5/8.7.3) with SMTP id IAA12463 for <ssl-users@listserv.mincom.oz.au>; Tue, 15 Oct 1996 08:16:32 +1000 (EST)
6 Received: by orb.mincom.oz.au id AA11544
7   (5.65c/IDA-1.4.4 for ssl-users@listserv.mincom.oz.au); Tue, 15 Oct 1996 08:16:15 +1000
8 Date: Tue, 15 Oct 1996 08:16:14 +1000 (EST)
9 From: Eric Young <eay@mincom.com>
10 X-Sender: eay@orb
11 To: Roland Haring <rharing@tandem.cl>
12 Cc: ssl-users@mincom.com
13 Subject: Re: Symmetric encryption with ssleay
14 In-Reply-To: <m0vBpyq-00001aC@tandemnet.tandem.cl>
15 Message-Id: <Pine.SOL.3.91.961015075623.11394A-100000@orb>
16 Mime-Version: 1.0
17 Content-Type: TEXT/PLAIN; charset=US-ASCII
18 Sender: ssl-lists-owner@mincom.com
19 Precedence: bulk
20 Status: RO
21 X-Status: 
22
23
24 On Fri, 11 Oct 1996, Roland Haring wrote:
25 > THE_POINT:
26 >       Would somebody be so kind to give me the minimum basic 
27 >       calls I need to do to libcrypto.a to get some text encrypted
28 >       and decrypted again? ...hopefully with code included to do
29 >       base64 encryption and decryption ... e.g. that sign-it.c code
30 >       posted some while ago was a big help :-) (please, do not point
31 >       me to apps/enc.c where I suspect my Heissenbug to be hidden :-)
32
33 Ok, the base64 encoding stuff in 'enc.c' does the wrong thing sometimes 
34 when the data is less than a line long (this is for decoding).  I'll dig 
35 up the exact fix today and post it.  I am taking longer on 0.6.5 than I 
36 intended so I'll just post this patch.
37
38 The documentation to read is in
39 doc/cipher.doc,
40 doc/encode.doc (very sparse :-).
41 and perhaps
42 doc/digest.doc,
43
44 The basic calls to encrypt with say triple DES are
45
46 Given
47 char key[EVP_MAX_KEY_LENGTH];
48 char iv[EVP_MAX_IV_LENGTH];
49 EVP_CIPHER_CTX ctx;
50 unsigned char out[512+8];
51 int outl;
52
53 /* optional generation of key/iv data from text password using md5
54  * via an upward compatable verson of PKCS#5. */
55 EVP_BytesToKey(EVP_des_ede3_cbc,EVP_md5,NULL,passwd,strlen(passwd),
56         key,iv);
57
58 /* Initalise the EVP_CIPHER_CTX */
59 EVP_EncryptInit(ctx,EVP_des_ede3_cbc,key,iv);
60
61 while (....)
62         {
63         /* This is processing 512 bytes at a time, the bytes are being
64          * copied into 'out', outl bytes are output.  'out' should not be the
65          * same as 'in' for reasons mentioned in the documentation. */
66         EVP_EncryptUpdate(ctx,out,&outl,in,512);
67         }
68
69 /* Output the last 'block'.  If the cipher is a block cipher, the last
70  * block is encoded in such a way so that a wrong decryption will normally be
71  * detected - again, one of the PKCS standards. */
72
73 EVP_EncryptFinal(ctx,out,&outl);
74
75 To decrypt, use the EVP_DecryptXXXXX functions except that EVP_DecryptFinal()
76 will return 0 if the decryption fails (only detectable on block ciphers).
77
78 You can also use
79 EVP_CipherInit()
80 EVP_CipherUpdate()
81 EVP_CipherFinal()
82 which does either encryption or decryption depending on an extra 
83 parameter to EVP_CipherInit().
84
85
86 To do the base64 encoding,
87 EVP_EncodeInit()
88 EVP_EncodeUpdate()
89 EVP_EncodeFinal()
90
91 EVP_DecodeInit()
92 EVP_DecodeUpdate()
93 EVP_DecodeFinal()
94
95 where the encoding is quite simple, but the decoding can be a bit more 
96 fun (due to dud input).
97
98 EVP_DecodeUpdate() returns -1 for an error on an input line, 0 if the 
99 'last line' was just processed, and 1 if more lines should be submitted.
100
101 EVP_DecodeFinal() returns -1 for an error or 1 if things are ok.
102
103 So the loop becomes
104 EVP_DecodeInit(....)
105 for (;;)
106         {
107         i=EVP_DecodeUpdate(....);
108         if (i < 0) goto err;
109
110         /* process the data */
111
112         if (i == 0) break;
113         }
114 EVP_DecodeFinal(....);
115 /* process the data */
116
117 The problem in 'enc.c' is that I was stuff the processing up after the 
118 EVP_DecodeFinal(...) when the for(..) loop was not being run (one line of 
119 base64 data) and this was because 'enc.c' tries to scan over a file until
120 it hits the first valid base64 encoded line.
121
122 hope this helps a bit.
123 eric
124 --
125 Eric Young                  | BOOL is tri-state according to Bill Gates.
126 AARNet: eay@mincom.oz.au    | RTFM Win32 GetMessage().
127
128