# Path: axolotl/kdf/hkdf.py
# class HKDF(object):
#     __metaclass__ = abc.ABCMeta
#     HASH_OUTPUT_SIZE = 32
# 
#     @staticmethod
#     def createFor(messageVersion):
#         from .hkdfv2 import HKDFv2
#         from .hkdfv3 import HKDFv3
# 
#         if messageVersion == 2:
#             return HKDFv2()
#         elif messageVersion == 3:
#             return HKDFv3()
#         else:
#             raise AssertionError("Unknown version: %s " % messageVersion)
# 
#     def deriveSecrets(self, inputKeyMaterial, info, outputLength, salt=None):
#         salt = salt or bytearray(self.__class__.HASH_OUTPUT_SIZE)
#         prk = self.extract(salt, inputKeyMaterial)
#         return self.expand(prk, info, outputLength)
# 
#     def extract(self, salt, inputKeyMaterial):
#         mac = hmac.new(bytes(salt), digestmod=hashlib.sha256)
#         mac.update(bytes(inputKeyMaterial))
#         return mac.digest()
# 
#     def expand(self, prk, info, outputSize):
#         iterations = int(math.ceil(float(outputSize) / float(self.__class__.HASH_OUTPUT_SIZE)))
#         mixin = bytearray()
#         results = bytearray()
#         remainingBytes = outputSize
# 
#         for i in range(self.getIterationStartOffset(), iterations + self.getIterationStartOffset()):
#             mac = hmac.new(prk, digestmod=hashlib.sha256)
#             mac.update(bytes(mixin))
#             if info is not None:
#                 mac.update(bytes(info))
#             updateChr = chr(i % 256)
#             mac.update(updateChr.encode())
# 
#             stepResult = mac.digest()
#             stepSize = min(remainingBytes, len(stepResult))
#             results.extend(stepResult[:stepSize])
#             mixin = stepResult
#             remainingBytes -= stepSize
# 
#         return bytes(results)
# 
#     @abc.abstractmethod
#     def getIterationStartOffset(self):
#         return 0


import unittest
from ...kdf.hkdf import HKDF
                          0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd,
                          0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc,
                          0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb,
                          0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa,
                          0xfb, 0xfc, 0xfd, 0xfe, 0xff])

        okm = bytearray([0xb1, 0x1e, 0x39, 0x8d, 0xc8, 0x03, 0x27, 0xa1, 0xc8, 0xe7, 0xf7, 0x8c, 0x59, 0x6a, 0x49,
                         0x34, 0x4f, 0x01, 0x2e, 0xda, 0x2d, 0x4e, 0xfa, 0xd8, 0xa0, 0x50, 0xcc, 0x4c, 0x19, 0xaf,
                         0xa9, 0x7c, 0x59, 0x04, 0x5a, 0x99, 0xca, 0xc7, 0x82, 0x72, 0x71, 0xcb, 0x41, 0xc6, 0x5e,
                         0x59, 0x0e, 0x09, 0xda, 0x32, 0x75, 0x60, 0x0c, 0x2f, 0x09, 0xb8, 0x36, 0x77, 0x93, 0xa9,
                         0xac, 0xa3, 0xdb, 0x71, 0xcc, 0x30, 0xc5, 0x81, 0x79, 0xec, 0x3e, 0x87, 0xc1, 0x4c, 0x01,
                         0xd5, 0xc1, 0xf3, 0x43, 0x4f, 0x1d, 0x87])

        actualOutput = HKDF.createFor(3).deriveSecrets(ikm, info, 82, salt=salt)
        self.assertEqual(okm, actualOutput)

    def test_vectorV3_(self):
        ikm = bytearray([0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
                         0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b])

        salt = bytearray([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c])

        info = bytearray([0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9])

        okm = bytearray([0x6e, 0xc2, 0x55, 0x6d, 0x5d, 0x7b, 0x1d, 0x81, 0xde, 0xe4, 0x22, 0x2a, 0xd7, 0x48, 0x36,
                         0x95, 0xdd, 0xc9, 0x8f, 0x4f, 0x5f, 0xab, 0xc0, 0xe0, 0x20, 0x5d, 0xc2, 0xef, 0x87, 0x52,
                         0xd4, 0x1e, 0x04, 0xe2, 0xe2, 0x11, 0x01, 0xc6, 0x8f, 0xf0, 0x93, 0x94, 0xb8, 0xad, 0x0b,
                         0xdc, 0xb9, 0x60, 0x9c, 0xd4, 0xee, 0x82, 0xac, 0x13, 0x19, 0x9b, 0x4a, 0xa9, 0xfd, 0xa8,
                         0x99, 0xda, 0xeb, 0xec])


