spiped 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +7 -0
  2. data/ext/spiped/extconf.rb +3 -0
  3. data/ext/spiped/spiped-source/BUILDING +46 -0
  4. data/ext/spiped/spiped-source/CHANGELOG +44 -0
  5. data/ext/spiped/spiped-source/COPYRIGHT +33 -0
  6. data/ext/spiped/spiped-source/Makefile +47 -0
  7. data/ext/spiped/spiped-source/Makefile.POSIX +27 -0
  8. data/ext/spiped/spiped-source/Makefile.inc +20 -0
  9. data/ext/spiped/spiped-source/Makefile.prog +23 -0
  10. data/ext/spiped/spiped-source/POSIX/README +10 -0
  11. data/ext/spiped/spiped-source/POSIX/posix-cflags.sh +10 -0
  12. data/ext/spiped/spiped-source/POSIX/posix-clock_realtime.c +3 -0
  13. data/ext/spiped/spiped-source/POSIX/posix-l.c +1 -0
  14. data/ext/spiped/spiped-source/POSIX/posix-l.sh +14 -0
  15. data/ext/spiped/spiped-source/POSIX/posix-msg_nosignal.c +3 -0
  16. data/ext/spiped/spiped-source/README +198 -0
  17. data/ext/spiped/spiped-source/STYLE +151 -0
  18. data/ext/spiped/spiped-source/lib/dnsthread/dnsthread.c +464 -0
  19. data/ext/spiped/spiped-source/lib/dnsthread/dnsthread.h +45 -0
  20. data/ext/spiped/spiped-source/libcperciva/alg/sha256.c +442 -0
  21. data/ext/spiped/spiped-source/libcperciva/alg/sha256.h +95 -0
  22. data/ext/spiped/spiped-source/libcperciva/cpusupport/Build/cpusupport-X86-AESNI.c +13 -0
  23. data/ext/spiped/spiped-source/libcperciva/cpusupport/Build/cpusupport-X86-CPUID.c +8 -0
  24. data/ext/spiped/spiped-source/libcperciva/cpusupport/Build/cpusupport.sh +37 -0
  25. data/ext/spiped/spiped-source/libcperciva/cpusupport/cpusupport.h +63 -0
  26. data/ext/spiped/spiped-source/libcperciva/cpusupport/cpusupport_x86_aesni.c +30 -0
  27. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_aes.c +166 -0
  28. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_aes.h +31 -0
  29. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_aes_aesni.c +229 -0
  30. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_aes_aesni.h +31 -0
  31. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_aesctr.c +124 -0
  32. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_aesctr.h +41 -0
  33. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_dh.c +293 -0
  34. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_dh.h +43 -0
  35. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_dh_group14.c +46 -0
  36. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_dh_group14.h +9 -0
  37. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_entropy.c +215 -0
  38. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_entropy.h +14 -0
  39. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_verify_bytes.c +21 -0
  40. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_verify_bytes.h +14 -0
  41. data/ext/spiped/spiped-source/libcperciva/datastruct/elasticarray.c +276 -0
  42. data/ext/spiped/spiped-source/libcperciva/datastruct/elasticarray.h +167 -0
  43. data/ext/spiped/spiped-source/libcperciva/datastruct/mpool.h +85 -0
  44. data/ext/spiped/spiped-source/libcperciva/datastruct/ptrheap.c +334 -0
  45. data/ext/spiped/spiped-source/libcperciva/datastruct/ptrheap.h +89 -0
  46. data/ext/spiped/spiped-source/libcperciva/datastruct/timerqueue.c +241 -0
  47. data/ext/spiped/spiped-source/libcperciva/datastruct/timerqueue.h +60 -0
  48. data/ext/spiped/spiped-source/libcperciva/events/events.c +203 -0
  49. data/ext/spiped/spiped-source/libcperciva/events/events.h +106 -0
  50. data/ext/spiped/spiped-source/libcperciva/events/events_immediate.c +149 -0
  51. data/ext/spiped/spiped-source/libcperciva/events/events_internal.h +95 -0
  52. data/ext/spiped/spiped-source/libcperciva/events/events_network.c +347 -0
  53. data/ext/spiped/spiped-source/libcperciva/events/events_network_selectstats.c +106 -0
  54. data/ext/spiped/spiped-source/libcperciva/events/events_timer.c +273 -0
  55. data/ext/spiped/spiped-source/libcperciva/network/network.h +95 -0
  56. data/ext/spiped/spiped-source/libcperciva/network/network_accept.c +103 -0
  57. data/ext/spiped/spiped-source/libcperciva/network/network_connect.c +258 -0
  58. data/ext/spiped/spiped-source/libcperciva/network/network_read.c +155 -0
  59. data/ext/spiped/spiped-source/libcperciva/network/network_write.c +188 -0
  60. data/ext/spiped/spiped-source/libcperciva/util/asprintf.c +49 -0
  61. data/ext/spiped/spiped-source/libcperciva/util/asprintf.h +16 -0
  62. data/ext/spiped/spiped-source/libcperciva/util/daemonize.c +134 -0
  63. data/ext/spiped/spiped-source/libcperciva/util/daemonize.h +10 -0
  64. data/ext/spiped/spiped-source/libcperciva/util/entropy.c +76 -0
  65. data/ext/spiped/spiped-source/libcperciva/util/entropy.h +13 -0
  66. data/ext/spiped/spiped-source/libcperciva/util/imalloc.h +33 -0
  67. data/ext/spiped/spiped-source/libcperciva/util/insecure_memzero.c +19 -0
  68. data/ext/spiped/spiped-source/libcperciva/util/insecure_memzero.h +33 -0
  69. data/ext/spiped/spiped-source/libcperciva/util/monoclock.c +52 -0
  70. data/ext/spiped/spiped-source/libcperciva/util/monoclock.h +14 -0
  71. data/ext/spiped/spiped-source/libcperciva/util/noeintr.c +54 -0
  72. data/ext/spiped/spiped-source/libcperciva/util/noeintr.h +14 -0
  73. data/ext/spiped/spiped-source/libcperciva/util/sock.c +472 -0
  74. data/ext/spiped/spiped-source/libcperciva/util/sock.h +56 -0
  75. data/ext/spiped/spiped-source/libcperciva/util/sock_internal.h +14 -0
  76. data/ext/spiped/spiped-source/libcperciva/util/sock_util.c +271 -0
  77. data/ext/spiped/spiped-source/libcperciva/util/sock_util.h +51 -0
  78. data/ext/spiped/spiped-source/libcperciva/util/sysendian.h +146 -0
  79. data/ext/spiped/spiped-source/libcperciva/util/warnp.c +76 -0
  80. data/ext/spiped/spiped-source/libcperciva/util/warnp.h +59 -0
  81. data/ext/spiped/spiped-source/proto/proto_conn.c +362 -0
  82. data/ext/spiped/spiped-source/proto/proto_conn.h +25 -0
  83. data/ext/spiped/spiped-source/proto/proto_crypt.c +396 -0
  84. data/ext/spiped/spiped-source/proto/proto_crypt.h +102 -0
  85. data/ext/spiped/spiped-source/proto/proto_handshake.c +330 -0
  86. data/ext/spiped/spiped-source/proto/proto_handshake.h +30 -0
  87. data/ext/spiped/spiped-source/proto/proto_pipe.c +202 -0
  88. data/ext/spiped/spiped-source/proto/proto_pipe.h +23 -0
  89. data/ext/spiped/spiped-source/spipe/Makefile +90 -0
  90. data/ext/spiped/spiped-source/spipe/README +24 -0
  91. data/ext/spiped/spiped-source/spipe/main.c +178 -0
  92. data/ext/spiped/spiped-source/spipe/pushbits.c +101 -0
  93. data/ext/spiped/spiped-source/spipe/pushbits.h +10 -0
  94. data/ext/spiped/spiped-source/spipe/spipe.1 +60 -0
  95. data/ext/spiped/spiped-source/spiped/Makefile +98 -0
  96. data/ext/spiped/spiped-source/spiped/README +62 -0
  97. data/ext/spiped/spiped-source/spiped/dispatch.c +214 -0
  98. data/ext/spiped/spiped-source/spiped/dispatch.h +27 -0
  99. data/ext/spiped/spiped-source/spiped/main.c +267 -0
  100. data/ext/spiped/spiped-source/spiped/spiped.1 +112 -0
  101. data/lib/spiped.rb +3 -0
  102. metadata +143 -0
