Skip to content

Commit 1a59d8b

Browse files
committed
mirage-crypto-ec: add Brainpool curves with 254/384/512 bits
This change adds the Brainpool curves, namely brainpoolP254r1, brainpoolP384r1, brainpoolP512r1. The implementation internally uses the twisted versions of the curves (...t1) in order to use the same a=-3 EC arithmetic as the NIST curves. Point coordinates in regular form are transformed to twisted form before calculations, and vice versa afterwards.
1 parent 3495beb commit 1a59d8b

39 files changed

+259689
-10
lines changed

bench/speed.ml

+46
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ let msg_str = String.make 100 '\xAA'
7676

7777
let msg_str_32 = String.sub msg_str 0 32
7878
let msg_str_48 = String.sub msg_str 0 48
79+
let msg_str_64 = String.sub msg_str 0 64
7980
let msg_str_65 = String.sub msg_str 0 65
8081

8182
module PSS = Mirage_crypto_pk.Rsa.PSS(Digestif.SHA256)
@@ -214,6 +215,27 @@ let ecdsa_p521 =
214215

215216
let ecdsa_p521_sig () = Mirage_crypto_ec.P521.Dsa.sign ~key:ecdsa_p521 msg_str_65
216217

218+
let ecdsa_brainpoolp256 =
219+
Result.get_ok
220+
(Mirage_crypto_ec.BrainpoolP256.Dsa.priv_of_octets
221+
"\x08\x9f\x4f\xfc\xcc\xf9\xba\x13\xfe\xdd\x09\x42\xef\x08\xcf\x2d\x90\x9f\x32\xe2\x93\x4a\xb5\xc9\x3b\x6c\x99\xbe\x5a\x9f\xf5\x27")
222+
223+
let ecdsa_brainpoolp256_sig () = Mirage_crypto_ec.BrainpoolP256.Dsa.sign ~key:ecdsa_brainpoolp256 msg_str_32
224+
225+
let ecdsa_brainpoolp384 =
226+
Result.get_ok
227+
(Mirage_crypto_ec.BrainpoolP384.Dsa.priv_of_octets
228+
"\x24\xdd\xf0\xfb\xb4\x1c\x28\x36\x5d\x30\x2d\xd9\xd2\x6f\xf9\xc3\x2c\x76\xc8\x5f\xa8\xb9\x13\x8a\x3e\xc6\x21\xd0\xca\xff\x6d\xe8\xa7\x24\xb4\x5d\x6f\xe0\xd9\x18\x00\x44\x24\x2b\x9f\x41\xc8\x4b")
229+
230+
let ecdsa_brainpoolp384_sig () = Mirage_crypto_ec.BrainpoolP384.Dsa.sign ~key:ecdsa_brainpoolp384 msg_str_48
231+
232+
let ecdsa_brainpoolp512 =
233+
Result.get_ok
234+
(Mirage_crypto_ec.BrainpoolP512.Dsa.priv_of_octets
235+
"\x62\x80\xeb\x95\x40\x5f\xa8\xc0\xe9\xd9\x70\x54\x73\x01\xbb\xef\xb1\x52\xc8\xc8\x11\x4a\xbc\x73\x0c\x89\xbf\x6d\xb3\xf7\xd9\x49\xfc\xfd\x7e\xbb\x82\xfd\x2d\xbd\x43\xd2\x8d\x47\xbf\x4e\xd9\x5d\xe9\x7b\xae\xd1\x9f\x7d\x08\x7c\xf3\x03\xd2\xb0\xcd\x41\x37\x67")
236+
237+
let ecdsa_brainpoolp512_sig () = Mirage_crypto_ec.BrainpoolP512.Dsa.sign ~key:ecdsa_brainpoolp512 msg_str_64
238+
217239
let ed25519 =
218240
Result.get_ok (Mirage_crypto_ec.Ed25519.priv_of_octets
219241
"\x3e\x0a\xb6\x82\x17\x12\x75\xc5\x69\xfc\xe9\xca\x8b\xcc\xd2\xd2\x77\x14\x54\xa2\x30\x0c\x35\x29\xf7\xa4\xd8\x0b\x84\x38\x83\xbc")
@@ -225,6 +247,9 @@ let ecdsas = [
225247
("P256k1", `P256k1 (ecdsa_p256k1, ecdsa_p256k1_sig ()));
226248
("P384", `P384 (ecdsa_p384, ecdsa_p384_sig ()));
227249
("P521", `P521 (ecdsa_p521, ecdsa_p521_sig ()));
250+
("BrainpoolP256", `BrainpoolP256 (ecdsa_brainpoolp256, ecdsa_brainpoolp256_sig ()));
251+
("BrainpoolP384", `BrainpoolP384 (ecdsa_brainpoolp384, ecdsa_brainpoolp384_sig ()));
252+
("BrainpoolP512", `BrainpoolP512 (ecdsa_brainpoolp512, ecdsa_brainpoolp512_sig ()));
228253
("Ed25519", `Ed25519 (ed25519, ed25519_sig ()));
229254
]
230255

@@ -238,6 +263,12 @@ let ecdh_shares =
238263
"\x04\x04\x89\xcf\x24\xbc\x80\xbf\x89\xfd\xfe\x9c\x05\xec\xc3\x9f\x69\x16\xad\x45\x09\xd9\x39\x85\x97\x95\x0d\x3d\x24\xe8\x28\xf6\xbf\x56\xba\x4a\xd6\xd2\x1e\xd7\x86\x3b\xed\x68\xe4\x13\x36\x4b\xd4\xc7\xb1\xe9\x04\x7d\x36\x12\x4c\x69\x53\xbe\x7c\x61\x20\x9c\xb3\xfc\x56\x45\x2f\x73\x05\x29\x37\x83\xc7\xc0\xed\x92\x9d\x6c\x98\xc7\xbc\x97\xf6\x0a\x72\xed\x22\x69\xa8\xeb\x19\xbb\x7e\xe1\x31"));
239264
("P521", `P521 (Mirage_crypto_ec.P521.Dh.secret_of_octets "\x00\xaa\x47\x0b\xa1\xcc\x84\x3b\xa3\x14\x82\x1e\x72\xde\x4c\xd2\x99\xae\xc1\xf2\x6e\x9d\x64\xa0\xd8\x7d\xb1\x8a\x3d\xa9\xf6\x5c\x45\xec\xfc\xc5\x61\x7f\xf0\xd7\x3b\x2e\x0e\x1c\xdf\xf8\x04\x8e\x01\xbe\x5e\x20\x14\x94\x12\xe7\xdb\xfa\xb7\xfe\xae\x24\x9b\x1b\xfa\x4d" |> Result.get_ok |> fst,
240265
"\x04\x00\x1d\x16\x29\xee\xb1\xc4\x25\xf9\x04\xd7\x55\x33\x00\x79\xd1\x3c\x77\xda\x92\x1e\x01\xcf\x50\xd7\x17\xe0\xd6\x85\x0a\x81\xa3\x90\x2b\xb9\x2a\x03\xfa\xea\xcb\xd6\x28\x9c\x15\x90\x68\x5a\x60\x44\xb5\xe9\x4d\xcf\xc4\x1d\xeb\x6a\x88\xdb\x62\xa8\x91\xb0\xb8\x93\xbb\x00\xe4\x2a\x66\xb2\xf0\x13\xbd\xd0\xd2\x7d\x8e\x07\xcb\x35\xfc\x3e\x2c\x2b\x22\xf9\x3e\xcf\xd5\xea\xb7\x88\x61\x97\xca\x07\x3c\x2c\x5e\x68\x31\xd6\x5e\x2d\x0b\x8a\xa4\x08\x43\x8e\x49\x54\x2f\x05\xf4\x1c\x57\x6d\xf7\x0e\x3c\xaf\x5b\xb8\x22\x7d\x48\x30\x94\xae\x58"));
266+
("BrainpoolP256", `BrainpoolP256 (Mirage_crypto_ec.BrainpoolP256.Dh.secret_of_octets "\x47\x0d\x57\x70\x6c\x77\x06\xb6\x8a\x3f\x42\x3a\xea\xf4\xff\x7f\xdd\x02\x49\x4a\x10\xd3\xe3\x81\xc3\xc1\x1f\x72\x76\x80\x2c\xdc" |> Result.get_ok |> fst,
267+
"\x04\x4c\xee\x5e\x10\x72\xb3\x0d\x64\xf7\x0b\xf0\x19\x58\xe2\x2c\x04\x4a\x21\x27\xdd\xd7\x44\xce\x30\x60\xc1\x59\x90\xff\x0f\xe1\x14\x8c\x6e\xe5\x65\x59\x82\x9a\x5a\x84\xdd\x5c\x86\x46\xee\x0c\x43\xd0\xb7\xc5\x01\x81\xf2\x34\xec\x09\xeb\xa4\x3b\xc8\x6b\x16\x9e"));
268+
("BrainpoolP384", `BrainpoolP384 (Mirage_crypto_ec.BrainpoolP384.Dh.secret_of_octets "\x24\xdd\xf0\xfb\xb4\x1c\x28\x36\x5d\x30\x2d\xd9\xd2\x6f\xf9\xc3\x2c\x76\xc8\x5f\xa8\xb9\x13\x8a\x3e\xc6\x21\xd0\xca\xff\x6d\xe8\xa7\x24\xb4\x5d\x6f\xe0\xd9\x18\x00\x44\x24\x2b\x9f\x41\xc8\x4b" |> Result.get_ok |> fst,
269+
"\x04\x70\xff\xb3\x50\x17\x32\x56\xeb\x43\x7b\x14\x03\x65\x84\x23\x97\xeb\xaf\x36\x11\xb6\x38\x95\x96\xc1\xf1\x7c\x5f\xf5\xce\x52\x01\xf2\x4f\x69\x85\xb8\xfe\x08\x90\xdc\xae\x54\xb2\x60\x3d\xfb\x40\x87\x24\x2a\xaf\x7b\x2d\x95\xb3\x19\x9b\xfa\x03\xe8\xfc\xe5\x4e\xf6\x80\xb5\x71\x09\x84\x72\x74\xdb\x3b\x3a\x65\x51\x2b\x7f\x83\x22\x48\xe7\x0b\x10\x30\xe9\x5d\xb6\x42\x95\x89\x77\x5c\x33\x46"));
270+
("BrainpoolP512", `BrainpoolP512 (Mirage_crypto_ec.BrainpoolP512.Dh.secret_of_octets "\x62\x80\xeb\x95\x40\x5f\xa8\xc0\xe9\xd9\x70\x54\x73\x01\xbb\xef\xb1\x52\xc8\xc8\x11\x4a\xbc\x73\x0c\x89\xbf\x6d\xb3\xf7\xd9\x49\xfc\xfd\x7e\xbb\x82\xfd\x2d\xbd\x43\xd2\x8d\x47\xbf\x4e\xd9\x5d\xe9\x7b\xae\xd1\x9f\x7d\x08\x7c\xf3\x03\xd2\xb0\xcd\x41\x37\x67" |> Result.get_ok |> fst,
271+
"\x04\x8a\x73\xa6\x66\x05\xa5\xdb\x25\x2e\xf4\x18\xff\x2c\x43\x96\x9b\xd4\x12\x81\x87\xce\x43\x1c\x36\xa3\x3d\x3f\xf3\x03\x4c\xf8\x91\x0f\xb0\x02\x1c\xe8\x49\x72\x36\x21\x19\x9d\x0d\x7e\xa4\x80\x5f\x3c\xda\xb8\x2f\x6c\x90\x92\x57\x76\x2d\xa2\xa9\x7e\x26\x30\x5b\x07\x8c\x1f\xd7\x91\xfa\x95\x7e\x97\x5e\x30\xdf\x5b\x87\x60\x54\x75\x82\x67\x12\x9e\x49\x74\xa0\x83\x37\x2b\x0c\xe0\x71\x18\x0d\x05\xe1\x97\x8b\xd9\x0b\x84\x07\xc0\xa7\xff\x7f\x66\x51\xbd\x3f\xfc\xf1\xa5\x74\xdf\xe9\x5a\x2e\x8a\xf3\x86\x6c\xbb\x38\x5d\x21"));
241272
("X25519", `X25519 (Mirage_crypto_ec.X25519.secret_of_octets "\x4c\x6d\xb7\xcf\x93\x5b\xcf\x84\x02\x61\x78\xd4\x0c\x95\x6a\xf0\x9d\x8e\x36\x32\x03\x49\x0d\x2c\x41\x62\x5a\xcb\x68\xb9\x31\xa4" |> Result.get_ok |> fst,
242273
"\xca\x19\x19\x3c\xf5\xc0\xb3\x8c\x61\xaa\x01\xc1\x72\xb2\xe9\x3d\x16\xf7\x50\xd0\x84\x62\x77\xad\x32\x2d\xe5\xe4\xfb\x33\x24\x29"));
243274
]
@@ -316,6 +347,9 @@ let benchmarks = [
316347
| `P256k1 _ -> P256k1.Dsa.generate () |> ignore
317348
| `P384 _ -> P384.Dsa.generate () |> ignore
318349
| `P521 _ -> P521.Dsa.generate () |> ignore
350+
| `BrainpoolP256 _ -> BrainpoolP256.Dsa.generate () |> ignore
351+
| `BrainpoolP384 _ -> BrainpoolP384.Dsa.generate () |> ignore
352+
| `BrainpoolP512 _ -> BrainpoolP512.Dsa.generate () |> ignore
319353
| `Ed25519 _ -> Ed25519.generate () |> ignore
320354
)
321355
fst ecdsas);
@@ -327,6 +361,9 @@ let benchmarks = [
327361
| `P256k1 (key, _) -> P256k1.Dsa.sign ~key msg_str_32
328362
| `P384 (key, _) -> P384.Dsa.sign ~key msg_str_48
329363
| `P521 (key, _) -> P521.Dsa.sign ~key msg_str_65
364+
| `BrainpoolP256 (key, _) -> BrainpoolP256.Dsa.sign ~key msg_str_32
365+
| `BrainpoolP384 (key, _) -> BrainpoolP384.Dsa.sign ~key msg_str_48
366+
| `BrainpoolP512 (key, _) -> BrainpoolP512.Dsa.sign ~key msg_str_64
330367
| `Ed25519 (key, _) -> Ed25519.sign ~key msg_str, ""
331368
)
332369
fst ecdsas);
@@ -338,6 +375,9 @@ let benchmarks = [
338375
| `P256k1 (key, signature) -> P256k1.Dsa.(verify ~key:(pub_of_priv key) signature msg_str_32)
339376
| `P384 (key, signature) -> P384.Dsa.(verify ~key:(pub_of_priv key) signature msg_str_48)
340377
| `P521 (key, signature) -> P521.Dsa.(verify ~key:(pub_of_priv key) signature msg_str_65)
378+
| `BrainpoolP256 (key, signature) -> BrainpoolP256.Dsa.(verify ~key:(pub_of_priv key) signature msg_str_32)
379+
| `BrainpoolP384 (key, signature) -> BrainpoolP384.Dsa.(verify ~key:(pub_of_priv key) signature msg_str_48)
380+
| `BrainpoolP512 (key, signature) -> BrainpoolP512.Dsa.(verify ~key:(pub_of_priv key) signature msg_str_64)
341381
| `Ed25519 (key, signature) -> Ed25519.(verify ~key:(pub_of_priv key) signature ~msg:msg_str)
342382
) fst ecdsas);
343383

@@ -357,6 +397,9 @@ let benchmarks = [
357397
| `P256k1 _ -> P256k1.Dh.gen_key () |> ignore
358398
| `P384 _ -> P384.Dh.gen_key () |> ignore
359399
| `P521 _ -> P521.Dh.gen_key () |> ignore
400+
| `BrainpoolP256 _ -> BrainpoolP256.Dh.gen_key () |> ignore
401+
| `BrainpoolP384 _ -> BrainpoolP384.Dh.gen_key () |> ignore
402+
| `BrainpoolP512 _ -> BrainpoolP512.Dh.gen_key () |> ignore
360403
| `X25519 _ -> X25519.gen_key () |> ignore)
361404
fst ecdh_shares);
362405

