Skip to content

Commit

Permalink
Extend supported algorithms
Browse files Browse the repository at this point in the history
Updated based on latest BouncyCastle provider. TODO: Examine
availability of HMAC variants.
  • Loading branch information
xsc committed Sep 17, 2024
1 parent 76657f2 commit eb80122
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 21 deletions.
88 changes: 75 additions & 13 deletions src/pandect/codegen.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,54 @@
[signature-generator :refer [signature-generator]]]))

(def ^:private algorithms-names
(-> '{gost "GOST3411"
whirlpool "Whirlpool"
adler32 "ADLER-32"
crc32 "CRC-32"
tiger "Tiger"
siphash "Siphash-2-4"
siphash48 "Siphash-4-8"}
(-> '{gost "GOST3411"
whirlpool "Whirlpool"
adler32 "ADLER-32"
crc32 "CRC-32"
tiger "Tiger"
parallelhash128-256 "PARALLELHASH128-256"
parallelhash256-512 "PARALLELHASH256-512"
siphash "Siphash-2-4"
siphash48 "Siphash-4-8"
shake128-256 "SHAKE128-256"
shake256-512 "SHAKE256-512"
sm3 "SM3"
tuplehash128-256 "TUPLEHASH128-256"
tuplehash256-512 "TUPLEHASH256-512"
}

;; BLAKE2B
(into
(for [length [160 256 384 512]]
[(as-sym 'blake2b- length) (str "BLAKE2B-" length)]))

;; BLAKE2S
(into
(for [length [128 160 224 256]]
[(as-sym 'blake2s- length) (str "BLAKE2S-" length)]))

;; BLAKE3
(into
(for [length [256]]
[(as-sym 'blake3- length) (str "BLAKE3-" length)]))

;; HARAKA
(into
(for [length [256 512]]
[(as-sym 'haraka- length) (str "HARAKA-" length)]))

;; KECCAK
(into
(for [length [224 256 384 512]]
(for [length [224 256 288 384 512]]
[(as-sym 'keccak- length) (str "Keccak-" length)]))

;; BLAKE2B
;; KUPYNA
(into
(for [length [160 256 384 512]]
[(as-sym 'blake2b- length) (str "BLAKE2B-" length)]))
(for [length [256 384 512]]
[(as-sym 'kupyna- length) (str "DSTU7564-" length)]))
(into
(for [length [256 384 512]]
[(as-sym 'dstu7464- length) (str "DSTU7564-" length)]))

;; MDx
(into
Expand All @@ -45,7 +76,38 @@
;; SHA-3
(into
(for [length [224 256 384 512]]
[(as-sym 'sha3- length) (str "SHA3-" length)]))))
[(as-sym 'sha3- length) (str "SHA3-" length)]))

;; SHA-512
(into
(for [length [224 256]]
[(as-sym 'sha512- length) (str "SHA-512/" length)]))

;; SKEIN-256
(into
(for [length [128 160 224 256]]
[(as-sym 'skein256- length) (str "SKEIN-256-" length)]))

;; SKEIN-512
(into
(for [length [128 160 224 256 384 512]]
[(as-sym 'skein512- length) (str "SKEIN-512-" length)]))

;; SKEIN-1024
(into
(for [length [384 512 1024]]
[(as-sym 'skein1024- length) (str "SKEIN-1024-" length)]))))


(defn missing-algorithms
[]
(->> (java.security.Security/getAlgorithms "MessageDigest")
(remove #(re-matches #"^.*\d\.\d.+" %))
(remove (comp (set (sort (map clojure.string/lower-case (vals algorithms-names))))

Check warning on line 106 in src/pandect/codegen.clj

View workflow job for this annotation

GitHub Actions / clj-kondo

src/pandect/codegen.clj#L106

[unresolved-namespace] Unresolved namespace clojure.string. Are you missing a require?
clojure.string/lower-case))
(sort)))

(missing-algorithms)

(def ^:private generators
[hash-generator
Expand Down Expand Up @@ -85,7 +147,7 @@
(fn [[sym algorithm]]
(let [filename (.replace (format "%s.clj" sym) \- \_)]
(locking *out*
(println "[codegen] *" algorithm))
(println "[codegen] *" sym '-> algorithm))
(doto (file base "pandect" "algo" filename)
(-> .getParentFile .mkdirs)
(write-source! sym algorithm)))))
Expand Down
58 changes: 50 additions & 8 deletions src/pandect/impl/bouncy_castle.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,74 @@
(vector
["GOST3411" "Hmac-GOST3411"]
["MD4" "Hmac-MD4"]
["PARALLELHASH128-256"]
["PARALLELHASH256-512"]
["SHA-224" "Hmac-SHA224"]
["SHAKE128-256"]
["SHAKE256-512"]
["SM3"]
["Tiger" "Hmac-Tiger"]
["Whirlpool" "Hmac-Whirlpool"])

;; KECCAK
(for [length [224 256 384 512]]
[(str "Keccak-" length) (str "Hmac-Keccak" length)])
["TUPLEHASH128-256"]
["TUPLEHASH256-512"]
["Whirlpool" "Hmac-Whirlpool"]
)

;; BLAKE2B
(for [length [160 256 384 512]]
[(str "BLAKE2B-" length)])

;; BLAKE2S
(for [length [128 160 224 256]]
[(str "BLAKE2S-" length)])

;; BLAKE3
(for [length [256]]
[(str "BLAKE3-" length)])

;; DSTU7564
(for [length [256 384 512]]
[(str "DSTU7564-" length)])

;; HARAKA-256
(for [length [256 512]]
[(str "HARAKA-" length)])

;; KECCAK
(for [length [224 256 288 384 512]]
[(str "Keccak-" length) (str "Hmac-Keccak" length)])

;; RipeMD
(for [rmd-length [128 160 256 320]]
[(str "RipeMD" rmd-length) (str "Hmac-RipeMD" rmd-length)])

;; SHA-3
(for [sha3-length [224 256 384 512]]
[(str "SHA3-" sha3-length)])))
[(str "SHA3-" sha3-length)])

;; SHA-512
(for [length [224 256]]
[(str "SHA-512/" length)])

;; SKEIN-256
(for [length [128 160 224 256]]
[(str "SKEIN-256-" length)])

;; SKEIN-512
(for [length [128 160 224 256 384 512]]
[(str "SKEIN-512-" length)])

;; SKEIN-1024
(for [length [384 512 1024]]
[(str "SKEIN-1024-" length)])

))

(doseq [[hash-algorithm
hmac-algorithm] MD_ALGORITHMS]
(gen/register-algorithm!
{:name hash-algorithm
:requires '(pandect.utils.bouncy-castle-provider)
:docstring "(requires `org.bouncycastle/bcprov-jdk15on` to be on the classpath)"}
:docstring "(requires `org.bouncycastle/bcprov-jdk18on` to be on the classpath)"}
(hash/make hash-algorithm)
(hmac/make hmac-algorithm)))

Expand All @@ -52,5 +94,5 @@
(gen/register-algorithm!
{:name hmac-algorithm
:requires '(pandect.utils.bouncy-castle-provider)
:docstring "(requires `org.bouncycastle/bcprov-jdk15on` to be on the classpath)"}
:docstring "(requires `org.bouncycastle/bcprov-jdk18on` to be on the classpath)"}
(hmac/make-exclusive hmac-algorithm)))

0 comments on commit eb80122

Please sign in to comment.