Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Jitter time measurement on Apple platforms #722

Merged
merged 5 commits into from
Dec 14, 2022

Conversation

dkostic
Copy link
Contributor

@dkostic dkostic commented Dec 12, 2022

Issues:

CryptoAlg-1441

Description of changes:

On Apple platforms, Jitter RNG implementation uses mach_absolute_time
function to get the current value of the clock. However, this function
deals in "tick" units, not in time units. On Intel based Apple
platforms 1 tick = 1 ns, which is good enough timer resolution for
Jitter. On M1 macbooks 1 tick ~ 41.67 ns. This seems to be a problem
for Jitter since it causes frequent failures to produce enough entropy
on M1 macbooks. Another potential cause for the issue might be that
mach_absolute_time clock doesn't increment while they system is
asleep. I am not sure what is the definition of "asleep" on M1 macs,
how it correlates to performance and efficiency cores and different
power saving states, etc. The upstream Jitter RNG repository has
recently added support for using the system counter on aarch64
which seems like way to go for all 64-bit Arm platforms.

So this change:

  • Adds the system counter calls on aarch64 platforms,
  • Removes the __MACH__ special case.

Call-outs:

Point out areas that need special attention or support during the review process. Discuss architecture or design changes.

Testing:

How is this change tested (unit tests, fuzz tests, etc.)? Are there any testing steps to be verified by the reviewer?

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and
the ISC license.

On Apple platforms, Jitter RNG implementation uses `mach_absolute_time`
function to get the current value of the clock. However, this function
deals in "tick" units, not in time units. On Intel based Apple
platforms 1 tick = 1 ns, which is good enough timer resolution for
Jitter. On M1 macbooks 1 tick ~ 41.67 ns. This seems to be a problem
for Jitter since it causes frequent failures to produce enough entropy
on M1 macbooks. Another potential cause for the issue might be that
`mach_absolute_time` clock doesn't increment while they system is
asleep. I am not sure what is the definition of "asleep" on M1 macs,
how it correlates to performance and efficiency cores and different
power saving states, etc. So to be on the safe side we change:
  - Instead of `mach_absolute_time` we call `clock_gettime_nsec_np`
    function, as suggested by Apple's documentation [1],
  - Instead of using `CLOCK_UPTIME_RAW` that corresponds to
    `mach_absolut_time`, we use `CLOCK_MONOTONIC_RAW` that
    corresponds to `mach_continuous_time` whose clock increments
    even while the system is asleep [2].

[1] https://developer.apple.com/documentation/kernel/1462446-mach_absolute_time
[2] https://developer.apple.com/documentation/kernel/1646199-mach_continuous_time
@torben-hansen torben-hansen self-requested a review December 12, 2022 18:20
@Taffer
Copy link
Contributor

Taffer commented Dec 12, 2022

This is required for the fips-2021-*-1MU branch.

Taffer
Taffer previously approved these changes Dec 14, 2022
@dkostic dkostic merged commit 9624f32 into aws:main Dec 14, 2022
@dkostic dkostic deleted the fix-jitter-macos branch December 14, 2022 18:59
Taffer pushed a commit to Taffer/aws-lc that referenced this pull request Dec 14, 2022
On Apple platforms, Jitter RNG implementation uses `mach_absolute_time`
function to get the current value of the clock. However, this function
deals in "tick" units, not in time units. On Intel based Apple
platforms 1 tick = 1 ns, which is good enough timer resolution for
Jitter. On M1 macbooks 1 tick ~ 41.67 ns. This seems to be a problem
for Jitter since it causes frequent failures to produce enough entropy
on M1 macbooks. Another potential cause for the issue might be that
`mach_absolute_time` clock doesn't increment while they system is
asleep. I am not sure what is the definition of "asleep" on M1 macs,
how it correlates to performance and efficiency cores and different
power saving states, etc. The upstream Jitter RNG repository has
recently added support for using the system counter on `aarch64`
which seems like way to go for all 64-bit Arm platforms.

So this change:
  - Adds the system counter calls on `aarch64` platforms,
  - Removes the `__MACH__` special case.
Sign up for free to 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.

3 participants