@wazir-dev/cli 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +111 -0
- package/CHANGELOG.md +14 -0
- package/CONTRIBUTING.md +101 -0
- package/LICENSE +21 -0
- package/README.md +314 -0
- package/assets/composition-engine.mmd +34 -0
- package/assets/demo-script.sh +17 -0
- package/assets/logo-dark.svg +14 -0
- package/assets/logo.svg +14 -0
- package/assets/pipeline.mmd +39 -0
- package/assets/record-demo.sh +51 -0
- package/docs/README.md +51 -0
- package/docs/adapters/context-mode.md +60 -0
- package/docs/concepts/architecture.md +87 -0
- package/docs/concepts/artifact-model.md +60 -0
- package/docs/concepts/composition-engine.md +36 -0
- package/docs/concepts/indexing-and-recall.md +160 -0
- package/docs/concepts/observability.md +41 -0
- package/docs/concepts/roles-and-workflows.md +59 -0
- package/docs/concepts/terminology-policy.md +27 -0
- package/docs/getting-started/01-installation.md +78 -0
- package/docs/getting-started/02-first-run.md +102 -0
- package/docs/getting-started/03-adding-to-project.md +15 -0
- package/docs/getting-started/04-host-setup.md +15 -0
- package/docs/guides/ci-integration.md +15 -0
- package/docs/guides/creating-skills.md +15 -0
- package/docs/guides/expertise-module-authoring.md +15 -0
- package/docs/guides/hook-development.md +15 -0
- package/docs/guides/memory-and-learnings.md +34 -0
- package/docs/guides/multi-host-export.md +15 -0
- package/docs/guides/troubleshooting.md +101 -0
- package/docs/guides/writing-custom-roles.md +15 -0
- package/docs/plans/2026-03-15-cli-pipeline-integration-design.md +592 -0
- package/docs/plans/2026-03-15-cli-pipeline-integration-plan.md +598 -0
- package/docs/plans/2026-03-15-docs-enforcement-plan.md +238 -0
- package/docs/readmes/INDEX.md +99 -0
- package/docs/readmes/features/expertise/README.md +171 -0
- package/docs/readmes/features/exports/README.md +222 -0
- package/docs/readmes/features/hooks/README.md +103 -0
- package/docs/readmes/features/hooks/loop-cap-guard.md +133 -0
- package/docs/readmes/features/hooks/post-tool-capture.md +121 -0
- package/docs/readmes/features/hooks/post-tool-lint.md +130 -0
- package/docs/readmes/features/hooks/pre-compact-summary.md +122 -0
- package/docs/readmes/features/hooks/pre-tool-capture-route.md +100 -0
- package/docs/readmes/features/hooks/protected-path-write-guard.md +128 -0
- package/docs/readmes/features/hooks/session-start.md +119 -0
- package/docs/readmes/features/hooks/stop-handoff-harvest.md +125 -0
- package/docs/readmes/features/roles/README.md +157 -0
- package/docs/readmes/features/roles/clarifier.md +152 -0
- package/docs/readmes/features/roles/content-author.md +190 -0
- package/docs/readmes/features/roles/designer.md +193 -0
- package/docs/readmes/features/roles/executor.md +184 -0
- package/docs/readmes/features/roles/learner.md +210 -0
- package/docs/readmes/features/roles/planner.md +182 -0
- package/docs/readmes/features/roles/researcher.md +164 -0
- package/docs/readmes/features/roles/reviewer.md +184 -0
- package/docs/readmes/features/roles/specifier.md +162 -0
- package/docs/readmes/features/roles/verifier.md +215 -0
- package/docs/readmes/features/schemas/README.md +178 -0
- package/docs/readmes/features/skills/README.md +63 -0
- package/docs/readmes/features/skills/brainstorming.md +96 -0
- package/docs/readmes/features/skills/debugging.md +148 -0
- package/docs/readmes/features/skills/design.md +120 -0
- package/docs/readmes/features/skills/prepare-next.md +109 -0
- package/docs/readmes/features/skills/run-audit.md +159 -0
- package/docs/readmes/features/skills/scan-project.md +109 -0
- package/docs/readmes/features/skills/self-audit.md +176 -0
- package/docs/readmes/features/skills/tdd.md +137 -0
- package/docs/readmes/features/skills/using-skills.md +92 -0
- package/docs/readmes/features/skills/verification.md +120 -0
- package/docs/readmes/features/skills/writing-plans.md +104 -0
- package/docs/readmes/features/tooling/README.md +320 -0
- package/docs/readmes/features/workflows/README.md +186 -0
- package/docs/readmes/features/workflows/author.md +181 -0
- package/docs/readmes/features/workflows/clarify.md +154 -0
- package/docs/readmes/features/workflows/design-review.md +171 -0
- package/docs/readmes/features/workflows/design.md +169 -0
- package/docs/readmes/features/workflows/discover.md +162 -0
- package/docs/readmes/features/workflows/execute.md +173 -0
- package/docs/readmes/features/workflows/learn.md +167 -0
- package/docs/readmes/features/workflows/plan-review.md +165 -0
- package/docs/readmes/features/workflows/plan.md +170 -0
- package/docs/readmes/features/workflows/prepare-next.md +167 -0
- package/docs/readmes/features/workflows/review.md +169 -0
- package/docs/readmes/features/workflows/run-audit.md +191 -0
- package/docs/readmes/features/workflows/spec-challenge.md +159 -0
- package/docs/readmes/features/workflows/specify.md +160 -0
- package/docs/readmes/features/workflows/verify.md +177 -0
- package/docs/readmes/packages/README.md +50 -0
- package/docs/readmes/packages/ajv.md +117 -0
- package/docs/readmes/packages/context-mode.md +118 -0
- package/docs/readmes/packages/gray-matter.md +116 -0
- package/docs/readmes/packages/node-test.md +137 -0
- package/docs/readmes/packages/yaml.md +112 -0
- package/docs/reference/configuration-reference.md +159 -0
- package/docs/reference/expertise-index.md +52 -0
- package/docs/reference/git-flow.md +43 -0
- package/docs/reference/hooks.md +87 -0
- package/docs/reference/host-exports.md +50 -0
- package/docs/reference/launch-checklist.md +172 -0
- package/docs/reference/marketplace-listings.md +76 -0
- package/docs/reference/release-process.md +34 -0
- package/docs/reference/roles-reference.md +77 -0
- package/docs/reference/skills.md +33 -0
- package/docs/reference/templates.md +29 -0
- package/docs/reference/tooling-cli.md +94 -0
- package/docs/truth-claims.yaml +222 -0
- package/expertise/PROGRESS.md +63 -0
- package/expertise/README.md +18 -0
- package/expertise/antipatterns/PROGRESS.md +56 -0
- package/expertise/antipatterns/backend/api-design-antipatterns.md +1271 -0
- package/expertise/antipatterns/backend/auth-antipatterns.md +1195 -0
- package/expertise/antipatterns/backend/caching-antipatterns.md +622 -0
- package/expertise/antipatterns/backend/database-antipatterns.md +1038 -0
- package/expertise/antipatterns/backend/index.md +24 -0
- package/expertise/antipatterns/backend/microservices-antipatterns.md +850 -0
- package/expertise/antipatterns/code/architecture-antipatterns.md +919 -0
- package/expertise/antipatterns/code/async-antipatterns.md +622 -0
- package/expertise/antipatterns/code/code-smells.md +1186 -0
- package/expertise/antipatterns/code/dependency-antipatterns.md +1209 -0
- package/expertise/antipatterns/code/error-handling-antipatterns.md +1360 -0
- package/expertise/antipatterns/code/index.md +27 -0
- package/expertise/antipatterns/code/naming-and-abstraction.md +1118 -0
- package/expertise/antipatterns/code/state-management-antipatterns.md +1076 -0
- package/expertise/antipatterns/code/testing-antipatterns.md +1053 -0
- package/expertise/antipatterns/design/accessibility-antipatterns.md +1136 -0
- package/expertise/antipatterns/design/dark-patterns.md +1121 -0
- package/expertise/antipatterns/design/index.md +22 -0
- package/expertise/antipatterns/design/ui-antipatterns.md +1202 -0
- package/expertise/antipatterns/design/ux-antipatterns.md +680 -0
- package/expertise/antipatterns/frontend/css-layout-antipatterns.md +691 -0
- package/expertise/antipatterns/frontend/flutter-antipatterns.md +1827 -0
- package/expertise/antipatterns/frontend/index.md +23 -0
- package/expertise/antipatterns/frontend/mobile-antipatterns.md +573 -0
- package/expertise/antipatterns/frontend/react-antipatterns.md +1128 -0
- package/expertise/antipatterns/frontend/spa-antipatterns.md +1235 -0
- package/expertise/antipatterns/index.md +31 -0
- package/expertise/antipatterns/performance/index.md +20 -0
- package/expertise/antipatterns/performance/performance-antipatterns.md +1013 -0
- package/expertise/antipatterns/performance/premature-optimization.md +623 -0
- package/expertise/antipatterns/performance/scaling-antipatterns.md +785 -0
- package/expertise/antipatterns/process/ai-coding-antipatterns.md +853 -0
- package/expertise/antipatterns/process/code-review-antipatterns.md +656 -0
- package/expertise/antipatterns/process/deployment-antipatterns.md +920 -0
- package/expertise/antipatterns/process/index.md +23 -0
- package/expertise/antipatterns/process/technical-debt-antipatterns.md +647 -0
- package/expertise/antipatterns/security/index.md +20 -0
- package/expertise/antipatterns/security/secrets-antipatterns.md +849 -0
- package/expertise/antipatterns/security/security-theater.md +843 -0
- package/expertise/antipatterns/security/vulnerability-patterns.md +801 -0
- package/expertise/architecture/PROGRESS.md +70 -0
- package/expertise/architecture/data/caching-architecture.md +671 -0
- package/expertise/architecture/data/data-consistency.md +574 -0
- package/expertise/architecture/data/data-modeling.md +536 -0
- package/expertise/architecture/data/event-streams-and-queues.md +634 -0
- package/expertise/architecture/data/index.md +25 -0
- package/expertise/architecture/data/search-architecture.md +663 -0
- package/expertise/architecture/data/sql-vs-nosql.md +708 -0
- package/expertise/architecture/decisions/architecture-decision-records.md +640 -0
- package/expertise/architecture/decisions/build-vs-buy.md +616 -0
- package/expertise/architecture/decisions/index.md +23 -0
- package/expertise/architecture/decisions/monolith-to-microservices.md +790 -0
- package/expertise/architecture/decisions/technology-selection.md +616 -0
- package/expertise/architecture/distributed/cap-theorem-and-tradeoffs.md +800 -0
- package/expertise/architecture/distributed/circuit-breaker-bulkhead.md +741 -0
- package/expertise/architecture/distributed/consensus-and-coordination.md +796 -0
- package/expertise/architecture/distributed/distributed-systems-fundamentals.md +564 -0
- package/expertise/architecture/distributed/idempotency-and-retry.md +796 -0
- package/expertise/architecture/distributed/index.md +25 -0
- package/expertise/architecture/distributed/saga-pattern.md +797 -0
- package/expertise/architecture/foundations/architectural-thinking.md +460 -0
- package/expertise/architecture/foundations/coupling-and-cohesion.md +770 -0
- package/expertise/architecture/foundations/design-principles-solid.md +649 -0
- package/expertise/architecture/foundations/domain-driven-design.md +719 -0
- package/expertise/architecture/foundations/index.md +25 -0
- package/expertise/architecture/foundations/separation-of-concerns.md +472 -0
- package/expertise/architecture/foundations/twelve-factor-app.md +797 -0
- package/expertise/architecture/index.md +34 -0
- package/expertise/architecture/integration/api-design-graphql.md +638 -0
- package/expertise/architecture/integration/api-design-grpc.md +804 -0
- package/expertise/architecture/integration/api-design-rest.md +892 -0
- package/expertise/architecture/integration/index.md +25 -0
- package/expertise/architecture/integration/third-party-integration.md +795 -0
- package/expertise/architecture/integration/webhooks-and-callbacks.md +1152 -0
- package/expertise/architecture/integration/websockets-realtime.md +791 -0
- package/expertise/architecture/mobile-architecture/index.md +22 -0
- package/expertise/architecture/mobile-architecture/mobile-app-architecture.md +780 -0
- package/expertise/architecture/mobile-architecture/mobile-backend-for-frontend.md +670 -0
- package/expertise/architecture/mobile-architecture/offline-first.md +719 -0
- package/expertise/architecture/mobile-architecture/push-and-sync.md +782 -0
- package/expertise/architecture/patterns/cqrs-event-sourcing.md +717 -0
- package/expertise/architecture/patterns/event-driven.md +797 -0
- package/expertise/architecture/patterns/hexagonal-clean-architecture.md +870 -0
- package/expertise/architecture/patterns/index.md +27 -0
- package/expertise/architecture/patterns/layered-architecture.md +736 -0
- package/expertise/architecture/patterns/microservices.md +753 -0
- package/expertise/architecture/patterns/modular-monolith.md +692 -0
- package/expertise/architecture/patterns/monolith.md +626 -0
- package/expertise/architecture/patterns/plugin-architecture.md +735 -0
- package/expertise/architecture/patterns/serverless.md +780 -0
- package/expertise/architecture/scaling/database-scaling.md +615 -0
- package/expertise/architecture/scaling/feature-flags-and-rollouts.md +757 -0
- package/expertise/architecture/scaling/horizontal-vs-vertical.md +606 -0
- package/expertise/architecture/scaling/index.md +24 -0
- package/expertise/architecture/scaling/multi-tenancy.md +800 -0
- package/expertise/architecture/scaling/stateless-design.md +787 -0
- package/expertise/backend/embedded-firmware.md +625 -0
- package/expertise/backend/go.md +853 -0
- package/expertise/backend/index.md +24 -0
- package/expertise/backend/java-spring.md +448 -0
- package/expertise/backend/node-typescript.md +625 -0
- package/expertise/backend/python-fastapi.md +724 -0
- package/expertise/backend/rust.md +458 -0
- package/expertise/backend/solidity.md +711 -0
- package/expertise/composition-map.yaml +443 -0
- package/expertise/content/foundations/content-modeling.md +395 -0
- package/expertise/content/foundations/editorial-standards.md +449 -0
- package/expertise/content/foundations/index.md +24 -0
- package/expertise/content/foundations/microcopy.md +455 -0
- package/expertise/content/foundations/terminology-governance.md +509 -0
- package/expertise/content/index.md +34 -0
- package/expertise/content/patterns/accessibility-copy.md +518 -0
- package/expertise/content/patterns/index.md +24 -0
- package/expertise/content/patterns/notification-content.md +433 -0
- package/expertise/content/patterns/sample-content.md +486 -0
- package/expertise/content/patterns/state-copy.md +439 -0
- package/expertise/design/PROGRESS.md +58 -0
- package/expertise/design/disciplines/dark-mode-theming.md +577 -0
- package/expertise/design/disciplines/design-systems.md +595 -0
- package/expertise/design/disciplines/index.md +25 -0
- package/expertise/design/disciplines/information-architecture.md +800 -0
- package/expertise/design/disciplines/interaction-design.md +788 -0
- package/expertise/design/disciplines/responsive-design.md +552 -0
- package/expertise/design/disciplines/usability-testing.md +516 -0
- package/expertise/design/disciplines/user-research.md +792 -0
- package/expertise/design/foundations/accessibility-design.md +796 -0
- package/expertise/design/foundations/color-theory.md +797 -0
- package/expertise/design/foundations/iconography.md +795 -0
- package/expertise/design/foundations/index.md +26 -0
- package/expertise/design/foundations/motion-and-animation.md +653 -0
- package/expertise/design/foundations/rtl-design.md +585 -0
- package/expertise/design/foundations/spacing-and-layout.md +607 -0
- package/expertise/design/foundations/typography.md +800 -0
- package/expertise/design/foundations/visual-hierarchy.md +761 -0
- package/expertise/design/index.md +32 -0
- package/expertise/design/patterns/authentication-flows.md +474 -0
- package/expertise/design/patterns/content-consumption.md +789 -0
- package/expertise/design/patterns/data-display.md +618 -0
- package/expertise/design/patterns/e-commerce.md +1494 -0
- package/expertise/design/patterns/feedback-and-states.md +642 -0
- package/expertise/design/patterns/forms-and-input.md +819 -0
- package/expertise/design/patterns/gamification.md +801 -0
- package/expertise/design/patterns/index.md +31 -0
- package/expertise/design/patterns/microinteractions.md +449 -0
- package/expertise/design/patterns/navigation.md +800 -0
- package/expertise/design/patterns/notifications.md +705 -0
- package/expertise/design/patterns/onboarding.md +700 -0
- package/expertise/design/patterns/search-and-filter.md +601 -0
- package/expertise/design/patterns/settings-and-preferences.md +768 -0
- package/expertise/design/patterns/social-and-community.md +748 -0
- package/expertise/design/platforms/desktop-native.md +612 -0
- package/expertise/design/platforms/index.md +25 -0
- package/expertise/design/platforms/mobile-android.md +825 -0
- package/expertise/design/platforms/mobile-cross-platform.md +983 -0
- package/expertise/design/platforms/mobile-ios.md +699 -0
- package/expertise/design/platforms/tablet.md +794 -0
- package/expertise/design/platforms/web-dashboard.md +790 -0
- package/expertise/design/platforms/web-responsive.md +550 -0
- package/expertise/design/psychology/behavioral-nudges.md +449 -0
- package/expertise/design/psychology/cognitive-load.md +1191 -0
- package/expertise/design/psychology/error-psychology.md +778 -0
- package/expertise/design/psychology/index.md +22 -0
- package/expertise/design/psychology/persuasive-design.md +736 -0
- package/expertise/design/psychology/user-mental-models.md +623 -0
- package/expertise/design/tooling/open-pencil.md +266 -0
- package/expertise/frontend/angular.md +1073 -0
- package/expertise/frontend/desktop-electron.md +546 -0
- package/expertise/frontend/flutter.md +782 -0
- package/expertise/frontend/index.md +27 -0
- package/expertise/frontend/native-android.md +409 -0
- package/expertise/frontend/native-ios.md +490 -0
- package/expertise/frontend/react-native.md +1160 -0
- package/expertise/frontend/react.md +808 -0
- package/expertise/frontend/vue.md +1089 -0
- package/expertise/humanize/domain-rules-code.md +79 -0
- package/expertise/humanize/domain-rules-content.md +67 -0
- package/expertise/humanize/domain-rules-technical-docs.md +56 -0
- package/expertise/humanize/index.md +35 -0
- package/expertise/humanize/self-audit-checklist.md +87 -0
- package/expertise/humanize/sentence-patterns.md +218 -0
- package/expertise/humanize/vocabulary-blacklist.md +105 -0
- package/expertise/i18n/PROGRESS.md +65 -0
- package/expertise/i18n/advanced/accessibility-and-i18n.md +28 -0
- package/expertise/i18n/advanced/bidirectional-text-algorithm.md +38 -0
- package/expertise/i18n/advanced/complex-scripts.md +30 -0
- package/expertise/i18n/advanced/performance-and-i18n.md +27 -0
- package/expertise/i18n/advanced/testing-i18n.md +28 -0
- package/expertise/i18n/content/content-adaptation.md +23 -0
- package/expertise/i18n/content/locale-specific-formatting.md +23 -0
- package/expertise/i18n/content/machine-translation-integration.md +28 -0
- package/expertise/i18n/content/translation-management.md +29 -0
- package/expertise/i18n/foundations/date-time-calendars.md +67 -0
- package/expertise/i18n/foundations/i18n-architecture.md +272 -0
- package/expertise/i18n/foundations/locale-and-language-tags.md +79 -0
- package/expertise/i18n/foundations/numbers-currency-units.md +61 -0
- package/expertise/i18n/foundations/pluralization-and-gender.md +109 -0
- package/expertise/i18n/foundations/string-externalization.md +236 -0
- package/expertise/i18n/foundations/text-direction-bidi.md +241 -0
- package/expertise/i18n/foundations/unicode-and-encoding.md +86 -0
- package/expertise/i18n/index.md +38 -0
- package/expertise/i18n/platform/backend-i18n.md +31 -0
- package/expertise/i18n/platform/flutter-i18n.md +148 -0
- package/expertise/i18n/platform/native-android-i18n.md +36 -0
- package/expertise/i18n/platform/native-ios-i18n.md +36 -0
- package/expertise/i18n/platform/react-i18n.md +103 -0
- package/expertise/i18n/platform/web-css-i18n.md +81 -0
- package/expertise/i18n/rtl/arabic-specific.md +175 -0
- package/expertise/i18n/rtl/hebrew-specific.md +149 -0
- package/expertise/i18n/rtl/rtl-animations-and-transitions.md +111 -0
- package/expertise/i18n/rtl/rtl-forms-and-input.md +161 -0
- package/expertise/i18n/rtl/rtl-fundamentals.md +211 -0
- package/expertise/i18n/rtl/rtl-icons-and-images.md +181 -0
- package/expertise/i18n/rtl/rtl-layout-mirroring.md +252 -0
- package/expertise/i18n/rtl/rtl-navigation-and-gestures.md +107 -0
- package/expertise/i18n/rtl/rtl-testing-and-qa.md +147 -0
- package/expertise/i18n/rtl/rtl-typography.md +160 -0
- package/expertise/index.md +113 -0
- package/expertise/index.yaml +216 -0
- package/expertise/infrastructure/cloud-aws.md +597 -0
- package/expertise/infrastructure/cloud-gcp.md +599 -0
- package/expertise/infrastructure/cybersecurity.md +816 -0
- package/expertise/infrastructure/database-mongodb.md +447 -0
- package/expertise/infrastructure/database-postgres.md +400 -0
- package/expertise/infrastructure/devops-cicd.md +787 -0
- package/expertise/infrastructure/index.md +27 -0
- package/expertise/performance/PROGRESS.md +50 -0
- package/expertise/performance/backend/api-latency.md +1204 -0
- package/expertise/performance/backend/background-jobs.md +506 -0
- package/expertise/performance/backend/connection-pooling.md +1209 -0
- package/expertise/performance/backend/database-query-optimization.md +515 -0
- package/expertise/performance/backend/index.md +23 -0
- package/expertise/performance/backend/rate-limiting-and-throttling.md +971 -0
- package/expertise/performance/foundations/algorithmic-complexity.md +954 -0
- package/expertise/performance/foundations/caching-strategies.md +489 -0
- package/expertise/performance/foundations/concurrency-and-parallelism.md +847 -0
- package/expertise/performance/foundations/index.md +24 -0
- package/expertise/performance/foundations/measuring-and-profiling.md +440 -0
- package/expertise/performance/foundations/memory-management.md +964 -0
- package/expertise/performance/foundations/performance-budgets.md +1314 -0
- package/expertise/performance/index.md +31 -0
- package/expertise/performance/infrastructure/auto-scaling.md +1059 -0
- package/expertise/performance/infrastructure/cdn-and-edge.md +1081 -0
- package/expertise/performance/infrastructure/index.md +22 -0
- package/expertise/performance/infrastructure/load-balancing.md +1081 -0
- package/expertise/performance/infrastructure/observability.md +1079 -0
- package/expertise/performance/mobile/index.md +23 -0
- package/expertise/performance/mobile/mobile-animations.md +544 -0
- package/expertise/performance/mobile/mobile-memory-battery.md +416 -0
- package/expertise/performance/mobile/mobile-network.md +452 -0
- package/expertise/performance/mobile/mobile-rendering.md +599 -0
- package/expertise/performance/mobile/mobile-startup-time.md +505 -0
- package/expertise/performance/platform-specific/flutter-performance.md +647 -0
- package/expertise/performance/platform-specific/index.md +22 -0
- package/expertise/performance/platform-specific/node-performance.md +1307 -0
- package/expertise/performance/platform-specific/postgres-performance.md +1366 -0
- package/expertise/performance/platform-specific/react-performance.md +1403 -0
- package/expertise/performance/web/bundle-optimization.md +1239 -0
- package/expertise/performance/web/image-and-media.md +636 -0
- package/expertise/performance/web/index.md +24 -0
- package/expertise/performance/web/network-optimization.md +1133 -0
- package/expertise/performance/web/rendering-performance.md +1098 -0
- package/expertise/performance/web/ssr-and-hydration.md +918 -0
- package/expertise/performance/web/web-vitals.md +1374 -0
- package/expertise/quality/accessibility.md +985 -0
- package/expertise/quality/evidence-based-verification.md +499 -0
- package/expertise/quality/index.md +24 -0
- package/expertise/quality/ml-model-audit.md +614 -0
- package/expertise/quality/performance.md +600 -0
- package/expertise/quality/testing-api.md +891 -0
- package/expertise/quality/testing-mobile.md +496 -0
- package/expertise/quality/testing-web.md +849 -0
- package/expertise/security/PROGRESS.md +54 -0
- package/expertise/security/agentic-identity.md +540 -0
- package/expertise/security/compliance-frameworks.md +601 -0
- package/expertise/security/data/data-encryption.md +364 -0
- package/expertise/security/data/data-privacy-gdpr.md +692 -0
- package/expertise/security/data/database-security.md +1171 -0
- package/expertise/security/data/index.md +22 -0
- package/expertise/security/data/pii-handling.md +531 -0
- package/expertise/security/foundations/authentication.md +1041 -0
- package/expertise/security/foundations/authorization.md +603 -0
- package/expertise/security/foundations/cryptography.md +1001 -0
- package/expertise/security/foundations/index.md +25 -0
- package/expertise/security/foundations/owasp-top-10.md +1354 -0
- package/expertise/security/foundations/secrets-management.md +1217 -0
- package/expertise/security/foundations/secure-sdlc.md +700 -0
- package/expertise/security/foundations/supply-chain-security.md +698 -0
- package/expertise/security/index.md +31 -0
- package/expertise/security/infrastructure/cloud-security-aws.md +1296 -0
- package/expertise/security/infrastructure/cloud-security-gcp.md +1376 -0
- package/expertise/security/infrastructure/container-security.md +721 -0
- package/expertise/security/infrastructure/incident-response.md +1295 -0
- package/expertise/security/infrastructure/index.md +24 -0
- package/expertise/security/infrastructure/logging-and-monitoring.md +1618 -0
- package/expertise/security/infrastructure/network-security.md +1337 -0
- package/expertise/security/mobile/index.md +23 -0
- package/expertise/security/mobile/mobile-android-security.md +1218 -0
- package/expertise/security/mobile/mobile-binary-protection.md +1229 -0
- package/expertise/security/mobile/mobile-data-storage.md +1265 -0
- package/expertise/security/mobile/mobile-ios-security.md +1401 -0
- package/expertise/security/mobile/mobile-network-security.md +1520 -0
- package/expertise/security/smart-contract-security.md +594 -0
- package/expertise/security/testing/index.md +22 -0
- package/expertise/security/testing/penetration-testing.md +1258 -0
- package/expertise/security/testing/security-code-review.md +1765 -0
- package/expertise/security/testing/threat-modeling.md +1074 -0
- package/expertise/security/testing/vulnerability-scanning.md +1062 -0
- package/expertise/security/web/api-security.md +586 -0
- package/expertise/security/web/cors-and-headers.md +433 -0
- package/expertise/security/web/csrf.md +562 -0
- package/expertise/security/web/file-upload.md +1477 -0
- package/expertise/security/web/index.md +25 -0
- package/expertise/security/web/injection.md +1375 -0
- package/expertise/security/web/session-management.md +1101 -0
- package/expertise/security/web/xss.md +1158 -0
- package/exports/README.md +17 -0
- package/exports/hosts/claude/.claude/agents/clarifier.md +42 -0
- package/exports/hosts/claude/.claude/agents/content-author.md +63 -0
- package/exports/hosts/claude/.claude/agents/designer.md +55 -0
- package/exports/hosts/claude/.claude/agents/executor.md +55 -0
- package/exports/hosts/claude/.claude/agents/learner.md +51 -0
- package/exports/hosts/claude/.claude/agents/planner.md +53 -0
- package/exports/hosts/claude/.claude/agents/researcher.md +43 -0
- package/exports/hosts/claude/.claude/agents/reviewer.md +54 -0
- package/exports/hosts/claude/.claude/agents/specifier.md +47 -0
- package/exports/hosts/claude/.claude/agents/verifier.md +71 -0
- package/exports/hosts/claude/.claude/commands/author.md +42 -0
- package/exports/hosts/claude/.claude/commands/clarify.md +38 -0
- package/exports/hosts/claude/.claude/commands/design-review.md +46 -0
- package/exports/hosts/claude/.claude/commands/design.md +44 -0
- package/exports/hosts/claude/.claude/commands/discover.md +37 -0
- package/exports/hosts/claude/.claude/commands/execute.md +48 -0
- package/exports/hosts/claude/.claude/commands/learn.md +38 -0
- package/exports/hosts/claude/.claude/commands/plan-review.md +42 -0
- package/exports/hosts/claude/.claude/commands/plan.md +39 -0
- package/exports/hosts/claude/.claude/commands/prepare-next.md +37 -0
- package/exports/hosts/claude/.claude/commands/review.md +40 -0
- package/exports/hosts/claude/.claude/commands/run-audit.md +41 -0
- package/exports/hosts/claude/.claude/commands/spec-challenge.md +41 -0
- package/exports/hosts/claude/.claude/commands/specify.md +38 -0
- package/exports/hosts/claude/.claude/commands/verify.md +37 -0
- package/exports/hosts/claude/.claude/settings.json +34 -0
- package/exports/hosts/claude/CLAUDE.md +19 -0
- package/exports/hosts/claude/export.manifest.json +38 -0
- package/exports/hosts/claude/host-package.json +67 -0
- package/exports/hosts/codex/AGENTS.md +19 -0
- package/exports/hosts/codex/export.manifest.json +38 -0
- package/exports/hosts/codex/host-package.json +41 -0
- package/exports/hosts/cursor/.cursor/hooks.json +16 -0
- package/exports/hosts/cursor/.cursor/rules/wazir-core.mdc +19 -0
- package/exports/hosts/cursor/export.manifest.json +38 -0
- package/exports/hosts/cursor/host-package.json +42 -0
- package/exports/hosts/gemini/GEMINI.md +19 -0
- package/exports/hosts/gemini/export.manifest.json +38 -0
- package/exports/hosts/gemini/host-package.json +41 -0
- package/hooks/README.md +18 -0
- package/hooks/definitions/loop_cap_guard.yaml +21 -0
- package/hooks/definitions/post_tool_capture.yaml +24 -0
- package/hooks/definitions/pre_compact_summary.yaml +19 -0
- package/hooks/definitions/pre_tool_capture_route.yaml +19 -0
- package/hooks/definitions/protected_path_write_guard.yaml +19 -0
- package/hooks/definitions/session_start.yaml +19 -0
- package/hooks/definitions/stop_handoff_harvest.yaml +20 -0
- package/hooks/loop-cap-guard +17 -0
- package/hooks/post-tool-lint +36 -0
- package/hooks/protected-path-write-guard +17 -0
- package/hooks/session-start +41 -0
- package/llms-full.txt +2355 -0
- package/llms.txt +43 -0
- package/package.json +79 -0
- package/roles/README.md +20 -0
- package/roles/clarifier.md +42 -0
- package/roles/content-author.md +63 -0
- package/roles/designer.md +55 -0
- package/roles/executor.md +55 -0
- package/roles/learner.md +51 -0
- package/roles/planner.md +53 -0
- package/roles/researcher.md +43 -0
- package/roles/reviewer.md +54 -0
- package/roles/specifier.md +47 -0
- package/roles/verifier.md +71 -0
- package/schemas/README.md +24 -0
- package/schemas/accepted-learning.schema.json +20 -0
- package/schemas/author-artifact.schema.json +156 -0
- package/schemas/clarification.schema.json +19 -0
- package/schemas/design-artifact.schema.json +80 -0
- package/schemas/docs-claim.schema.json +18 -0
- package/schemas/export-manifest.schema.json +20 -0
- package/schemas/hook.schema.json +67 -0
- package/schemas/host-export-package.schema.json +18 -0
- package/schemas/implementation-plan.schema.json +19 -0
- package/schemas/proposed-learning.schema.json +19 -0
- package/schemas/research.schema.json +18 -0
- package/schemas/review.schema.json +29 -0
- package/schemas/run-manifest.schema.json +18 -0
- package/schemas/spec-challenge.schema.json +18 -0
- package/schemas/spec.schema.json +20 -0
- package/schemas/usage.schema.json +102 -0
- package/schemas/verification-proof.schema.json +29 -0
- package/schemas/wazir-manifest.schema.json +173 -0
- package/skills/README.md +40 -0
- package/skills/brainstorming/SKILL.md +77 -0
- package/skills/debugging/SKILL.md +50 -0
- package/skills/design/SKILL.md +61 -0
- package/skills/dispatching-parallel-agents/SKILL.md +128 -0
- package/skills/executing-plans/SKILL.md +70 -0
- package/skills/finishing-a-development-branch/SKILL.md +169 -0
- package/skills/humanize/SKILL.md +123 -0
- package/skills/init-pipeline/SKILL.md +124 -0
- package/skills/prepare-next/SKILL.md +20 -0
- package/skills/receiving-code-review/SKILL.md +123 -0
- package/skills/requesting-code-review/SKILL.md +105 -0
- package/skills/requesting-code-review/code-reviewer.md +108 -0
- package/skills/run-audit/SKILL.md +197 -0
- package/skills/scan-project/SKILL.md +41 -0
- package/skills/self-audit/SKILL.md +153 -0
- package/skills/subagent-driven-development/SKILL.md +154 -0
- package/skills/subagent-driven-development/code-quality-reviewer-prompt.md +26 -0
- package/skills/subagent-driven-development/implementer-prompt.md +102 -0
- package/skills/subagent-driven-development/spec-reviewer-prompt.md +61 -0
- package/skills/tdd/SKILL.md +23 -0
- package/skills/using-git-worktrees/SKILL.md +163 -0
- package/skills/using-skills/SKILL.md +95 -0
- package/skills/verification/SKILL.md +22 -0
- package/skills/wazir/SKILL.md +463 -0
- package/skills/writing-plans/SKILL.md +30 -0
- package/skills/writing-skills/SKILL.md +157 -0
- package/skills/writing-skills/anthropic-best-practices.md +122 -0
- package/skills/writing-skills/persuasion-principles.md +50 -0
- package/templates/README.md +20 -0
- package/templates/artifacts/README.md +10 -0
- package/templates/artifacts/accepted-learning.md +19 -0
- package/templates/artifacts/accepted-learning.template.json +12 -0
- package/templates/artifacts/author.md +74 -0
- package/templates/artifacts/author.template.json +19 -0
- package/templates/artifacts/clarification.md +21 -0
- package/templates/artifacts/clarification.template.json +12 -0
- package/templates/artifacts/execute-notes.md +19 -0
- package/templates/artifacts/implementation-plan.md +21 -0
- package/templates/artifacts/implementation-plan.template.json +11 -0
- package/templates/artifacts/learning-proposal.md +19 -0
- package/templates/artifacts/next-run-handoff.md +21 -0
- package/templates/artifacts/plan-review.md +19 -0
- package/templates/artifacts/proposed-learning.template.json +12 -0
- package/templates/artifacts/research.md +21 -0
- package/templates/artifacts/research.template.json +12 -0
- package/templates/artifacts/review-findings.md +19 -0
- package/templates/artifacts/review.template.json +11 -0
- package/templates/artifacts/run-manifest.template.json +8 -0
- package/templates/artifacts/spec-challenge.md +19 -0
- package/templates/artifacts/spec-challenge.template.json +11 -0
- package/templates/artifacts/spec.md +21 -0
- package/templates/artifacts/spec.template.json +12 -0
- package/templates/artifacts/verification-proof.md +19 -0
- package/templates/artifacts/verification-proof.template.json +11 -0
- package/templates/examples/accepted-learning.example.json +14 -0
- package/templates/examples/author.example.json +152 -0
- package/templates/examples/clarification.example.json +15 -0
- package/templates/examples/docs-claim.example.json +8 -0
- package/templates/examples/export-manifest.example.json +7 -0
- package/templates/examples/host-export-package.example.json +11 -0
- package/templates/examples/implementation-plan.example.json +17 -0
- package/templates/examples/proposed-learning.example.json +13 -0
- package/templates/examples/research.example.json +15 -0
- package/templates/examples/research.example.md +6 -0
- package/templates/examples/review.example.json +17 -0
- package/templates/examples/run-manifest.example.json +9 -0
- package/templates/examples/spec-challenge.example.json +14 -0
- package/templates/examples/spec.example.json +21 -0
- package/templates/examples/verification-proof.example.json +21 -0
- package/templates/examples/wazir-manifest.example.yaml +65 -0
- package/templates/task-definition-schema.md +99 -0
- package/tooling/README.md +20 -0
- package/tooling/src/adapters/context-mode.js +50 -0
- package/tooling/src/capture/command.js +376 -0
- package/tooling/src/capture/store.js +99 -0
- package/tooling/src/capture/usage.js +270 -0
- package/tooling/src/checks/branches.js +50 -0
- package/tooling/src/checks/brand-truth.js +110 -0
- package/tooling/src/checks/changelog.js +231 -0
- package/tooling/src/checks/command-registry.js +36 -0
- package/tooling/src/checks/commits.js +102 -0
- package/tooling/src/checks/docs-drift.js +103 -0
- package/tooling/src/checks/docs-truth.js +201 -0
- package/tooling/src/checks/runtime-surface.js +156 -0
- package/tooling/src/cli.js +116 -0
- package/tooling/src/command-options.js +56 -0
- package/tooling/src/commands/validate.js +320 -0
- package/tooling/src/doctor/command.js +91 -0
- package/tooling/src/export/command.js +77 -0
- package/tooling/src/export/compiler.js +498 -0
- package/tooling/src/guards/loop-cap-guard.js +52 -0
- package/tooling/src/guards/protected-path-write-guard.js +67 -0
- package/tooling/src/index/command.js +152 -0
- package/tooling/src/index/storage.js +1061 -0
- package/tooling/src/index/summarizers.js +261 -0
- package/tooling/src/loaders.js +18 -0
- package/tooling/src/project-root.js +22 -0
- package/tooling/src/recall/command.js +225 -0
- package/tooling/src/schema-validator.js +30 -0
- package/tooling/src/state-root.js +40 -0
- package/tooling/src/status/command.js +71 -0
- package/wazir.manifest.yaml +135 -0
- package/workflows/README.md +19 -0
- package/workflows/author.md +42 -0
- package/workflows/clarify.md +38 -0
- package/workflows/design-review.md +46 -0
- package/workflows/design.md +44 -0
- package/workflows/discover.md +37 -0
- package/workflows/execute.md +48 -0
- package/workflows/learn.md +38 -0
- package/workflows/plan-review.md +42 -0
- package/workflows/plan.md +39 -0
- package/workflows/prepare-next.md +37 -0
- package/workflows/review.md +40 -0
- package/workflows/run-audit.md +41 -0
- package/workflows/spec-challenge.md +41 -0
- package/workflows/specify.md +38 -0
- package/workflows/verify.md +37 -0
|
@@ -0,0 +1,1001 @@
|
|
|
1
|
+
# Cryptography for Application Security
|
|
2
|
+
|
|
3
|
+
> **Expertise Module** | Last updated: 2026-03-08
|
|
4
|
+
> **Purpose:** Guide AI agents in implementing correct cryptographic patterns and avoiding common pitfalls.
|
|
5
|
+
> **Scope:** Symmetric/asymmetric encryption, hashing, password storage, TLS, key management, post-quantum readiness.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 1. Threat Landscape
|
|
10
|
+
|
|
11
|
+
### 1.1 The Scale of Cryptographic Failures
|
|
12
|
+
|
|
13
|
+
Cryptographic failures rank as **OWASP A02:2021** (previously "Sensitive Data Exposure"), reflecting a
|
|
14
|
+
shift in focus from the symptom (data exposure) to the root cause (broken or misused cryptography).
|
|
15
|
+
The category maps to **CWE-259** (Hard-coded Password), **CWE-327** (Broken/Risky Crypto Algorithm),
|
|
16
|
+
**CWE-328** (Reversible One-Way Hash), **CWE-330** (Insufficient Randomness), and **CWE-916**
|
|
17
|
+
(Use of Password Hash With Insufficient Computational Effort).
|
|
18
|
+
|
|
19
|
+
### 1.2 Deprecated and Broken Algorithms
|
|
20
|
+
|
|
21
|
+
| Algorithm | Status | Risk |
|
|
22
|
+
|-----------|--------|------|
|
|
23
|
+
| MD5 | **Broken** | Collision attacks in seconds; never use for integrity or passwords |
|
|
24
|
+
| SHA-1 | **Broken** | SHAttered attack (2017) produced real-world collisions; deprecated by NIST |
|
|
25
|
+
| DES | **Broken** | 56-bit key; brute-forced in hours on modern hardware |
|
|
26
|
+
| 3DES (Triple DES) | **Deprecated** | NIST deprecated after 2023; Sweet32 birthday attack on 64-bit blocks |
|
|
27
|
+
| RC4 | **Broken** | Statistical biases exploitable in TLS (RFC 7465 banned RC4 in 2015) |
|
|
28
|
+
| Blowfish (raw) | **Legacy** | 64-bit block size vulnerable to Sweet32; bcrypt usage is separate and still valid |
|
|
29
|
+
| RSA-1024 | **Deprecated** | Factorable with sufficient resources; minimum 2048-bit required |
|
|
30
|
+
| PKCS#1 v1.5 padding | **Vulnerable** | Bleichenbacher padding oracle attacks; use OAEP instead |
|
|
31
|
+
|
|
32
|
+
### 1.3 Real-World Breaches
|
|
33
|
+
|
|
34
|
+
**Adobe 2013 -- 153 million passwords exposed:**
|
|
35
|
+
Adobe encrypted (not hashed) passwords using 3DES in ECB mode with a single static key for all
|
|
36
|
+
accounts. ECB mode produces identical ciphertext for identical plaintext blocks, allowing attackers
|
|
37
|
+
to identify common passwords by frequency analysis. Password hints were stored in plaintext alongside
|
|
38
|
+
encrypted passwords, further accelerating cracking. This remains one of the most cited examples
|
|
39
|
+
of catastrophic cryptographic failure in application security.
|
|
40
|
+
*(Source: Schneier on Security, Krebs on Security)*
|
|
41
|
+
|
|
42
|
+
**Ashley Madison 2015 -- bcrypt undermined by MD5 fallback:**
|
|
43
|
+
While Ashley Madison used bcrypt (cost 12) for password hashing, a critical implementation error
|
|
44
|
+
left a parallel `$loginkey` token computed as `MD5(lowercase(username) + password)` for accounts
|
|
45
|
+
created before June 2012. The CynoSure Prime cracking team exploited this MD5 fallback to crack
|
|
46
|
+
over 11.2 million passwords in days -- approximately one million times faster than attacking bcrypt
|
|
47
|
+
directly. This demonstrates that the strongest algorithm in a system is irrelevant if a weaker
|
|
48
|
+
parallel path exists.
|
|
49
|
+
*(Source: CynoSure Prime blog, Ars Technica, CSO Online)*
|
|
50
|
+
|
|
51
|
+
**Heartbleed (2014) -- OpenSSL buffer over-read:**
|
|
52
|
+
CVE-2014-0160 allowed attackers to read up to 64KB of server memory per heartbeat request,
|
|
53
|
+
potentially exposing private keys, session tokens, and user data. Affected approximately 17% of
|
|
54
|
+
TLS-enabled web servers. Demonstrated the danger of memory-unsafe cryptographic implementations.
|
|
55
|
+
|
|
56
|
+
**SolarWinds / Codecov (2020-2021) -- supply chain + weak integrity checks:**
|
|
57
|
+
Compromised build pipelines delivered trojanized updates. Weak or absent cryptographic integrity
|
|
58
|
+
verification of build artifacts allowed malicious code to propagate undetected for months.
|
|
59
|
+
|
|
60
|
+
### 1.4 Post-Quantum Cryptography Threat
|
|
61
|
+
|
|
62
|
+
Quantum computers threaten all currently deployed asymmetric cryptography:
|
|
63
|
+
- **RSA, DSA, ECDSA, ECDH, Ed25519** -- vulnerable to Shor's algorithm
|
|
64
|
+
- **AES-128** -- reduced to 64-bit effective security via Grover's algorithm (AES-256 remains safe)
|
|
65
|
+
- **SHA-256** -- reduced to 128-bit collision resistance (still adequate)
|
|
66
|
+
|
|
67
|
+
**NIST Post-Quantum Standards (finalized August 2024):**
|
|
68
|
+
- **FIPS 203 (ML-KEM):** Module-Lattice-Based Key Encapsulation (from CRYSTALS-Kyber). Primary standard for general key exchange.
|
|
69
|
+
- **FIPS 204 (ML-DSA):** Module-Lattice-Based Digital Signature (from CRYSTALS-Dilithium).
|
|
70
|
+
- **FIPS 205 (SLH-DSA):** Stateless Hash-Based Digital Signature (from SPHINCS+).
|
|
71
|
+
- **HQC:** Selected March 2025 as backup KEM algorithm; draft standard expected 2026.
|
|
72
|
+
|
|
73
|
+
**Action now:** Inventory all asymmetric crypto usage. Plan migration to hybrid schemes
|
|
74
|
+
(classical + PQC) for data requiring long-term confidentiality ("harvest now, decrypt later" threat).
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
## 2. Core Security Principles
|
|
79
|
+
|
|
80
|
+
### 2.1 Never Roll Your Own Crypto
|
|
81
|
+
|
|
82
|
+
The single most important rule: **use established, audited cryptographic libraries**. Custom
|
|
83
|
+
implementations invariably introduce side-channel leaks, padding errors, or statistical weaknesses
|
|
84
|
+
that may go undetected for years. Even subtle mistakes -- a missing constant-time comparison, a
|
|
85
|
+
reused nonce -- can completely break the security of a scheme.
|
|
86
|
+
|
|
87
|
+
### 2.2 Algorithm Selection Guide
|
|
88
|
+
|
|
89
|
+
| Purpose | Recommended | Acceptable | Avoid |
|
|
90
|
+
|---------|-------------|------------|-------|
|
|
91
|
+
| Symmetric encryption | AES-256-GCM | ChaCha20-Poly1305 | AES-CBC (without HMAC), AES-ECB, DES, 3DES, RC4 |
|
|
92
|
+
| Asymmetric encryption | RSA-OAEP (2048+) | ECIES (P-256+) | RSA PKCS#1 v1.5, RSA <2048 |
|
|
93
|
+
| Digital signatures | Ed25519 | ECDSA (P-256), RSA-PSS (2048+) | RSA PKCS#1 v1.5 signing, DSA |
|
|
94
|
+
| Key exchange | X25519 | ECDH (P-256+) | DH <2048, static DH |
|
|
95
|
+
| Hashing (integrity) | SHA-256, SHA-3-256 | SHA-512, BLAKE2b | MD5, SHA-1 |
|
|
96
|
+
| Password hashing | Argon2id | bcrypt (cost 12+), scrypt | MD5, SHA-*, PBKDF2-SHA1 (<600k iterations) |
|
|
97
|
+
| MAC | HMAC-SHA-256 | Poly1305, KMAC | HMAC-MD5, HMAC-SHA-1 |
|
|
98
|
+
| KDF | HKDF-SHA-256 | PBKDF2-SHA-256 (600k+) | MD5-based KDFs |
|
|
99
|
+
|
|
100
|
+
### 2.3 IV/Nonce Handling
|
|
101
|
+
|
|
102
|
+
- **AES-GCM:** 12-byte (96-bit) nonce. MUST be unique per key. Never reuse. Nonce reuse with GCM
|
|
103
|
+
leaks the authentication key and allows forgery. Use `crypto.randomBytes(12)` or a counter.
|
|
104
|
+
- **AES-CBC:** 16-byte IV. Must be unpredictable (random). Predictable IVs enable BEAST-style attacks.
|
|
105
|
+
- **ChaCha20-Poly1305:** 12-byte nonce. Same uniqueness requirements as GCM.
|
|
106
|
+
- **General rule:** When in doubt, generate a cryptographically random nonce for every operation
|
|
107
|
+
and prepend it to the ciphertext.
|
|
108
|
+
|
|
109
|
+
### 2.4 Key Derivation Functions
|
|
110
|
+
|
|
111
|
+
Never use a raw password or passphrase as an encryption key. Derive keys using:
|
|
112
|
+
- **HKDF:** For deriving keys from already-strong keying material (e.g., Diffie-Hellman shared secrets).
|
|
113
|
+
- **PBKDF2:** For password-based key derivation; minimum 600,000 iterations with SHA-256 (OWASP 2023).
|
|
114
|
+
- **Argon2id:** Preferred for password-based key derivation; provides memory-hardness against GPU attacks.
|
|
115
|
+
- **scrypt:** Alternative memory-hard KDF; N=2^17, r=8, p=1 minimum.
|
|
116
|
+
|
|
117
|
+
### 2.5 Secure Random Number Generation
|
|
118
|
+
|
|
119
|
+
| Platform | CSPRNG Source | Usage |
|
|
120
|
+
|----------|--------------|-------|
|
|
121
|
+
| Node.js | `crypto.randomBytes()`, `crypto.randomUUID()` | Keys, IVs, tokens |
|
|
122
|
+
| Python | `secrets` module, `os.urandom()` | Keys, IVs, tokens |
|
|
123
|
+
| Browser | `crypto.getRandomValues()` | Client-side crypto |
|
|
124
|
+
| Java | `SecureRandom` | Keys, IVs, tokens |
|
|
125
|
+
| iOS | `SecRandomCopyBytes`, CryptoKit | Keys, IVs, tokens |
|
|
126
|
+
| Android | `SecureRandom` | Keys, IVs, tokens |
|
|
127
|
+
|
|
128
|
+
**Never use:** `Math.random()` (JS), `random` module (Python), `java.util.Random`,
|
|
129
|
+
`rand()` (C/C++) for any security purpose.
|
|
130
|
+
|
|
131
|
+
### 2.6 Defense in Depth for Crypto
|
|
132
|
+
|
|
133
|
+
1. **Use authenticated encryption** (AEAD) -- AES-GCM or ChaCha20-Poly1305 -- to get confidentiality AND integrity in one operation.
|
|
134
|
+
2. **Validate before decrypting** -- check authentication tags, HMAC, or signatures before processing decrypted data.
|
|
135
|
+
3. **Fail closed** -- any cryptographic error (bad MAC, padding error, invalid signature) must result in immediate rejection, not a fallback to weaker security.
|
|
136
|
+
4. **Rotate keys** on a defined schedule and on compromise suspicion.
|
|
137
|
+
5. **Separate keys by purpose** -- never use the same key for encryption and signing.
|
|
138
|
+
|
|
139
|
+
---
|
|
140
|
+
|
|
141
|
+
## 3. Implementation Patterns
|
|
142
|
+
|
|
143
|
+
### 3.1 Symmetric Encryption (AES-256-GCM)
|
|
144
|
+
|
|
145
|
+
AES-GCM is an Authenticated Encryption with Associated Data (AEAD) mode. It provides
|
|
146
|
+
confidentiality, integrity, and authenticity in a single operation. The authentication tag
|
|
147
|
+
prevents tampering, and Additional Authenticated Data (AAD) can protect unencrypted metadata.
|
|
148
|
+
|
|
149
|
+
**Key properties:**
|
|
150
|
+
- 256-bit key (32 bytes)
|
|
151
|
+
- 96-bit nonce (12 bytes) -- MUST be unique per encryption with same key
|
|
152
|
+
- 128-bit authentication tag (16 bytes) -- always verify before using plaintext
|
|
153
|
+
- Maximum plaintext size per operation: ~64 GB (2^39 - 256 bits)
|
|
154
|
+
- After ~2^32 encryptions with random nonces, rotate the key (birthday bound)
|
|
155
|
+
|
|
156
|
+
### 3.2 Asymmetric Cryptography
|
|
157
|
+
|
|
158
|
+
**RSA (2048+ bits):** Use OAEP padding for encryption, PSS padding for signatures.
|
|
159
|
+
Never use textbook RSA or PKCS#1 v1.5 padding. Consider 4096-bit keys for data
|
|
160
|
+
needing protection beyond 2030.
|
|
161
|
+
|
|
162
|
+
**Ed25519:** Modern EdDSA signature scheme over Curve25519. 128-bit security level.
|
|
163
|
+
Deterministic signatures (no random nonce needed, eliminating a class of implementation bugs).
|
|
164
|
+
Preferred over ECDSA for new systems.
|
|
165
|
+
|
|
166
|
+
**X25519:** Elliptic-curve Diffie-Hellman over Curve25519. Used for key agreement/exchange.
|
|
167
|
+
Standard in TLS 1.3, Signal Protocol, WireGuard.
|
|
168
|
+
|
|
169
|
+
### 3.3 Hashing
|
|
170
|
+
|
|
171
|
+
- **SHA-256 / SHA-3-256:** General-purpose integrity checking, content addressing, digital signatures.
|
|
172
|
+
- **BLAKE2b:** Faster than SHA-256 on software; suitable for integrity and MAC (with key).
|
|
173
|
+
- **SHA-512:** Larger output; useful when 256-bit collision resistance is insufficient.
|
|
174
|
+
- **Never use MD5 or SHA-1** for any security purpose. MD5 is acceptable only for non-security
|
|
175
|
+
checksums (e.g., cache keys) where collision resistance is irrelevant.
|
|
176
|
+
|
|
177
|
+
### 3.4 Password Hashing
|
|
178
|
+
|
|
179
|
+
**Argon2id (recommended for new systems):**
|
|
180
|
+
- Hybrid mode: resists both side-channel (data-independent) and GPU (data-dependent) attacks
|
|
181
|
+
- OWASP minimum: m=19456 (19 MiB), t=2 iterations, p=1 parallelism
|
|
182
|
+
- Strong recommendation: m=65536 (64 MiB), t=3 iterations, p=1
|
|
183
|
+
- High security: m=131072 (128 MiB), t=4 iterations, p=1
|
|
184
|
+
|
|
185
|
+
**bcrypt (proven, widely supported):**
|
|
186
|
+
- Cost factor 12 minimum (2025); adjust to target 250-500ms per hash
|
|
187
|
+
- Maximum input length: 72 bytes (silently truncates longer passwords)
|
|
188
|
+
- Encode password as UTF-8 before hashing
|
|
189
|
+
- Still secure when properly configured; no urgent need to migrate existing systems
|
|
190
|
+
|
|
191
|
+
**Never use for passwords:** MD5, SHA-1, SHA-256 (even with salt), unsalted hashes of any kind.
|
|
192
|
+
|
|
193
|
+
### 3.5 HMAC (Hash-based Message Authentication Code)
|
|
194
|
+
|
|
195
|
+
HMAC provides message authentication and integrity. Use HMAC-SHA-256 with a key of at least
|
|
196
|
+
256 bits. Common applications: API request signing, JWT signatures, webhook verification,
|
|
197
|
+
integrity of data at rest.
|
|
198
|
+
|
|
199
|
+
### 3.6 Digital Signatures
|
|
200
|
+
|
|
201
|
+
- **Ed25519:** Preferred for speed and security. Used in SSH keys, package signing, JWT (EdDSA).
|
|
202
|
+
- **ECDSA (P-256):** Widely supported; requires secure random nonce generation (failure is catastrophic -- see PS3 key leak). Use deterministic ECDSA (RFC 6979) where possible.
|
|
203
|
+
- **RSA-PSS:** Use with SHA-256, salt length equal to hash length. Preferred over PKCS#1 v1.5 for signatures.
|
|
204
|
+
|
|
205
|
+
### 3.7 TLS Configuration
|
|
206
|
+
|
|
207
|
+
**TLS 1.3 (preferred):**
|
|
208
|
+
- Only five cipher suites, all AEAD: TLS_AES_256_GCM_SHA384, TLS_AES_128_GCM_SHA256, TLS_CHACHA20_POLY1305_SHA256, TLS_AES_128_CCM_SHA256, TLS_AES_128_CCM_8_SHA256
|
|
209
|
+
- Forward secrecy mandatory (ephemeral key exchange only)
|
|
210
|
+
- 0-RTT: disable for non-idempotent operations; implement anti-replay for sensitive endpoints
|
|
211
|
+
- No version negotiation downgrade possible
|
|
212
|
+
|
|
213
|
+
**TLS 1.2 (acceptable with correct configuration):**
|
|
214
|
+
- Use only AEAD cipher suites (GCM, ChaCha20-Poly1305)
|
|
215
|
+
- Disable CBC cipher suites (POODLE, Lucky13)
|
|
216
|
+
- Require forward secrecy (ECDHE key exchange)
|
|
217
|
+
- Disable: SSLv3, TLS 1.0, TLS 1.1, compression, renegotiation
|
|
218
|
+
|
|
219
|
+
**Adoption status (2025):** TLS 1.3 used by ~70% of websites. TLS 1.2 still at 99.9% support.
|
|
220
|
+
NIST SP 800-52 Rev. 2 requires TLS 1.3 support for federal systems since January 2024.
|
|
221
|
+
|
|
222
|
+
---
|
|
223
|
+
|
|
224
|
+
## 4. Vulnerability Catalog
|
|
225
|
+
|
|
226
|
+
### V01: ECB Mode Usage (CWE-327)
|
|
227
|
+
**Risk:** Critical | **CVSS:** 7.5+
|
|
228
|
+
ECB encrypts each block independently, producing identical ciphertext for identical plaintext blocks.
|
|
229
|
+
Reveals patterns in data (the "ECB penguin" problem). The Adobe 2013 breach used 3DES-ECB.
|
|
230
|
+
```
|
|
231
|
+
// VULNERABLE: ECB mode
|
|
232
|
+
crypto.createCipheriv('aes-256-ecb', key, null);
|
|
233
|
+
|
|
234
|
+
// SECURE: GCM mode with random nonce
|
|
235
|
+
const nonce = crypto.randomBytes(12);
|
|
236
|
+
crypto.createCipheriv('aes-256-gcm', key, nonce);
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### V02: Static or Reused IVs/Nonces (CWE-329)
|
|
240
|
+
**Risk:** Critical | **CVSS:** 7.5+
|
|
241
|
+
Reusing a nonce with AES-GCM breaks authentication completely, leaking the GHASH key.
|
|
242
|
+
With AES-CTR, nonce reuse leaks plaintext via XOR of ciphertexts.
|
|
243
|
+
```
|
|
244
|
+
// VULNERABLE: Static IV
|
|
245
|
+
const iv = Buffer.from('1234567890ab');
|
|
246
|
+
|
|
247
|
+
// SECURE: Random nonce per encryption
|
|
248
|
+
const nonce = crypto.randomBytes(12);
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### V03: Weak PRNGs for Cryptographic Material (CWE-330)
|
|
252
|
+
**Risk:** Critical | **CVSS:** 9.0+
|
|
253
|
+
Using `Math.random()`, `random.random()`, or similar non-cryptographic PRNGs for keys,
|
|
254
|
+
tokens, or nonces makes them predictable.
|
|
255
|
+
```python
|
|
256
|
+
# VULNERABLE
|
|
257
|
+
import random
|
|
258
|
+
token = ''.join(random.choices('abcdef0123456789', k=32))
|
|
259
|
+
|
|
260
|
+
# SECURE
|
|
261
|
+
import secrets
|
|
262
|
+
token = secrets.token_hex(32)
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
### V04: MD5/SHA-1 for Password Hashing (CWE-916)
|
|
266
|
+
**Risk:** Critical | **CVSS:** 7.5+
|
|
267
|
+
Fast hashes allow billions of guesses per second on GPUs. MD5: ~200 billion/sec on modern GPUs.
|
|
268
|
+
```python
|
|
269
|
+
# VULNERABLE
|
|
270
|
+
password_hash = hashlib.md5(password.encode()).hexdigest()
|
|
271
|
+
|
|
272
|
+
# SECURE
|
|
273
|
+
from argon2 import PasswordHasher
|
|
274
|
+
ph = PasswordHasher(memory_cost=65536, time_cost=3, parallelism=1)
|
|
275
|
+
password_hash = ph.hash(password)
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
### V05: RSA Without Proper Padding (CWE-780)
|
|
279
|
+
**Risk:** High | **CVSS:** 7.0+
|
|
280
|
+
Textbook RSA or PKCS#1 v1.5 padding are vulnerable to chosen-ciphertext attacks
|
|
281
|
+
(Bleichenbacher 1998). Always use OAEP for encryption.
|
|
282
|
+
```python
|
|
283
|
+
# VULNERABLE: PKCS1 v1.5 padding
|
|
284
|
+
from Crypto.Cipher import PKCS1_v1_5
|
|
285
|
+
|
|
286
|
+
# SECURE: OAEP padding
|
|
287
|
+
from Crypto.Cipher import PKCS1_OAEP
|
|
288
|
+
cipher = PKCS1_OAEP.new(key, hashAlgo=SHA256)
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
### V06: Cryptographic Keys in Source Code (CWE-321)
|
|
292
|
+
**Risk:** Critical | **CVSS:** 9.0+
|
|
293
|
+
Hard-coded keys are trivially extracted from source code, compiled binaries, and container images.
|
|
294
|
+
```javascript
|
|
295
|
+
// VULNERABLE
|
|
296
|
+
const ENCRYPTION_KEY = 'a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6';
|
|
297
|
+
|
|
298
|
+
// SECURE: Load from environment or secret manager
|
|
299
|
+
const ENCRYPTION_KEY = Buffer.from(process.env.ENCRYPTION_KEY, 'hex');
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
### V07: Insufficient Key Length (CWE-326)
|
|
303
|
+
**Risk:** High | **CVSS:** 7.0+
|
|
304
|
+
- RSA < 2048 bits: factorable with sufficient resources
|
|
305
|
+
- AES-128: adequate today but consider AES-256 for post-quantum safety
|
|
306
|
+
- ECDSA < 224 bits: insufficient security margin
|
|
307
|
+
|
|
308
|
+
### V08: Timing Attacks on Comparison (CWE-208)
|
|
309
|
+
**Risk:** Medium-High | **CVSS:** 5.0-7.0
|
|
310
|
+
Standard string comparison (`===`, `==`) returns early on first mismatch, leaking information
|
|
311
|
+
about how many bytes match. Attackers can brute-force secrets byte-by-byte.
|
|
312
|
+
```javascript
|
|
313
|
+
// VULNERABLE: Early-exit comparison
|
|
314
|
+
if (providedToken === expectedToken) { /* ... */ }
|
|
315
|
+
|
|
316
|
+
// SECURE: Constant-time comparison
|
|
317
|
+
const crypto = require('crypto');
|
|
318
|
+
if (crypto.timingSafeEqual(Buffer.from(providedToken), Buffer.from(expectedToken))) { /* ... */ }
|
|
319
|
+
```
|
|
320
|
+
```python
|
|
321
|
+
# SECURE: Python constant-time comparison
|
|
322
|
+
import hmac
|
|
323
|
+
if hmac.compare_digest(provided_token, expected_token): ...
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
### V09: Missing Authentication Tag Verification (CWE-347)
|
|
327
|
+
**Risk:** Critical | **CVSS:** 8.0+
|
|
328
|
+
Decrypting AES-GCM ciphertext without verifying the authentication tag allows attackers to
|
|
329
|
+
tamper with ciphertext. Always call `setAuthTag()` before `final()`.
|
|
330
|
+
|
|
331
|
+
### V10: Padding Oracle Attacks (CWE-209)
|
|
332
|
+
**Risk:** High | **CVSS:** 7.5+
|
|
333
|
+
When an application reveals whether padding is valid (via error messages or timing differences),
|
|
334
|
+
attackers can decrypt CBC ciphertext without the key. Mitigation: use AEAD modes (GCM),
|
|
335
|
+
or encrypt-then-MAC with constant-time MAC verification.
|
|
336
|
+
|
|
337
|
+
### V11: Weak Password Hash Without Salt (CWE-916)
|
|
338
|
+
**Risk:** High | **CVSS:** 7.5+
|
|
339
|
+
Unsalted hashes allow precomputation attacks (rainbow tables). Even with a strong algorithm,
|
|
340
|
+
missing salt means identical passwords produce identical hashes.
|
|
341
|
+
|
|
342
|
+
### V12: Key Derivation Without Stretching (CWE-916)
|
|
343
|
+
**Risk:** High | **CVSS:** 7.0+
|
|
344
|
+
Deriving encryption keys directly from passwords without a KDF (PBKDF2, scrypt, Argon2)
|
|
345
|
+
allows brute-force at hash-computation speed instead of KDF-limited speed.
|
|
346
|
+
|
|
347
|
+
### V13: Cleartext Transmission of Sensitive Data (CWE-319)
|
|
348
|
+
**Risk:** High | **CVSS:** 7.5+
|
|
349
|
+
Transmitting passwords, tokens, or PII over HTTP or unencrypted channels exposes data to
|
|
350
|
+
network sniffers. Enforce TLS for all connections carrying sensitive data.
|
|
351
|
+
|
|
352
|
+
### V14: Certificate Validation Disabled (CWE-295)
|
|
353
|
+
**Risk:** Critical | **CVSS:** 8.0+
|
|
354
|
+
Disabling TLS certificate verification (`NODE_TLS_REJECT_UNAUTHORIZED=0`, `verify=False`)
|
|
355
|
+
allows man-in-the-middle attacks.
|
|
356
|
+
```javascript
|
|
357
|
+
// VULNERABLE: Disabling certificate verification
|
|
358
|
+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
|
|
359
|
+
|
|
360
|
+
// SECURE: Use proper CA certificates
|
|
361
|
+
const https = require('https');
|
|
362
|
+
const agent = new https.Agent({ ca: fs.readFileSync('ca-cert.pem') });
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
### V15: Deterministic ECDSA Nonce Failure (CWE-330)
|
|
366
|
+
**Risk:** Critical | **CVSS:** 9.0+
|
|
367
|
+
If the random nonce `k` in ECDSA is reused, biased, or predictable, the private key can be
|
|
368
|
+
recovered. This led to the PlayStation 3 master key extraction (2010). Use deterministic
|
|
369
|
+
ECDSA (RFC 6979) or Ed25519 (which is inherently deterministic).
|
|
370
|
+
|
|
371
|
+
### V16: Using Encryption for Integrity (CWE-327)
|
|
372
|
+
**Risk:** High | **CVSS:** 6.5+
|
|
373
|
+
Encryption without authentication (AES-CBC, AES-CTR alone) does not protect against
|
|
374
|
+
tampering. Attackers can flip ciphertext bits to modify plaintext predictably.
|
|
375
|
+
Always use AEAD (AES-GCM) or encrypt-then-MAC.
|
|
376
|
+
|
|
377
|
+
### V17: Insecure Key Storage in Browser Storage (CWE-922)
|
|
378
|
+
**Risk:** High | **CVSS:** 7.0+
|
|
379
|
+
Storing encryption keys or secrets in browser cookies, localStorage, or sessionStorage
|
|
380
|
+
exposes them to XSS attacks and browser extensions.
|
|
381
|
+
|
|
382
|
+
---
|
|
383
|
+
|
|
384
|
+
## 5. Security Checklist
|
|
385
|
+
|
|
386
|
+
### Encryption
|
|
387
|
+
- [ ] All symmetric encryption uses AES-256-GCM or ChaCha20-Poly1305 (AEAD modes)
|
|
388
|
+
- [ ] No use of ECB, raw CBC, or unauthenticated stream ciphers
|
|
389
|
+
- [ ] Unique random nonce generated for every encryption operation
|
|
390
|
+
- [ ] Authentication tags verified before processing decrypted data
|
|
391
|
+
- [ ] Encryption keys are at least 256 bits for symmetric, 2048 bits for RSA
|
|
392
|
+
- [ ] No encryption keys hard-coded in source code or configuration files
|
|
393
|
+
|
|
394
|
+
### Hashing and Passwords
|
|
395
|
+
- [ ] Passwords hashed with Argon2id (m=64MB, t=3, p=1) or bcrypt (cost 12+)
|
|
396
|
+
- [ ] No use of MD5, SHA-1, or unsalted hashes for any security purpose
|
|
397
|
+
- [ ] Password hash configuration targets 250-500ms per hash on production hardware
|
|
398
|
+
- [ ] Hash comparison uses constant-time functions (timingSafeEqual, hmac.compare_digest)
|
|
399
|
+
|
|
400
|
+
### Key Management
|
|
401
|
+
- [ ] Keys stored in dedicated secret managers (AWS KMS, HashiCorp Vault, GCP KMS)
|
|
402
|
+
- [ ] Key rotation policy defined and automated (at least annually)
|
|
403
|
+
- [ ] Separate keys for separate purposes (encryption vs. signing vs. derivation)
|
|
404
|
+
- [ ] Key material never logged, never included in error messages
|
|
405
|
+
- [ ] Key derivation from passwords uses PBKDF2 (600k+ iterations), scrypt, or Argon2
|
|
406
|
+
|
|
407
|
+
### TLS/Transport
|
|
408
|
+
- [ ] TLS 1.2 minimum enforced; TLS 1.3 preferred
|
|
409
|
+
- [ ] TLS 1.0, TLS 1.1, SSLv3 disabled
|
|
410
|
+
- [ ] Only AEAD cipher suites enabled (no CBC, no RC4)
|
|
411
|
+
- [ ] Forward secrecy enabled (ECDHE key exchange)
|
|
412
|
+
- [ ] HSTS header set with includeSubDomains and minimum 1-year max-age
|
|
413
|
+
- [ ] Certificate validation never disabled in production code
|
|
414
|
+
- [ ] Certificate expiry monitoring automated with alerting
|
|
415
|
+
|
|
416
|
+
### Random Number Generation
|
|
417
|
+
- [ ] All security-sensitive randomness from CSPRNG (crypto.randomBytes, secrets module)
|
|
418
|
+
- [ ] No use of Math.random(), random module, or java.util.Random for security
|
|
419
|
+
- [ ] Token/session IDs have at least 128 bits of entropy
|
|
420
|
+
|
|
421
|
+
### General
|
|
422
|
+
- [ ] Cryptographic library versions regularly updated
|
|
423
|
+
- [ ] No custom cryptographic algorithm implementations
|
|
424
|
+
- [ ] Cryptographic failures cause hard errors, never silent fallbacks
|
|
425
|
+
- [ ] Annual review of cipher suites and protocol versions (PCI DSS 4.0 requirement)
|
|
426
|
+
|
|
427
|
+
---
|
|
428
|
+
|
|
429
|
+
## 6. Tools and Automation
|
|
430
|
+
|
|
431
|
+
### Static Analysis
|
|
432
|
+
|
|
433
|
+
| Tool | Purpose | Crypto Capabilities |
|
|
434
|
+
|------|---------|-------------------|
|
|
435
|
+
| **Semgrep** | SAST for 30+ languages | Built-in rules for MD5, SHA1, DES, RC4, ECB mode, weak PRNGs, hard-coded secrets. Custom rules via pattern matching. |
|
|
436
|
+
| **Bandit** | Python SAST | Detects use of `hashlib.md5`, `hashlib.sha1`, `random` for crypto, weak SSL/TLS settings. |
|
|
437
|
+
| **ESLint (security plugins)** | JavaScript/TypeScript | `eslint-plugin-security` flags `Math.random()` and other insecure patterns. |
|
|
438
|
+
| **CodeQL** | GitHub Advanced Security | Crypto queries detect weak hashing, missing TLS validation, hard-coded credentials. |
|
|
439
|
+
| **Checkov** | IaC scanning | Detects unencrypted S3 buckets, RDS without encryption, weak KMS configurations. |
|
|
440
|
+
|
|
441
|
+
### TLS and Certificate Testing
|
|
442
|
+
|
|
443
|
+
| Tool | Purpose | Usage |
|
|
444
|
+
|------|---------|-------|
|
|
445
|
+
| **SSL Labs (ssllabs.com/ssltest)** | Web-based TLS grading | Test public-facing servers; target A+ grade. Checks protocol versions, cipher suites, certificate chain, known vulnerabilities. |
|
|
446
|
+
| **testssl.sh** | CLI TLS tester | `testssl.sh --full https://example.com` -- tests protocols, ciphers, vulnerabilities (BEAST, POODLE, Heartbleed, ROBOT), certificate details. Works on any TLS service, not just HTTPS. |
|
|
447
|
+
| **Mozilla SSL Configuration Generator** | TLS config templates | Generates secure Nginx/Apache/HAProxy configurations for Modern (TLS 1.3 only), Intermediate (TLS 1.2+), or Old compatibility levels. |
|
|
448
|
+
| **cert-manager** | Kubernetes certificate automation | Automatic TLS certificate provisioning and renewal via Let's Encrypt. |
|
|
449
|
+
| **Certbot** | ACME client | Automated Let's Encrypt certificate issuance and renewal. |
|
|
450
|
+
|
|
451
|
+
### Certificate Monitoring
|
|
452
|
+
|
|
453
|
+
| Tool | Purpose |
|
|
454
|
+
|------|---------|
|
|
455
|
+
| **Certificate Transparency Logs** | Monitor CT logs for unauthorized certificate issuance for your domains |
|
|
456
|
+
| **Keychecker / cert-manager** | Automated expiry alerting |
|
|
457
|
+
| **Uptime monitoring (Datadog, Pingdom)** | TLS certificate expiry checks as part of synthetic monitoring |
|
|
458
|
+
|
|
459
|
+
### Crypto Linting Rules (Semgrep Examples)
|
|
460
|
+
|
|
461
|
+
```yaml
|
|
462
|
+
# .semgrep/crypto-rules.yml
|
|
463
|
+
rules:
|
|
464
|
+
- id: weak-hash-md5
|
|
465
|
+
patterns:
|
|
466
|
+
- pattern: crypto.createHash('md5')
|
|
467
|
+
message: "MD5 is cryptographically broken. Use SHA-256 or SHA-3."
|
|
468
|
+
severity: ERROR
|
|
469
|
+
|
|
470
|
+
- id: ecb-mode
|
|
471
|
+
patterns:
|
|
472
|
+
- pattern: crypto.createCipheriv('aes-256-ecb', ...)
|
|
473
|
+
message: "ECB mode leaks plaintext patterns. Use AES-256-GCM."
|
|
474
|
+
severity: ERROR
|
|
475
|
+
|
|
476
|
+
- id: insecure-random
|
|
477
|
+
patterns:
|
|
478
|
+
- pattern: Math.random()
|
|
479
|
+
message: "Math.random() is not cryptographically secure. Use crypto.randomBytes()."
|
|
480
|
+
severity: WARNING
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
---
|
|
484
|
+
|
|
485
|
+
## 7. Platform-Specific Guidance
|
|
486
|
+
|
|
487
|
+
### 7.1 Node.js
|
|
488
|
+
|
|
489
|
+
**Primary library:** Built-in `crypto` module (backed by OpenSSL).
|
|
490
|
+
|
|
491
|
+
**Key practices:**
|
|
492
|
+
- Use `crypto.createCipheriv('aes-256-gcm', key, nonce)` -- never `createCipher()` (deprecated, uses MD5 key derivation).
|
|
493
|
+
- Generate keys/IVs with `crypto.randomBytes()`.
|
|
494
|
+
- Use `crypto.scryptSync()` or `crypto.pbkdf2Sync()` for password-based key derivation.
|
|
495
|
+
- Use `crypto.timingSafeEqual()` for all secret comparisons.
|
|
496
|
+
- For password hashing, use the `argon2` npm package (wraps reference C implementation) or `bcrypt`/`bcryptjs`.
|
|
497
|
+
- Set `crypto.constants.SSL_OP_NO_TLSv1 | crypto.constants.SSL_OP_NO_TLSv1_1` when creating TLS contexts.
|
|
498
|
+
- Node.js 20+ supports `crypto.subtle` (Web Crypto API) for browser-compatible operations.
|
|
499
|
+
|
|
500
|
+
**Common mistakes:**
|
|
501
|
+
- Using `createCipher()` instead of `createCipheriv()` (no control over IV)
|
|
502
|
+
- Calling `decipher.final()` without `decipher.setAuthTag()` for GCM
|
|
503
|
+
- Using `Buffer.from(password)` directly as a key instead of proper key derivation
|
|
504
|
+
|
|
505
|
+
### 7.2 Python
|
|
506
|
+
|
|
507
|
+
**Primary libraries:** `cryptography` (recommended), `PyCryptodome` (alternative).
|
|
508
|
+
|
|
509
|
+
**Key practices:**
|
|
510
|
+
- Use `cryptography.hazmat.primitives.ciphers.aead.AESGCM` for symmetric encryption.
|
|
511
|
+
- Use `cryptography.fernet.Fernet` for simple symmetric encryption (AES-128-CBC + HMAC, handles IV automatically).
|
|
512
|
+
- Generate randomness with `secrets.token_bytes()` or `os.urandom()`.
|
|
513
|
+
- Use `argon2-cffi` package for password hashing.
|
|
514
|
+
- Use `hmac.compare_digest()` for constant-time comparison.
|
|
515
|
+
- The `hashlib` module is for non-password hashing only (SHA-256, SHA-3).
|
|
516
|
+
- Avoid `PyCrypto` (unmaintained since 2014, known vulnerabilities).
|
|
517
|
+
|
|
518
|
+
**Common mistakes:**
|
|
519
|
+
- Using `hashlib.sha256(password).hexdigest()` for password storage
|
|
520
|
+
- Importing from `Crypto` (PyCrypto) instead of `Cryptodome` (PyCryptodome)
|
|
521
|
+
- Using `random.randint()` for token generation instead of `secrets`
|
|
522
|
+
|
|
523
|
+
### 7.3 Mobile -- iOS
|
|
524
|
+
|
|
525
|
+
**Primary framework:** Apple CryptoKit (iOS 13+).
|
|
526
|
+
|
|
527
|
+
**Key practices:**
|
|
528
|
+
- Use `AES.GCM` for symmetric encryption, `ChaChaPoly` for ChaCha20-Poly1305.
|
|
529
|
+
- Use `P256.Signing` / `Curve25519.Signing` for digital signatures.
|
|
530
|
+
- Use `SHA256.hash(data:)` for hashing; `HMAC<SHA256>` for MAC.
|
|
531
|
+
- Store keys in the **Secure Enclave** via `SecureEnclave.P256.Signing.PrivateKey()` for hardware-backed protection (keys never leave the chip).
|
|
532
|
+
- Use iOS **Keychain Services** for persistent key storage with `kSecAttrAccessibleWhenUnlockedThisDeviceOnly`.
|
|
533
|
+
- Enable App Transport Security (ATS) -- enforces TLS 1.2+ with forward secrecy by default.
|
|
534
|
+
- Use certificate pinning via `URLSession` delegate methods or `NSPinnedDomains` in Info.plist.
|
|
535
|
+
|
|
536
|
+
### 7.4 Mobile -- Android
|
|
537
|
+
|
|
538
|
+
**Primary frameworks:** `javax.crypto`, Android Keystore, Tink.
|
|
539
|
+
|
|
540
|
+
**Key practices:**
|
|
541
|
+
- Use **Android Keystore** for hardware-backed key storage (TEE or StrongBox on supported devices).
|
|
542
|
+
- Use **Google Tink** library for high-level crypto operations (replaces deprecated Jetpack Security crypto library, deprecated in v1.1.0).
|
|
543
|
+
- Generate keys with `KeyGenerator` using `AndroidKeyStore` provider.
|
|
544
|
+
- Use `Cipher.getInstance("AES/GCM/NoPadding")` for symmetric encryption.
|
|
545
|
+
- Enforce `setUserAuthenticationRequired(true)` for sensitive keys (requires biometric/PIN to use).
|
|
546
|
+
- Configure Network Security Config XML to enforce TLS and certificate pinning.
|
|
547
|
+
- Do NOT use `SharedPreferences` for secrets without encryption; use EncryptedSharedPreferences (via Tink).
|
|
548
|
+
|
|
549
|
+
### 7.5 TLS Termination -- Nginx
|
|
550
|
+
|
|
551
|
+
```nginx
|
|
552
|
+
# /etc/nginx/conf.d/tls.conf -- Mozilla Intermediate profile
|
|
553
|
+
ssl_protocols TLSv1.2 TLSv1.3;
|
|
554
|
+
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305;
|
|
555
|
+
ssl_prefer_server_ciphers off;
|
|
556
|
+
ssl_session_timeout 1d;
|
|
557
|
+
ssl_session_cache shared:SSL:10m;
|
|
558
|
+
ssl_session_tickets off;
|
|
559
|
+
|
|
560
|
+
# HSTS (1 year, includeSubDomains)
|
|
561
|
+
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
|
|
562
|
+
|
|
563
|
+
# OCSP stapling
|
|
564
|
+
ssl_stapling on;
|
|
565
|
+
ssl_stapling_verify on;
|
|
566
|
+
resolver 1.1.1.1 8.8.8.8 valid=300s;
|
|
567
|
+
```
|
|
568
|
+
|
|
569
|
+
### 7.6 Load Balancer / CDN
|
|
570
|
+
|
|
571
|
+
- **AWS ALB/NLB:** Use TLS 1.2+ security policies (`ELBSecurityPolicy-TLS13-*`). Terminate TLS at ALB; use ACM for certificate management.
|
|
572
|
+
- **Cloudflare:** Enable "Minimum TLS Version: 1.2", "Always Use HTTPS", "Authenticated Origin Pulls".
|
|
573
|
+
- **GCP Load Balancer:** Use managed SSL certificates; set SSL policy to MODERN or RESTRICTED profile.
|
|
574
|
+
- All platforms: re-encrypt traffic between load balancer and origin (TLS end-to-end), not just edge termination.
|
|
575
|
+
|
|
576
|
+
---
|
|
577
|
+
|
|
578
|
+
## 8. Incident Patterns
|
|
579
|
+
|
|
580
|
+
### 8.1 Detecting Cryptographic Failures
|
|
581
|
+
|
|
582
|
+
| Signal | Detection Method |
|
|
583
|
+
|--------|-----------------|
|
|
584
|
+
| Cleartext sensitive data in logs/DB | DLP scanning, log analysis rules, database column encryption audits |
|
|
585
|
+
| Weak TLS configuration | Continuous SSL Labs scanning, testssl.sh in CI/CD, certificate transparency monitoring |
|
|
586
|
+
| Deprecated algorithm usage | SAST (Semgrep, CodeQL), dependency scanning for vulnerable OpenSSL versions |
|
|
587
|
+
| Key leaks in source code | Secret scanning (GitGuardian, GitHub secret scanning, truffleHog) |
|
|
588
|
+
| Certificate expiry | Automated monitoring with 30/14/7/1 day alerts |
|
|
589
|
+
| Anomalous decryption patterns | Application-level logging of crypto operations (without logging keys/plaintext) |
|
|
590
|
+
|
|
591
|
+
### 8.2 Certificate Expiry Incidents
|
|
592
|
+
|
|
593
|
+
Certificate expiry is one of the most common crypto-related outages. Notable incidents:
|
|
594
|
+
- **Equifax (2017):** Expired SSL certificate on intrusion detection system allowed data exfiltration to go undetected for 76 days.
|
|
595
|
+
- **Microsoft Teams (2020):** Expired certificate caused a 3-hour global outage.
|
|
596
|
+
- **Let's Encrypt root expiry (2021):** IdenTrust DST Root CA X3 expiry broke older clients (Android < 7.1).
|
|
597
|
+
|
|
598
|
+
**Prevention:**
|
|
599
|
+
1. Automate certificate issuance and renewal (cert-manager, Certbot, ACM).
|
|
600
|
+
2. Monitor certificate expiry with multiple systems (infrastructure monitoring + dedicated cert checker).
|
|
601
|
+
3. Maintain a certificate inventory with owners and expiry dates.
|
|
602
|
+
4. Alert at 30, 14, 7, 3, and 1 day before expiry.
|
|
603
|
+
5. Use short-lived certificates (90 days via Let's Encrypt) to reduce blast radius.
|
|
604
|
+
|
|
605
|
+
### 8.3 Crypto Incident Response Playbook
|
|
606
|
+
|
|
607
|
+
1. **Identify scope:** Which keys, certificates, or algorithms are affected? What data was protected by them?
|
|
608
|
+
2. **Contain:** Revoke compromised keys/certificates immediately. Rotate affected credentials.
|
|
609
|
+
3. **Assess exposure:** Determine what data could have been decrypted or forged. Check logs for unauthorized access during the exposure window.
|
|
610
|
+
4. **Remediate:** Deploy patched algorithms/configurations. Regenerate all affected keys. Re-encrypt data with new keys if key compromise is confirmed.
|
|
611
|
+
5. **Notify:** If personal data was exposed, trigger breach notification procedures per GDPR (72h), PCI DSS, HIPAA, or applicable regulations.
|
|
612
|
+
6. **Post-mortem:** Document root cause, update cryptographic standards, add detection rules to prevent recurrence.
|
|
613
|
+
|
|
614
|
+
---
|
|
615
|
+
|
|
616
|
+
## 9. Compliance and Standards
|
|
617
|
+
|
|
618
|
+
### 9.1 OWASP A02:2021 -- Cryptographic Failures
|
|
619
|
+
|
|
620
|
+
The second most critical web application security risk. Key requirements:
|
|
621
|
+
- Classify data by sensitivity; apply crypto controls proportionally
|
|
622
|
+
- No unnecessary storage of sensitive data; purge when no longer needed
|
|
623
|
+
- Encrypt all sensitive data at rest and in transit
|
|
624
|
+
- Use current, strong algorithms, protocols, and keys with proper key management
|
|
625
|
+
- Encrypt all data in transit with TLS; enforce with HSTS
|
|
626
|
+
- Disable caching for responses containing sensitive data
|
|
627
|
+
- Do not use legacy protocols such as FTP or SMTP for transporting sensitive data
|
|
628
|
+
- Use authenticated encryption, not just encryption
|
|
629
|
+
|
|
630
|
+
### 9.2 NIST SP 800-57 -- Key Management
|
|
631
|
+
|
|
632
|
+
Three-part recommendation covering the full key lifecycle:
|
|
633
|
+
- **Part 1 (General):** Key types, states (pre-operational, operational, post-operational, destroyed), cryptoperiods, algorithm recommendations.
|
|
634
|
+
- **Part 2 (Organization):** Policy, roles, responsibilities for key management.
|
|
635
|
+
- **Part 3 (Application-Specific):** Guidance for PKI certificates, IPsec, TLS.
|
|
636
|
+
|
|
637
|
+
**Key cryptoperiods (NIST recommendations):**
|
|
638
|
+
| Key Type | Recommended Cryptoperiod |
|
|
639
|
+
|----------|------------------------|
|
|
640
|
+
| Symmetric encryption (data) | 1-2 years originator usage |
|
|
641
|
+
| Symmetric authentication (MAC) | 5 years max |
|
|
642
|
+
| Asymmetric (private signing) | 1-3 years |
|
|
643
|
+
| Asymmetric (public verification) | 1-3 years beyond signing key |
|
|
644
|
+
| Asymmetric (key transport) | 1-2 years |
|
|
645
|
+
| Root CA keys | 10-20 years |
|
|
646
|
+
|
|
647
|
+
### 9.3 FIPS 140-2 / FIPS 140-3
|
|
648
|
+
|
|
649
|
+
Federal standard for cryptographic module validation. Required for US government systems
|
|
650
|
+
and often for regulated industries (healthcare, finance).
|
|
651
|
+
|
|
652
|
+
- **FIPS 140-3** (effective 2019, superseding FIPS 140-2): four security levels (1-4).
|
|
653
|
+
- Mandates use of NIST-approved algorithms: AES, SHA-2/SHA-3, RSA, ECDSA, HMAC.
|
|
654
|
+
- Requires validated entropy sources and DRBG (Deterministic Random Bit Generators).
|
|
655
|
+
- Hardware Security Modules (HSMs) at Level 3+ provide tamper-evident physical security.
|
|
656
|
+
- FIPS 140-2 validations still accepted but no new validations issued.
|
|
657
|
+
|
|
658
|
+
### 9.4 PCI DSS 4.0 / 4.0.1 -- Encryption Requirements
|
|
659
|
+
|
|
660
|
+
PCI DSS 4.0 full compliance deadline: **March 31, 2025**. Key cryptographic requirements:
|
|
661
|
+
|
|
662
|
+
- **Requirement 3:** Protect stored account data. Use strong cryptography (AES-128+, RSA-2048+, ECDSA-224+, TDES for legacy only).
|
|
663
|
+
- **Requirement 4:** Protect cardholder data in transit with strong cryptography (TLS 1.2+ required).
|
|
664
|
+
- **Requirement 4.2.1 (new):** Maintain inventory of trusted keys and certificates. Review cipher suites and protocols at least annually.
|
|
665
|
+
- **Requirement 3.6:** Document and implement key management procedures covering generation, distribution, storage, rotation, and destruction.
|
|
666
|
+
- **Requirement 12.3.3:** Perform annual cryptographic cipher suite and protocol review.
|
|
667
|
+
|
|
668
|
+
### 9.5 GDPR and Data Protection
|
|
669
|
+
|
|
670
|
+
- Article 32: Implement encryption and pseudonymization as appropriate technical measures.
|
|
671
|
+
- Encryption of personal data can reduce breach notification requirements (encrypted data may not constitute a breach if the key is not compromised).
|
|
672
|
+
- No specific algorithm mandates, but "state of the art" standard implies current best practices.
|
|
673
|
+
|
|
674
|
+
---
|
|
675
|
+
|
|
676
|
+
## 10. Code Examples
|
|
677
|
+
|
|
678
|
+
### 10.1 AES-256-GCM Encryption/Decryption (TypeScript/Node.js)
|
|
679
|
+
|
|
680
|
+
```typescript
|
|
681
|
+
import { randomBytes, createCipheriv, createDecipheriv } from 'crypto';
|
|
682
|
+
|
|
683
|
+
const ALGORITHM = 'aes-256-gcm';
|
|
684
|
+
const NONCE_LENGTH = 12; // 96 bits, recommended for GCM
|
|
685
|
+
const TAG_LENGTH = 16; // 128-bit auth tag
|
|
686
|
+
|
|
687
|
+
/**
|
|
688
|
+
* Encrypt plaintext with AES-256-GCM.
|
|
689
|
+
* Returns nonce + ciphertext + tag (all needed for decryption).
|
|
690
|
+
*/
|
|
691
|
+
export function encrypt(plaintext: string, key: Buffer): Buffer {
|
|
692
|
+
if (key.length !== 32) {
|
|
693
|
+
throw new Error('Key must be 32 bytes (256 bits)');
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
const nonce = randomBytes(NONCE_LENGTH);
|
|
697
|
+
const cipher = createCipheriv(ALGORITHM, key, nonce);
|
|
698
|
+
|
|
699
|
+
const encrypted = Buffer.concat([
|
|
700
|
+
cipher.update(plaintext, 'utf8'),
|
|
701
|
+
cipher.final(),
|
|
702
|
+
]);
|
|
703
|
+
const tag = cipher.getAuthTag();
|
|
704
|
+
|
|
705
|
+
// Format: [12-byte nonce][ciphertext][16-byte tag]
|
|
706
|
+
return Buffer.concat([nonce, encrypted, tag]);
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
/**
|
|
710
|
+
* Decrypt AES-256-GCM ciphertext.
|
|
711
|
+
* Verifies authentication tag before returning plaintext.
|
|
712
|
+
*/
|
|
713
|
+
export function decrypt(payload: Buffer, key: Buffer): string {
|
|
714
|
+
if (key.length !== 32) {
|
|
715
|
+
throw new Error('Key must be 32 bytes (256 bits)');
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
const nonce = payload.subarray(0, NONCE_LENGTH);
|
|
719
|
+
const tag = payload.subarray(payload.length - TAG_LENGTH);
|
|
720
|
+
const ciphertext = payload.subarray(NONCE_LENGTH, payload.length - TAG_LENGTH);
|
|
721
|
+
|
|
722
|
+
const decipher = createDecipheriv(ALGORITHM, key, nonce);
|
|
723
|
+
decipher.setAuthTag(tag); // CRITICAL: must set before final()
|
|
724
|
+
|
|
725
|
+
const decrypted = Buffer.concat([
|
|
726
|
+
decipher.update(ciphertext),
|
|
727
|
+
decipher.final(), // Throws if tag verification fails
|
|
728
|
+
]);
|
|
729
|
+
|
|
730
|
+
return decrypted.toString('utf8');
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
// Usage
|
|
734
|
+
const key = randomBytes(32); // Store securely, never hard-code
|
|
735
|
+
const encrypted = encrypt('sensitive data', key);
|
|
736
|
+
const decrypted = decrypt(encrypted, key);
|
|
737
|
+
```
|
|
738
|
+
|
|
739
|
+
### 10.2 AES-256-GCM Encryption/Decryption (Python)
|
|
740
|
+
|
|
741
|
+
```python
|
|
742
|
+
import os
|
|
743
|
+
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
|
744
|
+
|
|
745
|
+
NONCE_LENGTH = 12 # 96 bits
|
|
746
|
+
|
|
747
|
+
def encrypt(plaintext: str, key: bytes) -> bytes:
|
|
748
|
+
"""Encrypt with AES-256-GCM. Returns nonce + ciphertext + tag."""
|
|
749
|
+
if len(key) != 32:
|
|
750
|
+
raise ValueError("Key must be 32 bytes (256 bits)")
|
|
751
|
+
|
|
752
|
+
nonce = os.urandom(NONCE_LENGTH)
|
|
753
|
+
aesgcm = AESGCM(key)
|
|
754
|
+
# encrypt() returns ciphertext + 16-byte tag appended
|
|
755
|
+
ciphertext_and_tag = aesgcm.encrypt(nonce, plaintext.encode("utf-8"), None)
|
|
756
|
+
return nonce + ciphertext_and_tag
|
|
757
|
+
|
|
758
|
+
|
|
759
|
+
def decrypt(payload: bytes, key: bytes) -> str:
|
|
760
|
+
"""Decrypt AES-256-GCM. Raises InvalidTag if tampered."""
|
|
761
|
+
if len(key) != 32:
|
|
762
|
+
raise ValueError("Key must be 32 bytes (256 bits)")
|
|
763
|
+
|
|
764
|
+
nonce = payload[:NONCE_LENGTH]
|
|
765
|
+
ciphertext_and_tag = payload[NONCE_LENGTH:]
|
|
766
|
+
aesgcm = AESGCM(key)
|
|
767
|
+
plaintext = aesgcm.decrypt(nonce, ciphertext_and_tag, None)
|
|
768
|
+
return plaintext.decode("utf-8")
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
# Usage
|
|
772
|
+
key = AESGCM.generate_key(bit_length=256)
|
|
773
|
+
encrypted = encrypt("sensitive data", key)
|
|
774
|
+
decrypted = decrypt(encrypted, key)
|
|
775
|
+
```
|
|
776
|
+
|
|
777
|
+
### 10.3 Password Hashing with Argon2id (Python)
|
|
778
|
+
|
|
779
|
+
```python
|
|
780
|
+
from argon2 import PasswordHasher
|
|
781
|
+
from argon2.exceptions import VerifyMismatchError
|
|
782
|
+
|
|
783
|
+
# Configure Argon2id with OWASP-recommended parameters
|
|
784
|
+
ph = PasswordHasher(
|
|
785
|
+
time_cost=3, # Number of iterations
|
|
786
|
+
memory_cost=65536, # 64 MiB memory usage
|
|
787
|
+
parallelism=1, # Degree of parallelism
|
|
788
|
+
hash_len=32, # Output hash length in bytes
|
|
789
|
+
salt_len=16, # Salt length in bytes
|
|
790
|
+
type=2, # 2 = Argon2id (hybrid)
|
|
791
|
+
)
|
|
792
|
+
|
|
793
|
+
def hash_password(password: str) -> str:
|
|
794
|
+
"""Hash a password with Argon2id. Returns encoded hash string."""
|
|
795
|
+
return ph.hash(password)
|
|
796
|
+
|
|
797
|
+
def verify_password(password: str, stored_hash: str) -> bool:
|
|
798
|
+
"""Verify a password against a stored Argon2id hash."""
|
|
799
|
+
try:
|
|
800
|
+
return ph.verify(stored_hash, password)
|
|
801
|
+
except VerifyMismatchError:
|
|
802
|
+
return False
|
|
803
|
+
|
|
804
|
+
# Usage
|
|
805
|
+
hashed = hash_password("user-password-here")
|
|
806
|
+
# Store `hashed` in database (contains algorithm, params, salt, hash)
|
|
807
|
+
# Example: $argon2id$v=19$m=65536,t=3,p=1$c29tZXNhbHQ$hash...
|
|
808
|
+
|
|
809
|
+
is_valid = verify_password("user-password-here", hashed)
|
|
810
|
+
|
|
811
|
+
# Check if rehashing is needed (e.g., after increasing parameters)
|
|
812
|
+
if is_valid and ph.check_needs_rehash(hashed):
|
|
813
|
+
new_hash = hash_password("user-password-here")
|
|
814
|
+
# Update stored hash in database
|
|
815
|
+
```
|
|
816
|
+
|
|
817
|
+
### 10.4 Password Hashing with Argon2id (TypeScript/Node.js)
|
|
818
|
+
|
|
819
|
+
```typescript
|
|
820
|
+
import argon2 from 'argon2';
|
|
821
|
+
|
|
822
|
+
const ARGON2_OPTIONS: argon2.Options = {
|
|
823
|
+
type: argon2.argon2id,
|
|
824
|
+
memoryCost: 65536, // 64 MiB
|
|
825
|
+
timeCost: 3, // 3 iterations
|
|
826
|
+
parallelism: 1,
|
|
827
|
+
hashLength: 32,
|
|
828
|
+
saltLength: 16,
|
|
829
|
+
};
|
|
830
|
+
|
|
831
|
+
export async function hashPassword(password: string): Promise<string> {
|
|
832
|
+
return argon2.hash(password, ARGON2_OPTIONS);
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
export async function verifyPassword(
|
|
836
|
+
password: string,
|
|
837
|
+
storedHash: string
|
|
838
|
+
): Promise<boolean> {
|
|
839
|
+
try {
|
|
840
|
+
return await argon2.verify(storedHash, password);
|
|
841
|
+
} catch {
|
|
842
|
+
return false;
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
// Check if rehash is needed after parameter upgrade
|
|
847
|
+
export function needsRehash(storedHash: string): boolean {
|
|
848
|
+
return argon2.needsRehash(storedHash, ARGON2_OPTIONS);
|
|
849
|
+
}
|
|
850
|
+
```
|
|
851
|
+
|
|
852
|
+
### 10.5 HMAC Signing and Verification (TypeScript/Node.js)
|
|
853
|
+
|
|
854
|
+
```typescript
|
|
855
|
+
import { createHmac, timingSafeEqual } from 'crypto';
|
|
856
|
+
|
|
857
|
+
const HMAC_ALGORITHM = 'sha256';
|
|
858
|
+
|
|
859
|
+
/**
|
|
860
|
+
* Generate HMAC signature for a message.
|
|
861
|
+
*/
|
|
862
|
+
export function sign(message: string, secret: Buffer): string {
|
|
863
|
+
return createHmac(HMAC_ALGORITHM, secret)
|
|
864
|
+
.update(message, 'utf8')
|
|
865
|
+
.digest('hex');
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
/**
|
|
869
|
+
* Verify HMAC signature using constant-time comparison.
|
|
870
|
+
*/
|
|
871
|
+
export function verify(
|
|
872
|
+
message: string,
|
|
873
|
+
signature: string,
|
|
874
|
+
secret: Buffer
|
|
875
|
+
): boolean {
|
|
876
|
+
const expected = sign(message, secret);
|
|
877
|
+
|
|
878
|
+
// CRITICAL: Use constant-time comparison to prevent timing attacks
|
|
879
|
+
if (expected.length !== signature.length) {
|
|
880
|
+
return false;
|
|
881
|
+
}
|
|
882
|
+
return timingSafeEqual(
|
|
883
|
+
Buffer.from(expected, 'hex'),
|
|
884
|
+
Buffer.from(signature, 'hex')
|
|
885
|
+
);
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
// Usage: Webhook signature verification
|
|
889
|
+
const secret = Buffer.from(process.env.WEBHOOK_SECRET!, 'hex');
|
|
890
|
+
const payload = '{"event": "payment.completed"}';
|
|
891
|
+
const receivedSignature = req.headers['x-signature'] as string;
|
|
892
|
+
|
|
893
|
+
if (!verify(payload, receivedSignature, secret)) {
|
|
894
|
+
throw new Error('Invalid webhook signature');
|
|
895
|
+
}
|
|
896
|
+
```
|
|
897
|
+
|
|
898
|
+
### 10.6 Secure Random Token Generation
|
|
899
|
+
|
|
900
|
+
```typescript
|
|
901
|
+
// TypeScript/Node.js
|
|
902
|
+
import { randomBytes, randomUUID } from 'crypto';
|
|
903
|
+
|
|
904
|
+
// 256-bit hex token (for API keys, session tokens)
|
|
905
|
+
const token = randomBytes(32).toString('hex'); // 64 hex chars
|
|
906
|
+
|
|
907
|
+
// URL-safe base64 token
|
|
908
|
+
const urlSafeToken = randomBytes(32).toString('base64url');
|
|
909
|
+
|
|
910
|
+
// UUID v4 (122 bits of randomness)
|
|
911
|
+
const uuid = randomUUID();
|
|
912
|
+
```
|
|
913
|
+
|
|
914
|
+
```python
|
|
915
|
+
# Python
|
|
916
|
+
import secrets
|
|
917
|
+
|
|
918
|
+
# 256-bit hex token
|
|
919
|
+
token = secrets.token_hex(32)
|
|
920
|
+
|
|
921
|
+
# URL-safe base64 token
|
|
922
|
+
url_safe_token = secrets.token_urlsafe(32)
|
|
923
|
+
|
|
924
|
+
# For password reset tokens, invitation codes, etc.
|
|
925
|
+
reset_token = secrets.token_urlsafe(48) # 384 bits
|
|
926
|
+
```
|
|
927
|
+
|
|
928
|
+
### 10.7 Vulnerable vs. Secure Patterns Summary
|
|
929
|
+
|
|
930
|
+
```javascript
|
|
931
|
+
// ---- VULNERABLE PATTERNS ----
|
|
932
|
+
|
|
933
|
+
// 1. ECB mode
|
|
934
|
+
crypto.createCipheriv('aes-256-ecb', key, null);
|
|
935
|
+
|
|
936
|
+
// 2. Hard-coded key
|
|
937
|
+
const KEY = 'mysecretkey12345';
|
|
938
|
+
|
|
939
|
+
// 3. Math.random for tokens
|
|
940
|
+
const token = Math.random().toString(36).substring(2);
|
|
941
|
+
|
|
942
|
+
// 4. SHA-256 for passwords
|
|
943
|
+
const hash = crypto.createHash('sha256').update(password).digest('hex');
|
|
944
|
+
|
|
945
|
+
// 5. String comparison for secrets
|
|
946
|
+
if (token === expectedToken) { grant(); }
|
|
947
|
+
|
|
948
|
+
// 6. Disabled TLS verification
|
|
949
|
+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
|
|
950
|
+
|
|
951
|
+
// 7. Deprecated createCipher (no IV control)
|
|
952
|
+
crypto.createCipher('aes-256-cbc', password);
|
|
953
|
+
|
|
954
|
+
|
|
955
|
+
// ---- SECURE PATTERNS ----
|
|
956
|
+
|
|
957
|
+
// 1. GCM authenticated encryption
|
|
958
|
+
const nonce = crypto.randomBytes(12);
|
|
959
|
+
crypto.createCipheriv('aes-256-gcm', key, nonce);
|
|
960
|
+
|
|
961
|
+
// 2. Key from environment / secret manager
|
|
962
|
+
const KEY = Buffer.from(process.env.ENCRYPTION_KEY, 'hex');
|
|
963
|
+
|
|
964
|
+
// 3. CSPRNG for tokens
|
|
965
|
+
const token = crypto.randomBytes(32).toString('hex');
|
|
966
|
+
|
|
967
|
+
// 4. Argon2id for passwords
|
|
968
|
+
const hash = await argon2.hash(password, { type: argon2.argon2id });
|
|
969
|
+
|
|
970
|
+
// 5. Constant-time comparison
|
|
971
|
+
crypto.timingSafeEqual(Buffer.from(token), Buffer.from(expectedToken));
|
|
972
|
+
|
|
973
|
+
// 6. Proper CA certificate handling
|
|
974
|
+
new https.Agent({ ca: fs.readFileSync('/etc/ssl/certs/ca-bundle.crt') });
|
|
975
|
+
|
|
976
|
+
// 7. createCipheriv with explicit IV
|
|
977
|
+
const iv = crypto.randomBytes(16);
|
|
978
|
+
crypto.createCipheriv('aes-256-cbc', derivedKey, iv);
|
|
979
|
+
```
|
|
980
|
+
|
|
981
|
+
---
|
|
982
|
+
|
|
983
|
+
## References and Sources
|
|
984
|
+
|
|
985
|
+
- [OWASP Top 10:2021 -- A02 Cryptographic Failures](https://owasp.org/Top10/2021/A02_2021-Cryptographic_Failures/)
|
|
986
|
+
- [NIST Post-Quantum Cryptography Standards (FIPS 203, 204, 205)](https://www.nist.gov/news-events/news/2024/08/nist-releases-first-3-finalized-post-quantum-encryption-standards)
|
|
987
|
+
- [NIST SP 800-57 Key Management Recommendations](https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-57Pt3r1.pdf)
|
|
988
|
+
- [NIST FIPS 140-3](https://csrc.nist.gov/pubs/fips/140-3/final)
|
|
989
|
+
- [PCI DSS 4.0 Cryptographic Requirements](https://www.thoropass.com/blog/pci-dss-encryption-requirements)
|
|
990
|
+
- [Adobe 2013 Breach -- Schneier on Security](https://www.schneier.com/blog/archives/2013/11/cryptographic_b.html)
|
|
991
|
+
- [Ashley Madison Password Cracking -- CynoSure Prime](https://blog.cynosureprime.com/2015/09/how-we-cracked-millions-of-ashley.html)
|
|
992
|
+
- [CWE-327: Broken or Risky Cryptographic Algorithm](https://cwe.mitre.org/data/definitions/327.html)
|
|
993
|
+
- [OWASP Password Storage Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html)
|
|
994
|
+
- [Mozilla SSL Configuration Generator](https://ssl-config.mozilla.org/)
|
|
995
|
+
- [testssl.sh](https://testssl.sh/)
|
|
996
|
+
- [Semgrep Security Rules](https://semgrep.dev/p/security-audit)
|
|
997
|
+
- [Node.js Crypto Documentation](https://nodejs.org/api/crypto.html)
|
|
998
|
+
- [Python cryptography Library](https://cryptography.io/)
|
|
999
|
+
- [Apple CryptoKit Documentation](https://developer.apple.com/documentation/cryptokit)
|
|
1000
|
+
- [Android Cryptography Guide](https://developer.android.com/privacy-and-security/cryptography)
|
|
1001
|
+
- [NIST SP 800-52 Rev. 2 -- TLS Guidelines](https://csrc.nist.gov/publications/detail/sp/800-52/rev-2/final)
|