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

Commit a3319e7

Browse files
committed
(feat): Added SHA 256 & 512 Algorithm
1 parent 23d9063 commit a3319e7

File tree

12 files changed

+644
-10
lines changed

12 files changed

+644
-10
lines changed

Sources/CryptoSwiftWrapper/CryptoSwiftWrapper.h

+8
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@
1818
// //
1919
//===----------------------------------------------------------------------===//
2020

21+
#ifndef CryptoSwiftWrapper_h
22+
#define CryptoSwiftWrapper_h
23+
2124
#include "CryptoSwiftWrapper/cyfn.h"
25+
#include "CryptoSwiftWrapper/sha256.h"
26+
#include "CryptoSwiftWrapper/sha512.h"
27+
#include "CryptoSwiftWrapper/sha.h"
28+
29+
#endif /* CryptoSwiftWrapper_h */

Sources/CryptoSwiftWrapper/Documentation.docc/CYErrors.md

+28-4
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,42 @@ The CYErrors constants provide a standardized way to handle errors encountered d
88

99
### Error Codes
1010

11-
#### `CY_ERR_GENKEY` (-100)
11+
#### `CY_ERR_GENKEY` (-1)
1212

1313
Error code indicating key generation failure.
1414

15-
#### `CY_ERR_ENCR` (-101)
15+
#### `CY_ERR_ENCR` (-2)
1616

1717
Error code indicating encryption failure.
1818

19-
#### `CY_ERR_DECR` (-102)
19+
#### `CY_ERR_DECR` (-3)
2020

2121
Error code indicating decryption failure.
2222

23-
#### `CY_ERR_INIT` (-103)
23+
#### `CY_ERR_INIT` (-4)
2424

2525
Error code indicating initialization failure.
26+
27+
#### `CY_ERR_SHA256_NULL_PTR` (-6)
28+
29+
Error code indicating a null pointer error in SHA256 operations.
30+
31+
#### `CY_ERR_SHA256_INVALID_LEN` (-7)
32+
33+
Error code indicating an invalid length error in SHA256 operations.
34+
35+
#### `SHA512_NULL_INPUT` (-8)
36+
37+
Error code indicating null input parameter in SHA512 operations.
38+
39+
#### `SHA512_NULL_OUTPUT` (-9)
40+
41+
Error code indicating null output parameter in SHA512 operations.
42+
43+
#### `SHA512_MEMORY_ERROR` (-10)
44+
45+
Error code indicating memory allocation failure in SHA512 operations.
46+
47+
#### `SHA512_INVALID_LENGTH` (-11)
48+
49+
Error code indicating invalid input length in SHA512 operations.

Sources/CryptoSwiftWrapper/Documentation.docc/Documentation.md

+4
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ Prefer CryptoSwiftWrapper for its simplicity and integration with Swift. It abst
4040

