pqc_asn1 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d82d0cdf76e9156af39368f55311d62bc7bf1a0f8b7c50e1a59cf8d49a90ff95
4
+ data.tar.gz: d922d07e2f12177fe0a0b1a88df589aa6edf9f9ff45f4ec6278a20926d628071
5
+ SHA512:
6
+ metadata.gz: dd25d378e2719c6b29abbb6cb68d8c2c1e76b974b7532c97cdb4399e19f24e90aa38ff36b12bbcf2b191f7f8f5bc8207a90ed76016c5bbaafe11ae588d842082
7
+ data.tar.gz: f77499b592e7cd301780cf33d9a9cf5112a167a1cf4c0c624d97ebdbcc3f1e9dd30ba25cc0d9c82831f8d208faec920d89ea21740f5f12af05dc08df066b2b28
data/CHANGELOG.md ADDED
@@ -0,0 +1,116 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ### Fixed
11
+
12
+ - `SecureBuffer#wipe!` now returns `self` instead of `nil`, enabling method
13
+ chaining (e.g. `buf.wipe!.frozen?`).
14
+ - `SecureBuffer.random(n)` rejects negative values with a clear `ArgumentError`
15
+ instead of silently wrapping via `NUM2SIZET`.
16
+ - `SecureBuffer#slice(offset, length)` rejects negative offset or length with
17
+ `ArgumentError` instead of wrapping to a huge unsigned value.
18
+ - `DER.read_tlv` rejects negative offset with `ArgumentError`.
19
+ - `DER::Cursor.new(data, pos)` rejects negative pos with `ArgumentError`.
20
+ - PEM label mismatch during IO streaming now raises `PEMError` (was `ParseError`).
21
+ - Truncated PEM over IO (BEGIN without matching END) now raises `PEMError`
22
+ with code `:pem_no_markers` instead of silently dropping the block.
23
+ - `DER.build_encrypted_pkcs8` raises `ArgumentError` when either argument is
24
+ `nil` instead of a confusing `NoMethodError`.
25
+
26
+ ### Changed
27
+
28
+ - `check_input_size!` is now a private method outside of `class << self`,
29
+ improving readability.
30
+
31
+ ## [0.1.0] — 2026-03-17
32
+
33
+ ### Added
34
+
35
+ - Algorithm-agnostic DER TLV read/write (`DER.read_tlv`, `DER.write_tlv` — private).
36
+ - SPKI encode/decode (`DER.build_spki`, `DER.parse_spki`).
37
+ - PKCS#8 / OneAsymmetricKey encode/decode (`DER.build_pkcs8`, `DER.parse_pkcs8`).
38
+ - EncryptedPrivateKeyInfo encode/decode (`DER.build_encrypted_pkcs8`,
39
+ `DER.parse_encrypted_pkcs8`) — codec only, no encryption/decryption.
40
+ - PEM encode/decode (`PEM.encode`, `PEM.decode`, `PEM.decode_auto`,
41
+ `PEM.decode_each`).
42
+ - `PEM.decode_each` accepts IO objects (or any `#each_line` responder) in
43
+ addition to Strings, reading line-by-line to avoid slurping the entire stream.
44
+ - `PEM::DecodeResult` — value object returned by `PEM.decode_auto` with
45
+ `#data`, `#label`, `#to_a`, `#to_h`, `#deconstruct_keys`, and `#==`.
46
+ Use `result.to_a` for explicit Array destructuring.
47
+ - Base64 encode/decode (RFC 4648, strict padding validation).
48
+ - Built-in OID constants for ML-DSA (FIPS 204), ML-KEM (FIPS 203), and
49
+ SLH-DSA (FIPS 205) — all 18 NIST PQC parameter sets.
50
+ - `OID.from_dotted` / `OID.to_dotted` — convert between dotted-decimal notation
51
+ and DER-encoded OID TLV bytes.
52
+ - `OID.name_for` — reverse lookup from OID to constant name.
53
+ - `OID.register(dotted, name, key_sizes: nil)` — register custom OIDs at
54
+ runtime; creates a named constant under `PqcAsn1::OID` and optionally extends
55
+ key size validation.
56
+ - `DER::KeyInfo` — immutable value object returned by `parse_spki` /
57
+ `parse_pkcs8` with `#oid`, `#parameters`, `#key`, `#public_key`, `#format`,
58
+ `#algorithm`, `#to_der`, `#to_pem`, and pattern-matching support.
59
+ - `DER::EncryptedKeyInfo` — value object for EncryptedPrivateKeyInfo structures.
60
+ - `DER::CompositeKeyInfo` — placeholder for future composite key support
61
+ (raises `NotImplementedError`).
62
+ - `DER.parse_auto` — detect SPKI vs PKCS#8 vs EncryptedPrivateKeyInfo by
63
+ structure and dispatch to the correct parser.
64
+ - `DER.parse_pem` — decode a PEM string and parse the contained DER structure;
65
+ uses the PEM label to select the parser.
66
+ - `DER.detect_format` — lightweight format detection returning `:spki`,
67
+ `:pkcs8`, `:encrypted_pkcs8`, or `nil`.
68
+ - `DER.build_spki` and `DER.build_pkcs8` accept `validate: true` keyword to
69
+ check key lengths against `DER::KEY_SIZES` before encoding.
70
+ - `DER.build_spki` and `DER.build_pkcs8` accept `parameters:` keyword for
71
+ AlgorithmIdentifier parameters and (PKCS#8 only) `public_key:` keyword for
72
+ the RFC 5958 publicKey field.
73
+ - `DER::KEY_SIZES` — frozen Hash mapping each built-in OID to
74
+ `{ public:, secret: }` expected byte counts.
75
+ - `DER::Cursor` — zero-copy DER reader as a first-class public API with
76
+ `#read`, `#read_raw`, `#read_sequence`, `#read_optional`, `#read_raw_optional`,
77
+ `#skip`, `#skip_optional`, `#peek_tag`, `#eof?`, `#remaining`, `#pos`, `#data`,
78
+ and convenience readers (`#read_integer`, `#read_oid`, `#read_octet_string`,
79
+ `#read_bit_string`).
80
+ - `SecureBuffer` — `mmap`-backed allocation with `mlock`, `mprotect` guard
81
+ pages, canary integrity checking, and secure zeroing on GC.
82
+ - `SecureBuffer#use { |bytes| }` — block-scoped access to secret key
83
+ bytes; the yielded String is securely zeroed after the block returns.
84
+ - `SecureBuffer#wipe!` — eagerly zero and mark the buffer as wiped.
85
+ - `SecureBuffer#slice(offset, length)` — extract a sub-range as a new
86
+ SecureBuffer.
87
+ - `SecureBuffer#to_pem(label = "PRIVATE KEY")` — PEM-encode DER contents
88
+ directly from a SecureBuffer.
89
+ - `SecureBuffer.random(n)` — fill a new buffer from the platform CSPRNG.
90
+ - `SecureBuffer.from_string(str)` — copy a String into a SecureBuffer.
91
+ - `SecureBuffer#==` — constant-time equality comparison.
92
+ - `DERError`, `PEMError`, `OIDError` — fine-grained error subclasses.
93
+ `DERError` and `PEMError` inherit from `ParseError` (backward-compatible);
94
+ `OIDError` inherits from `Error`.
95
+ - `Error#code` — fine-grained symbol (e.g. `:outer_sequence`, `:base64`)
96
+ identifying the exact failure point.
97
+ - `Error#category` — coarse programmatic error category (`:malformed_input`,
98
+ `:malformed_encoding`, `:validation`, `:system`).
99
+ - `Error#offset` — byte position in the input where a parse error was detected.
100
+ - `rake vendor:verify` — checks SHA-256 of vendored C files against pinned
101
+ checksums; fails loudly on mismatch.
102
+ - `rake vendor:lock` — records current SHA-256 digests after a deliberate
103
+ vendor upgrade.
104
+ - `rake vendor:concat` — re-vendor from a local libpqcasn1 checkout.
105
+ - `rake vendor:update[VERSION]` — re-vendor from a GitHub release tarball.
106
+ - `rake fixtures:generate` — regenerates test fixtures from scratch.
107
+ - No runtime dependencies; no OpenSSL.
108
+ - Vendored libpqcasn1 0.1.3.
109
+
110
+ ### Known limitations
111
+
112
+ - **No Windows support.** `SecureBuffer` uses POSIX `mmap`/`mprotect`/
113
+ `mlock`, which are unavailable on Windows. The gem will not compile on
114
+ Windows without a compatibility layer (e.g. Cygwin/MSYS2).
115
+ - Composite key support is not yet implemented (placeholder classes raise
116
+ `NotImplementedError`).
data/LICENSE ADDED
@@ -0,0 +1,14 @@
1
+ Copyright 2026 Suleyman Musayev
2
+
3
+ This project is dual-licensed. You may use, distribute, and modify it under
4
+ the terms of either:
5
+
6
+ * the MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT), or
7
+ * the Apache License, Version 2.0 (LICENSE-APACHE or
8
+ http://www.apache.org/licenses/LICENSE-2.0)
9
+
10
+ at your option.
11
+
12
+ Unless you explicitly state otherwise, any Contribution intentionally submitted
13
+ for inclusion in this project by you, as defined in the Apache-2.0 license,
14
+ shall be dual-licensed as above, without any additional terms or conditions.
data/LICENSE-APACHE ADDED
@@ -0,0 +1,185 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship made available under
36
+ the License, as indicated by a copyright notice that is included in
37
+ or attached to the work (an example is provided in the Appendix below).
38
+
39
+ "Derivative Works" shall mean any work, whether in Source or Object
40
+ form, that is based on (or derived from) the Work and for which the
41
+ editorial revisions, annotations, elaborations, or other modifications
42
+ represent, as a whole, an original work of authorship. For the purposes
43
+ of this License, Derivative Works shall not include works that remain
44
+ separable from, or merely link (or bind by name) to the interfaces of,
45
+ the Work and Derivative Works thereof.
46
+
47
+ "Contribution" shall mean, as submitted to the Licensor for inclusion
48
+ in the Work by the copyright owner or by an individual or Legal Entity
49
+ authorized to submit on behalf of the copyright owner. For the purposes
50
+ of this definition, "submitted" means any form of electronic, verbal,
51
+ or written communication sent to the Licensor or its representatives,
52
+ including but not limited to communication on electronic mailing lists,
53
+ source code control systems, and issue tracking systems that are managed
54
+ by, or on behalf of, the Licensor for the purpose of discussing and
55
+ improving the Work, but excluding communication that is conspicuously
56
+ marked or designated in writing by the copyright owner as
57
+ "Not a Contribution."
58
+
59
+ "Contributor" shall mean Licensor and any Legal Entity on behalf of
60
+ whom a Contribution has been received by the Licensor and subsequently
61
+ incorporated within the Work.
62
+
63
+ 2. Grant of Copyright License. Subject to the terms and conditions of
64
+ this License, each Contributor hereby grants to You a perpetual,
65
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
66
+ copyright license to reproduce, prepare Derivative Works of,
67
+ publicly display, publicly perform, sublicense, and distribute the
68
+ Work and such Derivative Works in Source or Object form.
69
+
70
+ 3. Grant of Patent License. Subject to the terms and conditions of
71
+ this License, each Contributor hereby grants to You a perpetual,
72
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
73
+ (except as stated in this section) patent license to make, have made,
74
+ use, offer to sell, sell, import, and otherwise transfer the Work,
75
+ where such license applies only to those patent claims licensable
76
+ by such Contributor that are necessarily infringed by their
77
+ Contribution(s) alone or by combination of their Contribution(s)
78
+ with the Work to which such Contribution(s) was submitted. If You
79
+ institute patent litigation against any entity (including a cross-claim
80
+ or counterclaim in a lawsuit) alleging that the Work or a Contribution
81
+ incorporated within the Work constitutes direct or contributory patent
82
+ infringement, then any patent licenses granted to You under this License
83
+ for that Work shall terminate as of the date such litigation is filed.
84
+
85
+ 4. Redistribution. You may reproduce and distribute copies of the
86
+ Work or Derivative Works thereof in any medium, with or without
87
+ modifications, and in Source or Object form, provided that You
88
+ meet the following conditions:
89
+
90
+ (a) You must give any other recipients of the Work or
91
+ Derivative Works a copy of this License; and
92
+
93
+ (b) You must cause any modified files to carry prominent notices
94
+ stating that You changed the files; and
95
+
96
+ (c) You must retain, in the Source form of any Derivative Works
97
+ that You distribute, all copyright, patent, trademark, and
98
+ attribution notices from the Source form of the Work,
99
+ excluding those notices that do not pertain to any part of
100
+ the Derivative Works; and
101
+
102
+ (d) If the Work includes a "NOTICE" text file as part of its
103
+ distribution, You must include a readable copy of the
104
+ attribution notices contained within such NOTICE file, in
105
+ at least one of the following places: within a NOTICE text file
106
+ distributed as part of the Derivative Works; within the Source
107
+ form or documentation, if provided along with the Derivative
108
+ Works; or, within a display generated by the Derivative Works,
109
+ if and wherever such third-party notices normally appear. The
110
+ contents of the NOTICE file are for informational purposes only
111
+ and do not modify the License. You may add Your own attribution
112
+ notices within Derivative Works that You distribute, alongside
113
+ or as an addendum to the NOTICE text from the Work, provided
114
+ that such additional attribution notices cannot be construed
115
+ as modifying the License.
116
+
117
+ You may add Your own license statement for Your modifications and
118
+ may provide additional grant of rights to use, reproduce, modify,
119
+ prepare Derivative Works of, publicly display, publicly perform,
120
+ sublicense, and distribute those modifications and additional rights
121
+ as a separate license, provided Your use, reproduction, modification,
122
+ preparation of Derivative Works, distribution, and other activities
123
+ remain in full compliance with the terms of this License.
124
+
125
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
126
+ any Contribution intentionally submitted for inclusion in the Work
127
+ by You to the Licensor shall be under the terms and conditions of
128
+ this License, without any additional terms or conditions.
129
+ Notwithstanding the above, nothing herein shall supersede or modify
130
+ the terms of any separate license agreement you may have executed
131
+ with Licensor regarding such Contributions.
132
+
133
+ 6. Trademarks. This License does not grant permission to use the trade
134
+ names, trademarks, service marks, or product names of the Licensor,
135
+ except as required for reasonable and customary use in describing the
136
+ origin of the Work and reproducing the content of the NOTICE file.
137
+
138
+ 7. Disclaimer of Warranty. Unless required by applicable law or
139
+ agreed to in writing, Licensor provides the Work (and each
140
+ Contributor provides its Contributions) on an "AS IS" BASIS,
141
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
142
+ implied, including, without limitation, any warranties or conditions
143
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
144
+ PARTICULAR PURPOSE. You are solely responsible for determining the
145
+ appropriateness of using or reproducing the Work and assume any
146
+ risks associated with Your exercise of permissions under this License.
147
+
148
+ 8. Limitation of Liability. In no event and under no legal theory,
149
+ whether in tort (including negligence), contract, or otherwise,
150
+ unless required by applicable law (such as deliberate and grossly
151
+ negligent acts) or agreed to in writing, shall any Contributor be
152
+ liable to You for damages, including any direct, indirect, special,
153
+ incidental, or exemplary damages of any character arising as a
154
+ result of this License or out of the use or inability to use the
155
+ Work (including but not limited to damages for loss of goodwill,
156
+ work stoppage, computer failure or malfunction, or all other
157
+ commercial damages or losses), even if such Contributor has been
158
+ advised of the possibility of such damages.
159
+
160
+ 9. Accepting Warranty or Additional Liability. While redistributing
161
+ the Work or Derivative Works thereof, You may choose to offer,
162
+ and charge a fee for, acceptance of support, warranty, indemnity,
163
+ or other liability obligations and/or rights consistent with this
164
+ License. However, in accepting such obligations, You may act only
165
+ on Your own behalf and on Your sole responsibility, not on behalf
166
+ of any other Contributor, and only if You agree to indemnify,
167
+ defend, and hold each Contributor harmless for any liability
168
+ incurred by, or claims asserted against, such Contributor by reason
169
+ of your accepting any such warranty or additional liability.
170
+
171
+ END OF TERMS AND CONDITIONS
172
+
173
+ Copyright 2026 Suleyman Musayev
174
+
175
+ Licensed under the Apache License, Version 2.0 (the "License");
176
+ you may not use this file except in compliance with the License.
177
+ You may obtain a copy of the License at
178
+
179
+ http://www.apache.org/licenses/LICENSE-2.0
180
+
181
+ Unless required by applicable law or agreed to in writing, software
182
+ distributed under the License is distributed on an "AS IS" BASIS,
183
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
184
+ See the License for the specific language governing permissions and
185
+ limitations under the License.
data/LICENSE-MIT ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Suleyman Musayev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,267 @@
1
+ # pqc_asn1
2
+
3
+ Algorithm-agnostic ASN.1/DER/PEM/Base64 codec for post-quantum cryptography key
4
+ serialization. Implemented in C with no OpenSSL dependency.
5
+
6
+ Handles SPKI (SubjectPublicKeyInfo, RFC 5480) and PKCS#8 / OneAsymmetricKey
7
+ (RFC 5958) structures for ML-DSA, ML-KEM, SLH-DSA, and any future scheme that
8
+ uses standard key-wrapping formats.
9
+
10
+ ## Installation
11
+
12
+ Add to your Gemfile:
13
+
14
+ ```ruby
15
+ gem "pqc_asn1"
16
+ ```
17
+
18
+ Or install directly:
19
+
20
+ ```sh
21
+ gem install pqc_asn1
22
+ ```
23
+
24
+ The gem builds a native C extension — a C compiler is required.
25
+
26
+ ## Usage
27
+
28
+ ### Public key (SPKI)
29
+
30
+ ```ruby
31
+ require "pqc_asn1"
32
+
33
+ # Encode
34
+ der = PqcAsn1::DER.build_spki(PqcAsn1::OID::ML_DSA_44, public_key_bytes)
35
+ pem = PqcAsn1::PEM.encode(der, "PUBLIC KEY")
36
+
37
+ # Decode
38
+ info = PqcAsn1::DER.parse_spki(der)
39
+ info.algorithm # => "ML_DSA_44"
40
+ info.key # => binary String (public key bytes)
41
+ info.to_der # => original DER bytes
42
+ info.to_pem # => PEM string with "PUBLIC KEY" label
43
+ ```
44
+
45
+ ### Private key (PKCS#8)
46
+
47
+ ```ruby
48
+ # build_pkcs8 returns a SecureBuffer — memory is locked and securely zeroed on GC
49
+ der = PqcAsn1::DER.build_pkcs8(PqcAsn1::OID::ML_DSA_44, secret_key_bytes)
50
+ pem = PqcAsn1::PEM.encode(der, "PRIVATE KEY")
51
+
52
+ # SecureBuffer convenience
53
+ der.to_pem # => PEM string with "PRIVATE KEY" label
54
+
55
+ # Decode
56
+ info = PqcAsn1::DER.parse_pkcs8(der)
57
+ info.algorithm # => "ML_DSA_44"
58
+
59
+ # Use block-scoped access — bytes are securely zeroed when the block exits
60
+ info.key.use { |bytes| sign(bytes) }
61
+
62
+ # Or wipe manually
63
+ info.key.wipe!
64
+ ```
65
+
66
+ ### Key size validation
67
+
68
+ ```ruby
69
+ # Raises ArgumentError if the key length doesn't match the OID
70
+ der = PqcAsn1::DER.build_spki(PqcAsn1::OID::ML_DSA_44, pk, validate: true)
71
+ der = PqcAsn1::DER.build_pkcs8(PqcAsn1::OID::ML_DSA_44, sk, validate: true)
72
+ ```
73
+
74
+ ### Auto-detection
75
+
76
+ ```ruby
77
+ # Detects SPKI vs PKCS#8 vs EncryptedPrivateKeyInfo automatically
78
+ info = PqcAsn1::DER.parse_auto(der_bytes)
79
+ info.format # => :spki, :pkcs8, or :encrypted_pkcs8
80
+
81
+ # PEM decode with auto-detection (uses PEM label to select parser)
82
+ info = PqcAsn1::DER.parse_pem(pem_string)
83
+ ```
84
+
85
+ ### PEM encoding / decoding
86
+
87
+ ```ruby
88
+ # Encode arbitrary bytes
89
+ pem = PqcAsn1::PEM.encode(bytes, "PUBLIC KEY")
90
+
91
+ # Decode — returns a DecodeResult
92
+ result = PqcAsn1::PEM.decode_auto(pem)
93
+ result.label # => "PUBLIC KEY"
94
+ result.data # => DER bytes
95
+
96
+ # Explicit destructuring via .to_a
97
+ data, label = PqcAsn1::PEM.decode_auto(pem).to_a
98
+
99
+ # Iterate multiple PEM blocks in a String or IO stream
100
+ PqcAsn1::PEM.decode_each(pem_string) { |r| process(r.data, r.label) }
101
+
102
+ # IO streaming — reads line-by-line without slurping the file
103
+ File.open("keys.pem") do |f|
104
+ PqcAsn1::PEM.decode_each(f) { |r| process(r.data, r.label) }
105
+ end
106
+
107
+ # Without a block — returns an Enumerator
108
+ PqcAsn1::PEM.decode_each(pem_string).map(&:data)
109
+ ```
110
+
111
+ ### Pattern matching (Ruby 2.7+)
112
+
113
+ ```ruby
114
+ case PqcAsn1::DER.parse_spki(der)
115
+ in { oid:, key: }
116
+ store_key(oid.name, key)
117
+ end
118
+ ```
119
+
120
+ ### OID utilities
121
+
122
+ ```ruby
123
+ # Dotted-decimal <-> DER TLV
124
+ der_tlv = PqcAsn1::OID.from_dotted("2.16.840.1.101.3.4.3.17")
125
+ PqcAsn1::OID.to_dotted(der_tlv) # => "2.16.840.1.101.3.4.3.17"
126
+
127
+ # Name lookup
128
+ PqcAsn1::OID.name_for(der_tlv) # => "ML_DSA_44"
129
+
130
+ # OID value objects
131
+ oid = PqcAsn1::OID.new("2.16.840.1.101.3.4.3.17")
132
+ oid.name # => "ML_DSA_44"
133
+ oid.dotted # => "2.16.840.1.101.3.4.3.17"
134
+ ```
135
+
136
+ ### Registering custom OIDs at runtime
137
+
138
+ Registration is thread-safe (protected by a Mutex).
139
+
140
+ ```ruby
141
+ oid = PqcAsn1::OID.register(
142
+ "1.3.9999.1",
143
+ "MY_ALGO_128",
144
+ key_sizes: { public: 1312, secret: 2528 }
145
+ )
146
+
147
+ # Look up by name or dotted string — no constant is defined automatically
148
+ PqcAsn1::OID["MY_ALGO_128"] # => OID value object
149
+ PqcAsn1::OID["1.3.9999.1"] # => same object
150
+ PqcAsn1::DER.build_spki(oid, pk, validate: true) # key size validation works
151
+
152
+ # To define a constant, assign explicitly:
153
+ PqcAsn1::OID::MY_ALGO_128 = oid
154
+ ```
155
+
156
+ ### SecureBuffer
157
+
158
+ ```ruby
159
+ # Build a PKCS#8 key — returns a SecureBuffer
160
+ der = PqcAsn1::DER.build_pkcs8(PqcAsn1::OID::ML_DSA_44, secret_key_bytes)
161
+
162
+ # Block-scoped access — bytes zeroed after the block
163
+ der.use { |bytes| sign(bytes) }
164
+
165
+ # Write DER bytes directly to an IO (minimises heap exposure)
166
+ File.open("key.der", "wb") { |f| der.write_to(f) }
167
+
168
+ # PEM-encode directly to an IO
169
+ File.open("key.pem", "w") { |f| der.to_pem_io(f) }
170
+
171
+ # Wipe returns self for chaining
172
+ der.wipe!.wiped? # => true
173
+
174
+ # Generate random secure bytes from the platform CSPRNG
175
+ nonce = PqcAsn1::SecureBuffer.random(32)
176
+
177
+ # Extract a sub-range as a new SecureBuffer
178
+ slice = der.slice(0, 64)
179
+ ```
180
+
181
+ ### Input size limits
182
+
183
+ ```ruby
184
+ # Default: 1 MiB — protects parse methods against oversized input
185
+ PqcAsn1::DER.max_input_size # => 1048576
186
+
187
+ # Adjust or disable
188
+ PqcAsn1::DER.max_input_size = 4 << 20 # 4 MiB
189
+ PqcAsn1::DER.max_input_size = nil # no limit
190
+ ```
191
+
192
+ ### Error handling
193
+
194
+ ```ruby
195
+ begin
196
+ PqcAsn1::DER.parse_spki(bad_bytes)
197
+ rescue PqcAsn1::DERError => e
198
+ # DER structure is malformed
199
+ puts e.category # => :malformed_input
200
+ puts e.code # => :outer_sequence (fine-grained symbol)
201
+ puts e.offset # => byte position where error was detected
202
+ rescue PqcAsn1::PEMError => e
203
+ # Base64 / PEM armor problem
204
+ rescue PqcAsn1::OIDError => e
205
+ # Unrecognised or invalid OID
206
+ rescue PqcAsn1::ParseError => e
207
+ # Catches both DERError and PEMError
208
+ rescue PqcAsn1::Error => e
209
+ # Top-level catch-all
210
+ end
211
+ ```
212
+
213
+ Error categories:
214
+
215
+ | Category | Meaning |
216
+ |---|---|
217
+ | `:malformed_input` | DER TLV structure is wrong |
218
+ | `:malformed_encoding` | Base64 or PEM armor is invalid |
219
+ | `:validation` | Key size or OID mismatch |
220
+ | `:system` | Memory allocation failure |
221
+
222
+ ## OID constants
223
+
224
+ `PqcAsn1::OID` contains value objects for all current NIST PQC algorithms:
225
+
226
+ | Constant | Algorithm | OID |
227
+ |---|---|---|
228
+ | `ML_DSA_44` | ML-DSA (FIPS 204) | 2.16.840.1.101.3.4.3.17 |
229
+ | `ML_DSA_65` | ML-DSA (FIPS 204) | 2.16.840.1.101.3.4.3.18 |
230
+ | `ML_DSA_87` | ML-DSA (FIPS 204) | 2.16.840.1.101.3.4.3.19 |
231
+ | `ML_KEM_512` | ML-KEM (FIPS 203) | 2.16.840.1.101.3.4.4.1 |
232
+ | `ML_KEM_768` | ML-KEM (FIPS 203) | 2.16.840.1.101.3.4.4.2 |
233
+ | `ML_KEM_1024` | ML-KEM (FIPS 203) | 2.16.840.1.101.3.4.4.3 |
234
+ | `SLH_DSA_SHA2_128S` | SLH-DSA (FIPS 205) | 2.16.840.1.101.3.4.3.20 |
235
+ | `SLH_DSA_SHA2_128F` | SLH-DSA (FIPS 205) | 2.16.840.1.101.3.4.3.21 |
236
+ | `SLH_DSA_SHA2_192S` | SLH-DSA (FIPS 205) | 2.16.840.1.101.3.4.3.22 |
237
+ | `SLH_DSA_SHA2_192F` | SLH-DSA (FIPS 205) | 2.16.840.1.101.3.4.3.23 |
238
+ | `SLH_DSA_SHA2_256S` | SLH-DSA (FIPS 205) | 2.16.840.1.101.3.4.3.24 |
239
+ | `SLH_DSA_SHA2_256F` | SLH-DSA (FIPS 205) | 2.16.840.1.101.3.4.3.25 |
240
+ | `SLH_DSA_SHAKE_128S` | SLH-DSA (FIPS 205) | 2.16.840.1.101.3.4.3.26 |
241
+ | `SLH_DSA_SHAKE_128F` | SLH-DSA (FIPS 205) | 2.16.840.1.101.3.4.3.27 |
242
+ | `SLH_DSA_SHAKE_192S` | SLH-DSA (FIPS 205) | 2.16.840.1.101.3.4.3.28 |
243
+ | `SLH_DSA_SHAKE_192F` | SLH-DSA (FIPS 205) | 2.16.840.1.101.3.4.3.29 |
244
+ | `SLH_DSA_SHAKE_256S` | SLH-DSA (FIPS 205) | 2.16.840.1.101.3.4.3.30 |
245
+ | `SLH_DSA_SHAKE_256F` | SLH-DSA (FIPS 205) | 2.16.840.1.101.3.4.3.31 |
246
+
247
+ ## Key size reference
248
+
249
+ `PqcAsn1::DER::KEY_SIZES` maps each OID to `{ public:, secret: }` byte counts.
250
+ Used automatically when `validate: true` is passed to `build_spki` / `build_pkcs8`.
251
+
252
+ ## Requirements
253
+
254
+ - Ruby >= 2.7.2
255
+ - A C compiler (the gem builds a native extension)
256
+ - POSIX platform (Linux, macOS). Windows is not supported — `SecureBuffer`
257
+ requires `mmap`/`mprotect`/`mlock` which are unavailable on native Windows
258
+
259
+ ## License
260
+
261
+ Licensed under either of
262
+
263
+ - MIT License ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
264
+ - Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
265
+
266
+ at your option. Contributions are dual-licensed under the same terms unless
267
+ explicitly stated otherwise.