Top Level Namespace

Defined Under Namespace

Modules: PQCrypto

Constant Summary collapse

VENDOR_ONLY_CFLAGS =
"-Wno-unused-parameter -Wno-unused-function -Wno-strict-prototypes -Wno-pedantic -Wno-c23-extensions -Wno-undef"
USE_SYSTEM =
arg_config("--use-system-libraries") || ENV["PQCRYPTO_USE_SYSTEM_LIBRARIES"]
KECCAK_BACKEND =
(ENV["PQCRYPTO_KECCAK_BACKEND"] || "clean").strip.downcase
SUPPORTED_KECCAK_BACKENDS =
%w[clean xkcp].freeze
SANITIZE =
ENV["PQCRYPTO_SANITIZE"]

Instance Method Summary collapse

Instance Method Details

#configure_compiler_environmentObject



48
49
50
51
52
53
54
# File 'ext/pqcrypto/extconf.rb', line 48

def configure_compiler_environment
  return unless RUBY_PLATFORM.include?("darwin")

  dir_config("homebrew", "/opt/homebrew")
  $CPPFLAGS << " -I/opt/homebrew/include"
  $LDFLAGS << " -L/opt/homebrew/lib"
end

#configure_keccak_backend(vendor_dir, common_dir) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'ext/pqcrypto/extconf.rb', line 113

def configure_keccak_backend(vendor_dir, common_dir)
  abort "Unsupported PQCRYPTO_KECCAK_BACKEND=#{KECCAK_BACKEND.inspect}. Supported: #{SUPPORTED_KECCAK_BACKENDS.join(", ")}" unless SUPPORTED_KECCAK_BACKENDS.include?(KECCAK_BACKEND)

  case KECCAK_BACKEND
  when "clean"
    {
      name: "clean",
      include_dirs: [],
      source_group: ["pqclean_common", [File.join(common_dir, "fips202.c")]]
    }
  when "xkcp"
    # The optimized backend must provide the same fips202.h-compatible API as
    # PQClean's common/fips202.c. Do not substitute OpenSSL EVP SHAKE here: the
    # PQClean SHAKE state layout is part of the ML-KEM/ML-DSA call graph.
    xkcp_dir = File.join(vendor_dir, "xkcp")
    adapter_source = File.join(xkcp_dir, "pqclean_fips202_xkcp.c")

    abort <<~MSG unless File.exist?(adapter_source)
      PQCRYPTO_KECCAK_BACKEND=xkcp was requested, but no reviewed XKCP adapter was found.

      Expected:
        #{adapter_source}

      Refusing to fall back silently to the clean backend. Vendor a fips202.h-compatible
      XKCP adapter first, then run the full SHAKE-dependent KAT/regression test matrix.
    MSG

    {
      name: "xkcp",
      include_dirs: [xkcp_dir],
      source_group: ["xkcp_keccak", [adapter_source]]
    }
  end
end

#configure_openssl!Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'ext/pqcrypto/extconf.rb', line 73

def configure_openssl!
  configure_compiler_environment

  abort "OpenSSL libcrypto is required" unless have_library("crypto")
  abort "OpenSSL libssl is required" unless have_library("ssl")
  abort "openssl/evp.h is required" unless have_header("openssl/evp.h")
  abort "openssl/rand.h is required" unless have_header("openssl/rand.h")
  abort "openssl/crypto.h is required" unless have_header("openssl/crypto.h")

  version_check = <<~SRC
    #include <openssl/opensslv.h>
    #if OPENSSL_VERSION_NUMBER < 0x30000000L
    #error "OpenSSL 3.0 or later is required"
    #endif
    int main(void) { return 0; }
  SRC

  abort "OpenSSL 3.0 or later is required" unless try_compile(version_check)

  sha3_check = <<~SRC
    #include <openssl/evp.h>
    int main(void) {
        const EVP_MD *md = EVP_sha3_256();
        return md == NULL ? 1 : 0;
    }
  SRC
  abort "OpenSSL SHA3-256 is required (X-Wing combiner)" unless try_compile(sha3_check)

  shake_check = <<~SRC
    #include <openssl/evp.h>
    int main(void) {
        const EVP_MD *md = EVP_shake256();
        return md == NULL ? 1 : 0;
    }
  SRC
  abort "OpenSSL SHAKE256 is required (X-Wing key expansion)" unless try_compile(shake_check)

  $CFLAGS << " -DHAVE_OPENSSL_EVP_H -DHAVE_OPENSSL_RAND_H"
end

#configure_pqclean(vendor_dir) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'ext/pqcrypto/extconf.rb', line 148

