OsslRandom::OsslRandom() : initOk(false) { FILE *fp = 0; unsigned char buff[128]; // TODO: make Windows Random Generator here #ifdef WIN32 HCRYPTPROV hCryptProv; if(!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0)) { return; } if(!CryptGenRandom(hCryptProv, sizeof(buff), buff)) { return; } CryptReleaseContext(hCryptProv, 0); #else fp = fopen("/dev/urandom", "r"); if (fp) { size_t count = fread(buff, sizeof(unsigned char), sizeof(buff), fp); if (count != sizeof(buff)) throw DsrpException("Could not initialize random number generator - small seed"); fclose(fp); } else throw DsrpException("Could not initialize random number generator"); #endif RAND_seed(buff, sizeof(buff)); initOk = true; }
bytes SrpClientAuthenticator::getSessionKey(bytes M2_from_server) { if (M2_from_server.size() == 0) throw DsrpException("SrpClientAuthenticator::getSessionKey: M2_from_server.size() == 0"); if (M2_calculated.size() == 0) throw DsrpException("SrpClientAuthenticator::getSessionKey: M2_calculated.size() == 0"); if (M2_from_server != M2_calculated) throw DsrpException("Authentification failed, bad password"); return K; }
DatagramEncryptor::DatagramEncryptor(const bytes &encryptionKey, const bytes &IV, const bytes &macKey) : aesCtr(&IV[0], IV.size(), &encryptionKey[0], encryptionKey.size()), hmac(sha1, macKey) { if (macKey.size() < sha1.outputLen()) throw DsrpException("DatagramEncryptor::DatagramEncryptor: macKey not long enough"); }
bytes OsslRandom::getRandom(unsigned int lenBytes) { if (!initOk) throw DsrpException("Could not get random number - PRNG not initialized"); if (lenBytes <= 0) throw DsrpException("Could not get random number - size is zero or negative"); unsigned char *r = (unsigned char *) malloc(lenBytes); if (r == NULL) throw DsrpException("Could not get random number - malloc() failed"); int rval = RAND_bytes(r, lenBytes); if (rval != 1) { free(r); throw DsrpException("Could not get random number"); } bytes out = Conversion::array2bytes(r, lenBytes); free(r); return out; }