CUBRID Engine  latest
encryption.c
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 
20 /*
21  * encryption.c - Encryption utilities
22  */
23 
24 #include "config.h"
25 
26 #if defined (WINDOWS)
27 #include <windows.h>
28 #include <tchar.h>
29 #include <stdio.h>
30 #elif defined (HAVE_RPC_DES_CRYPT_H)
31 #include <rpc/des_crypt.h>
32 #else // OPENSSL
33 #include "crypt_opfunc.h"
34 #include "error_code.h"
35 #endif
36 
37 #include "encryption.h"
38 #include "memory_alloc.h"
39 #include "sha1.h"
40 
41 #if defined (WINDOWS)
42 static BYTE des_Keyblob[] = {
43  0x08, 0x02, 0x00, 0x00, 0x01, 0x66, 0x00, 0x00, // BLOB header
44  0x08, 0x00, 0x00, 0x00, // key length, in bytes
45  'U', '9', 'a', '$', 'y', '1', '@', 'z' // DES key with parity
46 };
47 #else
48 static char crypt_Key[8];
49 #endif /* WINDOWS */
50 
51 /*
52  * crypt_seed - Prepares a set of seeds for the encryption algorithm
53  * return:
54  * key(in): key string
55  *
56  * Note: If no seed key is given, the built in default seed key is used.
57  * This must be called prior to calling any of the other encryption
58  * functions. For decryption, the seed key must match the key used to
59  * encrypt the original string.
60  */
61 void
62 crypt_seed (const char *key)
63 {
64  char default_key[] = { 10, 7, 1, 9, 12, 2, 11, 2, 12, 19, 1, 12, 9, 7, 11, 15 };
65 
66  /* use default key if none supplied */
67  if (key == NULL)
68  {
69  key = default_key;
70  }
71 #if defined(WINDOWS)
72  /* key size must be large than 8 byte */
73  memcpy (des_Keyblob + 12, key, 8);
74 #elif defined (HAVE_RPC_DES_CRYPT_H)
75  memcpy (crypt_Key, key, 8);
76  des_setparity (crypt_Key);
77 #else
78  memcpy (crypt_Key, key, 8);
79 #endif /* WINDOWS */
80 }
81 
82 /*
83  * crypt_encrypt_printable - combines the basic crypt_line encryption with a
84  * conversion to all capitol letters for printability
85  * return: number of chars in encrypted string
86  * line(in): line to encrypt
87  * crypt(in): encrypted output buffer
88  * maxlen(in): maximum length of buffer
89  */
90 int
91 crypt_encrypt_printable (const char *line, char *crypt, int maxlen)
92 {
93 #if defined(WINDOWS)
94  HCRYPTPROV hProv = 0;
95  HCRYPTKEY hKey = 0;
96  DWORD dwLength;
97  DWORD dwCount;
98  DWORD padding = PKCS5_PADDING;
99  DWORD cipher_mode = CRYPT_MODE_ECB;
100  BYTE pbBuffer[2048];
101 
102  /* Get handle to user default provider. */
103  if (CryptAcquireContext (&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
104  {
105  /* Import PlainText Key */
106  if (!CryptImportKey (hProv, des_Keyblob, sizeof (des_Keyblob), 0, CRYPT_EXPORTABLE, &hKey))
107  {
108  return -1;
109  }
110  else
111  {
112  /* Set DES ECB MODE */
113  if (!CryptSetKeyParam (hKey, KP_MODE, (PBYTE) & cipher_mode, 0))
114  {
115  return -1;
116  }
117 
118  /* Set Padding PKCS#5 */
119  if (!CryptSetKeyParam (hKey, KP_PADDING, (PBYTE) & padding, 0))
120  {
121  return -1;
122  }
123 
124  dwLength = (int) strlen (line);
125  memcpy (pbBuffer, line, dwLength);
126  dwCount = dwLength;
127 
128  if (!CryptEncrypt (hKey, 0, TRUE, 0, pbBuffer, &dwCount, 2048))
129  {
130  return -1;
131  }
132  memcpy (crypt, pbBuffer, dwCount);
133  crypt[dwCount] = 0x00;
134  }
135 
136  CryptReleaseContext (hProv, 0);
137  }
138  else
139  {
140  return -1;
141  }
142  return (dwCount);
143 #elif defined (HAVE_RPC_DES_CRYPT_H)
144  int outlen, inlen;
145  int i;
146  int padlen;
147  unsigned char inbuf[2048];
148  unsigned char padchar;
149 
150  inlen = strnlen (line, sizeof (inbuf));
151  strncpy ((char *) inbuf, line, inlen);
152 
153  /* Insert PKCS style padding */
154  padchar = 8 - (inlen % 8);
155  padlen = (int) padchar;
156 
157  for (i = 0; i < padlen; i++)
158  {
159  inbuf[inlen + i] = padchar;
160  }
161 
162  inlen += padlen;
163 
164  if (DES_FAILED (ecb_crypt (crypt_Key, (char *) inbuf, inlen, DES_ENCRYPT)))
165  {
166  return -1;
167  }
168  memcpy (crypt, inbuf, inlen);
169  outlen = inlen;
170 
171  crypt[outlen] = '\0';
172  return (outlen);
173 #else
174  int outlen = 0;
175  int line_len = (int) strlen (line);
176  char *dest = NULL;
177 
178  int ec = crypt_default_encrypt (NULL, line, line_len, crypt_Key, (int) sizeof crypt_Key, &dest, &outlen, DES_ECB);
179  if (ec != NO_ERROR)
180  {
181  return -1;
182  }
183 
184  memcpy (crypt, dest, outlen);
186 
187  crypt[outlen] = '\0';
188  return (outlen);
189 #endif
190 }
191 
192 /*
193  * crypt_encrypt_sha1_printable -
194  * return: number of chars in encrypted string
195  * line(in): line to hash
196  * crypt(in): hashed output buffer
197  * maxlen(in): maximum length of buffer
198  */
199 int
200 crypt_encrypt_sha1_printable (const char *line, char *crypt, int maxlen)
201 {
202  SHA1Context sha;
203  int i;
204 
205  SHA1Reset (&sha);
206  SHA1Input (&sha, (const unsigned char *) line, (int) strlen (line));
207 
208  if (!SHA1Result (&sha))
209  {
210  return -1;
211  }
212  else
213  {
214  /* convert to readable format */
215  for (i = 0; i < 5; i++)
216  {
217  sprintf (crypt + (i * 8), "%08X", sha.Message_Digest[i]);
218  }
219  crypt[40] = '\0';
220  }
221  return 40;
222 }
static char crypt_Key[8]
Definition: encryption.c:48
#define NO_ERROR
Definition: error_code.h:46
#define TRUE
Definition: broker_admin.c:49
int crypt_encrypt_printable(const char *line, char *crypt, int maxlen)
Definition: encryption.c:91
int SHA1Result(SHA1Context *context)
Definition: sha1.c:109
unsigned Message_Digest[5]
Definition: sha1.h:35
void crypt_seed(const char *key)
Definition: encryption.c:62
#define NULL
Definition: freelistheap.h:34
void SHA1Input(SHA1Context *context, const unsigned char *message_array, size_t length)
Definition: sha1.c:149
#define db_private_free_and_init(thrd, ptr)
Definition: memory_alloc.h:141
int crypt_default_encrypt(THREAD_ENTRY *thread_p, const char *src, int src_len, const char *key, int key_len, char **dest_p, int *dest_len_p, CIPHER_ENCRYPTION_TYPE enc_type)
Definition: crypt_opfunc.c:212
#define strlen(s1)
Definition: intl_support.c:43
int i
Definition: dynamic_load.c:954
int crypt_encrypt_sha1_printable(const char *line, char *crypt, int maxlen)
Definition: encryption.c:200
void SHA1Reset(SHA1Context *context)
Definition: sha1.c:75