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

remove cstruct from mirage-crypto #214

Merged
merged 21 commits into from
Mar 19, 2024
Merged

remove cstruct from mirage-crypto #214

merged 21 commits into from
Mar 19, 2024

Conversation

hannesm
Copy link
Member

@hannesm hannesm commented Mar 15, 2024

that's stlll WIP (compiles, but there are test failures) -- likely there's something wrong when I transcribed from cstruct to bytes/string.

also should be reviewed in terms of where stuff is unecessarily copied and could be avoided to be copied

//cc @reynir who may be interested ;)

@hannesm hannesm mentioned this pull request Mar 15, 2024
23 tasks
Copy link
Member

@reynir reynir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes in ccm.ml are a bit harder to follow and I will need to revisit.

I will suggest some changes.

src/cipher_block.ml Outdated Show resolved Hide resolved
src/cipher_block.ml Outdated Show resolved Hide resolved
src/cipher_block.ml Outdated Show resolved Hide resolved
@@ -256,25 +253,38 @@ module Modes = struct

let tag_size = GHASH.tagsize
let key_sizes, block_size = C.(key, block)
let z128, h = create block_size, create block_size
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we don't need a global h anymore as bytes are cheaper to allocate?

Comment on lines +265 to +270
let pack64s =
let _cs = Bytes.create 16 in
fun a b ->
Bytes.set_int64_be _cs 0 a;
Bytes.set_int64_be _cs 8 b;
Bytes.unsafe_to_string _cs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let pack64s =
let _cs = Bytes.create 16 in
fun a b ->
Bytes.set_int64_be _cs 0 a;
Bytes.set_int64_be _cs 8 b;
Bytes.unsafe_to_string _cs
let pack64s a b =
let b = Bytes.create 16 in
Bytes.set_int64_be b 0 a;
Bytes.set_int64_be b 8 b;
Bytes.unsafe_to_string b

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should evaluate this together with #186 in mind -- we have at various places global buffers (that are bad for OCaml 5, and eventually there's not much performance impact). I'd prefer to keep this "remove global buffers" out of this PR.

src/cipher_block.ml Show resolved Hide resolved
src/cipher_stream.ml Outdated Show resolved Hide resolved
src/ccm.ml Outdated Show resolved Hide resolved
src/ccm.ml Outdated Show resolved Hide resolved
src/ccm.ml Outdated Show resolved Hide resolved
Co-authored-by: Reynir Björnsson <reynir@reynir.dk>
@hannesm
Copy link
Member Author

hannesm commented Mar 18, 2024

thanks for the review, @reynir. indeed CCM could get another pass to reduce allocations and remove code duplications.

@reynir
Copy link
Member

reynir commented Mar 18, 2024

I get 15 failures. If I comment out __mc_ACCELERATE__ and __mc_detect_features__ in mirage_crypto.h I get a single ccm failure. It seems to me there is an error in the aesni code.

@hannesm
Copy link
Member Author

hannesm commented Mar 19, 2024

I did a bench between v0.11.3 and the recent commit here.

Takeaways: eventually investigate -pk performance drop (~10%); symmetric ciphers with big blocks got slower (investigate!).

If you want to post results, please take the time to reformat into tables (as below) to ensure that it is nice to read (maybe we should enhance speed.ml to output markdown directly).

Mirage-crypto-pk

rsa-generate

bits v0.11.3 2e67dec
1024 246.514 242.406
2048 28.892 27.669
4096 2.575 2.729

rsa-encrypt

bits v0.11.3 2e67dec
1024 99662.756 88124.200
2048 32551.288 28837.535
4096 9396.019 8917.400

rsa-decrypt

bits v0.11.3 2e67dec
1024 3948.342 4103.564
2048 777.000 752.930
4096 123.015 116.024

rsa-pkcs1-encrypt

bits v0.11.3 2e67dec
1024 80178.407 78159.531
2048 25902.508 24429.165
4096 8264.526 7602.220

rsa-pkcs1-decrypt

bits v0.11.3 2e67dec
1024 4318.791 4024.526
2048 799.547 751.573
4096 119.448 110.380

rsa-pkcs1-sign

bits v0.11.3 2e67dec
1024 3661.597 3323.568
2048 710.001 638.282
4096 113.858 103.422

rsa-pkcs1-verify

bits v0.11.3 2e67dec
1024 69396.681 65590.797
2048 24099.968 21826.953
4096 7716.985 7017.554

rsa-pss-sign

bits v0.11.3 2e67dec
1024 4120.499 3740.859
2048 777.121 700.463
4096 118.271 109.177

rsa-pss-verify

bits v0.11.3 2e67dec
1024 57217.814 56124.437
2048 20969.993 20105.002
4096 7222.298 6545.716

dsa-generate

