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 ); }
bool SymmetricCipherSalsa20::processInPlace(QByteArray& data) { Q_ASSERT((data.size() < blockSize()) || ((data.size() % blockSize()) == 0)); ECRYPT_encrypt_bytes(&m_ctx, reinterpret_cast<const u8*>(data.constData()), reinterpret_cast<u8*>(data.data()), data.size()); return true; }
void ECRYPT_encrypt_packet( ECRYPT_ctx* ctx, const u8* iv, const u8* plaintext, u8* ciphertext, u32 msglen) { ECRYPT_ivsetup(ctx, iv); ECRYPT_encrypt_bytes(ctx, plaintext, ciphertext, msglen); }
bool SymmetricCipherSalsa20::processInPlace(QByteArray& data, quint64 rounds) { Q_ASSERT((data.size() < blockSize()) || ((data.size() % blockSize()) == 0)); for (quint64 i = 0; i != rounds; ++i) { ECRYPT_encrypt_bytes(&m_ctx, reinterpret_cast<const u8*>(data.constData()), reinterpret_cast<u8*>(data.data()), data.size()); } return true; }
void ECRYPT_process_bytes( int action, /* 0 = encrypt; 1 = decrypt; */ ECRYPT_ctx* ctx, const u8* input, u8* output, u32 msglen) /* Message length in bytes. */ { if (action == 0) ECRYPT_encrypt_bytes(ctx, input, output, msglen); else ECRYPT_decrypt_bytes(ctx, input, output, msglen); }
QByteArray SymmetricCipherSalsa20::process(const QByteArray& data, bool* ok) { Q_ASSERT((data.size() < blockSize()) || ((data.size() % blockSize()) == 0)); QByteArray result; result.resize(data.size()); ECRYPT_encrypt_bytes(&m_ctx, reinterpret_cast<const u8*>(data.constData()), reinterpret_cast<u8*>(result.data()), data.size()); *ok = true; return result; }
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; }
void ECRYPT_process_packet( int action, ECRYPT_ctx* ctx, const u8* iv, const u8* input, u8* output, u32 msglen) { ECRYPT_ivsetup(ctx, iv); #ifdef ECRYPT_HAS_SINGLE_BYTE_FUNCTION ECRYPT_process_bytes(action, ctx, input, output, msglen); #else if (action == 0) ECRYPT_encrypt_bytes(ctx, input, output, msglen); else ECRYPT_decrypt_bytes(ctx, input, output, msglen); #endif }
void ECRYPT_keystream_bytes(ECRYPT_ctx *x,u8 *stream,u32 bytes) { u32 i; for (i = 0;i < bytes;++i) stream[i] = 0; ECRYPT_encrypt_bytes(x,stream,stream,bytes); }
void ECRYPT_decrypt_bytes(ECRYPT_ctx *x,const u8 *c,u8 *m,u32 bytes) { ECRYPT_encrypt_bytes(x,c,m,bytes); }
virtual void do_block(data_type * data) override { ECRYPT_encrypt_bytes(&m_ctx, data, data, m_blocklen); }
void ECRYPT_Salsa::SALSA_keystream_bytes(void *x,u8 *stream,u32 bytes) { u32 i; for (i = 0;i < bytes;++i) stream[i] = 0; ECRYPT_encrypt_bytes(x,stream,stream,bytes); }
void ECRYPT_Salsa::ECRYPT_decrypt_bytes(void *xa,const u8 *c,u8 *m,u32 bytes) { ECRYPT_encrypt_bytes(xa,c,m,bytes); }