block.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2009 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. // This Go implementation is derived in part from the reference
  5. // ANSI C implementation, which carries the following notice:
  6. //
  7. // rijndael-alg-fst.c
  8. //
  9. // @version 3.0 (December 2000)
  10. //
  11. // Optimised ANSI C code for the Rijndael cipher (now AES)
  12. //
  13. // @author Vincent Rijmen <[email protected]>
  14. // @author Antoon Bosselaers <[email protected]>
  15. // @author Paulo Barreto <[email protected]>
  16. //
  17. // This code is hereby placed in the public domain.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
  20. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
  23. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  26. // BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  27. // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  28. // OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  29. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. //
  31. // See FIPS 197 for specification, and see Daemen and Rijmen's Rijndael submission
  32. // for implementation details.
  33. // http://www.csrc.nist.gov/publications/fips/fips197/fips-197.pdf
  34. // http://csrc.nist.gov/archive/aes/rijndael/Rijndael-ammended.pdf
  35. package aes12
  36. // Encrypt one block from src into dst, using the expanded key xk.
  37. func encryptBlockGo(xk []uint32, dst, src []byte) {
  38. var s0, s1, s2, s3, t0, t1, t2, t3 uint32
  39. s0 = uint32(src[0])<<24 | uint32(src[1])<<16 | uint32(src[2])<<8 | uint32(src[3])
  40. s1 = uint32(src[4])<<24 | uint32(src[5])<<16 | uint32(src[6])<<8 | uint32(src[7])
  41. s2 = uint32(src[8])<<24 | uint32(src[9])<<16 | uint32(src[10])<<8 | uint32(src[11])
  42. s3 = uint32(src[12])<<24 | uint32(src[13])<<16 | uint32(src[14])<<8 | uint32(src[15])
  43. // First round just XORs input with key.
  44. s0 ^= xk[0]
  45. s1 ^= xk[1]
  46. s2 ^= xk[2]
  47. s3 ^= xk[3]
  48. // Middle rounds shuffle using tables.
  49. // Number of rounds is set by length of expanded key.
  50. nr := len(xk)/4 - 2 // - 2: one above, one more below
  51. k := 4
  52. for r := 0; r < nr; r++ {
  53. t0 = xk[k+0] ^ te0[uint8(s0>>24)] ^ te1[uint8(s1>>16)] ^ te2[uint8(s2>>8)] ^ te3[uint8(s3)]
  54. t1 = xk[k+1] ^ te0[uint8(s1>>24)] ^ te1[uint8(s2>>16)] ^ te2[uint8(s3>>8)] ^ te3[uint8(s0)]
  55. t2 = xk[k+2] ^ te0[uint8(s2>>24)] ^ te1[uint8(s3>>16)] ^ te2[uint8(s0>>8)] ^ te3[uint8(s1)]
  56. t3 = xk[k+3] ^ te0[uint8(s3>>24)] ^ te1[uint8(s0>>16)] ^ te2[uint8(s1>>8)] ^ te3[uint8(s2)]
  57. k += 4
  58. s0, s1, s2, s3 = t0, t1, t2, t3
  59. }
  60. // Last round uses s-box directly and XORs to produce output.
  61. s0 = uint32(sbox0[t0>>24])<<24 | uint32(sbox0[t1>>16&0xff])<<16 | uint32(sbox0[t2>>8&0xff])<<8 | uint32(sbox0[t3&0xff])
  62. s1 = uint32(sbox0[t1>>24])<<24 | uint32(sbox0[t2>>16&0xff])<<16 | uint32(sbox0[t3>>8&0xff])<<8 | uint32(sbox0[t0&0xff])
  63. s2 = uint32(sbox0[t2>>24])<<24 | uint32(sbox0[t3>>16&0xff])<<16 | uint32(sbox0[t0>>8&0xff])<<8 | uint32(sbox0[t1&0xff])
  64. s3 = uint32(sbox0[t3>>24])<<24 | uint32(sbox0[t0>>16&0xff])<<16 | uint32(sbox0[t1>>8&0xff])<<8 | uint32(sbox0[t2&0xff])
  65. s0 ^= xk[k+0]
  66. s1 ^= xk[k+1]
  67. s2 ^= xk[k+2]
  68. s3 ^= xk[k+3]
  69. dst[0], dst[1], dst[2], dst[3] = byte(s0>>24), byte(s0>>16), byte(s0>>8), byte(s0)
  70. dst[4], dst[5], dst[6], dst[7] = byte(s1>>24), byte(s1>>16), byte(s1>>8), byte(s1)
  71. dst[8], dst[9], dst[10], dst[11] = byte(s2>>24), byte(s2>>16), byte(s2>>8), byte(s2)
  72. dst[12], dst[13], dst[14], dst[15] = byte(s3>>24), byte(s3>>16), byte(s3>>8), byte(s3)
  73. }
  74. // Decrypt one block from src into dst, using the expanded key xk.
  75. func decryptBlockGo(xk []uint32, dst, src []byte) {
  76. var s0, s1, s2, s3, t0, t1, t2, t3 uint32
  77. s0 = uint32(src[0])<<24 | uint32(src[1])<<16 | uint32(src[2])<<8 | uint32(src[3])
  78. s1 = uint32(src[4])<<24 | uint32(src[5])<<16 | uint32(src[6])<<8 | uint32(src[7])
  79. s2 = uint32(src[8])<<24 | uint32(src[9])<<16 | uint32(src[10])<<8 | uint32(src[11])
  80. s3 = uint32(src[12])<<24 | uint32(src[13])<<16 | uint32(src[14])<<8 | uint32(src[15])
  81. // First round just XORs input with key.
  82. s0 ^= xk[0]
  83. s1 ^= xk[1]
  84. s2 ^= xk[2]
  85. s3 ^= xk[3]
  86. // Middle rounds shuffle using tables.
  87. // Number of rounds is set by length of expanded key.
  88. nr := len(xk)/4 - 2 // - 2: one above, one more below
  89. k := 4
  90. for r := 0; r < nr; r++ {
  91. t0 = xk[k+0] ^ td0[uint8(s0>>24)] ^ td1[uint8(s3>>16)] ^ td2[uint8(s2>>8)] ^ td3[uint8(s1)]
  92. t1 = xk[k+1] ^ td0[uint8(s1>>24)] ^ td1[uint8(s0>>16)] ^ td2[uint8(s3>>8)] ^ td3[uint8(s2)]
  93. t2 = xk[k+2] ^ td0[uint8(s2>>24)] ^ td1[uint8(s1>>16)] ^ td2[uint8(s0>>8)] ^ td3[uint8(s3)]
  94. t3 = xk[k+3] ^ td0[uint8(s3>>24)] ^ td1[uint8(s2>>16)] ^ td2[uint8(s1>>8)] ^ td3[uint8(s0)]
  95. k += 4
  96. s0, s1, s2, s3 = t0, t1, t2, t3
  97. }
  98. // Last round uses s-box directly and XORs to produce output.
  99. s0 = uint32(sbox1[t0>>24])<<24 | uint32(sbox1[t3>>16&0xff])<<16 | uint32(sbox1[t2>>8&0xff])<<8 | uint32(sbox1[t1&0xff])
  100. s1 = uint32(sbox1[t1>>24])<<24 | uint32(sbox1[t0>>16&0xff])<<16 | uint32(sbox1[t3>>8&0xff])<<8 | uint32(sbox1[t2&0xff])
  101. s2 = uint32(sbox1[t2>>24])<<24 | uint32(sbox1[t1>>16&0xff])<<16 | uint32(sbox1[t0>>8&0xff])<<8 | uint32(sbox1[t3&0xff])
  102. s3 = uint32(sbox1[t3>>24])<<24 | uint32(sbox1[t2>>16&0xff])<<16 | uint32(sbox1[t1>>8&0xff])<<8 | uint32(sbox1[t0&0xff])
  103. s0 ^= xk[k+0]
  104. s1 ^= xk[k+1]
  105. s2 ^= xk[k+2]
  106. s3 ^= xk[k+3]
  107. dst[0], dst[1], dst[2], dst[3] = byte(s0>>24), byte(s0>>16), byte(s0>>8), byte(s0)
  108. dst[4], dst[5], dst[6], dst[7] = byte(s1>>24), byte(s1>>16), byte(s1>>8), byte(s1)
  109. dst[8], dst[9], dst[10], dst[11] = byte(s2>>24), byte(s2>>16), byte(s2>>8), byte(s2)
  110. dst[12], dst[13], dst[14], dst[15] = byte(s3>>24), byte(s3>>16), byte(s3>>8), byte(s3)
  111. }
  112. // Apply sbox0 to each byte in w.
  113. func subw(w uint32) uint32 {
  114. return uint32(sbox0[w>>24])<<24 |
  115. uint32(sbox0[w>>16&0xff])<<16 |
  116. uint32(sbox0[w>>8&0xff])<<8 |
  117. uint32(sbox0[w&0xff])
  118. }
  119. // Rotate
  120. func rotw(w uint32) uint32 { return w<<8 | w>>24 }
  121. // Key expansion algorithm. See FIPS-197, Figure 11.
  122. // Their rcon[i] is our powx[i-1] << 24.
  123. func expandKeyGo(key []byte, enc, dec []uint32) {
  124. // Encryption key setup.
  125. var i int
  126. nk := len(key) / 4
  127. for i = 0; i < nk; i++ {
  128. enc[i] = uint32(key[4*i])<<24 | uint32(key[4*i+1])<<16 | uint32(key[4*i+2])<<8 | uint32(key[4*i+3])
  129. }
  130. for ; i < len(enc); i++ {
  131. t := enc[i-1]
  132. if i%nk == 0 {
  133. t = subw(rotw(t)) ^ (uint32(powx[i/nk-1]) << 24)
  134. } else if nk > 6 && i%nk == 4 {
  135. t = subw(t)
  136. }
  137. enc[i] = enc[i-nk] ^ t
  138. }
  139. // Derive decryption key from encryption key.
  140. // Reverse the 4-word round key sets from enc to produce dec.
  141. // All sets but the first and last get the MixColumn transform applied.
  142. if dec == nil {
  143. return
  144. }
  145. n := len(enc)
  146. for i := 0; i < n; i += 4 {
  147. ei := n - i - 4
  148. for j := 0; j < 4; j++ {
  149. x := enc[ei+j]
  150. if i > 0 && i+4 < n {
  151. x = td0[sbox0[x>>24]] ^ td1[sbox0[x>>16&0xff]] ^ td2[sbox0[x>>8&0xff]] ^ td3[sbox0[x&0xff]]
  152. }
  153. dec[i+j] = x
  154. }
  155. }
  156. }