SecureBytes.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 2014-2022 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
  5. // This software is provided 'as-is', without any express or implied warranty.
  6. //
  7. // In no event will the authors be held liable for any damages arising from the use of this software.
  8. //
  9. // 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:
  10. //
  11. // - 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.
  12. // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  13. // - This notice may not be removed or altered from any source or binary distribution.
  14. //
  15. #if canImport(Darwin)
  16. import Darwin
  17. #elseif canImport(Glibc)
  18. import Glibc
  19. #elseif canImport(WinSDK)
  20. import WinSDK
  21. #endif
  22. typealias Key = SecureBytes
  23. /// Keeps bytes in memory. Because this is class, bytes are not copied
  24. /// and memory area is locked as long as referenced, then unlocked on deinit
  25. final class SecureBytes {
  26. private let bytes: Array<UInt8>
  27. let count: Int
  28. init(bytes: Array<UInt8>) {
  29. self.bytes = bytes
  30. self.count = bytes.count
  31. self.bytes.withUnsafeBufferPointer { (pointer) -> Void in
  32. #if os(Windows)
  33. VirtualLock(UnsafeMutableRawPointer(mutating: pointer.baseAddress), SIZE_T(pointer.count))
  34. #elseif os(WASI)
  35. // not supported on WASI
  36. #else
  37. mlock(pointer.baseAddress, pointer.count)
  38. #endif
  39. }
  40. }
  41. deinit {
  42. self.bytes.withUnsafeBufferPointer { (pointer) -> Void in
  43. #if os(Windows)
  44. VirtualUnlock(UnsafeMutableRawPointer(mutating: pointer.baseAddress), SIZE_T(pointer.count))
  45. #elseif os(WASI)
  46. // not supported on WASI
  47. #else
  48. munlock(pointer.baseAddress, pointer.count)
  49. #endif
  50. }
  51. }
  52. }
  53. extension SecureBytes: Collection {
  54. typealias Index = Int
  55. var endIndex: Int {
  56. self.bytes.endIndex
  57. }
  58. var startIndex: Int {
  59. self.bytes.startIndex
  60. }
  61. subscript(position: Index) -> UInt8 {
  62. self.bytes[position]
  63. }
  64. subscript(bounds: Range<Index>) -> ArraySlice<UInt8> {
  65. self.bytes[bounds]
  66. }
  67. func formIndex(after i: inout Int) {
  68. self.bytes.formIndex(after: &i)
  69. }
  70. func index(after i: Int) -> Int {
  71. self.bytes.index(after: i)
  72. }
  73. }
  74. extension SecureBytes: ExpressibleByArrayLiteral {
  75. public convenience init(arrayLiteral elements: UInt8...) {
  76. self.init(bytes: elements)
  77. }
  78. }