'asdf'
This commit is contained in:
parent
c084e33544
commit
43e37d484d
142
elas.py
142
elas.py
|
@ -1,6 +1,8 @@
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from random import randint
|
from random import randint
|
||||||
|
from random import randrange
|
||||||
|
import hashlib
|
||||||
|
import base64
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class PrimeGaloisField:
|
class PrimeGaloisField:
|
||||||
|
@ -118,7 +120,7 @@ class Point:
|
||||||
return self.__class__(
|
return self.__class__(
|
||||||
x=x3.value,
|
x=x3.value,
|
||||||
y=y3.value,
|
y=y3.value,
|
||||||
curve=secp256k1
|
curve=self.curve
|
||||||
)
|
)
|
||||||
|
|
||||||
if self == other and self.y == inf:
|
if self == other and self.y == inf:
|
||||||
|
@ -134,7 +136,7 @@ class Point:
|
||||||
return self.__class__(
|
return self.__class__(
|
||||||
x=x3.value,
|
x=x3.value,
|
||||||
y=y3.value,
|
y=y3.value,
|
||||||
curve=secp256k1
|
curve=self.curve
|
||||||
)
|
)
|
||||||
|
|
||||||
def __rmul__(self, scalar: int) -> "Point":
|
def __rmul__(self, scalar: int) -> "Point":
|
||||||
|
@ -158,10 +160,8 @@ class Signature:
|
||||||
s_inv = pow(self.s, -1, N)
|
s_inv = pow(self.s, -1, N)
|
||||||
u = (z * s_inv) % N
|
u = (z * s_inv) % N
|
||||||
v = (self.r * s_inv) % N
|
v = (self.r * s_inv) % N
|
||||||
|
|
||||||
return (u*G + v*pub_key).x.value == self.r
|
return (u*G + v*pub_key).x.value == self.r
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class PrivateKey:
|
class PrivateKey:
|
||||||
secret: int
|
secret: int
|
||||||
|
@ -176,34 +176,122 @@ class PrivateKey:
|
||||||
|
|
||||||
return Signature(r, s)
|
return Signature(r, s)
|
||||||
|
|
||||||
|
def sha256(msg: str):
|
||||||
|
hash = int('0x'+hashlib.sha256(msg.encode()).hexdigest(), 16)
|
||||||
|
return hash
|
||||||
|
|
||||||
|
def sha1(msg: str):
|
||||||
|
hash = int('0x'+hashlib.sha1(msg.encode()).hexdigest(), 16)
|
||||||
|
return hash
|
||||||
|
|
||||||
|
def lsh(msg: str):
|
||||||
|
from lsh import LSHDigest
|
||||||
|
lsh = LSHDigest.getInstance(256, 256)
|
||||||
|
lsh.update(msg.encode())
|
||||||
|
hash = lsh.final()
|
||||||
|
return hex(int.from_bytes(hash,'big'))
|
||||||
|
|
||||||
|
def largePrime(bit):
|
||||||
|
def rand(n):
|
||||||
|
return randrange(2**(n-1)+1, 2**n-1)
|
||||||
|
|
||||||
|
def gLLP(n):
|
||||||
|
while True:
|
||||||
|
|
||||||
|
# Obtain a random number
|
||||||
|
prime_candidate = rand(n)
|
||||||
|
|
||||||
|
for divisor in first_primes_list:
|
||||||
|
if prime_candidate % divisor == 0 and divisor**2 <= prime_candidate:
|
||||||
|
break
|
||||||
|
# If no divisor found, return value
|
||||||
|
else: return prime_candidate
|
||||||
|
|
||||||
|
def iMRP(miller_rabin_candidate):
|
||||||
|
maxDivisionsByTwo = 0
|
||||||
|
evenComponent = miller_rabin_candidate-1
|
||||||
|
|
||||||
|
while evenComponent % 2 == 0:
|
||||||
|
evenComponent >>= 1
|
||||||
|
maxDivisionsByTwo += 1
|
||||||
|
assert(2**maxDivisionsByTwo * evenComponent == miller_rabin_candidate-1)
|
||||||
|
|
||||||
|
def trialComposite(round_tester):
|
||||||
|
if pow(round_tester, evenComponent, miller_rabin_candidate) == 1:
|
||||||
|
return False
|
||||||
|
for i in range(maxDivisionsByTwo):
|
||||||
|
if pow(round_tester, 2**i * evenComponent, miller_rabin_candidate) == miller_rabin_candidate-1:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
# Set number of trials here
|
||||||
|
numberOfRabinTrials = 20
|
||||||
|
for i in range(numberOfRabinTrials):
|
||||||
|
round_tester = randrange(2, miller_rabin_candidate)
|
||||||
|
if trialComposite(round_tester):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
first_primes_list = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
|
||||||
|
31, 37, 41, 43, 47, 53, 59, 61, 67,
|
||||||
|
71, 73, 79, 83, 89, 97, 101, 103,
|
||||||
|
107, 109, 113, 127, 131, 137, 139,
|
||||||
|
149, 151, 157, 163, 167, 173, 179,
|
||||||
|
181, 191, 193, 197, 199, 211, 223,
|
||||||
|
227, 229, 233, 239, 241, 251, 257,
|
||||||
|
263, 269, 271, 277, 281, 283, 293,
|
||||||
|
307, 311, 313, 317, 331, 337, 347, 349]
|
||||||
|
|
||||||
|
while True:
|
||||||
|
n = bit
|
||||||
|
prime_candidate = gLLP(n)
|
||||||
|
if not iMRP(prime_candidate):
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
return prime_candidate
|
||||||
|
break
|
||||||
|
|
||||||
|
def b64e(data: int):
|
||||||
|
base64_bytes = base64.b64encode(bytes.fromhex(str(data).replace('0x','')))
|
||||||
|
base64_message = base64_bytes.decode('ascii')
|
||||||
|
return base64_message
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
P = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
|
######[ SEC-P256-r1 ]#####################################################
|
||||||
A = 0
|
|
||||||
B = 7
|
|
||||||
|
|
||||||
field = PrimeGaloisField(prime=P)
|
|
||||||
secp256k1 = EllipticCurve(a=A, b=B, field=field)
|
|
||||||
|
|
||||||
G = Point(
|
|
||||||
x=0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798,
|
|
||||||
y=0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8,
|
|
||||||
curve=secp256k1)
|
|
||||||
|
|
||||||
N = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
|
|
||||||
|
|
||||||
I = Point(x=None, y=None, curve=secp256k1)
|
|
||||||
|
|
||||||
|
P = 0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF
|
||||||
|
A = 0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC
|
||||||
|
B = 0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B
|
||||||
|
N = 0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551
|
||||||
inf = float("inf")
|
inf = float("inf")
|
||||||
|
|
||||||
priv: int = 0xea11d6ada978a0b491aa5cbbe4df17a65c2fecc24448e95d1ccd854b43991bec
|
field = PrimeGaloisField(prime=P)
|
||||||
|
secp256r1 = EllipticCurve(a=A, b=B, field=field)
|
||||||
|
|
||||||
|
G = Point(
|
||||||
|
x=0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296,
|
||||||
|
y=0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5,
|
||||||
|
curve=secp256r1)
|
||||||
|
I = Point(x=None, y=None, curve=secp256r1)
|
||||||
|
|
||||||
|
#####################################################################
|
||||||
|
|
||||||
|
priv = 0x9f07090a27c7f3eaf51980059cae33420865890c72d51a8d3a20fee02c82afc63ab79c604ec6b691b94bc288b910327cd38cce7f11b61ab330b9b506c149722f
|
||||||
|
#largePrime(512)
|
||||||
|
msg = ''
|
||||||
|
|
||||||
|
z = sha256(msg)
|
||||||
|
|
||||||
e = PrivateKey(priv)
|
e = PrivateKey(priv)
|
||||||
|
|
||||||
pub = e.secret * G
|
pub = e.secret * G
|
||||||
print(pub)
|
|
||||||
z = 0x7e240de74fb1ed08fa08d38063f6a6a91462a815
|
|
||||||
|
|
||||||
signature: Signature = e.sign(z)
|
signature = e.sign(z)
|
||||||
print(e.sign(z))
|
# print(signature.verify(z, pub))
|
||||||
assert signature.verify(z, pub)
|
|
||||||
|
f_pubKey = hex(int(f'0x40{str(pub.x).replace("0x","")}{str(pub.y).replace("0x","")}',16))
|
||||||
|
f_privKey = hex(priv)
|
||||||
|
f_msg = hex(z)
|
||||||
|
f_Sign = hex(int(f'0x30450220{str(hex(e.sign(z).r)).replace("0x","")}022100{str(hex(e.sign(z).s)).replace("0x","")}',16))
|
||||||
|
|
||||||
|
print(b64e(f_pubKey))
|
|
@ -0,0 +1,30 @@
|
||||||
|
#-*- 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.
|
||||||
|
'''
|
||||||
|
|
||||||
|
__all__ = ['lsh_digest', 'lsh256', 'lsh512', 'hmac_lsh']
|
||||||
|
|
||||||
|
from .lsh_digest import LSHDigest
|
||||||
|
from .lsh256 import LSH256
|
||||||
|
from .lsh512 import LSH512
|
||||||
|
from .hmac_lsh import HmacLSH
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,115 @@
|
||||||
|
#-*- 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_digest import LSHDigest
|
||||||
|
|
||||||
|
## HMAC 구현 클래스
|
||||||
|
class HmacLSH:
|
||||||
|
|
||||||
|
__IPAD = 0x36
|
||||||
|
__OPAD = 0x5c
|
||||||
|
|
||||||
|
#__md = None
|
||||||
|
__blocksize = 0
|
||||||
|
__i_key_pad = None
|
||||||
|
__o_key_pad = None
|
||||||
|
__outlenbits = 0
|
||||||
|
|
||||||
|
|
||||||
|
## 생성자
|
||||||
|
# @param [in] self 객체 포인터
|
||||||
|
# @param [in] wordlenbits 워드 길이 256, 512 중 하나여야 함
|
||||||
|
# @param [in] outlenbits 출력 해시 길이 1 ~ wordlenbits 사이의 값이어야 함
|
||||||
|
def __init__(self, wordlenbits, outlenbits = 0):
|
||||||
|
self.__outlenbits = outlenbits
|
||||||
|
if outlenbits > 0:
|
||||||
|
self.__md = LSHDigest.getInstance(wordlenbits, outlenbits)
|
||||||
|
else:
|
||||||
|
self.__md = LSHDigest.getInstance(wordlenbits)
|
||||||
|
self.__blocksize = self.__md.get_blocksize()
|
||||||
|
|
||||||
|
## HMAC 계산을 위한 초기화
|
||||||
|
# @param [in] self 객체 포인터
|
||||||
|
# @param [in] key 키
|
||||||
|
def init(self, key):
|
||||||
|
|
||||||
|
if key is None:
|
||||||
|
key = bytearray([0] * self._blocksize)
|
||||||
|
|
||||||
|
if len(key) > self.__blocksize:
|
||||||
|
self.__md.reset()
|
||||||
|
key = self.__md.final(key)
|
||||||
|
|
||||||
|
self.__i_key_pad = [HmacLSH.__IPAD] * self.__blocksize
|
||||||
|
self.__o_key_pad = [HmacLSH.__OPAD] * self.__blocksize
|
||||||
|
|
||||||
|
for idx in range(len(key)):
|
||||||
|
self.__i_key_pad[idx] ^= key[idx]
|
||||||
|
self.__o_key_pad[idx] ^= key[idx]
|
||||||
|
|
||||||
|
self.reset()
|
||||||
|
|
||||||
|
|
||||||
|
## 새로운 HMAC을 계산할 수 있도록 객체를 초기화한다
|
||||||
|
# @param [in] self 객체 포인터
|
||||||
|
def reset(self):
|
||||||
|
self.__md.reset()
|
||||||
|
self.__md.update(self.__i_key_pad)
|
||||||
|
|
||||||
|
|
||||||
|
## HMAC을 계산할 메시지를 추가한다.
|
||||||
|
# @param [in] self 객체 포인터
|
||||||
|
# @param [in] msg 입력 메시지
|
||||||
|
def update(self, msg):
|
||||||
|
if msg is None:
|
||||||
|
return
|
||||||
|
self.__md.update(msg)
|
||||||
|
|
||||||
|
|
||||||
|
## HMAC을 계산하고 결과를 리턴한다.
|
||||||
|
# @param [in] self 객체 포인터
|
||||||
|
# @return 계산된 HMAC 값
|
||||||
|
def final(self):
|
||||||
|
result = self.__md.final()
|
||||||
|
self.__md.update(self.__o_key_pad)
|
||||||
|
self.__md.update(result)
|
||||||
|
result = self.__md.final()
|
||||||
|
self.reset()
|
||||||
|
return result
|
||||||
|
|
||||||
|
## digest 함수 - 최종 해쉬값을 계산하여 리턴한다.
|
||||||
|
# @param [in] wordlenbits 워드 길이 256, 512 중 하나여야 함
|
||||||
|
# @param [in] outlenbits 출력 해시 길이 1 ~ wordlenbits 사이의 값이어야 함
|
||||||
|
# @param [in] key HMAC key
|
||||||
|
# @param [in] data 입력 데이터
|
||||||
|
# @param [in] offset 데이터 시작 오프셋 (바이트)
|
||||||
|
# @param [in] length 데이터 길이 (비트)
|
||||||
|
# @return 계산된 HMAC값
|
||||||
|
@staticmethod
|
||||||
|
def digest(wordlenbits, outlenbits = None, key = None, data = None, offset = 0, length = -1):
|
||||||
|
hmac = HmacLSH(wordlenbits, outlenbits)
|
||||||
|
hmac.init(key)
|
||||||
|
hmac.update(data, offset, length)
|
||||||
|
return hmac.final()
|
||||||
|
|
|
@ -0,0 +1,139 @@
|
||||||
|
#-*- 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
|
||||||
|
|
||||||
|
MASK_U32 = 0xffffffff
|
||||||
|
|
||||||
|
## LSH256 구현 클래스
|
||||||
|
class LSH256(LSHTemplate):
|
||||||
|
|
||||||
|
_MASK = MASK_U32
|
||||||
|
_BLOCKSIZE = 128
|
||||||
|
_NUMSTEP = 26
|
||||||
|
_FORMAT_IN = '<LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL'
|
||||||
|
_FORMAT_OUT = '<LLLLLLLL'
|
||||||
|
|
||||||
|
## 사전 계산된 224비트 출력용 IV
|
||||||
|
__IV224 = [
|
||||||
|
0x068608D3, 0x62D8F7A7, 0xD76652AB, 0x4C600A43, 0xBDC40AA8, 0x1ECA0B68, 0xDA1A89BE, 0x3147D354,
|
||||||
|
0x707EB4F9, 0xF65B3862, 0x6B0B2ABE, 0x56B8EC0A, 0xCF237286, 0xEE0D1727, 0x33636595, 0x8BB8D05F,
|
||||||
|
]
|
||||||
|
|
||||||
|
## 사전 계산된 256비트 출력용 IV
|
||||||
|
__IV256 = [
|
||||||
|
0x46a10f1f, 0xfddce486, 0xb41443a8, 0x198e6b9d, 0x3304388d, 0xb0f5a3c7, 0xb36061c4, 0x7adbd553,
|
||||||
|
0x105d5378, 0x2f74de54, 0x5c2f2d95, 0xf2553fbe, 0x8051357a, 0x138668c8, 0x47aa4484, 0xe01afb41
|
||||||
|
]
|
||||||
|
|
||||||
|
## STEP 상수
|
||||||
|
_STEP = [
|
||||||
|
0x917caf90, 0x6c1b10a2, 0x6f352943, 0xcf778243, 0x2ceb7472, 0x29e96ff2, 0x8a9ba428, 0x2eeb2642,
|
||||||
|
0x0e2c4021, 0x872bb30e, 0xa45e6cb2, 0x46f9c612, 0x185fe69e, 0x1359621b, 0x263fccb2, 0x1a116870,
|
||||||
|
0x3a6c612f, 0xb2dec195, 0x02cb1f56, 0x40bfd858, 0x784684b6, 0x6cbb7d2e, 0x660c7ed8, 0x2b79d88a,
|
||||||
|
0xa6cd9069, 0x91a05747, 0xcdea7558, 0x00983098, 0xbecb3b2e, 0x2838ab9a, 0x728b573e, 0xa55262b5,
|
||||||
|
0x745dfa0f, 0x31f79ed8, 0xb85fce25, 0x98c8c898, 0x8a0669ec, 0x60e445c2, 0xfde295b0, 0xf7b5185a,
|
||||||
|
0xd2580983, 0x29967709, 0x182df3dd, 0x61916130, 0x90705676, 0x452a0822, 0xe07846ad, 0xaccd7351,
|
||||||
|
0x2a618d55, 0xc00d8032, 0x4621d0f5, 0xf2f29191, 0x00c6cd06, 0x6f322a67, 0x58bef48d, 0x7a40c4fd,
|
||||||
|
0x8beee27f, 0xcd8db2f2, 0x67f2c63b, 0xe5842383, 0xc793d306, 0xa15c91d6, 0x17b381e5, 0xbb05c277,
|
||||||
|
0x7ad1620a, 0x5b40a5bf, 0x5ab901a2, 0x69a7a768, 0x5b66d9cd, 0xfdee6877, 0xcb3566fc, 0xc0c83a32,
|
||||||
|
0x4c336c84, 0x9be6651a, 0x13baa3fc, 0x114f0fd1, 0xc240a728, 0xec56e074, 0x009c63c7, 0x89026cf2,
|
||||||
|
0x7f9ff0d0, 0x824b7fb5, 0xce5ea00f, 0x605ee0e2, 0x02e7cfea, 0x43375560, 0x9d002ac7, 0x8b6f5f7b,
|
||||||
|
0x1f90c14f, 0xcdcb3537, 0x2cfeafdd, 0xbf3fc342, 0xeab7b9ec, 0x7a8cb5a3, 0x9d2af264, 0xfacedb06,
|
||||||
|
0xb052106e, 0x99006d04, 0x2bae8d09, 0xff030601, 0xa271a6d6, 0x0742591d, 0xc81d5701, 0xc9a9e200,
|
||||||
|
0x02627f1e, 0x996d719d, 0xda3b9634, 0x02090800, 0x14187d78, 0x499b7624, 0xe57458c9, 0x738be2c9,
|
||||||
|
0x64e19d20, 0x06df0f36, 0x15d1cb0e, 0x0b110802, 0x2c95f58c, 0xe5119a6d, 0x59cd22ae, 0xff6eac3c,
|
||||||
|
0x467ebd84, 0xe5ee453c, 0xe79cd923, 0x1c190a0d, 0xc28b81b8, 0xf6ac0852, 0x26efd107, 0x6e1ae93b,
|
||||||
|
0xc53c41ca, 0xd4338221, 0x8475fd0a, 0x35231729, 0x4e0d3a7a, 0xa2b45b48, 0x16c0d82d, 0x890424a9,
|
||||||
|
0x017e0c8f, 0x07b5a3f5, 0xfa73078e, 0x583a405e, 0x5b47b4c8, 0x570fa3ea, 0xd7990543, 0x8d28ce32,
|
||||||
|
0x7f8a9b90, 0xbd5998fc, 0x6d7a9688, 0x927a9eb6, 0xa2fc7d23, 0x66b38e41, 0x709e491a, 0xb5f700bf,
|
||||||
|
0x0a262c0f, 0x16f295b9, 0xe8111ef5, 0x0d195548, 0x9f79a0c5, 0x1a41cfa7, 0x0ee7638a, 0xacf7c074,
|
||||||
|
0x30523b19, 0x09884ecf, 0xf93014dd, 0x266e9d55, 0x191a6664, 0x5c1176c1, 0xf64aed98, 0xa4b83520,
|
||||||
|
0x828d5449, 0x91d71dd8, 0x2944f2d6, 0x950bf27b, 0x3380ca7d, 0x6d88381d, 0x4138868e, 0x5ced55c4,
|
||||||
|
0x0fe19dcb, 0x68f4f669, 0x6e37c8ff, 0xa0fe6e10, 0xb44b47b0, 0xf5c0558a, 0x79bf14cf, 0x4a431a20,
|
||||||
|
0xf17f68da, 0x5deb5fd1, 0xa600c86d, 0x9f6c7eb0, 0xff92f864, 0xb615e07f, 0x38d3e448, 0x8d5d3a6a,
|
||||||
|
0x70e843cb, 0x494b312e, 0xa6c93613, 0x0beb2f4f, 0x928b5d63, 0xcbf66035, 0x0cb82c80, 0xea97a4f7,
|
||||||
|
0x592c0f3b, 0x947c5f77, 0x6fff49b9, 0xf71a7e5a, 0x1de8c0f5, 0xc2569600, 0xc4e4ac8c, 0x823c9ce1,
|
||||||
|
]
|
||||||
|
|
||||||
|
_ALPHA_EVEN = 29
|
||||||
|
_ALPHA_ODD = 5
|
||||||
|
|
||||||
|
_BETA_EVEN = 1
|
||||||
|
_BETA_ODD = 17
|
||||||
|
|
||||||
|
_GAMMA = [0, 8, 16, 24, 24, 16, 8, 0]
|
||||||
|
|
||||||
|
## 생성자
|
||||||
|
# @param [in] self 객체 포인터
|
||||||
|
# @param [in] outlenbits 출력 길이 (비트)
|
||||||
|
def __init__(self, outlenbits = 256):
|
||||||
|
self._init(outlenbits)
|
||||||
|
|
||||||
|
|
||||||
|
## IV 생성 함수 - 224, 256비트의 출력을 위해서는 사전 계산된 값을 사용하고, 그 외의 출력에 대해서는 IV 생성
|
||||||
|
# @param [in] self 객체 포인터
|
||||||
|
# @param [in] outlenbits 출력 길이 (비트)
|
||||||
|
def _init_iv(self, outlenbits):
|
||||||
|
def generate_iv():
|
||||||
|
self._cv = [32, self._outlenbits] + [0] * 14
|
||||||
|
self._compress(self._buf)
|
||||||
|
|
||||||
|
if outlenbits <= 0 or outlenbits > 256:
|
||||||
|
raise ValueError("outlenbits should be 0 ~ 256")
|
||||||
|
|
||||||
|
self._outlenbits = outlenbits
|
||||||
|
if self._outlenbits == 224:
|
||||||
|
self._cv = self.__IV224[:]
|
||||||
|
elif self._outlenbits == 256:
|
||||||
|
self._cv = self.__IV256[:]
|
||||||
|
else:
|
||||||
|
generate_iv()
|
||||||
|
|
||||||
|
|
||||||
|
## 32비트 회전 연산
|
||||||
|
# @param [in] value 회전하고자 하는 값
|
||||||
|
# @param [in] rot 회전량 (비트)
|
||||||
|
@staticmethod
|
||||||
|
def __rol32(value, rot):
|
||||||
|
return ((value << rot) | (value >> (32 - rot))) & MASK_U32
|
||||||
|
|
||||||
|
|
||||||
|
## 스텝 함수
|
||||||
|
# @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_U32
|
||||||
|
vr = (self._cv[colidx + 8] ^ self._msg[16 * idx + colidx + 8]) & MASK_U32
|
||||||
|
vl = LSH256.__rol32((vl + vr) & MASK_U32, alpha) ^ self._STEP[8 * idx + colidx]
|
||||||
|
vr = LSH256.__rol32((vl + vr) & MASK_U32, beta)
|
||||||
|
self._tcv[colidx ] = (vl + vr) & MASK_U32
|
||||||
|
self._tcv[colidx + 8] = LSH256.__rol32(vr, self._GAMMA[colidx])
|
||||||
|
|
||||||
|
self._word_permutation()
|
||||||
|
|
|
@ -0,0 +1,194 @@
|
||||||
|
#-*- 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 = '<QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ'
|
||||||
|
_FORMAT_OUT = '<QQQQQQQQ'
|
||||||
|
|
||||||
|
## 사전 계산된 224비트 출력용 IV
|
||||||
|
__IV224 = [
|
||||||
|
0x0c401e9fe8813a55, 0x4a5f446268fd3d35, 0xff13e452334f612a, 0xf8227661037e354a,
|
||||||
|
0xa5f223723c9ca29d, 0x95d965a11aed3979, 0x01e23835b9ab02cc, 0x52d49cbad5b30616,
|
||||||
|
0x9e5c2027773f4ed3, 0x66a5c8801925b701, 0x22bbc85b4c6779d9, 0xc13171a42c559c23,
|
||||||
|
0x31e2b67d25be3813, 0xd522c4deed8e4d83, 0xa79f5509b43fbafe, 0xe00d2cd88b4b6c6a,
|
||||||
|
]
|
||||||
|
|
||||||
|
## 사전 계산된 256비트 출력용 IV
|
||||||
|
__IV256 = [
|
||||||
|
0x6dc57c33df989423, 0xd8ea7f6e8342c199, 0x76df8356f8603ac4, 0x40f1b44de838223a,
|
||||||
|
0x39ffe7cfc31484cd, 0x39c4326cc5281548, 0x8a2ff85a346045d8, 0xff202aa46dbdd61e,
|
||||||
|
0xcf785b3cd5fcdb8b, 0x1f0323b64a8150bf, 0xff75d972f29ea355, 0x2e567f30bf1ca9e1,
|
||||||
|
0xb596875bf8ff6dba, 0xfcca39b089ef4615, 0xecff4017d020b4b6, 0x7e77384c772ed802,
|
||||||
|
]
|
||||||
|
|
||||||
|
## 사전 계산된 384비트 출력용 IV
|
||||||
|
__IV384 = [
|
||||||
|
0x53156a66292808f6, 0xb2c4f362b204c2bc, 0xb84b7213bfa05c4e, 0x976ceb7c1b299f73,
|
||||||
|
0xdf0cc63c0570ae97, 0xda4441baa486ce3f, 0x6559f5d9b5f2acc2, 0x22dacf19b4b52a16,
|
||||||
|
0xbbcdacefde80953a, 0xc9891a2879725b3e, 0x7c9fe6330237e440, 0xa30ba550553f7431,
|
||||||
|
0xbb08043fb34e3e30, 0xa0dec48d54618ead, 0x150317267464bc57, 0x32d1501fde63dc93
|
||||||
|
]
|
||||||
|
|
||||||
|
## 사전 계산된 512비트 출력용 IV
|
||||||
|
__IV512 = [
|
||||||
|
0xadd50f3c7f07094e, 0xe3f3cee8f9418a4f, 0xb527ecde5b3d0ae9, 0x2ef6dec68076f501,
|
||||||
|
0x8cb994cae5aca216, 0xfbb9eae4bba48cc7, 0x650a526174725fea, 0x1f9a61a73f8d8085,
|
||||||
|
0xb6607378173b539b, 0x1bc99853b0c0b9ed, 0xdf727fc19b182d47, 0xdbef360cf893a457,
|
||||||
|
0x4981f5e570147e80, 0xd00c4490ca7d3e30, 0x5d73940c0e4ae1ec, 0x894085e2edb2d819
|
||||||
|
]
|
||||||
|
|
||||||
|
## STEP 상수
|
||||||
|
_STEP = [
|
||||||
|
0x97884283c938982a, 0xba1fca93533e2355, 0xc519a2e87aeb1c03, 0x9a0fc95462af17b1,
|
||||||
|
0xfc3dda8ab019a82b, 0x02825d079a895407, 0x79f2d0a7ee06a6f7, 0xd76d15eed9fdf5fe,
|
||||||
|
0x1fcac64d01d0c2c1, 0xd9ea5de69161790f, 0xdebc8b6366071fc8, 0xa9d91db711c6c94b,
|
||||||
|
0x3a18653ac9c1d427, 0x84df64a223dd5b09, 0x6cc37895f4ad9e70, 0x448304c8d7f3f4d5,
|
||||||
|
0xea91134ed29383e0, 0xc4484477f2da88e8, 0x9b47eec96d26e8a6, 0x82f6d4c8d89014f4,
|
||||||
|
0x527da0048b95fb61, 0x644406c60138648d, 0x303c0e8aa24c0edc, 0xc787cda0cbe8ca19,
|
||||||
|
0x7ba46221661764ca, 0x0c8cbc6acd6371ac, 0xe336b836940f8f41, 0x79cb9da168a50976,
|
||||||
|
0xd01da49021915cb3, 0xa84accc7399cf1f1, 0x6c4a992cee5aeb0c, 0x4f556e6cb4b2e3e0,
|
||||||
|
0x200683877d7c2f45, 0x9949273830d51db8, 0x19eeeecaa39ed124, 0x45693f0a0dae7fef,
|
||||||
|
0xedc234b1b2ee1083, 0xf3179400d68ee399, 0xb6e3c61b4945f778, 0xa4c3db216796c42f,
|
||||||
|
0x268a0b04f9ab7465, 0xe2705f6905f2d651, 0x08ddb96e426ff53d, 0xaea84917bc2e6f34,
|
||||||
|
0xaff6e664a0fe9470, 0x0aab94d765727d8c, 0x9aa9e1648f3d702e, 0x689efc88fe5af3d3,
|
||||||
|
0xb0950ffea51fd98b, 0x52cfc86ef8c92833, 0xe69727b0b2653245, 0x56f160d3ea9da3e2,
|
||||||
|
0xa6dd4b059f93051f, 0xb6406c3cd7f00996, 0x448b45f3ccad9ec8, 0x079b8587594ec73b,
|
||||||
|
0x45a50ea3c4f9653b, 0x22983767c1f15b85, 0x7dbed8631797782b, 0x485234be88418638,
|
||||||
|
0x842850a5329824c5, 0xf6aca914c7f9a04c, 0xcfd139c07a4c670c, 0xa3210ce0a8160242,
|
||||||
|
0xeab3b268be5ea080, 0xbacf9f29b34ce0a7, 0x3c973b7aaf0fa3a8, 0x9a86f346c9c7be80,
|
||||||
|
0xac78f5d7cabcea49, 0xa355bddcc199ed42, 0xa10afa3ac6b373db, 0xc42ded88be1844e5,
|
||||||
|
0x9e661b271cff216a, 0x8a6ec8dd002d8861, 0xd3d2b629beb34be4, 0x217a3a1091863f1a,
|
||||||
|
0x256ecda287a733f5, 0xf9139a9e5b872fe5, 0xac0535017a274f7c, 0xf21b7646d65d2aa9,
|
||||||
|
0x048142441c208c08, 0xf937a5dd2db5e9eb, 0xa688dfe871ff30b7, 0x9bb44aa217c5593b,
|
||||||
|
0x943c702a2edb291a, 0x0cae38f9e2b715de, 0xb13a367ba176cc28, 0x0d91bd1d3387d49b,
|
||||||
|
0x85c386603cac940c, 0x30dd830ae39fd5e4, 0x2f68c85a712fe85d, 0x4ffeecb9dd1e94d6,
|
||||||
|
0xd0ac9a590a0443ae, 0xbae732dc99ccf3ea, 0xeb70b21d1842f4d9, 0x9f4eda50bb5c6fa8,
|
||||||
|
0x4949e69ce940a091, 0x0e608dee8375ba14, 0x983122cba118458c, 0x4eeba696fbb36b25,
|
||||||
|
0x7d46f3630e47f27e, 0xa21a0f7666c0dea4, 0x5c22cf355b37cec4, 0xee292b0c17cc1847,
|
||||||
|
0x9330838629e131da, 0x6eee7c71f92fce22, 0xc953ee6cb95dd224, 0x3a923d92af1e9073,
|
||||||
|
0xc43a5671563a70fb, 0xbc2985dd279f8346, 0x7ef2049093069320, 0x17543723e3e46035,
|
||||||
|
0xc3b409b00b130c6d, 0x5d6aee6b28fdf090, 0x1d425b26172ff6ed, 0xcccfd041cdaf03ad,
|
||||||
|
0xfe90c7c790ab6cbf, 0xe5af6304c722ca02, 0x70f695239999b39e, 0x6b8b5b07c844954c,
|
||||||
|
0x77bdb9bb1e1f7a30, 0xc859599426ee80ed, 0x5f9d813d4726e40a, 0x9ca0120f7cb2b179,
|
||||||
|
0x8f588f583c182cbd, 0x951267cbe9eccce7, 0x678bb8bd334d520e, 0xf6e662d00cd9e1b7,
|
||||||
|
0x357774d93d99aaa7, 0x21b2edbb156f6eb5, 0xfd1ebe846e0aee69, 0x3cb2218c2f642b15,
|
||||||
|
0xe7e7e7945444ea4c, 0xa77a33b5d6b9b47c, 0xf34475f0809f6075, 0xdd4932dce6bb99ad,
|
||||||
|
0xacec4e16d74451dc, 0xd4a0a8d084de23d6, 0x1bdd42f278f95866, 0xeed3adbb938f4051,
|
||||||
|
0xcfcf7be8992f3733, 0x21ade98c906e3123, 0x37ba66711fffd668, 0x267c0fc3a255478a,
|
||||||
|
0x993a64ee1b962e88, 0x754979556301faaa, 0xf920356b7251be81, 0xc281694f22cf923f,
|
||||||
|
0x9f4b6481c8666b02, 0xcf97761cfe9f5444, 0xf220d7911fd63e9f, 0xa28bd365f79cd1b0,
|
||||||
|
0xd39f5309b1c4b721, 0xbec2ceb864fca51f, 0x1955a0ddc410407a, 0x43eab871f261d201,
|
||||||
|
0xeaafe64a2ed16da1, 0x670d931b9df39913, 0x12f868b0f614de91, 0x2e5f395d946e8252,
|
||||||
|
0x72f25cbb767bd8f4, 0x8191871d61a1c4dd, 0x6ef67ea1d450ba93, 0x2ea32a645433d344,
|
||||||
|
0x9a963079003f0f8b, 0x74a0aeb9918cac7a, 0x0b6119a70af36fa3, 0x8d9896f202f0d480,
|
||||||
|
0x654f1831f254cd66, 0x1318a47f0366a25e, 0x65752076250b4e01, 0xd1cd8eb888071772,
|
||||||
|
0x30c6a9793f4e9b25, 0x154f684b1e3926ee, 0x6c7ac0b1fe6312ae, 0x262f88f4f3c5550d,
|
||||||
|
0xb4674a24472233cb, 0x2bbd23826a090071, 0xda95969b30594f66, 0x9f5c47408f1e8a43,
|
||||||
|
0xf77022b88de9c055, 0x64b7b36957601503, 0xe73b72b06175c11a, 0x55b87de8b91a6233,
|
||||||
|
0x1bb16e6b6955ff7f, 0xe8e0a5ec7309719c, 0x702c31cb89a8b640, 0xfba387cfada8cde2,
|
||||||
|
0x6792db4677aa164c, 0x1c6b1cc0b7751867, 0x22ae2311d736dc01, 0x0e3666a1d37c9588,
|
||||||
|
0xcd1fd9d4bf557e9a, 0xc986925f7c7b0e84, 0x9c5dfd55325ef6b0, 0x9f2b577d5676b0dd,
|
||||||
|
0xfa6e21be21c062b3, 0x8787dd782c8d7f83, 0xd0d134e90e12dd23, 0x449d087550121d96,
|
||||||
|
0xecf9ae9414d41967, 0x5018f1dbf789934d, 0xfa5b52879155a74c, 0xca82d4d3cd278e7c,
|
||||||
|
0x688fdfdfe22316ad, 0x0f6555a4ba0d030a, 0xa2061df720f000f3, 0xe1a57dc5622fb3da,
|
||||||
|
0xe6a842a8e8ed8153, 0x690acdd3811ce09d, 0x55adda18e6fcf446, 0x4d57a8a0f4b60b46,
|
||||||
|
0xf86fbfc20539c415, 0x74bafa5ec7100d19, 0xa824151810f0f495, 0x8723432791e38ebb,
|
||||||
|
0x8eeaeb91d66ed539, 0x73d8a1549dfd7e06, 0x0387f2ffe3f13a9b, 0xa5004995aac15193,
|
||||||
|
0x682f81c73efdda0d, 0x2fb55925d71d268d, 0xcc392d2901e58a3d, 0xaa666ab975724a42
|
||||||
|
]
|
||||||
|
|
||||||
|
_ALPHA_EVEN = 23
|
||||||
|
_ALPHA_ODD = 7
|
||||||
|
|
||||||
|
_BETA_EVEN = 59
|
||||||
|
_BETA_ODD = 3
|
||||||
|
|
||||||
|
_GAMMA = [0, 16, 32, 48, 8, 24, 40, 56]
|
||||||
|
|
||||||
|
## 생성자
|
||||||
|
# @param [in] self 객체 포인터
|
||||||
|
# @param [in] outlenbits 출력 길이 (비트)
|
||||||
|
def __init__(self, outlenbits = 512):
|
||||||
|
self._init(outlenbits)
|
||||||
|
|
||||||
|
|
||||||
|
## IV 생성 함수 - 224, 256, 384, 512 비트의 출력을 위해서는 사전 계산된 값을 사용하고, 그 외의 출력에 대해서는 IV 생성
|
||||||
|
# @param [in] self 객체 포인터
|
||||||
|
# @param [in] outlenbits 출력 길이 (비트)
|
||||||
|
def _init_iv(self, outlenbits):
|
||||||
|
def generate_iv():
|
||||||
|
self._cv = [64, self._outlenbits] + [0] * 14
|
||||||
|
self._compress(self._buf)
|
||||||
|
|
||||||
|
if outlenbits <= 0 or outlenbits > 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()
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
#-*- 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 .lsh256 import LSH256
|
||||||
|
from .lsh512 import LSH512
|
||||||
|
|
||||||
|
## 해쉬 함수 wrapper 클래스
|
||||||
|
class LSHDigest:
|
||||||
|
|
||||||
|
## 파라미터에 맞는 LSH 알고리즘 객체 생성
|
||||||
|
# @param [in] wordlenbits 워드 길이 (비트) 256, 512만 가능함
|
||||||
|
# @param [in] outlenbits 출력 길이 (비트) 1 ~ 256 (LSH-256) 혹은 1 ~ 512 (LSH-512) 가 가능함
|
||||||
|
# @return LSH 객체
|
||||||
|
@staticmethod
|
||||||
|
def getInstance(wordlenbits, outlenbits = None):
|
||||||
|
if outlenbits is None:
|
||||||
|
outlenbits = wordlenbits
|
||||||
|
|
||||||
|
if wordlenbits == 256:
|
||||||
|
return LSH256(outlenbits)
|
||||||
|
|
||||||
|
elif wordlenbits == 512:
|
||||||
|
return LSH512(outlenbits)
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise ValueError("Unsupported algorithm parameter");
|
||||||
|
|
||||||
|
|
||||||
|
## digest 함수 - 최종 해쉬값을 계산하여 리턴한다.
|
||||||
|
# @param [in] wordlenbits 워드 길이 256, 512 중 하나여야 함
|
||||||
|
# @param [in] outlenbits 출력 해시 길이 1 ~ wordlenbits 사이의 값이어야 함
|
||||||
|
# @param [in] data 입력 데이터
|
||||||
|
# @param [in] offset 데이터 시작 오프셋 (바이트)
|
||||||
|
# @param [in] length 데이터 길이 (비트)
|
||||||
|
# @return 계산된 해쉬값
|
||||||
|
@staticmethod
|
||||||
|
def digest(wordlenbits, outlenbits = None, data = None, offset = 0, length = -1):
|
||||||
|
if outlenbits is None:
|
||||||
|
outlenbits = wordlenbits
|
||||||
|
|
||||||
|
lsh = LSHDigest.getInstance(wordlenbits, outlenbits)
|
||||||
|
return lsh.final(data, offset, length)
|
|
@ -0,0 +1,240 @@
|
||||||
|
#-*- 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.
|
||||||
|
'''
|
||||||
|
|
||||||
|
import struct
|
||||||
|
|
||||||
|
## LSH 추상 클래스
|
||||||
|
class LSHTemplate:
|
||||||
|
|
||||||
|
_MASK = None
|
||||||
|
_WORDBITLEN = None
|
||||||
|
_NUMSTEP = None
|
||||||
|
_BLOCKSIZE = 0
|
||||||
|
_FORMAT_IN = None
|
||||||
|
_FORMAT_OUT = None
|
||||||
|
|
||||||
|
_outlenbits = 0
|
||||||
|
_boff = None
|
||||||
|
_cv = None
|
||||||
|
_tcv = None
|
||||||
|
_msg = None
|
||||||
|
_buf = None
|
||||||
|
|
||||||
|
_STEP = None
|
||||||
|
_ALPHA_EVEN = None
|
||||||
|
_ALPHA_ODD = None
|
||||||
|
_BETA_EVEN = None
|
||||||
|
_BETA_ODD = None
|
||||||
|
_GAMMA = None
|
||||||
|
|
||||||
|
## 생성자
|
||||||
|
# @param [in] self 객체 포인터
|
||||||
|
# @param [in] outlenbits 출력 길이 (비트)
|
||||||
|
def __init__(self, outlenbits):
|
||||||
|
self._init(outlenbits)
|
||||||
|
|
||||||
|
|
||||||
|
## HMAC 계산에 사용하기 위해서 내부 블록 길이 리턴
|
||||||
|
# @param [in] self 객체 포인터
|
||||||
|
# @return 내부 블록 길이
|
||||||
|
def get_blocksize(self):
|
||||||
|
return self._BLOCKSIZE
|
||||||
|
|
||||||
|
|
||||||
|
## 메시지 확장 함수
|
||||||
|
# @param [in] self 객체 포인터
|
||||||
|
# @param [in] data 입력 데이터
|
||||||
|
# @param [in] offset 데이터 시작 인덱스
|
||||||
|
def _msg_expansion(self, data, offset):
|
||||||
|
block = bytearray(data[offset:offset + self._BLOCKSIZE])
|
||||||
|
self._msg[0:32] = struct.unpack(self._FORMAT_IN, block[0:self._BLOCKSIZE])
|
||||||
|
|
||||||
|
for i in range(2, self._NUMSTEP + 1):
|
||||||
|
idx = 16 * i
|
||||||
|
self._msg[idx ] = (self._msg[idx - 16] + self._msg[idx - 29]) & self._MASK
|
||||||
|
self._msg[idx + 1] = (self._msg[idx - 15] + self._msg[idx - 30]) & self._MASK
|
||||||
|
self._msg[idx + 2] = (self._msg[idx - 14] + self._msg[idx - 32]) & self._MASK
|
||||||
|
self._msg[idx + 3] = (self._msg[idx - 13] + self._msg[idx - 31]) & self._MASK
|
||||||
|
self._msg[idx + 4] = (self._msg[idx - 12] + self._msg[idx - 25]) & self._MASK
|
||||||
|
self._msg[idx + 5] = (self._msg[idx - 11] + self._msg[idx - 28]) & self._MASK
|
||||||
|
self._msg[idx + 6] = (self._msg[idx - 10] + self._msg[idx - 27]) & self._MASK
|
||||||
|
self._msg[idx + 7] = (self._msg[idx - 9] + self._msg[idx - 26]) & self._MASK
|
||||||
|
self._msg[idx + 8] = (self._msg[idx - 8] + self._msg[idx - 21]) & self._MASK
|
||||||
|
self._msg[idx + 9] = (self._msg[idx - 7] + self._msg[idx - 22]) & self._MASK
|
||||||
|
self._msg[idx + 10] = (self._msg[idx - 6] + self._msg[idx - 24]) & self._MASK
|
||||||
|
self._msg[idx + 11] = (self._msg[idx - 5] + self._msg[idx - 23]) & self._MASK
|
||||||
|
self._msg[idx + 12] = (self._msg[idx - 4] + self._msg[idx - 17]) & self._MASK
|
||||||
|
self._msg[idx + 13] = (self._msg[idx - 3] + self._msg[idx - 20]) & self._MASK
|
||||||
|
self._msg[idx + 14] = (self._msg[idx - 2] + self._msg[idx - 19]) & self._MASK
|
||||||
|
self._msg[idx + 15] = (self._msg[idx - 1] + self._msg[idx - 18]) & self._MASK
|
||||||
|
|
||||||
|
## 워드 단위 순환 함수
|
||||||
|
# @param [in] self 객체 포인터
|
||||||
|
def _word_permutation(self):
|
||||||
|
self._cv[ 0] = self._tcv[ 6]
|
||||||
|
self._cv[ 1] = self._tcv[ 4]
|
||||||
|
self._cv[ 2] = self._tcv[ 5]
|
||||||
|
self._cv[ 3] = self._tcv[ 7]
|
||||||
|
self._cv[ 4] = self._tcv[12]
|
||||||
|
self._cv[ 5] = self._tcv[15]
|
||||||
|
self._cv[ 6] = self._tcv[14]
|
||||||
|
self._cv[ 7] = self._tcv[13]
|
||||||
|
self._cv[ 8] = self._tcv[ 2]
|
||||||
|
self._cv[ 9] = self._tcv[ 0]
|
||||||
|
self._cv[10] = self._tcv[ 1]
|
||||||
|
self._cv[11] = self._tcv[ 3]
|
||||||
|
self._cv[12] = self._tcv[ 8]
|
||||||
|
self._cv[13] = self._tcv[11]
|
||||||
|
self._cv[14] = self._tcv[10]
|
||||||
|
self._cv[15] = self._tcv[ 9]
|
||||||
|
|
||||||
|
## 스텝 함수 - LSH를 상속받는 클래스에서 별도로 구현해야 함
|
||||||
|
# @param [in] self 객체 포인터
|
||||||
|
# @param [in] idx 스텝 인덱스
|
||||||
|
# @param [in] alpha 회전값 알파
|
||||||
|
# @param [in] beta 회전값 베타
|
||||||
|
def _step(self, idx, alpha, beta):
|
||||||
|
raise NotImplementedError("Implement this method")
|
||||||
|
|
||||||
|
## 압축 함수
|
||||||
|
# @param [in] self 객체 포인터
|
||||||
|
# @param [in] data 입력 데이터
|
||||||
|
# @param [in] offset 데이터 시작 인덱스
|
||||||
|
def _compress(self, data, offset = 0):
|
||||||
|
|
||||||
|
self._msg_expansion(data, offset)
|
||||||
|
|
||||||
|
for idx in range(int(self._NUMSTEP / 2)):
|
||||||
|
self._step(2 * idx, self._ALPHA_EVEN, self._BETA_EVEN)
|
||||||
|
self._step(2 * idx + 1, self._ALPHA_ODD, self._BETA_ODD)
|
||||||
|
|
||||||
|
for idx in range(16):
|
||||||
|
self._cv[idx] ^= self._msg[16 * self._NUMSTEP + idx]
|
||||||
|
|
||||||
|
|
||||||
|
## IV 생성 함수 - LSH를 상속받는 클래스에서 별도로 구현해야 함
|
||||||
|
# @param [in] self 객체 포인터
|
||||||
|
# @param [in] outlenbits 출력 길이 (비트)
|
||||||
|
def _init_iv(self, outlenbits):
|
||||||
|
raise NotImplementedError("Implement this method")
|
||||||
|
|
||||||
|
def _init(self, outlenbits):
|
||||||
|
self._boff = 0
|
||||||
|
self._tcv = [0] * 16
|
||||||
|
self._msg = [0] * (16 * (self._NUMSTEP + 1))
|
||||||
|
self._buf = [0] * self._BLOCKSIZE
|
||||||
|
self._init_iv(outlenbits)
|
||||||
|
|
||||||
|
## 리셋 함수 - 키 입력 직후의 상태로 되돌린다
|
||||||
|
# @param self 객체 포인터
|
||||||
|
def reset(self):
|
||||||
|
self._init(self._outlenbits)
|
||||||
|
|
||||||
|
|
||||||
|
## 업데이트 함수
|
||||||
|
# @param [in] self 객체 포인터
|
||||||
|
# @param [in] data 입력 데이터
|
||||||
|
# @param [in] offset 데이터 시작 오프셋 (바이트)
|
||||||
|
# @param [in] length 데이터 길이 (비트)
|
||||||
|
def update(self, data, offset = 0, length = -1):
|
||||||
|
if data is None or len(data) == 0 or length == 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
if length == -1:
|
||||||
|
length = (len(data) - offset) << 3
|
||||||
|
|
||||||
|
len_bytes = length >> 3
|
||||||
|
len_bits = length & 0x7
|
||||||
|
|
||||||
|
buf_idx = self._boff >> 3
|
||||||
|
|
||||||
|
if (self._boff & 0x7) > 0:
|
||||||
|
raise AssertionError("bit level update is not allowed")
|
||||||
|
|
||||||
|
gap = self._BLOCKSIZE - (self._boff >> 3)
|
||||||
|
|
||||||
|
if len_bytes >= gap:
|
||||||
|
self._buf[buf_idx:self._BLOCKSIZE] = data[offset:offset + gap]
|
||||||
|
self._compress(self._buf)
|
||||||
|
self._boff = 0
|
||||||
|
offset += gap
|
||||||
|
len_bytes -= gap
|
||||||
|
|
||||||
|
while len_bytes >= self._BLOCKSIZE:
|
||||||
|
self._compress(data, offset)
|
||||||
|
offset += self._BLOCKSIZE
|
||||||
|
len_bytes -= self._BLOCKSIZE
|
||||||
|
|
||||||
|
if len_bytes > 0:
|
||||||
|
buf_idx = self._boff >> 3
|
||||||
|
self._buf[buf_idx:buf_idx + len_bytes] = data[offset:offset + len_bytes]
|
||||||
|
self._boff += len_bytes << 3
|
||||||
|
offset += len_bytes
|
||||||
|
|
||||||
|
if len_bits > 0:
|
||||||
|
buf_idx = self._boff >> 3
|
||||||
|
self._buf[buf_idx] = data[offset] & ((0xff >> len_bits) ^ 0xff)
|
||||||
|
self._boff += len_bits
|
||||||
|
|
||||||
|
## 종료 함수 - 최종 해쉬 값을 계산하여 리턴한다
|
||||||
|
# @param [in] self 객체 포인터
|
||||||
|
# @param [in] data 입력 데이터
|
||||||
|
# @param [in] offset 데이터 시작 오프셋 (바이트)
|
||||||
|
# @param [in] length 데이터 길이 (비트)
|
||||||
|
# @return 계산된 해쉬값
|
||||||
|
def final(self, data = None, offset = 0, length = -1):
|
||||||
|
if data is not None:
|
||||||
|
self.update(data, offset, length)
|
||||||
|
|
||||||
|
rbytes = self._boff >> 3
|
||||||
|
rbits = self._boff & 0x7
|
||||||
|
|
||||||
|
if rbits > 0:
|
||||||
|
self._buf[rbytes] |= (0x1 << (7 - rbits))
|
||||||
|
else:
|
||||||
|
self._buf[rbytes] = 0x80
|
||||||
|
|
||||||
|
pos = rbytes + 1
|
||||||
|
if (pos < self._BLOCKSIZE):
|
||||||
|
self._buf[pos:] = [0] * (self._BLOCKSIZE - pos)
|
||||||
|
|
||||||
|
self._compress(self._buf)
|
||||||
|
|
||||||
|
temp = [0] * 8
|
||||||
|
for idx in range(8):
|
||||||
|
temp[idx] = (self._cv[idx] ^ self._cv[idx + 8]) & self._MASK
|
||||||
|
|
||||||
|
self._init(self._outlenbits)
|
||||||
|
|
||||||
|
rbytes = self._outlenbits >> 3
|
||||||
|
rbits = self._outlenbits & 0x7
|
||||||
|
if rbits > 0:
|
||||||
|
rbytes += 1
|
||||||
|
|
||||||
|
result = bytearray(struct.pack(self._FORMAT_OUT, temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7]))
|
||||||
|
result = result[0:rbytes]
|
||||||
|
if rbits > 0:
|
||||||
|
result[rbytes - 1] &= (0xff << (8 - rbits))
|
||||||
|
|
||||||
|
return result
|
|
@ -0,0 +1,104 @@
|
||||||
|
#include<iostream>
|
||||||
|
#include<stack>
|
||||||
|
#include<cstring>
|
||||||
|
#include<cstdlib>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void printStack(stack<int> Stack){
|
||||||
|
stack<int> tmp;
|
||||||
|
while(!Stack.empty()){
|
||||||
|
tmp.push(Stack.top());
|
||||||
|
cout << Stack.top() << ' ';
|
||||||
|
Stack.pop();
|
||||||
|
}
|
||||||
|
while(!tmp.empty()){
|
||||||
|
Stack.push(tmp.top());
|
||||||
|
tmp.pop();
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv ){
|
||||||
|
if(argc<2){
|
||||||
|
cout << "Argument Error";
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
|
FILE* fp = fopen(argv[1],"rt");
|
||||||
|
if(fp == NULL){
|
||||||
|
cout << "File Error.";
|
||||||
|
return(1);
|
||||||
|
}
|
||||||
|
char str[64];
|
||||||
|
char* stok;
|
||||||
|
int op = 0;
|
||||||
|
int arg = 0;
|
||||||
|
int loop = 0;
|
||||||
|
|
||||||
|
stack<int> S;
|
||||||
|
int tmp1, tmp2;
|
||||||
|
|
||||||
|
while(!feof(fp)){
|
||||||
|
loop++;
|
||||||
|
fgets(str, 64, fp);
|
||||||
|
stok = strtok(str,"\n");
|
||||||
|
stok = strtok(str," ");
|
||||||
|
|
||||||
|
op = 0;
|
||||||
|
arg = 0;
|
||||||
|
if(!strcmp(stok,"PUSH")) op = 1;
|
||||||
|
else if(!strcmp(stok,"POP")) op = 2;
|
||||||
|
else if(!strcmp(stok,"ADD")) op = 3;
|
||||||
|
else if(!strcmp(stok,"SUB")) op = 4;
|
||||||
|
else if(!strcmp(stok,"MUL")) op = 5;
|
||||||
|
else if(!strcmp(stok,"EQUAL")) op = 6;
|
||||||
|
else if(!strcmp(stok,"SHOW")) op = 7;
|
||||||
|
|
||||||
|
if(op==1||op==2){
|
||||||
|
stok = strtok(NULL," ");
|
||||||
|
arg = atoi(stok);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch(op){
|
||||||
|
case 1:
|
||||||
|
S.push(arg);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
S.pop();
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
tmp1 = S.top();
|
||||||
|
S.pop();
|
||||||
|
tmp2 = S.top();
|
||||||
|
S.pop();
|
||||||
|
S.push(tmp1 + tmp2);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
tmp1 = S.top();
|
||||||
|
S.pop();
|
||||||
|
tmp2 = S.top();
|
||||||
|
S.pop();
|
||||||
|
S.push(tmp1 - tmp2);
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
tmp1 = S.top();
|
||||||
|
S.pop();
|
||||||
|
tmp2 = S.top();
|
||||||
|
S.pop();
|
||||||
|
S.push(tmp1 * tmp2);
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
tmp1 = S.top();
|
||||||
|
S.pop();
|
||||||
|
tmp2 = S.top();
|
||||||
|
S.pop();
|
||||||
|
S.push((tmp1 == tmp2));
|
||||||
|
break;
|
||||||
|
case 7:
|
||||||
|
cout << S.top() << endl;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// printStack(S);
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
Loading…
Reference in New Issue