ed25519 1.0.0-jruby

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.rspec +5 -0
  4. data/.rubocop.yml +35 -0
  5. data/.travis.yml +13 -0
  6. data/CHANGES.md +16 -0
  7. data/CODE_OF_CONDUCT.md +74 -0
  8. data/Gemfile +12 -0
  9. data/LICENSE +22 -0
  10. data/README.md +159 -0
  11. data/Rakefile +27 -0
  12. data/ed25519.gemspec +32 -0
  13. data/ed25519.png +0 -0
  14. data/ext/ed25519_java/org/cryptosphere/ed25519.java +228 -0
  15. data/ext/ed25519_ref10/api.h +4 -0
  16. data/ext/ed25519_ref10/base.h +1344 -0
  17. data/ext/ed25519_ref10/base2.h +40 -0
  18. data/ext/ed25519_ref10/d.h +1 -0
  19. data/ext/ed25519_ref10/d2.h +1 -0
  20. data/ext/ed25519_ref10/ed25519_ref10.c +99 -0
  21. data/ext/ed25519_ref10/ed25519_ref10.h +33 -0
  22. data/ext/ed25519_ref10/extconf.rb +9 -0
  23. data/ext/ed25519_ref10/fe.h +56 -0
  24. data/ext/ed25519_ref10/fe_0.c +19 -0
  25. data/ext/ed25519_ref10/fe_1.c +19 -0
  26. data/ext/ed25519_ref10/fe_add.c +57 -0
  27. data/ext/ed25519_ref10/fe_cmov.c +63 -0
  28. data/ext/ed25519_ref10/fe_copy.c +29 -0
  29. data/ext/ed25519_ref10/fe_frombytes.c +71 -0
  30. data/ext/ed25519_ref10/fe_invert.c +14 -0
  31. data/ext/ed25519_ref10/fe_isnegative.c +16 -0
  32. data/ext/ed25519_ref10/fe_isnonzero.c +19 -0
  33. data/ext/ed25519_ref10/fe_mul.c +252 -0
  34. data/ext/ed25519_ref10/fe_neg.c +45 -0
  35. data/ext/ed25519_ref10/fe_pow22523.c +13 -0
  36. data/ext/ed25519_ref10/fe_sq.c +148 -0
  37. data/ext/ed25519_ref10/fe_sq2.c +159 -0
  38. data/ext/ed25519_ref10/fe_sub.c +57 -0
  39. data/ext/ed25519_ref10/fe_tobytes.c +119 -0
  40. data/ext/ed25519_ref10/ge.h +95 -0
  41. data/ext/ed25519_ref10/ge_add.c +11 -0
  42. data/ext/ed25519_ref10/ge_add.h +97 -0
  43. data/ext/ed25519_ref10/ge_double_scalarmult.c +96 -0
  44. data/ext/ed25519_ref10/ge_frombytes.c +50 -0
  45. data/ext/ed25519_ref10/ge_madd.c +11 -0
  46. data/ext/ed25519_ref10/ge_madd.h +88 -0
  47. data/ext/ed25519_ref10/ge_msub.c +11 -0
  48. data/ext/ed25519_ref10/ge_msub.h +88 -0
  49. data/ext/ed25519_ref10/ge_p1p1_to_p2.c +12 -0
  50. data/ext/ed25519_ref10/ge_p1p1_to_p3.c +13 -0
  51. data/ext/ed25519_ref10/ge_p2_0.c +8 -0
  52. data/ext/ed25519_ref10/ge_p2_dbl.c +11 -0
  53. data/ext/ed25519_ref10/ge_p2_dbl.h +73 -0
  54. data/ext/ed25519_ref10/ge_p3_0.c +9 -0
  55. data/ext/ed25519_ref10/ge_p3_dbl.c +12 -0
  56. data/ext/ed25519_ref10/ge_p3_to_cached.c +17 -0
  57. data/ext/ed25519_ref10/ge_p3_to_p2.c +12 -0
  58. data/ext/ed25519_ref10/ge_p3_tobytes.c +14 -0
  59. data/ext/ed25519_ref10/ge_precomp_0.c +8 -0
  60. data/ext/ed25519_ref10/ge_scalarmult_base.c +104 -0
  61. data/ext/ed25519_ref10/ge_sub.c +11 -0
  62. data/ext/ed25519_ref10/ge_sub.h +97 -0
  63. data/ext/ed25519_ref10/ge_tobytes.c +14 -0
  64. data/ext/ed25519_ref10/keypair.c +22 -0
  65. data/ext/ed25519_ref10/open.c +47 -0
  66. data/ext/ed25519_ref10/pow22523.h +160 -0
  67. data/ext/ed25519_ref10/pow225521.h +160 -0
  68. data/ext/ed25519_ref10/sc.h +17 -0
  69. data/ext/ed25519_ref10/sc_muladd.c +366 -0
  70. data/ext/ed25519_ref10/sc_reduce.c +272 -0
  71. data/ext/ed25519_ref10/sha512.c +304 -0
  72. data/ext/ed25519_ref10/sha512.h +8 -0
  73. data/ext/ed25519_ref10/sign.c +41 -0
  74. data/ext/ed25519_ref10/sqrtm1.h +1 -0
  75. data/ext/ed25519_ref10/verify.c +40 -0
  76. data/lib/ed25519.rb +65 -0
  77. data/lib/ed25519/provider/jruby.rb +39 -0
  78. data/lib/ed25519/signing_key.rb +39 -0
  79. data/lib/ed25519/verify_key.rb +44 -0
  80. data/lib/ed25519/version.rb +5 -0
  81. data/lib/ed25519_java.jar +0 -0
  82. metadata +138 -0
