ring-native 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +3 -0
- data/README.md +22 -0
- data/Rakefile +1 -0
- data/ext/ring/extconf.rb +29 -0
- data/lib/ring/native.rb +8 -0
- data/lib/ring/native/version.rb +5 -0
- data/ring-native.gemspec +25 -0
- data/vendor/ring/BUILDING.md +40 -0
- data/vendor/ring/Cargo.toml +43 -0
- data/vendor/ring/LICENSE +185 -0
- data/vendor/ring/Makefile +35 -0
- data/vendor/ring/PORTING.md +163 -0
- data/vendor/ring/README.md +113 -0
- data/vendor/ring/STYLE.md +197 -0
- data/vendor/ring/appveyor.yml +27 -0
- data/vendor/ring/build.rs +108 -0
- data/vendor/ring/crypto/aes/aes.c +1142 -0
- data/vendor/ring/crypto/aes/aes_test.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/aes/aes_test.cc +93 -0
- data/vendor/ring/crypto/aes/asm/aes-586.pl +2368 -0
- data/vendor/ring/crypto/aes/asm/aes-armv4.pl +1249 -0
- data/vendor/ring/crypto/aes/asm/aes-x86_64.pl +2246 -0
- data/vendor/ring/crypto/aes/asm/aesni-x86.pl +1318 -0
- data/vendor/ring/crypto/aes/asm/aesni-x86_64.pl +2084 -0
- data/vendor/ring/crypto/aes/asm/aesv8-armx.pl +675 -0
- data/vendor/ring/crypto/aes/asm/bsaes-armv7.pl +1364 -0
- data/vendor/ring/crypto/aes/asm/bsaes-x86_64.pl +1565 -0
- data/vendor/ring/crypto/aes/asm/vpaes-x86.pl +841 -0
- data/vendor/ring/crypto/aes/asm/vpaes-x86_64.pl +1116 -0
- data/vendor/ring/crypto/aes/internal.h +87 -0
- data/vendor/ring/crypto/aes/mode_wrappers.c +61 -0
- data/vendor/ring/crypto/bn/add.c +394 -0
- data/vendor/ring/crypto/bn/asm/armv4-mont.pl +694 -0
- data/vendor/ring/crypto/bn/asm/armv8-mont.pl +1503 -0
- data/vendor/ring/crypto/bn/asm/bn-586.pl +774 -0
- data/vendor/ring/crypto/bn/asm/co-586.pl +287 -0
- data/vendor/ring/crypto/bn/asm/rsaz-avx2.pl +1882 -0
- data/vendor/ring/crypto/bn/asm/x86-mont.pl +592 -0
- data/vendor/ring/crypto/bn/asm/x86_64-gcc.c +599 -0
- data/vendor/ring/crypto/bn/asm/x86_64-mont.pl +1393 -0
- data/vendor/ring/crypto/bn/asm/x86_64-mont5.pl +3507 -0
- data/vendor/ring/crypto/bn/bn.c +352 -0
- data/vendor/ring/crypto/bn/bn_asn1.c +74 -0
- data/vendor/ring/crypto/bn/bn_test.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/bn/bn_test.cc +1696 -0
- data/vendor/ring/crypto/bn/cmp.c +200 -0
- data/vendor/ring/crypto/bn/convert.c +433 -0
- data/vendor/ring/crypto/bn/ctx.c +311 -0
- data/vendor/ring/crypto/bn/div.c +594 -0
- data/vendor/ring/crypto/bn/exponentiation.c +1335 -0
- data/vendor/ring/crypto/bn/gcd.c +711 -0
- data/vendor/ring/crypto/bn/generic.c +1019 -0
- data/vendor/ring/crypto/bn/internal.h +316 -0
- data/vendor/ring/crypto/bn/montgomery.c +516 -0
- data/vendor/ring/crypto/bn/mul.c +888 -0
- data/vendor/ring/crypto/bn/prime.c +829 -0
- data/vendor/ring/crypto/bn/random.c +334 -0
- data/vendor/ring/crypto/bn/rsaz_exp.c +262 -0
- data/vendor/ring/crypto/bn/rsaz_exp.h +53 -0
- data/vendor/ring/crypto/bn/shift.c +276 -0
- data/vendor/ring/crypto/bytestring/bytestring_test.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/bytestring/bytestring_test.cc +421 -0
- data/vendor/ring/crypto/bytestring/cbb.c +399 -0
- data/vendor/ring/crypto/bytestring/cbs.c +227 -0
- data/vendor/ring/crypto/bytestring/internal.h +46 -0
- data/vendor/ring/crypto/chacha/chacha_generic.c +140 -0
- data/vendor/ring/crypto/chacha/chacha_vec.c +323 -0
- data/vendor/ring/crypto/chacha/chacha_vec_arm.S +1447 -0
- data/vendor/ring/crypto/chacha/chacha_vec_arm_generate.go +153 -0
- data/vendor/ring/crypto/cipher/cipher_test.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/cipher/e_aes.c +390 -0
- data/vendor/ring/crypto/cipher/e_chacha20poly1305.c +208 -0
- data/vendor/ring/crypto/cipher/internal.h +173 -0
- data/vendor/ring/crypto/cipher/test/aes_128_gcm_tests.txt +543 -0
- data/vendor/ring/crypto/cipher/test/aes_128_key_wrap_tests.txt +9 -0
- data/vendor/ring/crypto/cipher/test/aes_256_gcm_tests.txt +475 -0
- data/vendor/ring/crypto/cipher/test/aes_256_key_wrap_tests.txt +23 -0
- data/vendor/ring/crypto/cipher/test/chacha20_poly1305_old_tests.txt +422 -0
- data/vendor/ring/crypto/cipher/test/chacha20_poly1305_tests.txt +484 -0
- data/vendor/ring/crypto/cipher/test/cipher_test.txt +100 -0
- data/vendor/ring/crypto/constant_time_test.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/constant_time_test.c +304 -0
- data/vendor/ring/crypto/cpu-arm-asm.S +32 -0
- data/vendor/ring/crypto/cpu-arm.c +199 -0
- data/vendor/ring/crypto/cpu-intel.c +261 -0
- data/vendor/ring/crypto/crypto.c +151 -0
- data/vendor/ring/crypto/curve25519/asm/x25519-arm.S +2118 -0
- data/vendor/ring/crypto/curve25519/curve25519.c +4888 -0
- data/vendor/ring/crypto/curve25519/x25519_test.cc +128 -0
- data/vendor/ring/crypto/digest/md32_common.h +181 -0
- data/vendor/ring/crypto/ec/asm/p256-x86_64-asm.pl +2725 -0
- data/vendor/ring/crypto/ec/ec.c +193 -0
- data/vendor/ring/crypto/ec/ec_curves.c +61 -0
- data/vendor/ring/crypto/ec/ec_key.c +228 -0
- data/vendor/ring/crypto/ec/ec_montgomery.c +114 -0
- data/vendor/ring/crypto/ec/example_mul.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/ec/internal.h +243 -0
- data/vendor/ring/crypto/ec/oct.c +253 -0
- data/vendor/ring/crypto/ec/p256-64.c +1794 -0
- data/vendor/ring/crypto/ec/p256-x86_64-table.h +9548 -0
- data/vendor/ring/crypto/ec/p256-x86_64.c +509 -0
- data/vendor/ring/crypto/ec/simple.c +1007 -0
- data/vendor/ring/crypto/ec/util-64.c +183 -0
- data/vendor/ring/crypto/ec/wnaf.c +508 -0
- data/vendor/ring/crypto/ecdh/ecdh.c +155 -0
- data/vendor/ring/crypto/ecdsa/ecdsa.c +304 -0
- data/vendor/ring/crypto/ecdsa/ecdsa_asn1.c +193 -0
- data/vendor/ring/crypto/ecdsa/ecdsa_test.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/ecdsa/ecdsa_test.cc +327 -0
- data/vendor/ring/crypto/header_removed.h +17 -0
- data/vendor/ring/crypto/internal.h +495 -0
- data/vendor/ring/crypto/libring.Windows.vcxproj +101 -0
- data/vendor/ring/crypto/mem.c +98 -0
- data/vendor/ring/crypto/modes/asm/aesni-gcm-x86_64.pl +1045 -0
- data/vendor/ring/crypto/modes/asm/ghash-armv4.pl +517 -0
- data/vendor/ring/crypto/modes/asm/ghash-x86.pl +1393 -0
- data/vendor/ring/crypto/modes/asm/ghash-x86_64.pl +1741 -0
- data/vendor/ring/crypto/modes/asm/ghashv8-armx.pl +422 -0
- data/vendor/ring/crypto/modes/ctr.c +226 -0
- data/vendor/ring/crypto/modes/gcm.c +1206 -0
- data/vendor/ring/crypto/modes/gcm_test.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/modes/gcm_test.c +348 -0
- data/vendor/ring/crypto/modes/internal.h +299 -0
- data/vendor/ring/crypto/perlasm/arm-xlate.pl +170 -0
- data/vendor/ring/crypto/perlasm/readme +100 -0
- data/vendor/ring/crypto/perlasm/x86_64-xlate.pl +1164 -0
- data/vendor/ring/crypto/perlasm/x86asm.pl +292 -0
- data/vendor/ring/crypto/perlasm/x86gas.pl +263 -0
- data/vendor/ring/crypto/perlasm/x86masm.pl +200 -0
- data/vendor/ring/crypto/perlasm/x86nasm.pl +187 -0
- data/vendor/ring/crypto/poly1305/poly1305.c +331 -0
- data/vendor/ring/crypto/poly1305/poly1305_arm.c +301 -0
- data/vendor/ring/crypto/poly1305/poly1305_arm_asm.S +2015 -0
- data/vendor/ring/crypto/poly1305/poly1305_test.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/poly1305/poly1305_test.cc +80 -0
- data/vendor/ring/crypto/poly1305/poly1305_test.txt +52 -0
- data/vendor/ring/crypto/poly1305/poly1305_vec.c +892 -0
- data/vendor/ring/crypto/rand/asm/rdrand-x86_64.pl +75 -0
- data/vendor/ring/crypto/rand/internal.h +32 -0
- data/vendor/ring/crypto/rand/rand.c +189 -0
- data/vendor/ring/crypto/rand/urandom.c +219 -0
- data/vendor/ring/crypto/rand/windows.c +56 -0
- data/vendor/ring/crypto/refcount_c11.c +66 -0
- data/vendor/ring/crypto/refcount_lock.c +53 -0
- data/vendor/ring/crypto/refcount_test.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/refcount_test.c +58 -0
- data/vendor/ring/crypto/rsa/blinding.c +462 -0
- data/vendor/ring/crypto/rsa/internal.h +108 -0
- data/vendor/ring/crypto/rsa/padding.c +300 -0
- data/vendor/ring/crypto/rsa/rsa.c +450 -0
- data/vendor/ring/crypto/rsa/rsa_asn1.c +261 -0
- data/vendor/ring/crypto/rsa/rsa_impl.c +944 -0
- data/vendor/ring/crypto/rsa/rsa_test.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/rsa/rsa_test.cc +437 -0
- data/vendor/ring/crypto/sha/asm/sha-armv8.pl +436 -0
- data/vendor/ring/crypto/sha/asm/sha-x86_64.pl +2390 -0
- data/vendor/ring/crypto/sha/asm/sha256-586.pl +1275 -0
- data/vendor/ring/crypto/sha/asm/sha256-armv4.pl +735 -0
- data/vendor/ring/crypto/sha/asm/sha256-armv8.pl +14 -0
- data/vendor/ring/crypto/sha/asm/sha256-x86_64.pl +14 -0
- data/vendor/ring/crypto/sha/asm/sha512-586.pl +911 -0
- data/vendor/ring/crypto/sha/asm/sha512-armv4.pl +666 -0
- data/vendor/ring/crypto/sha/asm/sha512-armv8.pl +14 -0
- data/vendor/ring/crypto/sha/asm/sha512-x86_64.pl +14 -0
- data/vendor/ring/crypto/sha/sha1.c +271 -0
- data/vendor/ring/crypto/sha/sha256.c +204 -0
- data/vendor/ring/crypto/sha/sha512.c +355 -0
- data/vendor/ring/crypto/test/file_test.cc +326 -0
- data/vendor/ring/crypto/test/file_test.h +181 -0
- data/vendor/ring/crypto/test/malloc.cc +150 -0
- data/vendor/ring/crypto/test/scoped_types.h +95 -0
- data/vendor/ring/crypto/test/test.Windows.vcxproj +35 -0
- data/vendor/ring/crypto/test/test_util.cc +46 -0
- data/vendor/ring/crypto/test/test_util.h +41 -0
- data/vendor/ring/crypto/thread_none.c +55 -0
- data/vendor/ring/crypto/thread_pthread.c +165 -0
- data/vendor/ring/crypto/thread_test.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/thread_test.c +200 -0
- data/vendor/ring/crypto/thread_win.c +282 -0
- data/vendor/ring/examples/checkdigest.rs +103 -0
- data/vendor/ring/include/openssl/aes.h +121 -0
- data/vendor/ring/include/openssl/arm_arch.h +129 -0
- data/vendor/ring/include/openssl/base.h +156 -0
- data/vendor/ring/include/openssl/bn.h +794 -0
- data/vendor/ring/include/openssl/buffer.h +18 -0
- data/vendor/ring/include/openssl/bytestring.h +235 -0
- data/vendor/ring/include/openssl/chacha.h +37 -0
- data/vendor/ring/include/openssl/cmac.h +76 -0
- data/vendor/ring/include/openssl/cpu.h +184 -0
- data/vendor/ring/include/openssl/crypto.h +43 -0
- data/vendor/ring/include/openssl/curve25519.h +88 -0
- data/vendor/ring/include/openssl/ec.h +225 -0
- data/vendor/ring/include/openssl/ec_key.h +129 -0
- data/vendor/ring/include/openssl/ecdh.h +110 -0
- data/vendor/ring/include/openssl/ecdsa.h +156 -0
- data/vendor/ring/include/openssl/err.h +201 -0
- data/vendor/ring/include/openssl/mem.h +101 -0
- data/vendor/ring/include/openssl/obj_mac.h +71 -0
- data/vendor/ring/include/openssl/opensslfeatures.h +68 -0
- data/vendor/ring/include/openssl/opensslv.h +18 -0
- data/vendor/ring/include/openssl/ossl_typ.h +18 -0
- data/vendor/ring/include/openssl/poly1305.h +51 -0
- data/vendor/ring/include/openssl/rand.h +70 -0
- data/vendor/ring/include/openssl/rsa.h +399 -0
- data/vendor/ring/include/openssl/thread.h +133 -0
- data/vendor/ring/include/openssl/type_check.h +71 -0
- data/vendor/ring/mk/Common.props +63 -0
- data/vendor/ring/mk/Windows.props +42 -0
- data/vendor/ring/mk/WindowsTest.props +18 -0
- data/vendor/ring/mk/appveyor.bat +62 -0
- data/vendor/ring/mk/bottom_of_makefile.mk +54 -0
- data/vendor/ring/mk/ring.mk +266 -0
- data/vendor/ring/mk/top_of_makefile.mk +214 -0
- data/vendor/ring/mk/travis.sh +40 -0
- data/vendor/ring/mk/update-travis-yml.py +229 -0
- data/vendor/ring/ring.sln +153 -0
- data/vendor/ring/src/aead.rs +682 -0
- data/vendor/ring/src/agreement.rs +248 -0
- data/vendor/ring/src/c.rs +129 -0
- data/vendor/ring/src/constant_time.rs +37 -0
- data/vendor/ring/src/der.rs +96 -0
- data/vendor/ring/src/digest.rs +690 -0
- data/vendor/ring/src/digest_tests.txt +57 -0
- data/vendor/ring/src/ecc.rs +28 -0
- data/vendor/ring/src/ecc_build.rs +279 -0
- data/vendor/ring/src/ecc_curves.rs +117 -0
- data/vendor/ring/src/ed25519_tests.txt +2579 -0
- data/vendor/ring/src/exe_tests.rs +46 -0
- data/vendor/ring/src/ffi.rs +29 -0
- data/vendor/ring/src/file_test.rs +187 -0
- data/vendor/ring/src/hkdf.rs +153 -0
- data/vendor/ring/src/hkdf_tests.txt +59 -0
- data/vendor/ring/src/hmac.rs +414 -0
- data/vendor/ring/src/hmac_tests.txt +97 -0
- data/vendor/ring/src/input.rs +312 -0
- data/vendor/ring/src/lib.rs +41 -0
- data/vendor/ring/src/pbkdf2.rs +265 -0
- data/vendor/ring/src/pbkdf2_tests.txt +113 -0
- data/vendor/ring/src/polyfill.rs +57 -0
- data/vendor/ring/src/rand.rs +28 -0
- data/vendor/ring/src/signature.rs +314 -0
- data/vendor/ring/third-party/NIST/README.md +9 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA1LongMsg.rsp +263 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA1Monte.rsp +309 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA1ShortMsg.rsp +267 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA224LongMsg.rsp +263 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA224Monte.rsp +309 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA224ShortMsg.rsp +267 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA256LongMsg.rsp +263 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA256Monte.rsp +309 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA256ShortMsg.rsp +267 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA384LongMsg.rsp +519 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA384Monte.rsp +309 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA384ShortMsg.rsp +523 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA512LongMsg.rsp +519 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA512Monte.rsp +309 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA512ShortMsg.rsp +523 -0
- data/vendor/ring/third-party/NIST/sha256sums.txt +1 -0
- metadata +333 -0
@@ -0,0 +1,248 @@
|
|
1
|
+
// Copyright 2015 Brian Smith.
|
2
|
+
//
|
3
|
+
// Permission to use, copy, modify, and/or distribute this software for any
|
4
|
+
// purpose with or without fee is hereby granted, provided that the above
|
5
|
+
// copyright notice and this permission notice appear in all copies.
|
6
|
+
//
|
7
|
+
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
|
8
|
+
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
9
|
+
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
10
|
+
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
11
|
+
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
12
|
+
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
13
|
+
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
14
|
+
|
15
|
+
//! Key agreement: ECDH.
|
16
|
+
|
17
|
+
use super::{c, digest, ecc, ffi};
|
18
|
+
use super::input::Input;
|
19
|
+
use std;
|
20
|
+
|
21
|
+
/// A key agreement algorithm.
|
22
|
+
pub struct Algorithm {
|
23
|
+
ec_group_fn: unsafe extern fn () -> *const ecc::EC_GROUP,
|
24
|
+
encoded_public_key_len: usize,
|
25
|
+
nid: c::int,
|
26
|
+
}
|
27
|
+
|
28
|
+
/// An ephemeral key pair for use (only) with `agree_ephemeral`. The
|
29
|
+
/// signature of `agree_ephemeral` ensures that an `EphemeralKeyPair` can be
|
30
|
+
/// used for at most one key agreement.
|
31
|
+
pub struct EphemeralKeyPair {
|
32
|
+
key: *mut EC_KEY,
|
33
|
+
algorithm: &'static Algorithm
|
34
|
+
}
|
35
|
+
|
36
|
+
impl EphemeralKeyPair {
|
37
|
+
/// Generate a new ephemeral key pair for the given algorithm.
|
38
|
+
///
|
39
|
+
/// C analog: `EC_KEY_new_by_curve_name` + `EC_KEY_generate_key`.
|
40
|
+
pub fn generate(algorithm: &'static Algorithm)
|
41
|
+
-> Result<EphemeralKeyPair, ()> {
|
42
|
+
let key = try!(ffi::map_bssl_ptr_result(unsafe {
|
43
|
+
EC_KEY_generate_key_ex((algorithm.ec_group_fn)())
|
44
|
+
}));
|
45
|
+
Ok(EphemeralKeyPair { key: key, algorithm: algorithm })
|
46
|
+
}
|
47
|
+
|
48
|
+
/// The size in bytes of the encoded public key returned from `public_key`.
|
49
|
+
#[inline(always)]
|
50
|
+
pub fn public_point_len(&self) -> usize {
|
51
|
+
self.algorithm.encoded_public_key_len
|
52
|
+
}
|
53
|
+
|
54
|
+
/// Fills `out` with the public point encoded in the standard form for the
|
55
|
+
/// algorithm.
|
56
|
+
///
|
57
|
+
/// `out.len()` must be equal to the value returned by `public_point_len`.
|
58
|
+
pub fn fill_with_encoded_public_key(&self, out: &mut [u8])
|
59
|
+
-> Result<(), ()> {
|
60
|
+
match unsafe {
|
61
|
+
EC_KEY_public_key_to_oct(self.key, out.as_mut_ptr(), out.len())
|
62
|
+
} {
|
63
|
+
n if n == self.public_point_len() => Ok(()),
|
64
|
+
_ => Err(())
|
65
|
+
}
|
66
|
+
}
|
67
|
+
}
|
68
|
+
|
69
|
+
impl Drop for EphemeralKeyPair {
|
70
|
+
fn drop(&mut self) {
|
71
|
+
unsafe {
|
72
|
+
EC_KEY_free(self.key);
|
73
|
+
}
|
74
|
+
}
|
75
|
+
}
|
76
|
+
|
77
|
+
/// Performs a key agreement with an ephemeral key pair's private key and the
|
78
|
+
/// given public key.
|
79
|
+
///
|
80
|
+
/// `my_key_pair` is the ephemeral key pair to use. Since `my_key_pair` is
|
81
|
+
/// moved, it will not be usable after calling `agree_ephemeral`, thus
|
82
|
+
/// guaranteeing that the key is used for only one key agreement.
|
83
|
+
/// `peer_algorithm` is the algorithm/curve for the peer's public key point;
|
84
|
+
/// `agree_ephemeral` will return `Err(())` if it does not match `my_key_pair's`
|
85
|
+
/// algorithm/curve. `peer_encoded_pubic_key` is the peer's public key. It
|
86
|
+
/// must be encoded in the standard form for the algorithm; see the algorithm's
|
87
|
+
/// documentation for details. `error_value` is the value to return if an error
|
88
|
+
/// occurs before `kdf` is called, e.g. when decoding of the peer's public key
|
89
|
+
/// fails. After the key agreement is done, `agree_ephemeral` calls `kdf` with
|
90
|
+
/// the raw key material from the key agrement operation and then returns what
|
91
|
+
/// `kdf` returns.
|
92
|
+
///
|
93
|
+
/// C analogs: `ECDH_compute_key_ex` (*ring* only), `EC_POINT_oct2point` +
|
94
|
+
/// `ECDH_compute_key`.
|
95
|
+
//
|
96
|
+
// TODO: If the key is authenticated then we don't necessarily need to verify
|
97
|
+
// that the peer's public point is on the curve since a malicious
|
98
|
+
// authenticated peer could just as easily give us a bad public point that is
|
99
|
+
// on the curve. Also, given that our key pair is ephemeral, we're not risking
|
100
|
+
// the leakage of a long-term key via invalid point attacks. Accordingly, even
|
101
|
+
// though the lower-level C code does check that the peer's point is on the
|
102
|
+
// curve, that check seems like overkill, at least for the most typical uses
|
103
|
+
// of this function. On the other hand, some users may feel that it is
|
104
|
+
// worthwhile to do point validity check even if it seems to be unnecssary.
|
105
|
+
// Accordingly, it might be worthwhile to change this interface in the future
|
106
|
+
// so that the caller can choose how much validation of the peer's public
|
107
|
+
// point is done.
|
108
|
+
pub fn agree_ephemeral<F, R, E>(my_key_pair: EphemeralKeyPair,
|
109
|
+
peer_alg: &Algorithm,
|
110
|
+
peer_encoded_public_point: Input,
|
111
|
+
error_value: E, kdf: F) -> Result<R, E>
|
112
|
+
where F: FnOnce(&[u8]) -> Result<R, E> {
|
113
|
+
let mut shared_key = [0u8; MAX_COORDINATE_LEN];
|
114
|
+
let mut shared_key_len = 0;
|
115
|
+
let peer_encoded_public_point =
|
116
|
+
peer_encoded_public_point.as_slice_less_safe();
|
117
|
+
match unsafe {
|
118
|
+
ECDH_compute_key_ex(shared_key.as_mut_ptr(), &mut shared_key_len,
|
119
|
+
shared_key.len(), my_key_pair.key, peer_alg.nid,
|
120
|
+
peer_encoded_public_point.as_ptr(),
|
121
|
+
peer_encoded_public_point.len())
|
122
|
+
} {
|
123
|
+
1 => kdf(&shared_key[0..shared_key_len]),
|
124
|
+
_ => Err(error_value)
|
125
|
+
}
|
126
|
+
}
|
127
|
+
|
128
|
+
// TODO: After ecdsa_test.cc is removed, this function should be removed and
|
129
|
+
// the caller should be changed to call `SHA512_5` directly. Also, the
|
130
|
+
// alternative implementation of this in crypto/test should be removed at
|
131
|
+
// that time.
|
132
|
+
#[allow(non_snake_case)]
|
133
|
+
#[doc(hidden)]
|
134
|
+
#[no_mangle]
|
135
|
+
pub extern fn BN_generate_dsa_nonce_digest(
|
136
|
+
out: *mut u8, out_len: c::size_t,
|
137
|
+
part1: *const u8, part1_len: c::size_t,
|
138
|
+
part2: *const u8, part2_len: c::size_t,
|
139
|
+
part3: *const u8, part3_len: c::size_t,
|
140
|
+
part4: *const u8, part4_len: c::size_t,
|
141
|
+
part5: *const u8, part5_len: c::size_t)
|
142
|
+
-> c::int {
|
143
|
+
SHA512_5(out, out_len, part1, part1_len, part2, part2_len, part3,
|
144
|
+
part3_len, part4, part4_len, part5, part5_len);
|
145
|
+
1
|
146
|
+
}
|
147
|
+
|
148
|
+
/// SHA512_5 calculates the SHA-512 digest of the concatenation of |part1|
|
149
|
+
/// through |part5|. Any part<N> may be null if and only if the corresponding
|
150
|
+
/// part<N>_len is zero. This ugliness exists in order to allow some of the
|
151
|
+
/// C ECC code to calculate SHA-512 digests.
|
152
|
+
#[allow(non_snake_case)]
|
153
|
+
#[doc(hidden)]
|
154
|
+
#[no_mangle]
|
155
|
+
pub extern fn SHA512_5(out: *mut u8, out_len: c::size_t,
|
156
|
+
part1: *const u8, part1_len: c::size_t,
|
157
|
+
part2: *const u8, part2_len: c::size_t,
|
158
|
+
part3: *const u8, part3_len: c::size_t,
|
159
|
+
part4: *const u8, part4_len: c::size_t,
|
160
|
+
part5: *const u8, part5_len: c::size_t) {
|
161
|
+
fn maybe_update(ctx: &mut digest::Context, part: *const u8,
|
162
|
+
part_len: c::size_t) {
|
163
|
+
if part_len != 0 {
|
164
|
+
assert!(!part.is_null());
|
165
|
+
ctx.update(unsafe { std::slice::from_raw_parts(part, part_len) });
|
166
|
+
}
|
167
|
+
}
|
168
|
+
|
169
|
+
let mut ctx = digest::Context::new(&digest::SHA512);
|
170
|
+
maybe_update(&mut ctx, part1, part1_len);
|
171
|
+
maybe_update(&mut ctx, part2, part2_len);
|
172
|
+
maybe_update(&mut ctx, part3, part3_len);
|
173
|
+
maybe_update(&mut ctx, part4, part4_len);
|
174
|
+
maybe_update(&mut ctx, part5, part5_len);
|
175
|
+
let digest = ctx.finish();
|
176
|
+
let digest = digest.as_ref();
|
177
|
+
let out = unsafe { std::slice::from_raw_parts_mut(out, out_len) };
|
178
|
+
assert_eq!(out.len(), digest.len());
|
179
|
+
for i in 0..digest.len() {
|
180
|
+
out[i] = digest[i];
|
181
|
+
}
|
182
|
+
}
|
183
|
+
|
184
|
+
// XXX: Replace with `const fn` when `const fn` is stable:
|
185
|
+
// https://github.com/rust-lang/rust/issues/24111
|
186
|
+
macro_rules! encoded_public_key_len {
|
187
|
+
( $bits:expr ) => ( 1 + (2 * (($bits + 7) / 8)) )
|
188
|
+
}
|
189
|
+
|
190
|
+
/// ECDH using the NIST P-256 (secp256r1) curve.
|
191
|
+
///
|
192
|
+
/// Public keys are encoding in uncompressed form using the
|
193
|
+
/// Octet-String-to-Elliptic-Curve-Point algorithm in [SEC 1: Elliptic Curve
|
194
|
+
/// Cryptography, Version 2.0](http://www.secg.org/sec1-v2.pdf).
|
195
|
+
///
|
196
|
+
/// C analogs: `EC_GROUP_P256` (*ring* only),
|
197
|
+
/// `EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1)`")
|
198
|
+
pub static ECDH_P256: Algorithm = Algorithm {
|
199
|
+
ec_group_fn: ecc::EC_GROUP_P256,
|
200
|
+
encoded_public_key_len: encoded_public_key_len!(256),
|
201
|
+
nid: 415, // NID_X9_62_prime256v1
|
202
|
+
};
|
203
|
+
|
204
|
+
/// ECDH using the NIST P-384 (secp384r1) curve.
|
205
|
+
///
|
206
|
+
/// Public keys are encoding in uncompressed form using the
|
207
|
+
/// Octet-String-to-Elliptic-Curve-Point algorithm in [SEC 1: Elliptic Curve
|
208
|
+
/// Cryptography, Version 2.0](http://www.secg.org/sec1-v2.pdf).
|
209
|
+
///
|
210
|
+
/// C analogs: `EC_GROUP_P384` (*ring* only),
|
211
|
+
/// `EC_GROUP_new_by_curve_name(NID_secp384r1)`")
|
212
|
+
pub static ECDH_P384: Algorithm = Algorithm {
|
213
|
+
ec_group_fn: ecc::EC_GROUP_P384,
|
214
|
+
encoded_public_key_len: encoded_public_key_len!(384),
|
215
|
+
nid: 715, // NID_secp384r1
|
216
|
+
};
|
217
|
+
|
218
|
+
/// ECDH using the NIST P-521 (secp521r1) curve.
|
219
|
+
///
|
220
|
+
/// Public keys are encoding in uncompressed form using the
|
221
|
+
/// Octet-String-to-Elliptic-Curve-Point algorithm in [SEC 1: Elliptic Curve
|
222
|
+
/// Cryptography, Version 2.0](http://www.secg.org/sec1-v2.pdf).
|
223
|
+
///
|
224
|
+
/// C analogs: `EC_GROUP_new_p521` (*ring* only),
|
225
|
+
/// `EC_GROUP_new_by_curve_name(NID_secp521r1)`")
|
226
|
+
pub static ECDH_P521: Algorithm = Algorithm {
|
227
|
+
ec_group_fn: ecc::EC_GROUP_P521,
|
228
|
+
encoded_public_key_len: encoded_public_key_len!(521),
|
229
|
+
nid: 716, // NID_secp521r1
|
230
|
+
};
|
231
|
+
|
232
|
+
const MAX_COORDINATE_LEN: usize = (521 + 7) / 8;
|
233
|
+
|
234
|
+
#[allow(non_camel_case_types)]
|
235
|
+
enum EC_KEY { }
|
236
|
+
|
237
|
+
extern {
|
238
|
+
fn EC_KEY_generate_key_ex(group: *const ecc::EC_GROUP) -> *mut EC_KEY;
|
239
|
+
fn EC_KEY_public_key_to_oct(key: *const EC_KEY, out: *mut u8,
|
240
|
+
out_len: c::size_t) -> c::size_t;
|
241
|
+
fn EC_KEY_free(key: *mut EC_KEY);
|
242
|
+
|
243
|
+
fn ECDH_compute_key_ex(out: *mut u8, out_len: *mut c::size_t,
|
244
|
+
max_out_len: c::size_t, my_key_pair: *const EC_KEY,
|
245
|
+
peer_curve_nid: c::int,
|
246
|
+
peer_pub_point_bytes: *const u8,
|
247
|
+
peer_pub_point_bytes_len: c::size_t) -> c::int;
|
248
|
+
}
|
@@ -0,0 +1,129 @@
|
|
1
|
+
// Copyright 2015 Brian Smith.
|
2
|
+
//
|
3
|
+
// Permission to use, copy, modify, and/or distribute this software for any
|
4
|
+
// purpose with or without fee is hereby granted, provided that the above
|
5
|
+
// copyright notice and this permission notice appear in all copies.
|
6
|
+
//
|
7
|
+
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
|
8
|
+
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
9
|
+
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
|
10
|
+
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
11
|
+
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
12
|
+
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
13
|
+
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
14
|
+
|
15
|
+
//! TODO: Module-level documentation.
|
16
|
+
|
17
|
+
#![allow(non_camel_case_types)]
|
18
|
+
|
19
|
+
macro_rules! define_type {
|
20
|
+
( $name:ident, $builtin:ty, $test_c_metrics:ident, $get_c_align_fn:ident,
|
21
|
+
$get_c_size_fn:ident, $doc:expr ) =>
|
22
|
+
{
|
23
|
+
#[doc = $doc]
|
24
|
+
pub type $name = $builtin;
|
25
|
+
|
26
|
+
define_metrics_tests!($name, $test_c_metrics, $get_c_align_fn,
|
27
|
+
$get_c_size_fn);
|
28
|
+
}
|
29
|
+
}
|
30
|
+
|
31
|
+
macro_rules! define_metrics_tests {
|
32
|
+
( $name:ident, $test_c_metrics:ident, $get_c_align_fn:ident,
|
33
|
+
$get_c_size_fn:ident ) =>
|
34
|
+
{
|
35
|
+
#[cfg(test)]
|
36
|
+
extern {
|
37
|
+
// We can't use `size_t` because we need to test that our
|
38
|
+
// definition of `size_t` is correct using this code! We use `u16`
|
39
|
+
// because even 8-bit and 16-bit microcontrollers have no trouble
|
40
|
+
// with it, and because `u16` is always as smaller or smaller than
|
41
|
+
// `usize`.
|
42
|
+
fn $get_c_align_fn() -> u16;
|
43
|
+
fn $get_c_size_fn() -> u16;
|
44
|
+
}
|
45
|
+
|
46
|
+
#[cfg(test)]
|
47
|
+
#[test]
|
48
|
+
fn $test_c_metrics() {
|
49
|
+
use std::mem;
|
50
|
+
|
51
|
+
let c_align = unsafe { $get_c_align_fn() };
|
52
|
+
let c_size = unsafe { $get_c_size_fn() };
|
53
|
+
|
54
|
+
// XXX: Remove these assertions and these uses of `as` when Rust
|
55
|
+
// supports implicit coercion of `u16` to `usize`.
|
56
|
+
assert!(mem::size_of_val(&c_align) <= mem::size_of::<usize>());
|
57
|
+
assert!(mem::size_of_val(&c_size) <= mem::size_of::<usize>());
|
58
|
+
assert_eq!((mem::align_of::<$name>(), mem::size_of::<$name>()),
|
59
|
+
(c_align as usize, c_size as usize));
|
60
|
+
}
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
define_type!(int, i32, test_int_metrics, ring_int_align, ring_int_size,
|
65
|
+
"The C `int` type. Equivalent to `libc::int`.");
|
66
|
+
|
67
|
+
define_type!(
|
68
|
+
size_t, usize, test_size_t_metrics, ring_size_t_align, ring_size_t_size,
|
69
|
+
"The C `size_t` type from `<stdint.h>`.
|
70
|
+
|
71
|
+
ISO C's `size_t` is defined to be the type of the result of the
|
72
|
+
`sizeof` operator and the type of the size parameter to `malloc`. That
|
73
|
+
is, C's `size_t` is only required to hold the size of the largest object
|
74
|
+
that can be allocated. In particular, it is legal for a C implementation
|
75
|
+
to have a maximum object size smaller than the entire address space. For
|
76
|
+
example, a C implementation may have an maximum object size of 2^32
|
77
|
+
bytes with a 64-bit address space, and typedef `size_t` as `uint32_t` so
|
78
|
+
that `sizeof(size_t) == 4` and `sizeof(void*) == 8`.
|
79
|
+
|
80
|
+
Rust's `usize`, on the other hand, is defined to always be the same size
|
81
|
+
as a pointer. This means that it is possible, in theory, to have a platform
|
82
|
+
where `usize` can represent values that `size_t` cannot represent. However,
|
83
|
+
on the vast majority of systems, `usize` and `size_t` are represented the
|
84
|
+
same way. If it were required to explicitly cast `usize` to `size_t` on
|
85
|
+
common platforms, then many programmers would habitually write expressions
|
86
|
+
such as `my_slice.len() as libc::size_t` expecting this to always work and
|
87
|
+
be safe. But such a cast is *not* safe on the uncommon platforms where
|
88
|
+
`mem::sizeof(libc::size_t) < mem::size_t(usize)`. Consequently, to reduce
|
89
|
+
the chances of programmers becoming habituated to such casts that would be
|
90
|
+
unsafe on unusual platforms, we have adopted the following convention:
|
91
|
+
|
92
|
+
* On common platforms where C's `size_t` is the same size as `usize`,
|
93
|
+
`ring::c::size_t` must be a type alias of `usize`.
|
94
|
+
|
95
|
+
* On uncommon platforms where C's `size_t` is not the same size as `usize`,
|
96
|
+
`ring::c::size_t` must be a type alias for a type other than `usize`.
|
97
|
+
|
98
|
+
* Code that was written without consideration for the uncommon platforms
|
99
|
+
should not do any explicit casting between `size_t` and `usize`. Such
|
100
|
+
code will fail to compile on the uncommon platforms; this is better than
|
101
|
+
executing with unsafe truncations.
|
102
|
+
|
103
|
+
* Code that was written with full consideration of the uncommon platforms
|
104
|
+
should have explicit casts using `num::cast` or other methods that avoid
|
105
|
+
unintended truncation. Such code will then work on all platforms.");
|
106
|
+
|
107
|
+
// XXX: MSVC's `alignof` returns strange values for `int8_t`.
|
108
|
+
#[cfg(not(windows))]
|
109
|
+
define_metrics_tests!(i8, test_i8_metrics, ring_int8_t_align, ring_int8_t_size);
|
110
|
+
|
111
|
+
// XXX: MSVC's `alignof` returns strange values for `uint8_t`.
|
112
|
+
#[cfg(not(windows))]
|
113
|
+
define_metrics_tests!(u8, test_u8_metrics, ring_uint8_t_align,
|
114
|
+
ring_uint8_t_size);
|
115
|
+
|
116
|
+
define_metrics_tests!(i16, test_i16_metrics, ring_int16_t_align,
|
117
|
+
ring_int16_t_size);
|
118
|
+
define_metrics_tests!(u16, test_u16_metrics, ring_uint16_t_align,
|
119
|
+
ring_uint16_t_size);
|
120
|
+
|
121
|
+
define_metrics_tests!(i32, test_i32_metrics, ring_int32_t_align,
|
122
|
+
ring_int32_t_size);
|
123
|
+
define_metrics_tests!(u32, test_u32_metrics, ring_uint32_t_align,
|
124
|
+
ring_uint32_t_size);
|
125
|
+
|
126
|
+
define_metrics_tests!(i64, test_i64_metrics, ring_int64_t_align,
|
127
|
+
ring_int64_t_size);
|
128
|
+
define_metrics_tests!(u64, test_u64_metrics, ring_uint64_t_align,
|
129
|
+
ring_uint64_t_size);
|
@@ -0,0 +1,37 @@
|
|
1
|
+
// Copyright 2015 Brian Smith.
|
2
|
+
//
|
3
|
+
// Permission to use, copy, modify, and/or distribute this software for any
|
4
|
+
// purpose with or without fee is hereby granted, provided that the above
|
5
|
+
// copyright notice and this permission notice appear in all copies.
|
6
|
+
//
|
7
|
+
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
|
8
|
+
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
9
|
+
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
|
10
|
+
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
11
|
+
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
12
|
+
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
13
|
+
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
14
|
+
|
15
|
+
//! Constant-time operations.
|
16
|
+
|
17
|
+
use super::c;
|
18
|
+
|
19
|
+
/// Returns `Ok(())` if `a == b` and `Err(())` otherwise. The comparison of
|
20
|
+
/// `a` and `b` is done in constant time with respect to the contents of each,
|
21
|
+
/// but NOT in constant time with respect to the lengths of `a` and `b`.
|
22
|
+
pub fn verify_slices_are_equal(a: &[u8], b: &[u8]) -> Result<(), ()> {
|
23
|
+
if a.len() != b.len() {
|
24
|
+
return Err(());
|
25
|
+
}
|
26
|
+
let result = unsafe {
|
27
|
+
CRYPTO_memcmp(a.as_ptr(), b.as_ptr(), a.len())
|
28
|
+
};
|
29
|
+
match result {
|
30
|
+
0 => Ok(()),
|
31
|
+
_ => Err(())
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
extern {
|
36
|
+
fn CRYPTO_memcmp(a: *const u8, b: *const u8, len: c::size_t) -> c::int;
|
37
|
+
}
|
@@ -0,0 +1,96 @@
|
|
1
|
+
// Copyright 2015 Brian Smith.
|
2
|
+
//
|
3
|
+
// Permission to use, copy, modify, and/or distribute this software for any
|
4
|
+
// purpose with or without fee is hereby granted, provided that the above
|
5
|
+
// copyright notice and this permission notice appear in all copies.
|
6
|
+
//
|
7
|
+
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
|
8
|
+
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
9
|
+
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
|
10
|
+
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
11
|
+
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
12
|
+
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
13
|
+
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
14
|
+
|
15
|
+
//! Building blocks for parsing DER-encoded ASN.1 structures.
|
16
|
+
//!
|
17
|
+
//! This module contains the foundational parts of an ASN.1 DER parser.
|
18
|
+
|
19
|
+
use super::input::*;
|
20
|
+
|
21
|
+
pub const CONSTRUCTED : u8 = 1 << 5;
|
22
|
+
pub const CONTEXT_SPECIFIC : u8 = 2 << 6;
|
23
|
+
|
24
|
+
#[derive(Clone, Copy, PartialEq)]
|
25
|
+
#[repr(u8)]
|
26
|
+
pub enum Tag {
|
27
|
+
Boolean = 0x01,
|
28
|
+
Integer = 0x02,
|
29
|
+
BitString = 0x03,
|
30
|
+
OctetString = 0x04,
|
31
|
+
Null = 0x05,
|
32
|
+
OID = 0x06,
|
33
|
+
Sequence = CONSTRUCTED | 0x10, // 0x30
|
34
|
+
UTCTime = 0x17,
|
35
|
+
GeneralizedTime = 0x18,
|
36
|
+
|
37
|
+
ContextSpecificConstructed0 = CONTEXT_SPECIFIC | CONSTRUCTED | 0,
|
38
|
+
ContextSpecificConstructed1 = CONTEXT_SPECIFIC | CONSTRUCTED | 1,
|
39
|
+
ContextSpecificConstructed3 = CONTEXT_SPECIFIC | CONSTRUCTED | 3,
|
40
|
+
}
|
41
|
+
|
42
|
+
pub fn expect_tag_and_get_value<'a>(input: &mut Reader<'a>, tag: Tag)
|
43
|
+
-> Result<Input<'a>, ()> {
|
44
|
+
let (actual_tag, inner) = try!(read_tag_and_get_value(input));
|
45
|
+
if (tag as usize) != (actual_tag as usize) {
|
46
|
+
return Err(());
|
47
|
+
}
|
48
|
+
Ok(inner)
|
49
|
+
}
|
50
|
+
|
51
|
+
pub fn read_tag_and_get_value<'a>(input: &mut Reader<'a>)
|
52
|
+
-> Result<(u8, Input<'a>), ()> {
|
53
|
+
let tag = try!(input.read_byte());
|
54
|
+
if (tag & 0x1F) == 0x1F {
|
55
|
+
return Err(()) // High tag number form is not allowed.
|
56
|
+
}
|
57
|
+
|
58
|
+
// If the high order bit of the first byte is set to zero then the length
|
59
|
+
// is encoded in the seven remaining bits of that byte. Otherwise, those
|
60
|
+
// seven bits represent the number of bytes used to encode the length.
|
61
|
+
let length = match try!(input.read_byte()) {
|
62
|
+
n if (n & 0x80) == 0 => n as usize,
|
63
|
+
0x81 => {
|
64
|
+
let second_byte = try!(input.read_byte());
|
65
|
+
if second_byte < 128 {
|
66
|
+
return Err(()) // Not the canonical encoding.
|
67
|
+
}
|
68
|
+
second_byte as usize
|
69
|
+
},
|
70
|
+
0x82 => {
|
71
|
+
let second_byte = try!(input.read_byte()) as usize;
|
72
|
+
let third_byte = try!(input.read_byte()) as usize;
|
73
|
+
let combined = (second_byte << 8) | third_byte;
|
74
|
+
if combined < 256 {
|
75
|
+
return Err(()); // Not the canonical encoding.
|
76
|
+
}
|
77
|
+
combined
|
78
|
+
},
|
79
|
+
_ => {
|
80
|
+
return Err(()); // We don't support longer lengths.
|
81
|
+
}
|
82
|
+
};
|
83
|
+
|
84
|
+
let inner = try!(input.skip_and_get_input(length));
|
85
|
+
Ok((tag, inner))
|
86
|
+
}
|
87
|
+
|
88
|
+
// TODO: investigate taking decoder as a reference to reduce generated code
|
89
|
+
// size.
|
90
|
+
pub fn nested<'a, F, R, E: Copy>(input: &mut Reader<'a>, tag: Tag, error: E,
|
91
|
+
decoder: F) -> Result<R, E>
|
92
|
+
where F : FnOnce(&mut Reader<'a>)
|
93
|
+
-> Result<R, E> {
|
94
|
+
let inner = try!(expect_tag_and_get_value(input, tag).map_err(|_| error));
|
95
|
+
read_all(inner, error, decoder)
|
96
|
+
}
|