xts.go 4.8 KB

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