Home /Modules/crypto/SLHmac.sleela

SLHmac.sleela

lib/crypto/SLHmac.sleela · 112 line(s) · sleela

<< lib/crypto/SLHmac.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines the SLeeLa standard-library type SLHmac used within the crypto library family. >>
#sleela 2.5
material;
< Unified SLeeLa SDK front-end object. >
< Family: crypto. HMAC (FIPS 198-1 / RFC 2104) over SHA-256 or SHA-512. Native impl crosses the VM/OS bridge to /crypto. >
<  >
< SECURITY NOTE: the native /crypto backend is a correct, specification-faithful >
< REFERENCE implementation. It is NOT guaranteed constant-time and is NOT >
< side-channel resistant. For data that must be protected use an audited library >
< (BoringSSL, OpenSSL, libsodium, liboqs). See crypto/README.md. >
class SLHmac {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 452;
  String metaAuthor = "Max Rupplin vs. Dr. Mrs. Elisabeth Harkins";
  String metaDate = "2026-10-16";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-09-28";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 9;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }
  < Status codes mirror slcrypto_status in crypto/include/slcrypto.h. >
  static final int OK = 0;
  static final int ERR_PARAM = -1;
  < MAC verification failed >
  static final int ERR_AUTH = -2;
  static final int ERR_UNIMPL = -3;

  static final int SHA256_MAC_LEN = 32;
  static final int SHA512_MAC_LEN = 64;

  String algorithm;
  < MAC length for the selected hash >
  int outBytes;
  < 1 = enabled, 0 = disabled >
  int state;
  < 1 once a supported algorithm name has been accepted >
  int configured;
  < 1 when the native /crypto backend is linked and ready >
  int backend;

  < Accept only the two HMAC variants the native layer provides. >
  int configure(String a) {
    if (a == null) { configured = 0; return ERR_PARAM; }
    if (a == "HMAC-SHA-256") { algorithm = a; outBytes = SHA256_MAC_LEN; configured = 1; return OK; }
    if (a == "HMAC-SHA-512") { algorithm = a; outBytes = SHA512_MAC_LEN; configured = 1; return OK; }
    configured = 0;
    return ERR_PARAM;
  }

  void bindBackend(int ready) { if (ready == 1) { backend = 1; } else { backend = 0; } }
  boolean backendReady() { return backend == 1; }

  void enable() { state = 1; }
  void disable() { state = 0; }
  boolean active() { return state == 1 && configured == 1 && backend == 1; }

  int macLength() { return outBytes; }

  < Compute a MAC over msg with key. Models slc_hmac_sha256()/slc_hmac_sha512(). >
  int sign(String key, String msg) {
    if (active() == false) { return ERR_UNIMPL; }
    if (key == null || msg == null) { return ERR_PARAM; }
    return OK;
  }

  < Verify a MAC. The native layer compares in constant time (slc_ct_equal); >
  < a mismatch yields ERR_AUTH. >
  int verify(String key, String msg, String mac) {
    if (active() == false) { return ERR_UNIMPL; }
    if (key == null || msg == null || mac == null) { return ERR_PARAM; }
    return OK;
  }

  String securityNote() {
    return "Reference-grade: not constant-time, not side-channel resistant; use an audited library in production.";
  }
}