Skip to content
This repository was archived by the owner on Apr 12, 2025. It is now read-only.

Commit 7cc5a46

Browse files
committed
(feat): more
- Adjusted build_script - Added headers to CCrypto libc
1 parent 975f6ab commit 7cc5a46

19 files changed

+1418
-213
lines changed

Package.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,32 @@ let package = Package(
3030
.library(
3131
name: "CryptoSwiftWrapper",
3232
targets: ["CryptoSwiftWrapper"]),
33+
.library(
34+
name: "CCrypto",
35+
targets: ["CCrypto"])
3336
],
3437
dependencies: [
3538
.package(url: "https://github.com/apple/swift-crypto.git", .upToNextMajor(from: "3.5.2")),
3639
],
3740
targets: [
3841
.target(
3942
name: "CryptoSwiftWrapper",
40-
dependencies: ["_cyfn", .product(name: "Crypto", package: "swift-crypto")],
43+
dependencies: ["_cyfn", "CCrypto", .product(name: "Crypto", package: "swift-crypto")],
44+
path: "Sources/CryptoSwiftWrapper",
4145
resources: [
4246
.copy("../../.PrivacyInfo.xcprivacy")
4347
],
44-
publicHeadersPath: "Sources/CryptoSwiftWrapper/include"
48+
publicHeadersPath: "include" // Sources/CryptoSwiftWrapper/
49+
),
50+
.target(
51+
name: "CCrypto",
52+
dependencies: ["_cyfn"],
53+
path: "Sources/CCrypto",
54+
publicHeadersPath: "include", // Sources/CCrypto/
55+
cSettings: [
56+
.headerSearchPath("include")
57+
],
58+
linkerSettings: []
4559
),
4660
.systemLibrary(
4761
name: "_cyfn", path: "Sources/_cyfn"),

Sources/CCrypto/CCrypto-Bridging.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// Header.h
3+
//
4+
//
5+
// Created by Nevio Hirani on 17.07.24.
6+
//
7+
8+
#ifndef CCrypto-Bridging_h
9+
#define CCrypto-Bridging_h
10+
11+
#include "sha3.h"
12+
#include "sha256.h"
13+
#include "sha512.h"
14+
15+
#endif /* CCrypto-Bridging_h */

Sources/CCrypto/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
##===----------------------------------------------------------------------===##
2+
##
3+
## This source file is part of the CryptoSwiftWrapper open source project
4+
##
5+
## This CMakeLists.txt is exclusive for CCrypto only
6+
##
7+
## Copyright (c) 2024 ScribbleLabApp
8+
##
9+
## See LICENSE for license information
10+
##
11+
## SPDX-License-Identifier: Apache-2.0
12+
##
13+
##===----------------------------------------------------------------------===##

Sources/CryptoSwiftWrapper/include/sha.h renamed to Sources/CCrypto/include/aes.h

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===-- CryptoSwiftWrapper/include/sha.h - Bridging ------------ -*- C -*-===//
1+
//===-- CCrypto/include/aes.h - AES -------------------------------*- C -*-===//
22
// //
33
// This source file is part of the Scribble Foundation open source project //
44
// //
@@ -17,13 +17,52 @@
1717
// limitations under the License. //
1818
// //
1919
//===----------------------------------------------------------------------===//
20+
///
21+
/// \file
22+
///
23+
//===----------------------------------------------------------------------===//
24+
25+
#ifndef aes_h
26+
#define aes_h
27+
28+
#include <stdint.h>
29+
#include <stddef.h>
30+
#include <stdlib.h>
31+
32+
#define AES_BLOCK_SIZE 16
33+
34+
#define Nb 4 // Number of columns (32-bit words) in the state
35+
#define Nk_128 4 // Number of columns in key for AES-128
36+
#define Nk_192 6 // Number of columns in key for AES-192
37+
#define Nk_256 8 // Number of columns in key for AES-256
38+
#define Nr_128 10 // Number of rounds for AES-128
39+
#define Nr_192 12 // Number of rounds for AES-192
40+
#define Nr_256 14 // Number of rounds for AES-256
41+
42+
43+
typedef enum {
44+
AES128 = 16,
45+
AES192 = 24,
46+
AES256 = 32
47+
} AES_KEYSIZE;
2048

21-
// This is a bridging header for SHA 256 and 512 implementations
49+
typedef enum {
50+
ECB_MODE,
51+
CBC_MODE,
52+
CFB_MODE,
53+
OFB_MODE,
54+
CTR_MODE
55+
} AES_MODE;
2256

23-
#ifndef sha_h
24-
#define sha_h
57+
typedef struct {
58+
AES_KEYSIZE key_size;
59+
AES_MODE mode;
60+
uint8_t key[32];
61+
uint8_t iv[AES_BLOCK_SIZE];
62+
} AES_CTX;
2563

26-
#include "sha256.h"
27-
#include "sha512.h"
64+
void AES_init(AES_CTX *ctx, AES_KEYSIZE key_size, AES_MODE mode, const uint8_t *key, const uint8_t *iv);
65+
void AES_encrypt(const AES_CTX *ctx, const uint8_t *plaintext, uint8_t *ciphertext, size_t length);
66+
void AES_decrypt(const AES_CTX *ctx, const uint8_t *ciphertext, uint8_t *plaintext, size_t length);
2867

29-
#endif /* sha_h */
68+
#endif /* aes_h */

Sources/CCrypto/include/blake2.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//===-- CCrypto/include/blake2.h - BLAKE2 ALGORITHM ---------------*- C -*-===//
2+
// //
3+
// This source file is part of the Scribble Foundation open source project //
4+
// //
5+
// Copyright (c) 2024 ScribbleLabApp. and the ScribbleLab project authors //
6+
// Licensed under Apache License v2.0 with Runtime Library Exception //
7+
// //
8+
// You may not use this file except in compliance with the License. //
9+
// You may obtain a copy of the License at //
10+
// //
11+
// http://www.apache.org/licenses/LICENSE-2.0 //
12+
// //
13+
// Unless required by applicable law or agreed to in writing, software //
14+
// distributed under the License is distributed on an "AS IS" BASIS, //
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
16+
// See the License for the specific language governing permissions and //
17+
// limitations under the License. //
18+
// //
19+
//===----------------------------------------------------------------------===//
20+
///
21+
/// \file
22+
///
23+
//===----------------------------------------------------------------------===//
24+
25+
#ifndef blake2_h
26+
#define blake2_h
27+
28+
#include <stdint.h>
29+
#include <string.h>
30+
31+
#define BLAKE2S_BLOCKBYTES 64
32+
#define BLAKE2S_OUTBYTES 32
33+
34+
#define BLAKE2S_STATE_SIZE 8
35+
#define BLAKE2S_KEYBYTES 32
36+
37+
#define ROTR32(x, y) (((x) >> (y)) | ((x) << (32 - (y))))
38+
39+
#define G(r, i, a, b, c, d) \
40+
do { \
41+
a = a + b + m[blake2s_sigma[r][2*i+0]]; \
42+
d = ROTR32(d ^ a, 16); \
43+
c = c + d; \
44+
b = ROTR32(b ^ c, 12); \
45+
a = a + b + m[blake2s_sigma[r][2*i+1]]; \
46+
d = ROTR32(d ^ a, 8); \
47+
c = c + d; \
48+
b = ROTR32(b ^ c, 7); \
49+
} while (0)
50+
51+
typedef struct {
52+
uint8_t b[BLAKE2S_BLOCKBYTES];
53+
uint32_t h[BLAKE2S_STATE_SIZE];
54+
uint32_t t[2];
55+
uint32_t f[2];
56+
size_t buflen;
57+
size_t outlen;
58+
const uint8_t *last_node;
59+
} blake2s_state;
60+
61+
static void blake2s_compress(blake2s_state *S, const uint8_t block[BLAKE2S_BLOCKBYTES]);
62+
int blake2s_init(blake2s_state *S, size_t outlen, const void *key, size_t keylen);
63+
int blake2s_update(blake2s_state *S, const void *pin, size_t inlen);
64+
int blake2s_final(blake2s_state *S, void *out, size_t outlen);
65+
66+
#endif /* blake2_h */

Sources/CCrypto/include/sha256.h

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//===-- CCrypto/include/sha256.h - SHA256 ALGORYTHM ------------ -*- C -*-===//
2+
// //
3+
// This source file is part of the Scribble Foundation open source project //
4+
// //
5+
// Copyright (c) 2024 ScribbleLabApp. and the ScribbleLab project authors //
6+
// Licensed under Apache License v2.0 with Runtime Library Exception //
7+
// //
8+
// You may not use this file except in compliance with the License. //
9+
// You may obtain a copy of the License at //
10+
// //
11+
// http://www.apache.org/licenses/LICENSE-2.0 //
12+
// //
13+
// Unless required by applicable law or agreed to in writing, software //
14+
// distributed under the License is distributed on an "AS IS" BASIS, //
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
16+
// See the License for the specific language governing permissions and //
17+
// limitations under the License. //
18+
// //
19+
//===----------------------------------------------------------------------===//
20+
///
21+
/// \file
22+
/// This file provides functions for generating and printing SHA-256 and SHA-512
23+
/// hashes. It includes a simple implementation of the
24+
/// SHA-256 hash algorithm.
25+
///
26+
//===----------------------------------------------------------------------===//
27+
28+
#ifndef sha256_h
29+
#define sha256_h
30+
31+
#include <stdio.h>
32+
#include <stdint.h>
33+
#include <string.h>
34+
#include <stddef.h>
35+
36+
#define CY_SHA256_SUCCESS 0 ///< Success code for SHA-256 operations
37+
#define CY_ERR_SHA256_NULL_PTR -6 ///< Error code for null pointer
38+
#define CY_ERR_SHA256_INVALID_LEN -7 ///< Error code for invalid length
39+
40+
/// \def ROTR
41+
/// Rotate right macro
42+
/// \param x Input value
43+
/// \param n Number of bits to rotate
44+
#define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
45+
46+
/// \def Ch
47+
/// Choice function used in SHA-256
48+
/// \param x Input value
49+
/// \param y Input value
50+
/// \param z Input value
51+
#define Ch(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
52+
53+
/// \def Maj
54+
/// Majority function used in SHA-256
55+
/// \param x Input value
56+
/// \param y Input value
57+
/// \param z Input value
58+
#define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
59+
60+
/// \def Sigma0
61+
/// Sigma0 function used in SHA-256
62+
/// \param x Input value
63+
#define Sigma0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
64+
65+
/// \def Sigma1
66+
/// Sigma1 function used in SHA-256
67+
/// \param x Input value
68+
#define Sigma1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
69+
70+
/// \def sigma0
71+
/// sigma0 function used in SHA-256
72+
/// \param x Input value
73+
#define sigma0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ ((x) >> 3))
74+
75+
/// \def sigma1
76+
/// sigma1 function used in SHA-256
77+
/// \param x Input value
78+
#define sigma1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ ((x) >> 10))
79+
80+
extern const uint32_t K[64];
81+
extern const uint32_t H256[8];
82+
83+
/// Compresses a single 512-bit block of input data using the SHA-256 algorithm.
84+
///
85+
/// - Parameters:
86+
/// - state: The current hash state (8 x 32-bit words).
87+
/// - block: The 512-bit block of input data (64 bytes).
88+
void sha256_compress(uint32_t state[8], const unsigned char block[64]);
89+
90+
/// Computes the SHA-256 hash of the input data.
91+
///
92+
/// - Parameters:
93+
/// - input: Pointer to the input data.
94+
/// - len: Length of the input data in bytes.
95+
/// - output: Pointer to the buffer where the computed hash will be stored (32 bytes).
96+
///
97+
/// - Returns:
98+
/// - CY_SHA256_SUCCESS (0) on success.
99+
/// - CY_ERR_SHA256_NULL_PTR (-6) if a null pointer is provided.
100+
/// - CY_ERR_SHA256_INVALID_LEN (-7) if the input length is invalid.
101+
int sha256_hash(const unsigned char *input, size_t len, unsigned char *output);
102+
103+
#endif /* sha256_h */

Sources/CCrypto/include/sha3.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//===-- CCrypto/include/sha3.h - SHA-3 ALGORITHM ------------------*- C -*-===//
2+
// //
3+
// This source file is part of the Scribble Foundation open source project //
4+
// //
5+
// Copyright (c) 2024 ScribbleLabApp. and the ScribbleLab project authors //
6+
// Licensed under Apache License v2.0 with Runtime Library Exception //
7+
// //
8+
// You may not use this file except in compliance with the License. //
9+
// You may obtain a copy of the License at //
10+
// //
11+
// http://www.apache.org/licenses/LICENSE-2.0 //
12+
// //
13+
// Unless required by applicable law or agreed to in writing, software //
14+
// distributed under the License is distributed on an "AS IS" BASIS, //
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
16+
// See the License for the specific language governing permissions and //
17+
// limitations under the License. //
18+
// //
19+
//===----------------------------------------------------------------------===//
20+
///
21+
/// \file
22+
/// This file provides functions for generating and printing SHA-3 hash values.
23+
///
24+
//===----------------------------------------------------------------------===//
25+
26+
#ifndef sha3_h
27+
#define sha3_h
28+
29+
#include <stdint.h>
30+
#include <string.h>
31+
32+
#define SHA3_SUCCESS 0 ///< Success code for SHA-3 operations.
33+
#define SHA3_NULL_INPUT -1 ///< Error code for null input parameter.
34+
#define SHA3_INVALID_LENGTH -2 ///< Error code for invalid input length.
35+
#define SHA3_MEMORY_ERROR -3
36+
37+
/// SHA-3 context structure.
38+
typedef struct {
39+
uint64_t state[25]; ///< Keccak state (1600 bits = 25 * 64 bits).
40+
uint64_t bitlen; ///< Total message bit length.
41+
unsigned int r; ///< Rate in bytes.
42+
unsigned int capacity; ///< Capacity in bytes.
43+
unsigned int hashlen; ///< Hash output length in bytes.
44+
unsigned char *buffer; ///< Buffer for data block.
45+
} sha3_ctx_t;
46+
47+
/// Initialize SHA-3 context for given hash length.
48+
/// \param ctx SHA-3 context structure to be initialized.
49+
/// \param hashlen Length of the hash output in bytes (224, 256, 384, or 512).
50+
/// \return SHA3_SUCCESS on success, SHA3_INVALID_LENGTH if hashlen is not valid.
51+
int sha3_init(sha3_ctx_t *ctx, unsigned int hashlen);
52+
53+
/// Update SHA-3 context with input data.
54+
/// \param ctx SHA-3 context structure.
55+
/// \param data Input data buffer.
56+
/// \param len Length of the input data buffer in bytes.
57+
/// \return SHA3_SUCCESS on success, SHA3_NULL_INPUT if ctx or data is NULL.
58+
int sha3_update(sha3_ctx_t *ctx, const unsigned char *data, size_t len);
59+
60+
/// Finalize SHA-3 hash computation and output the hash value.
61+
/// \param ctx SHA-3 context structure.
62+
/// \param hash Output buffer to store the hash value (must be pre-allocated).
63+
/// \return SHA3_SUCCESS on success, SHA3_NULL_INPUT if ctx or hash is NULL.
64+
int sha3_final(sha3_ctx_t *ctx, unsigned char *hash);
65+
66+
/// Compute SHA-3 hash directly from input data.
67+
/// \param data Input data buffer.
68+
/// \param len Length of the input data buffer in bytes.
69+
/// \param hash Output buffer to store the hash value (must be pre-allocated).
70+
/// \param hashlen Length of the hash output in bytes (224, 256, 384, or 512).
71+
/// \return SHA3_SUCCESS on success, SHA3_NULL_INPUT if data or hash is NULL,
72+
/// SHA3_INVALID_LENGTH if hashlen is not valid.
73+
int sha3(const unsigned char *data, size_t len, unsigned char *hash, unsigned int hashlen);
74+
75+
static void KeccakF1600_StatePermute(uint64_t state[25]);
76+
static void KeccakF(uint64_t state[25]);
77+
78+
static const uint64_t KeccakF_RoundConstants[24];
79+
static const unsigned int KeccakF_RoundConstantsSize;
80+
static const unsigned int KeccakF_RotationOffsets[24];
81+
static const unsigned int KeccakF_PiLane[25];
82+
static const unsigned int KeccakF_Mod5[25];
83+
84+
#endif /* sha3_h */

0 commit comments

Comments
 (0)