build: separate compiler and libs

This commit is contained in:
Li Jie
2025-01-07 21:49:08 +08:00
parent b0123567cd
commit 1172e5bdce
559 changed files with 190 additions and 176 deletions

View File

@@ -0,0 +1,72 @@
package hmac
// llgo:skipall
import (
"crypto/sha256"
"crypto/subtle"
"hash"
"unsafe"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/openssl"
)
type eface struct {
_type unsafe.Pointer
funcPtr *unsafe.Pointer
}
func funcOf(a any) unsafe.Pointer {
e := (*eface)(unsafe.Pointer(&a))
return *e.funcPtr
}
type digest openssl.HMAC_CTX
func (d *digest) Size() int { panic("todo: hmac.(*digest).Size") }
func (d *digest) BlockSize() int { panic("todo: hmac.(*digest).BlockSize") }
func (d *digest) Reset() {
(*openssl.HMAC_CTX)(d).Reset()
}
func (d *digest) Write(p []byte) (nn int, err error) {
(*openssl.HMAC_CTX)(d).UpdateBytes(p)
return len(p), nil
}
func (d *digest) Sum(in []byte) []byte {
const Size = openssl.EVP_MAX_MD_SIZE
var digestLen c.Uint
hash := (*[Size]byte)(c.Alloca(Size))
(*openssl.HMAC_CTX)(d).Final(&hash[0], &digestLen)
return append(in, hash[:digestLen]...)
}
// New returns a new HMAC hash using the given [hash.Hash] type and key.
// New functions like sha256.New from [crypto/sha256] can be used as h.
// h must return a new Hash every time it is called.
// Note that unlike other hash implementations in the standard library,
// the returned Hash does not implement [encoding.BinaryMarshaler]
// or [encoding.BinaryUnmarshaler].
func New(h func() hash.Hash, key []byte) hash.Hash {
var md *openssl.EVP_MD
switch funcOf(h) {
case c.Func(sha256.New):
md = openssl.EVP_sha256()
default:
panic("todo: hmac.New: unsupported hash function")
}
ctx := openssl.NewHMAC_CTX()
ctx.InitBytes(key, md)
return (*digest)(ctx)
}
// Equal compares two MACs for equality without leaking timing information.
func Equal(mac1, mac2 []byte) bool {
// We don't have to be constant time if the lengths of the MACs are
// different as that suggests that a completely different hash function
// was used.
return subtle.ConstantTimeCompare(mac1, mac2) == 1
}

View File

@@ -0,0 +1,71 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package md5
// llgo:skipall
import (
"crypto"
"hash"
"unsafe"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/openssl"
)
func init() {
crypto.RegisterHash(crypto.MD5, New)
}
// The blocksize of MD5 in bytes.
const BlockSize = 64
// The size of an MD5 checksum in bytes.
const Size = 16
type digest struct {
ctx openssl.MD5_CTX
}
func (d *digest) Size() int { return Size }
func (d *digest) BlockSize() int { return BlockSize }
func (d *digest) Reset() {
d.ctx.Init()
}
func (d *digest) Write(p []byte) (nn int, err error) {
d.ctx.UpdateBytes(p)
return len(p), nil
}
func (d *digest) Sum(in []byte) []byte {
hash := (*[Size]byte)(c.Alloca(Size))
d.ctx.Final((*byte)(unsafe.Pointer(hash)))
return append(in, hash[:]...)
}
func New() hash.Hash {
d := new(digest)
d.ctx.Init()
return d
}
func Sum(data []byte) (ret [Size]byte) {
openssl.MD5Bytes(data, &ret[0])
return
}

View File

@@ -0,0 +1,105 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rand
// llgo:skipall
import (
"io"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/openssl"
"github.com/qiniu/x/errors"
)
type rndReader struct{}
func (rndReader) Read(p []byte) (n int, err error) {
return Read(p)
}
// Reader is a global, shared instance of a cryptographically
// secure random number generator.
var Reader io.Reader = rndReader{}
type opensslError struct {
file *c.Char
line c.Int
flags c.Int
function *c.Char
data *c.Char
err openssl.Errno
}
// [error code]:[library name]:[function name]:[reason string]:[[filename:line]]: [text message]
func (p *opensslError) Error() string {
const bufsize = 1024
buf := (*c.Char)(c.Alloca(bufsize))
lib := openssl.ERRLibErrorString(p.err)
reason := openssl.ERRReasonErrorString(p.err)
n := uintptr(c.Snprintf(
buf, bufsize,
c.Str("%d:%s:%s:%s:[%s:%d]: "),
p.err, lib, p.function, reason, p.file, p.line))
n += c.Strlen(openssl.ERRErrorString(p.err, c.Advance(buf, n)))
return c.GoString(buf, n)
}
func getError() *opensslError {
ret := new(opensslError)
err := openssl.ERRGetErrorAll(&ret.file, &ret.line, &ret.function, &ret.data, &ret.flags)
if err == 0 {
return nil
}
return ret
}
func getErrors() error {
var errs errors.List
for openssl.ERRPeekError() != 0 {
errs.Add(getError())
}
return errs.ToError()
}
// Read is a helper function that calls Reader.Read using io.ReadFull.
// On return, n == len(b) if and only if err == nil.
func Read(b []byte) (n int, err error) {
if openssl.RANDBytes(b) != 0 {
return len(b), nil
}
return 0, getErrors()
}
/* TODO(xsw):
// batched returns a function that calls f to populate a []byte by chunking it
// into subslices of, at most, readMax bytes.
func batched(f func([]byte) error, readMax int) func([]byte) error {
return func(out []byte) error {
for len(out) > 0 {
read := len(out)
if read > readMax {
read = readMax
}
if err := f(out[:read]); err != nil {
return err
}
out = out[read:]
}
return nil
}
}
*/

