Class: Ltec::EC

Inherits:
Object
  • Object
show all
Defined in:
lib/ltec.rb

Overview

Your code goes here…

Constant Summary collapse

SECP256K1 =
'secp256k1'

Class Method Summary collapse

Class Method Details

.base64(str) ⇒ Object



12
13
14
# File 'lib/ltec.rb', line 12

def EC.base64(str)
    return Base64.strict_encode64(str)
end

.base64Decode(str) ⇒ Object



15
16
17
# File 'lib/ltec.rb', line 15

def EC.base64Decode(str)
    return Base64.decode64(str)
end

.base64ToHex(base64) ⇒ Object



27
28
29
# File 'lib/ltec.rb', line 27

def EC.base64ToHex(base64)
    return base64.unpack("m*")[0].unpack('H*')[0]
end

.decrypt(secKey, base64Cipher) ⇒ Object



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
# File 'lib/ltec.rb', line 117

def EC.decrypt(secKey,base64Cipher)
    encResult = base64Decode(base64Cipher)
    start = 8
    nonce = encResult[start...(start + 16)] 
    start = start + 16;
    mac = encResult[start...(start + 32)]
    start = start + 32;
    tmpPub = encResult[start...(start + 33)]
    start = start + 33;
    dataEnc = encResult[start...(encResult.length)]

    tmpPubHex = toHex(tmpPub)
    ec = OpenSSL::PKey::EC.new(SECP256K1)
    tmpBn = OpenSSL::BN.new(tmpPubHex,16)

    tmpPt = OpenSSL::PKey::EC::Point.new(ec.group,tmpBn)

    priKey = OpenSSL::BN.new(toHex(base64Decode(secKey)),16) 
    ptDh = tmpPt.mul(priKey);

    ptX = fromHex(ptDh.to_bn(:compressed).to_s(16))[1...33]
    dhHash = OpenSSL::Digest.digest("SHA512", ptX)

    key = dhHash[0...32]
    hmakkey = dhHash[32...64]

    # check mac 
    dataforMac = nonce + tmpPub + dataEnc
    mac2 = OpenSSL::HMAC.digest('sha256', hmakkey, dataforMac)
    if mac2 != mac 
        raise 'Mac not Fit,the privateKey is not fit'
    end
    # encryptor = Salsa20.new(key, nonce)
    # txt = encryptor.decrypt(dataEnc)
    encryptor = OpenSSL::Cipher::AES256.new(:CBC)
    encryptor.decrypt
    encryptor.key =  key
    encryptor.iv =  nonce
    txt = encryptor.update(dataEnc) + encryptor.final
    return txt
end

.encrypt(pubKey, msg) ⇒ Object



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
# File 'lib/ltec.rb', line 70

def EC.encrypt(pubKey,msg)
    hex = base64ToHex(pubKey)
    ec = OpenSSL::PKey::EC.new(SECP256K1)
    # puts ec 
    # puts ec.group
    pubNum = OpenSSL::BN.new(hex,16)
    pt = OpenSSL::PKey::EC::Point.new(ec.group,pubNum)

    randKey = OpenSSL::Random.random_bytes(32)
    hexRnd = toHex(randKey)
    rndBn = OpenSSL::BN.new(hexRnd,16)

    # 0 < rndBn < OrderOfG
    if rndBn.zero? 
        raise "OpenSSL::Random.random_bytes generate Fail"
    end


    rndPt =  pt.mul(0,rndBn)
    hexpt = rndPt.to_bn(:compressed).to_s(16)

    empherPub = fromHex(hexpt)
        
    #ecdh 
    ptDh = pt.mul(rndBn)
    ptX = fromHex(ptDh.to_bn(:compressed).to_s(16))[1...33]
        
    dhHash = OpenSSL::Digest.digest("SHA512", ptX)
        
    nonce = OpenSSL::Random.random_bytes(16)

    encryptor = OpenSSL::Cipher::AES256.new(:CBC)
    encryptor.encrypt
    encryptor.key =  dhHash[0...32]
    encryptor.iv =  nonce
    

    # encryptor = Salsa20.new(dhHash[0...32], nonce)
    encrypted_text = encryptor.update(msg) + encryptor.final
    dataforMac = nonce + empherPub + encrypted_text
    mac = OpenSSL::HMAC.digest('sha256', dhHash[32,64], dataforMac)

    # 
    return base64(fromHex('0100100020002100') + nonce + mac + empherPub + encrypted_text)
        
end

