Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overload resolution regression #11375

Closed
Araq opened this issue Jun 1, 2019 · 0 comments

Comments

Projects
None yet
1 participant
@Araq
Copy link
Member

commented Jun 1, 2019

The following used to compile but doesn't anymore:

type
  Sha2Context*[bits: static[int],
               bsize: static[int],
               T: uint32|uint64] = object
    count: array[2, T]
    state: array[8, T]
    buffer: array[bsize, byte]

  sha224* = Sha2Context[224, 64, uint32]
  sha256* = Sha2Context[256, 64, uint32]
  sha384* = Sha2Context[384, 128, uint64]
  sha512* = Sha2Context[512, 128, uint64]
  sha512_224* = Sha2Context[224, 128, uint64]
  sha512_256* = Sha2Context[256, 128, uint64]
  sha2* = sha224 | sha256 | sha384 | sha512 | sha512_224 | sha512_256

type
  RipemdContext*[bits: static[int]] = object
    count: array[2, uint32]
    state: array[bits div 32, uint32]
    buffer: array[64, byte]

  ripemd128* = RipemdContext[128]
  ripemd160* = RipemdContext[160]
  ripemd256* = RipemdContext[256]
  ripemd320* = RipemdContext[320]
  ripemd* = ripemd128 | ripemd160 | ripemd256 | ripemd320

const
  MaxHmacBlockSize = 256

type
  HMAC*[HashType] = object
    ## HMAC context object.
    mdctx: HashType
    opadctx: HashType

template sizeBlock*(h: HMAC[Sha2Context]): uint =
  ## Size of processing block in octets (bytes), while perform HMAC
  ## operation using SHA2 algorithms.
  cast[uint](h.HashType.sizeBlock)

template sizeBlock*(h: HMAC[RipemdContext]): uint =
  ## Size of processing block in octets (bytes), while perform HMAC
  ## operation using RIPEMD algorithms.
  cast[uint](h.HashType.sizeBlock)

proc init*[T](hmctx: var HMAC[T], key: ptr byte, ulen: uint) =
  ## Initialize HMAC context ``hmctx`` with key using ``key`` and size ``ulen``.
  ##
  ## ``key`` can be ``nil``.
  mixin init, update, finish
  var k: array[MaxHmacBlockSize, byte]
  var ipad: array[MaxHmacBlockSize, byte]
  var opad: array[MaxHmacBlockSize, byte]
  const sizeBlock = hmctx.sizeBlock

  hmctx.mdctx = T()
  hmctx.opadctx = T()
  init(hmctx.opadctx)

  if not isNil(key):
    if ulen > sizeBlock:
      init(hmctx.mdctx)
      update(hmctx.mdctx, key, ulen)
      discard finish(hmctx.mdctx, addr k[0], sizeBlock)
    else:
      if ulen > 0'u: copyMem(addr k[0], key, ulen)

  for i in 0..<int(sizeBlock):
    opad[i] = 0x5C'u8 xor k[i]
    ipad[i] = 0x36'u8 xor k[i]

  init(hmctx.mdctx)
  update(hmctx.mdctx, addr ipad[0], sizeBlock)
  update(hmctx.opadctx, addr opad[0], sizeBlock)

proc hmac*[A, B](HashType: typedesc, key: openarray[A],
                 data: openarray[B],
                 ostart: int = 0, ofinish: int = -1) =
  var ctx: HMAC[HashType]
  let so = if ostart < 0: (len(data) + ostart) else: ostart
  let eo = if ofinish < 0: (len(data) + ofinish) else: ofinish
  let length = (eo - so + 1) * sizeof(B)
  ctx.init(nil, 0)

discard sha512.hmac("", "")

Araq added a commit that referenced this issue Jun 1, 2019

@Araq Araq closed this in 4a35d2b Jun 2, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.