') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); Generic CTR by newpavlov · Pull Request #195 · RustCrypto/stream-ciphers · GitHub
Skip to content

Generic CTR - #195

Merged
newpavlov merged 7 commits into
masterfrom
ctr_rework
Dec 4, 2020
Merged

Generic CTR#195
newpavlov merged 7 commits into
masterfrom
ctr_rework

Conversation

@newpavlov

@newpavlovnewpavlov commented Dec 2, 2020

Copy link
Copy Markdown
Member

An attempt to define CTR mode generically. The approach is quite flexible and I think it can be even used with the __m128i type (i.e. we will be able to reduce amount of copied code in the aes crate).

But I get a weird compilation error when I try to define implementations of generate_block and load methods. Even though the default implementation works without problems, when I copy it to the concrete type (Ctr128BE) impl, I get an error which asks me to further restrict N with Div<U16> bound, even though it's already restricted by Div<Self::Size>. Even copying the suggested bound does not resolve the issue, compiler suggests to add already existing bound. Looks like a bug in the compiler to me.

UPD: The implementation got restricted back to 128-bit ciphers, probably until landing of const generics.

Comment threadctr/src/ctr32.rs Outdated
Comment threadctr/src/ctr32.rs
Comment on lines -99 to -89
pub fn seek_ctr(&mut self, pos: u32) {
self.ctr.seek(pos);
}

/// Get the current NIST SP800-38D counter value.
// TODO(tarcieri): implement `SyncStreamCipherSeek`
#[inline]
pub fn current_ctr(&self) -> u32 {
self.ctr.current_pos()
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aes-gcm (the crate) presently relies on these APIs, although they could probably be replaced with SyncStreamCipherSeek

@newpavlovnewpavlovDec 2, 2020

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's block position, then it can be simply computed by dividing byte position by block size. Though we probably can extend the seek trait with a method which would return this number right away. I also thought about exposing the block nature of stream ciphers, but I haven't found a good AP for it which would fit different backends and runtime detection for ChaCha.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd previously opened RustCrypto/traits#336 to discuss this

@newpavlov

Copy link
Copy Markdown
MemberAuthor

I am not sure how to work around the compiler error issue. Making the CtrFlavor trait generic over block size instead of the method results in a similar error. I guess we can continue to support only 128-bit ciphers and wait for const generics to land, which it looks like do not have this issue.

Comment threadctr/src/lib.rs Outdated
@newpavlov
newpavlov marked this pull request as ready for review December 4, 2020 09:04

@tarcieritarcieri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! A little curious about the impact on performance, but otherwise looks fantastic.

@newpavlov

Copy link
Copy Markdown
MemberAuthor

Well, I took all measures I can think of to allow compiler to properly optimize the code, but we can't know for sure without properly measuring it.

@newpavlov
newpavlov merged commit 7ddcab1 into masterDec 4, 2020
@newpavlov
newpavlov deleted the ctr_rework branch December 4, 2020 13:33
This was referenced Apr 29, 2021
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@newpavlov@tarcieri