@@ -367,6 +410,9 @@ let benchmarks = [
367410
| `P256k1 (sec, share) -> P256k1.Dh.key_exchange sec share |> Result.get_ok |> ignore
368411
| `P384 (sec, share) -> P384.Dh.key_exchange sec share |> Result.get_ok |> ignore
369412
| `P521 (sec, share) -> P521.Dh.key_exchange sec share |> Result.get_ok |> ignore
413+
| `BrainpoolP256 (sec, share) -> BrainpoolP256.Dh.key_exchange sec share |> Result.get_ok |> ignore
414+
| `BrainpoolP384 (sec, share) -> BrainpoolP384.Dh.key_exchange sec share |> Result.get_ok |> ignore
415+
| `BrainpoolP512 (sec, share) -> BrainpoolP512.Dh.key_exchange sec share |> Result.get_ok |> ignore
370416
| `X25519 (sec, share) -> X25519.key_exchange sec share |> Result.get_ok |> ignore)
371417
fst ecdh_shares);
372418

ec/dune

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
(foreign_stubs
66
(language c)
77
(names p256_stubs np256_stubs p384_stubs np384_stubs p521_stubs np521_stubs
8-
curve25519_stubs secp256k1_stubs)
8+
curve25519_stubs secp256k1_stubs brainpoolp256_stubs nbrainpoolp256_stubs
9+
brainpoolp384_stubs nbrainpoolp384_stubs brainpoolp512_stubs nbrainpoolp512_stubs)
910
(include_dirs ../src/native)
1011
(flags
1112
(:standard -DNDEBUG)

ec/gen_tables/gen_tables.ml

+3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ let curves =
8989
("p256", (module P256 : Dh_dsa));
9090
("p384", (module P384));
9191
("p521", (module P521));
92+
("brainpoolp256", (module BrainpoolP256));
93+
("brainpoolp384", (module BrainpoolP384));
94+
("brainpoolp512", (module BrainpoolP512));
9295
]
9396

9497
let usage () =

0 commit comments

Comments
 (0)