Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📦 update vendor files, test vendor build #31

Merged
merged 2 commits into from
Nov 19, 2023
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
12 changes: 10 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ jobs:
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: dist
name: dev
path: dist/*.lua

- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: release
path: dist/release/*.lua

# via https://github.com/softprops/action-gh-release/issues/270
- name: Create tag
id: tag
Expand All @@ -63,4 +69,6 @@ jobs:
tag_name: ${{ steps.tag.outputs.tag }}
files: |
dist/telem.lua
dist/telem.min.lua
dist/vendor.lua
dist/release/telem.min.lua
dist/release/vendor.min.lua
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
src/telem/bin/helloWorld.lua
dist/*
src.tar
3 changes: 3 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ SOFTWARE.

Portions of this software are copyright of their respective authors and released under the MIT license:
- lua-object-model, Copyright 2015 Pavel Batečko (Shira). For licensing see src/telem/lib/LICENSE-ObjectModel.txt
- ECNet2, Copyright 2020 Miguel Oliveira. For licensing see src/telem/vendor/LICENSE-ECNet2.txt
- CCryptoLib, Copyright 2023 Miguel Oliveira. For licensing see src/telem/vendor/LICENSE-CCryptoLib.txt
- RedRun, By JackMacWindows. For licensing see src/telem/vendor/redrun.lua
28 changes: 25 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
#!/bin/bash

workdir=$PWD

# echo $workdir

# glob the star
shopt -s globstar

mkdir -p dist
rm -rf dist/*
echo 'building...'
rm -rf dist/*\

echo 'building sources...'
luacc telem.init -o dist/telem.lua -i src $(for i in src/telem/lib/**/*.lua; do echo $i; done | sed 'y/\//./;s/^src.//;s/\.lua$//')

echo 'building vendors...'

cd $workdir/src/telem/vendor

# build vendor package
luacc init -o $workdir/dist/vendor.lua -i . $(for i in **/*.lua; do echo $i; done | sed 'y/\//./;s/^src.vendor.//;s/\.lua$//' | awk '{ if ($1 != "init") { print } }')

# patch redrun function
sed -i 's|\["redrun"\] = function()|["redrun"] = function(...)|g' $workdir/dist/vendor.lua

cd $workdir

echo 'squishing...'
luamin -f dist/telem.lua > dist/telem.min.lua
mkdir -p dist/release
luamin -f dist/telem.lua > dist/release/telem.min.lua
luamin -f dist/vendor.lua > dist/release/vendor.min.lua

# echo 'tarring...'
# tar -cf src.tar src
21 changes: 21 additions & 0 deletions src/telem/vendor/LICENSE-CCryptoLib.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Miguel Oliveira

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.
File renamed without changes.
93 changes: 93 additions & 0 deletions src/telem/vendor/ccryptolib/aead.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
--- The ChaCha20Poly1305AEAD authenticated encryption with associated data (AEAD) construction.

local expect = require "cc.expect".expect
local lassert = require "ccryptolib.internal.util".lassert
local packing = require "ccryptolib.internal.packing"
local chacha20 = require "ccryptolib.chacha20"
local poly1305 = require "ccryptolib.poly1305"

local p8x1, fmt8x1 = packing.compilePack("<I8")
local u4x4, fmt4x4 = packing.compileUnpack("<I4I4I4I4")
local bxor = bit32.bxor

--- Encrypts a message.
--- @param key string A 32-byte random key.
--- @param nonce string A 12-byte per-message unique nonce.
--- @param message string The message to be encrypted.
--- @param aad string aad Arbitrary associated data to also authenticate.
--- @param rounds number? The number of ChaCha20 rounds to use. Defaults to 20.
--- @return string ctx The ciphertext.
--- @return string tag The 16-byte authentication tag.
local function encrypt(key, nonce, message, aad, rounds)
expect(1, key, "string")
lassert(#key == 32, "key length must be 32", 2)
expect(2, nonce, "string")
lassert(#nonce == 12, "nonce length must be 12", 2)
expect(3, message, "string")
expect(4, aad, "string")
rounds = expect(5, rounds, "number", "nil") or 20
lassert(rounds % 2 == 0, "round number must be even", 2)
lassert(rounds >= 8, "round number must be no smaller than 8", 2)
lassert(rounds <= 20, "round number must be no larger than 20", 2)

-- Generate auth key and encrypt.
local msgLong = ("\0"):rep(64) .. message
local ctxLong = chacha20.crypt(key, nonce, msgLong, rounds, 0)
local authKey = ctxLong:sub(1, 32)
local ciphertext = ctxLong:sub(65)

-- Authenticate.
local pad1 = ("\0"):rep(-#aad % 16)
local pad2 = ("\0"):rep(-#ciphertext % 16)
local aadLen = p8x1("<I8", #aad)
local ctxLen = p8x1("<I8", #ciphertext)
local combined = aad .. pad1 .. ciphertext .. pad2 .. aadLen .. ctxLen
local tag = poly1305.mac(authKey, combined)

return ciphertext, tag
end

--- Decrypts a message.
--- @param key string The key used on encryption.
--- @param nonce string The nonce used on encryption.
--- @param tag string The authentication tag returned on encryption.
--- @param ciphertext string The ciphertext to be decrypted.
--- @param aad string The arbitrary associated data used on encryption.
--- @param rounds number The number of rounds used on encryption.
--- @return string? msg The decrypted plaintext. Or nil on auth failure.
local function decrypt(key, nonce, tag, ciphertext, aad, rounds)
expect(1, key, "string")
lassert(#key == 32, "key length must be 32", 2)
expect(2, nonce, "string")
lassert(#nonce == 12, "nonce length must be 12", 2)
expect(3, tag, "string")
lassert(#tag == 16, "tag length must be 16", 2)
expect(4, ciphertext, "string")
expect(5, aad, "string")
rounds = expect(6, rounds, "number", "nil") or 20
lassert(rounds % 2 == 0, "round number must be even", 2)
lassert(rounds >= 8, "round number must be no smaller than 8", 2)
lassert(rounds <= 20, "round number must be no larger than 20", 2)

-- Generate auth key.
local authKey = chacha20.crypt(key, nonce, ("\0"):rep(32), rounds, 0)

-- Check tag.
local pad1 = ("\0"):rep(-#aad % 16)
local pad2 = ("\0"):rep(-#ciphertext % 16)
local aadLen = p8x1(fmt8x1, #aad)
local ctxLen = p8x1(fmt8x1, #ciphertext)
local combined = aad .. pad1 .. ciphertext .. pad2 .. aadLen .. ctxLen
local t1, t2, t3, t4 = u4x4(fmt4x4, tag, 1)
local u1, u2, u3, u4 = u4x4(fmt4x4, poly1305.mac(authKey, combined), 1)
local eq = bxor(t1, u1) + bxor(t2, u2) + bxor(t3, u3) + bxor(t4, u4)
if eq ~= 0 then return nil end

-- Decrypt
return chacha20.crypt(key, nonce, ciphertext, rounds)
end

return {
encrypt = encrypt,
decrypt = decrypt,
}
Loading
Loading