7#ifndef MI_NEURAYLIB_ILIBRARY_AUTHENTICATION_H
8#define MI_NEURAYLIB_ILIBRARY_AUTHENTICATION_H
14#ifdef MI_PLATFORM_WINDOWS
15#include <mi/base/miwindows.h>
42 mi::base::Interface_declare<0x5a7d010a,0x2a65,0x43da,0x92,0xf2,0xcd,0xd9,0xc8,0x4b,0x10,0xd2>
68 const char* vendor_key,
Size vendor_key_length,
69 const char* secret_key,
Size secret_key_length,
87 virtual Size get_challenge(
char* buffer,
Size buffer_length) = 0;
104 virtual void submit_challenge_response(
105 const char* response,
Size response_length,
106 const char* vendor_key,
Size vendor_key_length,
107 const char* salt,
Size salt_length,
146static void generate_nonce(
char* buffer);
155static void calculate_response(
156 const char* salt,
const char* challenge,
157 const char* secret_key,
Size secret_key_length,
char* response);
164static void sha256(
const char* input,
unsigned int input_length,
char* buffer);
170 const char* vendor_key,
Size vendor_key_length,
171 const char* secret_key,
Size secret_key_length,
183 memset( &challenge[0], 0, 32);
184 if( authenticator->get_challenge( challenge,
sizeof( challenge)) >
sizeof( challenge))
188 detail::generate_nonce( salt);
191 detail::calculate_response( salt, challenge, secret_key, secret_key_length, response);
193 authenticator->submit_challenge_response(
194 response,
sizeof( response), vendor_key, vendor_key_length, salt,
sizeof( salt), count);
200void calculate_response(
201 const char* salt,
const char* challenge,
202 const char* secret_key,
Size secret_key_length,
char* response)
204 if( secret_key_length > 1024u * 10u)
207 Size total_len = 32u + 32u + secret_key_length;
208 char* buffer =
new char[total_len];
209 memcpy( buffer, salt, 32);
210 memcpy( buffer + 32, challenge, 32);
211 memcpy( buffer + 64, secret_key, secret_key_length);
212 sha256( buffer,
static_cast<Uint32>( total_len), response);
216void generate_nonce(
char* buffer)
221#ifdef MI_PLATFORM_WINDOWS
222 srand( GetTickCount());
224 QueryPerformanceCounter( &tmp);
225 number += tmp.QuadPart + GetCurrentProcessId() + GetTickCount();
229 srand(
static_cast<unsigned>( time( 0)));
231 gettimeofday( &tv, 0);
232 number +=
static_cast<Uint64>( tv.tv_sec + tv.tv_usec);
236 memcpy( buf, &r,
sizeof(
Uint32));
237 memcpy( buf +
sizeof(
Uint32), &number,
sizeof(
Uint64));
239 number +=
static_cast<Uint64>( rand());
241 sha256( buf,
static_cast<Uint32>(
sizeof( buf)), buffer);
246static const Uint32 sha256_constants[] = {
247 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
248 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
249 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
250 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
251 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
252 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
253 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
254 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
260 return ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) |
261 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24));
267 return (( x >> y) | (x << (32-y)));
270void sha256(
const char* input,
unsigned int input_length,
char* buffer)
272 if( (input_length <= 0) || (input == 0) || (buffer == 0))
276 Uint32 state[] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
277 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
280 unsigned int r = (input_length * 8 + 1) % 512;
281 unsigned int k = r > 448 ? 960 - r : 448 - r;
283 unsigned int pos = 0;
284 for(
unsigned int chunk = 0; k != 0; ++chunk) {
288 unsigned int to_copy = input_length - pos;
289 to_copy = to_copy > 64 ? 64 : to_copy;
291 memcpy( ptr, input + pos, to_copy);
296 if( pos == input_length) {
299 ptr[pos%64] |=
static_cast<Uint8>( 0x80);
301 if( (pos*8 + 1 + k) - (chunk*512) <= 448) {
302 Uint64 value = input_length * 8;
303 ptr =
reinterpret_cast<Uint8*
>(&W[14]);
307 ptr[0] =
static_cast<Uint8>((value >> 56) & 0xff);
308 ptr[1] =
static_cast<Uint8>((value >> 48) & 0xff);
309 ptr[2] =
static_cast<Uint8>((value >> 40) & 0xff);
310 ptr[3] =
static_cast<Uint8>((value >> 32) & 0xff);
311 ptr[4] =
static_cast<Uint8>((value >> 24) & 0xff);
312 ptr[5] =
static_cast<Uint8>((value >> 16) & 0xff);
313 ptr[6] =
static_cast<Uint8>((value >> 8) & 0xff);
314 ptr[7] =
static_cast<Uint8>( value & 0xff);
320 for(
int i = 0; i < 16; ++i)
321 W[i] = flip32( W[i]);
324 for(
Uint32 i = 16; i < 64; ++i) {
325 Uint32 s0 = rightrotate( W[i-15], 7) ^ rightrotate( W[i-15], 18) ^ (W[i-15] >> 3);
326 Uint32 s1 = rightrotate( W[i-2], 17) ^ rightrotate( W[i- 2], 19) ^ (W[i-2] >> 10);
327 W[i] = W[i-16] + s0 + W[i-7] + s1;
340 for(
Uint32 j = 0; j < 64; ++j) {
341 Uint32 s0 = rightrotate( a, 2) ^ rightrotate( a, 13) ^ rightrotate( a, 22);
342 Uint32 maj = (a & b) ^ (a & c) ^ (b & c);
344 Uint32 s1 = rightrotate( e, 6) ^ rightrotate( e, 11) ^ rightrotate( e, 25);
345 Uint32 ch = (e & f) ^ ((~e) & g);
346 Uint32 t1 = h + s1 + ch + sha256_constants[j] + W[j];
348 h = g; g = f; f = e; e = d + t1;
349 d = c; c = b; b = a; a = t1 + t2;
353 state[0] += a; state[1] += b; state[2] += c; state[3] += d;
354 state[4] += e; state[5] += f; state[6] += g; state[7] += h;
358 for(
int i = 0; i < 8; ++i)
359 state[i] = flip32( state[i]);
361 memcpy( buffer,
reinterpret_cast<char*
>( state), 32);
A simple string class.
Definition: istring.h:22
Handle class template for interfaces, automatizing the lifetime control via reference counting.
Definition: handle.h:113
Mixin class template for deriving new interface declarations.
Definition: interface_declare.h:43
This interface is used for authenticating the application against the library.
Definition: ilibrary_authentication.h:43
virtual const IString * get_last_error_message() const =0
Returns the last error message related to authentication.
static Sint32 authenticate(const INeuray *library, const char *vendor_key, Size vendor_key_length, const char *secret_key, Size secret_key_length, Sint32 count=1)
Convenience function implementing the full library authentication.
Definition: ilibrary_authentication.h:168
virtual Uint64 get_trial_seconds_left() const =0
Returns the number of seconds left for time-limited licenses.
virtual bool is_trial_license() const =0
Indicates whether the license provided for authentication is a time-limited license.
virtual const IString * get_host_id() const =0
Returns the host ID of the machine the program is running on.
This is an object representing the Iray library.
Definition: ineuray.h:44
virtual base::IInterface * get_api_component(const base::Uuid &uuid) const =0
Returns an API component from the Iray SDK API.
bool is_valid_interface() const
Returns true if the interface is valid.
Definition: handle.h:291
unsigned long long Uint64
64-bit unsigned integer.
Definition: types.h:62
unsigned int Uint32
32-bit unsigned integer.
Definition: types.h:49
unsigned char Uint8
8-bit unsigned integer.
Definition: types.h:47
Uint64 Size
Unsigned integral type that is large enough to hold the size of all types.
Definition: types.h:112
signed int Sint32
32-bit signed integer.
Definition: types.h:46
Smart-pointer handle class for interfaces, const and non-const version.
Main Iray SDK API interface.
Mixin class template for deriving new interface declarations.
Common namespace for APIs of NVIDIA Advanced Rendering Center GmbH.
Definition: neuraylib.h:179