all repos — min @ 8f33ecdeaa50fda100ad7443070a8c6627a469a9

A small but practical concatenative programming language.

vendor/nimAES.nim

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
# AES, Rijndael Algorithm implementation written in nim
#
# Copyright (c) 2015 Andri Lim
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#
#-------------------------------------

import strutils

type
  AESTable = object
    FSb, RSb: array[0..255, uint8]
    FT0, FT1, FT2, FT3, RT0, RT1, RT2, RT3: array[0..255, uint32]
    RCON: array[0..9, uint32]

  AESContext* = object
    nr: int
    rk: int
    buf: array[0..67, uint32]

proc initAES*(): AESContext =
  result.nr = 0
  result.rk = 0
  for i in 0..result.buf.len-1: result.buf[i] = 0
  
proc ROTL8(x: uint32): uint32 =
  result = (x shl 8) or (x shr 24)

proc XTIME[T](x: T): T =
  result = x shl T(1)
  if (x and T(0x80)) != T(0): result = result xor T(0x1B)
  else: result = result xor T(0x00)

proc computeRoundConstant(): array[0..9, uint32] =
  var x = 1'u32
  for i in 0..9:
    result[i] = x
    x = XTIME(x) and 0xFF

#compute pow and log tables over GF(2xor8)
proc computePowLog(): tuple[pow: array[0..255, int], log: array[0..255, int]] =
  var x = 1
  for i in 0..255:
    result.pow[i] = x
    result.log[x] = i
    x = (x xor XTIME(x)) and 0xFF

proc MUL(x, y: uint8, pow, log: array[0..255, int]): uint32 =
  result = 0
  if x != 0 and y != 0: result = uint32(pow[((log[x]+log[y]) mod 255)])

proc computeTable*(): AESTable =
  let (pow, log) = computePowLog()
  result.RCON = computeRoundConstant()

  template srl(x, y: typed, s: untyped): untyped =
    y = ((y shl 1) or (y shr 7)) and 0xFF
    s

  result.FSb[0] = 0x63
  result.RSb[0x63] = 0
  for i in 1..255:
    var x = pow[255 - log[i]]
    var y = x
    srl(x, y): x = x xor y
    srl(x, y): x = x xor y
    srl(x, y): x = x xor y
    srl(x, y): x = x xor y xor 0x63
    result.FSb[i] = uint8(x)
    result.RSb[x] = uint8(i)

  # generate the forward and reverse tables
  for i in 0..255:
    let x = result.FSb[i]
    let y = XTIME(x) and 0xFF
    let z = (y xor x) and 0xFF

    result.FT0[i] = uint32(y) xor (uint32(x) shl 8) xor
      (uint32(x) shl 16) xor (uint32(z) shl 24)

    result.FT1[i] = ROTL8(result.FT0[i])
    result.FT2[i] = ROTL8(result.FT1[i])
    result.FT3[i] = ROTL8(result.FT2[i])

    let w = result.RSb[i]
    result.RT0[i] = MUL(0x0E, w, pow, log) xor (MUL(0x09, w, pow, log) shl 8) xor
      (MUL(0x0D, w, pow, log) shl 16) xor (MUL(0x0B, w, pow, log) shl 24)

    result.RT1[i] = ROTL8(result.RT0[i])
    result.RT2[i] = ROTL8(result.RT1[i])
    result.RT3[i] = ROTL8(result.RT2[i])

proc version(major, minor, patch: int): int {.compiletime.} =
  result = major+minor+patch

const compilerVersion = version(NimMajor,NimMinor,NimPatch)

when compilerVersion <= version(0,11,2):
  let SBOX = computeTable()
elif compilerVersion >= version(0,11,3):
  const SBOX = computeTable()

proc GET_ULONG_LE(b: cstring, i: int): uint32 =
  result = cast[uint32](ord(b[i]) or (ord(b[i+1]) shl 8) or (ord(b[i+2]) shl 16) or (ord(b[i+3]) shl 24))

proc PUT_ULONG_LE(n: uint32, b: var cstring, i: int) =
  b[i]   = chr(int(n and 0xFF))
  b[i+1] = chr(int((n shr 8) and 0xFF))
  b[i+2] = chr(int((n shr 16) and 0xFF))
  b[i+3] = chr(int((n shr 24) and 0xFF))

