1 /* 2 * Copyright (c) 2017 JingPiao Chen <chenjingpiao (at) gmail.com> 3 * Copyright (c) 2017 The strace developers. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "tests.h" 30 31 #ifdef HAVE_LINUX_CRYPTOUSER_H 32 33 # include <stdio.h> 34 # include <stdint.h> 35 # include "test_nlattr.h" 36 # include <linux/cryptouser.h> 37 38 # define CRYPTOCFGA_REPORT_LARVAL 2 39 40 static void 41 init_crypto_user_alg(struct nlmsghdr *const nlh, const unsigned int msg_len) 42 { 43 SET_STRUCT(struct nlmsghdr, nlh, 44 .nlmsg_len = msg_len, 45 .nlmsg_type = CRYPTO_MSG_GETALG, 46 .nlmsg_flags = NLM_F_DUMP 47 ); 48 49 struct crypto_user_alg *const alg = NLMSG_DATA(nlh); 50 SET_STRUCT(struct crypto_user_alg, alg, 51 .cru_name = "abcd", 52 .cru_driver_name = "efgh", 53 .cru_module_name = "ijkl", 54 ); 55 } 56 57 static void 58 print_crypto_user_alg(const unsigned int msg_len) 59 { 60 printf("{len=%u, type=CRYPTO_MSG_GETALG" 61 ", flags=NLM_F_DUMP, seq=0, pid=0}" 62 ", {cru_name=\"abcd\", cru_driver_name=\"efgh\"" 63 ", cru_module_name=\"ijkl\", cru_type=0" 64 ", cru_mask=0, cru_refcnt=0, cru_flags=0}", 65 msg_len); 66 } 67 68 int 69 main(void) 70 { 71 skip_if_unavailable("/proc/self/fd/"); 72 73 const int fd = create_nl_socket(NETLINK_CRYPTO); 74 const unsigned int hdrlen = sizeof(struct crypto_user_alg); 75 void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); 76 77 static char pattern[4096]; 78 fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); 79 80 char *const str = tail_alloc(DEFAULT_STRLEN); 81 fill_memory_ex(str, DEFAULT_STRLEN, '0', 10); 82 TEST_NLATTR(fd, nlh0, hdrlen, 83 init_crypto_user_alg, print_crypto_user_alg, 84 CRYPTOCFGA_REPORT_LARVAL, 85 DEFAULT_STRLEN, str, DEFAULT_STRLEN, 86 printf("{type=\"%.*s\"...}", DEFAULT_STRLEN, str)); 87 str[DEFAULT_STRLEN - 1] = '\0'; 88 TEST_NLATTR(fd, nlh0, hdrlen, 89 init_crypto_user_alg, print_crypto_user_alg, 90 CRYPTOCFGA_REPORT_LARVAL, 91 DEFAULT_STRLEN, str, DEFAULT_STRLEN, 92 printf("{type=\"%s\"}", str)); 93 94 #ifdef HAVE_STRUCT_CRYPTO_REPORT_HASH 95 static const struct crypto_report_hash rhash = { 96 .type = "efgh", 97 .blocksize = 0xabcdefdc, 98 .digestsize = 0xfebcdacd 99 }; 100 TEST_NLATTR_OBJECT_EX(fd, nlh0, hdrlen, 101 init_crypto_user_alg, print_crypto_user_alg, 102 CRYPTOCFGA_REPORT_HASH, 103 pattern, rhash, print_quoted_memory, 104 printf("{type=\"efgh\""); 105 PRINT_FIELD_U(", ", rhash, blocksize); 106 PRINT_FIELD_U(", ", rhash, digestsize); 107 printf("}")); 108 #endif 109 110 #ifdef HAVE_STRUCT_CRYPTO_REPORT_BLKCIPHER 111 static const struct crypto_report_blkcipher rblkcipher = { 112 .type = "abcd", 113 .geniv = "efgh", 114 .blocksize = 0xabcdefac, 115 .min_keysize = 0xfeadbcda, 116 .max_keysize = 0xbdacdeac, 117 .ivsize = 0xefacbdac 118 }; 119 TEST_NLATTR_OBJECT_EX(fd, nlh0, hdrlen, 120 init_crypto_user_alg, print_crypto_user_alg, 121 CRYPTOCFGA_REPORT_BLKCIPHER, 122 pattern, rblkcipher, print_quoted_memory, 123 printf("{type=\"abcd\", geniv=\"efgh\""); 124 PRINT_FIELD_U(", ", rblkcipher, blocksize); 125 PRINT_FIELD_U(", ", rblkcipher, min_keysize); 126 PRINT_FIELD_U(", ", rblkcipher, max_keysize); 127 PRINT_FIELD_U(", ", rblkcipher, ivsize); 128 printf("}")); 129 #endif 130 131 #ifdef HAVE_STRUCT_CRYPTO_REPORT_AEAD 132 static const struct crypto_report_aead raead = { 133 .type = "abcd", 134 .geniv = "efgh", 135 .blocksize = 0xbaefdbac, 136 .maxauthsize = 0xfdbdbcda, 137 .ivsize = 0xacbefdac 138 }; 139 TEST_NLATTR_OBJECT_EX(fd, nlh0, hdrlen, 140 init_crypto_user_alg, print_crypto_user_alg, 141 CRYPTOCFGA_REPORT_AEAD, 142 pattern, raead, print_quoted_memory, 143 printf("{type=\"abcd\", geniv=\"efgh\""); 144 PRINT_FIELD_U(", ", raead, blocksize); 145 PRINT_FIELD_U(", ", raead, maxauthsize); 146 PRINT_FIELD_U(", ", raead, ivsize); 147 printf("}")); 148 #endif 149 150 #ifdef HAVE_STRUCT_CRYPTO_REPORT_RNG 151 static const struct crypto_report_rng rrng = { 152 .type = "abcd", 153 .seedsize = 0xabcdefac 154 }; 155 TEST_NLATTR_OBJECT_EX(fd, nlh0, hdrlen, 156 init_crypto_user_alg, print_crypto_user_alg, 157 CRYPTOCFGA_REPORT_RNG, 158 pattern, rrng, print_quoted_memory, 159 printf("{type=\"abcd\""); 160 PRINT_FIELD_U(", ", rrng, seedsize); 161 printf("}")); 162 #endif 163 164 #ifdef HAVE_STRUCT_CRYPTO_REPORT_CIPHER 165 static const struct crypto_report_cipher rcipher = { 166 .type = "abcd", 167 .blocksize = 0xabcdefac, 168 .min_keysize = 0xfeadbcda, 169 .max_keysize = 0xbdacdeac, 170 }; 171 TEST_NLATTR_OBJECT_EX(fd, nlh0, hdrlen, 172 init_crypto_user_alg, print_crypto_user_alg, 173 CRYPTOCFGA_REPORT_CIPHER, 174 pattern, rcipher, print_quoted_memory, 175 printf("{type=\"abcd\""); 176 PRINT_FIELD_U(", ", rcipher, blocksize); 177 PRINT_FIELD_U(", ", rcipher, min_keysize); 178 PRINT_FIELD_U(", ", rcipher, max_keysize); 179 printf("}")); 180 #endif 181 182 puts("+++ exited with 0 +++"); 183 return 0; 184 } 185 186 #else 187 188 SKIP_MAIN_UNDEFINED("HAVE_LINUX_CRYPTOUSER_H"); 189 190 #endif 191