Skip to content

Commit

Permalink
Release 3.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
leif-ibsen committed May 2, 2022
1 parent b7cb66e commit 3fccb04
Show file tree
Hide file tree
Showing 41 changed files with 427 additions and 59 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ SwiftECC requires Swift 5.0. It also requires that the Int and UInt types be 64
In your project Package.swift file add a dependency like<br/>

dependencies: [
.package(url: "https://github.com/leif-ibsen/SwiftECC", from: "3.3.0"),
.package(url: "https://github.com/leif-ibsen/SwiftECC", from: "3.4.0"),
]

<h2 id="basic"><b>Basics</b></h2>
Expand Down Expand Up @@ -465,39 +465,41 @@ Nonce = bytes 32 ..< 44</br>
<h4><b>AES-128/GCM block mode</b></h4>
KDF generates 32 bytes.

Encryption/decryption key = bytes 0 ..< 16</br>
AES encryption/decryption key = bytes 0 ..< 16</br>
Initialization vector = bytes 16 ..< 32</br>

<h4><b>AES-192/GCM block mode</b></h4>
KDF generates 40 bytes.

Encryption/decryption key = bytes 0 ..< 24</br>
AES encryption/decryption key = bytes 0 ..< 24</br>
Initialization vector = bytes 24 ..< 40</br>

<h4><b>AES-256/GCM block mode</b></h4>
KDF generates 48 bytes.

Encryption/decryption key = bytes 0 ..< 32</br>
AES encryption/decryption key = bytes 0 ..< 32</br>
Initialization vector = bytes 32 ..< 48</br>

<h4><b>AES-128/Non-GCM block mode</b></h4>
KDF generates 48 bytes.

Encryption/decryption key = bytes 0 ..< 16</br>
AES encryption/decryption key = bytes 0 ..< 16</br>
HMAC key = bytes 16 ..< 48</br>

<h4><b>AES-192/Non-GCM block mode</b></h4>
KDF generates 56 bytes.

Encryption/decryption key = bytes 0 ..< 24</br>
AES encryption/decryption key = bytes 0 ..< 24</br>
HMAC key = bytes 24 ..< 56</br>

<h4><b>AES-256/Non-GCM block mode</b></h4>
KDF generates 64 bytes.

Encryption/decryption key = bytes 0 ..< 32</br>
AES encryption/decryption key = bytes 0 ..< 32</br>
HMAC key = bytes 32 ..< 64</br>

The AES key and HMAC key can be retrieved with the ECPrivateKey method 'getKeyAndMac'.

For block modes CBC, CFB, CTR, and OFB the initialization vector (IV) is 16 zero bytes.

