|
| 1 | +// Copyright 2025 Cyber Security Non-Profit (CSNP) |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package cryptosamples; |
| 5 | + |
| 6 | +import javax.crypto.Cipher; |
| 7 | +import javax.crypto.KeyGenerator; |
| 8 | +import javax.crypto.SecretKey; |
| 9 | +import javax.crypto.spec.GCMParameterSpec; |
| 10 | +import java.security.*; |
| 11 | +import java.security.spec.ECGenParameterSpec; |
| 12 | + |
| 13 | +/** |
| 14 | + * Sample cryptographic code for testing the scanner (Java). |
| 15 | + * Run: cryptoscan scan ./crypto-samples to see detection results. |
| 16 | + */ |
| 17 | +public class CryptoSamples { |
| 18 | + |
| 19 | + // ======================================================================== |
| 20 | + // QUANTUM VULNERABLE - Asymmetric Cryptography |
| 21 | + // ======================================================================== |
| 22 | + |
| 23 | + /** |
| 24 | + * Generate RSA key pair - QUANTUM VULNERABLE |
| 25 | + * Remediation: Migrate to ML-KEM (FIPS 203) |
| 26 | + */ |
| 27 | + public static KeyPair generateRSAKeyPair() throws Exception { |
| 28 | + KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); |
| 29 | + keyGen.initialize(2048); |
| 30 | + return keyGen.generateKeyPair(); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Generate weak RSA-1024 key - CRITICAL (classically weak) |
| 35 | + */ |
| 36 | + public static KeyPair generateWeakRSAKey() throws Exception { |
| 37 | + KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); |
| 38 | + keyGen.initialize(1024); // Too small! |
| 39 | + return keyGen.generateKeyPair(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Generate ECDSA key - QUANTUM VULNERABLE |
| 44 | + * Remediation: Migrate to ML-DSA (FIPS 204) |
| 45 | + */ |
| 46 | + public static KeyPair generateECDSAKeyPair() throws Exception { |
| 47 | + KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC"); |
| 48 | + keyGen.initialize(new ECGenParameterSpec("secp256r1")); |
| 49 | + return keyGen.generateKeyPair(); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Sign with ECDSA - QUANTUM VULNERABLE |
| 54 | + */ |
| 55 | + public static byte[] signWithECDSA(PrivateKey privateKey, byte[] data) throws Exception { |
| 56 | + Signature signature = Signature.getInstance("SHA256withECDSA"); |
| 57 | + signature.initSign(privateKey); |
| 58 | + signature.update(data); |
| 59 | + return signature.sign(); |
| 60 | + } |
| 61 | + |
| 62 | + // ======================================================================== |
| 63 | + // QUANTUM PARTIAL - Symmetric Cryptography |
| 64 | + // ======================================================================== |
| 65 | + |
| 66 | + /** |
| 67 | + * Encrypt with AES-256-GCM - QUANTUM PARTIAL (acceptable) |
| 68 | + */ |
| 69 | + public static byte[] encryptAES256GCM(byte[] key, byte[] plaintext) throws Exception { |
| 70 | + SecretKey secretKey = new javax.crypto.spec.SecretKeySpec(key, "AES"); |
| 71 | + Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); |
| 72 | + byte[] iv = new byte[12]; |
| 73 | + SecureRandom random = new SecureRandom(); |
| 74 | + random.nextBytes(iv); |
| 75 | + cipher.init(Cipher.ENCRYPT_MODE, secretKey, new GCMParameterSpec(128, iv)); |
| 76 | + return cipher.doFinal(plaintext); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Encrypt with AES-128 - needs upgrade to AES-256 |
| 81 | + */ |
| 82 | + public static SecretKey generateAES128Key() throws Exception { |
| 83 | + KeyGenerator keyGen = KeyGenerator.getInstance("AES"); |
| 84 | + keyGen.init(128); // Should be 256 |
| 85 | + return keyGen.generateKey(); |
| 86 | + } |
| 87 | + |
| 88 | + // ======================================================================== |
| 89 | + // BROKEN/WEAK - Should be replaced immediately |
| 90 | + // ======================================================================== |
| 91 | + |
| 92 | + /** |
| 93 | + * Hash with MD5 - BROKEN (collision attacks exist) |
| 94 | + */ |
| 95 | + public static byte[] hashMD5(byte[] data) throws Exception { |
| 96 | + MessageDigest md = MessageDigest.getInstance("MD5"); |
| 97 | + return md.digest(data); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Hash with SHA-1 - WEAK (collision attacks demonstrated) |
| 102 | + */ |
| 103 | + public static byte[] hashSHA1(byte[] data) throws Exception { |
| 104 | + MessageDigest md = MessageDigest.getInstance("SHA-1"); |
| 105 | + return md.digest(data); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Encrypt with DES - BROKEN (56-bit key) |
| 110 | + */ |
| 111 | + public static byte[] encryptDES(byte[] key, byte[] plaintext) throws Exception { |
| 112 | + Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); |
| 113 | + // DES is completely broken |
| 114 | + return cipher.doFinal(plaintext); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Encrypt with 3DES - WEAK (deprecated) |
| 119 | + */ |
| 120 | + public static byte[] encrypt3DES(byte[] key, byte[] plaintext) throws Exception { |
| 121 | + Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); |
| 122 | + return cipher.doFinal(plaintext); |
| 123 | + } |
| 124 | + |
| 125 | + // ======================================================================== |
| 126 | + // QUANTUM SAFE - Hash functions |
| 127 | + // ======================================================================== |
| 128 | + |
| 129 | + /** |
| 130 | + * Hash with SHA-256 - QUANTUM SAFE |
| 131 | + */ |
| 132 | + public static byte[] hashSHA256(byte[] data) throws Exception { |
| 133 | + MessageDigest md = MessageDigest.getInstance("SHA-256"); |
| 134 | + return md.digest(data); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Hash with SHA-512 - QUANTUM SAFE |
| 139 | + */ |
| 140 | + public static byte[] hashSHA512(byte[] data) throws Exception { |
| 141 | + MessageDigest md = MessageDigest.getInstance("SHA-512"); |
| 142 | + return md.digest(data); |
| 143 | + } |
| 144 | + |
| 145 | + // ======================================================================== |
| 146 | + // PATTERNS THAT SHOULD BE FILTERED |
| 147 | + // ======================================================================== |
| 148 | + |
| 149 | + /** |
| 150 | + * Logs crypto info - should be filtered (print statements) |
| 151 | + */ |
| 152 | + public static void logCryptoInfo() { |
| 153 | + System.out.println("Using RSA-2048 for key exchange"); |
| 154 | + System.out.println("Signing with ECDSA P-256"); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Validates input - should be filtered (error messages) |
| 159 | + */ |
| 160 | + public static void validateKeyType(String keyType) { |
| 161 | + if (keyType == null) { |
| 162 | + throw new IllegalArgumentException("keyType must be a valid RSA or ECDSA key"); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments