/**
  Add entropy to the PRNG state
  @param in       The data to add
  @param inlen    Length of the data to add
  @param prng     PRNG state to update
  @return CRYPT_OK if successful
*/  
int sober128_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng)
{
    struct sober128_prng *c;
    ulong32               i, k;

    LTC_ARGCHK(in != NULL);
    LTC_ARGCHK(prng != NULL);
    c = &(prng->sober128);

    if (c->flag == 1) {
       /* this is the first call to the add_entropy so this input is the key */
       /* inlen must be multiple of 4 bytes */
       if ((inlen & 3) != 0) {
          return CRYPT_INVALID_KEYSIZE;
       }
    
       for (i = 0; i < inlen; i += 4) {
           k = BYTE2WORD((unsigned char *)&in[i]);
          ADDKEY(k);
          cycle(c->R);
          XORNL(nltap(c));
       }

       /* also fold in the length of the key */
       ADDKEY(inlen);

       /* now diffuse */
       s128_diffuse(c);

       s128_genkonst(c);
       s128_savestate(c);
       c->nbuf = 0;
       c->flag = 0;       
       c->set  = 1;
    } else {
       /* ok we are adding an IV then... */
       s128_reloadstate(c);

       /* inlen must be multiple of 4 bytes */
       if ((inlen & 3) != 0) {
          return CRYPT_INVALID_KEYSIZE;
       }
    
       for (i = 0; i < inlen; i += 4) {
           k = BYTE2WORD((unsigned char *)&in[i]);
          ADDKEY(k);
          cycle(c->R);
          XORNL(nltap(c));
       }

       /* also fold in the length of the key */
       ADDKEY(inlen);

       /* now diffuse */
       s128_diffuse(c);
       c->nbuf = 0;
    }

    return CRYPT_OK;
}
Esempio n. 2
0
/**
  Set IV to the Sober128 state
  @param c       The Sober12820 state
  @param iv      The IV data to add
  @param ivlen   The length of the IV (must be 12)
  @return CRYPT_OK on success
 */
