chacha20_impl.odin 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package chacha20
  2. import "base:intrinsics"
  3. import "core:crypto/_chacha20/ref"
  4. import "core:crypto/_chacha20/simd128"
  5. import "core:crypto/_chacha20/simd256"
  6. // DEFAULT_IMPLEMENTATION is the implementation that will be used by
  7. // default if possible.
  8. DEFAULT_IMPLEMENTATION :: Implementation.Simd256
  9. // Implementation is a ChaCha20 implementation. Most callers will not need
  10. // to use this as the package will automatically select the most performant
  11. // implementation available.
  12. Implementation :: enum {
  13. Portable,
  14. Simd128,
  15. Simd256,
  16. }
  17. @(private)
  18. init_impl :: proc(ctx: ^Context, impl: Implementation) {
  19. impl := impl
  20. if impl == .Simd256 && !simd256.is_performant() {
  21. impl = .Simd128
  22. }
  23. if impl == .Simd128 && !simd128.is_performant() {
  24. impl = .Portable
  25. }
  26. ctx._impl = impl
  27. }
  28. @(private)
  29. stream_blocks :: proc(ctx: ^Context, dst, src: []byte, nr_blocks: int) {
  30. switch ctx._impl {
  31. case .Simd256:
  32. simd256.stream_blocks(&ctx._state, dst, src, nr_blocks)
  33. case .Simd128:
  34. simd128.stream_blocks(&ctx._state, dst, src, nr_blocks)
  35. case .Portable:
  36. ref.stream_blocks(&ctx._state, dst, src, nr_blocks)
  37. }
  38. }
  39. @(private)
  40. hchacha20 :: proc "contextless" (dst, key, iv: []byte, impl: Implementation) {
  41. switch impl {
  42. case .Simd256:
  43. simd256.hchacha20(dst, key, iv)
  44. case .Simd128:
  45. simd128.hchacha20(dst, key, iv)
  46. case .Portable:
  47. ref.hchacha20(dst, key, iv)
  48. }
  49. }