diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9560f56 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/build/ +/ziran.local.toml diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..307776f --- /dev/null +++ b/LICENSE @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2026, Waozi +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9299edc --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ +ZIRAN ?= ziran +LOCKED := $(if $(wildcard ziran.local.toml),,--locked) +STD = $(shell $(ZIRAN) pkg path ziran $(LOCKED))/std +CRYPTO = $(shell $(ZIRAN) pkg path crypto $(LOCKED)) +KRYON = $(shell $(ZIRAN) pkg path kryon $(LOCKED)) +MINGCUTE = $(shell $(ZIRAN) pkg path mingcute $(LOCKED)) +PATHS = --module-path src --module-path crypto=$(CRYPTO)/src --module-path $(STD) + +.PHONY: test app +# RFC 6238 codes for SHA-1, SHA-256 and SHA-512, RFC 4648 Base32 and the +# authenticator exports, run as portable bundles like the app itself. +test: app + mkdir -p build + for name in totp exports; do \ + $(ZIRAN) bundle --root . $(PATHS) --entry $${name}_test:Run -o build/$$name.zx tests/$${name}_test.zi && \ + test "$$($(ZIRAN) run build/$$name.zx)" = 0 || exit 1; \ + done + @echo 'Auth: RFC 6238 codes, Base32, links and authenticator exports passed' + +# The Auth app as a hosted Kryon bundle, as Werkzeug packs it. +app: + mkdir -p build + $(ZIRAN) bundle --root . $(PATHS) --module-path kryon=$(KRYON)/src/backend --module-path kryon=$(KRYON)/src/ui \ + --module-path $(KRYON)/src/ui --module-path $(MINGCUTE)/src --define KRYON_HOSTED_UI --entry auth:Main -o build/auth.zx src/auth.zi diff --git a/README.md b/README.md new file mode 100644 index 0000000..71edb68 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# Auth + +Two-factor codes as a Ziran package: time-based one-time passwords (RFC 6238) +with HMAC-SHA-1, SHA-256 or SHA-512, `otpauth://` links, authenticator +exports (Google's `otpauth-migration://`, Ente Auth's plain text, Aegis plain +JSON) and the Auth app's screens for Kryon. The hashes come from +[crypto](https://github.com/ziranlang/crypto). + +Werkzeug packs the Auth app and its `werk auth` command from this package. + + make test # RFC vectors and exports + make app # build/auth.zx + +Modules: `auth` (the app), `totp` (codes, Base32 and links) and `exports` +(authenticator exports). diff --git a/src/totp.zi b/src/totp.zi index 5ecf293..d59b44d 100644 --- a/src/totp.zi +++ b/src/totp.zi @@ -1,7 +1,10 @@ // Time-based one-time passwords (RFC 6238) with HMAC-SHA-1, SHA-256 or // SHA-512, and the otpauth:// links authenticator apps export. #import "text_buffer" -#import "sha256" +// The hashes come from the crypto package. +CryptoSha1 :: #import "crypto/sha1"; +CryptoSha256 :: #import "crypto/sha256"; +CryptoSha512 :: #import "crypto/sha512"; HASH_SHA1 :: 1 HASH_SHA256 :: 2 @@ -15,222 +18,20 @@ Digest :: struct { count: s32 } -// The 80 SHA-512 round constants, each as its high and low 32 bits. -sha512_round_halves: [160]u32 = .[ -0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd, 0xb5c0fbcf, 0xec4d3b2f, -0xe9b5dba5, 0x8189dbbc, 0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019, -0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118, 0xd807aa98, 0xa3030242, -0x12835b01, 0x45706fbe, 0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2, -0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1, 0x9bdc06a7, 0x25c71235, -0xc19bf174, 0xcf692694, 0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3, -0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65, 0x2de92c6f, 0x592b0275, -0x4a7484aa, 0x6ea6e483, 0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5, -0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210, 0xb00327c8, 0x98fb213f, -0xbf597fc7, 0xbeef0ee4, 0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725, -0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70, 0x27b70a85, 0x46d22ffc, -0x2e1b2138, 0x5c26c926, 0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df, -0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8, 0x81c2c92e, 0x47edaee6, -0x92722c85, 0x1482353b, 0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001, -0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30, 0xd192e819, 0xd6ef5218, -0xd6990624, 0x5565a910, 0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8, -0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53, 0x2748774c, 0xdf8eeb99, -0x34b0bcb5, 0xe19b48a8, 0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb, -0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3, 0x748f82ee, 0x5defb2fc, -0x78a5636f, 0x43172f60, 0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec, -0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9, 0xbef9a3f7, 0xb2c67915, -0xc67178f2, 0xe372532b, 0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207, -0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178, 0x06f067aa, 0x72176fba, -0x0a637dc5, 0xa2c898a6, 0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b, -0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493, 0x3c9ebe0a, 0x15c9bebc, -0x431d67c4, 0x9c100d4c, 0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a, -0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817 -]; - -RotateLeft32 :: (value: u32, amount: u32) -> u32 { - return (value << amount) | (value >> (cast(u32)32 - amount)) -} - -RotateRight64 :: (value: u64, amount: u64) -> u64 { - return (value >> amount) | (value << (cast(u64)64 - amount)) -} - -// Pads a message the SHA way into blocks: 0x80, zeros, then the bit length -// in the last eight bytes. Returns the padded length. -Pad :: (input: []u8, count: s32, block: s32, output: []u8) -> s32 { - index: s32 = 0 - while index < count { - output[index] = input[index] - index += 1 - } - output[count] = cast(u8)0x80 - length_bytes: s32 = ifx block == 128 then 16 else 8 - total := ((count + 1 + length_bytes + block - 1) / block) * block - index = count + 1 - while index < total { - output[index] = cast(u8)0 - index += 1 - } - bits := cast(u64)count * cast(u64)8 - for shift: 0..7 { - output[total - 1 - cast(s32)shift] = cast(u8)(bits >> (cast(u64)shift * cast(u64)8)) - } - return total -} - -Sha1 :: (input: []u8, count: s32) -> Digest { - padded: [HASH_INPUT_LIMIT + 128]u8 - total := Pad(input, count, 64, padded[:]) - state: [5]u32 = .[0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0] - offset: s32 = 0 - while offset < total { - words: [80]u32 - for index: 0..15 { - at := offset + cast(s32)index * 4 - words[index] = (cast(u32)padded[at] << cast(u32)24) | (cast(u32)padded[at + 1] << cast(u32)16) | - (cast(u32)padded[at + 2] << cast(u32)8) | cast(u32)padded[at + 3] - } - for index: 16..79 { - mixed: u32 = words[index - 3] ^ words[index - 8] - mixed = mixed ^ words[index - 14] - mixed = mixed ^ words[index - 16] - words[index] = RotateLeft32(mixed, cast(u32)1) - } - a := state[0] - b := state[1] - c := state[2] - d := state[3] - e := state[4] - for index: 0..79 { - mix: u32 - constant: u32 - if index < 20 { - mix = (b & c) | (~b & d) - constant = 0x5a827999 - } else if index < 40 { - mix = b ^ c ^ d - constant = 0x6ed9eba1 - } else if index < 60 { - mix = (b & c) | (b & d) | (c & d) - constant = 0x8f1bbcdc - } else { - mix = b ^ c ^ d - constant = 0xca62c1d6 - } - next := RotateLeft32(a, cast(u32)5) + mix + e + constant + words[index] - e = d - d = c - c = RotateLeft32(b, cast(u32)30) - b = a - a = next - } - state[0] += a - state[1] += b - state[2] += c - state[3] += d - state[4] += e - offset += 64 - } - digest: Digest - digest.count = 20 - for index: 0..4 { - for part: 0..3 { - digest.bytes[index * 4 + part] = cast(u8)(state[index] >> (cast(u32)(3 - part) * cast(u32)8)) - } - } - return digest -} - -Sha512 :: (input: []u8, count: s32) -> Digest { - padded: [HASH_INPUT_LIMIT + 256]u8 - total := Pad(input, count, 128, padded[:]) - initial: [16]u32 = .[0x6a09e667, 0xf3bcc908, 0xbb67ae85, 0x84caa73b, 0x3c6ef372, 0xfe94f82b, - 0xa54ff53a, 0x5f1d36f1, 0x510e527f, 0xade682d1, 0x9b05688c, 0x2b3e6c1f, 0x1f83d9ab, 0xfb41bd6b, - 0x5be0cd19, 0x137e2179] - state: [8]u64 - for index: 0..7 { - state[index] = (cast(u64)initial[index * 2] << cast(u64)32) | cast(u64)initial[index * 2 + 1] - } - offset: s32 = 0 - while offset < total { - words: [80]u64 - for index: 0..15 { - value: u64 = 0 - for part: 0..7 { - value = (value << cast(u64)8) | cast(u64)padded[offset + cast(s32)index * 8 + cast(s32)part] - } - words[index] = value - } - for index: 16..79 { - low: u64 = RotateRight64(words[index - 15], cast(u64)1) - low = low ^ RotateRight64(words[index - 15], cast(u64)8) - low = low ^ (words[index - 15] >> cast(u64)7) - high: u64 = RotateRight64(words[index - 2], cast(u64)19) - high = high ^ RotateRight64(words[index - 2], cast(u64)61) - high = high ^ (words[index - 2] >> cast(u64)6) - words[index] = words[index - 16] + low + words[index - 7] + high - } - a := state[0] - b := state[1] - c := state[2] - d := state[3] - e := state[4] - f := state[5] - g := state[6] - h := state[7] - for index: 0..79 { - major: u64 = RotateRight64(e, cast(u64)14) - major = major ^ RotateRight64(e, cast(u64)18) - major = major ^ RotateRight64(e, cast(u64)41) - choose := (e & f) ^ (~e & g) - constant := (cast(u64)sha512_round_halves[index * 2] << cast(u64)32) | cast(u64)sha512_round_halves[index * 2 + 1] - first := h + major + choose + constant + words[index] - minor: u64 = RotateRight64(a, cast(u64)28) - minor = minor ^ RotateRight64(a, cast(u64)34) - minor = minor ^ RotateRight64(a, cast(u64)39) - mixture := (a & b) ^ (a & c) ^ (b & c) - h = g - g = f - f = e - e = d + first - d = c - c = b - b = a - a = first + minor + mixture - } - state[0] += a - state[1] += b - state[2] += c - state[3] += d - state[4] += e - state[5] += f - state[6] += g - state[7] += h - offset += 128 - } - digest: Digest - digest.count = 64 - for index: 0..7 { - for part: 0..7 { - digest.bytes[index * 8 + part] = cast(u8)(state[index] >> (cast(u64)(7 - part) * cast(u64)8)) - } - } - return digest -} - Hash :: (kind: s32, input: []u8, count: s32) -> Digest { + digest: Digest if kind == HASH_SHA256 { - hashed := Sha256(TextView(input[0:count])) - digest: Digest + hashed := CryptoSha256.Sha256Of(TextView(input[0:count])) + for index: 0..31 { digest.bytes[index] = hashed[index] } digest.count = 32 - for index: 0..31 { - digest.bytes[index] = hashed[index] - } - return digest + } else if kind == HASH_SHA512 { + unused CryptoSha512.Sha512(input[0:count], digest.bytes[0:64]) + digest.count = 64 + } else { + unused CryptoSha1.Sha1(input[0:count], digest.bytes[0:20]) + digest.count = 20 } - if kind == HASH_SHA512 { - return Sha512(input, count) - } - return Sha1(input, count) + return digest } // HMAC (RFC 2104). Keys up to KEY_LIMIT bytes and messages up to 64 bytes. @@ -799,7 +600,7 @@ LinkText :: (account: *Account, output: []u8) -> string { // so the same account added on two devices is one record. RecordIdFor :: (link: string, output: []u8) -> string { hex := "0123456789abcdef" - digest := Sha256(link) + digest := CryptoSha256.Sha256Of(link) for at: 0..15 { output[at * 2] = hex[cast(s64)(digest[at] >> cast(u8)4)] output[at * 2 + 1] = hex[cast(s64)(digest[at] & cast(u8)15)] diff --git a/src/exports_test.zi b/tests/exports_test.zi similarity index 100% rename from src/exports_test.zi rename to tests/exports_test.zi diff --git a/src/totp_test.zi b/tests/totp_test.zi similarity index 100% rename from src/totp_test.zi rename to tests/totp_test.zi diff --git a/ziran.lock b/ziran.lock new file mode 100644 index 0000000..b7ac4ab --- /dev/null +++ b/ziran.lock @@ -0,0 +1,47 @@ +{ + "format": 1, + "packages": [ + { + "commit": "fafa2cea97d0cb379d832086ba45a989fa6f9c8e", + "dependencies": {}, + "id": "p5a77c9c35110e925", + "name": "raylib", + "ref": "fafa2cea97d0cb379d832086ba45a989fa6f9c8e", + "source": true, + "url": "https://github.com/raysan5/raylib.git" + }, + { + "commit": "fade061f2e01f635ef412a63453e0c30fe6185ed", + "dependencies": {"raylib": "p5a77c9c35110e925"}, + "id": "p8fbb5bc439b52d33", + "name": "kryon", + "ref": "master", + "url": "https://github.com/kryonlabs/kryon.git" + }, + { + "commit": "d11e915b5ee809b1f76b35850dda71f63ec9e09c", + "dependencies": {}, + "id": "p9f270a7aabc9904e", + "name": "crypto", + "ref": "master", + "url": "https://github.com/ziranlang/crypto.git" + }, + { + "commit": "9cc6872f75447c3a24c16764f9829d0f58751cb0", + "dependencies": {}, + "id": "pe1027b9d16704793", + "name": "mingcute", + "ref": "master", + "url": "https://github.com/kryonlabs/mingcute.git" + } + ], + "root": { + "dependencies": {"crypto": "p9f270a7aabc9904e", "kryon": "p8fbb5bc439b52d33", "mingcute": "pe1027b9d16704793"}, + "name": "auth" + }, + "toolchain": { + "commit": "3c23e5d084e31ad35c3e3e671cba06c985981461", + "ref": "master", + "url": "https://github.com/ziranlang/ziran.git" + } +} diff --git a/ziran.toml b/ziran.toml new file mode 100644 index 0000000..d0d990c --- /dev/null +++ b/ziran.toml @@ -0,0 +1,26 @@ +[package] +tool = "kryon" +name = "auth" +entry = "src/auth.zi" +module_roots = ["src", "tests"] + +[toolchain] +git = "https://github.com/ziranlang/ziran.git" +ref = "master" + +[dependencies.kryon] +git = "https://github.com/kryonlabs/kryon.git" +ref = "master" + +[dependencies.mingcute] +git = "https://github.com/kryonlabs/mingcute.git" +ref = "master" + +[dependencies.crypto] +git = "https://github.com/ziranlang/crypto.git" +ref = "master" + +[exports] +auth = "src/auth.zi" +totp = "src/totp.zi" +exports = "src/exports.zi"