From 8069fdd873830048c267ffe4a6a1c6cd400c6a96 Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 15 Sep 2021 13:21:45 +0300 Subject: [PATCH] Use base/random.h instead of openssl::RandomValue. --- .../SourceFiles/api/api_cloud_password.cpp | 4 +- Telegram/SourceFiles/api/api_sending.cpp | 6 +- Telegram/SourceFiles/apiwrap.cpp | 15 ++--- .../SourceFiles/boxes/add_contact_box.cpp | 4 +- .../SourceFiles/boxes/create_poll_box.cpp | 4 +- .../boxes/peer_list_controllers.cpp | 4 +- Telegram/SourceFiles/calls/calls_call.cpp | 3 +- .../calls/group/calls_group_call.cpp | 4 +- .../chat_helpers/field_autocomplete.cpp | 4 +- Telegram/SourceFiles/core/utils.cpp | 5 -- Telegram/SourceFiles/core/utils.h | 3 - Telegram/SourceFiles/data/data_session.cpp | 8 +-- .../data/stickers/data_stickers.cpp | 1 - .../SourceFiles/export/export_api_wrap.cpp | 4 +- .../SourceFiles/history/history_message.cpp | 6 +- .../history_view_voice_record_bar.cpp | 4 +- .../inline_bots/inline_bot_result.cpp | 4 +- .../intro/intro_password_check.cpp | 4 +- .../media/clip/media_clip_reader.cpp | 4 +- .../media/view/media_view_overlay_widget.cpp | 4 +- .../SourceFiles/mtproto/config_loader.cpp | 5 +- .../mtproto/connection_abstract.cpp | 4 +- .../SourceFiles/mtproto/connection_http.cpp | 4 +- .../SourceFiles/mtproto/connection_tcp.cpp | 11 ++-- .../mtproto/details/mtproto_dc_key_binder.cpp | 3 +- .../details/mtproto_dc_key_creator.cpp | 25 +++++++- .../details/mtproto_domain_resolver.cpp | 6 +- .../details/mtproto_serialized_request.cpp | 4 +- .../SourceFiles/mtproto/mtproto_dh_utils.cpp | 2 + .../SourceFiles/mtproto/mtproto_dh_utils.h | 5 +- Telegram/SourceFiles/mtproto/session.cpp | 1 - .../SourceFiles/mtproto/session_private.cpp | 9 +-- .../SourceFiles/mtproto/session_private.h | 1 - .../passport/passport_encryption.cpp | 7 ++- .../passport/passport_form_controller.cpp | 6 +- .../passport/passport_panel_controller.cpp | 1 - .../platform/linux/linux_xdp_file_dialog.cpp | 4 +- .../linux/linux_xdp_open_with_dialog.cpp | 4 +- .../platform/linux/specific_linux.cpp | 4 +- .../platform/mac/notifications_manager_mac.mm | 4 +- .../details/storage_file_utilities.cpp | 5 +- .../SourceFiles/storage/file_download.cpp | 1 - .../storage/file_download_mtproto.cpp | 1 - .../SourceFiles/storage/localimageloader.cpp | 61 +++++++++++++++++-- .../SourceFiles/storage/localimageloader.h | 31 ++-------- Telegram/SourceFiles/storage/localstorage.cpp | 3 +- .../SourceFiles/storage/storage_domain.cpp | 7 ++- .../ui/chat/group_call_userpics.cpp | 4 +- .../ui/controls/call_mute_button.cpp | 4 +- .../ui/effects/fireworks_animation.cpp | 26 ++++---- .../window/notifications_utilities.cpp | 4 +- .../window/themes/window_theme.cpp | 4 +- .../window/themes/window_theme_editor_box.cpp | 6 +- .../themes/window_themes_generate_name.cpp | 6 +- Telegram/lib_base | 2 +- Telegram/lib_storage | 2 +- Telegram/lib_ui | 2 +- 57 files changed, 208 insertions(+), 166 deletions(-) diff --git a/Telegram/SourceFiles/api/api_cloud_password.cpp b/Telegram/SourceFiles/api/api_cloud_password.cpp index 3a06ffda3..b2f36d0b9 100644 --- a/Telegram/SourceFiles/api/api_cloud_password.cpp +++ b/Telegram/SourceFiles/api/api_cloud_password.cpp @@ -7,7 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "api/api_cloud_password.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "core/core_cloud_password.h" #include "apiwrap.h" @@ -27,7 +27,7 @@ void CloudPassword::reload() { )).done([=](const MTPaccount_Password &result) { _requestId = 0; result.match([&](const MTPDaccount_password &data) { - openssl::AddRandomSeed(bytes::make_span(data.vsecure_random().v)); + base::RandomAddSeed(bytes::make_span(data.vsecure_random().v)); if (_state) { *_state = Core::ParseCloudPasswordState(data); } else { diff --git a/Telegram/SourceFiles/api/api_sending.cpp b/Telegram/SourceFiles/api/api_sending.cpp index 1858eae33..554c54734 100644 --- a/Telegram/SourceFiles/api/api_sending.cpp +++ b/Telegram/SourceFiles/api/api_sending.cpp @@ -8,7 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "api/api_sending.h" #include "api/api_text_entities.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "base/unixtime.h" #include "data/data_document.h" #include "data/data_photo.h" @@ -76,7 +76,7 @@ void SendExistingMedia( const auto newId = FullMsgId( peerToChannel(peer->id), session->data().nextLocalMessageId()); - const auto randomId = openssl::RandomValue(); + const auto randomId = base::RandomValue(); auto flags = NewMessageFlags(peer); auto sendFlags = MTPmessages_SendMedia::Flags(0); @@ -248,7 +248,7 @@ bool SendDice(Api::MessageToSend &message) { const auto newId = FullMsgId( peerToChannel(peer->id), session->data().nextLocalMessageId()); - const auto randomId = openssl::RandomValue(); + const auto randomId = base::RandomValue(); auto &histories = history->owner().histories(); auto flags = NewMessageFlags(peer); diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index da301bdd1..de7d8aa58 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -46,6 +46,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "core/core_cloud_password.h" #include "core/application.h" #include "base/unixtime.h" +#include "base/random.h" #include "base/qt_adapters.h" #include "base/call_delayed.h" #include "lang/lang_keys.h" @@ -3724,7 +3725,7 @@ void ApiWrap::forwardMessages( ids.reserve(count); randomIds.reserve(count); for (const auto item : draft.items) { - const auto randomId = openssl::RandomValue(); + const auto randomId = base::RandomValue(); if (genClientSideMessage) { if (const auto message = item->toHistoryMessage()) { const auto newId = FullMsgId( @@ -4034,7 +4035,7 @@ void ApiWrap::sendMessage(MessageToSend &&message) { auto newId = FullMsgId( peerToChannel(peer->id), _session->data().nextLocalMessageId()); - auto randomId = openssl::RandomValue(); + auto randomId = base::RandomValue(); TextUtilities::Trim(sending); @@ -4158,7 +4159,7 @@ void ApiWrap::sendBotStart(not_null bot, PeerData *chat) { sendMessage(std::move(message)); return; } - const auto randomId = openssl::RandomValue(); + const auto randomId = base::RandomValue(); request(MTPmessages_StartBot( bot->inputUser, chat ? chat->input : MTP_inputPeerEmpty(), @@ -4184,7 +4185,7 @@ void ApiWrap::sendInlineResult( const auto newId = FullMsgId( peerToChannel(peer->id), _session->data().nextLocalMessageId()); - const auto randomId = openssl::RandomValue(); + const auto randomId = base::RandomValue(); auto flags = NewMessageFlags(peer); auto sendFlags = MTPmessages_SendInlineBotResult::Flag::f_clear_draft | 0; @@ -4340,7 +4341,7 @@ void ApiWrap::sendMedia( not_null item, const MTPInputMedia &media, Api::SendOptions options) { - const auto randomId = openssl::RandomValue(); + const auto randomId = base::RandomValue(); _session->data().registerMessageRandomId(randomId, item->fullId()); sendMediaWithRandomId(item, media, options, randomId); @@ -4414,7 +4415,7 @@ void ApiWrap::sendAlbumWithUploaded( const MessageGroupId &groupId, const MTPInputMedia &media) { const auto localId = item->fullId(); - const auto randomId = openssl::RandomValue(); + const auto randomId = base::RandomValue(); _session->data().registerMessageRandomId(randomId, localId); const auto albumIt = _sendingAlbums.find(groupId.raw()); @@ -4768,7 +4769,7 @@ void ApiWrap::createPoll( MTP_int(replyTo), PollDataToInputMedia(&data), MTP_string(), - MTP_long(openssl::RandomValue()), + MTP_long(base::RandomValue()), MTPReplyMarkup(), MTPVector(), MTP_int(action.options.scheduled) diff --git a/Telegram/SourceFiles/boxes/add_contact_box.cpp b/Telegram/SourceFiles/boxes/add_contact_box.cpp index ab5c3c35a..fe4f99ca8 100644 --- a/Telegram/SourceFiles/boxes/add_contact_box.cpp +++ b/Telegram/SourceFiles/boxes/add_contact_box.cpp @@ -10,7 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "lang/lang_keys.h" #include "mtproto/sender.h" #include "base/flat_set.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "boxes/confirm_box.h" #include "boxes/peer_list_controllers.h" #include "boxes/peers/add_participants_box.h" @@ -383,7 +383,7 @@ void AddContactBox::save() { lastName = QString(); } _sentName = firstName; - _contactId = openssl::RandomValue(); + _contactId = base::RandomValue(); _addRequest = _session->api().request(MTPcontacts_ImportContacts( MTP_vector( 1, diff --git a/Telegram/SourceFiles/boxes/create_poll_box.cpp b/Telegram/SourceFiles/boxes/create_poll_box.cpp index 12804aabe..7eb34916d 100644 --- a/Telegram/SourceFiles/boxes/create_poll_box.cpp +++ b/Telegram/SourceFiles/boxes/create_poll_box.cpp @@ -30,7 +30,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/unique_qptr.h" #include "base/event_filter.h" #include "base/call_delayed.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "window/window_session_controller.h" #include "styles/style_layers.h" #include "styles/style_boxes.h" @@ -885,7 +885,7 @@ not_null CreatePollBox::setupSolution( object_ptr CreatePollBox::setupContent() { using namespace Settings; - const auto id = openssl::RandomValue(); + const auto id = base::RandomValue(); const auto error = lifetime().make_state(Error::Question); auto result = object_ptr(this); diff --git a/Telegram/SourceFiles/boxes/peer_list_controllers.cpp b/Telegram/SourceFiles/boxes/peer_list_controllers.cpp index 3736cd9b8..8b9866ec7 100644 --- a/Telegram/SourceFiles/boxes/peer_list_controllers.cpp +++ b/Telegram/SourceFiles/boxes/peer_list_controllers.cpp @@ -7,7 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "boxes/peer_list_controllers.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "boxes/confirm_box.h" #include "ui/widgets/checkbox.h" #include "ui/ui_utility.h" @@ -36,7 +36,7 @@ void ShareBotGame(not_null bot, not_null chat) { auto &histories = history->owner().histories(); const auto requestType = Data::Histories::RequestType::Send; histories.sendRequest(history, requestType, [=](Fn finish) { - const auto randomId = openssl::RandomValue(); + const auto randomId = base::RandomValue(); const auto api = &chat->session().api(); history->sendRequestId = api->request(MTPmessages_SendMedia( MTP_flags(0), diff --git a/Telegram/SourceFiles/calls/calls_call.cpp b/Telegram/SourceFiles/calls/calls_call.cpp index 9c067f0ab..474c5a47d 100644 --- a/Telegram/SourceFiles/calls/calls_call.cpp +++ b/Telegram/SourceFiles/calls/calls_call.cpp @@ -16,6 +16,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "boxes/rate_call_box.h" #include "calls/calls_instance.h" #include "base/openssl_help.h" +#include "base/random.h" #include "mtproto/mtproto_dh_utils.h" #include "mtproto/mtproto_config.h" #include "core/application.h" @@ -233,7 +234,7 @@ void Call::startOutgoing() { _api.request(MTPphone_RequestCall( MTP_flags(flags), _user->inputUser, - MTP_int(openssl::RandomValue()), + MTP_int(base::RandomValue()), MTP_bytes(_gaHash), MTP_phoneCallProtocol( MTP_flags(MTPDphoneCallProtocol::Flag::f_udp_p2p diff --git a/Telegram/SourceFiles/calls/group/calls_group_call.cpp b/Telegram/SourceFiles/calls/group/calls_group_call.cpp index cdb02996f..9a4ee3d68 100644 --- a/Telegram/SourceFiles/calls/group/calls_group_call.cpp +++ b/Telegram/SourceFiles/calls/group/calls_group_call.cpp @@ -27,7 +27,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_peer_values.h" #include "data/data_session.h" #include "base/global_shortcuts.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "webrtc/webrtc_video_track.h" #include "webrtc/webrtc_media_devices.h" #include "webrtc/webrtc_create_adm.h" @@ -1008,7 +1008,7 @@ void GroupCall::start(TimeId scheduleDate) { _createRequestId = _api.request(MTPphone_CreateGroupCall( MTP_flags(scheduleDate ? Flag::f_schedule_date : Flag(0)), _peer->input, - MTP_int(openssl::RandomValue()), + MTP_int(base::RandomValue()), MTPstring(), // title MTP_int(scheduleDate) )).done([=](const MTPUpdates &result) { diff --git a/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp b/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp index 86f6e04b1..042d13674 100644 --- a/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp +++ b/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp @@ -35,7 +35,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/ui_utility.h" #include "ui/cached_round_corners.h" #include "base/unixtime.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "window/window_adaptive.h" #include "window/window_session_controller.h" #include "styles/style_chat.h" @@ -667,7 +667,7 @@ void FieldAutocomplete::showAnimated() { return; } if (_cache.isNull()) { - _stickersSeed = openssl::RandomValue(); + _stickersSeed = base::RandomValue(); _scroll->show(); _cache = Ui::GrabWidget(this); } diff --git a/Telegram/SourceFiles/core/utils.cpp b/Telegram/SourceFiles/core/utils.cpp index 10d3f9ad7..160af3f77 100644 --- a/Telegram/SourceFiles/core/utils.cpp +++ b/Telegram/SourceFiles/core/utils.cpp @@ -371,11 +371,6 @@ char *hashMd5Hex(const int32 *hashmd5, void *dest) { return md5To; } -void memset_rand(void *data, uint32 len) { - Assert(_sslInited); - RAND_bytes((uchar*)data, len); -} - namespace { QMap fastRusEng; QHash fastLetterRusEng; diff --git a/Telegram/SourceFiles/core/utils.h b/Telegram/SourceFiles/core/utils.h index 8a9aec63a..c3794e9cb 100644 --- a/Telegram/SourceFiles/core/utils.h +++ b/Telegram/SourceFiles/core/utils.h @@ -109,9 +109,6 @@ inline std::array hashMd5Hex(const void *data, int size) { return result; } -// good random (using openssl implementation) -void memset_rand(void *data, uint32 len); - QString translitRusEng(const QString &rus); QString rusKeyboardLayoutSwitch(const QString &from); diff --git a/Telegram/SourceFiles/data/data_session.cpp b/Telegram/SourceFiles/data/data_session.cpp index ae4698e84..5078d8728 100644 --- a/Telegram/SourceFiles/data/data_session.cpp +++ b/Telegram/SourceFiles/data/data_session.cpp @@ -60,7 +60,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/platform/base_platform_info.h" #include "base/unixtime.h" #include "base/call_delayed.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "facades.h" // Notify::switchInlineBotButtonReceived #include "app.h" #include "styles/style_boxes.h" // st::backgroundSize @@ -2427,7 +2427,7 @@ PhotoData *Session::photoFromWeb( return nullptr; } return photo( - openssl::RandomValue(), + base::RandomValue(), uint64(0), QByteArray(), base::unixtime::now(), @@ -2692,7 +2692,7 @@ DocumentData *Session::documentFromWeb( const ImageLocation &thumbnailLocation, const ImageLocation &videoThumbnailLocation) { const auto result = document( - openssl::RandomValue(), + base::RandomValue(), uint64(0), QByteArray(), base::unixtime::now(), @@ -2714,7 +2714,7 @@ DocumentData *Session::documentFromWeb( const ImageLocation &thumbnailLocation, const ImageLocation &videoThumbnailLocation) { const auto result = document( - openssl::RandomValue(), + base::RandomValue(), uint64(0), QByteArray(), base::unixtime::now(), diff --git a/Telegram/SourceFiles/data/stickers/data_stickers.cpp b/Telegram/SourceFiles/data/stickers/data_stickers.cpp index 1f6cf60d5..111519043 100644 --- a/Telegram/SourceFiles/data/stickers/data_stickers.cpp +++ b/Telegram/SourceFiles/data/stickers/data_stickers.cpp @@ -24,7 +24,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mtproto/mtproto_config.h" #include "ui/toast/toast.h" #include "ui/image/image_location_factory.h" -#include "base/openssl_help.h" #include "base/unixtime.h" #include "styles/style_chat_helpers.h" diff --git a/Telegram/SourceFiles/export/export_api_wrap.cpp b/Telegram/SourceFiles/export/export_api_wrap.cpp index c56ac4f89..9f4d27edb 100644 --- a/Telegram/SourceFiles/export/export_api_wrap.cpp +++ b/Telegram/SourceFiles/export/export_api_wrap.cpp @@ -14,7 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mtproto/mtproto_response.h" #include "base/value_ordering.h" #include "base/bytes.h" -#include "base/openssl_help.h" +#include "base/random.h" #include #include @@ -1748,7 +1748,7 @@ auto ApiWrap::prepareFileProcess( result->location = file.location; result->size = file.size; result->origin = origin; - result->randomId = openssl::RandomValue(); + result->randomId = base::RandomValue(); return result; } diff --git a/Telegram/SourceFiles/history/history_message.cpp b/Telegram/SourceFiles/history/history_message.cpp index f79d5ed91..19b9569f9 100644 --- a/Telegram/SourceFiles/history/history_message.cpp +++ b/Telegram/SourceFiles/history/history_message.cpp @@ -7,7 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "history/history_message.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "base/unixtime.h" #include "lang/lang_keys.h" #include "mainwidget.h" @@ -279,10 +279,10 @@ void FastShareMessage(not_null item) { for (const auto &fullId : data->msgIds) { msgIds.push_back(MTP_int(fullId.msg)); } - auto generateRandom = [&] { + const auto generateRandom = [&] { auto result = QVector(data->msgIds.size()); for (auto &value : result) { - value = openssl::RandomValue(); + value = base::RandomValue(); } return result; }; diff --git a/Telegram/SourceFiles/history/view/controls/history_view_voice_record_bar.cpp b/Telegram/SourceFiles/history/view/controls/history_view_voice_record_bar.cpp index b82a46edf..bfc6b2255 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_voice_record_bar.cpp +++ b/Telegram/SourceFiles/history/view/controls/history_view_voice_record_bar.cpp @@ -9,7 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "api/api_send_progress.h" #include "base/event_filter.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "base/unixtime.h" #include "boxes/confirm_box.h" #include "core/application.h" @@ -98,7 +98,7 @@ enum class FilterType { [[nodiscard]] not_null DummyDocument( not_null owner) { return owner->document( - openssl::RandomValue(), + base::RandomValue(), uint64(0), QByteArray(), base::unixtime::now(), diff --git a/Telegram/SourceFiles/inline_bots/inline_bot_result.cpp b/Telegram/SourceFiles/inline_bots/inline_bot_result.cpp index a95ffee53..26012fe22 100644 --- a/Telegram/SourceFiles/inline_bots/inline_bot_result.cpp +++ b/Telegram/SourceFiles/inline_bots/inline_bot_result.cpp @@ -8,7 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "inline_bots/inline_bot_result.h" #include "api/api_text_entities.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "data/data_photo.h" #include "data/data_document.h" #include "data/data_session.h" @@ -416,7 +416,7 @@ void Result::createGame(not_null session) { return; } - const auto gameId = openssl::RandomValue(); + const auto gameId = base::RandomValue(); _game = session->data().game( gameId, 0, diff --git a/Telegram/SourceFiles/intro/intro_password_check.cpp b/Telegram/SourceFiles/intro/intro_password_check.cpp index 6f20ca098..4fed224e2 100644 --- a/Telegram/SourceFiles/intro/intro_password_check.cpp +++ b/Telegram/SourceFiles/intro/intro_password_check.cpp @@ -18,7 +18,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/widgets/input_fields.h" #include "ui/widgets/labels.h" #include "main/main_account.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "styles/style_intro.h" #include "styles/style_boxes.h" @@ -192,7 +192,7 @@ void PasswordCheckWidget::requestPasswordData() { ).done([=](const MTPaccount_Password &result) { _sentRequest = 0; result.match([&](const MTPDaccount_password &data) { - openssl::AddRandomSeed(bytes::make_span(data.vsecure_random().v)); + base::RandomAddSeed(bytes::make_span(data.vsecure_random().v)); _passwordState = Core::ParseCloudPasswordState(data); passwordChecked(); }); diff --git a/Telegram/SourceFiles/media/clip/media_clip_reader.cpp b/Telegram/SourceFiles/media/clip/media_clip_reader.cpp index 98aeaced7..83a5268cb 100644 --- a/Telegram/SourceFiles/media/clip/media_clip_reader.cpp +++ b/Telegram/SourceFiles/media/clip/media_clip_reader.cpp @@ -10,7 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "media/clip/media_clip_ffmpeg.h" #include "media/clip/media_clip_check_streaming.h" #include "core/file_location.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "base/invoke_queued.h" #include "logs.h" @@ -115,7 +115,7 @@ void Reader::init(const Core::FileLocation &location, const QByteArray &data) { managers.push_back(new Manager(threads.back())); threads.back()->start(); } else { - _threadIndex = int32(openssl::RandomValue() % threads.size()); + _threadIndex = int32(base::RandomValue() % threads.size()); int32 loadLevel = 0x7FFFFFFF; for (int32 i = 0, l = threads.size(); i < l; ++i) { int32 level = managers.at(i)->loadLevel(); diff --git a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp index b067011b8..143671040 100644 --- a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp +++ b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp @@ -58,7 +58,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "window/window_session_controller.h" #include "window/window_controller.h" #include "base/platform/base_platform_info.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "base/unixtime.h" #include "base/qt_signal_producer.h" #include "base/event_filter.h" @@ -2847,7 +2847,7 @@ void OverlayWidget::initThemePreview() { const auto weakSession = base::make_weak(&_document->session()); const auto path = _document->location().name(); - const auto id = _themePreviewId = openssl::RandomValue(); + const auto id = _themePreviewId = base::RandomValue(); const auto weak = Ui::MakeWeak(_widget); crl::async([=, data = std::move(current)]() mutable { auto preview = GeneratePreview( diff --git a/Telegram/SourceFiles/mtproto/config_loader.cpp b/Telegram/SourceFiles/mtproto/config_loader.cpp index d9f55fac8..3529dd33e 100644 --- a/Telegram/SourceFiles/mtproto/config_loader.cpp +++ b/Telegram/SourceFiles/mtproto/config_loader.cpp @@ -7,7 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "mtproto/config_loader.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "mtproto/special_config_request.h" #include "mtproto/facade.h" #include "mtproto/mtproto_dc_options.h" @@ -185,8 +185,7 @@ void ConfigLoader::sendSpecialRequest() { } const auto weak = base::make_weak(this); - const auto index = openssl::RandomValue() - % _specialEndpoints.size(); + const auto index = base::RandomValue() % _specialEndpoints.size(); const auto endpoint = _specialEndpoints.begin() + index; _specialEnumCurrent = specialToRealDcId(endpoint->dcId); diff --git a/Telegram/SourceFiles/mtproto/connection_abstract.cpp b/Telegram/SourceFiles/mtproto/connection_abstract.cpp index 72f0e063a..8cb8fd80a 100644 --- a/Telegram/SourceFiles/mtproto/connection_abstract.cpp +++ b/Telegram/SourceFiles/mtproto/connection_abstract.cpp @@ -12,7 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mtproto/connection_resolving.h" #include "mtproto/session.h" #include "base/unixtime.h" -#include "base/openssl_help.h" +#include "base/random.h" namespace MTP { namespace details { @@ -189,7 +189,7 @@ ConnectionPointer AbstractConnection::Create( } uint32 AbstractConnection::extendedNotSecurePadding() const { - return uint32(openssl::RandomValue() & 0x3F); + return uint32(base::RandomValue() & 0x3F); } } // namespace details diff --git a/Telegram/SourceFiles/mtproto/connection_http.cpp b/Telegram/SourceFiles/mtproto/connection_http.cpp index bebdb4e33..6c369d735 100644 --- a/Telegram/SourceFiles/mtproto/connection_http.cpp +++ b/Telegram/SourceFiles/mtproto/connection_http.cpp @@ -7,7 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "mtproto/connection_http.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "base/qthelp_url.h" namespace MTP { @@ -21,7 +21,7 @@ constexpr auto kFullConnectionTimeout = crl::time(8000); HttpConnection::HttpConnection(QThread *thread, const ProxyData &proxy) : AbstractConnection(thread, proxy) -, _checkNonce(openssl::RandomValue()) { +, _checkNonce(base::RandomValue()) { _manager.moveToThread(thread); _manager.setProxy(ToNetworkProxy(proxy)); } diff --git a/Telegram/SourceFiles/mtproto/connection_tcp.cpp b/Telegram/SourceFiles/mtproto/connection_tcp.cpp index 4e37af10a..6c8fbf4a8 100644 --- a/Telegram/SourceFiles/mtproto/connection_tcp.cpp +++ b/Telegram/SourceFiles/mtproto/connection_tcp.cpp @@ -10,12 +10,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mtproto/details/mtproto_abstract_socket.h" #include "base/bytes.h" #include "base/openssl_help.h" +#include "base/random.h" #include "base/qthelp_url.h" -extern "C" { -#include -} // extern "C" - namespace MTP { namespace details { namespace { @@ -181,11 +178,11 @@ bytes::span TcpConnection::Protocol::VersionD::finalizePacket( Expects(buffer.size() > 2 && buffer.size() < 0x1000003U); const auto intsSize = uint32(buffer.size() - 2); - const auto padding = openssl::RandomValue() & 0x0F; + const auto padding = base::RandomValue() & 0x0F; const auto bytesSize = intsSize * sizeof(mtpPrime) + padding; buffer[1] = bytesSize; for (auto added = 0; added < padding; added += 4) { - buffer.push_back(openssl::RandomValue()); + buffer.push_back(base::RandomValue()); } return bytes::make_span(buffer).subspan(4, 4 + bytesSize); @@ -233,7 +230,7 @@ TcpConnection::TcpConnection( const ProxyData &proxy) : AbstractConnection(thread, proxy) , _instance(instance) -, _checkNonce(openssl::RandomValue()) { +, _checkNonce(base::RandomValue()) { } ConnectionPointer TcpConnection::clone(const ProxyData &proxy) { diff --git a/Telegram/SourceFiles/mtproto/details/mtproto_dc_key_binder.cpp b/Telegram/SourceFiles/mtproto/details/mtproto_dc_key_binder.cpp index 2ade496b9..3e1cdd8cf 100644 --- a/Telegram/SourceFiles/mtproto/details/mtproto_dc_key_binder.cpp +++ b/Telegram/SourceFiles/mtproto/details/mtproto_dc_key_binder.cpp @@ -11,6 +11,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mtproto/mtp_instance.h" #include "base/unixtime.h" #include "base/openssl_help.h" +#include "base/random.h" #include "scheme.h" #include @@ -84,7 +85,7 @@ SerializedRequest DcKeyBinder::prepareRequest( Expects(temporaryKey != nullptr); Expects(temporaryKey->expiresAt() != 0); - const auto nonce = openssl::RandomValue(); + const auto nonce = base::RandomValue(); const auto msgId = base::unixtime::mtproto_msg_id(); auto result = SerializedRequest::Serialize(MTPauth_BindTempAuthKey( MTP_long(_persistentKey->keyId()), diff --git a/Telegram/SourceFiles/mtproto/details/mtproto_dc_key_creator.cpp b/Telegram/SourceFiles/mtproto/details/mtproto_dc_key_creator.cpp index 6ca2e82de..5329efb5e 100644 --- a/Telegram/SourceFiles/mtproto/details/mtproto_dc_key_creator.cpp +++ b/Telegram/SourceFiles/mtproto/details/mtproto_dc_key_creator.cpp @@ -11,6 +11,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mtproto/connection_abstract.h" #include "mtproto/mtproto_dh_utils.h" #include "base/openssl_help.h" +#include "base/random.h" #include "base/unixtime.h" #include "scheme.h" #include "logs.h" @@ -99,6 +100,8 @@ template [[nodiscard]] bytes::vector EncryptPQInnerRSA( const PQInnerData &data, const RSAPublicKey &key) { + DEBUG_LOG(("AuthKey Info: encrypting pq inner...")); + constexpr auto kPrime = sizeof(mtpPrime); constexpr auto kDataWithPaddingPrimes = 192 / kPrime; constexpr auto kMaxSizeInPrimes = 144 / kPrime; @@ -126,6 +129,8 @@ template const auto dataWithPaddingBytes = bytes::make_span(dataWithPadding); bytes::set_random(dataWithPaddingBytes.subspan(sizeInPrimes * kPrime)); + DEBUG_LOG(("AuthKey Info: starting key generation for pq inner...")); + while (true) { auto dataWithHash = mtpBuffer(); dataWithHash.reserve(kDataWithPaddingPrimes + kDataHashPrimes); @@ -136,7 +141,7 @@ template // data_with_hash := data_pad_reversed // + SHA256(temp_key + data_with_padding); - const auto tempKey = openssl::RandomValue>(); + const auto tempKey = base::RandomValue>(); dataWithHash.resize(kDataWithPaddingPrimes + kDataHashPrimes); const auto dataWithHashBytes = bytes::make_span(dataWithHash); bytes::copy( @@ -148,6 +153,8 @@ template aesEncrypted.resize(dataWithHash.size()); const auto aesEncryptedBytes = bytes::make_span(aesEncrypted); + DEBUG_LOG(("AuthKey Info: encrypting ige for pq inner...")); + // aes_encrypted := AES256_IGE(data_with_hash, temp_key, 0); const auto tempIv = bytes::array{ { bytes::type(0) } }; aesIgeEncryptRaw( @@ -157,6 +164,8 @@ template tempKey.data(), tempIv.data()); + DEBUG_LOG(("AuthKey Info: counting hash for pq inner...")); + // temp_key_xor := temp_key XOR SHA256(aes_encrypted); const auto fullSize = (kKeySize / kPrime) + dataWithHash.size(); keyAesEncrypted.resize(fullSize); @@ -166,13 +175,18 @@ template keyAesEncryptedBytes[i] = tempKey[i] ^ aesHash[i]; } + DEBUG_LOG(("AuthKey Info: checking chosen key for pq inner...")); + // key_aes_encrypted := temp_key_xor + aes_encrypted; bytes::copy( keyAesEncryptedBytes.subspan(kKeySize), aesEncryptedBytes); if (IsGoodEncryptedInner(keyAesEncryptedBytes, key)) { + DEBUG_LOG(("AuthKey Info: chosen key for pq inner is good.")); return key.encrypt(keyAesEncryptedBytes); } + + DEBUG_LOG(("AuthKey Info: chosen key for pq inner is bad...")); } } @@ -360,7 +374,7 @@ void DcKeyCreator::pqSend(not_null attempt, TimeId expiresIn) { ).arg(expiresIn ? "temporary" : "persistent")); attempt->stage = Stage::WaitingPQ; attempt->expiresIn = expiresIn; - attempt->data.nonce = openssl::RandomValue(); + attempt->data.nonce = base::RandomValue(); sendNotSecureRequest(MTPReq_pq_multi(attempt->data.nonce)); } @@ -374,16 +388,19 @@ void DcKeyCreator::pqAnswered( LOG(("AuthKey Error: Unexpected stage %1").arg(int(attempt->stage))); return failed(); } + DEBUG_LOG(("AuthKey Info: getting dc RSA key...")); const auto rsaKey = _dcOptions->getDcRSAKey( _dcId, data.vserver_public_key_fingerprints().v); if (!rsaKey.valid()) { + DEBUG_LOG(("AuthKey Error: unknown public key.")); return failed(DcKeyError::UnknownPublicKey); } attempt->data.server_nonce = data.vserver_nonce(); - attempt->data.new_nonce = openssl::RandomValue(); + attempt->data.new_nonce = base::RandomValue(); + DEBUG_LOG(("AuthKey Info: parsing pq...")); const auto &pq = data.vpq().v; const auto parsed = ParsePQ(data.vpq().v); if (parsed.p.isEmpty() || parsed.q.isEmpty()) { @@ -391,6 +408,7 @@ void DcKeyCreator::pqAnswered( DEBUG_LOG(("AuthKey Error: problematic pq: %1").arg(Logs::mb(pq.constData(), pq.length()).str())); return failed(); } + DEBUG_LOG(("AuthKey Info: parse pq done.")); const auto dhEncString = [&] { return (attempt->expiresIn == 0) @@ -417,6 +435,7 @@ void DcKeyCreator::pqAnswered( rsaKey); }(); if (dhEncString.empty()) { + DEBUG_LOG(("AuthKey Error: could not encrypt pq inner.")); return failed(); } diff --git a/Telegram/SourceFiles/mtproto/details/mtproto_domain_resolver.cpp b/Telegram/SourceFiles/mtproto/details/mtproto_domain_resolver.cpp index 241cd0976..7318d5a39 100644 --- a/Telegram/SourceFiles/mtproto/details/mtproto_domain_resolver.cpp +++ b/Telegram/SourceFiles/mtproto/details/mtproto_domain_resolver.cpp @@ -7,7 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "mtproto/details/mtproto_domain_resolver.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "base/invoke_queued.h" #include "base/call_delayed.h" @@ -47,7 +47,7 @@ QString GenerateDnsRandomPadding() { constexpr auto kMinPadding = 13; constexpr auto kMaxPadding = 128; while (true) { - const auto result = 1 + (openssl::RandomValue() / 2); + const auto result = 1 + (base::RandomValue() / 2); Assert(result <= kMaxPadding); if (result >= kMinPadding) { return result; @@ -56,7 +56,7 @@ QString GenerateDnsRandomPadding() { }(); result.resize(count); for (auto &ch : result) { - ch = kValid[openssl::RandomValue() % (sizeof(kValid) - 1)]; + ch = kValid[base::RandomValue() % (sizeof(kValid) - 1)]; } return result; } diff --git a/Telegram/SourceFiles/mtproto/details/mtproto_serialized_request.cpp b/Telegram/SourceFiles/mtproto/details/mtproto_serialized_request.cpp index 3b3032281..f4ff13192 100644 --- a/Telegram/SourceFiles/mtproto/details/mtproto_serialized_request.cpp +++ b/Telegram/SourceFiles/mtproto/details/mtproto_serialized_request.cpp @@ -7,7 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "mtproto/details/mtproto_serialized_request.h" -#include "base/openssl_help.h" +#include "base/random.h" namespace MTP::details { namespace { @@ -30,7 +30,7 @@ uint32 CountPaddingPrimesCount( } // Some more random padding. - return result + ((openssl::RandomValue() & 0x0F) << 2); + return result + ((base::RandomValue() & 0x0F) << 2); } } // namespace diff --git a/Telegram/SourceFiles/mtproto/mtproto_dh_utils.cpp b/Telegram/SourceFiles/mtproto/mtproto_dh_utils.cpp index 9c136eb1d..db9c0542f 100644 --- a/Telegram/SourceFiles/mtproto/mtproto_dh_utils.cpp +++ b/Telegram/SourceFiles/mtproto/mtproto_dh_utils.cpp @@ -7,6 +7,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "mtproto/mtproto_dh_utils.h" +#include "base/openssl_help.h" + namespace MTP { namespace { diff --git a/Telegram/SourceFiles/mtproto/mtproto_dh_utils.h b/Telegram/SourceFiles/mtproto/mtproto_dh_utils.h index 2860bf02c..e857e6715 100644 --- a/Telegram/SourceFiles/mtproto/mtproto_dh_utils.h +++ b/Telegram/SourceFiles/mtproto/mtproto_dh_utils.h @@ -8,7 +8,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #pragma once #include "base/bytes.h" -#include "base/openssl_help.h" + +namespace openssl { +class BigNum; +} // namespace openssl namespace MTP { diff --git a/Telegram/SourceFiles/mtproto/session.cpp b/Telegram/SourceFiles/mtproto/session.cpp index c82a01a09..2ca35a14d 100644 --- a/Telegram/SourceFiles/mtproto/session.cpp +++ b/Telegram/SourceFiles/mtproto/session.cpp @@ -13,7 +13,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "core/application.h" #include "core/core_settings.h" #include "base/unixtime.h" -#include "base/openssl_help.h" namespace MTP { namespace details { diff --git a/Telegram/SourceFiles/mtproto/session_private.cpp b/Telegram/SourceFiles/mtproto/session_private.cpp index c27a020bd..4abd57a15 100644 --- a/Telegram/SourceFiles/mtproto/session_private.cpp +++ b/Telegram/SourceFiles/mtproto/session_private.cpp @@ -16,8 +16,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mtproto/mtproto_dc_options.h" #include "mtproto/connection_abstract.h" #include "platform/platform_specific.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "base/qthelp_url.h" +#include "base/openssl_help.h" #include "base/unixtime.h" #include "base/platform/base_platform_info.h" #include "zlib.h" @@ -398,7 +399,7 @@ void SessionPrivate::resetSession() { void SessionPrivate::changeSessionId() { auto sessionId = _sessionId; do { - sessionId = openssl::RandomValue(); + sessionId = base::RandomValue(); } while (_sessionId == sessionId); DEBUG_LOG(("MTP Info: setting server_session: %1").arg(sessionId)); @@ -574,7 +575,7 @@ void SessionPrivate::tryToSend() { && !_pingIdToSend && !_pingId && _pingSendAt <= crl::now()) { - _pingIdToSend = openssl::RandomValue(); + _pingIdToSend = base::RandomValue(); } const auto forceNewMsgId = sendAll && markSessionAsStarted(); if (forceNewMsgId && _keyCreator) { @@ -2528,7 +2529,7 @@ void SessionPrivate::authKeyChecked() { resendAll(); } // else receive salt in bad_server_salt first, then try to send all the requests - _pingIdToSend = openssl::RandomValue(); // get server_salt + _pingIdToSend = base::RandomValue(); // get server_salt _sessionData->queueNeedToResumeAndSend(); } diff --git a/Telegram/SourceFiles/mtproto/session_private.h b/Telegram/SourceFiles/mtproto/session_private.h index 7bf27676d..f54d95097 100644 --- a/Telegram/SourceFiles/mtproto/session_private.h +++ b/Telegram/SourceFiles/mtproto/session_private.h @@ -13,7 +13,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mtproto/mtproto_dc_options.h" #include "mtproto/connection_abstract.h" #include "mtproto/facade.h" -#include "base/openssl_help.h" #include "base/timer.h" namespace MTP { diff --git a/Telegram/SourceFiles/passport/passport_encryption.cpp b/Telegram/SourceFiles/passport/passport_encryption.cpp index 3dec6d4e8..c5c6e2a7e 100644 --- a/Telegram/SourceFiles/passport/passport_encryption.cpp +++ b/Telegram/SourceFiles/passport/passport_encryption.cpp @@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "passport/passport_encryption.h" #include "base/openssl_help.h" +#include "base/random.h" #include "mtproto/details/mtproto_rsa_public_key.h" #include @@ -108,7 +109,7 @@ bool CheckSecretBytes(bytes::const_span secret) { bytes::vector GenerateSecretBytes() { auto result = bytes::vector(kSecretSize); - memset_rand(result.data(), result.size()); + bytes::set_random(result); const auto full = ranges::accumulate( result, 0ULL, @@ -334,7 +335,7 @@ EncryptedData EncryptData( constexpr auto kFromPadding = kMinPadding + kAlignTo - 1; constexpr auto kPaddingDelta = kMaxPadding - kFromPadding; const auto randomPadding = kFromPadding - + (openssl::RandomValue() % kPaddingDelta); + + (base::RandomValue() % kPaddingDelta); const auto padding = randomPadding - ((bytes.size() + randomPadding) % kAlignTo); Assert(padding >= kMinPadding && padding <= kMaxPadding); @@ -343,7 +344,7 @@ EncryptedData EncryptData( Assert(unencrypted.size() % kAlignTo == 0); unencrypted[0] = static_cast(padding); - memset_rand(unencrypted.data() + 1, padding - 1); + base::RandomFill(unencrypted.data() + 1, padding - 1); bytes::copy( gsl::make_span(unencrypted).subspan(padding), bytes); diff --git a/Telegram/SourceFiles/passport/passport_form_controller.cpp b/Telegram/SourceFiles/passport/passport_form_controller.cpp index b248d63d4..d247168ff 100644 --- a/Telegram/SourceFiles/passport/passport_form_controller.cpp +++ b/Telegram/SourceFiles/passport/passport_form_controller.cpp @@ -14,7 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "boxes/passcode_box.h" #include "lang/lang_keys.h" #include "lang/lang_hardcoded.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "base/qthelp_url.h" #include "base/unixtime.h" #include "base/call_delayed.h" @@ -1431,7 +1431,7 @@ void FormController::restoreScan( void FormController::prepareFile( EditFile &file, const QByteArray &content) { - const auto fileId = openssl::RandomValue(); + const auto fileId = base::RandomValue(); file.fields.size = content.size(); file.fields.id = fileId; file.fields.dcId = _controller->session().mainDcId(); @@ -2652,7 +2652,7 @@ bool FormController::applyPassword(const MTPDaccount_password &result) { settings.newSecureAlgo = Core::ValidateNewSecureSecretAlgo( Core::ParseSecureSecretAlgo(result.vnew_secure_algo())); settings.pendingResetDate = result.vpending_reset_date().value_or_empty(); - openssl::AddRandomSeed(bytes::make_span(result.vsecure_random().v)); + base::RandomAddSeed(bytes::make_span(result.vsecure_random().v)); return applyPassword(std::move(settings)); } diff --git a/Telegram/SourceFiles/passport/passport_panel_controller.cpp b/Telegram/SourceFiles/passport/passport_panel_controller.cpp index 65242b9f6..900c4376d 100644 --- a/Telegram/SourceFiles/passport/passport_panel_controller.cpp +++ b/Telegram/SourceFiles/passport/passport_panel_controller.cpp @@ -13,7 +13,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "passport/passport_panel_edit_scans.h" #include "passport/passport_panel.h" #include "passport/ui/passport_details_row.h" -#include "base/openssl_help.h" #include "base/unixtime.h" #include "boxes/passcode_box.h" #include "boxes/confirm_box.h" diff --git a/Telegram/SourceFiles/platform/linux/linux_xdp_file_dialog.cpp b/Telegram/SourceFiles/platform/linux/linux_xdp_file_dialog.cpp index fa34ba91c..5361d034f 100644 --- a/Telegram/SourceFiles/platform/linux/linux_xdp_file_dialog.cpp +++ b/Telegram/SourceFiles/platform/linux/linux_xdp_file_dialog.cpp @@ -12,7 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/platform/linux/base_linux_glibmm_helper.h" #include "platform/linux/linux_wayland_integration.h" #include "storage/localstorage.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "base/qt_adapters.h" #include @@ -391,7 +391,7 @@ void XDPFileDialog::openPortal() { } const auto handleToken = Glib::ustring("tdesktop") - + std::to_string(openssl::RandomValue()); + + std::to_string(base::RandomValue()); options["handle_token"] = Glib::Variant::create( handleToken); diff --git a/Telegram/SourceFiles/platform/linux/linux_xdp_open_with_dialog.cpp b/Telegram/SourceFiles/platform/linux/linux_xdp_open_with_dialog.cpp index dddf1b54a..1e6fbbcb4 100644 --- a/Telegram/SourceFiles/platform/linux/linux_xdp_open_with_dialog.cpp +++ b/Telegram/SourceFiles/platform/linux/linux_xdp_open_with_dialog.cpp @@ -12,7 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "platform/linux/linux_wayland_integration.h" #include "core/application.h" #include "window/window_controller.h" -#include "base/openssl_help.h" +#include "base/random.h" #include @@ -93,7 +93,7 @@ bool ShowXDPOpenWithDialog(const QString &filepath) { }(); const auto handleToken = Glib::ustring("tdesktop") - + std::to_string(openssl::RandomValue()); + + std::to_string(base::RandomValue()); auto uniqueName = connection->get_unique_name(); uniqueName.erase(0, 1); diff --git a/Telegram/SourceFiles/platform/linux/specific_linux.cpp b/Telegram/SourceFiles/platform/linux/specific_linux.cpp index 1782cfc3c..aa9974a9e 100644 --- a/Telegram/SourceFiles/platform/linux/specific_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/specific_linux.cpp @@ -7,7 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "platform/linux/specific_linux.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "base/platform/base_platform_info.h" #include "ui/platform/linux/ui_linux_wayland_integration.h" #include "platform/linux/linux_desktop_environment.h" @@ -113,7 +113,7 @@ void PortalAutostart(bool start, bool silent) { }(); const auto handleToken = Glib::ustring("tdesktop") - + std::to_string(openssl::RandomValue()); + + std::to_string(base::RandomValue()); std::map options; options["handle_token"] = Glib::Variant::create( diff --git a/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm b/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm index 10262d63c..ede93dbe0 100644 --- a/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm +++ b/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm @@ -12,7 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/platform/base_platform_info.h" #include "platform/platform_specific.h" #include "base/platform/mac/base_utilities_mac.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "history/history.h" #include "ui/empty_userpic.h" #include "main/main_session.h" @@ -230,7 +230,7 @@ private: }; Manager::Private::Private(Manager *manager) -: _managerId(openssl::RandomValue()) +: _managerId(base::RandomValue()) , _managerIdString(QString::number(_managerId)) , _delegate([[NotificationDelegate alloc] initWithManager:manager managerId:_managerId]) { Core::App().settings().workModeValue( diff --git a/Telegram/SourceFiles/storage/details/storage_file_utilities.cpp b/Telegram/SourceFiles/storage/details/storage_file_utilities.cpp index a59d310a2..0ef6baa63 100644 --- a/Telegram/SourceFiles/storage/details/storage_file_utilities.cpp +++ b/Telegram/SourceFiles/storage/details/storage_file_utilities.cpp @@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mtproto/mtproto_auth_key.h" #include "base/platform/base_platform_file_utilities.h" #include "base/openssl_help.h" +#include "base/random.h" #include #include @@ -270,7 +271,7 @@ FileKey GenerateKey(const QString &basePath) { path.reserve(basePath.size() + 0x11); path += basePath; do { - result = openssl::RandomValue(); + result = base::RandomValue(); path.resize(basePath.size()); path += ToFilePart(result); } while (!result || KeyAlreadyUsed(path)); @@ -460,7 +461,7 @@ void FileWriteDescriptor::finish() { if (fullSize & 0x0F) { fullSize += 0x10 - (fullSize & 0x0F); toEncrypt.resize(fullSize); - memset_rand(toEncrypt.data() + size, fullSize - size); + base::RandomFill(toEncrypt.data() + size, fullSize - size); } *(uint32*)toEncrypt.data() = size; QByteArray encrypted(0x10 + fullSize, Qt::Uninitialized); // 128bit of sha1 - key128, sizeof(data), data diff --git a/Telegram/SourceFiles/storage/file_download.cpp b/Telegram/SourceFiles/storage/file_download.cpp index 65e685f62..a09a76308 100644 --- a/Telegram/SourceFiles/storage/file_download.cpp +++ b/Telegram/SourceFiles/storage/file_download.cpp @@ -22,7 +22,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "apiwrap.h" #include "core/crash_reports.h" #include "base/bytes.h" -#include "base/openssl_help.h" namespace { diff --git a/Telegram/SourceFiles/storage/file_download_mtproto.cpp b/Telegram/SourceFiles/storage/file_download_mtproto.cpp index 05f21985e..b62351fd6 100644 --- a/Telegram/SourceFiles/storage/file_download_mtproto.cpp +++ b/Telegram/SourceFiles/storage/file_download_mtproto.cpp @@ -15,7 +15,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mtproto/mtp_instance.h" #include "mtproto/mtproto_config.h" #include "mtproto/mtproto_auth_key.h" -#include "base/openssl_help.h" mtpFileLoader::mtpFileLoader( not_null session, diff --git a/Telegram/SourceFiles/storage/localimageloader.cpp b/Telegram/SourceFiles/storage/localimageloader.cpp index c20ecfacd..b6fea9132 100644 --- a/Telegram/SourceFiles/storage/localimageloader.cpp +++ b/Telegram/SourceFiles/storage/localimageloader.cpp @@ -14,6 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "core/file_utilities.h" #include "core/mime_type.h" #include "base/unixtime.h" +#include "base/random.h" #include "base/qt_adapters.h" #include "editor/scene/scene.h" // Editor::Scene::attachedStickers #include "media/audio/media_audio.h" @@ -58,7 +59,7 @@ PreparedFileThumbnail PrepareFileThumbnail(QImage &&original) { return {}; } auto result = PreparedFileThumbnail(); - result.id = openssl::RandomValue(); + result.id = base::RandomValue(); const auto scaled = (width > kThumbnailSize || height > kThumbnailSize); const auto scaledWidth = [&] { return (width > height) @@ -150,6 +151,56 @@ MTPInputSingleMedia PrepareAlbumItemMedia( } // namespace +SendMediaPrepare::SendMediaPrepare( + const QString &file, + const PeerId &peer, + SendMediaType type, + MsgId replyTo) +: id(base::RandomValue()) +, file(file) +, peer(peer) +, type(type) +, replyTo(replyTo) { +} + +SendMediaPrepare::SendMediaPrepare( + const QImage &img, + const PeerId &peer, + SendMediaType type, + MsgId replyTo) +: id(base::RandomValue()) +, img(img) +, peer(peer) +, type(type) +, replyTo(replyTo) { +} + +SendMediaPrepare::SendMediaPrepare( + const QByteArray &data, + const PeerId &peer, + SendMediaType type, + MsgId replyTo) +: id(base::RandomValue()) +, data(data) +, peer(peer) +, type(type) +, replyTo(replyTo) { +} + +SendMediaPrepare::SendMediaPrepare( + const QByteArray &data, + int duration, + const PeerId &peer, + SendMediaType type, + MsgId replyTo) +: id(base::RandomValue()) +, data(data) +, peer(peer) +, type(type) +, duration(duration) +, replyTo(replyTo) { +} + SendMediaReady::SendMediaReady( SendMediaType type, const QString &file, @@ -220,7 +271,7 @@ SendMediaReady PreparePeerPhoto(MTP::DcId dcId, PeerId peerId, QImage &&image) { push("b", scaled(320)); push("c", std::move(image), jpeg); - const auto id = openssl::RandomValue(); + const auto id = base::RandomValue(); const auto photo = MTP_photo( MTP_flags(0), MTP_long(id), @@ -400,7 +451,7 @@ void TaskQueueWorker::onTaskAdded() { _inTaskAdded = false; } -SendingAlbum::SendingAlbum() : groupId(openssl::RandomValue()) { +SendingAlbum::SendingAlbum() : groupId(base::RandomValue()) { } void SendingAlbum::fillMedia( @@ -493,7 +544,7 @@ FileLoadTask::FileLoadTask( const FileLoadTo &to, const TextWithTags &caption, std::shared_ptr album) -: _id(openssl::RandomValue()) +: _id(base::RandomValue()) , _session(session) , _dcId(session->mainDcId()) , _to(to) @@ -514,7 +565,7 @@ FileLoadTask::FileLoadTask( const VoiceWaveform &waveform, const FileLoadTo &to, const TextWithTags &caption) -: _id(openssl::RandomValue()) +: _id(base::RandomValue()) , _session(session) , _dcId(session->mainDcId()) , _to(to) diff --git a/Telegram/SourceFiles/storage/localimageloader.h b/Telegram/SourceFiles/storage/localimageloader.h index 22c780d46..cc2ab25d7 100644 --- a/Telegram/SourceFiles/storage/localimageloader.h +++ b/Telegram/SourceFiles/storage/localimageloader.h @@ -7,7 +7,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once -#include "base/openssl_help.h" #include "base/variant.h" #include "api/api_common.h" #include "ui/chat/attach/attach_prepare.h" @@ -31,44 +30,24 @@ struct SendMediaPrepare { const QString &file, const PeerId &peer, SendMediaType type, - MsgId replyTo) : id(openssl::RandomValue()), - file(file), - peer(peer), - type(type), - replyTo(replyTo) { - } + MsgId replyTo); SendMediaPrepare( const QImage &img, const PeerId &peer, SendMediaType type, - MsgId replyTo) : id(openssl::RandomValue()), - img(img), - peer(peer), - type(type), - replyTo(replyTo) { - } + MsgId replyTo); SendMediaPrepare( const QByteArray &data, const PeerId &peer, SendMediaType type, - MsgId replyTo) : id(openssl::RandomValue()), - data(data), - peer(peer), - type(type), - replyTo(replyTo) { - } + MsgId replyTo); SendMediaPrepare( const QByteArray &data, int duration, const PeerId &peer, SendMediaType type, - MsgId replyTo) : id(openssl::RandomValue()), - data(data), - peer(peer), - type(type), - duration(duration), - replyTo(replyTo) { - } + MsgId replyTo); + PhotoId id; QString file; QImage img; diff --git a/Telegram/SourceFiles/storage/localstorage.cpp b/Telegram/SourceFiles/storage/localstorage.cpp index d52d1f195..d25217348 100644 --- a/Telegram/SourceFiles/storage/localstorage.cpp +++ b/Telegram/SourceFiles/storage/localstorage.cpp @@ -15,6 +15,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_document.h" #include "data/data_document_media.h" #include "base/platform/base_platform_info.h" +#include "base/random.h" #include "ui/effects/animation_value.h" #include "core/update_checker.h" #include "core/file_location.h" @@ -445,7 +446,7 @@ void writeSettings() { FileWriteDescriptor settings(name, _basePath); if (_settingsSalt.isEmpty() || !SettingsKey) { _settingsSalt.resize(LocalEncryptSaltSize); - memset_rand(_settingsSalt.data(), _settingsSalt.size()); + base::RandomFill(_settingsSalt.data(), _settingsSalt.size()); SettingsKey = CreateLegacyLocalKey(QByteArray(), _settingsSalt); } settings.writeData(_settingsSalt); diff --git a/Telegram/SourceFiles/storage/storage_domain.cpp b/Telegram/SourceFiles/storage/storage_domain.cpp index d7d5ab96d..ff1bc3e86 100644 --- a/Telegram/SourceFiles/storage/storage_domain.cpp +++ b/Telegram/SourceFiles/storage/storage_domain.cpp @@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mtproto/mtproto_config.h" #include "main/main_domain.h" #include "main/main_account.h" +#include "base/random.h" namespace Storage { namespace { @@ -94,8 +95,8 @@ void Domain::generateLocalKey() { auto pass = QByteArray(MTP::AuthKey::kSize, Qt::Uninitialized); auto salt = QByteArray(LocalEncryptSaltSize, Qt::Uninitialized); - memset_rand(pass.data(), pass.size()); - memset_rand(salt.data(), salt.size()); + base::RandomFill(pass.data(), pass.size()); + base::RandomFill(salt.data(), salt.size()); _localKey = CreateLocalKey(pass, salt); encryptLocalKey(QByteArray()); @@ -103,7 +104,7 @@ void Domain::generateLocalKey() { void Domain::encryptLocalKey(const QByteArray &passcode) { _passcodeKeySalt.resize(LocalEncryptSaltSize); - memset_rand(_passcodeKeySalt.data(), _passcodeKeySalt.size()); + base::RandomFill(_passcodeKeySalt.data(), _passcodeKeySalt.size()); _passcodeKey = CreateLocalKey(passcode, _passcodeKeySalt); EncryptedDescriptor passKeyData(MTP::AuthKey::kSize); diff --git a/Telegram/SourceFiles/ui/chat/group_call_userpics.cpp b/Telegram/SourceFiles/ui/chat/group_call_userpics.cpp index 40f89176b..6b98a0f2a 100644 --- a/Telegram/SourceFiles/ui/chat/group_call_userpics.cpp +++ b/Telegram/SourceFiles/ui/chat/group_call_userpics.cpp @@ -8,7 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/chat/group_call_userpics.h" #include "ui/paint/blobs.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "styles/style_chat.h" namespace Ui { @@ -237,7 +237,7 @@ void GroupCallUserpics::sendRandomLevels() { } for (auto &userpic : _list) { if (const auto blobs = userpic.blobsAnimation.get()) { - const auto value = 30 + (openssl::RandomValue() % 70); + const auto value = 30 + base::RandomIndex(70); userpic.blobsAnimation->blobs.setLevel(float64(value) / 100.); } } diff --git a/Telegram/SourceFiles/ui/controls/call_mute_button.cpp b/Telegram/SourceFiles/ui/controls/call_mute_button.cpp index cac8dea77..da66edf26 100644 --- a/Telegram/SourceFiles/ui/controls/call_mute_button.cpp +++ b/Telegram/SourceFiles/ui/controls/call_mute_button.cpp @@ -12,7 +12,7 @@ #include "ui/painter.h" #include "ui/widgets/call_button.h" #include "ui/widgets/labels.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "styles/palette.h" #include "styles/style_widgets.h" #include "styles/style_calls.h" @@ -681,7 +681,7 @@ CallMuteButton::IconState CallMuteButton::randomWavingState() { { 240, 420 }, { 420, 540 }, }; - const auto index = openssl::RandomValue() % kAnimations.size(); + const auto index = base::RandomIndex(kAnimations.size()); return { 1, kAnimations[index].from, kAnimations[index].to }; } diff --git a/Telegram/SourceFiles/ui/effects/fireworks_animation.cpp b/Telegram/SourceFiles/ui/effects/fireworks_animation.cpp index 2a6bb7f35..74717cfa6 100644 --- a/Telegram/SourceFiles/ui/effects/fireworks_animation.cpp +++ b/Telegram/SourceFiles/ui/effects/fireworks_animation.cpp @@ -7,7 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "ui/effects/fireworks_animation.h" -#include "base/openssl_help.h" +#include "base/random.h" namespace Ui { namespace { @@ -37,12 +37,8 @@ std::vector PrepareBrushes() { }; } -int RandomInt(uint32 till) { - return int(openssl::RandomValue() % till); -} - [[nodiscard]] float64 RandomFloat01() { - return openssl::RandomValue() + return base::RandomValue() / float64(std::numeric_limits::max()); } @@ -190,22 +186,24 @@ void FireworksAnimation::startFall() { void FireworksAnimation::initParticle(Particle &particle, bool falling) { using Type = Particle::Type; - particle.color = RandomInt(_brushes.size()); - particle.type = RandomInt(2) ? Type::Rectangle : Type::Circle; - particle.right = (RandomInt(2) == 1); - particle.finishedStart = 1 + RandomInt(2); + using base::RandomIndex; + + particle.color = RandomIndex(_brushes.size()); + particle.type = RandomIndex(2) ? Type::Rectangle : Type::Circle; + particle.right = (RandomIndex(2) == 1); + particle.finishedStart = 1 + RandomIndex(2); if (particle.type == Type::Circle) { particle.size = style::ConvertScale(6 + RandomFloat01() * 3); } else { particle.size = style::ConvertScale(6 + RandomFloat01() * 6); - particle.rotation = RandomInt(360); + particle.rotation = RandomIndex(360); } if (falling) { particle.y = -RandomFloat01() * kFireworkHeight * 1.2f; - particle.x = 5 + RandomInt(kFireworkWidth - 10); + particle.x = 5 + RandomIndex(kFireworkWidth - 10); particle.xFinished = particle.finishedStart; } else { - const auto xOffset = 4 + RandomInt(10); + const auto xOffset = 4 + RandomIndex(10); const auto yOffset = kFireworkHeight / 4; if (particle.right) { particle.x = kFireworkWidth + xOffset; @@ -214,7 +212,7 @@ void FireworksAnimation::initParticle(Particle &particle, bool falling) { } particle.moveX = (particle.right ? -1 : 1) * (1.2 + RandomFloat01() * 4); particle.moveY = -(4 + RandomFloat01() * 4); - particle.y = yOffset / 2 + RandomInt(yOffset * 2); + particle.y = yOffset / 2 + RandomIndex(yOffset * 2); } } diff --git a/Telegram/SourceFiles/window/notifications_utilities.cpp b/Telegram/SourceFiles/window/notifications_utilities.cpp index 43bd96016..6c72b38cd 100644 --- a/Telegram/SourceFiles/window/notifications_utilities.cpp +++ b/Telegram/SourceFiles/window/notifications_utilities.cpp @@ -8,7 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "window/notifications_utilities.h" #include "base/platform/base_platform_file_utilities.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "core/application.h" #include "data/data_peer.h" #include "ui/empty_userpic.h" @@ -61,7 +61,7 @@ QString CachedUserpics::get( } v.path = u"%1tdata/temp/%2.png"_q.arg( cWorkingDir(), - QString::number(openssl::RandomValue(), 16)); + QString::number(base::RandomValue(), 16)); if (key.first || key.second) { if (peer->isSelf()) { const auto method = (_type == Type::Rounded) diff --git a/Telegram/SourceFiles/window/themes/window_theme.cpp b/Telegram/SourceFiles/window/themes/window_theme.cpp index 5a0c3341e..fbed3c8ec 100644 --- a/Telegram/SourceFiles/window/themes/window_theme.cpp +++ b/Telegram/SourceFiles/window/themes/window_theme.cpp @@ -18,7 +18,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "storage/localstorage.h" #include "storage/localimageloader.h" #include "storage/file_upload.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "base/parse_helper.h" #include "base/zlib_help.h" #include "base/unixtime.h" @@ -484,7 +484,7 @@ SendMediaReady PrepareWallPaper(MTP::DcId dcId, const QImage &image) { attributes.push_back(MTP_documentAttributeImageSize( MTP_int(image.width()), MTP_int(image.height()))); - const auto id = openssl::RandomValue(); + const auto id = base::RandomValue(); const auto document = MTP_document( MTP_flags(0), MTP_long(id), diff --git a/Telegram/SourceFiles/window/themes/window_theme_editor_box.cpp b/Telegram/SourceFiles/window/themes/window_theme_editor_box.cpp index 4ac56dc75..452de4568 100644 --- a/Telegram/SourceFiles/window/themes/window_theme_editor_box.cpp +++ b/Telegram/SourceFiles/window/themes/window_theme_editor_box.cpp @@ -34,7 +34,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/base_file_utilities.h" #include "base/zlib_help.h" #include "base/unixtime.h" -#include "base/openssl_help.h" +#include "base/random.h" #include "data/data_session.h" #include "data/data_document.h" #include "data/data_cloud_themes.h" @@ -338,7 +338,7 @@ bool CopyColorsToPalette( auto result = QString(); result.reserve(kRandomSlugSize); for (auto i = 0; i != kRandomSlugSize; ++i) { - const auto value = openssl::RandomValue() % values; + const auto value = base::RandomValue() % values; if (value < letters) { result.append(char('A' + value)); } else if (value < 2 * letters) { @@ -443,7 +443,7 @@ SendMediaReady PrepareThemeMedia( auto attributes = QVector( 1, MTP_documentAttributeFilename(MTP_string(filename))); - const auto id = openssl::RandomValue(); + const auto id = base::RandomValue(); const auto document = MTP_document( MTP_flags(0), MTP_long(id), diff --git a/Telegram/SourceFiles/window/themes/window_themes_generate_name.cpp b/Telegram/SourceFiles/window/themes/window_themes_generate_name.cpp index 9e56ae592..81c0e0907 100644 --- a/Telegram/SourceFiles/window/themes/window_themes_generate_name.cpp +++ b/Telegram/SourceFiles/window/themes/window_themes_generate_name.cpp @@ -7,7 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "window/themes/window_themes_generate_name.h" -#include "base/openssl_help.h" +#include "base/random.h" namespace Window { namespace Theme { @@ -339,13 +339,13 @@ QString GenerateName(const QColor &accent) { return result; }; const auto random = [&](const std::vector &values) { - const auto index = openssl::RandomValue() % values.size(); + const auto index = base::RandomValue() % values.size(); return capitalized(values[index]); }; const auto min = ranges::min_element(kColors, pred); Assert(min != end(kColors)); const auto color = capitalized(min->second); - return (openssl::RandomValue() % 2 == 0) + return (base::RandomValue() % 2 == 0) ? random(kAdjectives) + ' ' + color : color + ' ' + random(kSubjectives); } diff --git a/Telegram/lib_base b/Telegram/lib_base index 50d2d313e..e8adbd1fa 160000 --- a/Telegram/lib_base +++ b/Telegram/lib_base @@ -1 +1 @@ -Subproject commit 50d2d313e7127802ae25965b6144515cb5d243c2 +Subproject commit e8adbd1fa6c9d423dfa07e358cd8665c77f8503d diff --git a/Telegram/lib_storage b/Telegram/lib_storage index 403df6c4a..73d57840a 160000 --- a/Telegram/lib_storage +++ b/Telegram/lib_storage @@ -1 +1 @@ -Subproject commit 403df6c4a29562bd417c92d410e49819f5a48cc1 +Subproject commit 73d57840ac603107381e0e6b22d5b3bdcae492c6 diff --git a/Telegram/lib_ui b/Telegram/lib_ui index d7bdfa2b5..b35d2d257 160000 --- a/Telegram/lib_ui +++ b/Telegram/lib_ui @@ -1 +1 @@ -Subproject commit d7bdfa2b5ec149b0dd1d008a58a73469282ec58a +Subproject commit b35d2d257a421f6adce0e60d62126ee65f46f573