#-*- coding: utf-8 -*- ''' Copyright (c) 2016 NSR (National Security Research Institute) 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. ''' from .lsh_template import LSHTemplate ## 64비트 마스크 MASK_U64 = 0xffffffffffffffff ## LSH512 구현 클래스 class LSH512(LSHTemplate): _MASK = MASK_U64 _BLOCKSIZE = 256 _NUMSTEP = 28 _FORMAT_IN = ' 512: raise ValueError("outlenbits should be 0 ~ 512") self._outlenbits = outlenbits if self._outlenbits == 224: self._cv = self.__IV224[:] elif self._outlenbits == 256: self._cv = self.__IV256[:] elif self._outlenbits == 384: self._cv = self.__IV384[:] elif self._outlenbits == 512: self._cv = self.__IV512[:] else: generate_iv() ## 64비트 회전 연산 # @param [in] value 회전하고자 하는 값 # @param [in] rot 회전량 (비트) @staticmethod def __rol64(value, rot): return ((value << rot) | (value >> (64 - rot))) & MASK_U64 ## 스텝 함수 # @param [in] idx 스텝 인덱스 # @param [in] alpha 회전값 알파 # @param [in] beta 회전값 베타 def _step(self, idx, alpha, beta): vl = 0 vr = 0 for colidx in range(8): vl = (self._cv[colidx ] ^ self._msg[16 * idx + colidx ]) & MASK_U64 vr = (self._cv[colidx + 8] ^ self._msg[16 * idx + colidx + 8]) & MASK_U64 vl = LSH512.__rol64((vl + vr) & MASK_U64, alpha) ^ self._STEP[8 * idx + colidx] vr = LSH512.__rol64((vl + vr) & MASK_U64, beta) self._tcv[colidx ] = (vl + vr) & MASK_U64 self._tcv[colidx + 8] = LSH512.__rol64(vr, self._GAMMA[colidx]) self._word_permutation()