bits v0.11.3 2e67dec
1024 36.701 30.270
2048 2.806 2.994
3072 0.652 0.622

dsa-sign

bits v0.11.3 2e67dec
1024 5275.585 6231.120
2048 1556.582 1680.906
3072 804.983 816.800

dsa-verify

bits v0.11.3 2e67dec
1024 5816.408 5663.748
2048 1045.922 958.183
3072 518.107 481.354

dh-secret

ff v0.11.3 2e67dec
oakley5 (1536) 3032.128 3060.912
oakley14 (2048) 1802.198 1788.864
ffdhe2048 1831.895 1806.268
ffdhe3072 698.281 690.943
ffdhe4096 332.427 333.557
ffdhe6144 152.474 150.756

dh-share

ff v0.11.3 2e67dec
oakley5 (1536) 3136.975 3060.805
oakley14 (2048) 1815.464 1795.719
ffdhe2048 1770.226 1783.207
ffdhe3072 707.039 686.098
ffdhe4096 334.105 330.434
ffdhe6144 149.409 149.783

Mirage-crypto-EC

ecdsa-generate

curve v0.11.3 2e67dec
P224 21111.244
P256 19111.238 18093.548
P384 6875.565 6255.985
P521 1984.049 1913.943
Ed25519 23010.254 21235.631

ecdsa-sign

curve v0.11.3 2e67dec
P224 8007.429
P256 7087.295 7080.622
P384 2994.033 2943.164
P521 369.769 452.361
Ed25519 9707.768 10410.737

ecdsa-verify

curve v0.11.3 2e67dec
P224 1617.198
P256 1403.344 1373.387
P384 471.174 505.225
P521 188.230 199.992
Ed25519 7318.362 7570.709

ecdh-secret

curve v0.11.3 2e67dec
P224 12823.859
P256 10983.179 11464.203
P384 4233.049 4278.266
P521 1338.889 1532.938
X25519 13435.456 13697.952

ecdh-share

curve v0.11.3 2e67dec
P224 1977.251
P256 1718.880 1803.838
P384 658.676 661.094
P521 209.398 259.003
X25519 11992.015 13780.238

Mirage-crypto

chacha20-poly1305

bytes v0.11.3 2e67dec
16 8.750088 22.992639
64 33.065724 86.531219
256 101.673887 185.873385
1024 174.578238 255.051333
8192 239.535956 269.871226

aes-128-ecb

bytes v0.11.3 2e67dec
16 105.498096 292.146049
64 362.391916 831.209913
256 1251.160621 2419.865435
1024 2243.991188 3635.036516
8192 3085.718113 2747.862807

aes-128-cbc-e

bytes v0.11.3 2e67dec
16 100.547901 212.237948
64 262.149728 367.067685
256 398.493632 440.613705
1024 470.466794 464.008161
8192 510.332812 444.280881

aes-128-cbc-d

bytes v0.11.3 2e67dec
16 107.420509 220.133840
64 342.587800 630.087043
256 1037.329198 1891.342696
1024 1918.137900 2737.107887
8192 2550.317491 2147.277408

aes-128-ctr

bytes v0.11.3 2e67dec
16 113.291728 213.463761
64 344.533307 604.605176
256 982.143485 1663.046752
1024 1722.602513 2267.417022
8192 2284.769280 1660.058867

aes-128-gcm

bytes v0.11.3 2e67dec
16 29.240972 56.823077
64 95.538922 190.837645
256 364.810213 626.125396
1024 768.352643 1167.826327
8192 1494.688871 1044.287684

aes-128-ghash

bytes v0.11.3 2e67dec
16 33.228730 64.452378
64 118.162103 223.296100
256 456.438386 890.197201
1024 1536.536639 2416.307651
8192 4287.510326 4932.628748

aes-128-ccm

bytes v0.11.3 2e67dec
16 8.454101 42.842319
64 26.258126 92.953420
256 64.893819 133.830367
1024 103.561627 150.109963
8192 131.821323 147.169430

aes-192-ecb

bytes v0.11.3 2e67dec
16 95.685035 272.300153
64 369.142699 750.807963
256 1132.333746 2236.807647
1024 2003.572784 3069.053425
8192 2710.635347 2114.342020

aes-256-ecb

bytes v0.11.3 2e67dec
16 90.861583 246.026025
64 367.473906 681.153257
256 1073.220138 2006.093733
1024 1670.284999 2688.379832
8192 2049.013546 2032.937651

d3des-ecb

bytes v0.11.3 2e67dec
16 15.213730 17.693343
64 18.047863 19.096525
256 19.054213 19.331436
1024 18.679193 19.500274
8192 19.350125 19.472752

Mirage-crypto-rng

fortuna

