crypto.odin 1.9 KB

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