View File

@@ -0,0 +1,98 @@
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rand
/* TODO(xsw):
import (
"errors"
"io"
"math/big"
)
// Prime returns a number of the given bit length that is prime with high probability.
// Prime will return error for any error returned by rand.Read or if bits < 2.
func Prime(rand io.Reader, bits int) (*big.Int, error) {
if bits < 2 {
return nil, errors.New("crypto/rand: prime size must be at least 2-bit")
}
b := uint(bits % 8)
if b == 0 {
b = 8
}
bytes := make([]byte, (bits+7)/8)
p := new(big.Int)
for {
if _, err := io.ReadFull(rand, bytes); err != nil {
return nil, err
}
// Clear bits in the first byte to make sure the candidate has a size <= bits.
bytes[0] &= uint8(int(1<<b) - 1)
// Don't let the value be too small, i.e, set the most significant two bits.
// Setting the top two bits, rather than just the top bit,
// means that when two of these values are multiplied together,
// the result isn't ever one bit short.
if b >= 2 {
bytes[0] |= 3 << (b - 2)
} else {
// Here b==1, because b cannot be zero.
bytes[0] |= 1
if len(bytes) > 1 {
bytes[1] |= 0x80
}
}
// Make the value odd since an even number this large certainly isn't prime.
bytes[len(bytes)-1] |= 1
p.SetBytes(bytes)
if p.ProbablyPrime(20) {
return p, nil
}
}
}
// Int returns a uniform random value in [0, max). It panics if max <= 0.
func Int(rand io.Reader, max *big.Int) (n *big.Int, err error) {
if max.Sign() <= 0 {
panic("crypto/rand: argument to Int is <= 0")
}
n = new(big.Int)
n.Sub(max, n.SetUint64(1))
// bitLen is the maximum bit length needed to encode a value < max.
bitLen := n.BitLen()
if bitLen == 0 {
// the only valid result is 0
return
}
// k is the maximum byte length needed to encode a value < max.
k := (bitLen + 7) / 8
// b is the number of bits in the most significant byte of max-1.
b := uint(bitLen % 8)
if b == 0 {
b = 8
}
bytes := make([]byte, k)
for {
_, err = io.ReadFull(rand, bytes)
if err != nil {
return nil, err
}
// Clear bits in the first byte to increase the probability
// that the candidate is < max.
bytes[0] &= uint8(int(1<<b) - 1)
n.SetBytes(bytes)
if n.Cmp(max) < 0 {
return
}
}
}
*/

View File