proc setEncodeKey*(ctx: var AESContext, key: string): bool =
  var keySize = key.len * 8
  zeroMem(addr(ctx), sizeof(ctx))

  case keySize:
  of 128: ctx.nr = 10
  of 192: ctx.nr = 12
  of 256: ctx.nr = 14
  else: return false

  let len = keySize div 32
  for i in 0..len-1: ctx.buf[i] = GET_ULONG_LE(cstring(key), i * 4)

  var RK = 0
  if ctx.nr == 10:
    for i in 0..9:
      ctx.buf[RK+4] = ctx.buf[RK+0] xor SBOX.RCON[i] xor
        uint32(SBOX.FSb[(int(ctx.buf[RK+3] shr 8) and 0xFF)]) xor
        (uint32(SBOX.FSb[(int(ctx.buf[RK+3] shr 16) and 0xFF)]) shl 8) xor
        (uint32(SBOX.FSb[(int(ctx.buf[RK+3] shr 24) and 0xFF)]) shl 16) xor
        (uint32(SBOX.FSb[(int(ctx.buf[RK+3]) and 0xFF)]) shl 24)

      ctx.buf[RK+5] = ctx.buf[RK+1] xor ctx.buf[RK+4]
      ctx.buf[RK+6] = ctx.buf[RK+2] xor ctx.buf[RK+5]
      ctx.buf[RK+7] = ctx.buf[RK+3] xor ctx.buf[RK+6]
      inc(RK, 4)

  elif ctx.nr == 12:
    for i in 0..7:
      ctx.buf[RK+6] = ctx.buf[RK+0] xor SBOX.RCON[i] xor
        uint32(SBOX.FSb[int(ctx.buf[RK+5] shr 8) and 0xFF]) xor
        (uint32(SBOX.FSb[int(ctx.buf[RK+5] shr 16) and 0xFF]) shl 8) xor
        (uint32(SBOX.FSb[int(ctx.buf[RK+5] shr 24) and 0xFF]) shl 16) xor
        (uint32(SBOX.FSb[int(ctx.buf[RK+5]) and 0xFF]) shl 24)

      ctx.buf[RK+7] = ctx.buf[RK+1] xor ctx.buf[RK+6]
      ctx.buf[RK+8] = ctx.buf[RK+2] xor ctx.buf[RK+7]
      ctx.buf[RK+9] = ctx.buf[RK+3] xor ctx.buf[RK+8]
      ctx.buf[RK+10] = ctx.buf[RK+4] xor ctx.buf[RK+9]
      ctx.buf[RK+11] = ctx.buf[RK+5] xor ctx.buf[RK+10]
      inc(RK, 6)

  elif ctx.nr == 14:
    for i in 0..6:
      ctx.buf[RK+8] = ctx.buf[RK+0] xor SBOX.RCON[i] xor
        uint32(SBOX.FSb[int(ctx.buf[RK+7] shr 8) and 0xFF]) xor
        (uint32(SBOX.FSb[int(ctx.buf[RK+7] shr 16) and 0xFF]) shl 8) xor
        (uint32(SBOX.FSb[int(ctx.buf[RK+7] shr 24) and 0xFF]) shl 16) xor
        (uint32(SBOX.FSb[int(ctx.buf[RK+7]) and 0xFF]) shl 24)

      ctx.buf[RK+9] = ctx.buf[RK+1] xor ctx.buf[RK+8]
      ctx.buf[RK+10] = ctx.buf[RK+2] xor ctx.buf[RK+9]
      ctx.buf[RK+11] = ctx.buf[RK+3] xor ctx.buf[RK+10]

      ctx.buf[RK+12] = ctx.buf[RK+4] xor
        uint32(SBOX.FSb[int(ctx.buf[RK+11]) and 0xFF]) xor
        (uint32(SBOX.FSb[int(ctx.buf[RK+11] shr 8) and 0xFF]) shl 8) xor
        (uint32(SBOX.FSb[int(ctx.buf[RK+11] shr 16) and 0xFF]) shl 16) xor
        (uint32(SBOX.FSb[int(ctx.buf[RK+11] shr 24) and 0xFF]) shl 24)

      ctx.buf[RK+13] = ctx.buf[RK+5] xor ctx.buf[RK+12]
      ctx.buf[RK+14] = ctx.buf[RK+6] xor ctx.buf[RK+13]
      ctx.buf[RK+15] = ctx.buf[RK+7] xor ctx.buf[RK+14]
      inc(RK, 8)

  result = true

proc setDecodeKey*(ctx: var AESContext, key: string): bool =
  var keySize = key.len * 8
  zeroMem(addr(ctx), sizeof(ctx))

  case keySize:
  of 128: ctx.nr = 10
  of 192: ctx.nr = 12
  of 256: ctx.nr = 14
  else: return false
  var cty: AESContext
  if not cty.setEncodeKey(key): return false
  var SK = cty.nr * 4
  var RK = 0

  ctx.buf[RK] = cty.buf[SK]
  ctx.buf[RK+1] = cty.buf[SK+1]
  ctx.buf[RK+2] = cty.buf[SK+2]
  ctx.buf[RK+3] = cty.buf[SK+3]
  inc(RK, 4)
  dec(SK, 4)

  for i in countdown(ctx.nr-1, 1):
    for j in 0..3:
      let YSK = cty.buf[SK]
      ctx.buf[RK] = SBOX.RT0[SBOX.FSb[int(YSK) and 0xFF]] xor
        SBOX.RT1[SBOX.FSb[int(YSK shr 8) and 0xFF]] xor
        SBOX.RT2[SBOX.FSb[int(YSK shr 16) and 0xFF]] xor
        SBOX.RT3[SBOX.FSb[int(YSK shr 24) and 0xFF]]
      inc SK
      inc RK
    dec(SK, 8)

  ctx.buf[RK] = cty.buf[SK]
  ctx.buf[RK+1] = cty.buf[SK+1]
  ctx.buf[RK+2] = cty.buf[SK+2]
  ctx.buf[RK+3] = cty.buf[SK+3]
  result = true

template AES_FROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3: typed): untyped =
  X0 = ctx.buf[RK] xor SBOX.FT0[int(Y0 and 0xFF)] xor
    SBOX.FT1[int((Y1 shr 8) and 0xFF)] xor
    SBOX.FT2[int((Y2 shr 16) and 0xFF)] xor
    SBOX.FT3[int((Y3 shr 24) and 0xFF)]
  inc RK

  X1 = ctx.buf[RK] xor SBOX.FT0[int(Y1 and 0xFF)] xor
    SBOX.FT1[int((Y2 shr 8) and 0xFF)] xor
    SBOX.FT2[int((Y3 shr 16) and 0xFF)] xor
    SBOX.FT3[int((Y0 shr 24) and 0xFF)]
  inc RK

  X2 = ctx.buf[RK] xor SBOX.FT0[int(Y2 and 0xFF)] xor
    SBOX.FT1[int((Y3 shr 8) and 0xFF)] xor
    SBOX.FT2[int((Y0 shr 16) and 0xFF)] xor
    SBOX.FT3[int((Y1 shr 24) and 0xFF)]
  inc RK

  X3 = ctx.buf[RK] xor SBOX.FT0[int(Y3 and 0xFF)] xor
    SBOX.FT1[int((Y0 shr 8) and 0xFF)] xor
    SBOX.FT2[int((Y1 shr 16) and 0xFF)] xor
    SBOX.FT3[int((Y2 shr 24) and 0xFF)]
  inc RK

template AES_RROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3: typed): untyped =
  X0 = ctx.buf[RK] xor SBOX.RT0[int(Y0 and 0xFF)] xor
    SBOX.RT1[int((Y3 shr 8) and 0xFF)] xor
    SBOX.RT2[int((Y2 shr 16) and 0xFF)] xor
    SBOX.RT3[int((Y1 shr 24) and 0xFF)]
  inc RK

  X1 = ctx.buf[RK] xor SBOX.RT0[int(Y1 and 0xFF)] xor
    SBOX.RT1[int((Y0 shr 8) and 0xFF)] xor
    SBOX.RT2[int((Y3 shr 16) and 0xFF)] xor
    SBOX.RT3[int((Y2 shr 24) and 0xFF)]
  inc RK

  X2 = ctx.buf[RK] xor SBOX.RT0[int(Y2  and 0xFF)] xor
    SBOX.RT1[int((Y1 shr 8) and 0xFF)] xor
    SBOX.RT2[int((Y0 shr 16) and 0xFF)] xor
    SBOX.RT3[int((Y3 shr 24) and 0xFF)]
  inc RK

  X3 = ctx.buf[RK] xor SBOX.RT0[int(Y3 and 0xFF)] xor
    SBOX.RT1[int((Y2 shr 8) and 0xFF)] xor
    SBOX.RT2[int((Y1 shr 16) and 0xFF)] xor
    SBOX.RT3[int((Y0 shr 24) and 0xFF)]
  inc RK

proc encryptECB*(ctx: AESContext, input: cstring, output: var cstring) =
  var X0, X1, X2, X3, Y0, Y1, Y2, Y3: uint32
  var RK = 0

  X0 = GET_ULONG_LE(input, 0)
  X1 = GET_ULONG_LE(input, 4)
  X2 = GET_ULONG_LE(input, 8)
  X3 = GET_ULONG_LE(input, 12)

  X0 = X0 xor ctx.buf[RK]
  X1 = X1 xor ctx.buf[RK+1]
  X2 = X2 xor ctx.buf[RK+2]
  X3 = X3 xor ctx.buf[RK+3]
  inc(RK, 4)

  for i in countdown((ctx.nr shr 1) - 1, 1):
    AES_FROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3)
    AES_FROUND(X0, X1, X2, X3, Y0, Y1, Y2, Y3)

  AES_FROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3)

  X0 = ctx.buf[RK] xor uint32(SBOX.FSb[int(Y0 and 0xFF)]) xor
    (uint32(SBOX.FSb[int((Y1 shr 8) and 0xFF)]) shl 8) xor
    (uint32(SBOX.FSb[int((Y2 shr 16) and 0xFF)]) shl 16) xor
    (uint32(SBOX.FSb[int((Y3 shr 24) and 0xFF)]) shl 24)
  inc RK

  X1 = ctx.buf[RK] xor uint32(SBOX.FSb[int(Y1 and 0xFF)]) xor
    (uint32(SBOX.FSb[int((Y2 shr 8) and 0xFF)]) shl 8) xor
    (uint32(SBOX.FSb[int((Y3 shr 16) and 0xFF)]) shl 16) xor
    (uint32(SBOX.FSb[int((Y0 shr 24) and 0xFF)]) shl 24)
  inc RK

  X2 = ctx.buf[RK] xor uint32(SBOX.FSb[int(Y2 and 0xFF)]) xor
    (uint32(SBOX.FSb[int((Y3 shr 8) and 0xFF)]) shl 8) xor
    (uint32(SBOX.FSb[int((Y0 shr 16) and 0xFF)]) shl 16) xor
    (uint32(SBOX.FSb[int((Y1 shr 24) and 0xFF)]) shl 24)
  inc RK

  X3 = ctx.buf[RK] xor uint32(SBOX.FSb[int(Y3 and 0xFF)]) xor
    (uint32(SBOX.FSb[int((Y0 shr 8) and 0xFF)]) shl 8) xor
    (uint32(SBOX.FSb[int((Y1 shr 16) and 0xFF)]) shl 16) xor
    (uint32(SBOX.FSb[int((Y2 shr 24) and 0xFF)]) shl 24)

  PUT_ULONG_LE(X0, output, 0)
  PUT_ULONG_LE(X1, output, 4)
  PUT_ULONG_LE(X2, output, 8)
  PUT_ULONG_LE(X3, output, 12)

proc encryptECB*(ctx: AESContext, input: string): string =
  assert input.len == 16
  result = newString(16)
  var output = cstring(result)
  ctx.encryptECB(cstring(input), output)

