You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

146 lines
4.6 KiB

  1. // Copyright 2012 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 xts implements the XTS cipher mode as specified in IEEE P1619/D16.
  5. //
  6. // XTS mode is typically used for disk encryption, which presents a number of
  7. // novel problems that make more common modes inapplicable. The disk is
  8. // conceptually an array of sectors and we must be able to encrypt and decrypt
  9. // a sector in isolation. However, an attacker must not be able to transpose
  10. // two sectors of plaintext by transposing their ciphertext.
  11. //
  12. // XTS wraps a block cipher with Rogaway's XEX mode in order to build a
  13. // tweakable block cipher. This allows each sector to have a unique tweak and
  14. // effectively create a unique key for each sector.
  15. //
  16. // XTS does not provide any authentication. An attacker can manipulate the
  17. // ciphertext and randomise a block (16 bytes) of the plaintext.
  18. //
  19. // (Note: this package does not implement ciphertext-stealing so sectors must
  20. // be a multiple of 16 bytes.)
  21. package xts // import "golang.org/x/crypto/xts"
  22. import (
  23. "crypto/cipher"
  24. "encoding/binary"
  25. "errors"
  26. "golang.org/x/crypto/internal/subtle"
  27. )
  28. // Cipher contains an expanded key structure. It doesn't contain mutable state
  29. // and therefore can be used concurrently.
  30. type Cipher struct {
  31. k1, k2 cipher.Block
  32. }
  33. // blockSize is the block size that the underlying cipher must have. XTS is
  34. // only defined for 16-byte ciphers.
  35. const blockSize = 16
  36. // NewCipher creates a Cipher given a function for creating the underlying
  37. // block cipher (which must have a block size of 16 bytes). The key must be
  38. // twice the length of the underlying cipher's key.
  39. func NewCipher(cipherFunc func([]byte) (cipher.Block, error), key []byte) (c *Cipher, err error) {
  40. c = new(Cipher)
  41. if c.k1, err = cipherFunc(key[:len(key)/2]); err != nil {
  42. return
  43. }
  44. c.k2, err = cipherFunc(key[len(key)/2:])
  45. if c.k1.BlockSize() != blockSize {
  46. err = errors.New("xts: cipher does not have a block size of 16")
  47. }
  48. return
  49. }
  50. // Encrypt encrypts a sector of plaintext and puts the result into ciphertext.
  51. // Plaintext and ciphertext must overlap entirely or not at all.
  52. // Sectors must be a multiple of 16 bytes and less than 2²⁴ bytes.
  53. func (c *Cipher) Encrypt(ciphertext, plaintext []byte, sectorNum uint64) {
  54. if len(ciphertext) < len(plaintext) {
  55. panic("xts: ciphertext is smaller than plaintext")
  56. }
  57. if len(plaintext)%blockSize != 0 {
  58. panic("xts: plaintext is not a multiple of the block size")
  59. }
  60. if subtle.InexactOverlap(ciphertext[:len(plaintext)], plaintext) {
  61. panic("xts: invalid buffer overlap")
  62. }
  63. var tweak [blockSize]byte
  64. binary.LittleEndian.PutUint64(tweak[:8], sectorNum)
  65. c.k2.Encrypt(tweak[:], tweak[:])
  66. for len(plaintext) > 0 {
  67. for j := range tweak {
  68. ciphertext[j] = plaintext[j] ^ tweak[j]
  69. }
  70. c.k1.Encrypt(ciphertext, ciphertext)
  71. for j := range tweak {
  72. ciphertext[j] ^= tweak[j]
  73. }
  74. plaintext = plaintext[blockSize:]
  75. ciphertext = ciphertext[blockSize:]
  76. mul2(&tweak)
  77. }
  78. }
  79. // Decrypt decrypts a sector of ciphertext and puts the result into plaintext.
  80. // Plaintext and ciphertext must overlap entirely or not at all.
  81. // Sectors must be a multiple of 16 bytes and less than 2²⁴ bytes.
  82. func (c *Cipher) Decrypt(plaintext, ciphertext []byte, sectorNum uint64) {
  83. if len(plaintext) < len(ciphertext) {
  84. panic("xts: plaintext is smaller than ciphertext")
  85. }
  86. if len(ciphertext)%blockSize != 0 {
  87. panic("xts: ciphertext is not a multiple of the block size")
  88. }
  89. if subtle.InexactOverlap(plaintext[:len(ciphertext)], ciphertext) {
  90. panic("xts: invalid buffer overlap")
  91. }
  92. var tweak [blockSize]byte
  93. binary.LittleEndian.PutUint64(tweak[:8], sectorNum)
  94. c.k2.Encrypt(tweak[:], tweak[:])
  95. for len(ciphertext) > 0 {
  96. for j := range tweak {
  97. plaintext[j] = ciphertext[j] ^ tweak[j]
  98. }
  99. c.k1.Decrypt(plaintext, plaintext)
  100. for j := range tweak {
  101. plaintext[j] ^= tweak[j]
  102. }
  103. plaintext = plaintext[blockSize:]
  104. ciphertext = ciphertext[blockSize:]
  105. mul2(&tweak)
  106. }
  107. }
  108. // mul2 multiplies tweak by 2 in GF(2¹²⁸) with an irreducible polynomial of
  109. // x¹²⁸ + x⁷ + x² + x + 1.
  110. func mul2(tweak *[blockSize]byte) {
  111. var carryIn byte
  112. for j := range tweak {
  113. carryOut := tweak[j] >> 7
  114. tweak[j] = (tweak[j] << 1) + carryIn
  115. carryIn = carryOut
  116. }
  117. if carryIn != 0 {
  118. // If we have a carry bit then we need to subtract a multiple
  119. // of the irreducible polynomial (x¹²⁸ + x⁷ + x² + x + 1).
  120. // By dropping the carry bit, we're subtracting the x^128 term
  121. // so all that remains is to subtract x⁷ + x² + x + 1.
  122. // Subtraction (and addition) in this representation is just
  123. // XOR.
  124. tweak[0] ^= 1<<7 | 1<<2 | 1<<1 | 1
  125. }
  126. }