<h2 id="perf"><b>Performance</b></h2>
Expand Down
49 changes: 47 additions & 2 deletions Sources/SwiftECC/PrivateKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -346,16 +346,20 @@ public class ECPrivateKey: CustomStringConvertible {
/// - Returns: The decrypted message
/// - Throws: An exception if message authentication fails or the message is too short
public func decrypt(msg: Bytes, cipher: AESCipher, mode: BlockMode = .GCM) throws -> Bytes {
// [GUIDE] - algorithm 4.43
let bwl = 2 * ((self.domain.p.bitWidth + 7) / 8) + 1
let tagLength = mode == .GCM ? 16 : 32
if msg.count < bwl + tagLength {
throw ECException.notEnoughInput
}
let R = Bytes(msg[0 ..< bwl])
let S = try self.domain.multiplyPoint(self.domain.decodePoint(R), self.s).x
let S = try self.domain.multiplyPoint(self.domain.decodePoint(R), self.s)
if S.infinity {
throw ECException.authentication
}
let tag1 = Bytes(msg[msg.count - tagLength ..< msg.count])
var result = Bytes(msg[bwl ..< msg.count - tagLength])
let cipher = Cipher.instance(cipher, mode, self.domain.align(S.asMagnitudeBytes()), R)
let cipher = Cipher.instance(cipher, mode, self.domain.align(S.x.asMagnitudeBytes()), R)
let tag2 = try cipher.decrypt(&result)
if tag1 == tag2 {
return result
Expand All @@ -375,6 +379,47 @@ public class ECPrivateKey: CustomStringConvertible {
return try Data(self.decrypt(msg: Bytes(msg), cipher: cipher, mode: mode))
}

/// Returns the AES key and HMAC key that were used to encrypt the message
///
/// - Parameters:
/// - msg: The encrypted data
/// - cipher: The AES cipher that was used to encrypt
/// - mode: The block mode that was used to encrypt - GCM is default
/// - Returns: The AES key and HMAC key that were used during encryption
/// - Throws: An exception if the message is too short
public func getKeyAndMac(msg: Bytes, cipher: AESCipher, mode: BlockMode = .GCM) throws -> (key: Bytes, mac: Bytes) {
let bwl = 2 * ((self.domain.p.bitWidth + 7) / 8) + 1
let tagLength = mode == .GCM ? 16 : 32
if msg.count < bwl + tagLength {
throw ECException.notEnoughInput
}
let R = Bytes(msg[0 ..< bwl])
let S = try self.domain.multiplyPoint(self.domain.decodePoint(R), self.s).x
var keySize: Int
switch cipher {
case .AES128:
keySize = AES.keySize128
case .AES192:
keySize = AES.keySize192
case .AES256:
keySize = AES.keySize256
}
return Cipher.kdf(keySize, tagLength, self.domain.align(S.asMagnitudeBytes()), R)
}

/// Returns the AES key and HMAC key that were used to encrypt the message
///
/// - Parameters:
/// - msg: The encrypted data
/// - cipher: The AES cipher that was used to encrypt
/// - mode: The block mode that was used to encrypt - GCM is default
/// - Returns: The AES key and HMAC key that were used during encryption
/// - Throws: An exception if the message is too short
public func getKeyAndMac(msg: Data, cipher: AESCipher, mode: BlockMode = .GCM) throws -> (key: Data, mac: Data) {
let (key, mac) = try self.getKeyAndMac(msg: Bytes(msg), cipher: cipher, mode: mode)
return (Data(key), Data(mac))
}

/// Decrypts a byte array message with ECIES using the ChaCha20 cipher
///
/// - Parameters:
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftECC/PublicKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ public class ECPublicKey: CustomStringConvertible {
/// - mode: The block mode to use - GCM is default
/// - Returns: The encrypted message
public func encrypt(msg: Bytes, cipher: AESCipher, mode: BlockMode = .GCM) -> Bytes {
// [GUIDE] - algorithm 4.42
let (R, S) = computeRS()
let cipher = Cipher.instance(cipher, mode, S, R)
var result = msg
Expand Down
2 changes: 1 addition & 1 deletion docs/Classes.html
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ <h4>Declaration</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2022 <a class="link" href="" target="_blank" rel="external noopener"></a>. All rights reserved. (Last updated: 2022-04-05)</p>
<p>&copy; 2022 <a class="link" href="" target="_blank" rel="external noopener"></a>. All rights reserved. (Last updated: 2022-05-02)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.2</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
</section>
</article>
Expand Down
2 changes: 1 addition & 1 deletion docs/Classes/Domain.html
Original file line number Diff line number Diff line change
Expand Up @@ -1837,7 +1837,7 @@ <h4>Return Value</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2022 <a class="link" href="" target="_blank" rel="external noopener"></a>. All rights reserved. (Last updated: 2022-04-05)</p>
<p>&copy; 2022 <a class="link" href="" target="_blank" rel="external noopener"></a>. All rights reserved. (Last updated: 2022-05-02)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.2</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
</section>
</article>
Expand Down
160 changes: 159 additions & 1 deletion docs/Classes/ECPrivateKey.html
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,164 @@ <h4>Return Value</h4>
</section>
</div>
</li>
<li class="item">
<div>
<code>
<a name="/s:8SwiftECC12ECPrivateKeyC03getD6AndMac3msg6cipher4modeSays5UInt8VG3key_AJ3mactAJ_AA9AESCipherOAA9BlockModeOtKF"></a>
<a name="//apple_ref/swift/Method/getKeyAndMac(msg:cipher:mode:)" class="dashAnchor"></a>
<a class="token" href="#/s:8SwiftECC12ECPrivateKeyC03getD6AndMac3msg6cipher4modeSays5UInt8VG3key_AJ3mactAJ_AA9AESCipherOAA9BlockModeOtKF">getKeyAndMac(msg:<wbr>cipher:<wbr>mode:<wbr>)</a>
</code>
</div>
<div class="height-container">
<div class="pointer-container"></div>
<section class="section">
<div class="pointer"></div>
<div class="abstract">
<p>Returns the AES key and HMAC key that were used to encrypt the message</p>
<div class="aside aside-throws">
<p class="aside-title">Throws</p>
An exception if the message is too short

</div>

</div>
<div class="declaration">
<h4>Declaration</h4>
<div class="language">
<p class="aside-title">Swift</p>
<pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">func</span> <span class="nf">getKeyAndMac</span><span class="p">(</span><span class="nv">msg</span><span class="p">:</span> <span class="kt"><a href="../Typealiases.html#/s:8SwiftECC5Bytesa">Bytes</a></span><span class="p">,</span> <span class="nv">cipher</span><span class="p">:</span> <span class="kt"><a href="../Enums/AESCipher.html">AESCipher</a></span><span class="p">,</span> <span class="nv">mode</span><span class="p">:</span> <span class="kt"><a href="../Enums/BlockMode.html">BlockMode</a></span> <span class="o">=</span> <span class="o">.</span><span class="kt">GCM</span><span class="p">)</span> <span class="k">throws</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="nv">key</span><span class="p">:</span> <span class="kt"><a href="../Typealiases.html#/s:8SwiftECC5Bytesa">Bytes</a></span><span class="p">,</span> <span class="nv">mac</span><span class="p">:</span> <span class="kt"><a href="../Typealiases.html#/s:8SwiftECC5Bytesa">Bytes</a></span><span class="p">)</span></code></pre>

</div>
</div>
<div>
<h4>Parameters</h4>
<table class="graybox">
<tbody>
<tr>
<td>
<code>
<em>msg</em>
</code>
</td>
<td>
<div>
<p>The encrypted data</p>
</div>
</td>
</tr>
<tr>
<td>
<code>
<em>cipher</em>
</code>
</td>
<td>
<div>
<p>The AES cipher that was used to encrypt</p>
</div>
</td>
</tr>
<tr>
<td>
<code>
<em>mode</em>
</code>
</td>
<td>
<div>
<p>The block mode that was used to encrypt - GCM is default</p>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div>
<h4>Return Value</h4>
<p>The AES key and HMAC key that were used during encryption</p>
</div>
</section>
</div>
</li>
<li class="item">
<div>
<code>
<a name="/s:8SwiftECC12ECPrivateKeyC03getD6AndMac3msg6cipher4mode10Foundation4DataV3key_AJ3mactAJ_AA9AESCipherOAA9BlockModeOtKF"></a>
<a name="//apple_ref/swift/Method/getKeyAndMac(msg:cipher:mode:)" class="dashAnchor"></a>
<a class="token" href="#/s:8SwiftECC12ECPrivateKeyC03getD6AndMac3msg6cipher4mode10Foundation4DataV3key_AJ3mactAJ_AA9AESCipherOAA9BlockModeOtKF">getKeyAndMac(msg:<wbr>cipher:<wbr>mode:<wbr>)</a>
</code>
</div>
<div class="height-container">
<div class="pointer-container"></div>
<section class="section">
<div class="pointer"></div>
<div class="abstract">
<p>Returns the AES key and HMAC key that were used to encrypt the message</p>
<div class="aside aside-throws">
<p class="aside-title">Throws</p>
An exception if the message is too short

</div>

</div>
<div class="declaration">
<h4>Declaration</h4>
<div class="language">
<p class="aside-title">Swift</p>
<pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">func</span> <span class="nf">getKeyAndMac</span><span class="p">(</span><span class="nv">msg</span><span class="p">:</span> <span class="kt">Data</span><span class="p">,</span> <span class="nv">cipher</span><span class="p">:</span> <span class="kt"><a href="../Enums/AESCipher.html">AESCipher</a></span><span class="p">,</span> <span class="nv">mode</span><span class="p">:</span> <span class="kt"><a href="../Enums/BlockMode.html">BlockMode</a></span> <span class="o">=</span> <span class="o">.</span><span class="kt">GCM</span><span class="p">)</span> <span class="k">throws</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="nv">key</span><span class="p">:</span> <span class="kt">Data</span><span class="p">,</span> <span class="nv">mac</span><span class="p">:</span> <span class="kt">Data</span><span class="p">)</span></code></pre>

</div>
</div>
<div>
<h4>Parameters</h4>
<table class="graybox">
<tbody>
<tr>
<td>
<code>
<em>msg</em>
</code>
</td>
<td>
<div>
<p>The encrypted data</p>
</div>
</td>
</tr>
<tr>
<td>
<code>
<em>cipher</em>
</code>
</td>
<td>
<div>
<p>The AES cipher that was used to encrypt</p>
</div>
</td>
</tr>
<tr>
<td>
<code>
<em>mode</em>
</code>
</td>
<td>
<div>
<p>The block mode that was used to encrypt - GCM is default</p>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div>
<h4>Return Value</h4>
<p>The AES key and HMAC key that were used during encryption</p>
</div>
</section>
</div>
</li>
<li class="item">
<div>
<code>
Expand Down Expand Up @@ -1338,7 +1496,7 @@ <h4>Return Value</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2022 <a class="link" href="" target="_blank" rel="external noopener"></a>. All rights reserved. (Last updated: 2022-04-05)</p>
<p>&copy; 2022 <a class="link" href="" target="_blank" rel="external noopener"></a>. All rights reserved. (Last updated: 2022-05-02)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.2</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
</section>
</article>
Expand Down
2 changes: 1 addition & 1 deletion docs/Classes/ECPublicKey.html
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ <h4>Return Value</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2022 <a class="link" href="" target="_blank" rel="external noopener"></a>. All rights reserved. (Last updated: 2022-04-05)</p>
<p>&copy; 2022 <a class="link" href="" target="_blank" rel="external noopener"></a>. All rights reserved. (Last updated: 2022-05-02)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.2</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
</section>
</article>
Expand Down
2 changes: 1 addition & 1 deletion docs/Classes/ECSignature.html
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ <h4>Declaration</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2022 <a class="link" href="" target="_blank" rel="external noopener"></a>. All rights reserved. (Last updated: 2022-04-05)</p>
<p>&copy; 2022 <a class="link" href="" target="_blank" rel="external noopener"></a>. All rights reserved. (Last updated: 2022-05-02)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.2</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
</section>
</article>
Expand Down
2 changes: 1 addition & 1 deletion docs/Enums.html
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ <h4>Declaration</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2022 <a class="link" href="" target="_blank" rel="external noopener"></a>. All rights reserved. (Last updated: 2022-04-05)</p>
<p>&copy; 2022 <a class="link" href="" target="_blank" rel="external noopener"></a>. All rights reserved. (Last updated: 2022-05-02)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.2</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
</section>
</article>
Expand Down
2 changes: 1 addition & 1 deletion docs/Enums/AESCipher.html
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ <h4>Declaration</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2022 <a class="link" href="" target="_blank" rel="external noopener"></a>. All rights reserved. (Last updated: 2022-04-05)</p>
<p>&copy; 2022 <a class="link" href="" target="_blank" rel="external noopener"></a>. All rights reserved. (Last updated: 2022-05-02)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.2</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
</section>
</article>
Expand Down
Loading

0 comments on commit 3fccb04

Please sign in to comment.