block.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. "encoding/binary"
  7. )
  8. func cryptBlock(subkeys []uint64, dst, src []byte, decrypt bool) {
  9. b := binary.BigEndian.Uint64(src)
  10. b = permuteInitialBlock(b)
  11. left, right := uint32(b>>32), uint32(b)
  12. var subkey uint64
  13. for i := 0; i < 16; i++ {
  14. if decrypt {
  15. subkey = subkeys[15-i]
  16. } else {
  17. subkey = subkeys[i]
  18. }
  19. left, right = right, left^feistel(right, subkey)
  20. }
  21. // switch left & right and perform final permutation
  22. preOutput := (uint64(right) << 32) | uint64(left)
  23. binary.BigEndian.PutUint64(dst, permuteFinalBlock(preOutput))
  24. }
  25. // Encrypt one block from src into dst, using the subkeys.
  26. func encryptBlock(subkeys []uint64, dst, src []byte) {
  27. cryptBlock(subkeys, dst, src, false)
  28. }
  29. // Decrypt one block from src into dst, using the subkeys.
  30. func decryptBlock(subkeys []uint64, dst, src []byte) {
  31. cryptBlock(subkeys, dst, src, true)
  32. }
  33. // DES Feistel function
  34. func feistel(right uint32, key uint64) (result uint32) {
  35. sBoxLocations := key ^ expandBlock(right)
  36. var sBoxResult uint32
  37. for i := uint8(0); i < 8; i++ {
  38. sBoxLocation := uint8(sBoxLocations>>42) & 0x3f
  39. sBoxLocations <<= 6
  40. // row determined by 1st and 6th bit
  41. // column is middle four bits
  42. row := (sBoxLocation & 0x1) | ((sBoxLocation & 0x20) >> 4)
  43. column := (sBoxLocation >> 1) & 0xf
  44. sBoxResult ^= feistelBox[i][16*row+column]
  45. }
  46. return sBoxResult
  47. }
  48. // feistelBox[s][16*i+j] contains the output of permutationFunction
  49. // for sBoxes[s][i][j] << 4*(7-s)
  50. var feistelBox [8][64]uint32
  51. // general purpose function to perform DES block permutations
  52. func permuteBlock(src uint64, permutation []uint8) (block uint64) {
  53. for position, n := range permutation {
  54. bit := (src >> n) & 1
  55. block |= bit << uint((len(permutation)-1)-position)
  56. }
  57. return
  58. }
  59. func init() {
  60. for s := range sBoxes {
  61. for i := 0; i < 4; i++ {
  62. for j := 0; j < 16; j++ {
  63. f := uint64(sBoxes[s][i][j]) << (4 * (7 - uint(s)))
  64. f = permuteBlock(uint64(f), permutationFunction[:])
  65. feistelBox[s][16*i+j] = uint32(f)
  66. }
  67. }
  68. }
  69. }
  70. // expandBlock expands an input block of 32 bits,
  71. // producing an output block of 48 bits.
  72. func expandBlock(src uint32) (block uint64) {
  73. // rotate the 5 highest bits to the right.
  74. src = (src << 5) | (src >> 27)
  75. for i := 0; i < 8; i++ {
  76. block <<= 6
  77. // take the 6 bits on the right
  78. block |= uint64(src) & (1<<6 - 1)
  79. // advance by 4 bits.
  80. src = (src << 4) | (src >> 28)
  81. }
  82. return
  83. }
  84. // permuteInitialBlock is equivalent to the permutation defined
  85. // by initialPermutation.
  86. func permuteInitialBlock(block uint64) uint64 {
  87. // block = b7 b6 b5 b4 b3 b2 b1 b0 (8 bytes)
  88. b1 := block >> 48
  89. b2 := block << 48
  90. block ^= b1 ^ b2 ^ b1<<48 ^ b2>>48
  91. // block = b1 b0 b5 b4 b3 b2 b7 b6
  92. b1 = block >> 32 & 0xff00ff
  93. b2 = (block & 0xff00ff00)
  94. block ^= b1<<32 ^ b2 ^ b1<<8 ^ b2<<24 // exchange b0 b4 with b3 b7
  95. // block is now b1 b3 b5 b7 b0 b2 b4 b7, the permutation:
  96. // ... 8
  97. // ... 24
  98. // ... 40
  99. // ... 56
  100. // 7 6 5 4 3 2 1 0
  101. // 23 22 21 20 19 18 17 16
  102. // ... 32
  103. // ... 48
  104. // exchange 4,5,6,7 with 32,33,34,35 etc.
  105. b1 = block & 0x0f0f00000f0f0000
  106. b2 = block & 0x0000f0f00000f0f0
  107. block ^= b1 ^ b2 ^ b1>>12 ^ b2<<12
  108. // block is the permutation:
  109. //
  110. // [+8] [+40]
  111. //
  112. // 7 6 5 4
  113. // 23 22 21 20
  114. // 3 2 1 0
  115. // 19 18 17 16 [+32]
  116. // exchange 0,1,4,5 with 18,19,22,23
  117. b1 = block & 0x3300330033003300
  118. b2 = block & 0x00cc00cc00cc00cc
  119. block ^= b1 ^ b2 ^ b1>>6 ^ b2<<6
  120. // block is the permutation:
  121. // 15 14
  122. // 13 12
  123. // 11 10
  124. // 9 8
  125. // 7 6
  126. // 5 4
  127. // 3 2
  128. // 1 0 [+16] [+32] [+64]
  129. // exchange 0,2,4,6 with 9,11,13,15:
  130. b1 = block & 0xaaaaaaaa55555555
  131. block ^= b1 ^ b1>>33 ^ b1<<33
  132. // block is the permutation:
  133. // 6 14 22 30 38 46 54 62
  134. // 4 12 20 28 36 44 52 60
  135. // 2 10 18 26 34 42 50 58
  136. // 0 8 16 24 32 40 48 56
  137. // 7 15 23 31 39 47 55 63
  138. // 5 13 21 29 37 45 53 61
  139. // 3 11 19 27 35 43 51 59
  140. // 1 9 17 25 33 41 49 57
  141. return block
  142. }
  143. // permuteInitialBlock is equivalent to the permutation defined
  144. // by finalPermutation.
  145. func permuteFinalBlock(block uint64) uint64 {
  146. // Perform the same bit exchanges as permuteInitialBlock
  147. // but in reverse order.
  148. b1 := block & 0xaaaaaaaa55555555
  149. block ^= b1 ^ b1>>33 ^ b1<<33
  150. b1 = block & 0x3300330033003300
  151. b2 := block & 0x00cc00cc00cc00cc
  152. block ^= b1 ^ b2 ^ b1>>6 ^ b2<<6
  153. b1 = block & 0x0f0f00000f0f0000
  154. b2 = block & 0x0000f0f00000f0f0
  155. block ^= b1 ^ b2 ^ b1>>12 ^ b2<<12
  156. b1 = block >> 32 & 0xff00ff
  157. b2 = (block & 0xff00ff00)
  158. block ^= b1<<32 ^ b2 ^ b1<<8 ^ b2<<24
  159. b1 = block >> 48
  160. b2 = block << 48
  161. block ^= b1 ^ b2 ^ b1<<48 ^ b2>>48
  162. return block
  163. }
  164. // creates 16 28-bit blocks rotated according
  165. // to the rotation schedule
  166. func ksRotate(in uint32) (out []uint32) {
  167. out = make([]uint32, 16)
  168. last := in
  169. for i := 0; i < 16; i++ {
  170. // 28-bit circular left shift
  171. left := (last << (4 + ksRotations[i])) >> 4
  172. right := (last << 4) >> (32 - ksRotations[i])
  173. out[i] = left | right
  174. last = out[i]
  175. }
  176. return
  177. }
  178. // creates 16 56-bit subkeys from the original key
  179. func (c *desCipher) generateSubkeys(keyBytes []byte) {
  180. // apply PC1 permutation to key
  181. key := binary.BigEndian.Uint64(keyBytes)
  182. permutedKey := permuteBlock(key, permutedChoice1[:])
  183. // rotate halves of permuted key according to the rotation schedule
  184. leftRotations := ksRotate(uint32(permutedKey >> 28))
  185. rightRotations := ksRotate(uint32(permutedKey<<4) >> 4)
  186. // generate subkeys
  187. for i := 0; i < 16; i++ {
  188. // combine halves to form 56-bit input to PC2
  189. pc2Input := uint64(leftRotations[i])<<28 | uint64(rightRotations[i])
  190. // apply PC2 permutation to 7 byte input
  191. c.subkeys[i] = permuteBlock(pc2Input, permutedChoice2[:])
  192. }
  193. }