int sober128_stream_setiv(sober128_state *c, const unsigned char *iv, unsigned long ivlen)
{
   ulong32 i, k;

   LTC_ARGCHK(c  != NULL);
   LTC_ARGCHK(iv != NULL);
   LTC_ARGCHK(ivlen > 0);

   /* ok we are adding an IV then... */
   s128_reloadstate(c);

   /* ivlen must be multiple of 4 bytes */
   if ((ivlen & 3) != 0) {
      return CRYPT_INVALID_KEYSIZE;
   }

   for (i = 0; i < ivlen; i += 4) {
      k = BYTE2WORD((unsigned char *)&iv[i]);
      ADDKEY(k);
      cycle(c->R);
      XORNL(nltap(c));
   }

   /* also fold in the length of the key */
   ADDKEY(ivlen);

   /* now diffuse */
   s128_diffuse(c);
   c->nbuf = 0;

   return CRYPT_OK;
}
Esempio n. 3
0
int sober128_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng)
{
    struct sober128_prng *c;
    ulong32               i, k;

    c = &(prng->sober128);

    if (c->flag == 1) {
       /* this is the first call to the add_entropy so this input is the key */
       /* len must be multiple of 4 bytes */
       assert ((len & 3) == 0);

       for (i = 0; i < len; i += 4) {
           k = BYTE2WORD(&buf[i]);
          ADDKEY(k);
          cycle(c->R);
          XORNL(nltap(c));
       }

       /* also fold in the length of the key */
       ADDKEY(len);

       /* now diffuse */
       s128_diffuse(c);

       s128_genkonst(c);
       s128_savestate(c);
       c->nbuf = 0;
       c->flag = 0;
       c->set  = 1;
    } else {
       /* ok we are adding an IV then... */
       s128_reloadstate(c);

       /* len must be multiple of 4 bytes */
       assert ((len & 3) == 0);

       for (i = 0; i < len; i += 4) {
           k = BYTE2WORD(&buf[i]);
          ADDKEY(k);
          cycle(c->R);
          XORNL(nltap(c));
       }

       /* also fold in the length of the key */
       ADDKEY(len);

       /* now diffuse */
       s128_diffuse(c);
       c->nbuf = 0;
    }

    return CRYPT_OK;
}
Esempio n. 4
0
/* Having accumulated a MAC, finish processing and return it */
void
s128_finish(s128_ctx *c, UCHAR *buf, int nbytes)
{
    UCHAR       *endbuf;
    WORD	t = 0;

    if ((nbytes & 3) != 0)
	abort();
    /* perturb the state to mark end of input -- sort of like adding more key */
    ADDKEY(INITKONST);
    cycle(c->R);
    XORNL(nltap(c));
    s128_diffuse(c);
    /* don't bother optimising this loop, because it's a state
     * of delusion to generate more than N words of MAC.
     */
    endbuf = &buf[nbytes];
    while (buf < endbuf)
    {
	cycle(c->R);
	t = nltap(c);
	WORD2BYTE(t, buf);
	buf += 4;
    }
}
Esempio n. 5
0
/**
   Initialize an Sober128 context (only the key)
   @param c         [out] The destination of the Sober128 state
   @param key       The secret key
   @param keylen    The length of the secret key (octets)
   @return CRYPT_OK if successful
*/
int sober128_stream_setup(sober128_state *c, const unsigned char *key, unsigned long keylen)
{
   ulong32 i, k;

   LTC_ARGCHK(c   != NULL);
   LTC_ARGCHK(key != NULL);
   LTC_ARGCHK(keylen > 0);

   /* keylen must be multiple of 4 bytes */
   if ((keylen & 3) != 0) {
      return CRYPT_INVALID_KEYSIZE;
   }

   /* Register initialised to Fibonacci numbers */
   c->R[0] = 1;
   c->R[1] = 1;
   for (i = 2; i < N; ++i) {
      c->R[i] = c->R[i-1] + c->R[i-2];
   }
   c->konst = INITKONST;

   for (i = 0; i < keylen; i += 4) {
      k = BYTE2WORD((unsigned char *)&key[i]);
      ADDKEY(k);
      cycle(c->R);
      XORNL(nltap(c));
   }

   /* also fold in the length of the key */
   ADDKEY(keylen);

   /* now diffuse */
   s128_diffuse(c);
   s128_genkonst(c);
   s128_savestate(c);
   c->nbuf = 0;

   return CRYPT_OK;
}
Esempio n. 6
0
static void
s128_loadkey(s128_ctx *c, UCHAR key[], int keylen)
{
    int		i;
    WORD	k;

    /* start folding in key, reject odd sized keys */
    if ((keylen & 3) != 0)
	abort();
    for (i = 0; i < keylen; i += 4)
    {
	k = BYTE2WORD(&key[i]);
	ADDKEY(k);
        cycle(c->R);
	XORNL(nltap(c));
    }

    /* also fold in the length of the key */
    ADDKEY(keylen);

    /* now diffuse */
    s128_diffuse(c);
}
Esempio n. 7
0
void
ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud)
{
	int mod, omod;
	u_int16_t ibuf[MAXKEYS];	/* chars events */
	int s;
	int nkeys, i, j;
	int key;
#define ADDKEY(c) ibuf[nkeys++] = (c)

#ifdef UKBD_DEBUG
	/*
	 * Keep a trace of the last events.  Using printf changes the
	 * timing, so this can be useful sometimes.
	 */
	if (ukbdtrace) {
		struct ukbdtraceinfo *p = &ukbdtracedata[ukbdtraceindex];
		p->unit = sc->sc_hdev.sc_dev.dv_unit;
		microtime(&p->tv);
		p->ud = *ud;
		if (++ukbdtraceindex >= UKBDTRACESIZE)
			ukbdtraceindex = 0;
	}
	if (ukbddebug > 5) {
		struct timeval tv;
		microtime(&tv);
		DPRINTF((" at %lu.%06lu  mod=0x%02x key0=0x%02x key1=0x%02x "
			 "key2=0x%02x key3=0x%02x\n",
			 tv.tv_sec, tv.tv_usec,
			 ud->modifiers, ud->keycode[0], ud->keycode[1],
			 ud->keycode[2], ud->keycode[3]));
	}
#endif

	if (ud->keycode[0] == KEY_ERROR) {
		DPRINTF(("ukbd_intr: KEY_ERROR\n"));
		return;		/* ignore  */
	}
	nkeys = 0;
	mod = ud->modifiers;
	omod = sc->sc_odata.modifiers;
	if (mod != omod)
		for (i = 0; i < sc->sc_nmod; i++)
			if (( mod & sc->sc_mods[i].mask) !=
			    (omod & sc->sc_mods[i].mask))
				ADDKEY(sc->sc_mods[i].key |
				       (mod & sc->sc_mods[i].mask
					  ? PRESS : RELEASE));
	if (memcmp(ud->keycode, sc->sc_odata.keycode, sc->sc_nkeycode) != 0) {
		/* Check for released keys. */
		for (i = 0; i < sc->sc_nkeycode; i++) {
			key = sc->sc_odata.keycode[i];
			if (key == 0)
				continue;
			for (j = 0; j < sc->sc_nkeycode; j++)
				if (key == ud->keycode[j])
					goto rfound;
			DPRINTFN(3,("ukbd_intr: relse key=0x%02x\n", key));
			ADDKEY(key | RELEASE);
		rfound:
			;
		}

		/* Check for pressed keys. */
		for (i = 0; i < sc->sc_nkeycode; i++) {
			key = ud->keycode[i];
			if (key == 0)
				continue;
			for (j = 0; j < sc->sc_nkeycode; j++)
				if (key == sc->sc_odata.keycode[j])
					goto pfound;
			DPRINTFN(2,("ukbd_intr: press key=0x%02x\n", key));
			ADDKEY(key | PRESS);
		pfound:
			;
		}
	}
	sc->sc_odata = *ud;

	if (nkeys == 0)
		return;

	if (sc->sc_polling) {
		DPRINTFN(1,("ukbd_intr: pollchar = 0x%03x\n", ibuf[0]));
		memcpy(sc->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t));
		sc->sc_npollchar = nkeys;
		return;
	}