@@ -0,0 +1,73 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sha1
// llgo:skipall
import (
"crypto"
"hash"
"unsafe"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/openssl"
)
func init() {
crypto.RegisterHash(crypto.SHA1, New)
}
// The blocksize of SHA-1 in bytes.
const BlockSize = 64
// The size of a SHA-1 checksum in bytes.
const Size = 20
type digest struct {
ctx openssl.SHA_CTX
}
func (d *digest) Size() int { return Size }
func (d *digest) BlockSize() int { return BlockSize }
func (d *digest) Reset() {
d.ctx.Init()
}
func (d *digest) Write(p []byte) (nn int, err error) {
d.ctx.UpdateBytes(p)
return len(p), nil
}
func (d *digest) Sum(in []byte) []byte {
hash := (*[Size]byte)(c.Alloca(Size))
d.ctx.Final((*byte)(unsafe.Pointer(hash)))
return append(in, hash[:]...)
}
// New returns a new hash.Hash computing the SHA1 checksum.
func New() hash.Hash {
d := new(digest)
d.ctx.Init()
return d
}
// Sum returns the SHA-1 checksum of the data.
func Sum(data []byte) (ret [Size]byte) {
openssl.SHA1Bytes(data, &ret[0])
return
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sha256
import (
"hash"
"unsafe"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/openssl"
)
// The size of a SHA224 checksum in bytes.
const Size224 = 28
type digest224 struct {
ctx openssl.SHA224_CTX
}
func (d *digest224) Size() int { return Size224 }
func (d *digest224) BlockSize() int { return BlockSize }
func (d *digest224) Reset() {
d.ctx.Init()
}
func (d *digest224) Write(p []byte) (nn int, err error) {
d.ctx.UpdateBytes(p)
return len(p), nil
}
func (d *digest224) Sum(in []byte) []byte {
hash := (*[Size]byte)(c.Alloca(Size))
d.ctx.Final((*byte)(unsafe.Pointer(hash)))
return append(in, hash[:]...)
}
// New224 returns a new hash.Hash computing the SHA224 checksum.
func New224() hash.Hash {
d := new(digest224)
d.ctx.Init()
return d
}
// Sum224 returns the SHA224 checksum of the data.
func Sum224(data []byte) (ret [Size224]byte) {
openssl.SHA224Bytes(data, &ret[0])
return
}

View File

@@ -0,0 +1,74 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sha256
// llgo:skipall
import (
"crypto"
"hash"
"unsafe"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/openssl"
)
func init() {
crypto.RegisterHash(crypto.SHA224, New224)
crypto.RegisterHash(crypto.SHA256, New)
}
// The blocksize of SHA256 and SHA224 in bytes.
const BlockSize = 64
// The size of a SHA256 checksum in bytes.
const Size = 32
type digest256 struct {
ctx openssl.SHA256_CTX
}
func (d *digest256) Size() int { return Size }
func (d *digest256) BlockSize() int { return BlockSize }
func (d *digest256) Reset() {
d.ctx.Init()
}
func (d *digest256) Write(p []byte) (nn int, err error) {
d.ctx.UpdateBytes(p)
return len(p), nil
}
func (d *digest256) Sum(in []byte) []byte {
hash := (*[Size]byte)(c.Alloca(Size))
d.ctx.Final((*byte)(unsafe.Pointer(hash)))
return append(in, hash[:]...)
}
// New returns a new hash.Hash computing the SHA256 checksum.
func New() hash.Hash {
d := new(digest256)
d.ctx.Init()
return d
}
// Sum256 returns the SHA256 checksum of the data.
func Sum256(data []byte) (ret [Size]byte) {
openssl.SHA256Bytes(data, &ret[0])
return
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sha512
import (
"hash"
"unsafe"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/openssl"
)
type digest384 struct {
ctx openssl.SHA384_CTX
}
func (d *digest384) Size() int { return Size384 }
func (d *digest384) BlockSize() int { return BlockSize }
func (d *digest384) Reset() {
d.ctx.Init()
}
func (d *digest384) Write(p []byte) (nn int, err error) {
d.ctx.UpdateBytes(p)
return len(p), nil
}
func (d *digest384) Sum(in []byte) []byte {
hash := (*[Size]byte)(c.Alloca(Size))
d.ctx.Final((*byte)(unsafe.Pointer(hash)))
return append(in, hash[:]...)
}
func New384() hash.Hash {
d := new(digest384)
d.ctx.Init()
return d
}
func Sum384(data []byte) (ret [Size384]byte) {
openssl.SHA384Bytes(data, &ret[0])
return
}

View File

@@ -0,0 +1,102 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sha512
// llgo:skipall
import (
"crypto"
"hash"
"unsafe"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/openssl"
)
func init() {
crypto.RegisterHash(crypto.SHA384, New384)
crypto.RegisterHash(crypto.SHA512, New)
crypto.RegisterHash(crypto.SHA512_224, New512_224)
crypto.RegisterHash(crypto.SHA512_256, New512_256)
}
const (
// Size is the size, in bytes, of a SHA-512 checksum.
Size = 64
// Size224 is the size, in bytes, of a SHA-512/224 checksum.
Size224 = 28
// Size256 is the size, in bytes, of a SHA-512/256 checksum.
Size256 = 32
// Size384 is the size, in bytes, of a SHA-384 checksum.
Size384 = 48
// BlockSize is the block size, in bytes, of the SHA-512/224,
// SHA-512/256, SHA-384 and SHA-512 hash functions.
BlockSize = 128
)
type digest512 struct {
ctx openssl.SHA512_CTX
}
func (d *digest512) Size() int { return Size }
func (d *digest512) BlockSize() int { return BlockSize }
func (d *digest512) Reset() {
d.ctx.Init()
}
func (d *digest512) Write(p []byte) (nn int, err error) {
d.ctx.UpdateBytes(p)
return len(p), nil
}
func (d *digest512) Sum(in []byte) []byte {
hash := (*[Size]byte)(c.Alloca(Size))
d.ctx.Final((*byte)(unsafe.Pointer(hash)))
return append(in, hash[:]...)
}
func New() hash.Hash {
d := new(digest512)
d.ctx.Init()
return d
}
func Sum512(data []byte) (ret [Size]byte) {
openssl.SHA512Bytes(data, &ret[0])
return
}
func New512_224() hash.Hash {
panic("todo: crypto/sha512.New512_224")
}
func Sum512_224(data []byte) [Size224]byte {
panic("todo: crypto/sha512.Sum512_224")
}
func New512_256() hash.Hash {
panic("todo: crypto/sha512.New512_256")
}
func Sum512_256(data []byte) [Size256]byte {
panic("todo: crypto/sha512.Sum512_256")
}

View File

@@ -0,0 +1,22 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package subtle
// llgo:skip XORBytes
import (
_ "unsafe"
)