proc decryptECB*(ctx: AESContext, input: cstring, output: var cstring) =
  var X0, X1, X2, X3, Y0, Y1, Y2, Y3: uint32
  var RK = 0

  X0 = GET_ULONG_LE(input, 0)
  X1 = GET_ULONG_LE(input, 4)
  X2 = GET_ULONG_LE(input, 8)
  X3 = GET_ULONG_LE(input, 12)

  X0 = X0 xor ctx.buf[RK]
  X1 = X1 xor ctx.buf[RK+1]
  X2 = X2 xor ctx.buf[RK+2]
  X3 = X3 xor ctx.buf[RK+3]
  inc(RK, 4)

  for i in countdown((ctx.nr shr 1) - 1, 1):
    AES_RROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3)
    AES_RROUND(X0, X1, X2, X3, Y0, Y1, Y2, Y3)

  AES_RROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3)

  X0 = ctx.buf[RK] xor uint32(SBOX.RSb[int(Y0 and 0xFF)]) xor
    (uint32(SBOX.RSb[int((Y3 shr 8) and 0xFF)]) shl 8) xor
    (uint32(SBOX.RSb[int((Y2 shr 16) and 0xFF)]) shl 16) xor
    (uint32(SBOX.RSb[int((Y1 shr 24) and 0xFF)]) shl 24)
  inc RK

  X1 = ctx.buf[RK] xor uint32(SBOX.RSb[int(Y1 and 0xFF)]) xor
    (uint32(SBOX.RSb[int((Y0 shr 8) and 0xFF)]) shl 8) xor
    (uint32(SBOX.RSb[int((Y3 shr 16) and 0xFF)]) shl 16) xor
    (uint32(SBOX.RSb[int((Y2 shr 24) and 0xFF)]) shl 24)
  inc RK

  X2 = ctx.buf[RK] xor uint32(SBOX.RSb[int(Y2 and 0xFF)]) xor
    (uint32(SBOX.RSb[int((Y1 shr 8) and 0xFF)]) shl 8) xor
    (uint32(SBOX.RSb[int((Y0 shr 16) and 0xFF)]) shl 16) xor
    (uint32(SBOX.RSb[int((Y3 shr 24) and 0xFF)]) shl 24)
  inc RK

  X3 = ctx.buf[RK] xor uint32(SBOX.RSb[int(Y3 and 0xFF)]) xor
    (uint32(SBOX.RSb[int((Y2 shr 8) and 0xFF)]) shl 8) xor
    (uint32(SBOX.RSb[int((Y1 shr 16) and 0xFF)]) shl 16) xor
    (uint32(SBOX.RSb[int((Y0 shr 24) and 0xFF)]) shl 24)

  PUT_ULONG_LE(X0, output, 0)
  PUT_ULONG_LE(X1, output, 4)
  PUT_ULONG_LE(X2, output, 8)
  PUT_ULONG_LE(X3, output, 12)

proc decryptECB*(ctx: AESContext, input: string): string =
  assert input.len == 16
  result = newString(16)
  var output = cstring(result)
  ctx.decryptECB(cstring(input), output)
  
proc cryptOFB*(ctx: AESContext, nonce: var cstring, input: string): string =
  var len = input.len
  if (len mod 16) != 0: return nil

  result = newString(len)
  var x = 0
  while len > 0:
    var output = cast[cstring](addr(result[x]))
    encryptECB(ctx, nonce, output)
    copyMem(addr(nonce[0]), output, 16)

    for i in 0..15:
      output[i] = chr(ord(output[i]) xor ord(input[x+i]))

    inc(x, 16)
    dec(len, 16)

proc cryptOFB*(ctx: AESContext, nonce: var string, input: string): string =
  assert(nonce.len == 16)
  assert((input.len mod 16) == 0)
  var counter = cstring(nonce)
  result = ctx.cryptOFB(counter, input)

proc encryptCBC*(ctx: AESContext, iv: cstring, input: string): string =
  var len = input.len
  if (len mod 16) != 0: return nil

  result = newString(len)
  var x = 0
  while len > 0:
    var output = cast[cstring](addr(result[x]))

    for i in 0..15:
      output[i] = chr(ord(input[x+i]) xor ord(iv[i]))

    encryptECB(ctx, output, output)
    copyMem(iv, output, 16)

    inc(x, 16)
    dec(len, 16)

proc encryptCBC*(ctx: AESContext, iv: string, input: string): string =
  assert iv.len == 16
  result = ctx.encryptCBC(cstring(iv), input)