#ifdef WSDISPLAY_COMPAT_RAWKBD
	if (sc->sc_rawkbd) {
		u_char cbuf[MAXKEYS * 2];
		int c;
		int npress;

		for (npress = i = j = 0; i < nkeys; i++) {
			key = ibuf[i];
			c = ukbd_trtab[key & CODEMASK];
			if (c == NN)
				continue;
			if (c & 0x80)
				cbuf[j++] = 0xe0;
			cbuf[j] = c & 0x7f;
			if (key & RELEASE)
				cbuf[j] |= 0x80;
			else {
				/* remember pressed keys for autorepeat */
				if (c & 0x80)
					sc->sc_rep[npress++] = 0xe0;
				sc->sc_rep[npress++] = c & 0x7f;
			}
			DPRINTFN(1,("ukbd_intr: raw = %s0x%02x\n",
				    c & 0x80 ? "0xe0 " : "",
				    cbuf[j]));
			j++;
		}
		s = spltty();
		wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
		splx(s);
		if (npress != 0) {
			sc->sc_nrep = npress;
			timeout_add(&sc->sc_rawrepeat_ch,
			    hz * REP_DELAY1 / 1000);
		} else
			timeout_del(&sc->sc_rawrepeat_ch);
		return;
	}
#endif

	s = spltty();
	for (i = 0; i < nkeys; i++) {
		key = ibuf[i];
		wskbd_input(sc->sc_wskbddev,
		    key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
		    key&CODEMASK);
	}
	splx(s);
}
Esempio n. 8
0
bool StringKeyToHKey(LPCWSTR asKey, HKEY* phkey)
{
	static struct {LPCWSTR pszKey; LPCWSTR pszAlias; HKEY hk;} pRootKeys[10]; // HKEY__HIVE_NAME_LWR, HKEY_CLASSES_ROOT..HKEY_CURRENT_USER_LOCAL_SETTINGS
	static int nRootKeysFilled;
	if (!nRootKeysFilled)
	{
		int k = 0;
		#define ADDKEY(key,alias,hkey) \
			pRootKeys[k].pszKey = key; pRootKeys[k].pszAlias = alias; pRootKeys[k++].hk = hkey;
		
		ADDKEY(HKEY__HIVE_NAME_LWR, NULL, HKEY__HIVE);
		ADDKEY(L"hkey_current_user",     L"hkcu", HKEY_CURRENT_USER);
		ADDKEY(L"hkey_classes_root",     L"hkcr", HKEY_CLASSES_ROOT);
		ADDKEY(L"hkey_local_machine",    L"hklm", HKEY_LOCAL_MACHINE);
		ADDKEY(L"hkey_users",            L"hku",  HKEY_USERS);
		ADDKEY(L"hkey_performance_data", NULL,    HKEY_PERFORMANCE_DATA);
		ADDKEY(L"hkey_current_config",   NULL,    HKEY_CURRENT_CONFIG);
		ADDKEY(L"hkey_dyn_data",         NULL,    HKEY_DYN_DATA);
		ADDKEY(L"hkey_current_user_local_settings", NULL, HKEY_CURRENT_USER_LOCAL_SETTINGS);
		
		nRootKeysFilled = k;
	}
	_ASSERTE(pRootKeys[0].pszKey[0]!=0);

	#define CHK(i,c) ( (asKey[i] == (WORD)c) || (asKey[i]|0x20) == (WORD)c )
	if (!CHK(0,'h') || !CHK(1,'k'))
		return false;

	for (int k = 0; k < nRootKeysFilled; k++) {
		bool bMatch = true;
		int c = 2;
		while (asKey[c]) {
			if (!CHK(c, pRootKeys[k].pszKey[c])) {
				bMatch = false; break;
			}
			c++;
		}
		if (bMatch && pRootKeys[k].pszKey[c] == 0) {
			if (phkey) *phkey = pRootKeys[k].hk;
			return true;
		}
		// Alias
		if (pRootKeys[k].pszAlias)
		{
			bMatch = true;
			c = 2;
			while (asKey[c]) {
				if (!CHK(c, pRootKeys[k].pszAlias[c])) {
					bMatch = false; break;
				}
				c++;
			}
			if (bMatch && pRootKeys[k].pszAlias[c] == 0) {
				if (phkey) *phkey = pRootKeys[k].hk;
				return true;
			}
		}
	}
	
	return false;

	//if (!lstrcmpiW(asKey, L"HKCR") || !lstrcmpiW(asKey, L"HKEY_CLASSES_ROOT")) {
	//	if (phkey) *phkey = HKEY_CLASSES_ROOT;
	//} else
	//if (!lstrcmpiW(asKey, L"HKCU") || !lstrcmpiW(asKey, L"HKEY_CURRENT_USER")) {
	//	if (phkey) *phkey = HKEY_CURRENT_USER;
	//} else
	//if (!lstrcmpiW(asKey, L"HKLM") || !lstrcmpiW(asKey, L"HKEY_LOCAL_MACHINE")) {
	//	if (phkey) *phkey = HKEY_LOCAL_MACHINE;
	//} else
	//if (!lstrcmpiW(asKey, L"HKU") || !lstrcmpiW(asKey, L"HKEY_USERS")) {
	//	if (phkey) *phkey = HKEY_USERS;
	//} else
	//if (!lstrcmpiW(asKey, L"HKCC") || !lstrcmpiW(asKey, L"HKEY_CURRENT_CONFIG")) {
	//	if (phkey) *phkey = HKEY_CURRENT_CONFIG;
	//} else
	//if (!lstrcmpiW(asKey, L"HKEY_PERFORMANCE_DATA")) {
	//	if (phkey) *phkey = HKEY_PERFORMANCE_DATA;
	//} else
	//if (!lstrcmpiW(asKey, HKEY__HIVE_NAME)) {
	//	if (phkey) *phkey = HKEY__HIVE;
	//} else {
	//	return false;
	//}
	//return true;
}
Esempio n. 9
0
static void
btkbd_input(struct bthidev *hidev, uint8_t *data, int len)
{
	struct btkbd_softc *sc = (struct btkbd_softc *)hidev;
	struct btkbd_data *ud = &sc->sc_ndata;
	uint16_t ibuf[MAXKEYS];
	uint32_t mod, omod;
	int nkeys, i, j;
	int key;
	int s;

	if (sc->sc_wskbd == NULL || sc->sc_enabled == 0)
		return;

	/* extract key modifiers */
	ud->modifiers = 0;
	for (i = 0 ; i < sc->sc_nmod ; i++)
		if (hid_get_data(data, &sc->sc_modloc[i]))
			ud->modifiers |= sc->sc_mods[i].mask;

	/* extract keycodes */
	memcpy(ud->keycode, data + (sc->sc_keycodeloc.pos / 8),
	       sc->sc_nkeycode);

	if (ud->keycode[0] == KEY_ERROR)
		return;		/* ignore  */

	nkeys = 0;
	mod = ud->modifiers;
	omod = sc->sc_odata.modifiers;
	if (mod != omod)
		for (i = 0 ; i < sc->sc_nmod ; i++)
			if ((mod & sc->sc_mods[i].mask) !=
			    (omod & sc->sc_mods[i].mask))
				ADDKEY(sc->sc_mods[i].key |
				       (mod & sc->sc_mods[i].mask
					  ? PRESS : RELEASE));

	if (memcmp(ud->keycode, sc->sc_odata.keycode, sc->sc_nkeycode) != 0) {
		/* Check for released keys. */
		for (i = 0 ; i < sc->sc_nkeycode ; i++) {
			key = sc->sc_odata.keycode[i];
			if (key == 0)
				continue;

			for (j = 0 ; j < sc->sc_nkeycode ; j++)
				if (key == ud->keycode[j])
					goto rfound;

			ADDKEY(key | RELEASE);

		rfound:
			;
		}

		/* Check for pressed keys. */
		for (i = 0 ; i < sc->sc_nkeycode ; i++) {
			key = ud->keycode[i];
			if (key == 0)
				continue;

			for (j = 0; j < sc->sc_nkeycode; j++)
				if (key == sc->sc_odata.keycode[j])
					goto pfound;

			ADDKEY(key | PRESS);
		pfound:
			;
		}
	}
	sc->sc_odata = *ud;

	if (nkeys == 0)
		return;

#ifdef WSDISPLAY_COMPAT_RAWKBD
	if (sc->sc_rawkbd) {
		u_char cbuf[MAXKEYS * 2];
		int c;
		int npress;

		for (npress = i = j = 0 ; i < nkeys ; i++) {
			key = ibuf[i];
			c = btkbd_trtab[key & CODEMASK];
			if (c == NN)
				continue;

			if (c & 0x80)
				cbuf[j++] = 0xe0;

			cbuf[j] = c & 0x7f;
			if (key & RELEASE)
				cbuf[j] |= 0x80;
#ifdef BTKBD_REPEAT
			else {
				/* remember pressed keys for autorepeat */
				if (c & 0x80)
					sc->sc_rep[npress++] = 0xe0;

				sc->sc_rep[npress++] = c & 0x7f;
			}
#endif

			j++;
		}

		s = spltty();
		wskbd_rawinput(sc->sc_wskbd, cbuf, j);
		splx(s);
#ifdef BTKBD_REPEAT
		callout_stop(&sc->sc_repeat);
		if (npress != 0) {
			sc->sc_nrep = npress;
			callout_schedule(&sc->sc_repeat, hz * REP_DELAY1 / 1000);
		}
#endif
		return;
	}
#endif

	s = spltty();
	for (i = 0 ; i < nkeys ; i++) {
		key = ibuf[i];
		wskbd_input(sc->sc_wskbd,
		    key & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
		    key & CODEMASK);
	}
	splx(s);
}
Esempio n. 10
0
KeyMap::KeyMap(){
#define ADDKEY(KeyName) Keys[sf::Key::KeyName]=#KeyName;Names[#KeyName]=sf::Key::KeyName
    ADDKEY(A);
    ADDKEY(B);
    ADDKEY(C);
    ADDKEY(D);
    ADDKEY(E);
    ADDKEY(F);
    ADDKEY(G);
    ADDKEY(H);
    ADDKEY(I);
    ADDKEY(J);
    ADDKEY(K);
    ADDKEY(L);
    ADDKEY(M);
    ADDKEY(N);
    ADDKEY(O);
    ADDKEY(P);
    ADDKEY(Q);
    ADDKEY(R);
    ADDKEY(S);
    ADDKEY(T);
    ADDKEY(U);
    ADDKEY(V);
    ADDKEY(W);
    ADDKEY(X);
    ADDKEY(Y);
    ADDKEY(Z);
    ADDKEY(Num0);
    ADDKEY(Num1);
    ADDKEY(Num2);
    ADDKEY(Num3);
    ADDKEY(Num4);
    ADDKEY(Num5);
    ADDKEY(Num6);
    ADDKEY(Num7);
    ADDKEY(Num8);
    ADDKEY(Num9);
    ADDKEY(Escape);
    ADDKEY(LControl);
    ADDKEY(LShift);
    ADDKEY(LAlt);
    ADDKEY(LSystem);
    ADDKEY(RControl);
    ADDKEY(RShift);
    ADDKEY(RAlt);
    ADDKEY(RSystem);
    ADDKEY(Menu);
    ADDKEY(LBracket);
    ADDKEY(RBracket);
    ADDKEY(SemiColon);
    ADDKEY(Comma);
    ADDKEY(Period);
    ADDKEY(Quote);
    ADDKEY(Slash);
    ADDKEY(BackSlash);
    ADDKEY(Tilde);
    ADDKEY(Equal);
    ADDKEY(Dash);
    ADDKEY(Space);
    ADDKEY(Return);
    ADDKEY(Back);
    ADDKEY(Tab);
    ADDKEY(PageUp);
    ADDKEY(PageDown);
    ADDKEY(End);
    ADDKEY(Home);
    ADDKEY(Insert);
    ADDKEY(Delete);
    ADDKEY(Add);
    ADDKEY(Subtract);
    ADDKEY(Multiply);
    ADDKEY(Divide);
    ADDKEY(Left);
    ADDKEY(Right);
    ADDKEY(Up);
    ADDKEY(Down);
    ADDKEY(Numpad0);
    ADDKEY(Numpad1);
    ADDKEY(Numpad2);
    ADDKEY(Numpad3);
    ADDKEY(Numpad4);
    ADDKEY(Numpad5);
    ADDKEY(Numpad6);
    ADDKEY(Numpad7);
    ADDKEY(Numpad8);
    ADDKEY(Numpad9);
    ADDKEY(F1);
    ADDKEY(F2);
    ADDKEY(F3);
    ADDKEY(F4);
    ADDKEY(F5);
    ADDKEY(F6);
    ADDKEY(F7);
    ADDKEY(F8);
    ADDKEY(F9);
    ADDKEY(F10);
    ADDKEY(F11);
    ADDKEY(F12);
    ADDKEY(F13);
    ADDKEY(F14);
    ADDKEY(F15);
    ADDKEY(Pause);
#undef ADDKEY
}