| 1 | // vtest build: present_openssl? && !(openbsd && gcc) && !(sanitize-memory-clang || docker-ubuntu-musl) |
| 2 | import rand |
| 3 | import crypto.ecdsa |
| 4 | import encoding.hex |
| 5 | // The test file placed on its own directory. Its for workaround for |
| 6 | // module lookup problem, because there are two rand module availables, |
| 7 | // between `crypto.rand` and `rand` module. |
| 8 | // See [the talk](https://discord.com/channels/592103645835821068/592294828432424960/1328198034806407311) on discord. |
| 9 | |
| 10 | fn test_new_key_from_seed_with_random_size_and_data() ! { |
| 11 | num_iters := 100 |
| 12 | // default prime256v1 curve key size was 32 bytes. |
| 13 | max_key_size := i32(48) |
| 14 | for i := 0; i <= num_iters; i++ { |
| 15 | m := rand.i32n(max_key_size)! |
| 16 | random_bytes := rand.bytes(m)! |
| 17 | pvkey := ecdsa.new_key_from_seed(random_bytes) or { |
| 18 | // With default size, would error on m > 32 or m == 0 |
| 19 | // dump(m) |
| 20 | if m == 0 { |
| 21 | assert err == error('Seed with null-length was not allowed') |
| 22 | } else if m > 32 { |
| 23 | assert err == error('Seed length exceeds key size') |
| 24 | } else { |
| 25 | assert err == error('EC_KEY_check_key failed') |
| 26 | } |
| 27 | continue |
| 28 | } |
| 29 | ret_seed := pvkey.bytes()! |
| 30 | assert random_bytes == ret_seed |
| 31 | pvkey.free() |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | fn test_private_and_public_key_from_string() ! { |
| 36 | // See [this](https://github.com/vlang/v/blob/master/vlib/crypto/ecdsa/util_test.v) for detail |
| 37 | // of material used as a sample. |
| 38 | privkey_sample := '-----BEGIN PRIVATE KEY----- |
| 39 | MIG2AgEAMBAGByqGSM49AgEGBSuBBAAiBIGeMIGbAgEBBDAwzj2iiJZaxgk/C6mp |
| 40 | oVskdr6j7akl4bPB8JRnT1J5XNbLPK/iNd/BW+xUJEj/pxWhZANiAAT4/euEWRPV |
| 41 | 9cdhtjcKlwF2HrFMLvgxAXFx+01UPfMQ9XOj/85qUhVq1jXraSyDy5FYF28UW4dn |
| 42 | 04xVeRuPBbCFxc/uqYj2s5ItHcAZSV3L5sGlXadPfTqoIjCBQAx44k8= |
| 43 | -----END PRIVATE KEY-----' |
| 44 | |
| 45 | pubkey_sample := '-----BEGIN PUBLIC KEY----- |
| 46 | MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE+P3rhFkT1fXHYbY3CpcBdh6xTC74MQFx |
| 47 | cftNVD3zEPVzo//OalIVatY162ksg8uRWBdvFFuHZ9OMVXkbjwWwhcXP7qmI9rOS |
| 48 | LR3AGUldy+bBpV2nT306qCIwgUAMeOJP |
| 49 | -----END PUBLIC KEY-----' |
| 50 | |
| 51 | // Message tobe signed and verified |
| 52 | message_tobe_signed := 'Example of ECDSA with P-384'.bytes() |
| 53 | // Message signature generated with SHA384 digest with associated key previously. |
| 54 | signature := |
| 55 | hex.decode('3066023100b08f6ec77bb319fdb7bce55a2714d7e79cc645d834ee539d8903cfcc88c6fa90df1558856cb840b2dd82e82cd89d7046023100d9d482ca8a6545a3b081fbdd4bb9643a2b4eda4e21fd624833216596032471faae646891f8d2f0bbb86b796c36d3c390')! |
| 56 | |
| 57 | // loads a Privatekey and PublicKey from above sample |
| 58 | privkey := ecdsa.privkey_from_string(privkey_sample)! |
| 59 | pubkey := ecdsa.pubkey_from_string(pubkey_sample)! |
| 60 | // get a public key from private key |
| 61 | pbkey_from_privkey := privkey.public_key()! |
| 62 | |
| 63 | // two public key should be equal, its comes from the same source. |
| 64 | assert pubkey.equal(pbkey_from_privkey) |
| 65 | |
| 66 | // lets create the signature |
| 67 | created_signature := privkey.sign(message_tobe_signed)! |
| 68 | |
| 69 | verified1 := pubkey.verify(message_tobe_signed, signature)! |
| 70 | verified2 := pubkey.verify(message_tobe_signed, created_signature)! |
| 71 | |
| 72 | assert verified1 == true |
| 73 | assert verified2 == true |
| 74 | |
| 75 | // Its also should be verified with pbkey_from_privkey opaque |
| 76 | verified3 := pbkey_from_privkey.verify(message_tobe_signed, signature)! |
| 77 | verified4 := pbkey_from_privkey.verify(message_tobe_signed, created_signature)! |
| 78 | assert verified3 == true |
| 79 | assert verified4 == true |
| 80 | |
| 81 | // release the key |
| 82 | privkey.free() |
| 83 | pubkey.free() |
| 84 | pbkey_from_privkey.free() |
| 85 | } |
| 86 | |