skills/message-authentication-code-pattern/SKILL.md
Security pattern for implementing Message Authentication Codes (MACs) to ensure data integrity and origin authentication. Use when implementing HMAC, CMAC, or other MAC algorithms, verifying message integrity, authenticating message origin with shared secrets, or when non-repudiation is NOT required. Specialization of Cryptographic action pattern.
npx skillsauth add igbuend/grimbard message-authentication-code-patternInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
This pattern encapsulates common considerations for using Message Authentication Codes (MAC) to ensure the integrity of messages and authenticate the identity of the provider.
A Message Authentication Code (MAC) is a tag computed from a message using a special hash function whose output depends on a secret cryptographic key. Generating a MAC requires:
Only parties possessing the agreed-upon secret key can generate and verify valid MACs.
Appending a MAC to a message provides two properties:
Important Limitations:
If non-repudiation is required: Use digital signatures instead of MACs.
| Role | Type | Responsibility | |------|------|----------------| | EntityA | Entity | Wants to create a MAC for message(s) | | EntityB | Entity | Wants to verify whether message was modified | | MAC Generator | Cryptographic Primitive | Generates MAC for message and key | | MAC Verifier | Cryptographic Primitive | Verifies message and MAC match |
Note: MAC Generator and MAC Verifier can be the same library instance. EntityA and EntityB can also be the same entity.
EntityA → [generate(m, keyInfo)] → MAC Generator
MAC Generator → [mac] → EntityA
EntityA → [m + mac] → EntityB
EntityB → [verify(m, mac, keyInfo)] → MAC Verifier
MAC Verifier → [confirmation or error] → EntityB
The MAC Verifier checks whether the MAC generated from m using the given key is identical to the provided MAC. If so, confirms to EntityB; otherwise, returns error.
Critical: Always use dedicated MAC ciphers. Ad hoc constructions using unkeyed hash functions concatenated with secrets have been shown to be insecure.
| Use Case | Recommended Length | |----------|-------------------| | Long-term (10+ years) | 256 bits | | Standard (up to 10 years) | 128 bits | | Short-lived (e.g., session tokens) | 64 bits minimum |
A cryptographic key used to generate and verify MACs should:
Specialization of Cryptographic action.Use keys for a single purpose:
Specialization of Cryptographic action.Design for change:
Specialization of Cryptographic action.Reuse existing libraries:
If protecting integrity of encrypted messages in transit:
| Aspect | MAC | Digital Signature | |--------|-----|-------------------| | Key type | Symmetric (shared secret) | Asymmetric (public/private) | | Non-repudiation | No | Yes | | Who can verify | Only key holders | Anyone with public key | | Who can generate | Any key holder | Only private key holder | | Performance | Faster | Slower | | Use case | Internal integrity | External verification, legal |
development
Security anti-pattern for Cross-Site Scripting vulnerabilities (CWE-79). Use when generating or reviewing code that renders HTML, handles user input in web pages, uses innerHTML/document.write, or builds dynamic web content. Covers Reflected, Stored, and DOM-based XSS. AI code has 86% XSS failure rate.
development
Security anti-pattern for XPath injection vulnerabilities (CWE-643). Use when generating or reviewing code that queries XML documents, constructs XPath expressions, or handles user input in XML operations. Detects unescaped quotes and special characters in XPath queries.
development
Security anti-pattern for weak password hashing (CWE-327, CWE-759). Use when generating or reviewing code that stores or verifies user passwords. Detects use of MD5, SHA1, SHA256 without salt, or missing password hashing entirely. Recommends bcrypt, Argon2, or scrypt.
development
Security anti-pattern for weak encryption (CWE-326, CWE-327). Use when generating or reviewing code that encrypts data, handles encryption keys, or uses cryptographic modes. Detects DES, ECB mode, static IVs, and custom crypto implementations.