Top Level Namespace

Defined Under Namespace

Modules: PQCrypto

Constant Summary collapse

USE_SYSTEM =
arg_config("--use-system-libraries") || ENV["PQCRYPTO_USE_SYSTEM_LIBRARIES"]
SANITIZE =
ENV["PQCRYPTO_SANITIZE"]

Instance Method Summary collapse

Instance Method Details

#configure_compiler_environmentObject



23
24
25
26
27
28
29
# File 'ext/pqcrypto/extconf.rb', line 23

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_openssl!Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'ext/pqcrypto/extconf.rb', line 48

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/kdf.h is required" unless have_header("openssl/kdf.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)

  $CFLAGS << " -DHAVE_OPENSSL_EVP_H -DHAVE_OPENSSL_RAND_H -DHAVE_OPENSSL_KDF_H"
end

#configure_pqclean(vendor_dir) ⇒ Object



70
71
72
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
# File 'ext/pqcrypto/extconf.rb', line 70

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")

  include_dirs = [mlkem_dir, mldsa_dir, common_dir]
  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[fips202.c sha2.c sp800-185.c randombytes.c].map { |name| File.join(common_dir, name) }

  source_groups = [
    ["pqclean_mlkem", mlkem_sources],
    ["pqclean_mldsa", mldsa_sources],
    ["pqclean_common", common_sources]
  ]

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

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

  {
    include_dirs: include_dirs,
    source_groups: source_groups
  }
end

#find_vendor_dirObject



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

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



104
105
106
107
108
109
110
111
112
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
# File 'ext/pqcrypto/extconf.rb', line 104

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) $(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