void main(void) { // Arrays for input (plaintext) and key uint8_t input[8]; uint8_t key [16]; // Set up things for the code underneath (serial + trigger) inituart(248); // 4800 baud trigger_low(); // Print one helpful character to make the simulator happy putchar('\n'); // Let the SimpleSerial module fill in the input and key arrays while(1) { // Read data from SimpleSerial if(simpleserial_get(input, key, 8, 16)) { // We're done reading an input: encrypt in place and send back trigger_high(); tea_encrypt(input, key); trigger_low(); simpleserial_put(input, 8); } } }
void CAccountConnector::__BuildClientKey() { for (DWORD i = 0; i < 4; ++i) g_adwEncryptKey[i] = random(); const BYTE * c_pszKey = (const BYTE *) "JyTxtHljHJlVJHorRM301vf@4fvj10-v"; tea_encrypt((DWORD *) g_adwDecryptKey, (const DWORD *) g_adwEncryptKey, (const DWORD *) c_pszKey, 16); }
/** * Test implementation. */ void G_COLD tea_test(void) { tea_key_t key; tea_block_t value; tea_block_t encrypted; tea_block_t decrypted; int i; char in[80]; char out[80]; char recovered[80]; STATIC_ASSERT(sizeof(key.v) == TEA_KEY_SIZE); STATIC_ASSERT(sizeof(value.v) == TEA_BLOCK_SIZE); for (i = 0; i < 10; i++) { int j; bool randomized = FALSE; for (j = 0; j < 10; j++) { random_bytes(key.v, TEA_KEY_SIZE); random_bytes(value.v, TEA_BLOCK_SIZE); t_encrypt(&encrypted, &key, &value); if (0 != memcmp(value.v, encrypted.v, TEA_BLOCK_SIZE)) { randomized = TRUE; break; } } if (!randomized) g_error("no luck with random numbers in tea_test()"); t_decrypt(&decrypted, &key, &encrypted); if (0 != memcmp(value.v, decrypted.v, TEA_BLOCK_SIZE)) { g_error("TEA implementation tests FAILED"); return; } } STATIC_ASSERT(sizeof in == sizeof out); STATIC_ASSERT(sizeof in == sizeof recovered); random_bytes(key.v, TEA_KEY_SIZE); random_bytes(in, sizeof in); tea_encrypt(&key, out, in, sizeof in); tea_decrypt(&key, recovered, out, sizeof out); if (0 != memcmp(in, recovered, sizeof in)) g_error("TEA implementation tests FAILED"); }
/* * val 为待加密的数据 * len 为数据长度,单位Byte * */ int nm_encrypt(unsigned int *val, int len) { int i=0; #if defined(NM_TEA_CRYPT) int crypt_len = (rte_align(len, 8))/sizeof(unsigned int); for(i=0;i<crypt_len;i+=2){ tea_encrypt(val+i, tea_key); } return crypt_len*sizeof(unsigned int); #else unsigned char *ptr=(unsigned char *)val; for(i=0;i<len;i++){ if(*(ptr+i) & 0x80){ *(ptr+i) &= 0x7f; }else{ *(ptr+i) |= 0x80; } } return len; #endif }
/** * Create a security token from host address and port using specified key. * * Optionally, extra contextual data may be given (i.e. the token is not * only based on the address and port) to make the token more unique to * a specific context. * * @param stg the security token generator * @param n key index to use * @param tok where security token is written * @param addr address of the host for which we're generating a token * @param port port of the host for which we're generating a token * @param data optional contextual data * @param len length of contextual data */ static void sectoken_generate_n(sectoken_gen_t *stg, size_t n, sectoken_t *tok, host_addr_t addr, uint16 port, const void *data, size_t len) { char block[8]; char enc[8]; char *p = block; sectoken_gen_check(stg); g_assert(tok != NULL); g_assert(size_is_non_negative(n)); g_assert(n < stg->keycnt); g_assert((NULL != data) == (len != 0)); switch (host_addr_net(addr)) { case NET_TYPE_IPV4: p = poke_be32(p, host_addr_ipv4(addr)); break; case NET_TYPE_IPV6: { uint val; val = binary_hash(host_addr_ipv6(&addr), 16); p = poke_be32(p, val); } break; case NET_TYPE_LOCAL: case NET_TYPE_NONE: g_error("unexpected address for security token generation: %s", host_addr_to_string(addr)); } p = poke_be16(p, port); p = poke_be16(p, 0); /* Filler */ g_assert(p == &block[8]); STATIC_ASSERT(sizeof(tok->v) == sizeof(uint32)); STATIC_ASSERT(sizeof(block) == sizeof(enc)); tea_encrypt(&stg->keys[n], enc, block, sizeof block); /* * If they gave contextual data, encrypt them by block of 8 bytes, * filling the last partial block with zeroes if needed. */ if (data != NULL) { const void *q = data; size_t remain = len; char denc[8]; STATIC_ASSERT(sizeof(denc) == sizeof(enc)); while (remain != 0) { size_t fill = MIN(remain, 8U); unsigned i; if (fill != 8U) ZERO(&block); memcpy(block, q, fill); remain -= fill; q = const_ptr_add_offset(q, fill); /* * Encrypt block of contextual data (possibly filled with trailing * zeroes) and merge back the result into the main encryption * output with XOR. */ tea_encrypt(&stg->keys[n], denc, block, sizeof block); for (i = 0; i < sizeof denc; i++) enc[i] ^= denc[i]; } } poke_be32(tok->v, tea_squeeze(enc, sizeof enc)); }
int passwd_encrypt(FAR const char *password, char encrypted[MAX_ENCRYPTED + 1]) { union { char b[8]; uint16_t h[4]; uint32_t l[2]; } value; FAR const char *src; FAR char *bptr; FAR char *dest; uint32_t tmp; uint8_t remainder; int remaining; int converted; int gulpsize; int nbits; int i; /* How long is the password? */ remaining = strlen(password); if (remaining > MAX_PASSWORD) { return -E2BIG; } /* Convert the password in 8-byte TEA cycles */ src = password; dest = encrypted; *dest = '\0'; remainder = 0; nbits = 0; for (converted = 0; converted < remaining; converted += 8) { /* Copy bytes */ gulpsize = 8; if (gulpsize > remaining) { gulpsize = remaining; } bptr = value.b; for (i = 0; i < gulpsize; i++) { *bptr++ = *src++; } /* Pad with spaces if necessary */ for (; i < 8; i++) { *bptr++ = ' '; } /* Perform the conversion for this cycle */ tea_encrypt(value.l, g_tea_key); /* Generate the base64 output string from this cycle */ tmp = remainder; for (i = 0; i < 4; i++) { tmp = (uint32_t)value.h[i] << nbits | tmp; nbits += 16; while (nbits >= 6) { *dest++ = passwd_base64((uint8_t)(tmp & 0x3f)); tmp >>= 6; nbits -= 6; } } remainder = (uint8_t)tmp; *dest = '\0'; } /* Handle any remainder */ if (nbits > 0) { *dest++ = passwd_base64(remainder); *dest = '\0'; } return OK; }