def configure_pqclean(vendor_dir)
  return nil unless vendor_dir

  pqclean_dir = File.join(vendor_dir, "pqclean")
  return nil unless Dir.exist?(pqclean_dir)

  mlkem_dirs = {
    "pqclean_mlkem512" => File.join(pqclean_dir, "crypto_kem", "ml-kem-512", "clean"),
    "pqclean_mlkem768" => File.join(pqclean_dir, "crypto_kem", "ml-kem-768", "clean"),
    "pqclean_mlkem1024" => File.join(pqclean_dir, "crypto_kem", "ml-kem-1024", "clean")
  }
  mldsa_dirs = {
    "pqclean_mldsa44" => File.join(pqclean_dir, "crypto_sign", "ml-dsa-44", "clean"),
    "pqclean_mldsa65" => File.join(pqclean_dir, "crypto_sign", "ml-dsa-65", "clean"),
    "pqclean_mldsa87" => File.join(pqclean_dir, "crypto_sign", "ml-dsa-87", "clean")
  }
  common_dir = File.join(pqclean_dir, "common")

  keccak_config = configure_keccak_backend(vendor_dir, common_dir)

  include_dirs = [*mlkem_dirs.values, *mldsa_dirs.values, common_dir, *keccak_config[:include_dirs]]
  return nil unless include_dirs.all? { |dir| Dir.exist?(dir) }

  mlkem_source_groups = mlkem_dirs.map do |prefix, dir|
    [prefix, Dir.glob(File.join(dir, "*.c")).sort]
  end
  mldsa_source_groups = mldsa_dirs.map do |prefix, dir|
    [prefix, Dir.glob(File.join(dir, "*.c")).sort]
  end
  common_sources = %w[sha2.c sp800-185.c].map { |name| File.join(common_dir, name) }

  source_groups = [
    *mlkem_source_groups,
    *mldsa_source_groups,
    ["pqclean_common", common_sources],
    keccak_config[:source_group]
  ]

  return nil unless source_groups.all? { |_, sources| sources.all? { |path| File.exist?(path) } }

  $CFLAGS << " -DHAVE_PQCLEAN"
  include_dirs.each { |dir| $CPPFLAGS << " -I#{dir}" }

  {
    include_dirs: include_dirs,
    keccak_backend: keccak_config[:name],
    source_groups: source_groups
  }
end

#find_vendor_dirObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'ext/pqcrypto/extconf.rb', line 56

def find_vendor_dir
  candidates = [
    File.join(__dir__, "vendor"),
    File.expand_path("../../ext/pqcrypto/vendor", __dir__),
    File.join(Dir.pwd, "ext", "pqcrypto", "vendor")
  ]

  dir = __dir__
  6.times do
    candidates << File.join(dir, "ext", "pqcrypto", "vendor")
    dir = File.dirname(dir)
  end

  candidates.find { |path| File.exist?(File.join(path, ".vendored")) }
            &.then { |path| File.expand_path(path) }
end

#generate_version_header!Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'ext/pqcrypto/extconf.rb', line 7

def generate_version_header!
  version = PQCrypto::VERSION
  unless version.match?(/\A[0-9A-Za-z][0-9A-Za-z._+-]*\z/)
    abort "Invalid PQCrypto::VERSION for C header: #{version.inspect}"
  end

  header = File.join(__dir__, "pqcrypto_version.h")
  File.write(header, <<~C)
    /* Generated by extconf.rb from lib/pq_crypto/version.rb. Do not edit. */
    #ifndef PQCRYPTO_VERSION_H
    #define PQCRYPTO_VERSION_H

    #define PQCRYPTO_VERSION #{version.dump}

    #endif
  C
end

#inject_pqclean_sources!(pqclean_config) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'ext/pqcrypto/extconf.rb', line 198

def inject_pqclean_sources!(pqclean_config)
  return unless pqclean_config

  makefile = File.read("Makefile")

  vendor_objects = []
  build_rules = []

  pqclean_config[:source_groups].each do |prefix, sources|
    sources.each do |source|
      base = File.basename(source, ".c").tr("-", "_")
      object = "#{prefix}_#{base}.o"
      vendor_objects << object
      build_rules << <<~RULE
        #{object}: #{source}
        	$(ECHO) compiling #{source}
        	$(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) #{VENDOR_ONLY_CFLAGS} $(COUTFLAG)$@ -c $(CSRCFLAG)$<
      RULE
    end
  end

  objects_line = makefile.lines.find { |line| line.start_with?("OBJS = ") }
  raise "Could not find OBJS line in generated Makefile" unless objects_line

  makefile.sub!(objects_line, objects_line.chomp + " #{vendor_objects.join(' ')}\n")

  unless makefile.include?("# vendored pqclean objects")
    rules_block = "\n# vendored pqclean objects\n" + build_rules.join("\n") + "\n"
    anchor = "$(OBJS): $(HDRS) $(ruby_headers)\n"
    raise "Could not find OBJS dependency anchor in generated Makefile" unless makefile.include?(anchor)

    makefile.sub!(anchor, anchor + rules_block)
  end

  File.write("Makefile", makefile)
end