Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: cryptomgr - Test ciphers using ECB

As it is we only test ciphers when combined with a mode. That means
users that do not invoke a mode of operations may get an untested
cipher.

This patch tests all ciphers using the ECB mode so that simple cipher
users such as ansi-cprng are also protected.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

+169 -50
+1 -4
crypto/algboss.c
··· 210 210 CRYPTO_ALG_TYPE_BLKCIPHER_MASK) && !(type & CRYPTO_ALG_GENIV)) 211 211 goto skiptest; 212 212 213 - if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) 214 - goto skiptest; 215 - 216 - err = alg_test(param->driver, param->alg, 0, CRYPTO_ALG_TESTED); 213 + err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED); 217 214 218 215 skiptest: 219 216 crypto_alg_tested(param->driver, err);
+168 -46
crypto/testmgr.c
··· 541 541 return ret; 542 542 } 543 543 544 - static int test_cipher(struct crypto_ablkcipher *tfm, int enc, 544 + static int test_cipher(struct crypto_cipher *tfm, int enc, 545 545 struct cipher_testvec *template, unsigned int tcount) 546 + { 547 + const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm)); 548 + unsigned int i, j, k; 549 + int ret; 550 + char *q; 551 + const char *e; 552 + void *data; 553 + 554 + if (enc == ENCRYPT) 555 + e = "encryption"; 556 + else 557 + e = "decryption"; 558 + 559 + j = 0; 560 + for (i = 0; i < tcount; i++) { 561 + if (template[i].np) 562 + continue; 563 + 564 + j++; 565 + 566 + data = xbuf[0]; 567 + memcpy(data, template[i].input, template[i].ilen); 568 + 569 + crypto_cipher_clear_flags(tfm, ~0); 570 + if (template[i].wk) 571 + crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY); 572 + 573 + ret = crypto_cipher_setkey(tfm, template[i].key, 574 + template[i].klen); 575 + if (!ret == template[i].fail) { 576 + printk(KERN_ERR "alg: cipher: setkey failed " 577 + "on test %d for %s: flags=%x\n", j, 578 + algo, crypto_cipher_get_flags(tfm)); 579 + goto out; 580 + } else if (ret) 581 + continue; 582 + 583 + for (k = 0; k < template[i].ilen; 584 + k += crypto_cipher_blocksize(tfm)) { 585 + if (enc) 586 + crypto_cipher_encrypt_one(tfm, data + k, 587 + data + k); 588 + else 589 + crypto_cipher_decrypt_one(tfm, data + k, 590 + data + k); 591 + } 592 + 593 + q = data; 594 + if (memcmp(q, template[i].result, template[i].rlen)) { 595 + printk(KERN_ERR "alg: cipher: Test %d failed " 596 + "on %s for %s\n", j, e, algo); 597 + hexdump(q, template[i].rlen); 598 + ret = -EINVAL; 599 + goto out; 600 + } 601 + } 602 + 603 + ret = 0; 604 + 605 + out: 606 + return ret; 607 + } 608 + 609 + static int test_skcipher(struct crypto_ablkcipher *tfm, int enc, 610 + struct cipher_testvec *template, unsigned int tcount) 546 611 { 547 612 const char *algo = 548 613 crypto_tfm_alg_driver_name(crypto_ablkcipher_tfm(tfm)); ··· 630 565 631 566 req = ablkcipher_request_alloc(tfm, GFP_KERNEL); 632 567 if (!req) { 633 - printk(KERN_ERR "alg: cipher: Failed to allocate request for " 634 - "%s\n", algo); 568 + printk(KERN_ERR "alg: skcipher: Failed to allocate request " 569 + "for %s\n", algo); 635 570 ret = -ENOMEM; 636 571 goto out; 637 572 } ··· 660 595 ret = crypto_ablkcipher_setkey(tfm, template[i].key, 661 596 template[i].klen); 662 597 if (!ret == template[i].fail) { 663 - printk(KERN_ERR "alg: cipher: setkey failed " 598 + printk(KERN_ERR "alg: skcipher: setkey failed " 664 599 "on test %d for %s: flags=%x\n", j, 665 600 algo, crypto_ablkcipher_get_flags(tfm)); 666 601 goto out; ··· 688 623 } 689 624 /* fall through */ 690 625 default: 691 - printk(KERN_ERR "alg: cipher: %s failed on " 626 + printk(KERN_ERR "alg: skcipher: %s failed on " 692 627 "test %d for %s: ret=%d\n", e, j, algo, 693 628 -ret); 694 629 goto out; ··· 696 631 697 632 q = data; 698 633 if (memcmp(q, template[i].result, template[i].rlen)) { 699 - printk(KERN_ERR "alg: cipher: Test %d failed " 700 - "on %s for %s\n", j, e, algo); 634 + printk(KERN_ERR "alg: skcipher: Test %d " 635 + "failed on %s for %s\n", j, e, algo); 701 636 hexdump(q, template[i].rlen); 702 637 ret = -EINVAL; 703 638 goto out; ··· 724 659 ret = crypto_ablkcipher_setkey(tfm, template[i].key, 725 660 template[i].klen); 726 661 if (!ret == template[i].fail) { 727 - printk(KERN_ERR "alg: cipher: setkey failed " 662 + printk(KERN_ERR "alg: skcipher: setkey failed " 728 663 "on chunk test %d for %s: flags=%x\n", 729 664 j, algo, 730 665 crypto_ablkcipher_get_flags(tfm)); ··· 775 710 } 776 711 /* fall through */ 777 712 default: 778 - printk(KERN_ERR "alg: cipher: %s failed on " 713 + printk(KERN_ERR "alg: skcipher: %s failed on " 779 714 "chunk test %d for %s: ret=%d\n", e, j, 780 715 algo, -ret); 781 716 goto out; ··· 789 724 790 725 if (memcmp(q, template[i].result + temp, 791 726 template[i].tap[k])) { 792 - printk(KERN_ERR "alg: cipher: Chunk " 727 + printk(KERN_ERR "alg: skcipher: Chunk " 793 728 "test %d failed on %s at page " 794 729 "%u for %s\n", j, e, k, algo); 795 730 hexdump(q, template[i].tap[k]); ··· 800 735 for (n = 0; offset_in_page(q + n) && q[n]; n++) 801 736 ; 802 737 if (n) { 803 - printk(KERN_ERR "alg: cipher: " 738 + printk(KERN_ERR "alg: skcipher: " 804 739 "Result buffer corruption in " 805 740 "chunk test %d on %s at page " 806 741 "%u for %s: %u bytes:\n", j, e, ··· 914 849 static int alg_test_cipher(const struct alg_test_desc *desc, 915 850 const char *driver, u32 type, u32 mask) 916 851 { 917 - struct crypto_ablkcipher *tfm; 852 + struct crypto_cipher *tfm; 918 853 int err = 0; 919 854 920 - tfm = crypto_alloc_ablkcipher(driver, type, mask); 855 + tfm = crypto_alloc_cipher(driver, type, mask); 921 856 if (IS_ERR(tfm)) { 922 857 printk(KERN_ERR "alg: cipher: Failed to load transform for " 923 858 "%s: %ld\n", driver, PTR_ERR(tfm)); ··· 934 869 if (desc->suite.cipher.dec.vecs) 935 870 err = test_cipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs, 936 871 desc->suite.cipher.dec.count); 872 + 873 + out: 874 + crypto_free_cipher(tfm); 875 + return err; 876 + } 877 + 878 + static int alg_test_skcipher(const struct alg_test_desc *desc, 879 + const char *driver, u32 type, u32 mask) 880 + { 881 + struct crypto_ablkcipher *tfm; 882 + int err = 0; 883 + 884 + tfm = crypto_alloc_ablkcipher(driver, type, mask); 885 + if (IS_ERR(tfm)) { 886 + printk(KERN_ERR "alg: skcipher: Failed to load transform for " 887 + "%s: %ld\n", driver, PTR_ERR(tfm)); 888 + return PTR_ERR(tfm); 889 + } 890 + 891 + if (desc->suite.cipher.enc.vecs) { 892 + err = test_skcipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs, 893 + desc->suite.cipher.enc.count); 894 + if (err) 895 + goto out; 896 + } 897 + 898 + if (desc->suite.cipher.dec.vecs) 899 + err = test_skcipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs, 900 + desc->suite.cipher.dec.count); 937 901 938 902 out: 939 903 crypto_free_ablkcipher(tfm); ··· 1014 920 static const struct alg_test_desc alg_test_descs[] = { 1015 921 { 1016 922 .alg = "cbc(aes)", 1017 - .test = alg_test_cipher, 923 + .test = alg_test_skcipher, 1018 924 .suite = { 1019 925 .cipher = { 1020 926 .enc = { ··· 1029 935 } 1030 936 }, { 1031 937 .alg = "cbc(anubis)", 1032 - .test = alg_test_cipher, 938 + .test = alg_test_skcipher, 1033 939 .suite = { 1034 940 .cipher = { 1035 941 .enc = { ··· 1044 950 } 1045 951 }, { 1046 952 .alg = "cbc(blowfish)", 1047 - .test = alg_test_cipher, 953 + .test = alg_test_skcipher, 1048 954 .suite = { 1049 955 .cipher = { 1050 956 .enc = { ··· 1059 965 } 1060 966 }, { 1061 967 .alg = "cbc(camellia)", 1062 - .test = alg_test_cipher, 968 + .test = alg_test_skcipher, 1063 969 .suite = { 1064 970 .cipher = { 1065 971 .enc = { ··· 1074 980 } 1075 981 }, { 1076 982 .alg = "cbc(des)", 1077 - .test = alg_test_cipher, 983 + .test = alg_test_skcipher, 1078 984 .suite = { 1079 985 .cipher = { 1080 986 .enc = { ··· 1089 995 } 1090 996 }, { 1091 997 .alg = "cbc(des3_ede)", 1092 - .test = alg_test_cipher, 998 + .test = alg_test_skcipher, 1093 999 .suite = { 1094 1000 .cipher = { 1095 1001 .enc = { ··· 1104 1010 } 1105 1011 }, { 1106 1012 .alg = "cbc(twofish)", 1107 - .test = alg_test_cipher, 1013 + .test = alg_test_skcipher, 1108 1014 .suite = { 1109 1015 .cipher = { 1110 1016 .enc = { ··· 1143 1049 } 1144 1050 }, { 1145 1051 .alg = "cts(cbc(aes))", 1146 - .test = alg_test_cipher, 1052 + .test = alg_test_skcipher, 1147 1053 .suite = { 1148 1054 .cipher = { 1149 1055 .enc = { ··· 1173 1079 } 1174 1080 }, { 1175 1081 .alg = "ecb(aes)", 1176 - .test = alg_test_cipher, 1082 + .test = alg_test_skcipher, 1177 1083 .suite = { 1178 1084 .cipher = { 1179 1085 .enc = { ··· 1188 1094 } 1189 1095 }, { 1190 1096 .alg = "ecb(anubis)", 1191 - .test = alg_test_cipher, 1097 + .test = alg_test_skcipher, 1192 1098 .suite = { 1193 1099 .cipher = { 1194 1100 .enc = { ··· 1203 1109 } 1204 1110 }, { 1205 1111 .alg = "ecb(arc4)", 1206 - .test = alg_test_cipher, 1112 + .test = alg_test_skcipher, 1207 1113 .suite = { 1208 1114 .cipher = { 1209 1115 .enc = { ··· 1218 1124 } 1219 1125 }, { 1220 1126 .alg = "ecb(blowfish)", 1221 - .test = alg_test_cipher, 1127 + .test = alg_test_skcipher, 1222 1128 .suite = { 1223 1129 .cipher = { 1224 1130 .enc = { ··· 1233 1139 } 1234 1140 }, { 1235 1141 .alg = "ecb(camellia)", 1236 - .test = alg_test_cipher, 1142 + .test = alg_test_skcipher, 1237 1143 .suite = { 1238 1144 .cipher = { 1239 1145 .enc = { ··· 1248 1154 } 1249 1155 }, { 1250 1156 .alg = "ecb(cast5)", 1251 - .test = alg_test_cipher, 1157 + .test = alg_test_skcipher, 1252 1158 .suite = { 1253 1159 .cipher = { 1254 1160 .enc = { ··· 1263 1169 } 1264 1170 }, { 1265 1171 .alg = "ecb(cast6)", 1266 - .test = alg_test_cipher, 1172 + .test = alg_test_skcipher, 1267 1173 .suite = { 1268 1174 .cipher = { 1269 1175 .enc = { ··· 1278 1184 } 1279 1185 }, { 1280 1186 .alg = "ecb(des)", 1281 - .test = alg_test_cipher, 1187 + .test = alg_test_skcipher, 1282 1188 .suite = { 1283 1189 .cipher = { 1284 1190 .enc = { ··· 1293 1199 } 1294 1200 }, { 1295 1201 .alg = "ecb(des3_ede)", 1296 - .test = alg_test_cipher, 1202 + .test = alg_test_skcipher, 1297 1203 .suite = { 1298 1204 .cipher = { 1299 1205 .enc = { ··· 1308 1214 } 1309 1215 }, { 1310 1216 .alg = "ecb(khazad)", 1311 - .test = alg_test_cipher, 1217 + .test = alg_test_skcipher, 1312 1218 .suite = { 1313 1219 .cipher = { 1314 1220 .enc = { ··· 1323 1229 } 1324 1230 }, { 1325 1231 .alg = "ecb(seed)", 1326 - .test = alg_test_cipher, 1232 + .test = alg_test_skcipher, 1327 1233 .suite = { 1328 1234 .cipher = { 1329 1235 .enc = { ··· 1338 1244 } 1339 1245 }, { 1340 1246 .alg = "ecb(serpent)", 1341 - .test = alg_test_cipher, 1247 + .test = alg_test_skcipher, 1342 1248 .suite = { 1343 1249 .cipher = { 1344 1250 .enc = { ··· 1353 1259 } 1354 1260 }, { 1355 1261 .alg = "ecb(tea)", 1356 - .test = alg_test_cipher, 1262 + .test = alg_test_skcipher, 1357 1263 .suite = { 1358 1264 .cipher = { 1359 1265 .enc = { ··· 1368 1274 } 1369 1275 }, { 1370 1276 .alg = "ecb(tnepres)", 1371 - .test = alg_test_cipher, 1277 + .test = alg_test_skcipher, 1372 1278 .suite = { 1373 1279 .cipher = { 1374 1280 .enc = { ··· 1383 1289 } 1384 1290 }, { 1385 1291 .alg = "ecb(twofish)", 1386 - .test = alg_test_cipher, 1292 + .test = alg_test_skcipher, 1387 1293 .suite = { 1388 1294 .cipher = { 1389 1295 .enc = { ··· 1398 1304 } 1399 1305 }, { 1400 1306 .alg = "ecb(xeta)", 1401 - .test = alg_test_cipher, 1307 + .test = alg_test_skcipher, 1402 1308 .suite = { 1403 1309 .cipher = { 1404 1310 .enc = { ··· 1413 1319 } 1414 1320 }, { 1415 1321 .alg = "ecb(xtea)", 1416 - .test = alg_test_cipher, 1322 + .test = alg_test_skcipher, 1417 1323 .suite = { 1418 1324 .cipher = { 1419 1325 .enc = { ··· 1515 1421 } 1516 1422 }, { 1517 1423 .alg = "lrw(aes)", 1518 - .test = alg_test_cipher, 1424 + .test = alg_test_skcipher, 1519 1425 .suite = { 1520 1426 .cipher = { 1521 1427 .enc = { ··· 1572 1478 } 1573 1479 }, { 1574 1480 .alg = "pcbc(fcrypt)", 1575 - .test = alg_test_cipher, 1481 + .test = alg_test_skcipher, 1576 1482 .suite = { 1577 1483 .cipher = { 1578 1484 .enc = { ··· 1587 1493 } 1588 1494 }, { 1589 1495 .alg = "rfc3686(ctr(aes))", 1590 - .test = alg_test_cipher, 1496 + .test = alg_test_skcipher, 1591 1497 .suite = { 1592 1498 .cipher = { 1593 1499 .enc = { ··· 1638 1544 } 1639 1545 }, { 1640 1546 .alg = "salsa20", 1641 - .test = alg_test_cipher, 1547 + .test = alg_test_skcipher, 1642 1548 .suite = { 1643 1549 .cipher = { 1644 1550 .enc = { ··· 1757 1663 } 1758 1664 }, { 1759 1665 .alg = "xts(aes)", 1760 - .test = alg_test_cipher, 1666 + .test = alg_test_skcipher, 1761 1667 .suite = { 1762 1668 .cipher = { 1763 1669 .enc = { ··· 1773 1679 } 1774 1680 }; 1775 1681 1776 - int alg_test(const char *driver, const char *alg, u32 type, u32 mask) 1682 + static int alg_find_test(const char *alg) 1777 1683 { 1778 1684 int start = 0; 1779 1685 int end = ARRAY_SIZE(alg_test_descs); ··· 1792 1698 continue; 1793 1699 } 1794 1700 1795 - return alg_test_descs[i].test(alg_test_descs + i, driver, 1796 - type, mask); 1701 + return i; 1797 1702 } 1798 1703 1704 + return -1; 1705 + } 1706 + 1707 + int alg_test(const char *driver, const char *alg, u32 type, u32 mask) 1708 + { 1709 + int i; 1710 + 1711 + if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) { 1712 + char nalg[CRYPTO_MAX_ALG_NAME]; 1713 + 1714 + if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >= 1715 + sizeof(nalg)) 1716 + return -ENAMETOOLONG; 1717 + 1718 + i = alg_find_test(nalg); 1719 + if (i < 0) 1720 + goto notest; 1721 + 1722 + return alg_test_cipher(alg_test_descs + i, driver, type, mask); 1723 + } 1724 + 1725 + i = alg_find_test(alg); 1726 + if (i < 0) 1727 + goto notest; 1728 + 1729 + return alg_test_descs[i].test(alg_test_descs + i, driver, 1730 + type, mask); 1731 + 1732 + notest: 1799 1733 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver); 1800 1734 return 0; 1801 1735 }