proc decryptCBC*(ctx: AESContext, iv: cstring, inp: string): string =
  var len = inp.len
  if (len mod 16) != 0: return nil

  var data = cstring(inp)
  result = newString(len)
  var x = 0
  var temp: array[0..15, char]
  while len > 0:
    var input = cast[cstring](addr(data[x]))
    var output = cast[cstring](addr(result[x]))
    copyMem(addr(temp[0]), input, 16)
    ctx.decryptECB(input, output)

    for i in 0..15:
      output[i] = chr(ord(output[i]) xor ord(iv[i]))

    copyMem(iv, addr(temp[0]), 16)

    inc(x, 16)
    dec(len, 16)

proc decryptCBC*(ctx: AESContext, iv: string, input: string): string =
  assert iv.len == 16
  result = ctx.decryptCBC(cstring(iv), input)
  
proc encryptCFB128*(ctx: AESContext, iv_off: var int, iv: var cstring, input: string): string =
  var n = iv_off
  var len = input.len
  var i = 0
  result = newString(len)

  while len > 0:
    if n == 0: encryptECB(ctx, iv, iv)
    iv[n] = chr( ord(iv[n]) xor ord(input[i]) )
    result[i] = iv[n]

    n = ( n + 1 ) and 0x0F
    dec len
    inc i

  iv_off = n

proc encryptCFB128*(ctx: AESContext, iv_off: var int, iv: var string, input: string): string =
  assert iv.len == 16
  var initVector = cstring(iv)
  result = ctx.encryptCFB128(iv_off, initVector, input)

proc decryptCFB128*(ctx: AESContext, iv_off: var int, iv: var cstring, input: string): string =
  var n = iv_off
  var len = input.len
  var i = 0
  result = newString(len)

  while len > 0:
    if n == 0: encryptECB(ctx, iv, iv)
    result[i] = chr(ord(input[i]) xor ord(iv[n]))
    iv[n] = input[i]

    n = ( n + 1 ) and 0x0F
    dec len
    inc i

  iv_off = n

proc decryptCFB128*(ctx: AESContext, iv_off: var int, iv: var string, input: string): string =
  assert iv.len == 16
  var initVector = cstring(iv)
  result = ctx.decryptCFB128(iv_off, initVector, input)

proc encryptCFB8*(ctx: AESContext, iv: var cstring, input: string): string =
  var len = input.len
  var i = 0
  result = newString(len)
  var ov: array[0..16, char]

  while len > 0:
    copyMem(addr(ov), iv, 16)
    encryptECB(ctx, iv, iv)
    result[i] = chr(ord(iv[0]) xor ord(input[i]))
    ov[16] = result[i]
    copyMem(iv, addr(ov[1]), 16)
    inc i
    dec len

proc encryptCFB8*(ctx: AESContext, iv: var string, input: string): string =
  assert iv.len == 16
  var initVector = cstring(iv)
  result = ctx.encryptCFB8(initVector, input)

proc decryptCFB8*(ctx: AESContext, iv: var cstring, input: string): string =
  var len = input.len
  var i = 0
  result = newString(len)
  var ov: array[0..16, char]

  while len > 0:
    copyMem(addr(ov), iv, 16)
    encryptECB(ctx, iv, iv)
    ov[16] = input[i]
    result[i] = chr(ord(iv[0]) xor ord(input[i]))
    copyMem(iv, addr(ov[1]), 16)
    inc i
    dec len

proc decryptCFB8*(ctx: AESContext, iv: var string, input: string): string =
  assert iv.len == 16
  var initVector = cstring(iv)
  result = ctx.decryptCFB8(initVector, input)

proc cryptCTR*(ctx: AESContext, nc_off: var int, nonce: var cstring, input: string): string =
  var n = nc_off
  var x = 0
  var len = input.len
  var counter = cast[ptr array[0..15, uint8]](nonce)

  var temp: array[0..15, uint8]
  var stream_block = cast[cstring](addr(temp[0]))
  result = newString(len)

  while len > 0:
    if n == 0:
      encryptECB(ctx, nonce, stream_block)
      for i in countdown(16, 1):
        counter[][i-1] += 1
        if counter[][i-1] != 0: break

    result[x] = chr(ord(input[x]) xor ord(stream_block[n]))

    n = ( n + 1 ) and 0x0F
    dec len
    inc x

  nc_off = n

proc cryptCTR*(ctx: AESContext, nc_off: var int, nonce: var string, input: string): string =
  assert nonce.len == 16
  var initVector = cstring(nonce)
  result = ctx.cryptCTR(nc_off, initVector, input)