@@ -0,0 +1,8 @@
1
+ #ifndef SHA512_H
2
+ #define SHA512_H
3
+
4
+ #include <stdint.h>
5
+
6
+ int crypto_hash_sha512(uint8_t *out,const uint8_t *in,uint64_t inlen);
7
+
8
+ #endif /* SHA512_H */
@@ -0,0 +1,41 @@
1
+ #include <string.h>
2
+ #include "ed25519_ref10.h"
3
+ #include "sha512.h"
4
+ #include "ge.h"
5
+ #include "sc.h"
6
+
7
+ int crypto_sign_ed25519_ref10(
8
+ uint8_t *sm, uint64_t *smlen,
9
+ const uint8_t *m, uint64_t mlen,
10
+ const uint8_t *sk
11
+ )
12
+ {
13
+ unsigned char pk[32];
14
+ unsigned char az[64];
15
+ unsigned char nonce[64];
16
+ unsigned char hram[64];
17
+ ge_p3 R;
18
+
19
+ memmove(pk,sk + 32,32);
20
+
21
+ crypto_hash_sha512(az,sk,32);
22
+ az[0] &= 248;
23
+ az[31] &= 63;
24
+ az[31] |= 64;
25
+
26
+ *smlen = mlen + 64;
27
+ memmove(sm + 64,m,mlen);
28
+ memmove(sm + 32,az + 32,32);
29
+ crypto_hash_sha512(nonce,sm + 32,mlen + 32);
30
+ memmove(sm + 32,pk,32);
31
+
32
+ sc_reduce(nonce);
33
+ ge_scalarmult_base(&R,nonce);
34
+ ge_p3_tobytes(sm,&R);
35
+
36
+ crypto_hash_sha512(hram,sm,mlen + 64);
37
+ sc_reduce(hram);
38
+ sc_muladd(sm + 32,hram,az,nonce);
39
+
40
+ return 0;
41
+ }
@@ -0,0 +1 @@
1
+ -32595792,-7943725,9377950,3500415,12389472,-272473,-25146209,-2005654,326686,11406482
@@ -0,0 +1,40 @@
1
+ #include "ed25519_ref10.h"
2
+
3
+ int crypto_verify_32(const uint8_t *x,const uint8_t *y)
4
+ {
5
+ unsigned int differentbits = 0;
6
+ #define F(i) differentbits |= x[i] ^ y[i];
7
+ F(0)
8
+ F(1)
9
+ F(2)
10
+ F(3)
11
+ F(4)
12
+ F(5)
13
+ F(6)
14
+ F(7)
15
+ F(8)
16
+ F(9)
17
+ F(10)
18
+ F(11)
19
+ F(12)
20
+ F(13)
21
+ F(14)
22
+ F(15)
23
+ F(16)
24
+ F(17)
25
+ F(18)
26
+ F(19)
27
+ F(20)
28
+ F(21)
29
+ F(22)
30
+ F(23)
31
+ F(24)
32
+ F(25)
33
+ F(26)
34
+ F(27)
35
+ F(28)
36
+ F(29)
37
+ F(30)
38
+ F(31)
39
+ return (1 & ((differentbits - 1) >> 8)) - 1;
40
+ }
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ed25519/version"
4
+ require "ed25519/signing_key"
5
+ require "ed25519/verify_key"
6
+
7
+ # The Ed25519 digital signatre algorithm
8
+ # rubocop:disable Metrics/LineLength
9
+ module Ed25519
10
+ module_function
11
+
12
+ # Size of an Ed25519 key (public or private) in bytes
13
+ KEY_SIZE = 32
14
+
15
+ # Size of an Ed25519 signature in bytes
16
+ SIGNATURE_SIZE = 64
17
+
18
+ # Raised when a signature fails to verify
19
+ VerifyError = Class.new(StandardError)
20
+
21
+ # Raised when the built-in self-test fails
22
+ SelfTestFailure = Class.new(StandardError)
23
+
24
+ # Select the Ed25519::Provider to use based on the current environment
25
+ if defined? JRUBY_VERSION
26
+ require "ed25519/provider/jruby"
27
+ @provider = Ed25519::Provider::JRuby
28
+ else
29
+ require "ed25519_ref10"
30
+ @provider = Ed25519::Provider::Ref10
31
+ end
32
+
33
+ # Selected provider based on the logic above
34
+ def provider
35
+ @provider
36
+ end
37
+
38
+ # Perform a self-test to ensure the selected provider is working
39
+ def self_test
40
+ signature_key = Ed25519::SigningKey.new("A" * 32)
41
+ raise SelfTestFailure, "failed to generate verify key correctly" unless signature_key.verify_key.to_bytes.unpack("H*").first == "db995fe25169d141cab9bbba92baa01f9f2e1ece7df4cb2ac05190f37fcc1f9d"
42
+
43
+ message = "crypto libraries should self-test on boot"
44
+ signature = signature_key.sign(message)
45
+ raise SelfTestFailure, "failed to generate correct signature" unless signature.unpack("H*").first == "c62c12a3a6cbfa04800d4be81468ef8aecd152a6a26a81d91257baecef13ba209531fe905a843e833c8b71cee04400fa2af3a29fef1152ece470421848758d0a"
46
+
47
+ verify_key = signature_key.verify_key
48
+ raise SelfTestFailure, "failed to verify a valid signature" unless verify_key.verify(signature, message)
49
+
50
+ bad_signature = signature[0...63] + "X"
51
+ ex = nil
52
+
53
+ # rubocop:disable Lint/HandleExceptions
54
+ begin
55
+ verify_key.verify(bad_signature, message)
56
+ rescue Ed25519::VerifyError => ex
57
+ end
58
+ # rubocop:enable Lint/HandleExceptions
59
+
60
+ raise SelfTestFailure, "failed to detect an invalid signature" unless ex.is_a?(Ed25519::VerifyError)
61
+ end
62
+ end
63
+
64
+ # Automatically run self-test when library loads
65
+ Ed25519.self_test
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "java"
4
+ require "ed25519_java"
5
+
6
+ module Ed25519
7
+ module Provider
8
+ # Binding between the JRuby extension and the Ed25519::Provider API
9
+ #
10
+ # TODO: implement the Ed25519::Provider API natively in the Java extension
11
+ module JRuby
12
+ module_function
13
+
14
+ def create_keypair(seed)
15
+ raise ArgumentError, "seed must be #{KEY_SIZE}-bytes long" unless seed.length == Ed25519::KEY_SIZE
16
+
17
+ verify_key = org.cryptosphere.ed25519.publickey(seed.to_java_bytes)
18
+ verify_key = String.from_java_bytes(verify_key)
19
+ seed + verify_key
20
+ end
21
+
22
+ def sign(signing_key, message)
23
+ verify_key = signing_key[32, 32].to_java_bytes
24
+ signing_key = signing_key[0, 32].to_java_bytes
25
+
26
+ signature = org.cryptosphere.ed25519.signature(message.to_java_bytes, signing_key, verify_key)
27
+ String.from_java_bytes(signature)
28
+ end
29
+
30
+ def verify(verify_key, signature, message)
31
+ org.cryptosphere.ed25519.checkvalid(
32
+ signature.to_java_bytes,
33
+ message.to_java_bytes,
34
+ verify_key.to_java_bytes
35
+ )
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ module Ed25519
6
+ # Private key for producing digital signatures
7
+ class SigningKey
8
+ attr_reader :seed, :keypair, :verify_key
9
+
10
+ # Generate a random Ed25519 signing key (i.e. private scalar)
11
+ def self.generate
12
+ new SecureRandom.random_bytes(Ed25519::KEY_SIZE)
13
+ end
14
+
15
+ # Create a new Ed25519::SigningKey from the given seed value
16
+ #
17
+ # @param seed [String] 32-byte seed value from which the key should be derived
18
+ def initialize(seed)
19
+ raise ArgumentError, "seed must be #{KEY_SIZE}-bytes long" unless seed.length == KEY_SIZE
20
+ @seed = seed
21
+
22
+ @keypair = Ed25519.provider.create_keypair(seed)
23
+ @verify_key = VerifyKey.new(@keypair[32, 32])
24
+ end
25
+
26
+ def sign(message)
27
+ Ed25519.provider.sign(@keypair, message)
28
+ end
29
+
30
+ def inspect
31
+ to_s
32
+ end
33
+
34
+ def to_bytes
35
+ seed
36
+ end
37
+ alias to_str to_bytes
38
+ end
39
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ed25519
4
+ # Public key for verifying digital signatures
5
+ class VerifyKey
6
+ # Create a Ed25519::VerifyKey from its serialized Twisted Edwards representation
7
+ #
8
+ # @param key [String] 32-byte string representing a serialized public key
9
+ def initialize(key)
10
+ raise ArgumentError, "seed must be 32 bytes long" unless key.length == KEY_SIZE
11
+ @key_bytes = key
12
+ end
13
+
14
+ # Verify an Ed25519 signature against the message
15
+ #
16
+ # @param signature [String] 64-byte string containing an Ed25519 signature
17
+ # @param message [String] string containing message to be verified
18
+ #
19
+ # @raise Ed25519::VerifyError signature verification failed
20
+ #
21
+ # @return [true] message verified successfully
22
+ def verify(signature, message)
23
+ if signature.length != SIGNATURE_SIZE
24
+ raise ArgumentError, "expected #{SIGNATURE_SIZE} byte signature, got #{signature.length}"
25
+ end
26
+
27
+ return true if Ed25519.provider.verify(@key_bytes, signature, message)
28
+ raise VerifyError, "signature verification failed!"
29
+ end
30
+
31
+ # Return a compressed twisted Edwards coordinate representing the public key
32
+ #
33
+ # @return [String] bytestring serialization of this public key
34
+ def to_bytes
35
+ @key_bytes
36
+ end
37
+ alias to_str to_bytes
38
+
39
+ # Show hex representation of serialized coordinate in string inspection
40
+ def inspect
41
+ "#<#{self.class}:#{@key_bytes.unpack('H*').first}>"
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ed25519
4
+ VERSION = "1.0.0"
5
+ end
Binary file
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ed25519
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: jruby
6
+ authors:
7
+ - Tony Arcieri
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.16'
19
+ name: bundler
20
+ prerelease: false
21
+ type: :development
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ description: A Ruby binding to the Ed25519 elliptic curve public-key signature system
28
+ described in RFC 8032.
29
+ email:
30
+ - tony.arcieri@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".gitignore"
36
+ - ".rspec"
37
+ - ".rubocop.yml"
38
+ - ".travis.yml"
39
+ - CHANGES.md
40
+ - CODE_OF_CONDUCT.md
41
+ - Gemfile
42
+ - LICENSE
43
+ - README.md
44
+ - Rakefile
45
+ - ed25519.gemspec
46
+ - ed25519.png
47
+ - ext/ed25519_java/org/cryptosphere/ed25519.java
48
+ - ext/ed25519_ref10/api.h
49
+ - ext/ed25519_ref10/base.h
50
+ - ext/ed25519_ref10/base2.h
51
+ - ext/ed25519_ref10/d.h
52
+ - ext/ed25519_ref10/d2.h
53
+ - ext/ed25519_ref10/ed25519_ref10.c
54
+ - ext/ed25519_ref10/ed25519_ref10.h
55
+ - ext/ed25519_ref10/extconf.rb
56
+ - ext/ed25519_ref10/fe.h
57
+ - ext/ed25519_ref10/fe_0.c
58
+ - ext/ed25519_ref10/fe_1.c
59
+ - ext/ed25519_ref10/fe_add.c
60
+ - ext/ed25519_ref10/fe_cmov.c
61
+ - ext/ed25519_ref10/fe_copy.c
62
+ - ext/ed25519_ref10/fe_frombytes.c
63
+ - ext/ed25519_ref10/fe_invert.c
64
+ - ext/ed25519_ref10/fe_isnegative.c
65
+ - ext/ed25519_ref10/fe_isnonzero.c
66
+ - ext/ed25519_ref10/fe_mul.c
67
+ - ext/ed25519_ref10/fe_neg.c
68
+ - ext/ed25519_ref10/fe_pow22523.c
69
+ - ext/ed25519_ref10/fe_sq.c
70
+ - ext/ed25519_ref10/fe_sq2.c
71
+ - ext/ed25519_ref10/fe_sub.c
72
+ - ext/ed25519_ref10/fe_tobytes.c
73
+ - ext/ed25519_ref10/ge.h
74
+ - ext/ed25519_ref10/ge_add.c
75
+ - ext/ed25519_ref10/ge_add.h
76
+ - ext/ed25519_ref10/ge_double_scalarmult.c
77
+ - ext/ed25519_ref10/ge_frombytes.c
78
+ - ext/ed25519_ref10/ge_madd.c
79
+ - ext/ed25519_ref10/ge_madd.h
80
+ - ext/ed25519_ref10/ge_msub.c
81
+ - ext/ed25519_ref10/ge_msub.h
82
+ - ext/ed25519_ref10/ge_p1p1_to_p2.c
83
+ - ext/ed25519_ref10/ge_p1p1_to_p3.c
84
+ - ext/ed25519_ref10/ge_p2_0.c
85
+ - ext/ed25519_ref10/ge_p2_dbl.c
86
+ - ext/ed25519_ref10/ge_p2_dbl.h
87
+ - ext/ed25519_ref10/ge_p3_0.c
88
+ - ext/ed25519_ref10/ge_p3_dbl.c
89
+ - ext/ed25519_ref10/ge_p3_to_cached.c
90
+ - ext/ed25519_ref10/ge_p3_to_p2.c
91
+ - ext/ed25519_ref10/ge_p3_tobytes.c
92
+ - ext/ed25519_ref10/ge_precomp_0.c
93
+ - ext/ed25519_ref10/ge_scalarmult_base.c
94
+ - ext/ed25519_ref10/ge_sub.c
95
+ - ext/ed25519_ref10/ge_sub.h
96
+ - ext/ed25519_ref10/ge_tobytes.c
97
+ - ext/ed25519_ref10/keypair.c
98
+ - ext/ed25519_ref10/open.c
99
+ - ext/ed25519_ref10/pow22523.h
100
+ - ext/ed25519_ref10/pow225521.h
101
+ - ext/ed25519_ref10/sc.h
102
+ - ext/ed25519_ref10/sc_muladd.c
103
+ - ext/ed25519_ref10/sc_reduce.c
104
+ - ext/ed25519_ref10/sha512.c
105
+ - ext/ed25519_ref10/sha512.h
106
+ - ext/ed25519_ref10/sign.c
107
+ - ext/ed25519_ref10/sqrtm1.h
108
+ - ext/ed25519_ref10/verify.c
109
+ - lib/ed25519.rb
110
+ - lib/ed25519/provider/jruby.rb
111
+ - lib/ed25519/signing_key.rb
112
+ - lib/ed25519/verify_key.rb
113
+ - lib/ed25519/version.rb
114
+ - lib/ed25519_java.jar
115
+ homepage: https://github.com/cryptosphere/ed25519
116
+ licenses: []
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: 2.2.2
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.6.14
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: An efficient digital signature library providing the Ed25519 algorithm
138
+ test_files: []