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

Make SRP6RandomEphemeral thread safe #90

Open
Glusk opened this issue Nov 27, 2024 · 0 comments
Open

Make SRP6RandomEphemeral thread safe #90

Glusk opened this issue Nov 27, 2024 · 0 comments

Comments

@Glusk
Copy link
Owner

Glusk commented Nov 27, 2024

The class is currently not thread-safe. A naive thread safe implementation existed but was removed: #69

We could use a thread-safe cache pattern, as proposed here. Something like this;

public class SRP6RandomEphemeral {
  private FinalWrapper wrapper;
  // ... 
  public Bytes bytes(ByteOrder preferredOrder) {
    FinalWrapper w = wrapper;
    if (w == null) { // check 1
      synchronized(this) {
        w = wrapper;
        if (w == null) { // check2
          // Compute random BigInteger r
          // ...
          w = new FinalWrapper(new SRP6CustomIntegerVariable(r));
          wrapper = w;
        }
      }
    }
    return w.instance.bytes(preferredOrder);
  }

  private static class FinalWrapper {
    public final SRP6IntegerVariable instance;
    public FinalWrapper(SRP6IntegerVariable instance) {
      this.instance = instance;
    }
  }
}
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

No branches or pull requests

1 participant