utils.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Copyright 2013-2017 Donald Stufft and individual contributors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import nacl.exceptions as exc
  15. from nacl._sodium import ffi, lib
  16. from nacl.exceptions import ensure
  17. def sodium_memcmp(inp1: bytes, inp2: bytes) -> bool:
  18. """
  19. Compare contents of two memory regions in constant time
  20. """
  21. ensure(isinstance(inp1, bytes), raising=exc.TypeError)
  22. ensure(isinstance(inp2, bytes), raising=exc.TypeError)
  23. ln = max(len(inp1), len(inp2))
  24. buf1 = ffi.new("char []", ln)
  25. buf2 = ffi.new("char []", ln)
  26. ffi.memmove(buf1, inp1, len(inp1))
  27. ffi.memmove(buf2, inp2, len(inp2))
  28. eqL = len(inp1) == len(inp2)
  29. eqC = lib.sodium_memcmp(buf1, buf2, ln) == 0
  30. return eqL and eqC
  31. def sodium_pad(s: bytes, blocksize: int) -> bytes:
  32. """
  33. Pad the input bytearray ``s`` to a multiple of ``blocksize``
  34. using the ISO/IEC 7816-4 algorithm
  35. :param s: input bytes string
  36. :type s: bytes
  37. :param blocksize:
  38. :type blocksize: int
  39. :return: padded string
  40. :rtype: bytes
  41. """
  42. ensure(isinstance(s, bytes), raising=exc.TypeError)
  43. ensure(isinstance(blocksize, int), raising=exc.TypeError)
  44. if blocksize <= 0:
  45. raise exc.ValueError
  46. s_len = len(s)
  47. m_len = s_len + blocksize
  48. buf = ffi.new("unsigned char []", m_len)
  49. p_len = ffi.new("size_t []", 1)
  50. ffi.memmove(buf, s, s_len)
  51. rc = lib.sodium_pad(p_len, buf, s_len, blocksize, m_len)
  52. ensure(rc == 0, "Padding failure", raising=exc.CryptoError)
  53. return ffi.buffer(buf, p_len[0])[:]
  54. def sodium_unpad(s: bytes, blocksize: int) -> bytes:
  55. """
  56. Remove ISO/IEC 7816-4 padding from the input byte array ``s``
  57. :param s: input bytes string
  58. :type s: bytes
  59. :param blocksize:
  60. :type blocksize: int
  61. :return: unpadded string
  62. :rtype: bytes
  63. """
  64. ensure(isinstance(s, bytes), raising=exc.TypeError)
  65. ensure(isinstance(blocksize, int), raising=exc.TypeError)
  66. s_len = len(s)
  67. u_len = ffi.new("size_t []", 1)
  68. rc = lib.sodium_unpad(u_len, s, s_len, blocksize)
  69. if rc != 0:
  70. raise exc.CryptoError("Unpadding failure")
  71. return s[: u_len[0]]
  72. def sodium_increment(inp: bytes) -> bytes:
  73. """
  74. Increment the value of a byte-sequence interpreted
  75. as the little-endian representation of a unsigned big integer.
  76. :param inp: input bytes buffer
  77. :type inp: bytes
  78. :return: a byte-sequence representing, as a little-endian
  79. unsigned big integer, the value ``to_int(inp)``
  80. incremented by one.
  81. :rtype: bytes
  82. """
  83. ensure(isinstance(inp, bytes), raising=exc.TypeError)
  84. ln = len(inp)
  85. buf = ffi.new("unsigned char []", ln)
  86. ffi.memmove(buf, inp, ln)
  87. lib.sodium_increment(buf, ln)
  88. return ffi.buffer(buf, ln)[:]
  89. def sodium_add(a: bytes, b: bytes) -> bytes:
  90. """
  91. Given a couple of *same-sized* byte sequences, interpreted as the
  92. little-endian representation of two unsigned integers, compute
  93. the modular addition of the represented values, in constant time for
  94. a given common length of the byte sequences.
  95. :param a: input bytes buffer
  96. :type a: bytes
  97. :param b: input bytes buffer
  98. :type b: bytes
  99. :return: a byte-sequence representing, as a little-endian big integer,
  100. the integer value of ``(to_int(a) + to_int(b)) mod 2^(8*len(a))``
  101. :rtype: bytes
  102. """
  103. ensure(isinstance(a, bytes), raising=exc.TypeError)
  104. ensure(isinstance(b, bytes), raising=exc.TypeError)
  105. ln = len(a)
  106. ensure(len(b) == ln, raising=exc.TypeError)
  107. buf_a = ffi.new("unsigned char []", ln)
  108. buf_b = ffi.new("unsigned char []", ln)
  109. ffi.memmove(buf_a, a, ln)
  110. ffi.memmove(buf_b, b, ln)
  111. lib.sodium_add(buf_a, buf_b, ln)
  112. return ffi.buffer(buf_a, ln)[:]