Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Go Test

on:
push:
branches: ["master", "main"]
pull_request:
branches: ["master", "main"]

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
cache: true

- name: Install dependencies
run: go mod download

- name: Run tests
run: go test ./...
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
The MIT License (MIT)

Copyright (c) 2019 TRON-US
Copyright (c) 2025 Torus Labs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
20 changes: 18 additions & 2 deletions ecies.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func Encrypt(pubkeyHex string, plainbytes []byte) (string, *EciesMetadata, error
return "", nil, errors.New("file exceeds the maximum file-size(lt 2G)")
}

// derive Share Secret
// derive shared secret
priv, err := GenerateKey()
if err != nil {
return "", nil, err
Expand All @@ -46,6 +46,9 @@ func Encrypt(pubkeyHex string, plainbytes []byte) (string, *EciesMetadata, error
return "", nil, err
}
shareSecret, err := priv.ECDH(pk)
if err != nil {
return "", nil, err
}

// generate iv
iv, err := randBytes(16)
Expand Down Expand Up @@ -99,7 +102,7 @@ func Decrypt(privateKeyHex string, cipherText string, t *EciesMetadata) (string,
return "", err
}

// derive Share Secret
// derive shared secret
ecdh, err := privKey.ECDH(epk)
if err != nil {
return "", err
Expand All @@ -117,6 +120,19 @@ func Decrypt(privateKeyHex string, cipherText string, t *EciesMetadata) (string,

ecdhHash := sha512.Sum512(ecdh[1:])
encryptionKey := ecdhHash[:32]
macKey := ecdhHash[32:]

// Construct the buffer over which the MAC was originally calculated:
// IV (16) + Uncompressed EphemPubKey (65) + Ciphertext
macData := make([]byte, 0)
macData = append(macData, ivBytes...)
macData = append(macData, epk.Bytes(false)...)
macData = append(macData, cipherBytes...)
mac := getHmacCode(macKey, macData)
if mac != t.Mac {
return "", errors.New("invalid mac hash")
}

plaintext, e := aesCBCDec(encryptionKey, cipherBytes, ivBytes)
if e != nil {
return "", err
Expand Down
9 changes: 7 additions & 2 deletions ecies_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package go_eccrypto

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

const msg = "hello,world\n"
Expand All @@ -13,7 +14,6 @@ const pkHex = "048903aca62f342426d0595597bcd4b03519723c7292f231a5d40c02" +
const privHex = "0abfa58854e585d9bb04a1ffad0f5ac507ac042e7aa69abbcf18f3103a936f6f"

func TestEncrypt(t *testing.T) {

twogBytes := make([]byte, 2*GB)
twogBytes[2*GB-1] = 1
_, _, err := Encrypt(pkHex, twogBytes)
Expand All @@ -25,4 +25,9 @@ func TestEncrypt(t *testing.T) {
decrypted, err := Decrypt(privHex, s, m)
assert.NoError(t, err)
assert.Equal(t, msg, decrypted)

// test invalid mac
m.Mac = "invalid"
_, err = Decrypt(privHex, s, m)
assert.Errorf(t, err, "")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/TRON-US/go-eccrypto
module github.com/torusresearch/go-eccrypto

go 1.13

Expand Down