1 /* ====================================================================
2  * Copyright (c) 2002 Johnny Shelley.  All rights reserved.
3  *
4  * Bcrypt is licensed under the BSD software license. See the file
5  * called 'LICENSE' that you should have received with this software
6  * for details
7  * ====================================================================
8  */
9 
10 #include "includes.h"
11 #include "defines.h"
12 #include "functions.h"
13 
BFEncrypt(char ** input,char * key,uLong sz,BCoptions * options)14 uLong BFEncrypt(char **input, char *key, uLong sz, BCoptions *options) {
15   uInt32 L, R;
16   uLong i;
17   BLOWFISH_CTX ctx;
18   int j;
19   unsigned char *myEndian = NULL;
20   j = sizeof(uInt32);
21 
22   getEndian(&myEndian);
23 
24   memmove(*input+2, *input, sz);
25 
26   memcpy(*input, myEndian, 1);
27   memcpy(*input+1, &options->compression, 1);
28 
29   sz += 2;    /* add room for endian and compress flags */
30 
31   Blowfish_Init (&ctx, key, MAXKEYBYTES);
32 
33   for (i = 2; i < sz; i+=(j*2)) {	/* start just after tags */
34     memcpy(&L, *input+i, j);
35     memcpy(&R, *input+i+j, j);
36     Blowfish_Encrypt(&ctx, &L, &R);
37     memcpy(*input+i, &L, j);
38     memcpy(*input+i+j, &R, j);
39   }
40 
41   if (options->compression == 1) {
42     if ((*input = realloc(*input, sz + j + 1)) == NULL)
43       memerror();
44 
45     memset(*input+sz, 0, j + 1);
46     memcpy(*input+sz, &options->origsize, j);
47     sz += j;  /* make room for the original size      */
48   }
49 
50   free(myEndian);
51   return(sz);
52 }
53 
BFDecrypt(char ** input,char * key,char * key2,uLong sz,BCoptions * options)54 uLong BFDecrypt(char **input, char *key, char *key2, uLong sz,
55 	BCoptions *options) {
56   uInt32 L, R;
57   uLong i;
58   BLOWFISH_CTX ctx;
59   int j, swap = 0;
60   unsigned char *myEndian = NULL;
61   char *mykey = NULL;
62 
63   j = sizeof(uInt32);
64 
65   if ((mykey = malloc(MAXKEYBYTES + 1)) == NULL)
66     memerror();
67 
68   memset(mykey, 0, MAXKEYBYTES + 1);
69 
70   if ((swap = testEndian(*input)) == 1)
71     memcpy(mykey, key2, MAXKEYBYTES);
72   else
73     memcpy(mykey, key, MAXKEYBYTES);
74 
75   memcpy(&options->compression, *input+1, 1);
76 
77   if (options->compression == 1) {
78     memcpy(&options->origsize, *input+(sz - j), j);
79     sz -= j;  /* dump the size tag    */
80   }
81 
82   sz -= 2;    /* now dump endian and compress flags   */
83 
84   Blowfish_Init (&ctx, mykey, MAXKEYBYTES);
85 
86   for (i = 0; i < sz; i+=(j*2)) {
87     memcpy(&L, *input+i+2, j);
88     memcpy(&R, *input+i+j+2, j);
89 
90     if (swap == 1) {
91       L = swapEndian(L);
92       R = swapEndian(R);
93     }
94 
95     Blowfish_Decrypt(&ctx, &L, &R);
96 
97     if (swap == 1) {
98       L = swapEndian(L);
99       R = swapEndian(R);
100     }
101 
102     memcpy(*input+i, &L, j);
103     memcpy(*input+i+j, &R, j);
104   }
105 
106   while (memcmp(*input+(sz-1), "\0", 1) == 0)  /* strip excess nulls   */
107     sz--;                                       /* from decrypted files */
108 
109   sz -= MAXKEYBYTES;
110 
111   if (memcmp(*input+sz, mykey, MAXKEYBYTES) != 0)
112     return(0);
113 
114   free(mykey);
115   free(myEndian);
116   return(sz);
117 }
118