// common/blob_format.h
#pragma once
#include <stdint.h>
#include <stddef.h>

#define BLOB_MAGIC 0x5347584B
#define BLOB_VER_2 2

#define POLICY_MRENCLAVE 0
#define POLICY_MRSIGNER  1

#define BLOB_IV_LEN      12
#define BLOB_TAG_LEN     16
#define BLOB_ISV_PROD_ID 0x0001
#define BLOB_ISV_SVN_MIN 0x0001

#pragma pack(push, 1)
typedef struct blob_header_v2_t {
    uint32_t magic;       // BLOB_MAGIC
    uint8_t  ver;         // BLOB_VER_2
    uint8_t  policy;      // POLICY_*
    uint16_t isv_prod_id;
    uint16_t isv_svn;
    uint8_t  iv_len;      // 12
    uint8_t  tag_len;     // 16
    uint16_t keyinfo_len; // usually 512
    // followed by: key_info[keyinfo_len] || iv || ct[...] || tag[tag_len]
} blob_header_v2_t;
#pragma pack(pop)

// 基础校验
static inline int blob_header_v2_check(const blob_header_v2_t* h, size_t blob_len) {
    if (!h) return 0;
    if (blob_len < sizeof(blob_header_v2_t)) return 0;
    if (h->magic != BLOB_MAGIC) return 0;
    if (h->ver != BLOB_VER_2) return 0;
    if (h->iv_len != BLOB_IV_LEN || h->tag_len != BLOB_TAG_LEN) return 0;
    if (h->policy != POLICY_MRENCLAVE && h->policy != POLICY_MRSIGNER) return 0;
    size_t min = sizeof(blob_header_v2_t) + h->keyinfo_len + h->iv_len + h->tag_len + 1;
    return blob_len >= min;
}