bytes v0.11.3 2e67dec
16 40.128478 61.275627
64 145.876685 201.986627
256 505.176774 697.574029
1024 1257.045894 1439.696009
8192 2201.152848 1473.062793

@hannesm hannesm merged commit cfa9412 into mirage:main Mar 19, 2024
23 of 26 checks passed
@hannesm hannesm deleted the no-cstruct branch March 19, 2024 13:53
hannesm added a commit to hannesm/opam-repository that referenced this pull request Jun 29, 2024
CHANGES:

### Breaking changes

* mirage-crypto: Poly1305 API now uses string (mirage/mirage-crypto#203 @hannesm)
* mirage-crypto: Poly1305 no longer has type alias "type mac = string"
  (mirage/mirage-crypto#232 @hannesm)
* mirage-crypto: the API uses string instead of cstruct (mirage/mirage-crypto#214 @reynir @hannesm)
* mirage-crypto: Hash module has been removed. Use digestif if you need hash
  functions (mirage/mirage-crypto#213 @hannesm)
* mirage-crypto: the Cipher_block and Cipher_stream modules have been removed,
  its contents is inlined:
  Mirage_crypto.Cipher_block.S -> Mirage_crypto.Block
  Mirage_crypto.Cipher_stream.S -> Mirage_crypto.Stream
  Mirage_crypto.Cipher_block.AES.CTR -> Mirage_crypto.AES.CTR
  (mirage/mirage-crypto#225 @hannesm, suggested in mirage/mirage-crypto#224 by @reynir)
* mirage-crypto-pk: s-expression conversions for private and public keys (Dh,
  Dsa, Rsa) have been removed. You can use PKCS8 for encoding and decoding
  `X509.{Private,Public}_key.{en,de}code_{der,pem}` (mirage/mirage-crypto#208 @hannesm)
* mirage-crypto-pk: in the API, Cstruct.t is no longer present. Instead,
  string is used (mirage/mirage-crypto#211 @reynir @hannesm)
* mirage-crypto-rng: the API uses string instead of Cstruct.t. A new function
  `generate_into : ?g -> bytes -> ?off:int -> int -> unit` is provided
  (mirage/mirage-crypto#212 @hannesm @reynir)
* mirage-crypto-ec: remove NIST P224 support (mirage/mirage-crypto#209 @hannesm @Firobe)
* mirage-crypto: in Uncommon.xor_into the arguments ~src_off and ~dst_off are
  required now (mirage/mirage-crypto#232 @hannesm), renamed to unsafe_xor_into
  (98f01b14f5ebf98ba0e7e9c2ba97ec518f90fddc)
* mirage-crypto-pk, mirage-crypto-rng: remove type alias "type bits = int"
  (mirage/mirage-crypto#236 @hannesm)

### Bugfixes

* mirage-crypto (32 bit systems): CCM with long adata (mirage/mirage-crypto#207 @reynir)
* mirage-crypto-ec: fix K_gen for bitlen mod 8 != 0 (reported in mirage/mirage-crypto#105 that
  P521 test vectors don't pass, re-reported mirage/mirage-crypto#228, fixed mirage/mirage-crypto#230 @Firobe)
* mirage-crypto-ec: zero out bytes allocated for Field_element.zero (reported
  mirleft/ocaml-x509#167, fixed mirage/mirage-crypto#226 @dinosaure)

### Data race free

* mirage-crypto (3DES): avoid global state in key derivation (mirage/mirage-crypto#223 @hannesm)
* mirage-crypto-rng: use atomic instead of reference to be domain-safe (mirage/mirage-crypto#221
  @dinosaure @reynir @hannesm)
* mirage-crypto, mirage-crypto-rng, mirage-crypto-pk, mirage-crypto-ec:
  avoid global buffers, use freshly allocated strings/bytes instead, avoids
  data races (mirage/mirage-crypto#186 mirage/mirage-crypto#219 @dinosaure @reynir @hannesm)

### Other changes

* mirage-crypto: add {de,en}crypt_into functions (and unsafe variants) to allow
  less buffer allocations (mirage/mirage-crypto#231 @hannesm)
* mirage-crypto-rng-miou: new package which adds rng support with miou
  (mirage/mirage-crypto#227 @dinosaure)
* PERFORMANCE mirage-crypto: ChaCha20/Poly1305 use string instead of Cstruct.t,
  ChaCha20 interface unchanged, performance improvement roughly 2x
  (mirage/mirage-crypto#203 @hannesm @reynir)
* mirage-crypto-ec, mirage-crypto-pk, mirage-crypto-rng: use digestif for
  hashes (mirage/mirage-crypto#212 mirage/mirage-crypto#215 @reynir @hannesm)
* mirage-crypto-rng: use a set for entropy sources instead of a list
  (mirage/mirage-crypto#218 @hannesm)
* mirage-crypto-rng-mirage: provide a module type S (for use instead of
  mirage-random in mirage) (mirage/mirage-crypto#234 @hannesm)
avsm pushed a commit to avsm/opam-repository that referenced this pull request Sep 5, 2024
CHANGES:

### Breaking changes

* mirage-crypto: Poly1305 API now uses string (mirage/mirage-crypto#203 @hannesm)
* mirage-crypto: Poly1305 no longer has type alias "type mac = string"
  (mirage/mirage-crypto#232 @hannesm)
* mirage-crypto: the API uses string instead of cstruct (mirage/mirage-crypto#214 @reynir @hannesm)
* mirage-crypto: Hash module has been removed. Use digestif if you need hash
  functions (mirage/mirage-crypto#213 @hannesm)
* mirage-crypto: the Cipher_block and Cipher_stream modules have been removed,
  its contents is inlined:
  Mirage_crypto.Cipher_block.S -> Mirage_crypto.Block
  Mirage_crypto.Cipher_stream.S -> Mirage_crypto.Stream
  Mirage_crypto.Cipher_block.AES.CTR -> Mirage_crypto.AES.CTR
  (mirage/mirage-crypto#225 @hannesm, suggested in mirage/mirage-crypto#224 by @reynir)
* mirage-crypto-pk: s-expression conversions for private and public keys (Dh,
  Dsa, Rsa) have been removed. You can use PKCS8 for encoding and decoding
  `X509.{Private,Public}_key.{en,de}code_{der,pem}` (mirage/mirage-crypto#208 @hannesm)
* mirage-crypto-pk: in the API, Cstruct.t is no longer present. Instead,
  string is used (mirage/mirage-crypto#211 @reynir @hannesm)
* mirage-crypto-rng: the API uses string instead of Cstruct.t. A new function
  `generate_into : ?g -> bytes -> ?off:int -> int -> unit` is provided
  (mirage/mirage-crypto#212 @hannesm @reynir)
* mirage-crypto-ec: remove NIST P224 support (mirage/mirage-crypto#209 @hannesm @Firobe)
* mirage-crypto: in Uncommon.xor_into the arguments ~src_off and ~dst_off are
  required now (mirage/mirage-crypto#232 @hannesm), renamed to unsafe_xor_into
  (98f01b14f5ebf98ba0e7e9c2ba97ec518f90fddc)
* mirage-crypto-pk, mirage-crypto-rng: remove type alias "type bits = int"
  (mirage/mirage-crypto#236 @hannesm)

### Bugfixes

* mirage-crypto (32 bit systems): CCM with long adata (mirage/mirage-crypto#207 @reynir)
* mirage-crypto-ec: fix K_gen for bitlen mod 8 != 0 (reported in mirage/mirage-crypto#105 that
  P521 test vectors don't pass, re-reported mirage/mirage-crypto#228, fixed mirage/mirage-crypto#230 @Firobe)
* mirage-crypto-ec: zero out bytes allocated for Field_element.zero (reported
  mirleft/ocaml-x509#167, fixed mirage/mirage-crypto#226 @dinosaure)

### Data race free

* mirage-crypto (3DES): avoid global state in key derivation (mirage/mirage-crypto#223 @hannesm)
* mirage-crypto-rng: use atomic instead of reference to be domain-safe (mirage/mirage-crypto#221
  @dinosaure @reynir @hannesm)
* mirage-crypto, mirage-crypto-rng, mirage-crypto-pk, mirage-crypto-ec:
  avoid global buffers, use freshly allocated strings/bytes instead, avoids
  data races (mirage/mirage-crypto#186 mirage/mirage-crypto#219 @dinosaure @reynir @hannesm)

### Other changes

* mirage-crypto: add {de,en}crypt_into functions (and unsafe variants) to allow
  less buffer allocations (mirage/mirage-crypto#231 @hannesm)
* mirage-crypto-rng-miou: new package which adds rng support with miou
  (mirage/mirage-crypto#227 @dinosaure)
* PERFORMANCE mirage-crypto: ChaCha20/Poly1305 use string instead of Cstruct.t,
  ChaCha20 interface unchanged, performance improvement roughly 2x
  (mirage/mirage-crypto#203 @hannesm @reynir)
* mirage-crypto-ec, mirage-crypto-pk, mirage-crypto-rng: use digestif for
  hashes (mirage/mirage-crypto#212 mirage/mirage-crypto#215 @reynir @hannesm)
* mirage-crypto-rng: use a set for entropy sources instead of a list
  (mirage/mirage-crypto#218 @hannesm)
* mirage-crypto-rng-mirage: provide a module type S (for use instead of
  mirage-random in mirage) (mirage/mirage-crypto#234 @hannesm)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants