turns printfs back on
[freebsd-src/fkvm-freebsd.git] / crypto / openssl / demos / maurice / example3.c
blob03d8a20f62b39c3f9396b70243f007565278b393
1 /* NOCW */
2 /*
3 Please read the README file for condition of use, before
4 using this software.
6 Maurice Gittens <mgittens@gits.nl> January 1997
8 */
10 #include <stdio.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <sys/stat.h>
14 #include <openssl/evp.h>
16 #define STDIN 0
17 #define STDOUT 1
18 #define BUFLEN 512
19 #define INIT_VECTOR "12345678"
20 #define ENCRYPT 1
21 #define DECRYPT 0
22 #define ALG EVP_des_ede3_cbc()
24 static const char *usage = "Usage: example3 [-d] password\n";
26 void do_cipher(char *,int);
28 int main(int argc, char *argv[])
30 if ((argc == 2))
32 do_cipher(argv[1],ENCRYPT);
34 else if ((argc == 3) && !strcmp(argv[1],"-d"))
36 do_cipher(argv[2],DECRYPT);
38 else
40 fprintf(stderr,"%s", usage);
41 exit(1);
44 return 0;
47 void do_cipher(char *pw, int operation)
49 char buf[BUFLEN];
50 char ebuf[BUFLEN + 8];
51 unsigned int ebuflen; /* rc; */
52 unsigned char iv[EVP_MAX_IV_LENGTH], key[EVP_MAX_KEY_LENGTH];
53 /* unsigned int ekeylen, net_ekeylen; */
54 EVP_CIPHER_CTX ectx;
56 memcpy(iv, INIT_VECTOR, sizeof(iv));
58 EVP_BytesToKey(ALG, EVP_md5(), "salu", pw, strlen(pw), 1, key, iv);
60 EVP_CIPHER_CTX_init(&ectx);
61 EVP_CipherInit_ex(&ectx, ALG, NULL, key, iv, operation);
63 while(1)
65 int readlen = read(STDIN, buf, sizeof(buf));
67 if (readlen <= 0)
69 if (!readlen)
70 break;
71 else
73 perror("read");
74 exit(1);
78 EVP_CipherUpdate(&ectx, ebuf, &ebuflen, buf, readlen);
80 write(STDOUT, ebuf, ebuflen);
83 EVP_CipherFinal_ex(&ectx, ebuf, &ebuflen);
84 EVP_CIPHER_CTX_cleanup(&ectx);
86 write(STDOUT, ebuf, ebuflen);