CMS 3D CMS Logo

List of all members | Public Member Functions | Private Member Functions | Private Attributes
cond::auth::Cipher Class Reference

#include <Cipher.h>

Public Member Functions

std::string b64decrypt (const std::string &input)
 
std::string b64encrypt (const std::string &input)
 
 Cipher (const std::string &key)
 
std::string decrypt (const unsigned char *input, size_t inputSize)
 
size_t encrypt (const std::string &input, unsigned char *&output)
 
 ~Cipher ()
 

Private Member Functions

size_t bf_process_alloc (const unsigned char *input, size_t input_size, unsigned char *&output, bool decrypt=false)
 

Private Attributes

BLOWFISH_CTXm_ctx
 

Detailed Description

Definition at line 13 of file Cipher.h.

Constructor & Destructor Documentation

◆ Cipher()

cond::auth::Cipher::Cipher ( const std::string &  key)
explicit

Definition at line 10 of file Cipher.cc.

10  : m_ctx(new BLOWFISH_CTX) {
11  char* k = const_cast<char*>(key.c_str());
12  Blowfish_Init(m_ctx, reinterpret_cast<unsigned char*>(k), key.size());
13 }

References Blowfish_Init(), dqmdumpme::k, crabWrapper::key, and m_ctx.

◆ ~Cipher()

cond::auth::Cipher::~Cipher ( )

Definition at line 15 of file Cipher.cc.

15 { delete m_ctx; }

Member Function Documentation

◆ b64decrypt()

std::string cond::auth::Cipher::b64decrypt ( const std::string &  input)

Definition at line 122 of file Cipher.cc.

122  {
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 }

References base64_decode_alloc, input, runTheMatrix::ret, AlCaHLTBitMon_QueryRunRegistry::string, and cond::throwException().

Referenced by cond::CredentialStore::exportAll(), cond::CredentialStore::getUserCredentials(), cond::CredentialStore::importForPrincipal(), cond::CredentialStore::listConnections(), cond::CredentialStore::resetAdmin(), cond::CredentialStore::selectForUser(), cond::CredentialStore::setPermission(), cond::CredentialStore::startSession(), cond::updateConnectionData(), cond::CredentialStore::updatePrincipal(), and cond::updatePrincipalData().

◆ b64encrypt()

std::string cond::auth::Cipher::b64encrypt ( const std::string &  input)

Definition at line 109 of file Cipher.cc.

109  {
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 }

References base64_encode_alloc(), input, MillePedeFileConverter_cfg::out, runTheMatrix::ret, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by cond::setPermissionData(), cond::updateConnectionData(), and cond::updatePrincipalData().

◆ bf_process_alloc()

size_t cond::auth::Cipher::bf_process_alloc ( const unsigned char *  input,
size_t  input_size,
unsigned char *&  output,
bool  decrypt = false 
)
private

Definition at line 17 of file Cipher.cc.

20  {
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) {
60  } else {
62  }
63  memcpy(output + i, &L, j);
64  memcpy(output + i + j, &R, j);
65  }
66 
67  return output_size;
68 }

References cms::cuda::assert(), Blowfish_Decrypt(), Blowfish_Encrypt(), mps_fire::i, input, dqmiolumiharvest::j, dttmaxenums::L, EgHLTOffHistBins_cfi::nr, convertSQLitetoXML_cfg::output, and dttmaxenums::R.

◆ decrypt()

std::string cond::auth::Cipher::decrypt ( const unsigned char *  input,
size_t  inputSize 
)

Definition at line 79 of file Cipher.cc.

79  {
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 }

References mps_fire::i, input, MillePedeFileConverter_cfg::out, runTheMatrix::ret, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by cond::auth::DecodingKey::init().

◆ encrypt()

size_t cond::auth::Cipher::encrypt ( const std::string &  input,
unsigned char *&  output 
)

Definition at line 70 of file Cipher.cc.

70  {
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 }

References input, and convertSQLitetoXML_cfg::output.

Referenced by cond::auth::DecodingKey::flush().

Member Data Documentation

◆ m_ctx

BLOWFISH_CTX* cond::auth::Cipher::m_ctx
private

Definition at line 34 of file Cipher.h.

Referenced by Cipher().

runTheMatrix.ret
ret
prodAgent to be discontinued
Definition: runTheMatrix.py:542
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
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
base64_encode_alloc
size_t base64_encode_alloc(const char *in, size_t inlen, char **out)
Definition: base64.cc:100
dqmdumpme.k
k
Definition: dqmdumpme.py:60
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
EgHLTOffHistBins_cfi.nr
nr
Definition: EgHLTOffHistBins_cfi.py:4
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
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