int main(void) { u8 output[64], stream[64]; ECRYPT_ctx ctx; int i; // test the permutation first for(i=0;i<64;i++) stream[i]=(i+1); for(i=0;i<2048;i++) { salsa20_wordtobyte(output, (u32*)stream); } bin2hex("permutation result", output, 64); memset(&ctx, 0, sizeof(ctx)); ECRYPT_keysetup(&ctx, tv_key, 256, 64); ECRYPT_ivsetup(&ctx, tv_nonce); for(i=0;i<2048;i++) { ECRYPT_keystream_bytes(&ctx, stream, 64); } bin2hex("stream result", stream, 64); return 0; }
void salsa20_encrypt( const fc::sha256& key, uint64_t iv, const char* plain, char* cipher, uint64_t len ) { ECRYPT_ctx ctx; ECRYPT_keysetup( &ctx, (unsigned char*)&key, ECRYPT_MAXIVSIZE, ECRYPT_MAXKEYSIZE ); ECRYPT_ivsetup( &ctx, (unsigned char*)&iv ); ECRYPT_encrypt_bytes( &ctx, (const unsigned char*)plain, (unsigned char*)cipher, len ); }
void perform_iterated_test (u8 *key) { ECRYPT_ctx ctx; /* Keystream generator context */ u8 iv[4]; /* Array to contain iv derived from keystream */ u8 keystream[16]; /* Array to contain generated keystream bytes */ int i; /* Counting variable */ /* Display the key */ printf ("Iterated test key ="); for (i=0; i<10; i++) printf (" %02x", key[i]); printf ("\n"); /* Load key */ ECRYPT_keysetup (&ctx, key, 80, 0); ECRYPT_ivsetup (&ctx, iv); for (i=0; i<1000; i++) { /* Generate new key and iv from keystream */ ECRYPT_keystream_bytes (&ctx, key, 10); ECRYPT_keystream_bytes (&ctx, iv, 4); /* Load new key */ ECRYPT_keysetup (&ctx, key, 80, 32); /* Load new IV */ ECRYPT_ivsetup (&ctx, iv); } /* Generate keystream */ ECRYPT_keystream_bytes (&ctx, keystream, 16); /* Display the derived keytream */ printf ("Final keystream ="); for (i=0; i<16; i++) printf (" %02x", keystream[i]); printf ("\n"); printf ("\n"); }
bool SymmetricCipherSalsa20::setKey(const QByteArray& key) { Q_ASSERT((key.size() == 16) || (key.size() == 32)); m_key = key; ECRYPT_keysetup(&m_ctx, reinterpret_cast<const u8*>(m_key.constData()), m_key.size()*8, 64); return true; }
void Prng::init_chacha_png(uint8_t *key, uint8_t *iv) { chacha = (ECRYPT_ctx*)aligned_malloc(sizeof(ECRYPT_ctx)); random_state = new u8[RANDOM_STATE_SIZE]; ECRYPT_keysetup(chacha, key, 256, 64); ECRYPT_ivsetup(chacha, iv); chacha_refill_randomness(); return; }
static int chacha_generic_crypt(lua_State *L, bool is_ietf) { ECRYPT_ctx ctx; const char *key, *iv, *plaintext, *counter; char *ciphertext; size_t keysize, ivsize, msglen, countersize; int rounds = luaL_checkinteger(L, 1); /* IETF only normalizes ChaCha 20. */ if (rounds != 20 && (is_ietf || (rounds != 8 && rounds != 12))) return luaL_error(L, "invalid number of rounds: %d", rounds); luaL_checktype(L, 2, LUA_TSTRING); luaL_checktype(L, 3, LUA_TSTRING); luaL_checktype(L, 4, LUA_TSTRING); key = lua_tolstring(L, 2, &keysize); iv = lua_tolstring(L, 3, &ivsize); plaintext = lua_tolstring(L, 4, &msglen); counter = luaL_optlstring(L, 5, NULL, &countersize); if (ivsize != (is_ietf ? 12 : 8)) return luaL_error(L, "invalid IV size: %dB", (int)ivsize); if (keysize != 32 && (is_ietf || keysize != 16)) return luaL_error(L, "invalid key size: %dB", (int)keysize); if (counter && countersize != (is_ietf ? 4 : 8)) return luaL_error(L, "invalid counter size: %dB", (int)countersize); if (msglen == 0) { lua_pushlstring(L, "", 0); return 1; } ciphertext = malloc(msglen); if (!ciphertext) return luaL_error(L, "OOM"); /* keysize and ivsize are in bits */ ECRYPT_keysetup(&ctx, (u8*)key, 8 * keysize, 8 * ivsize); if (is_ietf) ECRYPT_IETF_ivsetup(&ctx, (u8*)iv, (u8*)counter); else ECRYPT_ivsetup(&ctx, (u8*)iv, (u8*)counter); ECRYPT_encrypt_bytes(&ctx, (u8*)plaintext, (u8*)ciphertext, msglen, rounds); lua_pushlstring(L, ciphertext, msglen); free(ciphertext); return 1; }
static int rounds(const u8 *iv, const u8 *key, u16 num_rounds) { ECRYPT_ctx ctx = {0}; u16 rounds_32bits = num_rounds / 32; u8 state[4] = {0}; u16 i, j; ECRYPT_keysetup(&ctx, key, 80, 80); ECRYPT_ivsetup(&ctx, iv, rounds_32bits); ECRYPT_process_bytes(&ctx, state, state, 4); i = 3 - ((num_rounds - 32 * rounds_32bits) / 8); j = 7 - ((num_rounds - 32 * rounds_32bits) % 8); if (state[i] & (1 << j)) return 1; else return 0; }
void Prng::init_chacha_png() { chacha = (ECRYPT_ctx*)aligned_malloc(sizeof(ECRYPT_ctx)); random_state = new u8[RANDOM_STATE_SIZE]; u8 key[256]; u8 iv[64]; std::ifstream rand; rand.open("/dev/urandom", std::ifstream::in); rand.read((char*)&key, (size_t)(256/8)); rand.read((char*)&iv, (size_t)(64/8)); rand.close(); ECRYPT_keysetup(chacha, key, 256, 64); ECRYPT_ivsetup(chacha, iv); chacha_refill_randomness(); return; }
int rounds(const u8* iv, const u8* key, const int rounds) { ECRYPT_ctx ctx = {0}; int rounds_32bits = rounds / 32; u8 state[4] = {0}; int i; int j; ECRYPT_keysetup(&ctx, key, 80, 80); ECRYPT_ivsetup(&ctx, iv, rounds_32bits); ECRYPT_process_bytes(&ctx, state, state, 4); i = 3 - (rounds - 32 * rounds_32bits) / 8; j = 7 - ((rounds - 32 * rounds_32bits) % 8); // return reverse(state[i]); if (state[i] & (1 << j)) return 1; else return 0; }
void perform_test (u8 *key, u8* iv, int iv_length_in_bits) { ECRYPT_ctx ctx; /* Keystream generator context */ u8 keystream[16]; /* Array to contain generated keystream bytes */ int i; /* Counting variable */ /* Load key */ ECRYPT_keysetup (&ctx, key, 80, iv_length_in_bits); /* Load IV */ ECRYPT_ivsetup (&ctx, iv); /* Generate keystream */ ECRYPT_keystream_bytes (&ctx, keystream, 16); /* Display the key */ printf ("Key ="); for (i=0; i<10; i++) printf (" %02x", key[i]); printf ("\n"); /* Display the IV */ printf ("IV ="); for (i=0; i<(iv_length_in_bits+7)/8; i++) printf (" %02x", iv[i]); printf ("\n"); /* Display the derived keytream */ printf ("Keystream ="); for (i=0; i<16; i++) printf (" %02x", keystream[i]); printf ("\n"); printf ("\n"); }
int main() { ECRYPT_ctx ctx; int i,t; u8 key[10]; u8 iv[10]; u8 in[NR_PROC_BYTES],out[NR_PROC_BYTES]; u32 dkey[3],div[3]; xs_rng rng; xs_seed(&rng,time(0)); // xs_seed(&rng,1337); memset(in,0x00,sizeof(in)); memset(out,0x00,sizeof(out)); memset(dkey,0,3*sizeof(u32)); memset(div,0,3*sizeof(u32)); printf("%d\n",NR_TESTS); for(t=0;t<NR_TESTS;t++) { /* memset(key,0xff,sizeof(key)); memset(iv,0xff,sizeof(iv)); */ for(i=0;i<sizeof(key);i++) { int tmp=xs_rand(&rng); key[i]=tmp&0xff; iv[i]=(tmp>>8)&0xff; } // memcpy(dkey,key,sizeof(key)); // memcpy(div,iv,sizeof(iv)); // printf("key:\n"); print_bin_array_u8(key,10); printf("\n"); // printf("iv:\n"); print_bin_array_u8(iv,10); printf("\n"); ECRYPT_init(); ECRYPT_keysetup(&ctx,key,80,80); ECRYPT_ivsetup(&ctx,iv); // printf("state:\n"); print_bin_array_u8(ctx.s,40); printf("\n"); ECRYPT_process_bytes(0,&ctx,in,out,sizeof(in)); print_x32_array(key,3); print_x32_array(iv,3); print_x32_rev_bits(out); printf("\n"); // printf("state:\n"); print_bin_array_u8(ctx.s,40); printf("\n"); // printf("output:\n"); print_bin_array_u8(out,NR_PROC_BYTES); printf("\n"); // printf("\n-----------------------\n"); // printf("dkey:\n"); print_bin_array_u32(dkey,3); printf("\n"); // printf("div:\n"); print_bin_array_u32(div,3); printf("\n"); #if 0 { int term[12]={3,13,18,26,38,40,47,49,55,57,66,79}; int nr_terms=12; u32 const_term=0; int tm; int k; for(tm=-1;tm<80;tm++) { int sum=0,output=0; memset(dkey,0,3*sizeof(u32)); if(tm!=-1) { black_box_key_set_bitpos(dkey,tm); } // printf("dkey:\n"); print_bin_array_u32(dkey,3); printf("\n"); for(k=0;k<(1<<nr_terms);k++) { memset(div,0,3*sizeof(u32)); black_box_id2iv(div,term,12,k); // printf("div[%3d]:\n",k); print_bin_array_u32(div,3); printf("\n"); output=d_trivium(dkey,div,32); sum^=output; } if(tm==-1) { const_term=sum; } else { printf("x_%02d=%08x\n",tm,(sum^const_term)&1); } } } #endif } return 0; }
virtual void do_prepare(const TaskData &) override { ECRYPT_keysetup(&m_ctx, m_key.data(), m_keylen*8, 0); ECRYPT_ivsetup(&m_ctx, m_key.data()); }
static int scrypt(void) { int in_fd_flags; unsigned char * const plaintext = secure_bytes, * const ciphertext = secure_bytes + SECURE_SPACE_BLOCK_BYTE_COUNT, * const key = secure_bytes + (2 * SECURE_SPACE_BLOCK_BYTE_COUNT), * const iv = secure_bytes + (2 * SECURE_SPACE_BLOCK_BYTE_COUNT) + (PASSWORD_LENGTH_MAX / 2); if (fcntl(STDIN_FILENO, F_GETFL, &in_fd_flags) == -1) err_exit(msg_in_unreadable); in_fd_flags &= O_NONBLOCK; if (fcntl(STDIN_FILENO, F_SETFL, &in_fd_flags) == -1) err_exit(msg_in_unreadable); blake2b(key, key + PASSWORD_LENGTH_MAX, key + PASSWORD_LENGTH_MAX, PASSWORD_LENGTH_MAX, strlen((const char *)(key + PASSWORD_LENGTH_MAX)), strlen((const char *)(key + PASSWORD_LENGTH_MAX))); memset(key + PASSWORD_LENGTH_MAX, 0, PASSWORD_LENGTH_MAX); memset(secure_bytes, 0, 2 * SECURE_SPACE_BLOCK_BYTE_COUNT); int bytes_read; for (;;) { if (must_terminate()) err_exit(msg_sig); bytes_read = read(STDIN_FILENO, plaintext, SECURE_SPACE_BLOCK_BYTE_COUNT); if (must_terminate()) err_exit(msg_sig); if (bytes_read == -1) err_exit(msg_in_unreadable); if (bytes_read == 0) break; ECRYPT_keysetup(&ecrypt_struct, key, (PASSWORD_LENGTH_MAX / 2) * 8, IV_LENGTH_MAX * 8); ECRYPT_ivsetup(&ecrypt_struct, iv); (operation == ENCRYPT ? ECRYPT_encrypt_bytes : ECRYPT_decrypt_bytes)(&ecrypt_struct, plaintext, ciphertext, bytes_read); memset(plaintext, 0, SECURE_SPACE_BLOCK_BYTE_COUNT); if (write(STDOUT_FILENO, ciphertext, bytes_read) != bytes_read) err_exit(msg_out_unwritable); memset(ciphertext, 0, SECURE_SPACE_BLOCK_BYTE_COUNT); if (must_terminate()) err_exit(msg_sig); if (bytes_read < SECURE_SPACE_BLOCK_BYTE_COUNT) break; } return EXIT_SUCCESS; }