Utils.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // Utils.swift
  3. // CryptoSwift
  4. //
  5. // Copyright (C) 2014-2017 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
  6. // This software is provided 'as-is', without any express or implied warranty.
  7. //
  8. // In no event will the authors be held liable for any damages arising from the use of this software.
  9. //
  10. // Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
  11. //
  12. // - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
  13. // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  14. // - This notice may not be removed or altered from any source or binary distribution.
  15. //
  16. func rotateLeft(_ value: UInt8, by: UInt8) -> UInt8 {
  17. return ((value << by) & 0xff) | (value >> (8 - by))
  18. }
  19. func rotateLeft(_ value: UInt16, by: UInt16) -> UInt16 {
  20. return ((value << by) & 0xffff) | (value >> (16 - by))
  21. }
  22. func rotateLeft(_ value: UInt32, by: UInt32) -> UInt32 {
  23. return ((value << by) & 0xffffffff) | (value >> (32 - by))
  24. }
  25. func rotateLeft(_ value: UInt64, by: UInt64) -> UInt64 {
  26. return (value << by) | (value >> (64 - by))
  27. }
  28. func rotateRight(_ value: UInt16, by: UInt16) -> UInt16 {
  29. return (value >> by) | (value << (16 - by))
  30. }
  31. func rotateRight(_ value: UInt32, by: UInt32) -> UInt32 {
  32. return (value >> by) | (value << (32 - by))
  33. }
  34. func rotateRight(_ value: UInt64, by: UInt64) -> UInt64 {
  35. return ((value >> by) | (value << (64 - by)))
  36. }
  37. func reversed(_ uint8: UInt8) -> UInt8 {
  38. var v = uint8
  39. v = (v & 0xf0) >> 4 | (v & 0x0f) << 4
  40. v = (v & 0xcc) >> 2 | (v & 0x33) << 2
  41. v = (v & 0xaa) >> 1 | (v & 0x55) << 1
  42. return v
  43. }
  44. func reversed(_ uint32: UInt32) -> UInt32 {
  45. var v = uint32
  46. v = ((v >> 1) & 0x55555555) | ((v & 0x55555555) << 1)
  47. v = ((v >> 2) & 0x33333333) | ((v & 0x33333333) << 2)
  48. v = ((v >> 4) & 0x0f0f0f0f) | ((v & 0x0f0f0f0f) << 4)
  49. v = ((v >> 8) & 0x00ff00ff) | ((v & 0x00ff00ff) << 8)
  50. v = ((v >> 16) & 0xffff) | ((v & 0xffff) << 16)
  51. return v
  52. }
  53. func xor<T,V>(_ left: T, _ right: V) -> ArraySlice<UInt8> where T: RandomAccessCollection, V: RandomAccessCollection, T.Element == UInt8, T.Index == Int, T.IndexDistance == Int, V.Element == UInt8, V.IndexDistance == Int, V.Index == Int {
  54. return xor(left, right).slice
  55. }
  56. func xor<T,V>(_ left: T, _ right: V) -> Array<UInt8> where T: RandomAccessCollection, V: RandomAccessCollection, T.Element == UInt8, T.Index == Int, T.IndexDistance == Int, V.Element == UInt8, V.IndexDistance == Int, V.Index == Int {
  57. let length = Swift.min(left.count, right.count)
  58. let buf = UnsafeMutablePointer<UInt8>.allocate(capacity: length)
  59. buf.initialize(to: 0, count: length)
  60. defer {
  61. buf.deinitialize()
  62. buf.deallocate(capacity: length)
  63. }
  64. // xor
  65. for i in 0..<length {
  66. buf[i] = left[left.startIndex.advanced(by: i)] ^ right[right.startIndex.advanced(by: i)]
  67. }
  68. return Array(UnsafeBufferPointer(start: buf, count: length))
  69. }
  70. /**
  71. ISO/IEC 9797-1 Padding method 2.
  72. Add a single bit with value 1 to the end of the data.
  73. If necessary add bits with value 0 to the end of the data until the padded data is a multiple of blockSize.
  74. - parameters:
  75. - blockSize: Padding size in bytes.
  76. - allowance: Excluded trailing number of bytes.
  77. */
  78. @inline(__always)
  79. func bitPadding(to data: inout Array<UInt8>, blockSize: Int, allowance: Int = 0) {
  80. let msgLength = data.count
  81. // Step 1. Append Padding Bits
  82. // append one bit (UInt8 with one bit) to message
  83. data.append(0x80)
  84. // Step 2. append "0" bit until message length in bits ≡ 448 (mod 512)
  85. let max = blockSize - allowance // 448, 986
  86. if msgLength % blockSize < max { // 448
  87. data += Array<UInt8>(repeating: 0, count: max - 1 - (msgLength % blockSize))
  88. } else {
  89. data += Array<UInt8>(repeating: 0, count: blockSize + max - 1 - (msgLength % blockSize))
  90. }
  91. }