# Path: axolotl/kdf/hkdf.py
# class HKDF(object):
#     __metaclass__ = abc.ABCMeta
#     HASH_OUTPUT_SIZE = 32
# 
#     @staticmethod
#     def createFor(messageVersion):
#         from .hkdfv2 import HKDFv2
#         from .hkdfv3 import HKDFv3
# 
#         if messageVersion == 2:
#             return HKDFv2()
#         elif messageVersion == 3:
#             return HKDFv3()
#         else:
#             raise AssertionError("Unknown version: %s " % messageVersion)
# 
#     def deriveSecrets(self, inputKeyMaterial, info, outputLength, salt=None):
#         salt = salt or bytearray(self.__class__.HASH_OUTPUT_SIZE)
#         prk = self.extract(salt, inputKeyMaterial)
#         return self.expand(prk, info, outputLength)
# 
#     def extract(self, salt, inputKeyMaterial):
#         mac = hmac.new(bytes(salt), digestmod=hashlib.sha256)
#         mac.update(bytes(inputKeyMaterial))
#         return mac.digest()
# 
#     def expand(self, prk, info, outputSize):
#         iterations = int(math.ceil(float(outputSize) / float(self.__class__.HASH_OUTPUT_SIZE)))
#         mixin = bytearray()
#         results = bytearray()
#         remainingBytes = outputSize
# 
#         for i in range(self.getIterationStartOffset(), iterations + self.getIterationStartOffset()):
#             mac = hmac.new(prk, digestmod=hashlib.sha256)
#             mac.update(bytes(mixin))
#             if info is not None:
#                 mac.update(bytes(info))
#             updateChr = chr(i % 256)
#             mac.update(updateChr.encode())
# 
#             stepResult = mac.digest()
#             stepSize = min(remainingBytes, len(stepResult))
#             results.extend(stepResult[:stepSize])
#             mixin = stepResult
#             remainingBytes -= stepSize
# 
#         return bytes(results)
# 
#     @abc.abstractmethod
#     def getIterationStartOffset(self):
#         return 0


# Path: axolotl/tests/kdf/test_hkdf.py
import unittest
from ...kdf.hkdf import HKDF

                          0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd,
                          0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc,
                          0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb,
                          0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa,
                          0xfb, 0xfc, 0xfd, 0xfe, 0xff])

        okm = bytearray([0xb1, 0x1e, 0x39, 0x8d, 0xc8, 0x03, 0x27, 0xa1, 0xc8, 0xe7, 0xf7, 0x8c, 0x59, 0x6a, 0x49,
                         0x34, 0x4f, 0x01, 0x2e, 0xda, 0x2d, 0x4e, 0xfa, 0xd8, 0xa0, 0x50, 0xcc, 0x4c, 0x19, 0xaf,
                         0xa9, 0x7c, 0x59, 0x04, 0x5a, 0x99, 0xca, 0xc7, 0x82, 0x72, 0x71, 0xcb, 0x41, 0xc6, 0x5e,
                         0x59, 0x0e, 0x09, 0xda, 0x32, 0x75, 0x60, 0x0c, 0x2f, 0x09, 0xb8, 0x36, 0x77, 0x93, 0xa9,
                         0xac, 0xa3, 0xdb, 0x71, 0xcc, 0x30, 0xc5, 0x81, 0x79, 0xec, 0x3e, 0x87, 0xc1, 0x4c, 0x01,
                         0xd5, 0xc1, 0xf3, 0x43, 0x4f, 0x1d, 0x87])

        actualOutput = HKDF.createFor(3).deriveSecrets(ikm, info, 82, salt=salt)
        self.assertEqual(okm, actualOutput)

    def test_vectorV3_(self):
        ikm = bytearray([0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
                         0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b])

        salt = bytearray([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c])

        info = bytearray([0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9])

        okm = bytearray([0x6e, 0xc2, 0x55, 0x6d, 0x5d, 0x7b, 0x1d, 0x81, 0xde, 0xe4, 0x22, 0x2a, 0xd7, 0x48, 0x36,
                         0x95, 0xdd, 0xc9, 0x8f, 0x4f, 0x5f, 0xab, 0xc0, 0xe0, 0x20, 0x5d, 0xc2, 0xef, 0x87, 0x52,
                         0xd4, 0x1e, 0x04, 0xe2, 0xe2, 0x11, 0x01, 0xc6, 0x8f, 0xf0, 0x93, 0x94, 0xb8, 0xad, 0x0b,
                         0xdc, 0xb9, 0x60, 0x9c, 0xd4, 0xee, 0x82, 0xac, 0x13, 0x19, 0x9b, 0x4a, 0xa9, 0xfd, 0xa8,
                         0x99, 0xda, 0xeb, 0xec])


        actualOutput = HKDF.createFor(2).deriveSecrets(ikm, info, 64, salt=salt)