Skip to content

Commit

Permalink
gcc 14 support (#62)
Browse files Browse the repository at this point in the history
* rsaKeygenGetDefault test

* add ubuntu 24 and gcc 14 to ci

* use PrngClassPointerConst for rsa keygen

* fix test_brssl

* add comment about ubuntu 24

* define and use ConstPtrPtrHashClass

* remove previous ecKeygen

* remove previous x509NoanchorInit

* improve test

* add ecKeygen test

* add x509NoanchorInit test

* remove commented code

* rename target to linux-gcc-14

* test already exists

* fix tests
  • Loading branch information
diegomrsantos authored Jul 18, 2024
1 parent 646fa21 commit d4e2f55
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 11 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
target:
- os: linux
cpu: amd64
- os: linux-gcc-14 # this is to use ubuntu 24 and install gcc 14. Must be removed when ubuntu-latest is 24.04
cpu: amd64
- os: linux
cpu: i386
- os: macos
Expand All @@ -31,6 +33,9 @@ jobs:
- target:
os: linux
builder: ubuntu-latest
- target:
os: linux-gcc-14 # this is to use ubuntu 24 and install gcc 14. Must be removed when ubuntu-latest is 24.04
builder: ubuntu-24.04
- target:
os: macos
cpu: amd64
Expand Down Expand Up @@ -163,10 +168,20 @@ jobs:
bash build_nim.sh nim csources dist/nimble NimBinaries
echo '${{ github.workspace }}/nim/bin' >> $GITHUB_PATH
- name: Use gcc 14
if : ${{ matrix.target.os == 'linux-gcc-14' }}
run: |
# Add GCC-14 to alternatives
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 14
# Set GCC-14 as the default
sudo update-alternatives --set gcc /usr/bin/gcc-14
- name: Run tests
run: |
nim --version
nimble --version
gcc --version
nimble install -y --depsOnly
env NIMLANG=c nimble test
# C++ support requires fixing const pointer proc assignments
Expand Down
13 changes: 7 additions & 6 deletions bearssl/abi/bearssl_hash.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,20 @@ const
{.compile: bearHashPath & "sha2small.c".}

type
ConstPtrPtrHashClass* {.importc: "const br_hash_class**", header: "bearssl_hash.h", bycopy.} = pointer

HashClass* {.importc: "br_hash_class", header: "bearssl_hash.h", bycopy.} = object
contextSize* {.importc: "context_size".}: uint
desc* {.importc: "desc".}: uint32
init* {.importc: "init".}: proc (ctx: ptr ptr HashClass) {.importcFunc.}
update* {.importc: "update".}: proc (ctx: ptr ptr HashClass; data: pointer;
init* {.importc: "init".}: proc (ctx: ConstPtrPtrHashClass) {.importcFunc.}
update* {.importc: "update".}: proc (ctx: ConstPtrPtrHashClass; data: pointer;
len: uint) {.importcFunc.}
`out`* {.importc: "out".}: proc (ctx: ptr ptr HashClass; dst: pointer) {.importcFunc.}
state* {.importc: "state".}: proc (ctx: ptr ptr HashClass; dst: pointer): uint64 {.
`out`* {.importc: "out".}: proc (ctx: ConstPtrPtrHashClass; dst: pointer) {.importcFunc.}
state* {.importc: "state".}: proc (ctx: ConstPtrPtrHashClass; dst: pointer): uint64 {.
importcFunc.}
setState* {.importc: "set_state".}: proc (ctx: ptr ptr HashClass; stb: pointer;
setState* {.importc: "set_state".}: proc (ctx: ConstPtrPtrHashClass; stb: pointer;
count: uint64) {.importcFunc.}


template hashdesc_Id*(id: untyped): untyped =
((uint32)(id) shl hashdesc_Id_Off)

Expand Down
8 changes: 4 additions & 4 deletions bearssl/abi/bearssl_rsa.nim
Original file line number Diff line number Diff line change
Expand Up @@ -357,20 +357,20 @@ template rsaKbufPubSize*(size: untyped): untyped =


type
RsaKeygen* {.importc: "br_rsa_keygen".} = proc (rngCtx: ptr ptr PrngClass; sk: ptr RsaPrivateKey; kbufPriv: pointer;
RsaKeygen* {.importc: "br_rsa_keygen".} = proc (rngCtx: PrngClassPointerConst; sk: ptr RsaPrivateKey; kbufPriv: pointer;
pk: ptr RsaPublicKey; kbufPub: pointer; size: cuint; pubexp: uint32): uint32 {.
importcFunc.}


proc rsaI15Keygen*(rngCtx: ptr ptr PrngClass; sk: ptr RsaPrivateKey; kbufPriv: pointer;
proc rsaI15Keygen*(rngCtx: PrngClassPointerConst; sk: ptr RsaPrivateKey; kbufPriv: pointer;
pk: ptr RsaPublicKey; kbufPub: pointer; size: cuint; pubexp: uint32): uint32 {.
importcFunc, importc: "br_rsa_i15_keygen", header: "bearssl_rsa.h".}

proc rsaI31Keygen*(rngCtx: ptr ptr PrngClass; sk: ptr RsaPrivateKey; kbufPriv: pointer;
proc rsaI31Keygen*(rngCtx: PrngClassPointerConst; sk: ptr RsaPrivateKey; kbufPriv: pointer;
pk: ptr RsaPublicKey; kbufPub: pointer; size: cuint; pubexp: uint32): uint32 {.
importcFunc, importc: "br_rsa_i31_keygen", header: "bearssl_rsa.h".}

proc rsaI62Keygen*(rngCtx: ptr ptr PrngClass; sk: ptr RsaPrivateKey; kbufPriv: pointer;
proc rsaI62Keygen*(rngCtx: PrngClassPointerConst; sk: ptr RsaPrivateKey; kbufPriv: pointer;
pk: ptr RsaPublicKey; kbufPub: pointer; size: cuint; pubexp: uint32): uint32 {.
importcFunc, importc: "br_rsa_i62_keygen", header: "bearssl_rsa.h".}

Expand Down
2 changes: 1 addition & 1 deletion tests/test_brssl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ suite "x509":
var x509: X509MinimalContext

x509MinimalInit(x509, nil, nil, 0)
x509NoanchorInit(xwc, addr x509.vtable)
x509NoanchorInit(xwc, X509ClassPointerConst(addr x509.vtable))
20 changes: 20 additions & 0 deletions tests/test_ec.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import
unittest2,
../bearssl/[rand, ec]

{.used.}

type
EcPrivateKey* = ref object
buffer*: array[EC_KBUF_PRIV_MAX_SIZE, byte]
key*: ec.EcPrivateKey

suite "ec":
test "test ecKeygen interface":
let rng = HmacDrbgContext.new()

var ecimp = ecGetDefault()
var res = new EcPrivateKey
check ecKeygen(
PrngClassPointerConst(addr rng.vtable), ecimp, addr res.key, addr res.buffer[0], cint(EC_secp256r1)
) != 0
41 changes: 41 additions & 0 deletions tests/test_rsa.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import
unittest2,
../bearssl/[rand, rsa]

{.used.}

const
DefaultKeySize* = 3072 ## Default RSA key size in bits.
DefaultPublicExponent* = 65537'u32

type
RsaPrivateKey* = ref object
buffer*: seq[byte]
seck*: rsa.RsaPrivateKey
pubk*: rsa.RsaPublicKey
pexp*: ptr byte
pexplen*: uint

suite "rsa":
test "test rsaKeygenGetDefault interface":
let rng = HmacDrbgContext.new()

let
sko = 0
pko = rsaKbufPrivSize(DefaultKeySize)
eko = pko + rsaKbufPubSize(DefaultKeySize)
length = eko + ((DefaultKeySize + 7) shr 3)

let res = new RsaPrivateKey
res.buffer = newSeq[byte](length)

var keygen = rsaKeygenGetDefault()
check keygen(
addr rng.vtable,
addr res.seck,
addr res.buffer[sko],
addr res.pubk,
addr res.buffer[pko],
cuint(DefaultKeySize),
DefaultPublicExponent,
) != 0

0 comments on commit d4e2f55

Please sign in to comment.