4141
- ``encrypt_data(plaintext:plaintext_len:key:iv:ciphertext:)``
4242
- ``decrypt_data(ciphertext:ciphertext_len:key:iv:tag:tag_len:plaintext:)``
43+
44+
### SHA 256 & 512
45+
46+
- <doc:SHA_IMPL>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# SHA Algorithm Implementation
2+
3+
Implementing SHA hashing algorithm functions in both C and Swift
4+
5+
## Overview
6+
7+
The SHA-256 and SHA-512 algorithms are implemented in C within this package to provide robust cryptographic hashing functionalities. These algorithms ensure data integrity and security through their respective 256-bit and 512-bit hash outputs.
8+
9+
### Key Components
10+
11+
- **Constants and Macros:** The implementations utilize predefined constants and macros for bitwise operations and block processing, enhancing efficiency and clarity in the code.
12+
- **Message Schedule Processing:** Both algorithms employ a message schedule structure (`SHA256_Message_Schedule` and `SHA512_Message_Schedule`) to manage data flow and perform iterative hash computations over data blocks.
13+
14+
- **Compression Function:** The `sha256_compress` and `sha512_compress` functions handle the core compression steps, combining message schedule data with current state values to produce updated hash values.**
15+
16+
- **Error Handling:** Custom error codes (`SHA512_NULL_INPUT`, `SHA512_NULL_OUTPUT`, `SHA512_MEMORY_ERROR`, `SHA512_INVALID_LENGTH`, etc.) are defined to manage exceptional scenarios like null inputs, memory allocation failures, and invalid data lengths.
17+
18+
These SHA implementations are integrated into larger systems requiring secure hash computations, such as cryptographic protocols, digital signatures, and data integrity verification mechanisms. They provide essential tools for ensuring data confidentiality and authenticity in software applications.
19+
20+
### Background
21+
SHA-256 is a widely-used cryptographic hash function that generates a 256-bit hash value, ensuring data integrity and security in various applications. Implemented efficiently in C, it employs bitwise operations and constants to process data blocks, providing robust hashing capabilities.
22+
23+
Whereas SHA-256 is more often used SHA-512 is a stronger variant of SHA-256, producing a 512-bit hash value. Its implementation in C utilizes similar principles but operates on larger data blocks, enhancing security and resistance to cryptographic attacks. It supports applications requiring higher security standards and larger hash outputs.
24+
25+
@Comment {
26+
### Implementation
27+
28+
@TabNavigator {
29+
@Tab("Swift") {}
30+
@Tab("C") {}
31+
}
32+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===-- CryptoSwiftWrapper/include/sha.h - Bridging ------------ -*- 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+
// This is a bridging header for SHA 256 and 512 implementations
22+
23+
#ifndef sha_h
24+
#define sha_h
25+
26+
#include "sha256.h"
27+
#include "sha512.h"
28+
29+
#endif /* sha_h */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===-- CryptoSwiftWrapper/include/sha256.h - Bridging --------- -*- 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+
// This is a bridging header for sha256.h to be usable from "CryptoSwiftWrapper/sha256.h"
22+
23+
#ifndef sha256_h
24+
#define sha256_h
25+
26+
// Include the original _cyfn/sha256.h header
27+
#include "_cyfn/sha256.h"
28+
29+
#endif /* sha256_h */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===-- CryptoSwiftWrapper/include/sha512.h - Bridging --------- -*- 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+
// This is a bridging header for sha256.h to be usable from "CryptoSwiftWrapper/sha512.h"
22+
23+
#ifndef sha512_h
24+
#define sha512_h
25+
26+
// Include the original _cyfn/sha512.h header
27+
#include "_cyfn/sha512.h"
28+
29+
#endif /* sha512_h */

Sources/_cyfn/cyfn.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@
5656
#define AES_KEY_SIZE 256
5757
#define AES_BLOCK_SIZE 16
5858

59-
#define CY_ERR_GENKEY -100 ///< Error code indicating key generation failure.
60-
#define CY_ERR_ENCR -101 ///< Error code indicating encryption failure.
61-
#define CY_ERR_DECR -102 ///< Error code indicating decryption failure.
62-
#define CY_ERR_INIT -103 ///< Error code indicating initialization failure.
63-
#define CY_ERR_OSSL -104 ///< Error code indicating an OpenSSL error.
59+
#define CY_ERR_GENKEY -1 ///< Error code indicating key generation failure.
60+
#define CY_ERR_ENCR -2 ///< Error code indicating encryption failure.
61+
#define CY_ERR_DECR -3 ///< Error code indicating decryption failure.
62+
#define CY_ERR_INIT -4 ///< Error code indicating initialization failure.
63+
#define CY_ERR_OSSL -5 ///< Error code indicating an OpenSSL error.
6464

6565
#ifdef __cplusplus
6666
extern "C" {

Sources/_cyfn/cyfnBridging.h

+15
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 cyfnBridging_h
9+
#define cyfnBridging_h
10+
11+
#include "cyfn.h"
12+
#include "sha256.h"
13+
#include "sha512.h"
14+
15+
#endif /* cyfnBridging_h */

Sources/_cyfn/module.modulemap

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

22
module _cyfn {
3-
header "cyfn.h"
3+
header "cyfnBridging.h"
44
export *
55
}

0 commit comments

Comments
 (0)