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



26
27
28
29
30
31
32
# File 'ext/pqcrypto/extconf.rb', line 26

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'ext/pqcrypto/extconf.rb', line 91

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'ext/pqcrypto/extconf.rb', line 51

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



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'ext/pqcrypto/extconf.rb', line 126

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_dir = File.join(pqclean_dir, "crypto_kem", "ml-kem-768", "clean")
  mldsa_dir = File.join(pqclean_dir, "crypto_sign", "ml-dsa-65", "clean")
  common_dir = File.join(pqclean_dir, "common")

  keccak_config = configure_keccak_backend(vendor_dir, common_dir)

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

  mlkem_sources = Dir.glob(File.join(mlkem_dir, "*.c")).sort
  mldsa_sources = Dir.glob(File.join(mldsa_dir, "*.c")).sort
  common_sources = %w[sha2.c sp800-185.c].map { |name| File.join(common_dir, name) }

  source_groups = [
    ["pqclean_mlkem", mlkem_sources],
    ["pqclean_mldsa", mldsa_sources],
    ["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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'ext/pqcrypto/extconf.rb', line 34

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

#inject_pqclean_sources!(pqclean_config) ⇒ Object



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
197
198
199
# File 'ext/pqcrypto/extconf.rb', line 164

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