2
0

PKCS1v15.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 2014-2021 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. // PKCS is a group of public-key cryptography standards devised
  16. // and published by RSA Security Inc, starting in the early 1990s.
  17. //
  18. /// EMSA PKCS1 v1.5 Padding Scheme
  19. ///
  20. /// The EMSA Version of the PKCS1 v1.5 padding scheme is **deterministic** (it pads the messages contents with 255 value bytes)
  21. /// ```
  22. /// // The returned structure
  23. /// // - PS is the applied padding
  24. /// // - M is your original Message
  25. /// EM = 0x00 || 0x01 || PS || 0x00 || M.
  26. /// ```
  27. /// - Note: This Padding scheme is intended to be used for encoding RSA Signatures
  28. ///
  29. /// [EMSA-PKCS1v1_5 IETF Spec](https://datatracker.ietf.org/doc/html/rfc8017#section-9.2)
  30. struct EMSAPKCS1v15Padding: PaddingProtocol {
  31. init() {
  32. }
  33. @inlinable
  34. func add(to bytes: Array<UInt8>, blockSize: Int) -> Array<UInt8> {
  35. var r = blockSize - ((bytes.count + 3) % blockSize)
  36. if r <= 0 { r = blockSize - 3 }
  37. return [0x00, 0x01] + Array<UInt8>(repeating: 0xFF, count: r) + [0x00] + bytes
  38. }
  39. @inlinable
  40. func remove(from bytes: Array<UInt8>, blockSize _: Int?) -> Array<UInt8> {
  41. assert(!bytes.isEmpty, "Need bytes to remove padding")
  42. assert(bytes.prefix(2) == [0x00, 0x01], "Invalid padding prefix")
  43. guard let paddingLength = bytes.dropFirst(2).firstIndex(of: 0x00) else { return bytes }
  44. guard (paddingLength + 1) <= bytes.count else { return bytes }
  45. return Array(bytes[(paddingLength + 1)...])
  46. }
  47. }
  48. /// EME PKCS1 v1.5 Padding Scheme
  49. ///
  50. /// The EME Version of the PKCS1 v1.5 padding scheme is **non deterministic** (it pads the messages contents with psuedo-random bytes)
  51. /// ```
  52. /// // The returned structure
  53. /// // - PS is the applied padding
  54. /// // - M is your original Message
  55. /// EM = 0x00 || 0x02 || PS || 0x00 || M.
  56. /// ```
  57. /// - Note: This Padding scheme is intended to be used for encoding messages before RSA Encryption
  58. ///
  59. /// [EME-PKCS1v1_5 IETF Spec](https://datatracker.ietf.org/doc/html/rfc8017#section-7.2.1)
  60. struct EMEPKCS1v15Padding: PaddingProtocol {
  61. init() {
  62. }
  63. @inlinable
  64. func add(to bytes: Array<UInt8>, blockSize: Int) -> Array<UInt8> {
  65. var r = blockSize - ((bytes.count + 3) % blockSize)
  66. if r <= 0 { r = blockSize - 3 }
  67. return [0x00, 0x02] + (0..<r).map { _ in UInt8.random(in: 1...UInt8.max) } + [0x00] + bytes
  68. }
  69. @inlinable
  70. func remove(from bytes: Array<UInt8>, blockSize _: Int?) -> Array<UInt8> {
  71. assert(!bytes.isEmpty, "Need bytes to remove padding")
  72. assert(bytes.prefix(2) == [0x00, 0x02], "Invalid padding prefix")
  73. guard let paddingLength = bytes.dropFirst(2).firstIndex(of: 0x00) else { return bytes }
  74. guard (paddingLength + 1) <= bytes.count else { return bytes }
  75. return Array(bytes[(paddingLength + 1)...])
  76. }
  77. }