CMS 3D CMS Logo

Cipher.cc
Go to the documentation of this file.
3 #include <cstring>
4 // blowfish encryption
5 #include "blowfish.h"
6 // GNU base 64 encoding
7 #include "base64.h"
8 #include <cassert>
9 
11  char* k = const_cast<char*>(key.c_str());
12  Blowfish_Init(m_ctx, reinterpret_cast<unsigned char*>(k), key.size());
13 }
14 
15 cond::auth::Cipher::~Cipher() { delete m_ctx; }
16 
17 size_t cond::auth::Cipher::bf_process_alloc(const unsigned char* input,
18  size_t input_size,
19  unsigned char*& output,
20  bool decrypt) {
21  assert(input_size != 0);
22 
23  uInt32 L, R;
24  unsigned int j = sizeof(uInt32);
25 
26  unsigned int output_size = 0;
27 
28  if (!input_size) {
29  output = nullptr;
30  return 0;
31  }
32 
33  for (unsigned int i = 0; i < input_size; i += (j * 2)) {
34  output_size = i + 2 * j;
35  }
36  output = (unsigned char*)malloc(output_size);
37  memset(output, 0, output_size);
38 
39  for (unsigned int i = 0; i < input_size; i += (j * 2)) {
40  L = R = 0;
41  unsigned int nl = 0;
42  unsigned int nr = 0;
43  if (input_size > i + j) {
44  nl = j;
45  if (input_size > i + 2 * j) {
46  nr = j;
47  } else {
48  nr = input_size - i - j;
49  }
50  } else {
51  nl = input_size - i;
52  nr = 0;
53  }
54  if (nl)
55  memcpy(&L, input + i, nl);
56  if (nr)
57  memcpy(&R, input + i + j, nr);
58  if (!decrypt) {
59  Blowfish_Encrypt(m_ctx, &L, &R);
60  } else {
61  Blowfish_Decrypt(m_ctx, &L, &R);
62  }
63  memcpy(output + i, &L, j);
64  memcpy(output + i + j, &R, j);
65  }
66 
67  return output_size;
68 }
69 
70 size_t cond::auth::Cipher::encrypt(const std::string& input, unsigned char*& output) {
71  if (input.empty()) {
72  output = nullptr;
73  return 0;
74  }
75  return bf_process_alloc(reinterpret_cast<const unsigned char*>(input.c_str()), input.size(), output, false);
76  ;
77 }
78 
79 std::string cond::auth::Cipher::decrypt(const unsigned char* input, size_t inputSize) {
80  if (!inputSize)
81  return "";
82  unsigned char* out = nullptr;
83  size_t outSize = bf_process_alloc(input, inputSize, out, true);
84  size_t i = 0;
85  for (i = 0; i < outSize; i++) {
86  if (out[i] == 0)
87  break;
88  }
89 
90  char* sout = reinterpret_cast<char*>(out);
91  // the output can still contain one or more \0 chars...
92  //size_t soutSize = strlen( sout );
93  size_t soutSize = 0;
94  for (soutSize = 0; soutSize < outSize; soutSize++)
95  if (out[soutSize] == 0)
96  break;
97 
98  if (soutSize < outSize) {
99  outSize = soutSize;
100  }
101 
102  std::string ret("");
103  if (outSize)
104  ret = std::string(sout, outSize);
105  free(out);
106  return ret;
107 }
108 
110  if (input.empty())
111  return "";
112  unsigned char* out = nullptr;
113  size_t outSize = bf_process_alloc(reinterpret_cast<const unsigned char*>(input.c_str()), input.size(), out, false);
114  char* b64out = nullptr;
115  size_t b64size = base64_encode_alloc(reinterpret_cast<const char*>(out), outSize, &b64out);
116  std::string ret(b64out, b64size);
117  free(out);
118  free(b64out);
119  return ret;
120 }
121 
123  if (b64in.empty())
124  return "";
125  char* input = nullptr;
126  size_t inputSize = 0;
127  if (!base64_decode_alloc(b64in.c_str(), b64in.size(), &input, &inputSize)) {
128  throwException("Input provided is not a valid base64 string.", "Cipher::b64decrypt");
129  }
130  std::string ret = decrypt(reinterpret_cast<const unsigned char*>(input), inputSize);
131  free(input);
132  return ret;
133 }
runTheMatrix.ret
ret
prodAgent to be discontinued
Definition: runTheMatrix.py:373
dttmaxenums::L
Definition: DTTMax.h:29
mps_fire.i
i
Definition: mps_fire.py:428
input
static const std::string input
Definition: EdmProvDump.cc:48
Exception.h
convertSQLitetoXML_cfg.output
output
Definition: convertSQLitetoXML_cfg.py:72
cond::auth::Cipher::decrypt
std::string decrypt(const unsigned char *input, size_t inputSize)
Definition: Cipher.cc:79
cms::cuda::assert
assert(be >=bs)
cond::auth::Cipher::bf_process_alloc
size_t bf_process_alloc(const unsigned char *input, size_t input_size, unsigned char *&output, bool decrypt=false)
Definition: Cipher.cc:17
BLOWFISH_CTX
Definition: blowfish.h:39
cond::auth::Cipher::~Cipher
~Cipher()
Definition: Cipher.cc:15
cond::auth::Cipher::b64encrypt
std::string b64encrypt(const std::string &input)
Definition: Cipher.cc:109
cond::auth::Cipher::Cipher
Cipher(const std::string &key)
Definition: Cipher.cc:10
base64_encode_alloc
size_t base64_encode_alloc(const char *in, size_t inlen, char **out)
Definition: base64.cc:100
Cipher.h
dqmdumpme.k
k
Definition: dqmdumpme.py:60
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
base64.h
blowfish.h
EgHLTOffHistBins_cfi.nr
nr
Definition: EgHLTOffHistBins_cfi.py:4
cond::auth::Cipher::b64decrypt
std::string b64decrypt(const std::string &input)
Definition: Cipher.cc:122
cond::auth::Cipher::m_ctx
BLOWFISH_CTX * m_ctx
Definition: Cipher.h:34
Blowfish_Decrypt
void Blowfish_Decrypt(BLOWFISH_CTX *ctx, uInt32 *xl, uInt32 *xr)
Definition: blowfish.cc:207
MillePedeFileConverter_cfg.out
out
Definition: MillePedeFileConverter_cfg.py:31
cond::auth::Cipher::encrypt
size_t encrypt(const std::string &input, unsigned char *&output)
Definition: Cipher.cc:70
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
base64_decode_alloc
#define base64_decode_alloc(in, inlen, out, outlen)
Definition: base64.h:54
Blowfish_Init
void Blowfish_Init(BLOWFISH_CTX *ctx, unsigned char *key, int keyLen)
Definition: blowfish.cc:235
crabWrapper.key
key
Definition: crabWrapper.py:19
dttmaxenums::R
Definition: DTTMax.h:29
uInt32
unsigned int uInt32
Definition: blowfish.h:25
cond::throwException
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:18
Blowfish_Encrypt
void Blowfish_Encrypt(BLOWFISH_CTX *ctx, uInt32 *xl, uInt32 *xr)
Definition: blowfish.cc:181