@@ -0,0 +1,95 @@
1
+ #ifndef _SHA256_H_
2
+ #define _SHA256_H_
3
+
4
+ #include <stddef.h>
5
+ #include <stdint.h>
6
+
7
+ /*
8
+ * Use #defines in order to avoid namespace collisions with anyone else's
9
+ * SHA256 code (e.g., the code in OpenSSL).
10
+ */
11
+ #define SHA256_Init libcperciva_SHA256_Init
12
+ #define SHA256_Update libcperciva_SHA256_Update
13
+ #define SHA256_Final libcperciva_SHA256_Final
14
+ #define SHA256_Buf libcperciva_SHA256_Buf
15
+ #define SHA256_CTX libcperciva_SHA256_CTX
16
+ #define HMAC_SHA256_Init libcperciva_HMAC_SHA256_Init
17
+ #define HMAC_SHA256_Update libcperciva_HMAC_SHA256_Update
18
+ #define HMAC_SHA256_Final libcperciva_HMAC_SHA256_Final
19
+ #define HMAC_SHA256_Buf libcperciva_HMAC_SHA256_Buf
20
+ #define HMAC_SHA256_CTX libcperciva_HMAC_SHA256_CTX
21
+
22
+ /* Context structure for SHA256 operations. */
23
+ typedef struct {
24
+ uint32_t state[8];
25
+ uint64_t count;
26
+ uint8_t buf[64];
27
+ } SHA256_CTX;
28
+
29
+ /**
30
+ * SHA256_Init(ctx):
31
+ * Initialize the SHA256 context ${ctx}.
32
+ */
33
+ void SHA256_Init(SHA256_CTX *);
34
+
35
+ /**
36
+ * SHA256_Update(ctx, in, len):
37
+ * Input ${len} bytes from ${in} into the SHA256 context ${ctx}.
38
+ */
39
+ void SHA256_Update(SHA256_CTX *, const void *, size_t);
40
+
41
+ /**
42
+ * SHA256_Final(digest, ctx):
43
+ * Output the SHA256 hash of the data input to the context ${ctx} into the
44
+ * buffer ${digest}.
45
+ */
46
+ void SHA256_Final(uint8_t[32], SHA256_CTX *);
47
+
48
+ /**
49
+ * SHA256_Buf(in, len, digest):
50
+ * Compute the SHA256 hash of ${len} bytes from $in} and write it to ${digest}.
51
+ */
52
+ void SHA256_Buf(const void *, size_t, uint8_t[32]);
53
+
54
+ /* Context structure for HMAC-SHA256 operations. */
55
+ typedef struct {
56
+ SHA256_CTX ictx;
57
+ SHA256_CTX octx;
58
+ } HMAC_SHA256_CTX;
59
+
60
+ /**
61
+ * HMAC_SHA256_Init(ctx, K, Klen):
62
+ * Initialize the HMAC-SHA256 context ${ctx} with ${Klen} bytes of key from
63
+ * ${K}.
64
+ */
65
+ void HMAC_SHA256_Init(HMAC_SHA256_CTX *, const void *, size_t);
66
+
67
+ /**
68
+ * HMAC_SHA256_Update(ctx, in, len):
69
+ * Input ${len} bytes from ${in} into the HMAC-SHA256 context ${ctx}.
70
+ */
71
+ void HMAC_SHA256_Update(HMAC_SHA256_CTX *, const void *, size_t);
72
+
73
+ /**
74
+ * HMAC_SHA256_Final(digest, ctx):
75
+ * Output the HMAC-SHA256 of the data input to the context ${ctx} into the
76
+ * buffer ${digest}.
77
+ */
78
+ void HMAC_SHA256_Final(uint8_t[32], HMAC_SHA256_CTX *);
79
+
80
+ /**
81
+ * HMAC_SHA256_Buf(K, Klen, in, len, digest):
82
+ * Compute the HMAC-SHA256 of ${len} bytes from ${in} using the key ${K} of
83
+ * length ${Klen}, and write the result to ${digest}.
84
+ */
85
+ void HMAC_SHA256_Buf(const void *, size_t, const void *, size_t, uint8_t[32]);
86
+
87
+ /**
88
+ * PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
89
+ * Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
90
+ * write the output to buf. The value dkLen must be at most 32 * (2^32 - 1).
91
+ */
92
+ void PBKDF2_SHA256(const uint8_t *, size_t, const uint8_t *, size_t,
93
+ uint64_t, uint8_t *, size_t);
94
+
95
+ #endif /* !_SHA256_H_ */
@@ -0,0 +1,13 @@
1
+ #include <wmmintrin.h>
2
+
3
+ static char a[16];
4
+
5
+ int main(void)
6
+ {
7
+ __m128i x, y;
8
+
9
+ x = _mm_loadu_si128((__m128i *)a);
10
+ y = _mm_aesenc_si128(x, x);
11
+ _mm_storeu_si128((__m128i *)a, y);
12
+ return (a[0]);
13
+ }
@@ -0,0 +1,8 @@
1
+ #include <cpuid.h>
2
+
3
+ int main(void)
4
+ {
5
+ unsigned int a, b, c, d;
6
+
7
+ return __get_cpuid(0, &a, &b, &c, &d);
8
+ }
@@ -0,0 +1,37 @@
1
+ # Should be sourced by `command -p sh mk-h.sh` from within a Makefile
2
+ # Standard output should be written to cpusupport-config.h, which is both a
3
+ # C header file defining CPUSUPPORT_ARCH_FEATURE macros and sourceable sh
4
+ # code which sets CFLAGS_ARCH_FEATURE environment variables.
5
+ feature() {
6
+ ARCH=$1
7
+ FEATURE=$2
8
+ shift 2;
9
+ printf "Checking if compiler supports $ARCH $FEATURE feature..." 1>&2
10
+ for CFLAG in "$@"; do
11
+ if ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L ${CFLAG} \
12
+ cpusupport-$ARCH-$FEATURE.c 2>/dev/null; then
13
+ rm -f a.out
14
+ break;
15
+ fi
16
+ CFLAG=NOTSUPPORTED;
17
+ done
18
+ case $CFLAG in
19
+ NOTSUPPORTED)
20
+ echo " no" 1>&2
21
+ ;;
22
+ "")
23
+ echo " yes" 1>&2
24
+ echo "#define CPUSUPPORT_${ARCH}_${FEATURE}"
25
+ ;;
26
+ *)
27
+ echo " yes, via $CFLAG" 1>&2
28
+ echo "#define CPUSUPPORT_${ARCH}_${FEATURE}"
29
+ echo "#ifdef cpusupport_dummy"
30
+ echo "export CFLAGS_${ARCH}_${FEATURE}=\"${CFLAG}\""
31
+ echo "#endif"
32
+ ;;
33
+ esac
34
+ }
35
+
36
+ feature X86 CPUID ""
37
+ feature X86 AESNI "" "-maes" "-maes -Wno-cast-align" "-maes -Wno-missing-prototypes -Wno-cast-qual"
@@ -0,0 +1,63 @@
1
+ #ifndef _CPUSUPPORT_H_
2
+ #define _CPUSUPPORT_H_
3
+
4
+ /*
5
+ * To enable support for non-portable CPU features at compile time, one or
6
+ * more CPUSUPPORT_ARCH_FEATURE macros should be defined. This can be done
7
+ * directly on the compiler command line; or a file can be created with the
8
+ * necessary #define lines and then -D CPUSUPPORT_CONFIG_FILE=cpuconfig.h
9
+ * (or similar) can be provided to include that file here.
10
+ */
11
+ #ifdef CPUSUPPORT_CONFIG_FILE
12
+ #include CPUSUPPORT_CONFIG_FILE
13
+ #endif
14
+
15
+ /*
16
+ * The CPUSUPPORT_FEATURE macro declares the necessary variables and
17
+ * functions for detecting CPU feature support at run time. The function
18
+ * defined in the macro acts to cache the result of the ..._detect function
19
+ * using the ..._present and ..._init variables.
20
+ */
21
+ #define CPUSUPPORT_FEATURE(arch, feature) \
22
+ extern int cpusupport_ ## arch ## _ ## feature ## _present; \
23
+ extern int cpusupport_ ## arch ## _ ## feature ## _init; \
24
+ int cpusupport_ ## arch ## _ ## feature ## _detect(void); \
25
+ \
26
+ static inline int \
27
+ cpusupport_ ## arch ## _ ## feature(void) \
28
+ { \
29
+ \
30
+ if (cpusupport_ ## arch ## _ ## feature ## _present) \
31
+ return (1); \
32
+ else if (cpusupport_ ## arch ## _ ## feature ## _init) \
33
+ return (0); \
34
+ cpusupport_ ## arch ## _ ## feature ## _present = \
35
+ cpusupport_ ## arch ##_ ## feature ## _detect(); \
36
+ cpusupport_ ## arch ## _ ## feature ## _init = 1; \
37
+ return (cpusupport_ ## arch ## _ ## feature ## _present); \
38
+ } \
39
+ struct cpusupport_ ## arch ## _ ## feature ## _dummy
40
+
41
+ /*
42
+ * CPUSUPPORT_FEATURE_DECL(arch, feature):
43
+ * Macro which defines variables and provides a function declaration for
44
+ * detecting the presence of "feature" on the "arch" architecture. The
45
+ * function body following this macro expansion must return nonzero if the
46
+ * feature is present, or zero if the feature is not present or the detection
47
+ * fails for any reason.
48
+ */
49
+ #define CPUSUPPORT_FEATURE_DECL(arch, feature) \
50
+ int cpusupport_ ## arch ## _ ## feature ## _present = 0; \
51
+ int cpusupport_ ## arch ## _ ## feature ## _init = 0; \
52
+ int \
53
+ cpusupport_ ## arch ## _ ## feature ## _detect(void)
54
+
55
+ /*
56
+ * Any features listed here must have associated C files compiled and linked
57
+ * in, since the macro references symbols which must be defined. Projects
58
+ * which do not need to detect certain CPU features may wish to remove lines
59
+ * from this list so that the associated C files can be omitted.
60
+ */
61
+ CPUSUPPORT_FEATURE(x86, aesni);
62
+
63
+ #endif /* !_CPUSUPPORT_H_ */
@@ -0,0 +1,30 @@
1
+ #include "cpusupport.h"
2
+
3
+ #ifdef CPUSUPPORT_X86_CPUID
4
+ #include <cpuid.h>
5
+ #endif
6
+
7
+ #define CPUID_AESNI_BIT (1 << 25)
8
+
9
+ CPUSUPPORT_FEATURE_DECL(x86, aesni)
10
+ {
11
+ #ifdef CPUSUPPORT_X86_CPUID
12
+ unsigned int eax, ebx, ecx, edx;
13
+
14
+ /* Check if CPUID supports the level we need. */
15
+ if (!__get_cpuid(0, &eax, &ebx, &ecx, &edx))
16
+ goto unsupported;
17
+ if (eax < 1)
18
+ goto unsupported;
19
+
20
+ /* Ask about CPU features. */
21
+ if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx))
22
+ goto unsupported;
23
+
24
+ /* Return the relevant feature bit. */
25
+ return (ecx & CPUID_AESNI_BIT);
26
+
27
+ unsupported:
28
+ #endif
29
+ return (0);
30
+ }
@@ -0,0 +1,166 @@
1
+ #include <assert.h>
2
+ #include <stdint.h>
3
+ #include <stdlib.h>
4
+ #include <string.h>
5
+
6
+ #include <openssl/aes.h>
7
+
8
+ #include "cpusupport.h"
9
+ #include "crypto_aes_aesni.h"
10
+ #include "insecure_memzero.h"
11
+ #include "warnp.h"
12
+
13
+ #include "crypto_aes.h"
14
+
15
+ /**
16
+ * This represents either an AES_KEY or a struct crypto_aes_key_aesni; we
17
+ * know which it is based on whether we're using AESNI code or not. As such,
18
+ * it's just an opaque pointer; but declaring it as a named structure type
19
+ * prevents type-mismatch bugs in upstream code.
20
+ */
21
+ struct crypto_aes_key;
22
+
23
+ #ifdef CPUSUPPORT_X86_AESNI
24
+ /* Test whether OpenSSL and AESNI code produce the same AES ciphertext. */
25
+ static int
26
+ aesnitest(uint8_t ptext[16], uint8_t * key, size_t keylen)
27
+ {
28
+ AES_KEY kexp_openssl;
29
+ void * kexp_aesni;
30
+ uint8_t ctext_openssl[16];
31
+ uint8_t ctext_aesni[16];
32
+
33
+ /* Expand the key. */
34
+ AES_set_encrypt_key(key, keylen * 8, &kexp_openssl);
35
+ if ((kexp_aesni = crypto_aes_key_expand_aesni(key, keylen)) == NULL)
36
+ goto err0;
37
+
38
+ /* Encrypt the block. */
39
+ AES_encrypt(ptext, ctext_openssl, &kexp_openssl);
40
+ crypto_aes_encrypt_block_aesni(ptext, ctext_aesni, kexp_aesni);
41
+
42
+ /* Free the AESNI expanded key. */
43
+ crypto_aes_key_free_aesni(kexp_aesni);
44
+
45
+ /* Do the outputs match? */
46
+ return (memcmp(ctext_openssl, ctext_aesni, 16));
47
+
48
+ err0:
49
+ /* Failure! */
50
+ return (-1);
51
+ }
52
+
53
+ /* Should we use AESNI? */
54
+ static int
55
+ useaesni(void)
56
+ {
57
+ static int aesnigood = -1;
58
+ uint8_t key[32];
59
+ uint8_t ptext[16];
60
+ size_t i;
61
+
62
+ /* If we haven't decided which code to use yet, decide now. */
63
+ while (aesnigood == -1) {
64
+ /* Default to OpenSSL. */
65
+ aesnigood = 0;
66
+
67
+ /* If the CPU doesn't claim to support AESNI, stop here. */
68
+ if (!cpusupport_x86_aesni())
69
+ break;
70
+
71
+ /* Test cases: key is 0x00010203..., ptext is 0x00112233... */
72
+ for (i = 0; i < 16; i++)
73
+ ptext[i] = 0x11 * i;
74
+ for (i = 0; i < 32; i++)
75
+ key[i] = i;
76
+
77
+ /* Test that AESNI and OpenSSL produce the same results. */
78
+ if (aesnitest(ptext, key, 16) || aesnitest(ptext, key, 32)) {
79
+ warn0("Disabling AESNI due to failed self-test");
80
+ break;
81
+ }
82
+
83
+ /* AESNI works; use it. */
84
+ aesnigood = 1;
85
+ }
86
+
87
+ return (aesnigood);
88
+ }
89
+ #endif /* CPUSUPPORT_X86_AESNI */
90
+
91
+ /**
92
+ * crypto_aes_key_expand(key, len):
93
+ * Expand the ${len}-byte AES key ${key} into a structure which can be passed
94
+ * to crypto_aes_encrypt_block. The length must be 16 or 32.
95
+ */
96
+ struct crypto_aes_key *
97
+ crypto_aes_key_expand(const uint8_t * key, size_t len)
98
+ {
99
+ AES_KEY * kexp;
100
+
101
+ /* Sanity-check. */
102
+ assert((len == 16) || (len == 32));
103
+
104
+ #ifdef CPUSUPPORT_X86_AESNI
105
+ /* Use AESNI if we can. */
106
+ if (useaesni())
107
+ return (crypto_aes_key_expand_aesni(key, len));
108
+ #endif
109
+
110
+ /* Allocate structure. */
111
+ if ((kexp = malloc(sizeof(AES_KEY))) == NULL)
112
+ goto err0;
113
+
114
+ /* Expand the key. */
115
+ AES_set_encrypt_key(key, len * 8, kexp);
116
+
117
+ /* Success! */
118
+ return ((void *)kexp);
119
+
120
+ err0:
121
+ /* Failure! */
122
+ return (NULL);
123
+ }
124
+
125
+ /**
126
+ * crypto_aes_encrypt_block(in, out, key):
127
+ * Using the expanded AES key ${key}, encrypt the block ${in} and write the
128
+ * resulting ciphertext to ${out}.
129
+ */
130
+ void
131
+ crypto_aes_encrypt_block(const uint8_t * in, uint8_t * out,
132
+ const struct crypto_aes_key * key)
133
+ {
134
+
135
+ #ifdef CPUSUPPORT_X86_AESNI
136
+ if (useaesni()) {
137
+ crypto_aes_encrypt_block_aesni(in, out, (const void *)key);
138
+ return;
139
+ }
140
+ #endif
141
+
142
+ /* Get AES to do the work. */
143
+ AES_encrypt(in, out, (const void *)key);
144
+ }
145
+
146
+ /**
147
+ * crypto_aes_key_free(key):
148
+ * Free the expanded AES key ${key}.
149
+ */
150
+ void
151
+ crypto_aes_key_free(struct crypto_aes_key * key)
152
+ {
153
+
154
+ #ifdef CPUSUPPORT_X86_AESNI
155
+ if (useaesni()) {
156
+ crypto_aes_key_free_aesni((void *)key);
157
+ return;
158
+ }
159
+ #endif
160
+
161
+ /* Attempt to zero the expanded key. */
162
+ insecure_memzero(key, sizeof(AES_KEY));
163
+
164
+ /* Free the key. */
165
+ free(key);
166
+ }
@@ -0,0 +1,31 @@
1
+ #ifndef _CRYPTO_AES_H_
2
+ #define _CRYPTO_AES_H_
3
+
4
+ #include <stddef.h>
5
+ #include <stdint.h>
6
+
7
+ /* Opaque structure. */
8
+ struct crypto_aes_key;
9
+
10
+ /**
11
+ * crypto_aes_key_expand(key, len):
12
+ * Expand the ${len}-byte AES key ${key} into a structure which can be passed
13
+ * to crypto_aes_encrypt_block. The length must be 16 or 32.
14
+ */
15
+ struct crypto_aes_key * crypto_aes_key_expand(const uint8_t *, size_t);
16
+
17
+ /**
18
+ * crypto_aes_encrypt_block(in, out, key):
19
+ * Using the expanded AES key ${key}, encrypt the block ${in} and write the
20
+ * resulting ciphertext to ${out}.
21
+ */
22
+ void crypto_aes_encrypt_block(const uint8_t *, uint8_t *,
23
+ const struct crypto_aes_key *);
24
+
25
+ /**
26
+ * crypto_aes_key_free(key):
27
+ * Free the expanded AES key ${key}.
28
+ */
29
+ void crypto_aes_key_free(struct crypto_aes_key *);
30
+
31
+ #endif /* !_CRYPTO_AES_H_ */