cipher.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package des
  5. import (
  6. "crypto/cipher"
  7. "crypto/internal/subtle"
  8. "encoding/binary"
  9. "strconv"
  10. )
  11. // The DES block size in bytes.
  12. const BlockSize = 8
  13. type KeySizeError int
  14. func (k KeySizeError) Error() string {
  15. return "crypto/des: invalid key size " + strconv.Itoa(int(k))
  16. }
  17. // desCipher is an instance of DES encryption.
  18. type desCipher struct {
  19. subkeys [16]uint64
  20. }
  21. // NewCipher creates and returns a new cipher.Block.
  22. func NewCipher(key []byte) (cipher.Block, error) {
  23. if len(key) != 8 {
  24. return nil, KeySizeError(len(key))
  25. }
  26. c := new(desCipher)
  27. c.generateSubkeys(key)
  28. return c, nil
  29. }
  30. func (c *desCipher) BlockSize() int { return BlockSize }
  31. func (c *desCipher) Encrypt(dst, src []byte) {
  32. if len(src) < BlockSize {
  33. panic("crypto/des: input not full block")
  34. }
  35. if len(dst) < BlockSize {
  36. panic("crypto/des: output not full block")
  37. }
  38. if subtle.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
  39. panic("crypto/des: invalid buffer overlap")
  40. }
  41. encryptBlock(c.subkeys[:], dst, src)
  42. }
  43. func (c *desCipher) Decrypt(dst, src []byte) {
  44. if len(src) < BlockSize {
  45. panic("crypto/des: input not full block")
  46. }
  47. if len(dst) < BlockSize {
  48. panic("crypto/des: output not full block")
  49. }
  50. if subtle.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
  51. panic("crypto/des: invalid buffer overlap")
  52. }
  53. decryptBlock(c.subkeys[:], dst, src)
  54. }
  55. // A tripleDESCipher is an instance of TripleDES encryption.
  56. type tripleDESCipher struct {
  57. cipher1, cipher2, cipher3 desCipher
  58. }
  59. // NewTripleDESCipher creates and returns a new cipher.Block.
  60. func NewTripleDESCipher(key []byte) (cipher.Block, error) {
  61. if len(key) != 24 {
  62. return nil, KeySizeError(len(key))
  63. }
  64. c := new(tripleDESCipher)
  65. c.cipher1.generateSubkeys(key[:8])
  66. c.cipher2.generateSubkeys(key[8:16])
  67. c.cipher3.generateSubkeys(key[16:])
  68. return c, nil
  69. }
  70. func (c *tripleDESCipher) BlockSize() int { return BlockSize }
  71. func (c *tripleDESCipher) Encrypt(dst, src []byte) {
  72. if len(src) < BlockSize {
  73. panic("crypto/des: input not full block")
  74. }
  75. if len(dst) < BlockSize {
  76. panic("crypto/des: output not full block")
  77. }
  78. if subtle.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
  79. panic("crypto/des: invalid buffer overlap")
  80. }
  81. b := binary.BigEndian.Uint64(src)
  82. b = permuteInitialBlock(b)
  83. left, right := uint32(b>>32), uint32(b)
  84. left = (left << 1) | (left >> 31)
  85. right = (right << 1) | (right >> 31)
  86. for i := 0; i < 8; i++ {
  87. left, right = feistel(left, right, c.cipher1.subkeys[2*i], c.cipher1.subkeys[2*i+1])
  88. }
  89. for i := 0; i < 8; i++ {
  90. right, left = feistel(right, left, c.cipher2.subkeys[15-2*i], c.cipher2.subkeys[15-(2*i+1)])
  91. }
  92. for i := 0; i < 8; i++ {
  93. left, right = feistel(left, right, c.cipher3.subkeys[2*i], c.cipher3.subkeys[2*i+1])
  94. }
  95. left = (left << 31) | (left >> 1)
  96. right = (right << 31) | (right >> 1)
  97. preOutput := (uint64(right) << 32) | uint64(left)
  98. binary.BigEndian.PutUint64(dst, permuteFinalBlock(preOutput))
  99. }
  100. func (c *tripleDESCipher) Decrypt(dst, src []byte) {
  101. if len(src) < BlockSize {
  102. panic("crypto/des: input not full block")
  103. }
  104. if len(dst) < BlockSize {
  105. panic("crypto/des: output not full block")
  106. }
  107. if subtle.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
  108. panic("crypto/des: invalid buffer overlap")
  109. }
  110. b := binary.BigEndian.Uint64(src)
  111. b = permuteInitialBlock(b)
  112. left, right := uint32(b>>32), uint32(b)
  113. left = (left << 1) | (left >> 31)
  114. right = (right << 1) | (right >> 31)
  115. for i := 0; i < 8; i++ {
  116. left, right = feistel(left, right, c.cipher3.subkeys[15-2*i], c.cipher3.subkeys[15-(2*i+1)])
  117. }
  118. for i := 0; i < 8; i++ {
  119. right, left = feistel(right, left, c.cipher2.subkeys[2*i], c.cipher2.subkeys[2*i+1])
  120. }
  121. for i := 0; i < 8; i++ {
  122. left, right = feistel(left, right, c.cipher1.subkeys[15-2*i], c.cipher1.subkeys[15-(2*i+1)])
  123. }
  124. left = (left << 31) | (left >> 1)
  125. right = (right << 31) | (right >> 1)
  126. preOutput := (uint64(right) << 32) | uint64(left)
  127. binary.BigEndian.PutUint64(dst, permuteFinalBlock(preOutput))
  128. }