crypto.odin 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. package crypto implements a selection of cryptography algorithms and useful
  3. helper routines.
  4. */
  5. package crypto
  6. import "base:runtime"
  7. import "core:mem"
  8. // compare_constant_time returns 1 iff a and b are equal, 0 otherwise.
  9. //
  10. // The execution time of this routine is constant regardless of the contents
  11. // of the slices being compared, as long as the length of the slices is equal.
  12. // If the length of the two slices is different, it will early-return 0.
  13. compare_constant_time :: proc "contextless" (a, b: []byte) -> int {
  14. // If the length of the slices is different, early return.
  15. //
  16. // This leaks the fact that the slices have a different length,
  17. // but the routine is primarily intended for comparing things
  18. // like MACS and password digests.
  19. n := len(a)
  20. if n != len(b) {
  21. return 0
  22. }
  23. return compare_byte_ptrs_constant_time(raw_data(a), raw_data(b), n)
  24. }
  25. // compare_byte_ptrs_constant_time returns 1 iff the bytes pointed to by
  26. // a and b are equal, 0 otherwise.
  27. //
  28. // The execution time of this routine is constant regardless of the
  29. // contents of the memory being compared.
  30. @(optimization_mode="none")
  31. compare_byte_ptrs_constant_time :: proc "contextless" (a, b: ^byte, n: int) -> int {
  32. x := mem.slice_ptr(a, n)
  33. y := mem.slice_ptr(b, n)
  34. v: byte
  35. for i in 0..<n {
  36. v |= x[i] ~ y[i]
  37. }
  38. // After the loop, v == 0 iff a == b. The subtraction will underflow
  39. // iff v == 0, setting the sign-bit, which gets returned.
  40. return int((u32(v)-1) >> 31)
  41. }
  42. // rand_bytes fills the dst buffer with cryptographic entropy taken from
  43. // the system entropy source. This routine will block if the system entropy
  44. // source is not ready yet. All system entropy source failures are treated
  45. // as catastrophic, resulting in a panic.
  46. //
  47. // Support for the system entropy source can be checked with the
  48. // `HAS_RAND_BYTES` boolean constant.
  49. rand_bytes :: proc (dst: []byte) {
  50. // zero-fill the buffer first
  51. mem.zero_explicit(raw_data(dst), len(dst))
  52. _rand_bytes(dst)
  53. }
  54. random_generator :: proc() -> runtime.Random_Generator {
  55. return {
  56. procedure = proc(data: rawptr, mode: runtime.Random_Generator_Mode, p: []byte) {
  57. switch mode {
  58. case .Read:
  59. rand_bytes(p)
  60. case .Reset:
  61. // do nothing
  62. case .Query_Info:
  63. if len(p) != size_of(runtime.Random_Generator_Query_Info) {
  64. return
  65. }
  66. info := (^runtime.Random_Generator_Query_Info)(raw_data(p))
  67. info^ += {.Uniform, .Cryptographic, .External_Entropy}
  68. }
  69. },
  70. data = nil,
  71. }
  72. }