aes.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // based on the aes implimentation in triple sec
  2. // https://github.com/keybase/triplesec
  3. // which is in turn based on the one from crypto-js
  4. // https://code.google.com/p/crypto-js/
  5. var Buffer = require('safe-buffer').Buffer
  6. function asUInt32Array (buf) {
  7. if (!Buffer.isBuffer(buf)) buf = Buffer.from(buf)
  8. var len = (buf.length / 4) | 0
  9. var out = new Array(len)
  10. for (var i = 0; i < len; i++) {
  11. out[i] = buf.readUInt32BE(i * 4)
  12. }
  13. return out
  14. }
  15. function scrubVec (v) {
  16. for (var i = 0; i < v.length; v++) {
  17. v[i] = 0
  18. }
  19. }
  20. function cryptBlock (M, keySchedule, SUB_MIX, SBOX, nRounds) {
  21. var SUB_MIX0 = SUB_MIX[0]
  22. var SUB_MIX1 = SUB_MIX[1]
  23. var SUB_MIX2 = SUB_MIX[2]
  24. var SUB_MIX3 = SUB_MIX[3]
  25. var s0 = M[0] ^ keySchedule[0]
  26. var s1 = M[1] ^ keySchedule[1]
  27. var s2 = M[2] ^ keySchedule[2]
  28. var s3 = M[3] ^ keySchedule[3]
  29. var t0, t1, t2, t3
  30. var ksRow = 4
  31. for (var round = 1; round < nRounds; round++) {
  32. t0 = SUB_MIX0[s0 >>> 24] ^ SUB_MIX1[(s1 >>> 16) & 0xff] ^ SUB_MIX2[(s2 >>> 8) & 0xff] ^ SUB_MIX3[s3 & 0xff] ^ keySchedule[ksRow++]
  33. t1 = SUB_MIX0[s1 >>> 24] ^ SUB_MIX1[(s2 >>> 16) & 0xff] ^ SUB_MIX2[(s3 >>> 8) & 0xff] ^ SUB_MIX3[s0 & 0xff] ^ keySchedule[ksRow++]
  34. t2 = SUB_MIX0[s2 >>> 24] ^ SUB_MIX1[(s3 >>> 16) & 0xff] ^ SUB_MIX2[(s0 >>> 8) & 0xff] ^ SUB_MIX3[s1 & 0xff] ^ keySchedule[ksRow++]
  35. t3 = SUB_MIX0[s3 >>> 24] ^ SUB_MIX1[(s0 >>> 16) & 0xff] ^ SUB_MIX2[(s1 >>> 8) & 0xff] ^ SUB_MIX3[s2 & 0xff] ^ keySchedule[ksRow++]
  36. s0 = t0
  37. s1 = t1
  38. s2 = t2
  39. s3 = t3
  40. }
  41. t0 = ((SBOX[s0 >>> 24] << 24) | (SBOX[(s1 >>> 16) & 0xff] << 16) | (SBOX[(s2 >>> 8) & 0xff] << 8) | SBOX[s3 & 0xff]) ^ keySchedule[ksRow++]
  42. t1 = ((SBOX[s1 >>> 24] << 24) | (SBOX[(s2 >>> 16) & 0xff] << 16) | (SBOX[(s3 >>> 8) & 0xff] << 8) | SBOX[s0 & 0xff]) ^ keySchedule[ksRow++]
  43. t2 = ((SBOX[s2 >>> 24] << 24) | (SBOX[(s3 >>> 16) & 0xff] << 16) | (SBOX[(s0 >>> 8) & 0xff] << 8) | SBOX[s1 & 0xff]) ^ keySchedule[ksRow++]
  44. t3 = ((SBOX[s3 >>> 24] << 24) | (SBOX[(s0 >>> 16) & 0xff] << 16) | (SBOX[(s1 >>> 8) & 0xff] << 8) | SBOX[s2 & 0xff]) ^ keySchedule[ksRow++]
  45. t0 = t0 >>> 0
  46. t1 = t1 >>> 0
  47. t2 = t2 >>> 0
  48. t3 = t3 >>> 0
  49. return [t0, t1, t2, t3]
  50. }
  51. // AES constants
  52. var RCON = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36]
  53. var G = (function () {
  54. // Compute double table
  55. var d = new Array(256)
  56. for (var j = 0; j < 256; j++) {
  57. if (j < 128) {
  58. d[j] = j << 1
  59. } else {
  60. d[j] = (j << 1) ^ 0x11b
  61. }
  62. }
  63. var SBOX = []
  64. var INV_SBOX = []
  65. var SUB_MIX = [[], [], [], []]
  66. var INV_SUB_MIX = [[], [], [], []]
  67. // Walk GF(2^8)
  68. var x = 0
  69. var xi = 0
  70. for (var i = 0; i < 256; ++i) {
  71. // Compute sbox
  72. var sx = xi ^ (xi << 1) ^ (xi << 2) ^ (xi << 3) ^ (xi << 4)
  73. sx = (sx >>> 8) ^ (sx & 0xff) ^ 0x63
  74. SBOX[x] = sx
  75. INV_SBOX[sx] = x
  76. // Compute multiplication
  77. var x2 = d[x]
  78. var x4 = d[x2]
  79. var x8 = d[x4]
  80. // Compute sub bytes, mix columns tables
  81. var t = (d[sx] * 0x101) ^ (sx * 0x1010100)
  82. SUB_MIX[0][x] = (t << 24) | (t >>> 8)
  83. SUB_MIX[1][x] = (t << 16) | (t >>> 16)
  84. SUB_MIX[2][x] = (t << 8) | (t >>> 24)
  85. SUB_MIX[3][x] = t
  86. // Compute inv sub bytes, inv mix columns tables
  87. t = (x8 * 0x1010101) ^ (x4 * 0x10001) ^ (x2 * 0x101) ^ (x * 0x1010100)
  88. INV_SUB_MIX[0][sx] = (t << 24) | (t >>> 8)
  89. INV_SUB_MIX[1][sx] = (t << 16) | (t >>> 16)
  90. INV_SUB_MIX[2][sx] = (t << 8) | (t >>> 24)
  91. INV_SUB_MIX[3][sx] = t
  92. if (x === 0) {
  93. x = xi = 1
  94. } else {
  95. x = x2 ^ d[d[d[x8 ^ x2]]]
  96. xi ^= d[d[xi]]
  97. }
  98. }
  99. return {
  100. SBOX: SBOX,
  101. INV_SBOX: INV_SBOX,
  102. SUB_MIX: SUB_MIX,
  103. INV_SUB_MIX: INV_SUB_MIX
  104. }
  105. })()
  106. function AES (key) {
  107. this._key = asUInt32Array(key)
  108. this._reset()
  109. }
  110. AES.blockSize = 4 * 4
  111. AES.keySize = 256 / 8
  112. AES.prototype.blockSize = AES.blockSize
  113. AES.prototype.keySize = AES.keySize
  114. AES.prototype._reset = function () {
  115. var keyWords = this._key
  116. var keySize = keyWords.length
  117. var nRounds = keySize + 6
  118. var ksRows = (nRounds + 1) * 4
  119. var keySchedule = []
  120. for (var k = 0; k < keySize; k++) {
  121. keySchedule[k] = keyWords[k]
  122. }
  123. for (k = keySize; k < ksRows; k++) {
  124. var t = keySchedule[k - 1]
  125. if (k % keySize === 0) {
  126. t = (t << 8) | (t >>> 24)
  127. t =
  128. (G.SBOX[t >>> 24] << 24) |
  129. (G.SBOX[(t >>> 16) & 0xff] << 16) |
  130. (G.SBOX[(t >>> 8) & 0xff] << 8) |
  131. (G.SBOX[t & 0xff])
  132. t ^= RCON[(k / keySize) | 0] << 24
  133. } else if (keySize > 6 && k % keySize === 4) {
  134. t =
  135. (G.SBOX[t >>> 24] << 24) |
  136. (G.SBOX[(t >>> 16) & 0xff] << 16) |
  137. (G.SBOX[(t >>> 8) & 0xff] << 8) |
  138. (G.SBOX[t & 0xff])
  139. }
  140. keySchedule[k] = keySchedule[k - keySize] ^ t
  141. }
  142. var invKeySchedule = []
  143. for (var ik = 0; ik < ksRows; ik++) {
  144. var ksR = ksRows - ik
  145. var tt = keySchedule[ksR - (ik % 4 ? 0 : 4)]
  146. if (ik < 4 || ksR <= 4) {
  147. invKeySchedule[ik] = tt
  148. } else {
  149. invKeySchedule[ik] =
  150. G.INV_SUB_MIX[0][G.SBOX[tt >>> 24]] ^
  151. G.INV_SUB_MIX[1][G.SBOX[(tt >>> 16) & 0xff]] ^
  152. G.INV_SUB_MIX[2][G.SBOX[(tt >>> 8) & 0xff]] ^
  153. G.INV_SUB_MIX[3][G.SBOX[tt & 0xff]]
  154. }
  155. }
  156. this._nRounds = nRounds
  157. this._keySchedule = keySchedule
  158. this._invKeySchedule = invKeySchedule
  159. }
  160. AES.prototype.encryptBlockRaw = function (M) {
  161. M = asUInt32Array(M)
  162. return cryptBlock(M, this._keySchedule, G.SUB_MIX, G.SBOX, this._nRounds)
  163. }
  164. AES.prototype.encryptBlock = function (M) {
  165. var out = this.encryptBlockRaw(M)
  166. var buf = Buffer.allocUnsafe(16)
  167. buf.writeUInt32BE(out[0], 0)
  168. buf.writeUInt32BE(out[1], 4)
  169. buf.writeUInt32BE(out[2], 8)
  170. buf.writeUInt32BE(out[3], 12)
  171. return buf
  172. }
  173. AES.prototype.decryptBlock = function (M) {
  174. M = asUInt32Array(M)
  175. // swap
  176. var m1 = M[1]
  177. M[1] = M[3]
  178. M[3] = m1
  179. var out = cryptBlock(M, this._invKeySchedule, G.INV_SUB_MIX, G.INV_SBOX, this._nRounds)
  180. var buf = Buffer.allocUnsafe(16)
  181. buf.writeUInt32BE(out[0], 0)
  182. buf.writeUInt32BE(out[3], 4)
  183. buf.writeUInt32BE(out[2], 8)
  184. buf.writeUInt32BE(out[1], 12)
  185. return buf
  186. }
  187. AES.prototype.scrub = function () {
  188. scrubVec(this._keySchedule)
  189. scrubVec(this._invKeySchedule)
  190. scrubVec(this._key)
  191. }
  192. module.exports.AES = AES