.fromHex(hex) ⇒ Object



21
22
23
# File 'lib/ltec.rb', line 21

def EC.fromHex(hex)
    return [hex].pack("H*")
end

.generateKeyPair(inputSecKey) ⇒ Object



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
# File 'lib/ltec.rb', line 31

def EC.generateKeyPair(inputSecKey)
    if inputSecKey 
        puts "generate key pair from secret key"
       if inputSecKey.length < 44   # 32 byte
           throw "secret key length error ,it's 32 "
           
       end

        ec = OpenSSL::PKey::EC.new(SECP256K1)
        pubNum = OpenSSL::BN.new("1",16)
        tmpPt = OpenSSL::PKey::EC::Point.new(ec.group)

        priKey = OpenSSL::BN.new(toHex(base64Decode(inputSecKey)),16) 

        maxStr = 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141'
        OpenSSL
        max = OpenSSL::BN.new(maxStr,16) 
        if priKey >= max
            throw "Private Key must be smaller than #{maxStr}"
        end




        pubPt =  tmpPt.mul(0,priKey)
        hexpt = pubPt.to_bn(:compressed).to_s(16)
        pub64 = hexToBase64(hexpt)

        return {"seckey" => inputSecKey.strip,"pubkey" => pub64.strip}
    else 
        puts 'create New key pair'
        ec1 = OpenSSL::PKey::EC.generate(SECP256K1)
        seckey = EC.hexToBase64(ec1.private_key.to_s(16))
        pubkey = EC.hexToBase64(ec1.public_key.to_bn(:compressed).to_s(16))
        return {"seckey" => seckey.strip,"pubkey" => pubkey.strip}
    end
   
end

.hexToBase64(hex) ⇒ Object



24
25
26
# File 'lib/ltec.rb', line 24

def EC.hexToBase64(hex)
    return [[hex].pack("H*")].pack("m") 
end

.testObject



159
160
161
# File 'lib/ltec.rb', line 159

def EC.test()
   puts 'hello R'
end

.toHex(str) ⇒ Object



18
19
20
# File 'lib/ltec.rb', line 18

def EC.toHex(str)
    return str.unpack('H*')[0]
end

.xDec(priKey, msg) ⇒ Object



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
# File 'lib/ltec.rb', line 164

def EC.xDec(priKey,msg)
    priHex = base64ToHex(priKey)
    prN = OpenSSL::BN.new(priHex,16)
    bfMsg = base64Decode(msg)
    pub = bfMsg[0,33]
    pubhex = toHex(pub)
    

    
    pubNum = OpenSSL::BN.new(pubhex,16)
    curve = OpenSSL::PKey::EC::Group.new(SECP256K1)
    ptPub = OpenSSL::PKey::EC::Point.new(curve,pubNum)

    ptDh = ptPub.mul(prN)
    ptX = fromHex(ptDh.to_bn(:compressed).to_s(16))[1...33]

    dhHash = OpenSSL::Digest.digest("SHA512", ptX)
    encryptor = OpenSSL::Cipher::AES256.new(:CTR)
    encryptor.decrypt
    encryptor.key =  dhHash[0...32]
    encryptor.iv =  dhHash[32...48]

    
    EC.base64(encryptor.update(bfMsg[33..-1]) + encryptor.final)
end

.xEnc(pubKey, msg64) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/ltec.rb', line 189

def EC.xEnc(pubKey,msg64)
    msg = base64Decode(msg64)
    hex = base64ToHex(pubKey)


    ec = OpenSSL::PKey::EC.new(SECP256K1)
    curve = OpenSSL::PKey::EC::Group.new(SECP256K1)
    kp = OpenSSL::PKey::EC::generate(curve)
    pubNum = OpenSSL::BN.new(hex,16)
    ptPub = OpenSSL::PKey::EC::Point.new(ec.group,pubNum)

    rndBn = kp.private_key.to_bn
    ptTmp = kp.public_key

    ptDh =  ptPub.mul(rndBn)
    ptX = fromHex(ptDh.to_bn(:compressed).to_s(16))[1...33]

    dhHash = OpenSSL::Digest.digest("SHA512", ptX)
    encryptor = OpenSSL::Cipher::AES256.new(:CTR)
    encryptor.encrypt
    encryptor.key =  dhHash[0...32]
    encryptor.iv =  dhHash[32...48]
    EC.base64( fromHex(ptTmp.to_bn(:compressed).to_s(16)) + encryptor.update(msg) + encryptor.final)
end